Systemverilog Procedural Statements: New Operators
Systemverilog Procedural Statements: New Operators
Systemverilog Procedural Statements: New Operators
• New Operators
Increment and decrement operator (Similar to C)
Post-increment/decrement and pre-increment/decrement
j=i++ j is assigned value of i and then i is incremented by 1
j=++i i is incremented by 1 and thenj is assigned value of i
j=i--; j is assigned value of i and then i is decremented by 1
j=--i; i is decremented by 1 and then j is assigned value of i
May cause a race condition since ++ and -- operators behave as
blocking assignments
Example while (i++ < MAX) begin
. . .
end
Last value of i inside the loop will be MAX
1
SystemVerilog Procedural Statements
• Assignment Operators
Equality operators
Case equality operator === , !== (looks for an exact match of x
or z values)
Logical equality operator ==, != (returns 1’bx if x or z in either
operand)
SystemVerilog introduces two new operators known as
wildcard equality operators ( ==? and !=? )
Performs a bitwise operation and treat X and Z values in a
given bit position of their right operand as a wildcard.
X and Z values in the left operand are not treated as wildcards.
a=4’b11x0; a=4’b1001;
b=4’b1100; b=4’b1x01;
a==?b a==?b
unknown Result ?
2
SystemVerilog Procedural Statements
Equivalent to
logic [3:0] a;
if (a inside {4’b0001, 4’b0010, 4’b0100, 4’b1000})
integer a;
a inside { [16:23], [32:47] };
3
SystemVerilog Procedural Statements
4
SystemVerilog Procedural Statements
5
SystemVerilog Procedural Statements
6
SystemVerilog Procedural Statements
• do…while loop
A while loop in Verilog might not execute at all
If the expression is not true at the beginning of the execution of
the while-loop, the statement shall not be executed at all.
do..while loop in System Verilog is similar to C
A do while loop executes atleast once
tests its control expression at the end of the loop
All logic for setting the outputs of the loop can be placed inside
the loop
Makes code more compact and intuitive
7
Verilog Tasks and Functions Overview
• Functions
A function shall not contain any time-controlled statements such
as #100, @(posedge clock) or wait(ready)
function [range]
A function cannot call a task function_name;
The purpose of function is to return a value parameters
Functions evaluate and execute in “0” time input declarations
A function shall contain at least one input reg declarations
… text body …
argument and no output or inout argument. endfunction
Tasks
Tasks shall contain input, multiple outputs or inout arguments
Tasks consume time so it can contain Delay, Timing and
Events
task task_name;
parameters
input declarations
output declarations
inout declarations
reg declarations
… text body …
endtask
8
SystemVerilog Tasks and Functions
• Tasks and Functions
Begin and end statements are optional in systemverilog functions
and tasks.
Functions can have output and inout as formal arguments
Void functions do not return a value
• Return Statement
SystemVerilog adds a return statement
If a return statement is executed that value is returned else the last
value assigned to the function name is the return value
A return statement can be used to exit a task or a function before
reaching its end
The return statement shall override any value assigned to the function
name.
function [15:0] myfunc1 (input [7:0] x,y);
myfunc1 = x * y – 1;
endfunction
9
SystemVerilog Tasks and Functions
• Void functions
Void functions do not have a return value
Output and inout formal arguments allow a void function to
propagate changes to the scope
Void functions can be called as statements like tasks, but have
10
SystemVerilog Tasks and Functions
• Passing task/function arguments by name
SystemVerilog allows arguments to tasks and functions to be
bound by name of formal arguments
It reduces error
Named argument values can be passed in any order
Syntax for named argument passing is same as Verilog’s syntax
for named port connections
11
SystemVerilog Tasks and Functions
• Default formal argument direction and type
Systemverilog allows to specify a default type & value for each
formal argument
When a task or function is called, arguments with defaults can be
omitted from the call
If a value is not passed the default values are used
12
SystemVerilog Tasks and Functions
// function call
byte packet1[1000:1];
int k = crc( packet1 ); // pass by value or by reference: call is the same
13
SystemVerilog Tasks and Functions
• Passing argument values by reference instead of copy
Pass by reference can be read-only by the use of const keyword
Const qualifier can be used together with ref to indicate that the
argument is a read-only variable.
Protect arguments from being modified by task/function
Task ref arguments are sensitive to change
Ref arguments can read current values
Ref arguments can propagate changes immediately
Read only
14
SystemVerilog Tasks and Functions
• Argument Passing
Type is sticky, following arguments default to that type
input - copy value in at beginning - default
output - copy value out at end
inout - copy in at beginning and out at end
ref - pass by reference (effects seen right away)
► Saves time and memory for passing arrays to tasks & functions
Modifier: const - argument cannot be modified
task
task final(x,
final(x, y,
y, output
output bit
bit [7:0]
[7:0] p,
p, q);
q);
15