System Verilog Imp
System Verilog Imp
1.
1
LOGIC
2
BIT: for faster simulation, less memory consumption, used when x,z values not expected[
for eg length can be 0 to n and not x or z.
3
BYTE:shorter version of integer of 8 bits(full code not written)
4
STRING
5
6
7
8
9
10
11
12
13
14
15
Creating our own data types in SystemVerilog
To create our own datatypes 3 keywords provided by sv
1. typedef
2. enum
3. struct
1. TYPEDEF
For this example, let's suppose that we wanted to create an 8 bit wide logic type to use in our code.
Now we can declare a variable which uses the logic8 data type we created using a typedef.
For this example, suppose we wanted to create an array which consists of 4 elements of 8 bit logic
vectors.
16
2. ENUM
In SystemVerilog, we typically use enum types to encode the states of a finite state
machine.
Syntax:
After we have created an enumerated type, we can only assign it to one of the values which are listed
in the <values> field.
if we want to use the enum type in more than one place then we can use a typedef to create a new type
instead.
For example
When we create an enum in SystemVerilog, we are actually creating a group of labels for an
underlying int.
By default, the actual value associated with the enumerated value will be an int. The value of
the underlying int depends on the order of the values in our list.
For example
17
18
3. Struct and union
We use SystemVerilog structs and unions to group a number of related variables together.
This is a useful feature as it allows us to create more complex data types which represents
related data. For example, we could use a struct to store the three values required in an RGB
type display.
Although unions an structs are very similar to one another, there is an important difference in
the way they work.
When we write a struct in SystemVerilog, each of the different members of the struct
are allocated their own memory space.
As a result of this, we can assign values to each of the different members in the struct
independently.
In contrast to this, when we write a union each of the different members of the union use a
shared memory location.
This means that when we write data to one of the members in the union, all of the other
members will be assigned to this value.
19
ARRAYS
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.
Static Arrays
A static array is one whose size is known before compilation time. In the example shown below,
a static array of 8-bit wide is declared.
module tb;
bit [7:0] m_data; // A vector or 1D packed array
initial begin
// 1. Assign a value to the vector
m_data = 8'hA2;
20
end
end
endmodule
1. packed arrays
2. unpacked arrays.
21
Unpacked arrays may be fixed-size arrays, dynamic arrays, associative arrays or
queues.
22
3. PACKED+UNPACKED ARRAY
Dynamic Arrays
A dynamic array is one whose size is not known during compilation, but instead is
defined and expanded as needed during runtime. A dynamic array is easily recognized
by its empty square brackets [ ] .
23
24
COPYING DYNAMIC ARRAY EXAMPLE
Associative Arrays
An associative array is one where the content is stored with a certain key. This is easily
recognized by the presence of a data type inside its square brackets [ ] . The key is
represented inside the square brackets.
25
int m_name [string]; // Key is of type string,
and data is of type int
26
Queues
A queue is a data type where data can be either pushed into the queue or popped from
the array. It is easily recognized by the $ symbol inside square brackets [ ] .
27
QUEUE METHODS EXAMPLES
28
QUEUE OF CLASSES IN SYSTEM VERILOG
29
QUEUE OF DYNAMIC ARRAYS IN SYSTEM VERILOG
30
31
STRUCTURE
A structure can contain elements of different data types which can be referenced as a
whole or individually by their names. This is quite different from arrays where the
elements are of the same data-type.
A structure is unpacked by default and can be defined using the struct keyword and a
list of member declarations can be provided within the curly brackets followed by the
name of the structure.
32
STRUCT USING TYPEDEF
if there's a need to create multiple structure variables with the same constituents, it'll be
better to create a user defined data type of the structure by typedef. Then st_fruit will
become a data-type which can then be used to create variables of that type.
33
PACKED STRUCTURES
TYPEDEF
34
SYSTEMVERILOG LOOPS
DO WHILE: This executes the code first and then checks for the condition to see if the
code should be executed again.
35
FOREACH: This is best suited to loop through array variables, because you don't have to
find the array size, set up a variable to start from 0 until array_size-1 and increment it on
every iteration.
36
BREAK:
CONTINUE:
37
IF_ELSE_IF:
unique-if
unique0-if
priority-if
Unlike unique-if, unique0-if does not report a violation if none of the conditions match
38
NO ELSE BLOCK FOR UNIQUE IF
39
priority-if
priority-if evaluates all conditions in sequential order and a violation is reported
when:
None of the conditions are true or if there's no else clause to the final if construct
No else clause in priority-if
40
CASE
Same as Verilog
Unique case
unique and unique0 ensure that there is no overlapping case items and hence can be
evaluated in parallel. If there are overlapping case items, then a violation is reported.
If more than one case item is found to match the given expression, then a
violation is reported and the first matching expression is executed
If no case item is found to match the given expression, then a violation is
reported only for unqiue
41
42
BOCKING AND NON BLOCKING ASSIGNMENTS
BLOCKING
43
1. there are two initial blocks which are executed in parallel
2. Statements are executed sequentially in each block and both blocks finish
at time 0ns.
3. Variable a gets assigned first, followed by the display statement which is
then followed by all other statements.
NON BLOCKING
In above example if we replace ‘=’ with ‘<=’, following output observed
Non-blocking assignment allows assignments to be scheduled without blocking the
execution of following statements.
The RHS of every non-blocking statement of a particular time-step is captured, and
moves onto the next statement. The captured RHS value is assigned to the LHS variable
only at the end of the time-step.
EVENT
An event is a static object handle to synchronize between two or more concurrently
active processes. One process will trigger the event, and another process waits for the
event.
44
What is the difference between @ and .triggered ?
An event's triggered state persists throughout the time step, until simulation advances.
Hence if both wait for the event and trigger of the event happens at the same time there
will be a race condition and the triggered property helps to avoid that.
wait_order
Waits for events to be triggered in the given order, and issues an error if any event
executes out of order.
Merging Events
45
When one event variable is assigned to another, all processes waiting for the first event
to trigger will wait until the second variable is triggered.
Functions
The primary purpose of a function is to return a value that can be used in an expression
and cannot consume simulation time.
A function cannot have time controlled statements like @, #, fork join, or wait
A function cannot start a task since tasks are allowed to consume simulation time
Often times we find certain pieces of code to be repetitive and called multiple times
In such cases, we can declare a function and place the repetitive code inside the
function and allow it to return the result. This will reduce the amount of lines in the RTL
drastically since all you need to do now is to do a function call and pass data on which
the computation needs to be performed.
Note that a function shall have atleast one input declared and the return type will be void
if the function does not return anything.
Syntax
function [automatic] [return_type] name ([port_list]);
[statements]
endfunction
46
DIFFERENCE BETWEEN FUNCTION AND TASK
When we write a function, it performs a calculation and returns a single value.
In contrast, a task executes a number of sequential statements but doesn't return a value.
Instead, the task can have an unlimited number of outputs
The keyword automatic will make the function reentrant and items declared within the
task are dynamically allocated rather than shared between different invocations of the
task. This will be useful for recursive functions and when the same function is executed
concurrently by N processes when forked.
Function declarations
There are two ways to declare inputs to a function:
function [7:0] sum;
input [7:0] a, b;
begin
sum = a + b;
end
endfunction
Calling a function
A function call is an operand with an expression and has a syntax as shown below.
reg [7:0] result;
reg [7:0] a, b;
initial begin
a = 4;
b = 5;
#10 result = sum (a, b); //////////result is a function
end
Function rules
47
A function cannot start a task because it may consume simulation time, but can
call other functions
A function should have atleast one input
A function cannot have non-blocking assignments or force-release or assign-
deassign
A function cannot have any triggers
A function cannot have an output or inout
Recursive Functions
Functions that call itself are called recursive functions. In the example shown below, a
recursive function is written to compute the factorial of a given number.
module tb;
initial begin
integer result = factorial(4);
$display("factorial(4) = %0d", result);
end
return result;
endfunction
endmodule
Arguments that are passed by reference are not copied into the subroutine area, instead
a reference to the original argument is passed to the subroutine. The argument
declaration is preceded by the ref keyword. Any changes made to the variable inside
the subroutine will be reflected in the original variable outside the subroutine.
48
Example:pass by value
Example:pass by reference
49
50
51