SV Arrays

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 39

SYSTEM VERILOG - ARRAYS

ARRAYS:

An array is collection of variables of same type, and accessed using the


same name plus one or more indices.

Types of Arrays :

• Fixed Size Arrays

• Packed and Un-packed Arrays

• Dynamic Array

• Associative Array

• Queues

• String

Note: We have some array methods also.

int array1 [6];            //fixed size single dimension array

int array2 [5:0];          //fixed size single dimension array

int array3 [3:0][2:0];     //fixed size multi dimension array

bit [7:0] array4[2:0];     //unpacked array declaration

bit [2:0][7:0] array5;     //packed array declaration

bit [2:0][7:0] array6 [3]; //mixed packed and unpacked array


In system Verilog we have following type arrays:-

• Fixed Size Array:


In fixed size array, array size will be constant throughout the simulation, Once the
array is declared no need to create it. By default, the array will be initialized with value ‘0’.
• Single dimensional array
Syntax : int array1 [6]; int array2 [5:0];
• Multidimensional array are also known as an array of arrays.

Two dimensional array

Syntax : int arr [2][3]; This array has total 2*3=6 elements.

Three dimensional array:

Syntax : int array1 [2:0][3:0]; 12 elements

array assignment

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}};

Fixed Size Array Example

This example shows array declaration and array manipulation using for and foreach loop.

module fixedsize_array;

  //declaration of array’s

int array1[6];               //single dimension array


  int array2[5:0];             //single dimension array

  int array3[2:0][3:0];        //multi dimension array

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}};

    //displaying array elements

    $display("-------displaying array1-------");

    foreach(array1[i]) $display("\t array1[%0d] = %0d",i,array1[i]);

    $display("-------displaying array2-------");

    for(int i=0;i<6;i++) $display("\t array2[%0d] = %0d",i,array2[i]);

    $display("-------displaying array3-------");

    foreach(array3[i,j]) $display("\t array3[%0d][%0d] = %0d",i,j,array3[i][j]);

    $display("-------displaying uninitialized array4-------");

    for(int i=0;i<5;i++) $display("\t array4[%0d] = %0d",i,array4[i]);

  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

bit [7:0] temp_var; // packed array of bit types

bit temp_var [7:0]; // unpacked array of real types

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

Unpacked arrays can be of any data type.


Unpacked arrays shall be declared by specifying the element ranges after the identifier name.
An unpacked array may or may not be so represented as a contiguous set of bits.

Example bit [7:0] array4[2:0];


Dynamic 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 [ ];

• data_type is the data type of the array elements.


• Methods : new[ ]    –> allocates the storage.
size( )    –> returns the current size of a dynamic array.
delete( ) –> empties the array, resulting in a zero-sized array.

//declaration

bit [7:0] d_array1[ ];

int d_array2[ ];

//memory allocation

d_array1 = new[4]; //dynamic array of 4 elements

d_array2 = new[6]; //dynamic array of 6 elements

//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

d_array1 = new[10]; //dynamic array of 10 elements

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.

//Allocate 6 new elements and retain values of 4 elements.

d_array1 = new[10](d_array1);

//delete array

d_array1.delete;

array_name.delete() method will delete the array.

Dynamic Array Declaration, Allocation and Initialization.


module dynamic_array;

//dynamic array declaration

bit [7:0] d_array1[];

int d_array2[];

initial begin

$display("Before Memory Allocation");

$display("\tSize of d_array1 %0d",d_array1.size());

$display("\tSize of d_array2 %0d",d_array2.size());

//memory allocation

d_array1 = new[4];

d_array2 = new[6];

$display("After Memory Allocation");

$display("\tSize of d_array1 %0d",d_array1.size());

$display("\tSize of d_array2 %0d",d_array2.size());

//array initialization

d_array1 = {0,1,2,3};

foreach(d_array2[j]) d_array2[j] = j;

$display("--- d_array1 Values are ---");

foreach(d_array1[i]) $display("\td_aaray1[%0d] = %0d",i, d_array1[i]);

