SV DataStruct
SV DataStruct
Control flow
-Geethanjali G
Agenda
Data Structures
Arrays
Packed Arrays
Unpacked Arrays
Array Slicing
Dynamic Arrays
Associative Arrays
Queues
Queues
Packed Arrays
Packed array
A packed array is a mechanism for subdividing a vector into subfields which can be
conveniently accessed as array elements.
Packed arrays refer to the dimension declared before the object name.
Ex : bit [3:0] b;
Limitation of packed array: Only bit, logic, reg can be used in packed array.
Variables with predefined length cannot be used as packed array like byte, integer, longint,
shortint, int.
Un-packed Arrays
Un-Packed array
Unpacked arrays refer to the dimensions declared after the object name
Unpacked arrays can be made of any type
Only access a single element at a time
pack_unpack_array.sv
Multi-dimensional Arrays
Arrays of instances can be multidimensional. Example:
Array Slicing
Array Slicing:
Array Indexing :
logic [6:0] [7:0] a;
logic [:0] b;
b = a[0]; // It assigns 8 bits of a[0] to b
Dynamic Arrays
It is an unpacked array, whose size can be set or changed at run-time.
Dynamic declaration of one-dimensional arrays.
dynamic_arr.sv
Associative Arrays
The associative array is an indexed array, where the element is linked with an index
variable.
index can be wildcard(any integral value), string, integer, class, packed array, unpacked
array.
Associative arrays do not have any storage allocated until it is used. The size of the array is
unknown.
Examples:
int array_name[*]; //Wildcard index ,can be indexed by any integral data type.
int array_name [string] ; // String index
int array_name [some class]; // Class index
int array_name [integer]; // Integer index
Ex : Associative Arrays - sparse memory model
Associative Array Methods
Num ( )
Return the number of entries in the associative array
Delete ( )
Removes the entry at the specified index
Exists ( )
Checks if an element exists at the specified index
Returns 1 if it exists; else returns 0
First ( )
Assigns the index variable passed, with the first index in the associative array
Returns 0 if the array is empty; else returns 1
Last ( ) associative_ar.sv
Assigns the index variable passed, with the last index in the associative array
Returns 0 if the array is empty; else returns 1
Next ( )
Assigns the index variable passed, with the next index in the associative array
Returns 0 if next element does not exist; else returns 1
Previous ( )
Assigns the index variable passed, with the previous index in the associative
array
Returns 0 if next element does not exist; else returns 1
Queues
Variable size array with automatic sizing, It is a single dimension (un-packed array).
Each element in the queue is identified by a number which represents its position
Supports searching, sorting, and insertion methods.
0 represents the first position
$ represents the last position
It can grow and shrink automatically
Declaration :
e.g.(1) integer int_q [ $ ] ; // Un-initialized queue of integers
e.g.(2) string str_q [$:100] ; // Queue of strings with maximum of 100 entries
Queue Methods
Size ( )
Returns the number of elements in the queue.
Insert ( )
Inserts the specified element to the specified index location.
Delete ( )
Deletes an element at the specified index location
Queue Methods
Pop_front ( )
Removes and returns the first element of the queue.
e.g. ele = Q.pop_front( ); is same as ele = Q[0] ; Q = Q[1:$] ;
Pop_back ( )
Removes and returns the last element of the queue.
e.g. ele = Q.pop_back( ); is same as ele = Q[$] ; Q = Q[1:$-1] ;
Push_front ( )
Inserts a given element as the first element in the queue
e.g. Q.push_front(ele) ; is same as Q = {ele, Q} ;
Push_back ( )
Inserts a given element to the end of the queue
e.g. Q.push_back(ele) ; is same as Q = {Q, ele} ;
Structures and Unions
Structure is a collection of objects that can be of different data types
• Data members can be referenced individually (using the . operator) or altogether as a unit.
• structure allocates the memory equal to the total memory required by the members.
struct {
int a //Members of structure
byte b;
bit [7:0] c
} struct_data;
Struct_data.c = 8’h1E;
Structures and Unions cont..
Structure can be defined using “typedef”
Ex: you might pack together the opcode and operand fields to make a value that contains
an entire processor instruction.
Limitation : Only integer types (bit, logic, int, reg, time) can be used in a packed structure.
structure.sv
If any data type within a packed structure is 4-state, the whole structure is treated as 4-
state. Any 2-state members are converted ( cast ).
Unions
• A union allows us to treat the same space in memory as a number of different
variables.
• All members begin at the same memory address , ie one block is used by all the
member of the union.
union {
int a;
byte b;
bit [7:0] c;
} my_data;
• Control flows are constructs that controls the flow of a process in a program
.
1. Unique if
2. Priority if
3. Unique case
4. Priority case
unique if
Features:
Syntax: unique if(condition)
<statement>
else if(condition)
1. Each line is mutually exclusive and unique. <statement>
else
2. A unique if indicates that there should not be any <statement>
statement is true.
priority if , unique case priority case
Features:
4. Issues warning when there is no condition match unless there is an explicit else.
Enhancements in SystemVerilog
do….while
foreach
enhanced for loop
forever
1. Ability to declare the for loop control variable within the for loop.
2. Same variables can be declared in different procedural blocks.
3. ++ operator can be used which is not there in verilog.
syntax : for (< initial assignment >; < expression >, < step assignment >)
< statement >
Example :
forloop.sv
while and do…while
while (condition)
foreach
The maximum value of the index in the foreach loop is decided by the size of the declared array
initial
begin
foreach (a[i])
begin
$display (“The array contents are %d",a[i]);
end
#1 $finish;
end
endmodule
Jump break and continue
Syntax : return
return.sv
Does not return any value. Just used to come out of the void function or
task.
Tasks and Functions
Input ,output, inout argument values can be passed into a task and
only input arguments into functions.
Tasks
Task Syntax:
//port declarations//
begin task_intro.sv
<statements>
end
endtask
Functions
Can also have local variables, registers, time variables, integers or events
Do not contain always or initial statements but are called from these blocks,
or other tasks and functions
Example :
function logic [15:0] example(int a, int b, output logic [15:0] u, v);
…….
endfunction
a, b : default to inputs
u, v : Both are outputs
begin…..end can be omitted – Sequential execution is maintained
Function - Example
module function_intro ();
bit a ;
initial begin
#1 a = doInit(4,5);
#1 a = doInit(9,6);
#1 $finish;
end
endmodule
Void function
System Verilog allows using void data type to discard a function’s return value(no
return value)
module function_discard ();
bit a ;
Eg. : void ‘(function_name()); initial begin
#1 void'(doInit(4,5));
#1 void'(doInit(9,6));
#1 $finish;
end
function bit unsigned doInit (bit [3:0] count, add);
reg [7:0] b;
if (count > 5) begin
$display ("@%g Returning from function", $time);
end
b = add;
$display ("@%g Value passed is %d", $time, count + b);
endfunction
endmodule
Argument passing in function /task
endtask
Pass by reference
The task/function can then access the argument data via the reference.
Both the caller and the subroutine share the same representation of the
argument
endfunction
Pass by reference - ex
function_pass_by_ref.sv
Tasks and Function
Functions Tasks
Statements that follow a “fork...join” are blocked from execution until all the processes have completed
execution.
fork...join_any
Statements that follow a “fork...join_any” are blocked from execution until any one of the processes has
completed execution.
fork...join_none
Statements that follow a “fork...join_none “are not blocked from execution while the processes are executing.
Does not wait for any forked process is complete and thus starts execution statements after the join_none