SV basics: task and function

overview

 Similar to the C language, functions (function) and tasks (task) can improve code reusability and cleanliness.

Their purpose is to divide large process blocks into smaller pieces, which are easy to read and maintain .

There are similarities and differences between functions and tasks.

function function

The primary purpose of a function is to provide a return value for an operation expression , which not only facilitates the simplification of the original code, but also facilitates the maintenance of large-scale code.

The function definition and use of SV are similar to C:

  1. Functions can specify input variables and output variables
  2. The function can return a value or not return a value (use the void function if it does not return a value)


Argument direction of functions that can be declared

  1. input (default)
  2. output
  3. inout
  4. ref

The way to declare the parameter list:

1. Declare parameters and directions in the parameter list
2. Declare parameters and directions in the function body
Example:

function logic [15:0] myfunc1(int x, int y);
    ...
endfunction
 
function logic [15:0] myfunc2;
    input int x;
    input int y;
    ...
endfunction

The return method of the function of SV       is consistent with that of C

  1. Use return to return the value directly (return will return immediately)
  2. Assign the value to the variable with the same name as the function (after assigning to the variable with the same name as the function, the subsequent code will continue to be executed)

Functions can call functions, but must return immediately. That is, the behavior of blocking and waiting cannot occur.  

Example:

//myfunc1,返回值经过计算,赋予了与函数同名的变量
function [15:0] myfunc1 (input [7:0] x, y) ;
    myfunc1 = x *y - 1;
endfunction
 
//myfunc2,将计算的返回值,通过return返回,并且立即退出函数
function [15:0] myfunc2 (input [7:0] x, y);
    return x *y - 1;
endfunction

 void function

A void function does not return a value. 

If you call a function with a return value, but do not use the return value, we recommend adding void'() for type conversion. [This step can eliminate the warning message brought during compilation]

task task

Similar to functions, tasks also have parameter lists. But the task doesn't return a value.

Task definitions can specify declaration parameters

  • input
  • output
  • inout
  • ref

Task declaration parameter method (same as function) 

  1.  Declare the parameters and direction in the parameter list
  2.  Declare parameters and directions in the task body

Example:  

task mytask1 (output int x, input logic y);
    ...
endtask
 
task mytask2;
    output x;
    input y;
    int x;
    logic y;
    ...
endtask

Tasks can consume simulation time. Some blocking statements can be built in. (unlike functions)

Tasks can call other tasks or functions.

Although the task will not return a value, we can still use return to end the task.

Example: 

In the task light, you need to get the parameters first, then decide how many clock cycles you need to wait, and then set the clock to off.

Light has built-in blocking tasks. To encapsulate such operations, you can only use tasks instead of functions.

The task light, which is called in the always process block, assigns its final result to red 

logic red;
parameter red_tics = 100;
always begin //控制灯光
    red = on; //打开红灯
    light(red,red_tics) ; //等待end
//等待‘tics’时钟上升沿的任务
//在关掉‘color’灯之前
task light (output color,input [31:0] tics) ;
    repeat (tics)@ (posedge clock);
    color = off;//关灯
endtask: light

The difference between tasks and functions

  1.  Functions do not consume simulation time, whereas tasks may. (The biggest difference)
  2. Functions cannot call tasks, but tasks can call functions. (The function needs to return immediately, and there is no built-in blocking statement or task with blocking behavior, so the function can only call the function, not the task. Conversely, the task can call the function, and the task can also call the task, because the task can be built-in blocking statement)
  3. A function can only return a value, while a task does not return a value. (If a function has a return value other than a void return type, the result of the function call can be used as an operand in an expression )
  4.  A function can be used as an operand in an expression, and the value of the operand is the return value of the function .

Parameter passing

Parameter type 
1, input parameter

 The direction of the input parameter is passed by value when the method is called.

 That is, during the transfer process, the value of the external variable will be copied and assigned to the input parameter.

2. output, inout parameters

Similar to input parameters, output and inout will also undergo a process of value transfer when they are passed in or passed out.

 The process of value passing only occurs when the method is called and returned.

3. ref parameter

 Different from the previous three parameters. 

 When the ref parameter is passed, the value copy will not occur, but the variable pointer will be passed to the method, and the operation on the parameter inside the method will affect the external variable at the same time.

If you want to prevent the ref parameter passed in from the outside from being modified by the method, you can add a const modifier to indicate that the variable is a read-only variable.

 The way to pass parameters

  1. Pass parameters by parameter position when calling a method
  2. Parameters are passed by way of parameter name mapping. (SV allows instantiation similar to modules)

Pass parameters according to their position 

Specifies the default value for the parameter

SV allows methods to specify default values ​​for input parameters when declaring them.

        When a method with parameter default values ​​is called, if these parameters are not passed values, then the compiler will pass in the corresponding default values ​​for these parameters.

Example:  

There are many ways to pass parameters when calling the read() function. Brief summary:

Default values ​​can be used for the two input parameters. When calling, at least one parameter value must be passed, and the position of the three parameters should be followed.

//没有指定方向的情况下,参数都是输入方向
//为参数j 、data指定了默认值
task read(int j = 0, int k, int data = 1 );
    ...
endtask
 
read( , 5 );//等同于read ( 0,5,1 );
read ( 2,5 );//等同于read( 2,5,1 );
read( , 5,);//等同于read ( 0,5,1 );
read( , 5,7 );//等同于read( 0,5,7 );
read( 1,5,2 );//等同于read( 1,5,2 );
read ( );//错误;k没有默认值
read( 1,, 7 );//错误;k没有默认值

Pass parameters by means of parameter name mapping

        SV allows similar to module instantiation, parameters can be passed by parameter location when calling a method, and parameters can also be passed by parameter name mapping.

Example:  

function int fun( int j = 1,string s = "no”);
    ...
endfunction
fun( .j(2),.s("yes") ) ; //fun( 2,"yes");
fun( .s("yes") ); //fun( 1,"yes" );
fun( , "yes" );  //fun( 1,"yes" ) ;
fun( . j(2) );  //fun( 2,“no”);
fun( .s("yes"),.j(2));  // fun( 2, "yes");
fun( .s(),.j() );  // fun( 1, "no”);
fun( 2 ); //fun( 2, "no”);
fun( );  //fun


Copyright statement: This article is an original article of CSDN blogger "Tong Tonghua", which follows the CC 4.0 BY-SA copyright agreement. For reprinting, please attach the original source link and this statement.
Original link: https://blog.csdn.net/weixin_41788560/article/details/124057717

Guess you like

Origin blog.csdn.net/Arvin_ing/article/details/127224779