Professional Documents
Culture Documents
SV Arrays
SV Arrays
SV Arrays
ARRAYS:
Types of Arrays :
• Dynamic Array
• Associative Array
• Queues
• String
Syntax : int arr [2][3]; This array has total 2*3=6 elements.
array assignment
array1 = '{0,1,2,3,4,5};
array2 = '{0,1,2,3,4,5};
This example shows array declaration and array manipulation using for and foreach loop.
module fixedsize_array;
//declaration of array’s
int array4[4:0];
initial begin
//array initialization
array1 = '{0,1,2,3,4,5};
array2 = '{0,1,2,3,4,5};
array3 = '{'{0,1,2,3},'{4,5,6,7},'{8,9,10,11}};
$display("-------displaying array1-------");
$display("-------displaying array2-------");
$display("-------displaying array3-------");
end
endmodule
Simulator Output:
-------displaying array1-------
array1[0] = 0
array1[1] = 1
array1[2] = 2
array1[3] = 3
array1[4] = 4
array1[5] = 5
-------displaying array2-------
array2[0] = 5
array2[1] = 4
array2[2] = 3
array2[3] = 2
array2[4] = 1
array2[5] = 0
-------displaying array3-------
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
-------displaying uninitialized array4-------
array4[0] = 0
array4[1] = 0
array4[2] = 0
array4[3] = 0
array4[4] = 0
Packed and Unpacked Arrays:
The term packed array is used to refer to the dimensions declared before the data identifier name
The term unpacked array is used to refer to the dimensions declared after the data identifier name
Packed array:-
• Packed arrays can be of single bit data types like reg, logic, bit.
• Dimensions declared before the data identifier name.
Syntax: logic[15:0] arr;
• Packed arrays are fixed sized , single or multidimensional array.
• Packed array is guaranteed to be represented as a contiguous set of bits.
• Syntax : bit [2:0][7:0] array1;
• The below example shows storing packed array as a contiguous set of bits.
7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4
UnPacked array
• A dynamic array is unpacked array whose size can be set or changed at runtime.
• During the runtime we can increase size and decrease the size.
Dynamic array can be represented as [ ]; Syntax : int array_1 [ ];
• Array creation : array_1 = new [10];
• data_type array_name [ ];
//declaration
int d_array2[ ];
//memory allocation
//array initialization
d_array1 = {0,1,2,3};
foreach(d_array2[j]) d_array2[j] = j;
//Change the length of the array after declaration/initialization
In the above syntax, d_array1 will get allotted with 10 new memory locations and old values of d_array1 will get deleted.
old values of d_array1 elements can be retained by extending the current array by using the below syntax.
d_array1 = new[10](d_array1);
//delete array
d_array1.delete;
int d_array2[];
initial begin
//memory allocation
d_array1 = new[4];
d_array2 = new[6];
//array initialization
d_array1 = {0,1,2,3};
foreach(d_array2[j]) d_array2[j] = j;
$display("---------------------------------");
$display("---------------------------------");
end
endmodule
Simulator Output:
Size of d_array1 0
Size of d_array2 0
Size of d_array1 4
Size of d_array2 6
d_aaray1[0] = 0
d_aaray1[1] = 1
d_aaray1[2] = 2
d_aaray1[3] = 3
---------------------------------
d_aaray2[0] = 0
d_aaray2[1] = 1
d_aaray2[2] = 2
d_aaray2[3] = 3
d_aaray2[4] = 4
d_aaray2[5] = 5
---------------------------------
module dynamic_array;
int d_array2[];
initial begin
//memory allocation
d_array1 = new[2];
d_array2 = new[3];
//array initialization
d_array1 = {2,3};
foreach(d_array2[j]) d_array2[j] = j;
$display("--- d_array1 Values are ---");
$display("---------------------------------");
$display("---------------------------------");
//delete array
d_array1.delete;
d_array2.delete;
end
endmodule
Simulator Output:
d_aaray1[0] = 2
d_aaray1[1] = 3
---------------------------------
--- d_array2 Values are ---
d_aaray2[0] = 0
d_aaray2[1] = 1
d_aaray2[2] = 2
---------------------------------
Size of d_array1 0
Size of d_array2 0
The below example shows the increasing dynamic array size by overriding and retaining old values.
module dynamic_array;
int d_array2[];
initial begin
//memory allocation
d_array1 = new[2];
d_array2 = new[3];
//array initialization
d_array1 = {2,3};
foreach(d_array2[j]) d_array2[j] = j;
end
endmodule
Simulator Output:
----- d_array1 Values are -----
d_aaray1[0] = 2
d_aaray1[1] = 3
d_aaray2[0] = 0
d_aaray2[1] = 1
d_aaray2[2] = 2
d_aaray1[0] = 0
d_aaray1[1] = 0
d_aaray1[2] = 0
d_aaray1[3] = 0
d_aaray2[0] = 0
d_aaray2[1] = 1
d_aaray2[2] = 2
d_aaray2[3] = 0
d_aaray2[4] = 0
array_1=new[50](array_1);
$display("display the size of the array_1=%0d",array_1.size());
array_1.delete();
$display("display the size of the array_1=%0d",array_1.size());
end
endmodule
Unpacked Array:-
• Unpacked arrays shall be declared by specifying the element ranges after the identifier
name.
7 6
7 6
7 6
Associative Array:-
Syntax:
Array Declaration
where:
data_type – data type of the array elements.
array_name – name of the associative array.
index_type – data-type to be used as an index, or *.
* indicates the array is indexed by any integral expression of arbitrary size.
module associative_array;
//array declaration
int a_array[*];
int index;
initial begin
repeat(3) begin
a_array[index] = index*2;
index=index+4;
end
$display("--------------------------------------------------------");
a_array.first(index);
a_array.last(index);
end
endmodule
Simulator Output:
a_array[0] = 0
a_array[4] = 8
a_array[8] = 16
--------------------------------------------------------
module associative_array;
//array declaration
int a_array[*];
int index;
initial begin
repeat(3) begin
a_array[index] = index*2;
index=index+4;
end
if(a_array.exists(8))
else
$display("Index 8 doesnt exists in a_array");
a_array.last(index);
a_array.prev(index);
a_array.next(index);
end
endmodule
Simulator Output:
entry is a_array[4] = 8
entry is a_array[8] = 16
module associative_array;
//array declaration
int a_array1[bit [7:0]]; //index type is bit [7:0] and entry type is int
initial begin
a_array1[5] = 10;
a_array1[8] = 20;
a_array2["GOOD_PKT"] = 1;
a_array2["BAD_PKT"] = 0;
foreach(a_array1[index])
$display("a_array1[%0d] = %0d",index,a_array1[index]);
foreach(a_array2[index])
$display("a_array2[%0s] = %0d",index,a_array2[index]);
end
endmodule
Simulator Output:
a_array1[5] = 10
a_array1[8] = 20
a_array2[BAD_PKT] = 0
a_array2[GOOD_PKT] = 1
Calling array.delete() method will delete the complete array, which leads to the deletion of all the entries of an array.
module associative_array;
//array declaration
int a_array[*];
int index;
initial begin
repeat(3) begin
a_array[index] = index*2;
index=index+4;
end
a_array.delete();
endmodule
Simulator Output:
a
i
r
n
r
t
a
y
a
_
r
1
r
[
a
1
y
]
_
=
1
5
[
0
*
0
]
;
;
a
i
r
n
r
t
a
i y
n _
d 1
[
2
] a
= r
7 r
0 a
0 y
; _
1
a [
r 5
r ]
a =
y 9
_ 0
1 0
[ ;
3
] a
= r
3 r
0 a
0 y
; _
1
a [
r 6
r ]
a =
y 1
_ 0
1 0
[ 0
4 ;
]
$display("number of entries array_1=
=
%0d",array_1.num());
2
0 foreach(array_1[i])
array_1.prev(index);
array_1.next(index);
if(array_1.exists(2))
$display("index 2 is
exists in array_1");
else
$display("inde
x 2 is not exists in
array_1"); if
(array_1.exists(7))
else
end
endmodule
code of Associative array using(string):
m n
o d
d e
u x
l ;
e
i
a n
s i
s t
o i
c a
; l
begin
i
n $display("number of entries array_1=
t %0d",array_1.num()); array_1["A"]=500;
a
a r
r r
r a
a y
y _
_ 1
1 [
[ "
* B
] "
; ]
=
i
7
n
0
t
0
;
i
a
a r
r r
r a
a y
y _
_ 1
1 [
[ "
" C
C "
" ]
] =
= 9
3 0
0 0
0 ;
;
a
a r
r r
r a
a y
y _
_ 1
1 [
[ "
" E
D "
" ]
] =
= 1
2 0
0 0
0 0
; ;
$display("number of entries array_1= array_1.first(index);
%0d",array_1.num());
$display("display the first index array_1=
foreach(array_1[i]) %0d",index,array_1[index]);
$display("number of entries array_1[%0s]= array_1.last(index);
%0d",i,array_1[i]);
$display("display the last index array_1=
%0d",index,array_1[index]);
array_1.prev(index);
$display("inde
x 2 is exists in
array_1"); else
$display("index 2
is not exists in
array_1");
if(array_1.exists(7)
)
else
end
endmodule
Queues:-
Queue Declaration
data_type queue_name[$];
where:
data_type – data type of the queue elements.
queue_name – name of the queue.
queue_1 = {0,1,2,3};
queue_4 = {“Red”,"Blue”,"Green”};
Unbounded Queue
SystemVerilog queue
Bounded Queue
Queue Methods:-
module queues_array;
//declaration
string queue_2[$];
initial begin
//Queue Initialization:
queue_1 = {0,1,2,3};
queue_2 = {"Red","Blue","Green"};
//Size-Method
queue_2.insert(1,"Orange");
//Delete Method
queue_2.delete(3);
foreach(queue_2[i])$display("\tqueue_2[%0d] = %0s",i,queue_2[i]);
end
endmodule
Simulator Output:
queue_1[0] = 0
queue_1[1] = 1
queue_1[2] = 2
queue_1[3] = 3
queue_2[0] = Red
queue_2[1] = Blue
queue_2[2] = Green
queue_2[0] = Red
queue_2[1] = Orange
queue_2[2] = Blue
queue_2[3] = Green
queue_2[1] = Orange
queue_2[2] = Blue
example ;
module queues_array;
//declaration
int lvar;
initial begin
//Queue Initialization:
queue_1 = {0,1,2,3};
//Size-Method
//Push_front Method
queue_1.push_front(22);
//Push_back Method
queue_1.push_back(44);
//Pop_front Method
lvar = queue_1.pop_front();
lvar = queue_1.pop_back();
end
endmodule
Simulator Output:
Queue_1 size is 4
The number of entries of the bounded queue is limited, push_back to the bounded queue (after the queue full
condition) will not impact any changes to the queue. push_front to the bounded queue (after the queue full
condition) will delete the last entry from queue and stores a new entry in the 0th index of the queue.
module queues_array;
//declaration
int queue[$:2];
int index;
int temp_var;
initial begin
//Queue Initialization:
queue = {7,3,1};
$display("\tqueue = %p",queue);
queue.push_back(10);
$display("\tqueue = %p",queue);
queue.push_front(10);
$display("\tqueue = %p",queue);
end
endmodule
Simulator Output:
queue = '{7, 3, 1}
queue = '{7, 3, 1}
queue = '{10, 7, 3}
Accessing random element of queue
In the below example, random queue entry will be accessed by using index. Unlike pop_front/pop_back option
queue entry will not get deleted on accessing with an index of the queue.
module queues_array;
//declaration
int queue[$];
int index;
int temp_var;
initial begin
//Queue Initialization:
queue = {7,3,1,0,8};
foreach(queue[i])
$display("\tqueue[%0d] = %0d",i,queue[i]);
$display("-------------------------------------\n");
temp_var = queue[index];
end //}
end
endmodule
Simulator Output:
queue[0] = 7
queue[1] = 3
queue[2] = 1
queue[3] = 0
queue[4] = 8
-------------------------------------
Calling queue.delete(index) method will delete the entry stored with ‘index’.
module queues;
//declaration
int queue[$];
int index;
int temp_var;
initial begin
//Queue Initialization:
queue = {7,3,1,0,8};
queue.delete(index);
queue.delete(index);
end
endmodule
Simulator Output:
Index 3 is deleted
Index 0 is deleted
Calling queue.delete() method will delete the complete queue, which leads to the deletion of all the entries of
the queue.
module qu_delete;
//queue declaration
int qu[$];
initial begin
qu.push_back(2);
qu.push_back(13);
qu.push_back(5);
qu.push_back(65);
$display("[Before-Delete] Queue size is %0d",qu.size());
qu.delete();
end
endmodule
Simulator Output: