System verilog Systemverilog const ref arg构造对象时的位置

System verilog Systemverilog const ref arg构造对象时的位置,system-verilog,System Verilog,我正在创建一个类,该类需要对测试台的配置对象进行引用。由于配置必须在整个模拟过程中保持完整,因此我将其作为const ref对象传递。下面是我要运行的sudo代码: class tb_config; int unsigned rate; int unsigned chnls[]; const int unsigned nb_chnls; function new (int unsigned rate, int unsigned nb_chnls); this.rate

我正在创建一个类,该类需要对测试台的配置对象进行引用。由于配置必须在整个模拟过程中保持完整,因此我将其作为const ref对象传递。下面是我要运行的sudo代码:

class tb_config;
  int unsigned rate;
  int unsigned chnls[];
  const int unsigned nb_chnls;

  function new (int unsigned rate, int unsigned nb_chnls);
    this.rate     = rate;
    this.nb_chnls = nb_chnls;
    chnls         = new[nb_chnls];
  endfunction
endclass

class tx_phy;
  static int phy_id;
  tb_config cfg;

  function new (int unsigned phy_id, const ref tb_config cfg);
      this.phy_id = phy_id;
      this.cfg    = cfg; 
  endfunction

endclass


module test;
  tb_config cfg = new(100, 4);
  tx_phy phy    = new( 1234, cfg);

endmodule
上面的代码工作得非常好,符合我的期望。但是如果我将tx_phy::new中的参数更改为函数new(const ref tb_config cfg,int unsigned phy_id)并相应地将值传递给构造函数,我在Cadence Incisive中得到以下错误:

invalid ref argument usage because actual argument is not a variable.
当我在EDA中使用Aldec进行测试时,也会发生同样的情况:


我假设这是一种语言限制,但是还有其他原因吗???

原因是,如果没有指定,参数类型是隐式的。您为第一个参数指定了
const-ref
,但没有为第二个参数指定任何内容,因此它也是隐式的
const-ref
。将
input
添加到第二个参数声明中可以解决此问题

 function new (const ref tb_config cfg, input int unsigned phy_id);
我还想添加
const ref tb_config cfg
等同于编写

 function new (tb_config cfg, int unsigned phy_id);
这两个参数都是隐式的
input
参数,这意味着它们是在输入时复制的

类变量已经是引用。通过
ref
传递类变量意味着您可以从函数中更新类变量的句柄。使参数a
const ref
意味着您将无法更新类变量,但仍然可以更新变量引用的类的成员。如果您有一个类对象的句柄,而不是通过声明它们
protected
local
,则没有任何机制可以阻止更新该类对象的成员


在SystemVerilog中通过
ref
传递函数参数唯一有意义的地方是,当参数是像数组这样的大型数据结构时,作为一种优化,您只需要访问数组的几个元素。当参数在任务生命周期内需要更新时(即将时钟作为参数传递),可以使用任务
ref
参数。

非常感谢!现在它有意义了。我之所以添加ref,是因为编译器在我未指定它时给了我错误:应为标记:“ref”。“testbench.sv”