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

SystemVerilog中如何用循环创建参数化类的对象数组?

SystemVerilog 参数化类数组创建问题

我基于参数化类写了如下代码,希望构建一个包含不同参数类实例的数组:

virtual class base;
  ???
endclass
class my_class#(int width) extends base;
      typedef bit [width-1:0] vec_t; 
      virtual function vec_t get_zeros();
          vec_t x = '0;
          return x;
      endfunction
endclass
base  my_array [1:3];
initial begin
           my_array[1] = my_class#(1)::new;     
           my_array[2] = my_class#(2)::new;     
           my_array[3] = my_class#(3)::new;         
           ...
end

核心疑问

  • 能否通过循环实现上述实例化操作?
    我尝试了如下写法,但因为SystemVerilog要求类参数必须是静态编译期常量,这段代码无法运行:
for (int i=1; i<=3; i++)
    my_array[i] = my_class#(i)::new;
  • Generate块是否可行?
    我猜测generate块可以解决这个问题,但始终写不出能编译通过的语法。

  • 基类的设计困惑
    不确定是否必须使用基类;如果需要基类,不清楚基类中需要定义什么内容,尤其是如何处理get_zeros函数——它的返回类型依赖于子类的参数width。

内容的提问来源于stack exchange,提问作者Craig

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 01:57:36