Verilog Task
A function is meant to do some processing on the input and return a single value. In contrast, a task is more general and can calculate multiple result values and return them using output and inout type arguments.
Tasks can contain time-consuming simulation elements such as @, posedge, and others. Tasks are used in all programming languages, generally known as procedures or subroutines.
Data is passed to the task, the processing is done, and the result returned. They have to be specifically called, with the data ins and outs, rather than just wired into the general netlist.
Included in the main body of code, they can be called many times, reducing code repetition.
- Tasks are defined in the module in which they are used. It is possible to define a task in a separate file and use the compiled directive, including the task in the file, which instantiates the task.
- Tasks can include timing delays, such as posedge, negedge, # delay, and wait.
- Tasks can have any number of inputs and outputs.
- The variables declared within the task are local to that task. The order of declaration within the task defines how the variables passed to the task by the caller are used.
- Tasks can take, drive, and source global variables when no local variables are used. When local variables are used, the output is assigned only at the end of task execution.
- Tasks can call another task or function.
- Tasks can be used for modeling both combinational and sequential logic.
A task must be specifically called with a statement. It cannot be used within an expression as a function can.
Syntax
A task begins with keyword task and ends with keyword endtask. Inputs and outputs are declared after the keyword task.
Local variables are declared after input and output declaration.
The keyword automatic will make the reentrant task. Otherwise, it will be static by default. If a task is static, all its member variables will be shared across different invocations of the same task that has been launched concurrently.
NOTE: Hierarchical references cannot access an automatic task item.
Calling a task
If the task does not need any arguments, then a list of arguments can be avoided. If the task needs arguments, they can be provided in the same statement as its invocation.
The task-enabling arguments (x, y, z) correspond to the arguments (a, b, c) defined by the task.
Since a and b are inputs, values of x and y will be placed in a and b, respectively. Because c is declared output and connected with z during invocation, the sum will automatically be passed to the variable z from c.
Global tasks
Tasks that are declared outside all modules are called global tasks as they have a global scope and can be called within any module.
After executes the above code, it produces the following output.
Difference between Function and Task
Function | Task |
---|---|
It cannot have time-controlling statements/delay and hence executes in the same simulation time unit. | It can contain time-controlling statements/delay and may only complete at some other time. |
It cannot enable a task. | It can enable other tasks and functions. |
The function should have atleast one input argument and cannot have output or inout arguments. | Tasks can have zero or more arguments of any type. |
A function can return only a single value. | It cannot return a value but can achieve the same effect using output arguments. |