2VERILOG Functions Tasks
2VERILOG Functions Tasks
Delay, Event and Timing Control statements are not allowed inside
function.
Declaration : Style1
function [return_type] function_name;
//input declarations;
//local registers declaration;
begin //begin-end if multiple statements
sequential statements;
end
endfunction
Return type and all inputs are 1-bit reg by default.
Return variable name is same as function_name.
Only Blocking sequential statements are allowed.
Declaration : Style2
function [return_type] function_name (input_declarations);
//local registers declaration;
begin
sequential statements;
end
endfunction
Usage:
assign net_name = function_name (input_list);
reg_name1 = function_name (input_list);
reg_name2 <= function_name (input_list);
Problem Statement : Write a function which accepts 8 bit input din and
returns parity. Use for loop instead of reduction operator.
function [63:0] fact (input [31:0] din); module fact_test (din, dout);
begin input [31:0] din;
if (din<=1) output reg [63:0] dout;
fact=1;
else //function declaration
fact= fact(din -1) * din ; always @ (*)
end dout=fact(din);
endfunction
endmodule
2015 © Centre for Development of Advanced Computing Verilog
Automatic functions
Assume that user gave 4 as an input to the fact_test function. What will
be the value of the dout.
Expected: 24 Outcome: 1
Function
4 3 2 1
Input
Function
fact (3) *4 fact (2) *3 fact (1) *2 1
Return
Since automatic keyword is added to the function, for every function call
a new set of memory is created and the result is returned to location
where the function call occurs.
Function
4 3 2 1
Input
Function
4 * fact (3) 3* fact (2) 2* fact (1) 1
Return
4*3*2* 1 3*2* 1 2* 1 1
endmodule
2015 © Centre for Development of Advanced Computing Verilog
Functions
always @ (*)
b=adder(din);
endmodule
2015 © Centre for Development of Advanced Computing Verilog
Constant function
Tasks can pass multiple values through output and inout ports.
Declaration : Style1
task task_name;
//input, output or inout declarations;
//local registers declaration;
begin //begin-end if multiple statements
[timing_control] sequential statements;
end
endtask
Declaration : Style2
task task_name (input, output or inout declarations);
// local registers declaration;
begin
[timing_control] sequential statements;
end
endtask
Usage:
task_name (input, output or inout list);
Functions Tasks
Functions are used to return single value Tasks can be provide multiple values
Function musts have at least one input Tasks may have zero or more arguments
argument of type input, output or inout
Timing control statements are not allowed Timing control statements are allowed
Always executes in zero simulation time May not execute in zero simulation time
Both Blocking and Non Blocking
Only Blocking statements are allowed
statements are allowed
Functions can invoke other functions only Tasks can invoke other functions and tasks
If both defparam and #() are used, the value of defparam will
be used for overriding.
parameter size=0;
input [size-1: 0] a, b;
input cin;
output [size-1: 0] sum;
output carry;
endmodule
module custom_and
#(parameter size=0)
(input [size-1: 0] a, b, output [size-1: 0] op_and);
endmodule
defparam instance_label.parameter_name1=value1;
defparam instance_label.parameter_name2=value2;
module_name #(.parameter_name1(value1),
.parameter_name1(value2))
label (port mapping);
module adder_test;
defparam m0.size=4;
reg cin; reg [m0.size-1: 0] a, b;
wire [m0.size-1: 0] sum; wire carry;
always
begin
a=$random; b=$random; c=$random; #10;
end
endmodule
2015 © Centre for Development of Advanced Computing Verilog
Overriding Parameter
module and_test ;
reg [m0.size – 1 : 0] a, b;
wire [m0.size -1 : 0] op_and;
custom_and #(6) m0 (a, b, op_and);
always
begin
a=$random; b=$random; #10;
end
endmodule