Advanced VLSI
Advanced VLSI
~SAI SAGAR S P-
1RF21EC041
TABLE OF CONTENTS
1. Introduction
2. Task overview
3. Routine Arguments
6. Default values
Tasks are similar to functions in other programming languages but are distinct in that they can
perform complex actions, including interactions with simulation time, and can return multiple
outputs through output or inout arguments.
EXAMPLE:
task generate_clock_pulse(output bit clk);
clk = 1;
#5 clk = 0; // Delay for 5 time units
endtask
● Types of Tasks:
○ Non-blocking: Executes and allows the simulation to proceed concurrently.
○ Blocking: Pauses execution until the task is completed.
ROUTINE ARGUMENTS:
For tasks:
task my_task(input logic a, output int b, inout bit
c); Example of Routine Argument Usage
// Task body
endtask task compute_sum(input int a, input int b,
output int sum, inout int counter);
sum = a + b;
For functions:
counter += 1; // Increment counter each
function int my_function(input int a, input int b); time the task is called
// Function body endtask
endfunction
Common coding error
In SystemVerilog, one common coding mistake is forgetting that the argument type is sticky, meaning
that the argument type of the current argument can be carried over from the previous one if not explicitly
declared
For example
task my_task(input int a, b, c);
// b and c are also treated as inputs of type int because a is defined as such.
endtask
To avoid sticky types, always declare both the direction (input, output, inout, ref) and the data type
explicitly for each argument in tasks and functions. For example:
task my_task(input int a, input int b, input int c);
// Now, each argument has an explicit declaration.
endtask
Returning from a Routine
Example:
● Tasks in SystemVerilog do not return values but may still use the return statement to terminate
execution early. This can be useful in cases where the task encounters an error or when the
required operation is complete before the end of the task body.
To return arrays from a function, it's best to use typedef to define the array type. This simplifies the
function definition and keeps your code readable, especially when dealing with multi-dimensional or
complex arrays.
endfunction
Default Values:
As your testbench grows in sophistication, you may want to add additional controls to your code
but not break existing code.
In SystemVerilog you can specify a default value that is used if you leave out an argument in the
call.
The following example adds low and high arguments to the print_csm function so you can
print a checksum of a range of values.
You can call this function in the following ways, as shown in the sample . Note
that the first call is compatible with both versions of the print_csm routine.
Using a default value of −1 (or any out-of-range value) is a good way to see if the
call specified a value.
A Verilog for loop always executes the initialization ( int i=low ), and test
( i<=high ) before starting the loop. Thus, if you accidentally passed a low value that
was larger than high or the array size, the for loop would never execute the body.
Passing Argument by name:
In the SystemVerilog LRM that the arguments to a task or function are sometimes
called “ports,” just like the connections for a module.
If you have a task or function with many arguments, some with default values, and
you only want to set a few of those arguments, you can specify a subset by
specifying the name of the routine argument with a port-like syntax, as shown