Professional Documents
Culture Documents
WEEK 13
The following example designs a task FA and calls it in a loop four times to generate a 4 bit ripple
carry adder:
always@* begin
carry[0] = c_in;
for( i=0; i<4 ; i=i+1)
begin FA(a[i], b[i], carry[i], sum[i], carry[i+1]); end
c_out = carry[4];
end
endmodule
www.iiu.edu.pk 3 Saturday, November 27, 2021
Functions
Verilog function is in many respects like task as it also implements code that can
be called several times inside a module.
A function is defined in the module using function and endfunction keywords.
The function can compute only one output. To compute this output, the function
must have at least one input.
The output must be assigned to an implicit variable bearing the name and range of
the function.
The range of the output is also specified with the function declaration.
A function in Verilog cannot use timing constructs like # or @ . A function can be
called from a procedural block or continuous assignment statement.
It may also be called from other functions and tasks, whereas a function cannot
call a task. A reentrant function can be designed by adding the automatic
keyword.
A simple example here writes a function to implement a 2:1 multiplexer and then
uses it three times to design a 4:1 multiplexer:
Function
A function can enable another function but not another task.
Functions always execute in 0 simulation time.
Functions must not contain any delay, event, or timing control statements.
Functions must have at least one input argument. They can have more than one
input.
Functions always return a single value. They cannot have output or inout
arguments.
www.iiu.edu.pk 7 Saturday, November 27, 2021
Task and Function
Task
Tasks are used for common Verilog code that contains delays, timing, event constructs, or multiple output arguments.
Tasks can have input, output, and inout arguments
Function
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.
Functions can have input arguments. In addition, they can have local variables, registers, time variables, integers, real, or events.
Task and Function Similarities
Both tasks and functions must be defined in a module and are local to the module.
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.
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:
There are delay, timing, or event control constructs in the procedure.
The procedure has zero or more than one output arguments.