搭一个完整的验证总线的UVM验证环境
时间: 2024-03-20 22:41:07 浏览: 69
好的,下面是一个简单的UVM验证环境,其中包含一个完整的验证总线:
```systemverilog
`include "uvm_macros.svh"
// 定义验证环境
class my_env extends uvm_env;
`uvm_component_utils(my_env)
// 声明验证组件
my_agent agent1;
my_agent agent2;
my_driver driver;
my_monitor monitor;
my_scoreboard scoreboard;
my_sequencer sequencer;
my_env_config env_config;
// 验证总线
virtual my_bus_if bus_if;
// 构造函数
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
// build_phase 阶段
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
// 创建验证组件
agent1 = my_agent::type_id::create("agent1", this);
agent2 = my_agent::type_id::create("agent2", this);
driver = my_driver::type_id::create("driver", this);
monitor = my_monitor::type_id::create("monitor", this);
scoreboard = my_scoreboard::type_id::create("scoreboard", this);
sequencer = my_sequencer::type_id::create("sequencer", this);
// 连接验证组件
driver.seq_item_port.connect(sequencer.seq_item_export);
monitor.ap.connect(bus_if);
scoreboard.ap.connect(monitor.sigs);
sequencer.bus_if_port.connect(bus_if);
endfunction
// connect_phase 阶段
virtual function void connect_phase(uvm_phase phase);
super.connect_phase(phase);
// 连接验证总线
bus_if = my_bus_if::type_id::create("bus_if", this);
endfunction
// end_of_elaboration_phase 阶段
virtual function void end_of_elaboration_phase(uvm_phase phase);
super.end_of_elaboration_phase(phase);
// 配置验证环境
env_config = my_env_config::type_id::create("env_config", this);
env_config.agent1_config = agent1.get_config();
env_config.agent2_config = agent2.get_config();
env_config.driver_config = driver.get_config();
env_config.monitor_config = monitor.get_config();
env_config.scoreboard_config = scoreboard.get_config();
env_config.sequencer_config = sequencer.get_config();
env_config.set_config_object();
endfunction
endclass
// 定义验证总线接口
interface my_bus_if;
clock clk;
logic reset;
logic [31:0] addr;
logic [31:0] data;
logic read_write;
logic [1:0] burst_size;
// 接口方法
virtual task read (input int unsigned addr, output int unsigned data);
virtual task write (input int unsigned addr, input int unsigned data);
endinterface
// 定义验证组件
class my_agent extends uvm_agent;
`uvm_component_utils(my_agent)
// 声明验证组件的其他成员
my_agent_config config;
my_driver driver;
my_monitor monitor;
my_scoreboard scoreboard;
my_sequencer sequencer;
// 构造函数
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
// build_phase 阶段
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
// 创建验证组件的其他成员
driver = my_driver::type_id::create("driver", this);
monitor = my_monitor::type_id::create("monitor", this);
scoreboard = my_scoreboard::type_id::create("scoreboard", this);
sequencer = my_sequencer::type_id::create("sequencer", this);
// 连接验证组件的其他成员
driver.seq_item_port.connect(sequencer.seq_item_export);
monitor.ap.connect(bus_if);
scoreboard.ap.connect(monitor.sigs);
sequencer.bus_if_port.connect(bus_if);
endfunction
// get_config 方法
virtual function uvm_object get_config();
return config;
endfunction
endclass
// 定义验证组件的配置对象
class my_agent_config extends uvm_object;
`uvm_object_utils(my_agent_config)
// 配置对象的成员
bit [7:0] slave_id;
endclass
// 定义驱动
class my_driver extends uvm_driver #(my_seq_item);
`uvm_component_utils(my_driver)
// 构造函数
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
// run_phase 阶段
virtual task run_phase(uvm_phase phase);
my_seq_item seq_item;
// 从 sequencer 端口获取事务并将其发送到 DUT
forever begin
seq_item = seq_item_port.get_next_item();
send(seq_item);
end
endtask
// send 方法
virtual task send(my_seq_item seq_item);
// 将事务转换为 DUT 端口可以理解的信号
// ...
// 将信号发送到 DUT 端口
bus_if.write(addr, data);
// 将事务标记为完成
seq_item_port.item_done();
endtask
endclass
// 定义监控器
class my_monitor extends uvm_monitor;
`uvm_component_utils(my_monitor)
// 监控器分析端口
uvm_analysis_port #(my_sig) ap;
// 构造函数
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
// run_phase 阶段
virtual task run_phase(uvm_phase phase);
my_sig sig;
// 从 DUT 端口获取信号并将其转换为事务
forever begin
wait(bus_if.read_write == 1'b1);
sig = new;
sig.addr = bus_if.addr;
sig.data = bus_if.data;
sig.is_read = 1;
ap.write(sig);
end
endtask
endclass
// 定义分析对象
class my_sig;
bit [31:0] addr;
bit [31:0] data;
bit is_read;
endclass
// 定义记分板
class my_scoreboard extends uvm_scoreboard;
`uvm_component_utils(my_scoreboard)
// 记分板分析端口
uvm_analysis_port #(my_sig) ap;
// 构造函数
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
// write 方法
virtual function void write(input my_sig sig);
// 检查信号是否符合预期
// ...
// 如果符合预期,则标记事务为完成
item_done();
endfunction
endclass
// 定义顺序器
class my_sequencer extends uvm_sequencer #(my_seq_item);
`uvm_component_utils(my_sequencer)
// 构造函数
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
// get_config 方法
virtual function uvm_object get_config();
my_sequencer_config config;
// 从 env_config 中获取配置对象
env_config.get_object("sequencer_config", config);
return config;
endfunction
// get_sequencer_config 方法
virtual function void get_sequencer_config(ref uvm_config_db #(my_sequencer_config) cfg);
my_sequencer_config config;
// 从 env_config 中获取配置对象
env_config.get_object("sequencer_config", config);
cfg = config;
endfunction
endclass
// 定义顺序器配置对象
class my_sequencer_config extends uvm_object;
`uvm_object_utils(my_sequencer_config)
// 配置对象的成员
bit [7:0] master_id;
endclass
// 定义事务
class my_seq_item extends uvm_sequence_item;
`uvm_object_utils(my_seq_item)
// 事务的成员
bit [31:0] addr;
bit [31:0] data;
bit is_read;
// 构造函数
function new(string name = "");
super.new(name);
endfunction
endclass
// 定义验证环境配置对象
class my_env_config extends uvm_object;
`uvm_object_utils(my_env_config)
// 配置对象的成员
my_agent_config agent1_config;
my_agent_config agent2_config;
my_driver_config driver_config;
my_monitor_config monitor_config;
my_scoreboard_config scoreboard_config;
my_sequencer_config sequencer_config;
endclass
```
注意,以上只是一个简单的示例,具体的验证环境需要根据实际需求进行修改。同时,还需要在顶层模块中实例化和启动该验证环境。
阅读全文