Module 5b
Module 5b
Tasks have input, output, and inout arguments; functions have input arguments. Thus,
values can be passed into and out from tasks and functions. Considering the analogy of
FORTRAN, tasks are similar to SUBROUTINE and functions are similar to
FUNCTION.
Tasks and functions are included in the design hierarchy. Like named blocks, tasks or
functions can be addressed by means of hierarchical names.
Learning Objectives
175
8.1 Differences between Tasks and Functions
Tasks and functions serve different purposes in Verilog. We discuss tasks and functions
in greater detail in the following sections. However, first it is important to understand
differences between tasks and functions, as outlined in Table 8-1.
Both tasks and functions must be defined in a module and are local to the module. Tasks
are used for common Verilog code that contains delays, timing, event constructs, or
multiple output arguments. Functions are used when common Verilog code is purely
combinational, executes in zero simulation time, and provides exactly one output.
Functions are typically used for conversions and commonly used calculations.
Tasks can have input, output, and inout arguments; functions can have input arguments.
In addition, they can have local variables, registers, time variables, integers, real, or
events. Tasks or functions cannot have wires. Tasks and functions contain behavioral
statements only. Tasks and functions do not contain always or initial statements but are
called from always blocks, initial blocks, or other tasks and functions.
176
8.2 Tasks
Tasks are declared with the keywords task and endtask. Tasks must be used if any one of
the following conditions is true for the procedure:
task_declaration ::=
task [ automatic ] task_identifier ;
{ task_item_declaration }
statement
endtask
| task [ automatic ] task_identifier ( task_port_list ) ;
{ block_item_declaration }
statement
endtask
task_item_declaration ::=
block_item_declaration
| { attribute_instance } tf_input_declaration ;
| { attribute_instance } tf_output_declaration ;
| { attribute_instance } tf_inout_declaration ;
task_port_list ::= task_port_item { , task_port_item }
task_port_item ::=
{ attribute_instance } tf_input_declaration
| { attribute_instance } tf_output_declaration
| { attribute_instance } tf_inout_declaration
tf_input_declaration ::=
input [ reg ] [ signed ] [ range ] list_of_port_identifiers
| input [ task_port_type ] list_of_port_identifiers
tf_output_declaration ::=
output [ reg ] [ signed ] [ range ]
list_of_port_identifiers
| output [ task_port_type ] list_of_port_identifiers
tf_inout_declaration ::=
inout [ reg ] [ signed ] [ range ] list_of_port_identifiers
| inout [ task_port_type ] list_of_port_identifiers
task_port_type ::=
time | real | realtime | integer
177
I/O declarations use keywords input, output, or inout, based on the type of argument
declared. Input and inout arguments are passed into the task. Input arguments are
processed in the task statements. Output and inout argument values are passed back to the
variables in the task invocation statement when the task is completed. Tasks can invoke
other tasks or functions.
Although the keywords input, inout, and output used for I/O arguments in a task are the
same as the keywords used to declare ports in modules, there is a difference. Ports are
used to connect external signals to the module. I/O arguments in a task are used to pass
values to and from the task.
We discuss two examples of tasks. The first example illustrates the use of input and
output arguments in tasks. The second example models an asymmetric sequence
generator that generates an asymmetric sequence on the clock signal.
Example 8-2 illustrates the use of input and output arguments in tasks. Consider a task
called bitwise_oper, which computes the bitwise and, bitwise or, and bitwise ex-or of two
16-bit numbers. The two 16-bit numbers a and b are inputs and the three outputs are 16-
bit numbers ab_and, ab_or, ab_xor. A parameter delay is also used in the task.
178
output [15:0] ab_and, ab_or, ab_xor; //outputs from the task
input [15:0] a, b; //inputs to the task
begin
#delay ab_and = a & b;
ab_or = a | b;
ab_xor = a ^ b;
end
endtask
...
endmodule
In the above task, the input values passed to the task are A and B. Hence, when the task is
entered, a = A and b = B. The three output values are computed after a delay. This delay
is specified by the parameter delay, which is 10 units for this example. When the task is
completed, the output values are passed back to the calling output arguments. Therefore,
AB_AND = ab_and, AB_OR = ab_or, and AB_XOR = ab_xor when the task is
completed.
Another method of declaring arguments for tasks is the ANSI C style. Example 8-3
shows the bitwise_oper task defined with an ANSI C style argument declaration.
Tasks can directly operate on reg variables defined in the module. Example 8-4 directly
operates on the reg variable clock to continuously produce an asymmetric sequence. The
clock is initialized with an initialization sequence.
Tasks are normally static in nature. All declared items are statically allocated and they are
shared across all uses of the task executing concurrently. Therefore, if a task is called
concurrently from two places in the code, these task calls will operate on the same task
variables. It is highly likely that the results of such an operation will be incorrect.
To avoid this problem, a keyword automatic is added in front of the task keyword to
make the tasks re-entrant. Such tasks are called automatic tasks. All items declared inside
automatic tasks are allocated dynamically for each invocation. Each task call operates in
an independent space. Thus, the task calls operate on independent copies of the task
variables. This results in correct operation. It is recommended that automatic tasks be
used if there is a chance that a task might be called concurrently from two locations in the
code.
181
8.3 Functions
Functions are declared with the keywords function and endfunction. Functions are used if
all of the following conditions are true for the procedure:
function_declaration ::=
function [ automatic ] [ signed ] [ range_or_type ]
function_identifier ;
function_item_declaration { function_item_declaration }
function_statement
endfunction
| function [ automatic ] [ signed ] [ range_or_type ]
function_identifier (function_port_list ) ;
block_item_declaration { block_item_declaration }
function_statement
endfunction
function_item_declaration ::=
block_item_declaration
| tf_input_declaration ;
function_port_list ::= { attribute_instance } tf_input_declaration {,
{ attribute_instance } tf_input_declaration }
range_or_type ::= range | integer | real | realtime | time
There are some peculiarities of functions. When a function is declared, a register with
name function_identifer is declared implicitly inside Verilog. The output of a function is
passed back by setting the value of the register function_identifer appropriately. The
function is invoked by specifying function name and input arguments. At the end of
function execution, the return value is placed where the function was invoked. The
optional range_or_type specifies the width of the internal register. If no range or type is
182
specified, the default bit width is 1. Functions are very similar to FUNCTION in
FORTRAN.
Notice that at least one input argument must be defined for a function. There are no
output arguments for functions because the implicit register function_identifer contains
the output value. Also, functions cannot invoke other tasks. They can invoke only other
functions.
We will discuss two examples. The first example models a parity calculator that returns a
1-bit value. The second example models a 32-bit left/right shift register that returns a 32-
bit shifted value.
Parity calculation
Let us discuss a function that calculates the parity of a 32-bit address and returns the
value. We assume even parity. Example 8-7 shows the definition and invocation of the
function calc_parity.
183
Note that in the first invocation of calc_parity, the returned value was used to set the reg
parity. In the second invocation, the value returned was directly used inside the $display
task. Thus, the returned value is placed wherever the function was invoked.
Another method of declaring arguments for functions is the ANSI C style. Example 8-8
shows the calc_parity function defined with an ANSI C style argument declaration.
Left/right shifter
To illustrate how a range for the output value of a function can be specified, let us
consider a function that shifts a 32-bit value to the left or right by one bit, based on a
control signal. Example 8-9 shows the implementation of the left/right shifter.
end
endfunction
...
...
endmodule
However, the keyword automatic can be used to declare a recursive (automatic) function
where all function declarations are allocated dynamically for each recursive calls. Each
call to an automatic function operates in an independent variable space.Automatic
function items cannot be accessed by hierarchical references. Automatic functions can be
invoked through the use of their hierarchical name.