Arrays in SystemVerilog
An array is a collection of similar variables, accessible via indices and the array name. Verilog-2001 supports multi-dimensional arrays and net and variable type array declarations are valid. Verilog restricts access to only one element or a slice of an array at a time.
-
reg r [0:63]; // 1 dimensional unpacked array
-
wire [0:7] count [0:144]; // a 1-dimensional unpacked array 145 8-bit nets
-
int i [0:7] [0:15]; // a 2-dimensional unpacked array
-
-
// Packed Arrays
-
wire [3:0] count; // packed array of 4-bits
-
-
wire [3:0] [1:0] addr; // 2-dimensional packed array
-
wire [7:0] addr_out = addr; // entire packed array
-
-
// Initializing Packed array
-
wire [7:0] [1:0] data = 16'h0;
-
// Initializing Unpacked array
-
wire data [0:1] [0:3] = '{ '{11, 12, 13, 14}, '{2, 3, 4, 5} }
-
int addr [0:1] [0:15] = '{ default: 2'h3 } // initialize all elements to 2'b11
SV extends the unpacked arrays to new types like int, shortint, logic, bit, byte, real and shortreal. SV also allows accessing all elements of an array at a time unlike Verilog. For packed arrays, multi-dimensional arrays are supported by SV. The entire packed array is stored as contiguous order of bits and arithmetic/logical operations are supported.
Packed arrays can be assigned to another packed array even if their sizes do not match. However, unpacked arrays can be assigned to another unpacked array only if the vector and dimension size are equal. To assign an unpacked array to a packed array or vice versa, bit stream casting needs to be done. A bit-stream cast converts an unpacked array into a stream of bits using the SV static cast operator '. Both packed and unpacked arrays are synthesizable.
SV also supports dynamic arrays for higher levels of modeling (not synthesizable) - A dynamic array is one dimension of an unpacked array, the size of which is set or changed during runtime.
-
bit [3:0] crc [];// Dynamic array of 4-bit vectors
SV supports new[], size() and delete() methods similar to C for dynamic arrays.
Associative arrays are also supported by SV - the array index is not restricted to integral values (like int, shortint) and can be any data type.
-
integer assoc[*]; // * is wildcard indicates unspecified but integral value index
-
bit [7:0] assoc [string]; // indexed by string
SV supports in-built methods for associative arrays as tabulated below.
Associative Methods |
Description |
|
<array>.num( )
|
Returns number of entries in the associative array. |
|
<array>.delete(N)
|
if index N is not specified, deletes all elements of the |
|
<array>.exists(N) |
checks if element exists at that particular index of the array. returns 1 if true. index is mandatory |
|
<array>.first(N) |
assigns to the index variable N the value of the smallest |
|
<array>.last(N) |
assigns to the index variable N the value of the largest index in the array, returns 1 if array is not empty, 0 otherwise |
| <array>.next(N) | finds the entry whose index is greater than current index N and assigns the index variable to that next entry. Retruns 1 if true |
| <array>.prev(N) | finds the entry whose index is smaller than current index N and assigns the index variable to that previous entry. Retruns 1 if true |