一个简单的UVM验证平台

本文详细介绍了如何构建一个简单的UVM验证平台,包括加入driver、transaction、env、monitor、agent、reference model、scoreboard、field_automation机制、sequencer以及base_test。讲解了UVM组件之间的交互、transaction的使用、工厂机制、接口管理和测试流程,旨在帮助读者理解UVM验证方法学。
摘要由CSDN通过智能技术生成

1.

SystemC是基于C++的,用户需要自己管理内存,容易发生内存泄漏[^内存泄漏指的是程序中已动态分配的堆内存由于某种原因程序未释放或无法释放,造成系统内存的浪费,导致程序运行速度减慢甚至系统崩溃]。

而SystemVerilog则不存在这个问题,而且与Verilog完全兼容,提供DPI接口,而且本身自带内存管理机制,不用担心内存泄漏问题。还支持系统函数调用,可以直接调用可执行程序,即外部已经用C写好的参考模型。

2.

验证平台大致包括如下几个部分

  • driver:实现激励。

  • scoreboard:用于将DUT与参考模型进行比较。

  • monitor:用于收集DUT的输出并传递给scoreboard。

3.

一个典型的UVM验证平台

image-20200927093135563

4.

使用UVM的第一条原则是:验证平台中所有的组件应该派生自UVM中的类。

加入driver

1.

先列出我们下面要讲的dut,这里是一个简单的数据接收器。

module dut(clk,rst_n,rxd,rx_dv,txd,tx_en);
	input clk;
	input rst_n;
	input[7:0] rxd;
	input rx_dv;
	output [7:0] txd;
	output tx_en;
	reg[7:0] txd;
	reg tx_en;
always @(posedge clk) begin
	if(!rst_n) begin
		txd <= 8'b0;
		tx_en <= 1'b0;
	end
	else begin
		txd <= rxd;
		tx_en <= rx_dv;
	end
end
endmodule

2.

这里的driver类用于发送激励,派生自uvm_driver,这里的构造new函数有两个参数,一个是string类型的name,即类名,另个是uvm_component类型的parent。这里uvm_driver是一个派生自uvm_component的类,而每一个派生自uvm_component的类都要指明nameparnent这两个参数。

具体代码如下:

class my_driver extends uvm_driver;
	function new(string name = "my_driver", uvm_component parent = null);
		super.new(name, parent);
	endfunction
	extern virtual task main_phase(uvm_phase phase);
endclass
        
task my_driver::main_phase(uvm_phase phase);
	top_tb.rxd <= 8'b0;
	top_tb.rx_dv <= 1'b0;
	while(!top_tb.rst_n)
	@(posedge top_tb.clk);
	for(int i = 0; i < 256; i++)begin
		@(posedge top_tb.clk);
		top_tb.rxd <= $urandom_range(0, 255);
		top_tb.rx_dv <= 1'b1;
		`uvm_info("my_driver", "data is drived", UVM_LOW)
	end
	@(posedge top_tb.clk);
	top_tb.rx_dv <= 1'b0;
endtask

3.

UVM用phase来管理验证平台的运行,类型为uvm_phase,而名称一般为xxxx_phase,这里driver所做的事情几乎都在main_phase中完成。这里实现一个driver就等于是实现其main_phase

4.

这里的代码还使用了uvm_info宏,主要完成信息打印,但比$display语句更强大,它有三个参数,第一个参数是字符串,用于把打印信息进行归类,第二个参数也是字符串,是具体需要打印的信息,第三个参数是冗余级别。对于一些关键的信息需要打印出来的,可以设置为UVM_LOW,而对于一些可有可无的信息,则可以设置为UVM_HIGH,默认情况下是UVM_MEDIUM,因此默认情况下只显示UVM_MEDIUMUVM_LOW的信息。

这里打印的结果如下所示:

UVM_INFO my_driver.sv(20)@48500000:drv[my_driver]data is drived
  • UVM_INFO关键字:表明这是一个uvm_info宏打印的结果。

  • my_driver.sv(20):指明此条打印信息的来源,其中括号里的数字表示原始的uvm_info打印语句在my_driver.sv中的行号。

  • 48500000:表明此条信息的打印时间。

  • drv:这是driver在UVM树中的路径索引。

    这里的路径索引还可以通过函数get_full_name()来获取,使用方式如下:

    $display("the full name of current component is: %s", get_full_name());
    

因此建议使用宏uvm_info来代替以前的$display语句。

5.

下面对my_driver实例化并且最终搭建的验证平台如下:

`timescale 1ns/1ps
`include "uvm_macros.svh"//UVM宏定义文件
import uvm_pkg::*;//UVM类库文件
`include "my_driver.sv"
module top_tb;
	reg clk;
	reg rst_n;
	reg[7:0] rxd;
	reg rx_dv;
	wire[7:0] txd;
	wire tx_en;
