SystemVerilog:用类对象数组聚合类



我想要一个SystemVerilog类,它包含另一个类的数组,如下所示:

class AggregateClass;
integer x;
OtherClass[x] otherClassArray;
extern function new(int x, logic in);
endclass: AggregateClass
class OtherClass;
// ...
extern function new(logic in);
// ...
endclass: OtherClass
  1. 如何在SystemVerilog中定义类数组
  2. 如何让构造函数设置所述类数组中类对象的数量

在声明静态数组时,需要一个常量来声明其大小;因此,不能在那里使用变量。但是,在SystemVerilog中,您可以使用如下动态数组:

class B;
int val;
function new(int v);
val = v;
endfunction
function void print;
$display("%0d", val);
endfunction
endclass
class A;
int x;
B b[int]; // << declaration of a dynamic array

function new(int n);
x = n;
for (int i = 0; i < n; i++)
b[i] = new(i); // <<< construction of dynamic array elements of class B.
endfunction

function void print;
$display("size: %0d", b.size);
for (int i = 0; i < x; i++)
b[i].print;
endfunction

endclass

module top;
initial begin
A a = new(4);
a.print();
end
endmodule

最新更新