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
相关产品推荐
相关产品推荐

