Friday, March 02, 2012

Arrays - Multiple Dimensions

Arrays - Multiple Dimensions

Like Verilog memories, the dimensions following the type set the packed size. The dimensions following the instance set the unpacked size.

bit [3:0] [7:0] joe [1:10]; // 10 entries of 4 bytes (packed into 32 bits) can be used as follows:
joe[9] = joe[8] + 1; // 4 byte add
joe[7][3:2] = joe[6][1:0]; // 2 byte copy

Note that the dimensions declared following the type and before the name ([3:0][7:0] in the preceding declaration) vary more rapidly than the dimensions following the name ([1:10] in the preceding declaration)

In a list of dimensions, the right-most one varies most rapidly, as in C. However a packed dimension varies more rapidly than an unpacked one.

bit [1:10] foo1 [1:5]; // 1 to 10 varies most rapidly; compatible withVerilog-2001 arrays
bit foo2 [1:5] [1:10]; // 1 to 10 varies most rapidly, compatible with C
bit [1:5] [1:10] foo3; // 1 to 10 varies most rapidly
bit [1:5] [1:6] foo4 [1:7] [1:8]; // 1 to 6 varies most rapidly, followed by 1 to 5, then 1 to 8 and then 1 to 7

Multiple packed dimensions can also be defined in stages with typedef.

typedef bit [1:5] bsix;
bsix [1:10] foo5; // 1 to 5 varies most rapidly

Multiple unpacked dimensions can also be defined in stages with typedef.

typedef bsix mem_type [0:3]; // array of four ’bsix’ elements
mem_type bar [0:7]; // array of eight ’mem_type’ elements

When the array is used with a smaller number of dimensions, these have to be the slowest varying ones.

bit [9:0] foo6;
foo6 = foo1[2]; // a 10 bit quantity.

As in Verilog-2001, a comma-separated list of array declarations can be made. All arrays in the list shall have the same data type and the same packed array dimensions.

bit [7:0] [31:0] foo7 [1:5] [1:10], foo8 [0:255]; // two arrays declared

If an index expression is out of the address bounds or if any bit in the address is X or Z, then the index shall be invalid. The result of reading from an array with an invalid index shall return the default uninitialized value for the array element type. Writing to an array with an invalid index shall perform no operation. Implementations can generate a warning if an invalid index occurs for a read or write operation of an array.

No comments:

Post a Comment

Popular Posts