Fixed-sized arrays :

(sometimes known as static arrays) whose size cannot change once their declaration is done
Fixed Size Arrays
Fixed Size Arrays
Multiple dimensions supported
Out-of-bounds write ignored
Out-of-bounds read returns x or 0 depending on type_
byte, shortints andintsare stored in a single longword
longintis stored in two longwords


Fixed Arrays can be classified
 Packed arrays
 Unpacked arrays

syntax

Type name [constant];

Single Dimension Array

Int array_lo_hi [0: 15] // 16 locations from [0] to [15]
Int array_lo_hi[16] // 16 locations from [0] to [15]

Multidimensional Array

Int array [0:7][0:3];
Int array [8][8];
Array [7][3]=0;

Simplified unpacked array declaration





Fixed Size Arrays: Initializing Unpacked Arrays

int array1 [4] = '{0,1,2,3};

array1 [0:2] ='{5,6,7};

array1 ='{{3}, '{3{8}}};

int md [2][3]='{'{0,1,2}, '{3,4,5}};

Packed Arrays

 Treated as a both an array and a single value
 Stored as contiguous bits
 Packed bit and word dimensions should be specified
 Dimensions must be specified in [lo:hi] format

Packed Arrays

 Packed arrays can be initialized at declaration using simple assignments
 The assignment can be a constant value, a concatenation of constant values or a replication of constant value

Packed Array: Initialization

logic [3:0][7:0] a = 32'h0;

logic [3:0][7:0] b = {16'hz, 16'h0};

logic [3:0][7:0] c = {16{2'b01}};

Example: Fixed Array

module array_basics;
initial
begin
//declaring and initialising a 1d array

int a[4]='{0,1,2,3};
integer b[7];
//declaring and initialising a 2d array
int c[4][4]='{'{0,2,3,4},'{1,2,2,1},'{5,5,4,7},'{4,5,7,2}};
$display ("A0=%d,A1=%d,A2=%d,A3=%d",a[0],a[1],a[2],a[3]);
a[0]=2;//Replacing the first location of the array
$display ("A0=%d,A1=%d,A2=%d,A3=%d",a[0],a[1],a[2],a[3]);
b[0:3]='{4{7}};
$display ("b=%p",b);//%p is the access specifier for printing the whole array
a = '{default:10}; //all indices of an array can be written a default value
$display ("A0=%d,A1=%d,A2=%d,A3=%d",a[0],a[1],a[2],a[3]);
foreach (a[i])//it iterates automatically without any need of explicitly incrementing the variable
a[i] *= a[i];
$display ("a=%p",a);
//displaying multidimensional array using foreach
foreach(c[i,j])
$display("c[%d][%d]=%d",i,j,c[i][j]);
//displaying Multiple Dimension array in matrix form using foreach(similar to nested for loop in other programming languages)

foreach(c[i])begin
$write("%2d:",i);
foreach(c[,j])
$write("%3d",c[i][j]);
$display;
end
end
//$display and $write perform the same function of printing out values on console. $display adds a “\n” character after the end of the line
endmodule

OUT PUT :

# ----------------OUTPUT RESULT -----------------------
# A0= 0,A1= 1,A2= 2,A3= 3
# A0= 2,A1= 1,A2= 2,A3= 3
# b='{7, 7, 7, 7, x, x, x} # A0= 10,A1= 10,A2= 10,A3= 10
# a='{100, 100, 100, 100}
# c[ 0][ 0]= 0
# c[ 0][ 1]= 2
# c[ 0][ 2]= 3
# c[ 0][ 3]= 4
# c[ 1][ 0]= 1
# c[ 1][ 1]= 2
# c[ 1][ 2]= 2
# c[ 1][ 3]= 1
# c[ 2][ 0]= 5
# c[ 2][ 1]= 5
# c[ 2][ 2]= 4
# c[ 2][ 3]= 7
# c[ 3][ 0]= 4
# c[ 3][ 1]= 5
# c[ 3][ 2]= 7
# c[ 3][ 3]= 2
# 0: 0 2 3 4
# 1: 1 2 2 1
# 2: 5 5 4 7
# 3: 4 5 7 2
# ------------------------------------------------------

Example 05: Fixed array

module fixed_array();
int fixed_array[2][4];
initial begin
foreach(fixed_array[i,k])
fixed_array[i][k] = i*k;
foreach(fixed_array[i,k])
$display(fixed_array[i][k]);
end
endmodule

OUT PUT :

# ----------------OUTPUT RESULT -----------------------
# 0
# 0
# 0
# 0
# 0
# 1
# 2
# 3
# ------------------------------------------------------

Example 05: ONE DIMENSIONAL ARRAY

module one_dimensional_arr ();

initial
begin

int arr[5] = {10,20,30,40,50};
for (int i=0;i<5;i++)
// Accessing each variable
$display("value of arr[%d] is %d \n", i, arr[i]);
end

endmodule

OUT PUT :

# ----------------OUTPUT RESULT -----------------------
# value of arr[ 0] is 10
# value of arr[ 1] is 20
# value of arr[ 2] is 30
# value of arr[ 3] is 40
# value of arr[ 4] is 50
# ------------------------------------------------------