Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Data Types in System Verilog

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

SYSTEM VERILOG

SystemVerilog is commonly used in the semiconductor. It is a hardware description


and hardware verification language used to model, design, simulate testbench.
SystemVerilog is based on Verilog and some extensions. It is standardized as IEEE
1800.
SystemVerilog provides support for gate-level, RTL, and behavioral descriptions,
coverage, object-oriented, assertion, and constrained random constructs. It also
includes application programming interfaces (APIs) to foreign programming
languages.

Data Types in System Verilog

A storage format having a specific range or type is called data type.


2 state data type: It has bits 0 and 1
4 state data type: It has bits 0, 1, X, and Z

Signed and Unsigned

Unsigned: can hold positive values alone.

Signed: can hold both positive and negative values.

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

The value of constants cannot be changed. It is read-only in nature.

Integer data type

int 2 state data type, 32-bit signed integer

integer 4 state data type, 32-bit signed integer


shortint 2 state data type, 16-bit signed integer

longint 2 state data type, 64-bit signed integer

bit 2 state data type, unsigned, user-defined vector size

byte 2 state data type, 8-bit signed integer or ASCII character

logic 4 state data type, unsigned, user-defined vector size

reg 4 state data type, unsigned, user-defined vector size

time 4 state data type, 64-bit unsigned integer

Real, shortreal, and realtime data types

Real, shortreal, and realtime data types – Known as real variables.

real – same as double datatype in C

shortreal – same as a float in C

realtime – interchangeably used with the real data type.

void data type

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

Data type event is a handle to a synchronization object. The object which is


referenced by an event variable can be triggered and waited for.

event is_empty ;// declaration of is_empty event.


As proceeding further, we will discuss events in detail.

User-defined types

User-defined types use keyword typedef which is an extension of SystemVerilog data


type.

Typedef int myint; // Declaration

myint a,b; // Usage of user defined data type.

Enumeration

A set of integral named constants is called an enumerated type. It defines a set of


named values having an anonymous int type.

enum {red, yellow, green} traffic_signal;

//Anonymous int type -> red = 0, yellow = 1 and green = 2

Logic data type

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

1. Fixed-size array in SystemVerilog


2. Single dimensional array
3. Multidimensional array
a. Two-dimensional array.
b. Three-dimensional array
4. Packed and Unpacked array in SystemVerilog
5. Dynamic array in SystemVerilog
6. Associative array in SystemVerilog
Fixed-size array in SystemVerilog

Array size is fixed throughout the simulation. Its value will be initialized with a ‘0’
value

Single dimensional array


int arr [3];

int arr [2:0];

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

A multidimensional array is also known as an array of an array. In mathematics, we


studied matrix, this can be understood as a multidimensional matrix.

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.

logic [5:0] var1;


bit [2:0] var2;
Packed and Unpacked array:

Packed array

A packed array refers to the dimension mentioned before the variable or object name.
This is also known as the vector width.

Memory allocation is always a continuous set of information that is accessed by an


array index
.reg [5:0] array; // [5:0] is packed dimension or vector width.

bit [2:0][3:0] array;

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.

Memory allocation may or may not be a continuous set of information.

reg arr [3:0]; // [3:0] is unpacked dimension.

int array [2:0][3:0];


Example:
module array_example;
int array [2:0][3:0] = '{'{1, 2, 3, 4}, '{5, 6, 7, 8}, '{9, 10, 11,
12} };
initial begin
foreach (array[i,j]) begin
$display("array[%0d][%0d] = %0d", i, j, array[i][j]);
end
end
endmodule

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

All arrays mentioned above are types of static arrays.


