Array assignment

Jan. 12, 2024 || Item:2.5.Array assignment

Assigning to a fixed-size unpacked array requires that the source and the target both be arrays with the same number of unpacked dimensions, and the length of each dimension be the same. Assignment is done by assigning each element of the source array to the corresponding element of the target array, which requires that the source and target arrays be of compatible types. Compatible types are types that are assignment compatible.Assigning fixed-size unpacked arrays of unequal size to one another shall result in a type check error.

int A[10:1];    // fixed-size array of 10 elements

int B[0:9];     // fixed-size array of 10 elements

int C[24:1];    // fixed-size array of 24 elements

A = B;          // ok. Compatible type and same size

A = C;          // type check error: different sizes

An array of wires can be assigned to an array of variables having the same number of unpacked dimensions and the same length for each of those dimensions, and vice-versa.

wire [31:0] W [9:0];

assign W = A;

initial #10 B = W;

A dynamic array can be assigned to a one-dimensional fixed-size array of a compatible type, if the size of the dynamic array is the same as the length of the fixed-size array dimension. Unlike assigning with a fixed-size array, this operation requires a run-time check that can result in an error.

A dynamic array or a one-dimensional fixed-size array can be assigned to a dynamic array of a compatible type. In this case, the assignment creates a new dynamic array with a size equal to the length of the fixed-size array. For example:

int A[100:1];      // fixed-size array of 100 elements

int B[];           // empty dynamic array

int C[] = new[8];  // dynamic array of size 8

B = A;             // ok. B has 100 elements

B = C;             // ok. B has 8 elements

The last statement above is equivalent to:

B = new[ C.size ] (C);

Similarly, the source of an assignment can be a complex expression involving array slices or concatenations.

For example:

string d[1:5] = { "a", "b", "c", "d", "e" };

string p[];

p = { d[1:3], "hello", d[4:5] };

The preceding example creates the dynamic array p with contents: “a”, “b”, “c”, “hello”, “d”, “e”.



Comments:

Leave a comment: