文章目录
前言
在具体的验证项目中,build_phase
是 UVM 验证环境初始化的核心阶段,用于构建组件树形结构和配置参数传递。以下是分步骤的详细应用说明:
1. 定义验证环境层次结构
验证平台通常包含以下层级(以处理器验证为例):
- Test:顶层测试用例,配置全局参数。
- Env:验证环境容器,集成所有子组件(Agent、Scoreboard 等)。
- Agent:代理组件,管理 Driver、Monitor 和 Sequencer。
- Driver/Monitor/Sequencer:具体功能组件,与 DUT 交互。
- Scoreboard/Coverage:数据检查和覆盖率收集组件。
2. 在 build_phase
中实例化组件
目标:在父组件的 build_phase
中创建子组件实例,形成树形结构。
示例代码:Env 的 build_phase
class processor_env extends uvm_env;
// 子组件声明
instruction_agent i_agent;
data_agent d_agent;
regfile_scoreboard reg_sb;
coverage_collector cov;
// 必须调用 super.build_phase(phase)
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
// 实例化组件
i_agent = instruction_agent::type_id::create("i_agent", this);
d_agent = data_agent::type_id::create("d_agent", this);
reg_sb = regfile_scoreboard::type_id::create("reg_sb", this);
cov = coverage_collector::type_id::create("cov", this);
endfunction
endclass
3. 使用 config_db
传递虚拟接口
目标:将 DUT 的物理接口通过 uvm_config_db
传递到 UVM 组件。
步骤 1:在 Testbench 顶层设置虚拟接口
module top_tb;
// 实例化 DUT 和接口
processor_if proc_if();
processor_dut dut(.bus(proc_if));
initial begin
// 将接口传递给 UVM 环境
uvm_config_db#(virtual processor_if)::set(
null, // 全局上下文
"uvm_test_top.env*", // 目标路径(匹配所有 env 下的组件)
"proc_vif", // 键值名称
proc_if // 接口句柄
);
run_test("base_test");
end
endmodule
步骤 2:在 Agent 中获取接口并传递给 Driver/Monitor
class instruction_agent extends uvm_agent;
instruction_driver driver;
virtual processor_if vif;
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
driver = instruction_driver::type_id::create("driver", this);
// 从 config_db 获取接口
if (!uvm_config_db#(virtual processor_if)::get(
this, // 当前组件上下文
"", // 相对路径(空表示当前)
"proc_vif", // 键值名称
vif // 存储的目标变量
)) begin
`uvm_fatal("NO_IF", "Virtual interface not found!")
end
// 将接口传递给 Driver
uvm_config_db#(virtual processor_if)::set(
this, // 当前组件上下文
"driver", // 目标路径(子组件名)
"proc_vif", // 键值名称
vif // 接口句柄
);
endfunction
endclass
4. 动态配置组件
目标:通过 config_db
传递参数控制组件行为(如使能覆盖率)。
在 Test 中设置配置参数
class base_test extends uvm_test;
processor_env env;
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
env = processor_env::type_id::create("env", this);
// 设置覆盖率使能标志
uvm_config_db#(bit)::set(
this, // 当前组件上下文
"env.cov", // 目标路径
"enable", // 键值名称
1 // 值(1=使能)
);
endfunction
endclass
在 Coverage 组件中获取参数
class coverage_collector extends uvm_component;
bit enable;
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
// 获取配置参数
if (!uvm_config_db#(bit)::get(
this, // 当前组件上下文
"", // 相对路径
"enable", // 键值名称
enable // 存储的目标变量
)) begin
enable = 0; // 默认值
end
endfunction
endclass
5. 条件化组件实例化
目标:根据配置动态决定是否实例化组件(如可选模块)。
在 Env 中按需创建组件
class processor_env extends uvm_env;
// 动态实例化标志
bit enable_debug_monitor = 0;
debug_monitor dbg_mon;
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
// 从 config_db 获取是否启用调试监视器
uvm_config_db#(bit)::get(this, "", "enable_debug_monitor", enable_debug_monitor);
if (enable_debug_monitor) begin
dbg_mon = debug_monitor::type_id::create("dbg_mon", this);
end
endfunction
endclass
6. 关键注意事项
- 执行顺序:UVM 的
build_phase
按自顶向下顺序执行,确保父组件先于子组件配置。 - 工厂创建:必须使用
type_id::create
而非new()
,以支持工厂重载(如替换 VIP)。 - 路径匹配:
config_db::set/get
的路径需严格匹配,推荐使用通配符*
或uvm_test_top
简化。 - 错误处理:对关键参数(如虚拟接口)使用
uvm_fatal
确保问题早暴露。
7. 完整示例:处理器验证环境
Testbench 顶层
module top;
processor_if proc_if();
processor_dut dut(.bus(proc_if));
initial begin
uvm_config_db#(virtual processor_if)::set(null, "uvm_test_top.env*", "proc_vif", proc_if);
run_test("alu_test");
end
endmodule
Test 用例
class alu_test extends uvm_test;
processor_env env;
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
env = processor_env::type_id::create("env", this);
// 配置:启用调试监视器
uvm_config_db#(bit)::set(this, "env", "enable_debug_monitor", 1);
endfunction
endclass
Env 集成
class processor_env extends uvm_env;
instruction_agent i_agent;
data_agent d_agent;
regfile_scoreboard reg_sb;
debug_monitor dbg_mon;
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
i_agent = instruction_agent::type_id::create("i_agent", this);
d_agent = data_agent::type_id::create("d_agent", this);
reg_sb = regfile_scoreboard::type_id::create("reg_sb", this);
// 按需实例化调试监视器
bit enable_dbg;
if (uvm_config_db#(bit)::get(this, "", "enable_debug_monitor", enable_dbg) && enable_dbg) begin
dbg_mon = debug_monitor::type_id::create("dbg_mon", this);
end
endfunction
endclass
通过以上步骤,build_phase
实现了验证环境的灵活构建和配置,为后续的仿真阶段(如 connect_phase
、run_phase
)奠定基础。