$display("---------------------------------");

$display("--- d_array2 Values are ---");


foreach(d_array2[i]) $display("\td_aaray2[%0d] = %0d",i, d_array2[i]);

$display("---------------------------------");

end

endmodule

Simulator Output:

Before Memory Allocation

Size of d_array1 0

Size of d_array2 0

After Memory Allocation

Size of d_array1 4

Size of d_array2 6

--- d_array1 Values are ---

d_aaray1[0] = 0

d_aaray1[1] = 1

d_aaray1[2] = 2

d_aaray1[3] = 3

---------------------------------

--- d_array2 Values are ---

d_aaray2[0] = 0
d_aaray2[1] = 1

d_aaray2[2] = 2

d_aaray2[3] = 3

d_aaray2[4] = 4

d_aaray2[5] = 5

---------------------------------

Dynamic Array delete method

module dynamic_array;

//dynamic array declaration

bit [7:0] d_array1[];

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 ---");

foreach(d_array1[i]) $display("\td_aaray1[%0d] = %0d",i, d_array1[i]);

$display("---------------------------------");

$display("--- d_array2 Values are ---");

foreach(d_array2[i]) $display("\td_aaray2[%0d] = %0d",i, d_array2[i]);

$display("---------------------------------");

//delete array

d_array1.delete;

d_array2.delete;

$display("After Array Delete");

$display("\tSize of d_array1 %0d",d_array1.size());

$display("\tSize of d_array2 %0d",d_array2.size());

end

endmodule

Simulator Output:

--- d_array1 Values are ---

d_aaray1[0] = 2

d_aaray1[1] = 3

---------------------------------
--- d_array2 Values are ---

d_aaray2[0] = 0

d_aaray2[1] = 1

d_aaray2[2] = 2

---------------------------------

After Array Delete

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;

//dynamic array declaration

bit [7:0] d_array1[];

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 -----");

foreach(d_array1[i]) $display("\td_aaray1[%0d] = %0d",i, d_array1[i]);

$display("----- d_array2 Values are -----");

foreach(d_array2[i]) $display("\td_aaray2[%0d] = %0d",i, d_array2[i]);

//Increasing the size by overriding the old values

d_array1 = new[4]; //Create dynamic array of 4 elements

$display("Size of Array d_array1 %0d",d_array1.size());

$display("----- d_array1 Values are -----");

foreach(d_array1[i]) $display("\td_aaray1[%0d] = %0d",i, d_array1[i]);

//Increasing the size by retaining the old values

d_array2 = new[5](d_array2); //Create dynamic array of 5 elements, retaining old values

$display("Size of Array d_array2 %0d",d_array2.size());

$display("----- d_array2 Values are -----");

foreach(d_array2[i]) $display("\td_aaray2[%0d] = %0d",i, d_array2[i]);

end

endmodule

Simulator Output:
----- d_array1 Values are -----

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 Array d_array1 4

----- d_array1 Values are -----

d_aaray1[0] = 0

d_aaray1[1] = 0

d_aaray1[2] = 0

d_aaray1[3] = 0

Size of Array d_array2 5

----- d_array2 Values are -----

d_aaray2[0] = 0

d_aaray2[1] = 1

d_aaray2[2] = 2

d_aaray2[3] = 0

d_aaray2[4] = 0

Sample code for Dynamic array:


module sv; int array_1[]; initial
begin