Example:
module array_example;
bit [4:0] array[2:0][1:0] = '{'{5'h5, 5'h1}, '{5'h10, 5'h2}, '{5'h15,
5'h3} };
initial
begin
foreach (array[i,j]) begin
$display("array[%0h][%0h] = %0h", i, j, array[i][j]);
end
end
endmodule
output:
array[2][1] = 5
array[2][0] = 1
array[1][1] = 10
array[1][0] = 2
array[0][1] = 15
array[0][0] = 3
Dynamic Array in SystemVerilog

As name dynamic suggests, an array whose size can be changed during run time
simulation.

The size of an array can be specified during run-time by using new[ ].


Note: By default, the size of a dynamic array is 0 unless a new[ ] is used.

Dynamic Array Declaration:

bit [2:0] array [];


string str_arr [];
// To allocate the size of an array
array = new[2];
Dynamic array Methods

Methods Description

function int size() Returns the current size of a dynamic array

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

Dynamic array Example :

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.

Associative array declaration

data_type array_name [ index_type ];


bit[7:0] assoc_array [int];

Associative array methods

Methods Description
function int size(); Returns the size of an associative array. Returns 0 if an array is empty

function int num(); Returns the number of entries of an associative array.

function int exists (input


Returns 1 if an element exists at a specified index else returns 0.
index)

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 (input


Delete array entry for mentioned array index.
index)

function void delete () Delete complete array means all entries will be removed.

Why do we need an associative array in SystemVerilog?

1. In a dynamic array, we need to allocate memory before using it. But in an


associative array, memory can be allocated when it is used.
2. A dynamic array is specific for a particular data type. When it comes to an
associative array, elements of an array can be of any type. We can store the
concatenation of various data types or class structures as well.
Associative array Example

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

SystemVerilog provides built-in methods for array reduction, ordering, locator,


iterator index querying.

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.

//With local variable

arr.find(x) with (x.color == YELLOW);

//Without local variable

arr.find with (item.color == YELLOW);

Array Locator methods

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.

Element locator methods

Methods Description

find Returns all elements satisfying the given expression.

find_first Returns the first element satisfying the given expression.


find_last Returns the last element satisfying the given expression.

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.

Element locator Example

An associative array of type transaction class and a fixed array are used to
demonstrate how the above methods work.

Element array locator module

typedef enum {RED, GREEN, YELLOW} color_type;


class transaction;
rand bit [2:0] addr;
rand bit [2:0] data;
rand color_type colors;

function void print;


$display("addr = %0d, data = %0d and color = %s", addr, data, colors.name);
endfunction
endclass

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

Generate associative array


function void array_gen();
tr = new();
assert(tr.randomize());
tr.print();
tr_assoc_arr[3] = tr;

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;

// Find all elements having item as YELLOW color


tr_q = tr_assoc_arr.find with (item.colors == YELLOW);
qsize = tr_q.size;
$display("Number of elements for color item 'YELLOW' = %0d", qsize);

for(int i = 0; i < qsize; i++) begin


$display("Element %0d: ",i+1);
tr = tr_q.pop_front();
tr.print();
end

// expression has multiple conditions


tr_q = tr_assoc_arr.find with (item.data >= 1 && item.data <= 5);
qsize = tr_q.size;
$display("\nNumber of elements for data item in range of 1 to 5 = %0d", qsize);

for(int i = 0; i < qsize; i++) begin


$display("Element %0d: ",i+1);
tr = tr_q.pop_front();
tr.print();
end
endfunction

Corresponding output:

-------------------------------------
------------- find method -----------
-------------------------------------
Number of elements for color item 'YELLOW' = 1
Element 1:
addr = 5, data = 2 and color = YELLOW

Number of elements for data item in range of 1 to 5 = 3


Element 1:
addr = 4, data = 3 and color = RED
Element 2:
addr = 2, data = 3 and color = GREEN
Element 3:
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);

for(int i = 0; i < qsize; i++) begin


$display("Element %0d: ",i+1);
tr = tr_q.pop_front();
tr.print();
end

// expression has multiple conditions


tr_q = tr_assoc_arr.find_first with (item.data >= 1 && item.data <= 5);
qsize = tr_q.size;
$display("\nNumber of elements for data item in range of 1 to 5 = %0d", qsize);

for(int i = 0; i < qsize; i++) begin


$display("Element %0d: ",i+1);
tr = tr_q.pop_front();
tr.print();
end
endfunction

Corresponding output:
-------------------------------------
--------- find_first method ---------
-------------------------------------
Number of elements for color item 'YELLOW' = 1
Element 1:
addr = 5, data = 2 and color = YELLOW

