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

如何启动继承的uvm_test类?调用.start()方法遇空指针错误

问题:使用.start()替代uvm_config_db启动UVM测试类时出现空指针错误

我尝试用.start()方法替代uvm_config_db()来启动uvm_test类,注释掉了uvm_config_db相关代码并实现了run_phase(),代码如下:

virtual class test_base extends uvm_test;
  ahb_agent ahb0;
  ahb_agent ahb1;

  function new(string name, uvm_component parent);
    super.new(name, parent);
  endfunction

  virtual function void build_phase(uvm_phase phase);
    ahb0 = ahb_agent::type_id::create("ahb0", this);
    ahb1 = ahb_agent::type_id::create("ahb1", this);
  endfunction
endclass

class test1 extends test_base;
  `uvm_component_utils(test1)
   main_ahb_sequence seq;
   ahb_agent agent;
  function new(string name, uvm_component parent);
    super.new(name, parent);
  endfunction

  function void build_phase(uvm_phase phase);
    seq = main_ahb_sequence::type_id::create("seq", this);
  endfunction


  task run_phase(uvm_phase phase);
    super.run_phase(phase);
    phase.raise_objection(this);

    seq.start(ahb0.sequencer);

    phase.drop_objection(this);
  endtask

/*
  virtual function void end_of_elaboration_phase(uvm_phase phase);
    uvm_config_db #(uvm_object_wrapper)::set(this, "ahb0.sequencer.run_phase",
      "default_sequence", main_ahb_sequence::get_type());
    uvm_config_db #(uvm_object_wrapper)::set(this, "ahb1.sequencer.run_phase",
      "default_sequence", main_ahb_sequence::get_type());
  endfunction
*/

运行后触发空指针错误:

xmsim: *E,TRNULLID: NULL pointer dereference.
          File: ./test1.svh, line = 42, pos = 17

我怀疑是test_base类未正确构造,但不知道如何正确启动继承的uvm_test类。


问题原因

错误核心是test1的build_phase未调用父类test_base的build_phase方法。test_base中负责创建ahb0、ahb1两个agent的逻辑完全没执行,导致ahb0是null指针,调用ahb0.sequencer自然触发空指针错误。

修复方案

  1. 在test1的build_phase中添加父类方法调用,确保test_base的agent被正确创建:
function void build_phase(uvm_phase phase);
  super.build_phase(phase); // 调用父类build_phase,创建ahb0、ahb1
  seq = main_ahb_sequence::type_id::create("seq", this);
endfunction
  1. 删除test1中未使用的ahb_agent agent;变量,精简代码。

内容的提问来源于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.28 09:25:37