写一个uvm环境,在monitor中检测当前bit和改bit的补码值,然后在scoreboard中进行比对
时间: 2024-05-12 19:19:09 浏览: 82
基于UVM的基带射频接口电路的验证
以下是一个基本的UVM环境,包含一个monitor和一个scoreboard,用于检测当前bit和改bit的补码值:
```systemverilog
`include "uvm_macros.svh"
class my_env extends uvm_env;
my_agent m_agent;
my_scoreboard m_scoreboard;
function new(string name = "my_env", uvm_component parent);
super.new(name, parent);
endfunction : new
function void build_phase(uvm_phase phase);
super.build_phase(phase);
m_agent = my_agent::type_id::create("m_agent", this);
m_scoreboard = my_scoreboard::type_id::create("m_scoreboard", this);
endfunction : build_phase
function void connect_phase(uvm_phase phase);
super.connect_phase(phase);
m_agent.monitor_ap.connect(m_scoreboard.analysis_export);
endfunction : connect_phase
endclass : my_env
class my_monitor extends uvm_monitor;
uvm_analysis_port#(my_analysis_item) analysis_port;
my_analysis_item analysis_item;
function new(string name = "my_monitor", uvm_component parent);
super.new(name, parent);
endfunction : new
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
analysis_port = new("analysis_port", this);
endfunction : build_phase
virtual task run_phase(uvm_phase phase);
super.run_phase(phase);
while(1) begin
// Read current bit and its two's complement
bit curr_bit = my_interface.read_bit();
logic signed [31:0] curr_twos_comp = $signed(curr_bit) ? -1 - curr_bit : curr_bit;
// Wait for change in bit value
@(my_interface.posedge);
// Read changed bit and its two's complement
bit changed_bit = my_interface.read_bit();
logic signed [31:0] changed_twos_comp = $signed(changed_bit) ? -1 - changed_bit : changed_bit;
// Create analysis item and send to scoreboard
analysis_item = new("analysis_item");
analysis_item.curr_bit = curr_bit;
analysis_item.curr_twos_comp = curr_twos_comp;
analysis_item.changed_bit = changed_bit;
analysis_item.changed_twos_comp = changed_twos_comp;
analysis_port.write(analysis_item);
// Wait for next posedge
@(my_interface.posedge);
end
endtask : run_phase
endclass : my_monitor
class my_scoreboard extends uvm_scoreboard;
uvm_analysis_export#(my_analysis_item) analysis_export;
my_analysis_item analysis_item;
function new(string name = "my_scoreboard", uvm_component parent);
super.new(name, parent);
endfunction : new
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
analysis_export = new("analysis_export", this);
endfunction : build_phase
virtual function void run_phase(uvm_phase phase);
super.run_phase(phase);
while(1) begin
// Wait for analysis item from monitor
analysis_export.get_next_item(analysis_item);
// Compare current bit and its two's complement with changed bit and its two's complement
if (analysis_item.curr_bit != analysis_item.changed_bit) begin
if (analysis_item.curr_twos_comp != analysis_item.changed_twos_comp) begin
`uvm_error("BIT_ERROR", $sformatf("Bit value changed from %0d to %0d, but two's complement changed from %0d to %0d", analysis_item.curr_bit, analysis_item.changed_bit, analysis_item.curr_twos_comp, analysis_item.changed_twos_comp))
end
end
end
endfunction : run_phase
endclass : my_scoreboard
class my_agent extends uvm_agent;
my_monitor monitor_ap;
function new(string name = "my_agent", uvm_component parent);
super.new(name, parent);
endfunction : new
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
monitor_ap = my_monitor::type_id::create("monitor_ap", this);
endfunction : build_phase
endclass : my_agent
class my_analysis_item extends uvm_sequence_item;
bit curr_bit;
logic signed [31:0] curr_twos_comp;
bit changed_bit;
logic signed [31:0] changed_twos_comp;
`uvm_object_utils(my_analysis_item)
function new(string name = "my_analysis_item");
super.new(name);
endfunction : new
endclass : my_analysis_item
```
在这个环境中,`my_monitor`任务读取当前的bit和它的补码值,然后等待bit值的改变。一旦有改变,它读取新的bit和它的补码值,并创建一个`my_analysis_item`对象,其中包含了当前bit和改变的bit以及它们的补码值。然后,它使用`analysis_port`将该对象发送到`my_scoreboard`。
`my_scoreboard`在接收到`my_analysis_item`对象后,比较当前bit和它的补码值与改变的bit和它的补码值。如果bit值不同,那么它会检查补码值是否也不同。如果补码值也不同,那么它将在控制台上输出一条错误消息。
在`my_env`中,`my_agent`和`my_scoreboard`被创建,并通过`monitor_ap`和`analysis_export`连接起来。然后,整个环境可以被实例化并运行。
阅读全文