Presentation of Arrays
Presentation of Arrays
Arrays in SystemVerilog
A Vector is a single element that is n-bits wide while Arrays are multiple
elements that are 1-bit or n-bits wide. An array is a collection of variables,
all of the same type, and accessed using the same name plus one or more
indices.
Sample Program :
bit [3:0][7:0] m_data;
initial begin;
m_data = 32’h0102_0304; //Assign to 32 contiguous bits
//display 2-d packed array as a contiguous set of bits
$display ("m_data = 0x%h", m_data);
//display 1 byte each stored at m_data[0]...m_data[3]
for (int i = 0; i < 4; i++)
begin
$display ("m_data[%0d] = 0x%h", i, m_data[i]);
end
end
endmodule
Resultant Output:
m_data = 0x01020304
m_data[0] = 0x04
m_data[1] = 0x03
m_data[2] = 0x02
m_data[3] = 0x01
In this example, we declare a 2-D packed array called “m_data.” Note that
the dimensions are on the left side of the array name. The way to read this
array is that it has four locations/entries (rows) of 8-bits (columns) each
row. The [7:0] represents a byte and [3:0] represents four locations where
these bytes are stored. And since this is packed array, the entire array is
treated as a single contiguous set of bits. Since there are four entries (8-bits
each), the total dimension is 4*8 = 32 bits. Hence, we can assign the entire
array with a single value (32’h 0102_0304). When we display “m_data,” it
comes out as a contiguous set of bits as shown in the simulation log. We
then display each of the four locations and see 8-bits stored at each of these
locations, in the simulation log
In the initial block, the first 16 bits ([1:0][7:0]) of each of the three ([2:0])
locations of the array are assigned with different values. Then, the entire
array is assigned with a single 48-bit hex value (0xcafe_face_0708).
The first $display statement outputs the value of "m_data" as a 48-bit hex
value, which is a contiguous set of bits representing the entire array.
The second $display statement outputs the new value of "m_data" as a 48-
bit hex value after the entire array is assigned with a single value.
The two nested foreach loops iterate over every element in the array and
output their values. The first loop iterates over the first dimension of the
array and outputs the index of the current layer along with the
corresponding 16-bit value. The second loop iterates over the first two
dimensions of the array and outputs the indices of the current layer and
row along with the corresponding 8-bit value.
Sample Program :
module PU;
logic [31:0] v1 [7:0]; //1-D packed & 1-D unpacked (memory)
initial begin
//Array Index 7 of unpacked
v1[7] = 'h FF_FF_FF_FF;
//equivalent to v1[7][31:0]
$display(v1);
//Array Index 6 of unpacked; 31:0 of packed
v1[6][31:0] = 'h 11_11_11_11;
$display(v1);
//Array Index 5 of unpacked; 15:0 of packed
v1[5][15:0] = 'h aa_aa;
$display(v1);
//Array Index 4 of unpacked; 0th bit of packed
v1[4][0] = 1;
$display(v1);
End
Endmodule
Resultant Output:
'{‘h ffffffff, ‘h xxxxxxxx, ‘h xxxxxxxx, ‘h xxxxxxxx, ‘h xxxxxxxx, ‘h xxxxxxxx, ‘h
xxxxxxxx, ‘h xxxxxxxx} '{‘h ffffffff, ‘h 11111111, ‘h xxxxxxxx, ‘h xxxxxxxx, ‘h
xxxxxxxx, ‘h xxxxxxxx, ‘h xxxxxxxx, ‘h xxxxxxxx} '{‘h ffffffff, ‘h 11111111, ‘h
xxxxaaaa, ‘h xxxxxxxx, ‘h xxxxxxxx, ‘h xxxxxxxx, ‘h xxxxxxxx, ‘h xxxxxxxx}
'{‘h ffffffff, ‘h 11111111, ‘h xxxxaaaa, ‘h xxxxxxx1, ‘h xxxxxxxx, ‘h xxxxxxxx,
‘h xxxxxxxx, ‘h xxxxxxxx
This code block defines a module called "PU" that contains a 1-dimensional
packed and 1-dimensional unpacked memory array called "v1".
The first assignment assigns the hex value 0xFFFFFFFF to index 7 of the
unpacked array, which is equivalent to assigning the same value to all 32
bits of v1[7]. The $display statement outputs the entire contents of the
array after this assignment.
The second assignment assigns the hex value 0x11111111 to the 32 least
significant bits of index 6 of the unpacked array. The $display statement the
contents of the array again after this assignment.
The third assignment assigns the hex value 0xAAAA to the 16 least
significant bits of index 5 of the unpacked array. The $display statement
outputs the contents of the array again after this assignment.
The fourth assignment assigns the value 1 to the least significant bit of
index 4 of the unpacked array. The $display statement outputs the contents
of the array again after this assignment.
Overall, this code block demonstrates the use of packed and unpacked
notation for accessing individual bits and groups of bits within a larger data
structure.
THANK YOU,
HIMANSHU PANJWANI.