Number of elements for data item in range of 1 to 5 = 1


Element 1:
addr = 4, data = 3 and color = RED

find_last method

function void find_last_method();


transaction tr_q[$], tr;
int qsize;

// Find last element having item as YELLOW color


tr_q = tr_assoc_arr.find_last with (item.colors == YELLOW);
qsize = tr_q.size;
$display("Number of elements for color item 'YELLOW' = %0d", qsize);

for(int i = 0; i < qsize; i++) begin


$display("Element %0d: ",i+1);
tr = tr_q.pop_front();
tr.print();
end

// expression has multiple conditions


tr_q = tr_assoc_arr.find_last with (item.data >= 1 && item.data <= 5);
qsize = tr_q.size;
$display("\nNumber of elements for data item in range of 1 to 5 = %0d", qsize);

for(int i = 0; i < qsize; i++) begin


$display("Element %0d: ",i+1);
tr = tr_q.pop_front();
tr.print();
end
endfunction

Corresponding output:
-------------------------------------
--------- find_last method ----------
-------------------------------------
Number of elements for color item 'YELLOW' = 1
Element 1:
addr = 5, data = 2 and color = YELLOW

Number of elements for data item in range of 1 to 5 = 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);

// Find unique elements of addr.


tr_q = tr_assoc_arr.unique with (item.addr);
qsize = tr_q.size;
$display("Number of unique elements for addr = %0d", qsize);

for(int i = 0; i < qsize; i++) begin


$display("Element %0d: ",i+1);
tr = tr_q.pop_front();
tr.print();
end
endfunction

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

min and max methods

function void min_max_method();


transaction tr_q[$], tr;
int arr_q[$];
int qsize;

// Find min and max element in an array


arr_q = arr.min;
$display("min element in arr = %p", arr_q);
arr_q = arr.max;
$display("max element in arr = %p", arr_q);

tr_q = tr_assoc_arr.min with (item.addr);


qsize = tr_q.size;

for(int i = 0; i < qsize; i++) begin


$display("Min element w.r.t. addr is ");
tr = tr_q.pop_front();
tr.print();
end

tr_q = tr_assoc_arr.max with (item.addr);


qsize = tr_q.size;

for(int i = 0; i < qsize; i++) begin


$display("Max element w.r.t. addr is ");
tr = tr_q.pop_front();
tr.print();
end
endfunction

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

Returns the indexes of all elements with unique values or whose


unique_index
expression is unique.

Index locator module


typedef enum {RED, GREEN, YELLOW} color_type;
class transaction;
rand bit [2:0] addr;
rand bit [2:0] data;
rand color_type colors;

function void print;


$display("addr = %0d, data = %0d and color = %s", addr, data, colors.name);
endfunction
endclass

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

Generate associative array


function void array_gen();
tr = new();
assert(tr.randomize());
tr.print();
tr_assoc_arr[3] = tr;

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;

// Find all idx having element as RED color


idx_q = tr_assoc_arr.find_index with (item.colors == RED);
qsize = idx_q.size;
$display("Number of indexes having color item 'RED' = %0d", qsize);

for(int i = 0; i < qsize; i++) begin


idx = idx_q.pop_front();
$display("Element %0d for popped index = %0d: ",i+1, idx);
tr_assoc_arr[idx].print();
end

//expression has multiple conditions


idx_q = tr_assoc_arr.find_index with (item.data == 3 && item.colors <= RED);
qsize = idx_q.size;
$display("\nNumber of indexes for data == 3 and color == RED is %0d", qsize);
for(int i = 0; i < qsize; i++) begin
idx = idx_q.pop_front();
$display("Element %0d for popped index = %0d: ",i+1, idx);
tr_assoc_arr[idx].print();
end
endfunction

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

Number of indexes for data == 3 and color == RED is 1


Element 1 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;

// Find first idx having element as RED color


idx_q = tr_assoc_arr.find_first_index with (item.colors == RED);
qsize = idx_q.size;
$display("Number of index having color item 'RED' = %0d", qsize);

for(int i = 0; i < qsize; i++) begin