dut my_dut(.clk(clk),
	.rst_n(rst_n),
	.rxd(rxd),
	.rx_dv(rx_dv),
	.txd(txd),
	.tx_en(tx_en));
initial begin
	my_driver drv;
	drv = new("drv", null);
	drv.main_phase(null);
	$finish();
end
initial begin
	clk = 0;
	forever begin
		#100 clk = ~clk;
	end
end
initial begin
	rst_n = 1'b0;
    #1000;
	rst_n = 1'b1;
end
endmodule

6.

接下来我们来为driver加入factory机制,即加入宏uvm_component_utils,主要完成将my_driver登记注册的功能。

加入后的driver代码如下:

class my_driver extends uvm_driver;
`uvm_component_utils(my_driver)
function new(string name = "my_driver", uvm_component parent = null);
	super.new(name, parent);
	`uvm_info("my_driver", "new is called", UVM_LOW);
endfunction
extern virtual task main_phase(uvm_phase phase);
endclass
task my_driver::main_phase(uvm_phase phase);
	`uvm_info("my_driver", "main_phase is called", UVM_LOW);
	top_tb.rxd <= 8'b0;
	top_tb.rx_dv <= 1'b0;
	while(!top_tb.rst_n)
	@(posedge top_tb.clk);
	for(int i = 0; i < 256; i++)begin
		@(posedge top_tb.clk);
		top_tb.rxd <= $urandom_range(0, 255);
		top_tb.rx_dv <= 1'b1;
		`uvm_info("my_driver", "data is drived", UVM_LOW);
	end
	@(posedge top_tb.clk);
	top_tb.rx_dv <= 1'b0;
endtask    

同时还要对top_tb顶层做一些修改。

module top_tb;
    ...
	initial begin
		run_test("my_driver");
	end
endmodule

修改完成后,原先的顶层代码的下面这部分就可以删掉了。即一句run_test(my_driver);就完成了对my_driver类的例化和main_phase的自动调用。

initial begin
	my_driver drv;
	drv = new("drv", null);
	drv.main_phase(null);
	$finish();
end

因此这里的uvm_component_utils其实完成了根据传递的字符串(类名)来创建了一个实例。

注意,所有派生自uvm_component及其派生类的类都应该使用uvm_component_utils宏注册。

initial begin
	my_driver drv;
	drv = new("drv", null);
	drv.main_phase(null);
	$finish();
end

但是注意这里的输出仅为下面这样:

new is called
main_phased is called

字符串循环data is drived部分没有被输出。看起来仿真被中断了,因为在之前的顶层模块中我们使用$finish();来结束仿真,但加入factory机制后的顶层模块这句被删除了,那么是如何自动结束仿真的呢。

7.

这里需要引入一个叫做objection的机制。在每个phase中,UVM会检查是否有objection被提起(raise_objection),如果有,那么等待这个objection被撤销(drop_objection)后停止仿真;如果没有,则马上结束当前phase。

以下是加入objection机制的driver。可以看到增加了phase.raise_objection(this);phase.drop_objection(this);这对语句用于控制仿真的raise和drop。此时,就可以正常输出字符串循环data is drived了。

注意phase.raise_objection(this);必须放在消耗仿真时间单位语句的前面,否则不起作用。

task my_driver::main_phase(uvm_phase phase);
    phase.raise_objection(this);
	`uvm_info("my_driver", "main_phase is called", UVM_LOW);
	top_tb.rxd <= 8'b0;
	top_tb.rx_dv <= 1'b0;
	while(!top_tb.rst_n)
	@(posedge top_tb.clk);
	for(int i = 0; i < 256; i++)begin
		@(posedge top_tb.clk);
		top_tb.rxd <= $urandom_range(0, 255);
		top_tb.rx_dv <= 1'b1;
		`uvm_info("my_driver", "data is drived", UVM_LOW);
	end
	@(posedge top_tb.clk);
	top_tb.rx_dv <= 1'b0;
	phase.drop_objection(this);
endtask

8.

下面我们再来加入virtual interface来使得代码更加整洁规范并便于修改移植。

首先来定义interface。

interface my_if(input clk, input rst_n);
	logic [7:0] data;
	logic valid;
endinterface

然后在顶层例化和使用interface。

...
my_if input_if(clk, rst_n);
my_if output_if(clk, rst_n);
dut my_dut(.clk(clk),
	.rst_n(rst_n),
	.rxd(input_if.data),
	.rx_dv(input_if.valid),
	.txd(output_if.data),
	.tx_en(output_if.valid));
...

接着在my_driver里声明virtual interface,以便使用。

class my_
以下是一个简单UVM验证平台示例: 1. 创建一个UVM测试环境类(testbench),该类将包含所有UVM组件,并提供测试所需的接口和配置信息。 ``` class my_testbench extends uvm_env; `uvm_component_utils(my_testbench) // Define interface signals and configurations // ... // Define UVM components my_agent m_agent; my_driver m_driver; my_monitor m_monitor; my_scoreboard m_scoreboard; // Define UVM sequences and sequences library my_sequence_lib m_sequence_lib; function new(string name, uvm_component parent); super.new(name, parent); endfunction function void build_phase(uvm_phase phase); super.build_phase(phase); m_agent = my_agent::type_id::create("m_agent", this); m_driver = my_driver::type_id::create("m_driver", this); m_monitor = my_monitor::type_id::create("m_monitor", this); m_scoreboard = my_scoreboard::type_id::create("m_scoreboard", this); m_sequence_lib = my_sequence_lib::type_id::create("m_sequence_lib", this); endfunction function void connect_phase(uvm_phase phase); super.connect_phase(phase); // Connect UVM components m_driver.seq_item_port.connect(m_agent.seq_item_export); m_monitor.analysis_port.connect(m_agent.analysis_export); m_scoreboard.reference_port.connect(m_monitor.monitor_export); endfunction virtual function void run_phase(uvm_phase phase); super.run_phase(phase); m_sequence_lib.run_sequence(); endfunction endclass ``` 2. 创建一个UVM代理(agent)类,该类将实现将数据传输到DUT和从DUT接收数据的功能。 ``` class my_agent extends uvm_agent; `uvm_component_utils(my_agent) // Define interface signals and configurations // ... // Define UVM components my_sequencer m_sequencer; function new(string name, uvm_component parent); super.new(name, parent); endfunction function void build_phase(uvm_phase phase); super.build_phase(phase); m_sequencer = my_sequencer::type_id::create("m_sequencer", this); endfunction virtual function void connect_phase(uvm_phase phase); super.connect_phase(phase); // Connect UVM components m_sequencer.seq_item_port.connect(seq_item_export); endfunction endclass ``` 3. 创建一个UVM驱动(driver)类,该类将向DUT发送数据。 ``` class my_driver extends uvm_driver; `uvm_component_utils(my_driver) // Define interface signals and configurations // ... function new(string name, uvm_component parent); super.new(name, parent); endfunction virtual task run_phase(uvm_phase phase); super.run_phase(phase); forever begin seq_item_port.get_next_item(req); send_req_to_dut(); seq_item_port.item_done(); end endtask endclass ``` 4. 创建一个UVM监视器(monitor)类,该类将从DUT接收数据。 ``` class my_monitor extends uvm_monitor; `uvm_component_utils(my_monitor) // Define interface signals and configurations // ... function new(string name, uvm_component parent); super.new(name, parent); endfunction virtual task run_phase(uvm_phase phase); super.run_phase(phase); forever begin wait_for_dut_data(); pass_data_to_scoreboard(); end endtask endclass ``` 5. 创建一个UVM板(scoreboard)类,该类将比较从驱动程序发送的数据和从监视器接收的数据,以确认DUT是否按预期工作。 ``` class my_scoreboard extends uvm_scoreboard; `uvm_component_utils(my_scoreboard) // Define interface signals and configurations // ... function new(string name, uvm_component parent); super.new(name, parent); endfunction virtual function void compare(ref_item, dut_item, ...); // Compare reference and DUT data // ... endfunction endclass ``` 6. 创建UVM序列(sequence)类和UVM序列库(sequence library)类,以定义测试序列并将其添加到测试环境中。 ``` class my_sequence extends uvm_sequence; `uvm_object_param_utils(my_sequence) // Define sequence items and data // ... function new(string name = "my_sequence"); super.new(name); endfunction virtual task body(); // Define test sequence // ... endtask endclass class my_sequence_lib extends uvm_sequence_library; `uvm_component_utils(my_sequence_lib) // Define test sequences my_sequence m_test_sequence; function new(string name, uvm_component parent); super.new(name, parent); endfunction function void build_phase(uvm_phase phase); super.build_phase(phase); m_test_sequence = my_sequence::type_id::create("m_test_sequence"); m_test_sequence.set_sequence_state(uvm_sequence_base::is_auto_item); endfunction function void run_sequence(); m_test_sequence.start(m_sequencer); m_test_sequence.wait_for_sequence_state(UVM_FINISHED); endfunction endclass ``` 7. 在UVM测试中使用这些组件,创建一个UVM测试(test)类并运行它。 ``` class my_test extends uvm_test; `uvm_component_utils(my_test) my_testbench m_testbench; function new(string name, uvm_component parent); super.new(name, parent); endfunction virtual function void build_phase(uvm_phase phase); super.build_phase(phase); m_testbench = my_testbench::type_id::create("m_testbench", this); endfunction virtual task run_phase(uvm_phase phase); super.run_phase(phase); // Run testbench m_testbench.run_phase(phase); endtask endclass module top; initial begin uvm_config_db#(uvm_object_wrapper)::set(null, "uvm_test_top", "test", my_test::type_id::get()); run_test(); end endmodule ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值