Multidimensional array
Table of Contents
A multidimensional array is an array containing one or more arrays.
Multidimensional arrays can be of more than two levels deep. However, arrays more than three levels deep are hard to manage.
Number of indices required to access an array element differs on array dimension,
- Two indices are required to access a two-dimensional array element
- Three indices are required to access a three-dimensional array element
Multidimensional Dynamic array
SystemVerilog dynamic array can be,
- regular array
- irregular array
regular array
A regular array is a multidimensional array with member arrays of the same sizes.
for example,
2-D array with the number of columns same for all the rows
In below 3 x 2 array diagram,
All the 3 rows have 2 columns.
Irregular array
An irregular array is a multidimensional array with member arrays of different sizes.
for example,
2-D array with the number of columns different for the rows
In below array with 3 rows,
row 1, 2 and 3 have 2, 1 and 3 columns respectively.
2-Dimensional dynamic array example
Regular array
Below is a 3×2 2D dynamic array example.
In example shows the array declaration, creation, assignment and displaying the array elements.
program dynamic_array; //dynamic array declaration bit [7:0] d_array[][]; initial begin //memory allocation d_array = new[3]; foreach(d_array[i]) d_array[i] = new[2]; //assigning random value to elements foreach(d_array[i,j]) d_array[i][j] = $random; //displaying array elements foreach(d_array[i,j]) $display("\td_aaray[%0d,%0d] = %0d",i,j, d_array[i][j]); end endprogram
Simulator Output
d_aaray[0,0] = 36 d_aaray[0,1] = 129 d_aaray[1,0] = 9 d_aaray[1,1] = 99 d_aaray[2,0] = 13 d_aaray[2,1] = 141
Irregular array
Below is a 2D irregular dynamic array example.
In example shows the array declaration, creation, assignment and displaying the array elements.
program dynamic_array; //dynamic array declaration bit [7:0] d_array[][]; initial begin //memory allocation d_array = new[3]; d_array[0] = new[2]; d_array[1] = new[1]; d_array[2] = new[4]; //assigning random value to elements foreach(d_array[i,j]) d_array[i][j] = $random; //displaying array elements foreach(d_array[i,j]) $display("\td_aaray[%0d,%0d] = %0d",i,j, d_array[i][j]); end endprogram
Simulator Output
d_aaray[0,0] = 36 d_aaray[0,1] = 129 d_aaray[1,0] = 9 d_aaray[2,0] = 99 d_aaray[2,1] = 13 d_aaray[2,2] = 141 d_aaray[2,3] = 101
3-Dimensional dynamic array example
Regular array
Below is a 2x2x4 3D dynamic array example.
In example shows the array declaration, creation, assignment and displaying the array elements.
program dynamic_array; //dynamic array declaration bit [7:0] d_array[][][]; initial begin //memory allocation d_array = new[2]; foreach(d_array[i]) begin //{ d_array[i] = new[2]; foreach(d_array[i,j]) begin //{ d_array[i][j] = new[4]; end //} end //} //assigning random value to elements foreach(d_array[i,j,k]) d_array[i][j][k] = $random; //displaying array elements foreach(d_array[i,j,k]) $display("\td_aaray[%0d,%0d,%0d] = %0d",i,j,k,d_array[i][j][k]); end endprogram
Simulator Output
d_aaray[0,0,0] = 36 d_aaray[0,0,1] = 129 d_aaray[0,0,2] = 9 d_aaray[0,0,3] = 99 d_aaray[0,1,0] = 13 d_aaray[0,1,1] = 141 d_aaray[0,1,2] = 101 d_aaray[0,1,3] = 18 d_aaray[1,0,0] = 1 d_aaray[1,0,1] = 13 d_aaray[1,0,2] = 118 d_aaray[1,0,3] = 61 d_aaray[1,1,0] = 237 d_aaray[1,1,1] = 140 d_aaray[1,1,2] = 249 d_aaray[1,1,3] = 198
Irregular array
Below is a 3D irregular dynamic array example.
In example shows the array declaration, creation, assignment and displaying the array elements.
program dynamic_array; //dynamic array declaration bit [7:0] d_array[][][]; initial begin //memory allocation d_array = new[2]; d_array[0] = new[1]; d_array[1] = new[2]; d_array[0][0] = new[3]; d_array[1][0] = new[2]; d_array[1][1] = new[1]; //assigning random value to elements foreach(d_array[i,j,k]) d_array[i][j][k] = $random; //displaying array elements foreach(d_array[i,j,k]) $display("\td_aaray[%0d,%0d,%0d] = %0d",i,j,k,d_array[i][j][k]); end endprogram
Simulator Output
d_aaray[0,0,0] = 36 d_aaray[0,0,1] = 129 d_aaray[0,0,2] = 9 d_aaray[1,0,0] = 99 d_aaray[1,0,1] = 13 d_aaray[1,1,0] = 141