Data Types
Integer, Void, String, Event data Types
User-defined Data type
Enumeration
Class data type
Structural Data Types:
wire and reg
structural data types called nets which model hardware connections between circuit
components.
The wire nets act like real wires in circuits.
The reg type hold their values until another value is put on them, just like a register
hardware component.
The declarations for wire and reg signals are inside a module but outside any initial or
always block.
The initial state of a,
reg is x unknown,
wire is z.
Behavioral Data Types:
integer, real, and time
An integer declares one or more variables of type integer. These variables
can hold values ranging from -231 to 231-1.
Integer Syntax
integer integer_variable_name;
A real declaration declares one or more variables of type real. The real variables are
stored as 64 bit quantities, and store the real values. Real numbers can be specified in either
decimal notation (for example, 14.72) or in scientific notation (for example, 39e8).
Examples:
1.8
635.16
1.2E12 (the exponent symbol can be e or E)
1.30e-2
Syntax
real real_variable_name;
Both integer and real variables are initialized to zero at the start of the simulation.
Examples:
integer a[0:64] ; // an array of 65 integer values
real float_v ; // a variable to store real value
Time
Time is a 64-bit quantity that can be used in conjunction with the $time system task to hold
simulation time. Time is not supported for synthesis and hence is used only for simulation
purposes.
Syntax
time time_variable_name;
Parameters
Parameters represent constants, hence it is illegal to modify their value at runtime.
However, parameters can be modified at compilation time to have values that are
different from those specified in the declaration assignment. This allows customization
of module instances. A parameter can be modified with the defparam statement, or in
the module instance statement.
parameter size = 16 ;
logic
logic is the improved version of reg form verilog to Systemverilog, so it Can be driven by
continuous assignments, gates and modules in addition to being a variable.
declaration:
logic a, addr;
Two-state types
Two state types improves performance and memory usage over four state type.two state
data types are bit, byte, int, shortint, longint, byte.
bit – Unsigned
byte, shortint, int, longint – Signed
unsigned two-state types,
bit single_bit ; // unsigned single bit
bit [31:0] 32_bit ; // 32-bit unsigned integer
signed two-state types,
int integer ; // 32-bit signed integer
byte 8_bit ; // 8-bit signed integer
shortint 16_bit ; // 16-bit signed integer
longint 64_bit ; // 64-bit signed integer
unsigned from signed two-state types,
int unsigned integer ; // 32-bit unsigned integer
byte unsigned 8_bit ; // 8-bit unsigned integer
shortint unsigned 16_bit ; // 16-bit unsigned integer
longint unsigned 64_bit ; // 64-bit unsigned integer
Void Data Types
The void data type represents non-existent data. This type can be specified as the return
type of functions, indicating no return value.
syntax:
void’(function_call());
EVENT
SystemVerilog enhances the verilog event in several ways.An event is now a handle to a
synchronization object that can be passed around to routines. In verilog, if the triggering
thread executes before the blocking thread, the trigger is missed. SystemVerilog introduces
triggered function that lets you check whether an event has been triggered.
event declaration examples,
event e1;
event e2;
event done;
Detailed explanation of events and Event operations are explained in later
chapters(SystemVerilog Events).
User Defined
The user can define a new type using typedef, as in C.
typedef int integre_v;
This can then be instantiated as:
integer_v var1;
integer_v var2;
ENUMERATIONS / ENUM
An enumerated type defines a set of named values. The simplest enumerated type
declaration contains a list of constant names and one or more variables.
In the following example, colors is defined to be variable of the unnamed enumerated int
type that includes the members red, green, blue, yellow, white, black.
enum { red, green, blue, yellow, white, black } Colors;
The actual values are default to integers starting at 0 and then increase. in the above
example by default variable will get default value of 0,1,2,3,4,5 respectively from red. The
values can be set for the names and also values can be set for some of the names and not
set for other names. A name without a value is automatically assigned an increment of the
value of the previous name.
In the following example value is set for red = 0, blue = 4, white = 10.
enum { red=0, green, blue=4, yellow, white=10, black } Colors;
green, yellow, black automatically assigned to the increment-value of 1,5,11 respectively.
If an automatically incremented value is assigned elsewhere in the same enumeration, this
shall be a syntax error.
In the below example yellow will get the increment-value of 5,value of white is set with 5.
this will cause the syntax error.
enum { red=0, green=0, blue=4, yellow, white=5, black=6 } Colors
Defining new data types as enumerated types:
A type name can be given so that the same type can be used in many places.
typedef enum {GOOD, BAD} pkt_type;
pkt_type pkt_a; // named type
Enumerated types methods:
first() -- > returns the value of the first member of the enumeration.
last() -- > returns the value of the last member of the enumeration.
next() -- > returns the value of next member of the enumeration.
next(N) -- > returns the value of next Nth member of the enumeration.
prev() -- > returns the value of previous member of the enumeration.
prev(N) -- > returns the value of previous Nth member of the enumeration.
num() -- > returns the number of elements in the given enumeration.
name() -- > returns the string representation of the given enumeration value.
Example-1 : Enumeration Type [DataTypes]
This example shows how to declare enum.
module enum_datatype;
//declaration
enum { red, green, blue, yellow, white, black } Colors;
//display members of Colors
initial begin
Colors = Colors.first;
for(int i=0;i<6;i++) begin
$display("Colors :: Value of %0s \t is = %0d",Colors.name,Colors);
Colors = Colors.next;
end
end
endmodule
Simulator Output
Colors :: Value of red is = 0
Colors :: Value of green is = 1
Colors :: Value of blue is = 2
Colors :: Value of yellow is = 3
Colors :: Value of white is = 4
Colors :: Value of black is = 5
Example-2 : Enumeration Type [DataTypes]
This example shows how to set other than default values to enum.
module enum_datatype;
//declaration
enum { red=0, green, blue=4, yellow, white=10, black } Colors;
//display members of Colors
initial begin
Colors = Colors.first;
for(int i=0;i<6;i++) begin
$display("Colors :: Value of %0s \t is = %0d",Colors.name,Colors);
Colors = Colors.next;
end
end
endmodule
Simulator Output
Colors :: Value of red is = 0
Colors :: Value of green is = 1
Colors :: Value of blue is = 4
Colors :: Value of yellow is = 5
Colors :: Value of white is = 10
Colors :: Value of black is = 11
Example-3 : Enumeration Type [DataTypes]
This example shows error, in case of automatic increment-value is same as value assigned
to other enum member.
module enum_datatype;
//declaration
enum { red=0, green=1, blue=4, yellow, white=5, black=6 } Colors;
initial begin
Colors = Colors.first;
for(int i=0;i<6;i++)
$display("Colors :: Value of %0s is = %0d",Colors.name(),Colors);
end
endmodule
Simulator Output
Error-[ENUMDUPL] Duplicate labels in enum
testbench.sv, 9
The enum label 'white' has the value 5 which is duplicate of enum label
'yellow' in the declared enum.
1 error
Example-4 : Enumeration Type [DataTypes]
This example shows declaring new data types using enumerated type.
module enum_datatype;
//declaration
typedef enum {GOOD, BAD} pkt_type;
pkt_type pkt_a;
pkt_type pkt_b;
initial begin
pkt_a = GOOD;
pkt_b=BAD;
if(pkt_a == GOOD)
$display("pkt_a is GOOD packet");
else
$display("pkt_a is BAD packet");
if(pkt_b == GOOD)
$display("pkt_b is GOOD packet");
else
$display("pkt_b is BAD packet");
end
endmodule
Simulator Output
pkt_a is GOOD packet
pkt_b is BAD packet
Example-6 : Enumeration Type [DataTypes]
This example shows how to use enumeration methods.
module enum_datatype;
//declaration
typedef enum { red=0, green, blue=4, yellow, white=10, black } colors;
enum { a, b, c, d, e, f, g } alphabets;
colors first_set;
colors second_set;
initial begin
first_set = first_set.first();
$display("first_set first color is \t %0s, \t Value = %0d", first_set.name(),first_set);
first_set = first_set.last();
$display("first_set last color is \t %0s, \t Value = %0d", first_set.name(),first_set);
second_set = first_set;
$display("second_set color is \t %0s, \t Value = %0d", second_set.name(),second_set);
second_set =second_set.prev(2);
$display("second_set color is \t %0s, \t Value = %0d", second_set.name(),second_set);
second_set =second_set.next(2);
$display("second_set color is \t %0s, \t Value = %0d", second_set.name(),second_set);
$display("Number of members in alphabets is \t %0d",alphabets.num());
$display("Default First members in alphabets is \t %0s , \t value is
%0d",alphabets.name(),alphabets);
alphabets=alphabets.next;
$display("Next members in alphabets is \t %0s , \t value is
%0d",alphabets.name(),alphabets);
alphabets=alphabets.last;
$display("Last members in alphabets is \t %0s , \t value is
%0d",alphabets.name(),alphabets);
alphabets=alphabets.prev(3);
$display("3rd members from last in alphabets is \t %0s , \t value is
%0d",alphabets.name(),alphabets);
end
endmodule
Simulator Output
first_set first color is red, Value = 0
first_set last color is black, Value = 11
second_set color is black, Value = 11
second_set color is yellow, Value = 5
second_set color is black, Value = 11
Number of members in alphabets is 7
Default First members in alphabets is a and value is 0
Next members in alphabets is b , value is 1
Last members in alphabets is g , value is 6
3rd members from last in alphabets is d , value is 3
Class - data type
A Class is a collection of data and a set of subroutines that operate on that data. The
data in a class are referred to as class properties, and its subroutines are called methods.A
Class is declared using the class...endclass keywords.
Example:
class packet;
// Properties
bit [31:0] address;
bit [31:0] data ;
// Method
function new();
$display("Inside new Function of packet");
endfunction
endclass : packet
ARRAYS
Fixed size arrays
Packed and un-packed arrays
Dynamic array
Associative array
Queues
ARRAY
An array is a collection of variables, all of the same type, and accessed using the same
name plus one or more indices. there are different types of arrays, few array declaration
examples are given below.
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 vector width/dimensions declared before the
object name is referred as packed array
array size/dimensions declared after the object name is referred
as unpacked array.
A packed array is a mechanism for subdividing a vector into sub-
fields which can be conveniently accessed as array elements
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 array will be initialized with value ‘0’.
single dimensional array:
int array1 [6] ; //Compact declaration
int array2 [5:0] ; // Verbose declaration
Multidimensional array:
int array3 [3:0] [2:0];
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}};
Example-1 : Fixed Size Array
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
Un-Packed and Packed arrays will be saved as below,
Un-Packed array
Ex: bit [7:0] array4[2:0];
7 6 5 4 3 2 1 0 array4[0]
7 6 5 4 3 2 1 0 array4[1]
7 6 5 4 3 2 1 0 array4[2]
Packed array
Refer to the dimensions declared before the object name.
Ex: bit [2:0] [7:0] array5;
7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
array5[2] array5[1] array5[0]
Dynamic Array
A dynamic array is one dimension of an unpacked array whose
size can be set or changed at run-time.
Dynamic array is Declared using a empty word subscript [ ].
The space for a dynamic array doesn’t exist until the array is
explicitly created at run-time, space is allocated when
new[number] is called. number indicates the number of
space/elements to be allocated.
The syntax to declare a dynamic array is:
data_type array_name [ ];
data_type is the data type of the array elements.
Dynamic array methods are,
new[ ] --> allocates the storage.
size( ) --> returns the current size of a dynamic array.
delete( ) --> empties the array, resulting in a zero-sized array.
Example:
//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 below syntax.
//allocate 6 new elements and retain values of 4 elements.
d_array1 = new[10](d_array1);
//delete array
d_array1.delete;
Example-1 : Dynamic Array Decalaration, 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
Example-2 : 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
Example-3 : Dynamic Array, Increasing 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
Associative array
Associative array Stores entries in a sparse matrix.
(Distributed memory)
Dynamic arrays are useful for contiguous collections of
variables whose number changes dynamically. When the
size of the collection is unknown or the data space is sparse,
an associative array is a better option.
Associative arrays allocate the storage only when it is used,
unless like in the dynamic array we need to allocate memory
before using it.
In associative array index expression is not restricted to
integral expressions, but can be of any type.
An associative array implements a lookup table of the
elements of its declared type. The data type to be used as an
index serves as the lookup key, and imposes an ordering.
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.
Example:
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:
num() --> returns the number of entries in the associative array.
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.
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.
prev(var) --> assigns the value of previous index to the variable var.
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
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
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
Queues
A queue is a variable-size, ordered collection of homogeneous elements. like dynamic array,
queues can grow and shrink, queue supports adding and removing elements anywhere.
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 entree.
Queue Declaration:
data_type queue_name[$];
where:
data_type - data type of the queue elements.
queue_name - name of the queue.
Example:
bit queue_1[$]; // queue of bits
int queue_2[$]; // queue of int
byte queue_3[$:255]; // queue of byte with max size 256 entries
string queue_4[$]; // queue of strings
Queue Initialization:
queue_1 = {0,1,2,3};
queue_4 = {“Red”,"Blue”,"Green”};
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.
Example-1 : Queue Declaration, Initialization, Size, Insert and Delete Method.
This example shows declaration and usage Queue methods.
module queues_array;
//declaration
bit [31:0] queue_1[$];
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-2 : Queue, push_front(), push_back(), pop_front() and pop_back()
Method.
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