Module4-1
Module4-1
3.1 Introduction
• As you verify your design, you need to write a great deal of code, most of which
is in tasks and functions.
• System Verilog introduces many incremental improvements to make this easier
by making the language look more like C, especially around argument passing
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
Returning a value
The function definition will implicitly create an internal variable of the
same name as that of the function. Hence it is illegal to declare another
variable of the same name inside the scope of the function. The return
value is initialized by assigning the function result to the internal variable
Sum =a+b
Function Rules
•A function cannot contain any time-controlled
statements like #, @, wait, posedge, negedge
•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.
Task
A function is meant to do some processing on the input and return a single
value, whereas a task is more general and can calculate multiple result
values and return them using output and inout type arguments.
Tasks can contain simulation time consuming elements such
as @, posedge and others.
Syntax
A task need not have a set of arguments in the port list, in which case it can
be kept empty.
// Style 1
Static Task
If a task is static, then all its member variables will be shared across different
invocations of the same task that has been launched to run concurrently
A label on a begin or fork statement, you can put the same label on the matching end or join
statement.
This makes it easier to match the start and finish of a block.
You can also put a label on other System Verilog end statements such as endmodule, endtask,
endfunction, and others that you will learn in this book.
Example 3-1 demonstrates some of the new constructs.
3.3 Tasks, Functions, and Void Functions
SystemVerilog allows you to pass array arguments without the ref direction, but the array is copied onto the stack, an
expensive operation for all but the smallest arrays
Example 3-10 also shows the const modifier.
a result, the array a is initialized when print_sum is called, but cannot be modified in the routine.
Always use ref when passing arrays to a routine. If you don’t want the routine to change the array values, use
the const ref type. With this, the compiler checks that your routine does not modify the array.
The second benefit of ref arguments is that a task can modify a variable and is instantly seen by the calling
function. This is useful when you have several threads executing concurrently and want a simple way to pass
information.
3.5.4 Default Argument Values
In System Verilog you can specify a default value that is used if you leave out an
argument in the call
3.5.4 Default Argument Values
3.5.5 Common Coding Errors
The most common coding mistake that you are likely to make with a routine is forgetting that
the argument type is sticky with respect to the previous argument, and that the default type
for the first argument is a single-bit input. Start with the following simple task header.
The two arguments are input integers. As you are writing the task, you realize that you need access
to an array, so you add a new array argument, and use the ref type so it does not have to be copied
What argument types are a and b? They take the direction of the previous argument that is a ref. Using ref for a simple
variable such as an int is not usually needed, but you would not get even a warning from the compiler, and thus would
not realize that you were using the wrong type