异构核共用UVM测试环境,按核条件创建采样Covergroup的问题求助
异构核Coverage类实例化与采样问题解决方案
你遇到的核心问题确实是core变量赋值时机晚于covergroup创建时机:new()函数执行时core还是默认值0,导致两个my_cov_collector实例都只创建了core0的covergroup,后续对core1实例调用sample()时,core1 covergroup根本没初始化,所以报错。
下面提供两种可行的解决方案:
方案一:将covergroup创建延迟到core变量赋值后执行
把covergroup的初始化逻辑从new()移到一个单独的初始化函数中,在build_phase给core赋值后再调用这个函数:
修改后的代码示例:
class my_agent extends uvm_agent; my_cov_collector cov_collector[NUM_CORES]; // 补充类型声明 function void build_phase(uvm_phase phase); super.build_phase(phase); for(int i=0;i<NUM_CORES; i++) begin cov_collector[i] = my_cov_collector::type_id::create($psprintf("cov_collector[%0d]",i),this); cov_collector[i].core = i; cov_collector[i].init_cov(); // 赋值后初始化covergroup end endfunction endclass class my_cov_collector extends uvm_monitor; int core; covergroup core0; // 这里添加core0的覆盖点定义 endgroup covergroup core1; // 这里添加core1的覆盖点定义 endgroup function new(string name="my_cov_collector", uvm_component parent=null); super.new(name, parent); // 补充UVM组件构造函数的标准参数 endfunction // 新增初始化covergroup的函数 function void init_cov(); if(core==0) begin core0 = new(); $display("Created core0 covergroup for instance %s", get_full_name()); end if(core==1) begin core1 = new(); $display("Created core1 covergroup for instance %s", get_full_name()); end endfunction task sample(); if(core==0 && core0 != null) begin core0.sample(); end if(core==1 && core1 != null) begin core1.sample(); end endtask endclass
关键修改点:
- 给
my_cov_collector新增init_cov()函数,专门负责根据core值创建对应的covergroup - 在
my_agent的build_phase中,给cov_collector实例的core赋值后,立即调用init_cov() - 给
sample()函数增加空指针判断,避免未初始化时调用报错 - 补全UVM组件构造函数的标准参数(
name和parent),符合UVM规范
方案二:构造函数传参,提前传入core值
在创建my_cov_collector实例时,直接把core值通过构造函数传进去,这样new()执行时就能拿到正确的core值来创建covergroup:
修改后的代码示例:
class my_agent extends uvm_agent; my_cov_collector cov_collector[NUM_CORES]; function void build_phase(uvm_phase phase); super.build_phase(phase); for(int i=0;i<NUM_CORES; i++) begin // 创建时直接传入core值 cov_collector[i] = my_cov_collector::type_id::create($psprintf("cov_collector[%0d]",i),this, i); end endfunction endclass class my_cov_collector extends uvm_monitor; int core; covergroup core0; // 覆盖点定义 endgroup covergroup core1; // 覆盖点定义 endgroup // 重载构造函数,新增core参数 function new(string name="my_cov_collector", uvm_component parent=null, int core_id); super.new(name, parent); core = core_id; // 直接赋值core if(core==0) begin core0 = new(); end if(core==1) begin core1 = new(); end endfunction task sample(); if(core==0) begin core0.sample(); end if(core==1) begin core1.sample(); end endtask endclass
关键修改点:
- 重载
my_cov_collector的构造函数,新增core_id参数 - 在
new()函数内部直接把core_id赋值给core,并立即创建对应的covergroup - 在
my_agent的build_phase中,调用create()时传入core值(UVM的create()方法支持传递额外参数给组件的构造函数)
两种方案都能解决你的问题,方案二更符合UVM组件构造的逻辑,方案一则更灵活(比如支持后续动态修改core值后重新初始化covergroup)。
内容的提问来源于stack exchange,提问作者Rutuja14
相关产品推荐
相关产品推荐

