dsula
dsula

Reputation: 195

systemverilog unpacked array concatenation

I'm trying to create an unpacked array like this:

logic [3:0] AAA[0:9];

I'd like to initialize this array to the following values:

AAA = '{1, 1, 1, 1, 2, 2, 2, 3, 3, 4};

For efficiency I'd like to use repetition constructs, but that's when things are falling apart. Is this not possible, or am I not writing this correctly? Any help is appreciated.

AAA = { '{4{1}}, '{3{2}}, '{2{3}}, 4 };

Upvotes: 5

Views: 16365

Answers (3)

Arun D'souza
Arun D'souza

Reputation: 202

I had another solution but I'm not sure if it is synthesizable. Would a streaming operator work here? I'm essentially taking a packed array literal and streaming it into the data structure AAA. I've put it on EDA Playground

module tb;

  logic [3:0] AAA[0:9];

  initial begin
    AAA = { >> int {
      {4{4'(1)}},
      {3{4'(2)}},
      {2{4'(3)}},
      4'(4)
    } };
    $display("%p",AAA);
  end

endmodule

Output:

Compiler version P-2019.06-1; Runtime version P-2019.06-1;  Mar 25 11:20 2020
'{'h1, 'h1, 'h1, 'h1, 'h2, 'h2, 'h2, 'h3, 'h3, 'h4} 
           V C S   S i m u l a t i o n   R e p o r t 
Time: 0 ns
CPU Time:      0.580 seconds;       Data structure size:   0.0Mb
Wed Mar 25 11:20:07 2020
Done

Upvotes: 1

dave_59
dave_59

Reputation: 42623

You can't do arbitrary replication of unpacked array elements.

If your code doesn't need to be synthesized, you can do

module top;
  typedef logic [3:0] DAt[];

  logic [3:0] AAA[0:9];

  initial begin
    AAA = {DAt'{4{1}}, DAt'{3{2}}, DAt'{2{3}}, 4};
    $display("%p",AAA);
  end
endmodule

Upvotes: 2

Silicon1602
Silicon1602

Reputation: 1181

Firstly, the construct you are using is actually called the replication operator. This might help you in future searches, for example in the SystemVerilog LRM.

Secondly, you are using an array concatenation and not an array assignment in your last block of code (note the missing apostrophe '). The LRM gives the following (simple) example in Section 10.10.1 (Unpacked array concatenations compared with array assignment patterns) to explain the difference:

int A3[1:3];
A3 = {1, 2, 3}; // unpacked array concatenation
A3 = '{1, 2, 3}; // array assignment pattern

The LRM says in the same section that

...unpacked array concatenations forbid replication, defaulting, and explicit typing, but they offer the additional flexibility of composing an array value from an arbitrary mix of elements and arrays.

int A9[1:9]; 
A9 = {9{1}}; // illegal, no replication in unpacked array concatenation

Lets also have a look at the alternative: array assignment. In the same section, the LRM mentions that

...items in an assignment pattern can be replicated using syntax, such as '{ n{element} }, and can be defaulted using the default: syntax. However, every element item in an array assignment pattern must be of the same type as the element type of the target array.

If transforming it to an array assignment (by adding an apostrophe), your code actually translates to:

AAA = '{'{1,1,1,1}, '{2,2,2}, '{3,3}, 4};

This means that the SystemVerilog interpreter will only see 4 elements and it will complain that too few elements were given in the assignment.

In Section 10.9.1 (Array assignment patterns), the LRM says the following about this:

Concatenation braces are used to construct and deconstruct simple bit vectors. A similar syntax is used to support the construction and deconstruction of arrays. The expressions shall match element for element, and the braces shall match the array dimensions. Each expression item shall be evaluated in the context of an assignment to the type of the corresponding element in the array.

[...]

A syntax resembling replications (see 11.4.12.1) can be used in array assignment patterns as well. Each replication shall represent an entire single dimension.

To help interprete the bold text in the quote above, the LRM gives the following example:

int n[1:2][1:3] = '{2{'{3{y}}}}; // same as '{'{y,y,y},'{y,y,y}}

Upvotes: 6

Related Questions