Definition of function and task (systemverilog)

Keywords: systemverilog

There are two methods defined in sv's class:.function and task.Below are descriptions of their features and ways of declaring them, as well as their similarities and differences.

1. function

Features of 1.1 function

  • function cannot have built-in time-consuming statements, which are returned immediately upon invocation, i.e. do not consume emulation time;
  • A function must have at least one input variable, either no return value or one return value.
  • The default data type of the variable in the function parameter list is logic type.
  • If the inversion is not specified for a variable in the parameter list of a function, the default is the input direction.
  • If a function has a return value, the return value type of the function needs to be specified when the function is defined, and only one value can be returned by return, or by assignment to an internal variable with the same name as the function (rarely used);
  • If the function does not return a value, specify void when the function is defined;
  • Functions cannot call task because task may contain time-consuming statements, which are not supported by functions.

Definition of 1.1 function

1. Parameter Direction

The parameter direction can be:

  • input
  • output
  • inout
  • ref, which is a reference, will be used later
// input int x ;
// input int y;
function logic [15:0] myfunc1(int x, int y);

function logic [15:0] myfunc2;
	input int x;
	input int y;

//Return parameter type logic [15:0] 
function logic [15:0] myfunc3(int a, int b, output logic [15:0] u, v);

(2) Return value

When there is a return value, the function is defined as follows:

//Method 1: By assigning a value to an internal variable with the same name as the function;
//This is rarely used, as is the practice of introducing deep copy chapters in SV green papers
function [15:0] myfunc1 (input [7:0] x,y);
	myfunc1 = x * y - 1; // 

//Mode 2: Return with return
function [15:0] myfunc2 (input [7:0] x,y);
	return x * y - 1; // Return value with return

a = b + myfunc1(c, d);

When there is no return value, the function is defined as follows:

function void myprint (int a);

If you do not want the return value of a function, you can also use a cast to discard the return value of a function that has a return value, as follows:

//Normally, if a dynamic cast fails or succeeds, there will be a return value.
//When we don't care about this return value, we can cast it to void

(3) Static and dynamic function s

Refer to my previous articles: Static and dynamic One article.

2. task

Features of 2.1 task

  • Task can have built-in time-consuming statements, that is, when task is called, the result may not be returned immediately;
  • Task can call either function or task.
  • task cannot use return return return values;
  • task can return a value by defining the parameter direction in the parameter list, and it can return one or more values.
  • The parameters in the task's parameter list default to input if direction is not specified;
  • The default parameter data type is the logic type.

Definition of 2.2 task

1. Parameter Direction

The parameter direction can be:

  • input
  • output
  • inout
  • ref, which is a reference, will be used later

Here are two task s with direction definitions.

//Input a, b;The default is input direction
//output  logic [15:0] u,
//output  logic v;
task mytask1(a, b, output logic [15:0] u, v); 
task mytask2;
	input a;
	input b;
	output logic [15:0] u;
	output logic  v;


module traffic_lights;
	logic clock, red, green;
	parameter on = 1, off = 0;
	parameter red_tics = 350, green_tics = 200;

	initial red = off;
	initial green = off;

	always begin // waveform for the clock
		#100 clock = 0;
		#100 clock = 1;
	always begin
		red = on; // Turn on the red light
		light(red, red_tics); // Waiting for red_After tics cycle, turn off the red light
		green = on; //  Turn on the green light
		light(green, green_tics); // Waiting for green_After tics cycle, turn off the green light

	//First wait until the clock of the tics cycle, then turn off the lights that are passed in with the appropriate color
	task light (output color, input [31:0] tics);
		repeat (tics) @ (posedge clock);
		color = off; //Turn off lights of the appropriate color
	endtask: light
endmodule: traffic_lights

(2) Use of ref

The following passes the array directly into the task.This method copies the a, b array directly into the stack and passes it to the method.

//input  [3:0][7:0] a
//input  [3:0][7:0] b [3:0]
//output [3:0][7:0] y[1:0]
task mytask3(input [3:0][7:0] a, b[3:0], output [3:0][7:0] y[1:0]);

It doesn't matter if the array takes up less memory.If it's a very large array, it will affect memory.You can then use ref to pass the array.

  • ref is a reference to a variable (but not a net), and its value is the last value assigned to it.
  • However, if one variable is connected to multiple ref ports, competition may arise because ports of multiple modules may update the same variable;

Advantages of using ref:

  • Parameters are passed by reference, not by copy.If ref is not used, direct references to arrays will be copied into the stack area, affecting performance;
  • The result of modifying a variable in a task is always visible to the function calling it;
  • const ref keeps the referenced object unchanged in the subprogram;

(3) Static and dynamic task s

For static and dynamic, start with my blog: Static and dynamic One article.

  • task for module, interface, program and package is static by default.
  • If a task is declared static, it can be visible to others throughout the simulation process and will always exist.
  • If a task is static/automatic, then all member variables within it are also static/automatic;
  • If you want to declare an automatic task within a module, you need to display it with the automatic modifications as follows:
 task automatic my_auto_task(x,output y);

3. Differences between function and task

You can read my previous blog directly: The difference between function and task.

Posted by argoSquirrel on Fri, 03 Sep 2021 18:38:19 -0700