$display("display the size of the


array_1=%0d",array_1.size());
array_1=new[10];

$display("display the size of the array_1=%0d",array_1.size());

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 can be of any data type.

• Unpacked arrays shall be declared by specifying the element ranges after the identifier
name.

• An unpacked array may or may not be so presented as a contiguous set of bits.


• Syntax : bit array2[2:0];

• Below diagram shows storing unpacked array as a non-contiguous set of bits.

7 6

7 6

7 6
Associative Array:-

• Associative array has non-continuous elements.


• Associative array can be represented as [*] , * indicates index values.
• Index values can be either integers(1,2,3) or strings(a,b,c).
• For large memory storing we are using associative array.
• Associative arrays allocate the storage only when it is used, unless like in the
dynamic array we need to allocate memory before using it.
• When the size of a collection is unknown or the data space is sparse, an associative array is a better
option.
• Dynamic arrays are useful for contiguous collections of variables whose number changes dynamically.

Syntax:

int array_1[*] //associative array of integer (unspecified index).

Array Declaration

data_type array_name [ index_type ];

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.

int a_array1[*] ;            // associative array of integer (unspecified index)

bit [31:0] a_array2[string]; // associative array of 32-bit, indexed by string

ev_array [myClass];          //associative array of event,indexed by class


Associative array methods are:-

• num(), returns the number of entries in the associative array

• first(var), assigns the value of first index to the variable var

• last(var), assigns the value of last index to the variable var

• next(var), assigns the value of next index to the variable var

• perv(var), assigns the value of previous index to the variable var

• delete(index), removes the entry at the specified index.exa_array.delete(index)

• exists(index) returns 1 if an element exists at the specified index else returns 0

Example-1 : Associative Array Declaration, num(), first() and last() method’s.

module associative_array;

//array declaration

int a_array[*];

int index;

initial begin

//allocating array and assigning value to it

repeat(3) begin

a_array[index] = index*2;

index=index+4;

end

//num() –Associative array method

$display("\tNumber of entries in a_array is %0d",a_array.num());

$display("--- Associative array a_array entries and Values are ---");

foreach(a_array[i]) $display("\ta_array[%0d] \t = %0d",i,a_array[i]);

$display("--------------------------------------------------------");

//first()-Associative array method

a_array.first(index);

$display("\First entry is \t a_array[%0d] = %0d",index,a_array[index]);


//last()-Associative array method

a_array.last(index);

$display("\Last entry is \t a_array[%0d] = %0d",index,a_array[index]);

end

endmodule

Simulator Output:

Number of entries in a_array is 3

--- Associative array a_array entries and Values are ---

a_array[0] = 0

a_array[4] = 8

a_array[8] = 16

--------------------------------------------------------

First entry is a_array[0] = 0

Last entry is a_array[8] = 16

exists(), prev() and last() method’s

Example-2 : Associative Array – exists(), prev() and last() method’s.

module associative_array;

//array declaration

int a_array[*];

int index;

initial begin

//allocating array and assigning value to it

repeat(3) begin

a_array[index] = index*2;

index=index+4;

end

//exists()-Associative array method

if(a_array.exists(8))

$display("Index 8 exists in a_array");

else
$display("Index 8 doesnt exists in a_array");

//last()-Associative array method

a_array.last(index);

$display("Last entry is a_array[%0d] = %0d",index,a_array[index]);

//prev()-Associative array method

a_array.prev(index);

$display("entry is a_array[%0d] = %0d",index,a_array[index]);

//next()-Associative array method

a_array.next(index);

$display("entry is a_array[%0d] = %0d",index,a_array[index]);

end

endmodule

Simulator Output:

Index 8 exists in a_array

Last entry is a_array[8] = 16

entry is a_array[4] = 8

entry is a_array[8] = 16

bit and string index type

Example-3: Associative Array – bit and string index type.

module associative_array;

//array declaration

int a_array1[bit [7:0]]; //index type is bit [7:0] and entry type is int

bit a_array2[string] ; //index type is string and entry type is bit

initial begin

//allocating array and assigning value to it

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

Deleting complete Assoc Array

Example-4: Deleting complete Associative Array

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

//allocating array and assigning value to it

repeat(3) begin

a_array[index] = index*2;

index=index+4;

end

$display("[Before-Delete] Associative array size is %0d",a_array.size());

a_array.delete();

$display("[After -Delete] Associative array size is %0d",a_array.size());


end

endmodule

Simulator Output:

[Before-Delete] Associative array size is 3

[After -Delete] Associative array size is 0


code of Associative array using(int): e
x
m
;
o
d
i
u
n
l
i
e
t
i
a
a
s
l
s
o begin
c
$display("number of entries array_1=
; %0d",array_1.num());

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])

