Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

task and functions

Tasks and functions


• Repeatedly used functionality/logic makes the description
verbose.

• Commonly used logic can be separated and can be


implemented with tasks/functions.

• Tasks and functions are used to abstract verilog code that is


used in many places in the design.

• Tasks & functions provide the ability to execute common


logic/functionality from several different places in a
description.

• Tasks and functions are procedures.


Task properties
• Task can be used with delay, timing or event control
constructs.
• Tasks can have zero or more arguments.
• Supports input, output and inout arguments.
• Passes the values through output and inout arguments to
the task call.
• A task can call other tasks and functions as well.
• Tasks are declared with keyword task and endtask.
Task format
task log_cal;
task <task_name>;
parameter DEL = 30;
<task_declarations >
output [7:0]and_out,or_out,xor_out;
input declarations;
input [7:0]in1, in2;
output declarations;
begin
inout declarations; #DEL and_out = in1 & in2;
or_out = in1 | in2;
begin
<assignments> xor_out = in1 ^ in2;
end end
endtask
endtask
Task calling

• A task is called or enabled by a task enable statement that


specifies the argument values passed to the task and the
variables that receive the results.

• A task enable statement is a procedural statement.

• The list of arguments must match the order of input, output


and inout declarations in the task definition.
Task calling
• Arguments are passed by value, not by reference.

• A task can be called more than once concurrently with each


call having its own control.

• Variables declared within a task is static.

• Output and inout arguments in a task call must be


registers.
Task calling example
wire [7:0]P, Q;
reg [7:0]PQ_AND, PQ_OR, PQ_XOR;

always @(P or Q)
begin
log_cal (PQ_AND, PQ_OR, PQ_XOR, P, Q);
end
Function properties
• Functions should not contain delay information.
• Functions must contain at least one input.
• Supports only input arguments.
• A function can call other function only.

• Can return only one value.


• Functions are declared with keyword function and
endfunction.
Function format

function par_cal;
function <range><function_name>;
input [15:0]dat_in;
<function_declarations >
begin
input declarations;
par_cal = ^dat_in;
begin end
<assignments>
end endfunction
endfunction
Function calling

• A function is called or enabled by a function enable call that


specifies the argument values passed to the function.

• A function call is a part of an expression i.e. an operand


within an expression.

• Function returns the computed result in the name of the


function itself.
Function calling
• The list of input arguments must match the order of input
declarations in the function definition.

• Arguments are passed by value, not by reference.

• A function can be called more than once concurrently with


each call having its own control.

• Variables declared within a function is static.


Function calling example

wire [15:0]data;
reg parity_value;

always @(enable)
begin
if (enable == 1’b1)
parity_value = par_cal(data);
end
Tasks and Functions
functions tasks
 can enable other function,  can enable other functions
not a task. and tasks.
 execute in 0 simulation  execute in non 0 simulation
time. time.
 do not support delay, event  can have delays, event or
or timing controls. timing controls.
 must have at least one  can have zero or more
input argument, can have arguments of type input,
more than one input. output or inout.
 return single value , they  do not return any value, but
can not have output or can pass multiple values
inout arguments. thro’ output and inout args.
When to use tasks ?
• Use tasks when:
– there are delay, timing, or event control constructs in
the procedure.

OR
– the procedure has zero or more than one output
arguments.

OR
– the procedure has no input arguments.
When to use functions ?

• Use functions when:


– there are no delay, timing, or event control constructs
in the procedure.
AND
– the procedure returns a single value.
AND
– there is at least one input argument.

You might also like