idx = idx_q.pop_front();
$display("Element %0d for popped first index = %0d: ",i+1, idx);
tr_assoc_arr[idx].print();
end

//expression has multiple conditions


idx_q = tr_assoc_arr.find_first_index with (item.data >= 3 && item.colors <= RED);
qsize = idx_q.size;
$display("\nNumber of indexes for data >= 3 and color == RED is %0d", qsize);

for(int i = 0; i < qsize; i++) begin


idx = idx_q.pop_front();
$display("Element %0d for popped first index = %0d: ",i+1, idx);
tr_assoc_arr[idx].print();
end
endfunction

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

Number of indexes for data >= 3 and color == RED is 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;

// Find all idx having element as RED color


idx_q = tr_assoc_arr.find_last_index with (item.colors == RED);
qsize = idx_q.size;
$display("Number of indexes having color item 'RED' = %0d", qsize);

for(int i = 0; i < qsize; i++) begin


idx = idx_q.pop_front();
$display("Element %0d for popped last index = %0d: ",i+1, idx);
tr_assoc_arr[idx].print();
end

//expression has multiple conditions


idx_q = tr_assoc_arr.find_last_index with (item.data >= 3 && item.colors <= RED);
qsize = idx_q.size;
$display("\nNumber of indexes for data >= 3 and color == RED is %0d", qsize);

for(int i = 0; i < qsize; i++) begin


idx = idx_q.pop_front();
$display("Element %0d for popped last index = %0d: ",i+1, idx);
tr_assoc_arr[idx].print();
end
endfunction

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

Number of indexes for data >= 3 and color == RED is 1


Element 1 for popped last index = 8:
addr = 4, data = 3 and color = RED

unique_index method

function void unique_index_method();


int idx_q[$], idx;
int qsize;

idx_q = arr.unique_index();
$display("unique element in arr = %p", idx_q);

// Find all idx having element as RED color


idx_q = tr_assoc_arr.unique_index with (item.addr);
qsize = idx_q.size;
$display("Number of indexes having unique addr = %0d", qsize);

for(int i = 0; i < qsize; i++) begin


idx = idx_q.pop_front();
$display("Element %0d for popped index = %0d: ",i+1, idx);
tr_assoc_arr[idx].print();
end
endfunction

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

Methods having mandatory and optional ‘with’ clause


The below table shows what all methods ‘with’ clause is mandatory or optional to use.

Type Mandatory ‘with’ clause methods Optional ‘with’ clause methods

Element Locator find unique

find_first min

find_last max

Index Locator find_index unique_index

find_first_index

find_last_index

Array ordering methods in SV

The ordering methods are used to reorder the single-dimensional arrays or queues.

Methods Description

shuffle Randomizes the order of the elements in an array

sort Sorts the unpacked array in ascending order

rsort Sorts the unpacked array in descending order

reverse Reverses all the elements of a packed or unpacked array.

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

function void print_array_queue();


$display("Array = %p", arr);
$display("Queue = %p", que);
endfunction

function void shuffle_method();


arr.shuffle();
que.shuffle();
endfunction

function void sort_method();


arr.sort();
que.sort();
endfunction

function void rsort_method();


arr.rsort();
que.rsort();
endfunction

function void reverse_method();


arr.reverse();
que.reverse();
endfunction
endmodule
Array reduction 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

and Returns bitwise AND (&) of all elements of the array.

or Returns bitwise OR (|) of all elements of the array.

xor Returns bitwise XOR (^) of all elements of the array.

sum Returns the sum of all elements of the array.

product Returns product of all elements of the array.

Note: If the ‘with’ clause is specified, the above reduction methods return value based
on evaluating the expression for each array element.

Array reduction methods Example


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

function void print_array_queue();


$display("Array = %p", arr);
$display("Queue = %p", que);
endfunction
endmodule

Iterator index querying

The iterator index querying method iterates over array indexes or elements at each
iteration.

find method

q = arr.find(x) with (x == x.index);

//or

q = arr.find with (item == item.index);

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.

find method Example

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:

Count = 3 having same index and element

same index and elements are '{4, 6, 7}

You might also like