0 $display("number of entries array_1[%0s]=


; %0d",i,array_1[i]); array_1.first(index);
$display("display the first index array_1=
%0d",index,array_1[index]); array_1.last(index);

$display("display the last index array_1=


%0d",index,array_1[index]);

array_1.prev(index);

$display("display the prev index array_1=


%0d",index,array_1[index]);

array_1.next(index);

$display("display the next index array_1=


%0d",index,array_1[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))

$display("index 7 is exists in array_1");

else

$display("index 7 is not exists in array_1");

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("display the prev index array_1=


%0d",index,array_1[index]); if(array_1.exists(2))

$display("inde
x 2 is exists in
array_1"); else

$display("index 2
is not exists in
array_1");
if(array_1.exists(7)
)

$display("index 7 is exists in array_1");

else

$display("index 7 is not exists in array_1");

end

endmodule
Queues:-

• Queues can be represented as $.


• In queue 0 represents the first, and $ representing the last entries.
• Queue supports adding and removing elements anywhere.
A queue is a variable-size, ordered collection of homogeneous elements.

• like a dynamic array, queues can grow and shrink


• Queues are declared using the same syntax as unpacked arrays, but specifying $ as
the array size. In queue 0 represents the first, and $ representing the last entries.
• Queue has flexible memory.
• Queue has variable sizing.
• Queue has 2 types:
Bounded ---- If size is mentioned.
Unbounded ---- If size is not mentioned.
• Syntax:
• int queue_1 [$];
• example:-
• int queue_1[$]; //queue of bits (Unbounded queue)
• byte queue2[$:255]; //queue of byte (bounded queue with 256 entries)

Queue Declaration

data_type queue_name[$];

where:
data_type     – data type of the queue elements.
queue_name – name of the queue.

Queue Declaration Example

bit queue_1[$]; // queue of bits (unbound queue)

int queue_2[$]; // queue of int

byte queue_3[$:255]; // queue of byte (bounded queue with 256 entries)

string queue_4[$]; // queue of strings


Queue Initialization

queue_1 = {0,1,2,3};

queue_4 = {“Red”,"Blue”,"Green”};

Unbounded Queue

SystemVerilog queue

Bounded Queue

SystemVerilog Bounded queue

Queue Methods:-

• size() returns the number of items in the queue


• insert() inserts the given item at the specified index position
• delete() deletes the item at the specified index position
• push_front() inserts the given element at the front of the queue
• push_back() inserts the given element at the end of the queue
• pop_front() removes and returns the first element of the queue
• pop_back() removes and returns the last element of the queue
Unbounded Queue Declaration, Initialization, Size, Insert and Delete Method

This example shows the declaration and usage Queue methods.

module queues_array;

//declaration

bit [31:0] queue_1[$]; //unbounded queue

string queue_2[$];

initial begin

//Queue Initialization:

queue_1 = {0,1,2,3};

queue_2 = {"Red","Blue","Green"};

//Size-Method

$display("----- Queue_1 size is %0d -----",queue_1.size());

foreach(queue_1[i]) $display("\tqueue_1[%0d] = %0d",i,queue_1[i]);

$display("----- Queue_2 size is %0d -----",queue_2.size());

foreach(queue_2[i]) $display("\tqueue_2[%0d] = %0s",i,queue_2[i]);


//Insert-Method

queue_2.insert(1,"Orange");

$display("----- Queue_2 size after inserting Orange is %0d -----",queue_2.size());

foreach(queue_2[i]) $display("\tqueue_2[%0d] = %0s",i,queue_2[i]);

//Delete Method

queue_2.delete(3);

$display("----- Queue_2 size after Delete is %0d -----",queue_2.size());

foreach(queue_2[i])$display("\tqueue_2[%0d] = %0s",i,queue_2[i]);

end

endmodule

Simulator Output:

----- Queue_1 size is 4 -----

queue_1[0] = 0

queue_1[1] = 1

queue_1[2] = 2

queue_1[3] = 3

----- Queue_2 size is 3 -----

queue_2[0] = Red

queue_2[1] = Blue

queue_2[2] = Green

----- Queue_2 size after inserting Orange is 4 -----

queue_2[0] = Red

queue_2[1] = Orange

queue_2[2] = Blue

queue_2[3] = Green

----- Queue_2 size after Delete is 3 -----


queue_2[0] = Red

queue_2[1] = Orange

queue_2[2] = Blue

example ;

module queues_array;

//declaration

bit [31:0] queue_1[$];

int lvar;

initial begin

//Queue Initialization:

queue_1 = {0,1,2,3};

//Size-Method

$display("\tQueue_1 size is %0d",queue_1.size());

//Push_front Method

queue_1.push_front(22);

$display("\tQueue_1 size after push_front is %0d",queue_1.size());

//Push_back Method

queue_1.push_back(44);

$display("\tQueue_1 size after push_back is %0d",queue_1.size());

//Pop_front Method

lvar = queue_1.pop_front();

$display("\tQueue_1 pop_front value is %0d",lvar);


//Pop_back Method

lvar = queue_1.pop_back();

$display("\tQueue_1 pop_back value is %0d",lvar);

end

endmodule

Simulator Output:

Queue_1 size is 4

Queue_1 size after push_front is 5

Queue_1 size after push_back is 6

Queue_1 pop_front value is 22

Queue_1 pop_back value is 44

Bounded queue declaration and accessing

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("Queue elements are,");

$display("\tqueue = %p",queue);

queue.push_back(10);

$display("After push_back Queue elements are,");

$display("\tqueue = %p",queue);

queue.push_front(10);

$display("After push_front Queue elements are,");

$display("\tqueue = %p",queue);

end

endmodule

Simulator Output:

Queue elements are,

queue = '{7, 3, 1}

After push_back Queue elements are,

queue = '{7, 3, 1}

After push_front Queue elements are,

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};

$display("----- Queue elements with index -----");

foreach(queue[i])

$display("\tqueue[%0d] = %0d",i,queue[i]);

$display("-------------------------------------\n");

$display("Before Queue size is %0d",queue.size());

repeat(2) begin //{

index = $urandom_range(0,4); //index of queue is from 0 to 4

temp_var = queue[index];

$display("Value of Index %0d in Queue is %0d",index,temp_var);

end //}

$display("After Queue size is %0d",queue.size());

end

endmodule

Simulator Output:

----- Queue elements with index -----

queue[0] = 7

queue[1] = 3
queue[2] = 1

queue[3] = 0

queue[4] = 8

-------------------------------------

Before Queue size is 5

Value of Index 3 in Queue is 0

Value of Index 0 in Queue is 7

After Queue size is 5

Deleting random element of queue with index

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};

$display("Queue entries are %p",queue);

$display("Before Queue size is %0d",queue.size());

index = $urandom_range(0,4); //index of queue is from 0 to 4

$display("Index %0d is deleted",index);

queue.delete(index);

$display("After Queue size is %0d",queue.size());

$display("Queue entries are %p",queue);

$display("\nQueue entries are %p",queue);

$display("Before Queue size is %0d",queue.size());

index = $urandom_range(0,3); //index of queue is from 0 to 4


$display("Index %0d is deleted",index);

queue.delete(index);

$display("After Queue size is %0d",queue.size());

$display("Queue entries are %p",queue);

end

endmodule

Simulator Output:

Queue entries are '{7, 3, 1, 0, 8}

Before Queue size is 5

Index 3 is deleted

After Queue size is 4

Queue entries are '{7, 3, 1, 8}

Queue entries are '{7, 3, 1, 8}

Before Queue size is 4

Index 0 is deleted

After Queue size is 3

Queue entries are '{3, 1, 8}

Deleting complete queue

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();

$display("[After -Delete] Queue size is %0d",qu.size());

end

endmodule

Simulator Output:

[Before-Delete] Queue size is 4

[After -Delete] Queue size is 0

You might also like