SystemVerilog Arrays

An array is a group of variables having the same data type. It can be accessed using an index value.

An index is a memory address and the array value is stored at that address.

Types of an array

  1. Fixed-size array in SystemVerilog
  2. Single dimensional array
  3. Multidimensional array
    a. Two-dimensional array.
    b. Three-dimensional array
  4. Packed and Unpacked array in SystemVerilog
  5. Dynamic array in SystemVerilog
  6. Associative array in SystemVerilog

Fixed-size array in SystemVerilog

Array size is fixed throughout the simulation. Its value will be initialized with a ‘0’ value.

Single dimensional array

single diamentional array

Array assignment

; initial begin foreach (array[i]) begin $display("array[%0d] = %0d", i, array[i]); end end endmodule

Output:

Multidimensional array

A multidimensional array is also known as an array of an array. In mathematics, we studied matrix, this can be understood as a multidimensional matrix.

Two-dimensional array

2d array

, ', ', ', ', '>; // Declaration and assignment int arr[6][2]; // Declaration arr[0][0] = 1; // Assignment arr[0][1] = 100; arr[1][0] = 2; arr[1][1] = 200; arr[2][0] = 3; arr[2][1] = 300; arr[3][0] = 4; arr[3][1] = 400; arr[4][0] = 5; arr[4][1] = 500; arr[5][0] = 6; arr[5][1] = 600;

Example:

, ', ', ', ', '>; initial begin foreach (array[i,j]) begin $display("array[%0d][%0d] = %0d", i,j, array[i][j]); end end endmodule

Output:

Three-dimensional array

Example:

, ', '>, ', ', '>, ', ', '> >; initial begin foreach (array[i,j, k]) begin $display("array[%0d][%0d][%0d] = %0d", i,j, k, array[i][j][k]); end end endmodule

Output:

Scalar Vs Vector

Scalar

The data object which does not have a specific range for bit/logic/reg is a scalar.

Vector

The data object which has a specific range for bit/logic/reg is a vector.

Packed and Unpacked array

Packed array

A packed array refers to the dimension mentioned before the variable or object name. This is also known as the vector width .

Memory allocation is always a continuous set of information that is accessed by an array index.

Example:

; initial begin foreach (array[i]) begin $display("array[%0h] = %0h", i, array[i]); end end endmodule

Output:

Unpacked array

An unpacked array refers to the dimension mentioned after the variable or object name.

Memory allocation may or may not be a continuous set of information.

unpacked array

Example:

, ', ' >; initial begin foreach (array[i,j]) begin $display("array[%0d][%0d] = %0d", i, j, array[i][j]); end end endmodule

Output:

Combination of a packed and unpacked array

All arrays mentioned above are types of static arrays .

Example:

, ', ' >; initial begin foreach (array[i,j]) begin $display("array[%0h][%0h] = %0h", i, j, array[i][j]); end end endmodule

Output: