SystemVerilog 2d array

SystemVerilog 2d array initialization

The two-dimensional array is an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and columns.

SystemVerilog 2D array Syntax

data_type array_name [rows][columns];

SystemVerilog 2D array declaration

int array [2:0][3:0];

The data in a two-dimensional array is stored in a tabular form as shown in the below diagram.

SystemVerilog Multidimensional array
SystemVerilog two-dimensional array

SystemVerilog 2D array initialization

array = '{ '{0,1,2,3},'{4,5,6,7},'{8,9,10,11}};

SystemVerilog 2D array example

module two_d_array;
  //declaration of array’s
  int array[2:0][3:0];        //2 dimension array
 
  initial begin
    //array initialization
    array = '{'{0,1,2,3},'{4,5,6,7},'{8,9,10,11}};
 
    //displaying array elements 
    $display("-------displaying 2d array-------");
    foreach(array[i,j]) $display("\t array[%0d][%0d] = %0d",i,j,array[i][j]);
  end
endmodule

Simulator Output

-------displaying 2d array-------
array3[2][3] = 0
array3[2][2] = 1
array3[2][1] = 2
array3[2][0] = 3
array3[1][3] = 4
array3[1][2] = 5
array3[1][1] = 6
array3[1][0] = 7
array3[0][3] = 8
array3[0][2] = 9
array3[0][1] = 10
array3[0][0] = 11

Click to execute on