You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UVM工厂override后i_type值未达预期,求正确获取方法

UVM工厂Override前后约束未生效问题的解决方法

问题根源

你的i_type始终为00,核心原因有两个:

  1. 未触发约束随机化:UVM对象的约束不会自动生效,必须显式调用randomize()方法,才能让约束条件作用于成员变量。
  2. 类成员位宽不匹配:基类中i_type定义为8位(bit[7:0]),子类却定义为2位(bit[1:0]),导致子类约束仅覆盖低两位,高位保留默认值0,最终输出结果不符合预期。

修正方案

1. 统一成员变量位宽

确保基类和子类的i_type位宽一致,避免类型不匹配导致的截断/补零:

class cfg extends uvm_object;
  `uvm_object_utils(cfg)
  rand bit[7:0] i_type; // 统一为8位
  constraint c_type {i_type == 'h88;}
  function new(string name="cfg");
    super.new(name);
  endfunction
endclass

class c_0 extends cfg;
  `uvm_object_utils(c_0)
  constraint c_type {i_type == 'h03;} // 2'b11扩展为8位的十六进制值
  function new(string name="c_0");
    super.new(name);
  endfunction
endclass

2. 创建对象后显式随机化

在获取对象实例后,必须调用randomize()触发约束生效:

cfg cfg_inst;

// 未Override时的测试
cfg_inst = cfg::type_id::create("cfg_inst");
if (!cfg_inst.randomize()) begin
  `uvm_error("RAND_FAIL", "Base class randomization failed")
end
`uvm_info("PRE_OVERRIDE", $sformatf("i_type = 'h%02h", cfg_inst.i_type), UVM_LOW);
// 预期输出:i_type = 'h88

// 执行类型Override
cfg::type_id::set_type_override(c_0::get_type());

// Override后的测试
cfg_inst = cfg::type_id::create("cfg_inst");
if (!cfg_inst.randomize()) begin
  `uvm_error("RAND_FAIL", "Derived class randomization failed")
end
`uvm_info("POST_OVERRIDE", $sformatf("i_type = 'h%02h", cfg_inst.i_type), UVM_LOW);
// 预期输出:i_type = 'h03

3. 确保Override时机正确

set_type_override必须在创建对象之前调用,否则无法覆盖已创建的实例。如果需要针对特定实例做Override,可使用set_inst_override方法。

内容的提问来源于stack exchange,提问作者bu-ral

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 18:32:48