Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

3.

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.

3.1 Packed and UnPacked Arrays:


The main difference between an unpacked array and a packed array is that
an unpacked array is not guaranteed to be represented as a contiguous set
of bits, while a packed array is guaranteed to be represented as a
contiguous set of bits. Other way to look at the difference is that when a
packed array appears as a primary, it is treated as a single vector.

➢ What do you mean by contiguous set of bits ??


A contiguous set of bits refers to a group of bits that are adjacent to each
other and occupy consecutive positions within a larger data structure, such
as a byte, word, or register. For example, in a 16-bit register, bits 0-15 form
a contiguous set of bits because they are adjacent to each other and occupy
consecutive positions within the register. In contrast, bits 0-7 and 12-15 do
not form a contiguous set of bits because they are not
adjacent to each other.

Logic [31:0] v1 [7:0] ;


// This line of code declares an array called "v1" of 8 elements, each
element being a 32-bit logic data type. The array is indexed from 0 to 7.

bit up [3:0]; // 1-D unpacked array


// This line of code declares a variable called "up" of 4 bits. It can hold
values of either 0 or 1 (representing logic low or high). The variable is
indexed from 0 to 3.
This is unpacked array representation..

bit [3:0] p; // 1-D packed array

This is packed array representation..


You can see that 1D packed array is quite similar to a vector.

3.1.1 2-D Packed Array:


bit [2:0][3:0] a;
this represents 3 rows and 4 columns array(more like a matrix). There are
3 subfields and each field is 4 bits wide.

a[2][3] a[2][2] a[2][1] a[2][0]


a[1][3] a[1][2] a[1][1] a[1][0]
a[0][3] a[0][2] a[0][1] a[0][0]

If we write a[0][0] = 3’b001;


a[1][2] = 3’b011;
a[2][1] = 3’b101;
then, it’ll look something like this…
101
011
001
// 4 entries(rows) of 8 bits(columns) each
Total packed dimensions(contiguous bits) = 4*8 = 32 bits

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

This code block declares a 2-dimensional packed array called "m_data" of 4


rows and 8 columns, where each element is a 4-bit data type. The "initial"
block assigns the hex value 0x01020304 to the entire 32 bits of the array,
which is a contiguous set of bits. The first $display statement outputs the
value of "m_data" as a 32-bit hex value. The second $display statement
outputs each byte stored at m_data[0] through m_data[3], where each byte
is displayed as a 2-digit hex value. The for loop iterates over each row of
the array and outputs the row index and the corresponding byte value.

3.1.2 3D Packed Arrays :

bit [2:0][1:0][7:0] m_data;


// This line declares a 3-dimensional packed array called "m_data" with 3
layers, 2 rows and 8 columns. The first dimension is indexed from 0 to 2,
the second dimension is indexed from 0 to 1, and the third dimension is
indexed from 0 to 7. Each element of the array is a 1-byte (8-bit) data type.
Sample Program:
module tb;
bit [2:0][1:0][7:0] m_data; // 3-D packed array
initial begin
// Assign 16-bits ([1:0][7:0]) at each of the three
//([2:0])locations
m_data[0] = 16'h0102;
m_data[1] = 16'h0304;
m_data[2] = 16'h0506;
// m_data as a single packed value
$display ("m_data = 0x%h", m_data);
//Assign the entire array with a single value
m_data = 48'hcafe_face_0708;
// m_data as a single packed value
$display("m_data = 0x%h", m_data);
foreach (m_data[i])
begin
$display ("m_data[%0d] = 0x%h", i, m_data[i]);
foreach (m_data[, j])
begin
$display ("m_data[%0d][%0d] = 0x%h", i, j, m_
data[i][j]);
end
end
end
endmodule
Resultant Output:
m_data = 0x050603040102
m_data = 0xcafeface0708
m_data[2] = 0xcafe
m_data[2][1] = 0xca
m_data[2][0] = 0xfe
m_data[1] = 0xface
m_data[1][1] = 0xfa
m_data[1][0] = 0xce
m_data[0] = 0x0708
m_data[0][1] = 0x07
m_data[0][0] = 0x08

This code block is a testbench module that demonstrates the use of a 3-


dimensional packed array called "m_data".

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.

3.1.3 1D Packed and 1D Unpacked Array

Logic [31:0] v1 [7:0] ;


// This line of code declares an array called "v1" of 8 elements, each
element being a 32-bit logic data type. The array is indexed from 0 to 7.

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".

In the initial block, several assignments are made to different elements of


the array using both packed and unpacked notation.

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.

You might also like