SystemVerilog dynamic arrays


#1

This is a handy cheatsheet of what you can do with SystemVerilog dynamic arrays

Examples on EDA playground: https://www.edaplayground.com/x/u9L

Initializing a dynamic array

logic [7:0] darray1[] = '{'d1, 'd2, 'd3};

Get size of dynamic array

  $display("darray1 size %0d", darray1.size());

Resizing a dynamic array

logic [7:0] darray1[] = '{'d1, 'd2, 'd3};

initial begin
  darray1 = new[10](darray1); // copy over old contents and resize
end

Copying over a dynamic array to another fixed or dynamic array

logic [7:0] darray1[] = '{'d1, 'd2, 'd3};
logic [7:0] darray2[];
logic [7:0] farray1[3];

initial begin
  darray2 = darray1; 
  // or
  darray2 = new[darray1.size()](darray1);

  // assignment to fixed array of same size
 farray = darray1;
end

Iterate over elements of dynamic array

foreach (darray1[i]) begin
  $display("element %0d = 0x%x", i, darray1[i]);
end

Welcome to the discuss.systemverilog.io