<- Click here to Toggle

Home » Resources » Arrays » Introduction

Arrays - Introduction

An array is a collection of variables, all of the same type, and accessed using the same name plus one or more indices .

SystemVerilog uses the term “packed array” to refer to the dimensions declared before the object name (what Verilog-2001 refers to as the vector width). The term “unpacked array” is used to refer to the dimensions declared after the object name.

bit [7:0] c1; // packed array
real u [7:0]; // unpacked array

Packed and unpacked arrays:

A packed array is a mechanism for subdividing a vector into subfields which can be conveniently accessed as array elements. Consequently, a packed array is guaranteed to be represented as a contiguous set of bits.

Packed arrays can only be made of the single bit types (bit, logic, reg, wire, and the other net types) and recursively other packed arrays and packed structures.

Unpacked arrays can be made of any type. SystemVerilog enhances fixed-size unpacked arrays in that in addition to all other variable types, unpacked arrays can also be made of object handles.

The following operations can be performed on all arrays, packed or unpacked. The examples provided with these rules assume that A and B are arrays of the same shape and type.

» Reading and writing the array, e.g., A = B
» Reading and writing a slice of the array, e.g., A[i:j] = B[i:j]
» Reading and writing a variable slice of the array, e.g., A[x+:c] = B[y+:c]
» Reading and writing an element of the array, e.g., A[i] = B[i]
» Equality operations on the array or slice of the array, e.g. A==B, A[i:j] != B[i:j]

Array querying functions:

SystemVerilog provides new system functions to return information about an array. These are: $left, $right, $low, $high, $increment, $size, and $dimensions.

Array literals:

Array literals are syntactically similar to C initializers, but with the replicate operator ({{}}) allowed.

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

Arrays as arguments:

Arrays can be passed as arguments to tasks or functions. The rules that govern array argument passing by value are the same as for array assignment . When an array argument is passed by value, a copy of the array is passed to the called task or function. This is true for all array types: fixed-size, dynamic, or associative.

The declaration is :

task fun(int a[3:1][3:1]); ( declares task fun that takes one argument, a two dimensional array with each dimension of size three).