Data Types in System Verilog
Data Types in System Verilog
Data Types in System Verilog
Literals : Fixed value in the program is called literal. In simple terms, it is known as
constants. Its value can not be altered during the execution of the program.
Variables
Variables can store any value in memory based on their data type. Its value can be
changed during run time.
Constants
Non-existence data is known as a void data type. It is usually used with functions
where it’s return type is void.
String
An ordered collection of characters is called a string data type. The length of string
variables may vary during simulation, hence the variable having type string is
dynamic in nature.
Event
User-defined types
Enumeration
In Verilog behavior modeling, always, and initial procedural blocks use reg data type
whereas, in dataflow modeling, continuous assignment uses wire data type.
SystemVerilog allows driving signals in the ‘assign’ statements and procedural blocks
using logic data type. The logic is a 4-state data type that allows capturing the Z or X
behavior of the design.
SystemVerilog Arrays
An array is a group of variables having the same data type. It can be accessed using an
index value.
An index is a memory address and the array value is stored at that address.
Types of an array
Array size is fixed throughout the simulation. Its value will be initialized with a ‘0’
value
Array assignment
int arr[3] = '{5,6,7}; // Declaration, and assignment
Or
int arr[3];
arr[0] = 5;
arr[1] = 6;
arr[2] = 7;
Example:
module foreach_example;
int array[5] = '{100, 200, 300, 400, 500};
initial begin
foreach (array[i]) begin
$display("array[%0d] = %0d", i, array[i]);
end
end
endmodule
output:
array[0] = 100
array[1] = 200
array[2] = 300
array[3] = 400
array[4] = 500
Multidimensional array
Two-dimensional array
int array[6][2] = '{'{1, 100}, '{2, 200}, '{3, 300}, '{4,400}, '{5,
500}, '{6, 600}}; // Declaration and assignment
int arr[6][2]; // Declaration
arr[0][0] = 1; // Assignment
arr[0][1] = 100;
arr[1][0] = 2;
arr[1][1] = 200;
arr[2][0] = 3;
arr[2][1] = 300;
arr[3][0] = 4;
arr[3][1] = 400;
arr[4][0] = 5;
arr[4][1] = 500;
arr[5][0] = 6;
arr[5][1] = 600;
Example:
module foreach_example;
int array[6][2] = '{'{1, 100}, '{2, 200}, '{3, 300}, '{4,400}, '{5,
500}, '{6, 600}};
initial begin
foreach (array[i,j]) begin
$display("array[%0d][%0d] = %0d", i,j, array[i][j]);
end
end
endmodule
output:
array[0][0] = 1
array[0][1] = 100
array[1][0] = 2
array[1][1] = 200
array[2][0] = 3
array[2][1] = 300
array[3][0] = 4
array[3][1] = 400
array[4][0] = 5
array[4][1] = 500
array[5][0] = 6
array[5][1] = 600
Three-dimensional array:
int array[3][3][3] = '{'{'{1, 10, 100}, '{2, 20, 200}, '{3, 30,
300}}, '{'{4, 40, 400}, '{5, 50, 500}, '{6, 60, 600}}, '{'{7, 70,
700}, '{8, 80, 800}, '{9, 90, 900}} };
Example:
module foreach_example;
int array[3][3][3] = '{'{'{1, 10, 100}, '{2, 20, 200}, '{3, 30,
300}}, '{'{4, 40, 400}, '{5, 50, 500}, '{6, 60, 600}}, '{'{7, 70,
700}, '{8, 80, 800}, '{9, 90, 900}} };
initial begin
foreach (array[i,j, k]) begin
$display("array[%0d][%0d][%0d] = %0d", i,j, k, array[i][j][k]);
end
end
endmodule
Output:
array[0][0][0] = 1
array[0][0][1] = 10
array[0][0][2] = 100
array[0][1][0] = 2
array[0][1][1] = 20
array[0][1][2] = 200
array[0][2][0] = 3
array[0][2][1] = 30
array[0][2][2] = 300
array[1][0][0] = 4
array[1][0][1] = 40
array[1][0][2] = 400
array[1][1][0] = 5
array[1][1][1] = 50
array[1][1][2] = 500
array[1][2][0] = 6
array[1][2][1] = 60
array[1][2][2] = 600
array[2][0][0] = 7
array[2][0][1] = 70
array[2][0][2] = 700
array[2][1][0] = 8
array[2][1][1] = 80
array[2][1][2] = 800
array[2][2][0] = 9
array[2][2][1] = 90
array[2][2][2] = 900
Scalar Vs Vector
Scalar :
The data object which does not have a specific range for bit/logic/reg is a scalar.
logic var1;
bit var2;
Vector:
The data object which has a specific range for bit/logic/reg is a vector.
Packed array
A packed array refers to the dimension mentioned before the variable or object name.
This is also known as the vector width.
Example:
module array_example;
bit [2:0][3:0] array = '{4'h2, 4'h4, 4'h6};
initial begin
foreach (array[i]) begin
$display("array[%0h] = %0h", i, array[i]);
end
end
endmodule
Output:
array[2] = 2
array[1] = 4
array[0] = 6
Unpacked array
An unpacked array refers to the dimension mentioned after the variable or object
name.
Output:
array[2][3] = 1
array[2][2] = 2
array[2][1] = 3
array[2][0] = 4
array[1][3] = 5
array[1][2] = 6
array[1][1] = 7
array[1][0] = 8
array[0][3] = 9
array[0][2] = 10
array[0][1] = 11
array[0][0] = 12
Combination of a packed and unpacked array
As name dynamic suggests, an array whose size can be changed during run time
simulation.
Methods Description
function void delete() Delete array results in an empty dynamic array i.e. zero-sized array.
new[ ] To create a memory. It can also be used to resize or copy a dynamic array.
Note: The new [ ] is different from the function new() which is used in the OOP
concept. [Read more in SystemVerilog Classes].
module dynamic_array_example;
int array []; initial begin
array = new[5];
array = '{5, 10, 15, 20, 25};
// Print elements of an array
foreach (array[i])
$display("array[%0d] = %0d", i, array[i]);
// size of an array
$display("size of array = %0d", array.size());
// Resizing of an array and copy old array content
array = new[8] (array);
$display("size of array after resizing = %0d", array.size());
// Print elements of an array
foreach (array[i])
$display("array[%0d] = %0d", i, array[i]);
// Override existing array: Previous array elememt values will be lost
array = new[6];
$display("size of array after overriding = %0d", array.size());
// Print elements of an array
foreach (array[i])
$display("array[%0d] = %0d", i, array[i]); array.delete(); $display("size of
array after deleting = %0d", array.size());
end
endmodule
Output:
array[0] = 5
array[1] = 10
array[2] = 15
array[3] = 20
array[4] = 25
size of array = 5
size of array after resizing = 8
array[0] = 5
array[1] = 10
array[2] = 15
array[3] = 20
array[4] = 25
array[5] = 0
array[6] = 0
array[7] = 0
size of array after overriding = 6
array[0] = 0
array[1] = 0
array[2] = 0
array[3] = 0
array[4] = 0
array[5] = 0
size of array after deleting = 0
Associative array in SystemVerilog
An associate array is used where the size of a collection is not known or data space is
sparse.
Methods Description
function int size(); Returns the size of an associative array. Returns 0 if an array is empty
Assign a value of the first index to the ‘value’ variable else returns 0 or
function int first (value)
an empty array.
Assign a value of the last index to the ‘value’ variable else returns 0 or
function int last (value)
an empty array.
function int prev (value) Assign a value of the previous index to the ‘value’ variable.
function int next (value) Assign a value of the next index to the ‘value’ variable.
function void delete () Delete complete array means all entries will be removed.
module associative_array_example;
typedef enum {TRANS, RECEIVE, REPEATER} tr_type;
bit [7:0] array_enum [tr_type];
bit [7:0] array_int [int];
initial begin
array_enum[TRANS] = 10;
array_enum[RECEIVE] = 20;
array_enum[REPEATER] = 30;
foreach (array_enum[i])
$display("array_enum[%s] = %0d", i.name(), array_enum[i]);
array_int[5] = 2;
array_int[10] = 4; array_int[7] = 6;
foreach (array_int[i])
$display("array_int[%0d] = %0d", i, array_int[i]);
end
endmodule
Output:
array_enum[TRANS] = 10
array_enum[RECEIVE] = 20
array_enum[REPEATER] = 30
array_int[5] = 2
array_int[7] = 6
array_int[10] = 4
Associative array methods Example
module associative_array_example;
bit [7:0] array [int];
int index;
initial begin
array[5] = 2;
array[10] = 4;
array[7] = 6;
array[9] = 8;
array[20] = 10;
array[13] = 12;
// Print array elements
foreach (array[i])
$display("array[%0d] = %0d", i, array[i]);
// Print array size and number of entries
$display("size = %0d, Number of entries = %0d of array",
array.size(), array.num());
$display("--------------------------");
// exists method
if(array.exists(7))
$display("An element exists at index = 7");
else
$display("An element doesn't exists at index = 7");
if(array.exists(8))
$display("An element exists at index = 8");
else
$display("An element doesn't exists at index = 8");
$display("--------------------------");
// first, last, prev, next method
array.first(index);
$display("First index of array = %0d", index);
array.last(index);
$display("Last index of array = %0d", index);
index = 9; array.prev(index);
// Previous index of 9
$display("Prev index of 9 is %0d", index);
index = 10; array.next(index);
// Next index of 10
$display("Next index of 10 is %0d", index);
$display("--------------------------");
// Delete particular index
array.delete(7);
// Print array elements
$display("After deleting element having index 7");
foreach (array[i])
$display("array[%0d] = %0d", i, array[i]);
$display("--------------------------");
// Delete complete array
array.delete();
$display("size = %0d of array", array.size());
end
endmodule
Output:
array[5] = 2
array[7] = 6
array[9] = 8
array[10] = 4
array[13] = 12
array[20] = 10
size = 6, Number of entries = 6 of array
--------------------------
An element exists at index = 7
An element doesn't exists at index = 8
--------------------------
First index of array = 5
Last index of array = 20
Prev index of 9 is 7
Next index of 10 is 13
--------------------------
After deleting element having index 7
array[5] = 2
array[9] = 8
array[10] = 4
array[13] = 12
array[20] = 10
--------------------------
size = 0 of array
Array manipulation methods
In array manipulation methods, it iterates over array elements and evaluates the
expression using the ‘with’ clause
‘with’ clause
The expression to be evaluated when iterating over the array elements can be
specified using the ‘with’ clause. The iterator argument can be specified with some
local variable which is used to refer to the current element of an array in the
expression. However, it is not mandatory to use a local variable and if it is not used
then the ‘item’ keyword is referred as a by-default keyword.
Example:
Here, color is a class variable of an enum type. The below example shows how to use
the find method with and without a local variable.
Array locator methods operate on queues, unpacked arrays, but their return type is
a queue. An array locator methods do below operations
1. Search an index or elements of the array
2. Array traversal in an unspecified order
The array location methods can be classified into two types
1. Element locator
2. Index locator
Element locator
In element locator type, locator methods iterate over all elements in an array, and then
it is used to evaluate the expression specified ‘with’ clause and returns element/s in a
queue. For some of the methods, the ‘with’ clause is optional.
Methods Description
unique Returns all elements with unique values or whose expression is unique
Returns the element with the minimum value or whose expression evaluates to a
min
minimum.
Returns the element with the maximum value or whose expression evaluates to a
max
maximum.
An associative array of type transaction class and a fixed array are used to
demonstrate how the above methods work.
module elememt_arr_locator_ex;
transaction tr;
transaction tr_assoc_arr[int];
int arr[8] = '{5,6,9,2,3,4,6,10};
initial begin
$display("-------------------------------------");
$display("--------- Generating array ----------");
$display("-------------------------------------");
array_gen();
$display("-------------------------------------");
$display("------------- find method -----------");
$display("-------------------------------------");
find_method();
$display("-------------------------------------");
$display("--------- find_first method ---------");
$display("-------------------------------------");
find_first_method();
$display("-------------------------------------");
$display("--------- find_last method ----------");
$display("-------------------------------------");
find_last_method();
$display("-------------------------------------");
$display("---------- unique method ------------");
$display("-------------------------------------");
unique_method();
$display("-------------------------------------");
$display("---------- min max method -----------");
$display("-------------------------------------");
min_max_method();
end
endmodule
tr = new();
assert(tr.randomize());
tr.print();
tr_assoc_arr[5] = tr;
tr = new();
assert(tr.randomize());
tr.print();
tr_assoc_arr[8] = tr;
tr = new();
assert(tr.randomize());
tr.print();
tr_assoc_arr[11] = tr;
endfunction
Corresponding output:
-------------------------------------
--------- Generating array ----------
-------------------------------------
addr = 1, data = 7 and color = RED
addr = 2, data = 7 and color = RED
addr = 4, data = 3 and color = RED
addr = 2, data = 3 and color = GREEN
addr = 5, data = 2 and color = YELLOW
find method
function void find_method();
transaction tr_q[$], tr;
int qsize;
Corresponding output:
-------------------------------------
------------- find method -----------
-------------------------------------
Number of elements for color item 'YELLOW' = 1
Element 1:
addr = 5, data = 2 and color = YELLOW
find_first method
function void find_first_method();
transaction tr_q[$], tr;
int qsize;
// Find first element having item as YELLOW color
tr_q = tr_assoc_arr.find_first with (item.colors == YELLOW);
qsize = tr_q.size;
$display("Number of elements for color item 'YELLOW' = %0d", qsize);
Corresponding output:
-------------------------------------
--------- find_first method ---------
-------------------------------------
Number of elements for color item 'YELLOW' = 1
Element 1:
addr = 5, data = 2 and color = YELLOW
find_last method
Corresponding output:
-------------------------------------
--------- find_last method ----------
-------------------------------------
Number of elements for color item 'YELLOW' = 1
Element 1:
addr = 5, data = 2 and color = YELLOW
unique method
function void unique_method();
transaction tr_q[$], tr;
int arr_q[$];
int qsize;
arr_q = arr.unique();
$display("unique element in arr = %p", arr_q);
Corresponding output:
-------------------------------------
---------- unique method ------------
-------------------------------------
unique element in arr = '{5, 6, 9, 2, 3, 4, 10}
Number of unique elements for addr = 4
Element 1:
addr = 1, data = 7 and color = RED
Element 2:
addr = 2, data = 7 and color = RED
Element 3:
addr = 4, data = 3 and color = RED
Element 4:
addr = 5, data = 2 and color = YELLOW
Corresponding output:
-------------------------------------
---------- min max method -----------
-------------------------------------
min element in arr = '{2}
max element in arr = '{10}
Min element w.r.t. addr is
addr = 1, data = 7 and color = RED
Max element w.r.t. addr is
addr = 5, data = 2 and color = YELLOW
Index locator
The index locator method returns index or indexes on iterating over the array.
Methods Description
find_index Returns the indexes of all elements satisfying the given expression
find_first_index Returns the index of the first element satisfying the given expression
find_last_index Returns the index of the last element satisfying the given expression
module elememt_arr_locator_ex;
transaction tr;
transaction tr_assoc_arr[int];
int arr[8] = '{5,6,9,2,3,4,6,10};
initial begin
$display("-------------------------------------");
$display("--------- Generating array ----------");
$display("-------------------------------------");
array_gen();
$display("-------------------------------------");
$display("--------- find_index method ---------");
$display("-------------------------------------");
find_index_method();
$display("-------------------------------------");
$display("------ find_first_index_method ------");
$display("-------------------------------------");
find_first_index_method();
$display("-------------------------------------");
$display("------ find_last_index method -------");
$display("-------------------------------------");
find_last_index_method();
$display("-------------------------------------");
$display("-------- unique_index method --------");
$display("-------------------------------------");
unique_index_method();
end
endmodule
tr = new();
assert(tr.randomize());
tr.print();
tr_assoc_arr[5] = tr;
tr = new();
assert(tr.randomize());
tr.print();
tr_assoc_arr[8] = tr;
tr = new();
assert(tr.randomize());
tr.print();
tr_assoc_arr[11] = tr;
tr = new();
assert(tr.randomize());
tr.print();
tr_assoc_arr[15] = tr;
endfunction
Corresponding output:
-------------------------------------
--------- Generating array ----------
-------------------------------------
addr = 1, data = 7 and color = RED
addr = 2, data = 7 and color = RED
addr = 4, data = 3 and color = RED
addr = 2, data = 3 and color = GREEN
addr = 5, data = 2 and color = YELLOW
find_index method
function void find_index_method();
int idx_q[$], idx;
int qsize;
Corresponding output:
-------------------------------------
--------- find_index method ---------
-------------------------------------
Number of indexes having color item 'RED' = 3
Element 1 for popped index = 3:
addr = 1, data = 7 and color = RED
Element 2 for popped index = 5:
addr = 2, data = 7 and color = RED
Element 3 for popped index = 8:
addr = 4, data = 3 and color = RED
find_first_index method
function void find_first_index_method();
int idx_q[$], idx;
int qsize;
Corresponding output:
-------------------------------------
------ find_first_index method ------
-------------------------------------
Number of index having color item 'RED' = 1
Element 1 for popped first index = 3:
addr = 1, data = 7 and color = RED
find_last_index method
function void find_last_index_method();
int idx_q[$], idx;
int qsize;
Corresponding output:
-------------------------------------
------ find_last_index method -------
-------------------------------------
Number of indexes having color item 'RED' = 1
Element 1 for popped last index = 8:
addr = 4, data = 3 and color = RED
unique_index method
idx_q = arr.unique_index();
$display("unique element in arr = %p", idx_q);
Corresponding output:
-------------------------------------
-------- unique_index method --------
-------------------------------------
unique element in arr = '{0, 1, 2, 3, 4, 5, 7}
Number of indexes having unique addr = 4
Element 1 for popped index = 3:
addr = 1, data = 7 and color = RED
Element 2 for popped index = 5:
addr = 2, data = 7 and color = RED
Element 3 for popped index = 8:
addr = 4, data = 3 and color = RED
Element 4 for popped index = 15:
addr = 5, data = 2 and color = YELLOW
find_first min
find_last max
find_first_index
find_last_index
The ordering methods are used to reorder the single-dimensional arrays or queues.
Methods Description
Note:
1. The sort and rsort methods may use the ‘with’ clause. The ‘with’ clause usage
is optional.
2. The shuffle and reverse methods lead to a compilation error if the ‘with’
clause is specified.
Array ordering methods Example
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec
ullamcorper mattis, pulvinar dapibus leo.
module arr_ordering_ex;
int arr[8] = '{5,6,9,2,3,4,6,10};
int que[$];
initial begin
que.push_back(45);
que.push_back(30);
que.push_back(99);
que.push_back(51);
que.push_back(85);
$display("-------------------------------------");
$display("------ Printing array and queue -----");
$display("-------------------------------------");
print_array_queue();
$display("-------------------------------------");
$display("----------- shuffle method ----------");
$display("-------------------------------------");
shuffle_method();
print_array_queue();
$display("-------------------------------------");
$display("---------- reverse method -----------");
$display("-------------------------------------");
reverse_method();
print_array_queue();
$display("-------------------------------------");
$display("------------- sort method -----------");
$display("-------------------------------------");
sort_method();
print_array_queue();
$display("-------------------------------------");
$display("------------ rsort method -----------");
$display("-------------------------------------");
rsort_method();
print_array_queue();
end
//-------------------------
// Array gen and methods
//-------------------------
The array reduction methods are used to reduce the array to a single value with the
optional use of the ‘with’ clause.
The reduction methods can be applied on any unpacked array. For a ‘with’ clause,
boolean or arithmetic reduction operation must be specified.
Methods Description
Note: If the ‘with’ clause is specified, the above reduction methods return value based
on evaluating the expression for each array element.
$display("-------------------------------------");
$display("------ Printing array and queue -----");
$display("-------------------------------------");
print_array_queue();
$display("-------------------------------------");
$display("------------ Methods ----------------");
$display("-------------------------------------");
$display("and for: arr = %0h and queue = %0h", arr.and(), que.and());
$display("or for : arr = %0h and queue = %0h", arr.or(), que.or());
$display("xor for: arr = %0h and queue = %0h", arr.xor(), que.xor());
$display("sum for: arr = %0d and queue = %0d", arr.sum(), que.sum());
$display("product for: arr = %0d and queue = %0d", arr.product(), que.product());
end
//-------------------------
// print array and queue
//-------------------------
The iterator index querying method iterates over array indexes or elements at each
iteration.
find method
//or
As mentioned above, the find method compares the index value with the element and
pushes it into the queue. Thus, queue size represents matched value count for the
same index and element value.
module iterator_index_ex;
int arr[8] = '{5,6,9,2,4,4,6,7};
int q[$], qsize;
initial begin
q = arr.find(x) with (x == x.index);
//or
//q = arr.find with (item == item.index);
qsize = q.size();
$display("Count = %0d having same index and element", qsize);
$display("same index and elements are %p", q);
end
endmodule
Output: