4. 按类型覆盖与实例覆盖优先级

任务目标

在UVM验证环境中,通过set_type_override_by_type和set_inst_override_by_type同时设置类型覆盖与实例覆盖,验证当两者作用于同一对象时,实例覆盖具有更高优先级。

实现方案

构建最小UVM环境:定义基类base_driver与两个派生类drv_a、drv_b。在测试类中,先对base_driver设置类型覆盖为drv_a,再对特定实例路径设置实例覆盖为drv_b。通过UVM工厂创建该实例,并检查其实际类型。

关键完整代码

// file: override_priority_test.sv
`include "uvm_macros.svh"
import uvm_pkg::*;

class base_driver extends uvm_component;
    `uvm_component_utils(base_driver)
    function new(string name, uvm_component parent);
        super.new(name, parent);
    endfunction
endclass

class drv_a extends base_driver;
    `uvm_component_utils(drv_a)
    function new(string name, uvm_component parent);
        super.new(name, parent);
    endfunction
endclass

class drv_b extends base_driver;
    `uvm_component_utils(drv_b)
    function new(string name, uvm_component parent);
        super.new(name, parent);
    endfunction
endclass

class override_test extends uvm_test;
    `uvm_component_utils(override_test)
    function new(string name, uvm_component parent);
        super.new(name, parent);
    endfunction

    function void build_phase(uvm_phase phase);
        base_driver bd;
        super.build_phase(phase);
        // 类型覆盖:所有base_driver替换为drv_a
        set_type_override_by_type(base_driver::get_type(), drv_a::get_type());
        // 实例覆盖:仅替换特定路径上的实例为drv_b
        set_inst_override_by_type("u_env.*", base_driver::get_type(), drv_b::get_type());
        // 创建实例(路径匹配实例覆盖)
        bd = base_driver::type_id::create("u_env", this);
        if (bd == null) `uvm_fatal("TEST", "create failed")
        if ($cast(drv_b'(bd)))
            `uvm_info("TEST", "PASS: instance override wins", UVM_LOW)
        else if ($cast(drv_a'(bd)))
            `uvm_error("TEST", "FAIL: type override applied instead of instance override")
        else
            `uvm_error("TEST", "FAIL: unexpected type created")
    endfunction

    function void run_phase(uvm_phase phase);
        phase.raise_objection(this);
        #10;
        phase.drop_objection(this);
    endfunction
endclass

module top;
    initial begin
        run_test("override_test");
    end
endmodule

运行方式

  1. 使用支持UVM-1.2或UVM-1800.2的仿真器(如VCS、Questa、Xcelium)编译运行:
    vlog +incdir+$UVM_HOME/src $UVM_HOME/src/uvm_pkg.sv override_priority_test.sv
    vsim -c -do "run -all; quit" work.top
  2. 若使用VCS:
    vcs +incdir+$UVM_HOME/src -sverilog $UVM_HOME/src/uvm_pkg.sv override_priority_test.sv
    ./simv

验证方法与判定标准

仿真日志中应出现PASS: instance override wins,且无FAIL或UVM_ERROR。该结果表明:虽然类型覆盖将base_driver全局替换为drv_a,但实例覆盖针对路径u_env.*的规则优先级更高,最终创建出drv_b实例。若日志显示FAIL: type override applied,则说明实例覆盖未生效,需检查路径匹配语法或覆盖设置顺序。

关键点说明

  • 实例覆盖的路径字符串"u_env.*"使用通配符匹配当前测试下创建的u_env实例;若实际层次不同,需相应调整路径。
  • UVM工厂解析覆盖规则时,实例覆盖始终优先于类型覆盖,与设置先后顺序无关。
  • 本案例通过$cast动态类型检查直接验证最终对象类型,无需依赖打印字符串或额外标志位。