Slide8-Tasks and Functions
Slide8-Tasks and Functions
Dr.Ravi Sindal
IET,DAVV,Indore
Today program
Reusing code
Tasks and Functions
Dr.Ravi Sindal,IET,DAVV,Indore
Introduction
Procedures/Subroutines/Functions in SW
programming languages
The same functionality, in different places
Verilog equivalence:
Tasks and Functions
Used in behavioral modeling
Part of design hierarchy Hierarchical name
Dr.Ravi Sindal,IET,DAVV,Indore
Contents
Functions
Tasks
Differences between tasks and functions
Dr.Ravi Sindal,IET,DAVV,Indore
Tasks and Functions
Functions
Functions
Dr.Ravi Sindal,IET,DAVV,Indore
Functions (cont’d)
Dr.Ravi Sindal,IET,DAVV,Indore
Functions (cont’d)
Dr.Ravi Sindal,IET,DAVV,Indore
Functions (cont’d)
Semantics
much like function in Pascal
An internal implicit reg is declared inside the
function with the same name
The return value is specified by setting that
implicit reg
<range_or_type> defines width and type of
the implicit reg
<type> can be integer or real
default bit width is 1
Dr.Ravi Sindal,IET,DAVV,Indore
Function Examples
Parity Generator
module parity; function calc_parity;
reg [31:0] addr; input [31:0] address;
reg parity; begin
calc_parity = ^address;
initial begin end
… endfunction
end
endmodule
always @(addr)
begin
parity = calc_parity(addr);
$display("Parity calculated = %b",
calc_parity(addr) );
end
Dr.Ravi Sindal,IET,DAVV,Indore
Function Examples
Controllable Shifter
module shifter; function [31:0] shift;
`define LEFT_SHIFT 1'b0 input [31:0] address;
`define RIGHT_SHIFT 1'b1 input control;
reg [31:0] addr, left_addr, begin
right_addr; shift =
reg control; (control==`LEFT_SHIF
T) ?(address<<1) :
initial (address>>1);
begin end
… endfunction
end
endmodule
always @(addr)
begin
left_addr =shift(addr, `LEFT_SHIFT);
right_addr =shift(addr,`RIGHT_SHIFT);
end Dr.Ravi Sindal,IET,DAVV,Indore
Tasks and Functions
Tasks
Tasks
Dr.Ravi Sindal,IET,DAVV,Indore
Tasks (cont’d)
task <task_name>;
<I/O declarations>
<variable and event declarations>
begin // if more than one statement needed
<statement(s)>
end // if begin used!
endtask
Dr.Ravi Sindal,IET,DAVV,Indore
Tasks (cont’d)
Dr.Ravi Sindal,IET,DAVV,Indore
Tasks (cont’d)
Dr.Ravi Sindal,IET,DAVV,Indore
Task Examples
Use of input and output arguments
Dr.Ravi Sindal,IET,DAVV,Indore
Tasks and Functions
Differences between
Tasks and Functions
Differences between...
Functions Tasks
Can enable (call) just Can enable other tasks
another function (not and functions
task) May execute in non-
Execute in 0 simulation zero simulation time
time May contain any timing
No timing control control statements
statements allowed May have arbitrary
At lease one input input, output, or
Return only a single inout
value Do not return any value
Dr.Ravi Sindal,IET,DAVV,Indore
Differences between… (cont’d)
Both
are defined in a module
are local to the module
can have local variables (registers, but not nets) and
events
contain only behavioral statements
do not contain initial or always statements
are called from initial or always statements or other
tasks or functions
Dr.Ravi Sindal,IET,DAVV,Indore
Differences between… (cont’d)
Tasks can be used for common Verilog code
Function are used when the common code
is purely combinational
executes in 0 simulation time
provides exactly one output
Functions are typically used for conversions and
commonly used calculations
Dr.Ravi Sindal,IET,DAVV,Indore