Professional Documents
Culture Documents
A designer is frequently required to implement the same functionality at many places in a behavioral design. This means that the commonly used parts should be abstracted into routines and the routines must be invoked instead of repeating the code. Verilog provides tasks and functions to break up large behavioral designs into smaller pieces.
2014 Tasks and Functions 2
Differences between...
Functions
Can enable (call) just another function (not task) Execute in 0 simulation time No timing control statements allowed At least one input
Return only a single value
2014 Tasks and Functions
Tasks
Can enable other tasks and functions May execute in non-zero simulation time May contain any timing control statements May have arbitrary input, output, or inout Do not return any value
3
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
2014
Tasks can be used for common Verilog code Tasks are capable of enabling a function as well as enabling other versions of a Task 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
2014 Tasks and Functions 5
Tasks
Keywords: task, endtask
Must be used if the procedure has
any timing control constructs zero or more than one output arguments no input arguments
2014
2014
input and inout arguments are passed into the task output and inout arguments are passed back to the invoking statement when task is completed
2014 Tasks and Functions 8
2014
task bitwise_oper; output [15:0] ab_and, ab_or, ab_xor; input [15:0] a, b; begin #delay ab_and = a & b; ab_or = a | b; ab_xor = a ^ b; end endtask
endmodule
module traffic_lights; reg clock, red, amber, green; parameter on = 1, off = 0, red_tics = 350, amber_tics = 30, green_tics = 200; initial red = off; initial amber = off; initial green = off;
task light; output color; input [31:0] tics; always begin // sequence to control the lights. begin red = on; // turn red light on repeat (tics) @ (posedge clock); light(red, red_tics); // and wait. color = off; // turn light off. green = on; // turn green light on end light(green, green_tics); // and wait. endtask amber = on; // turn amber light on
light(amber, amber_tics); // and wait. end // task to wait for tics positive edge clocks // before turning color light off.
always begin // waveform for the clock. #100 clock = 0; #100 clock = 1; end endmodule // traffic_lights.
Tasks and Functions 12
2014
syntax
task automatic<task_name>; <I/O declarations> <variable and event declarations> begin // if more than one statement needed <statement(s)> end // if begin used! endtask
2014 Tasks and Functions 13
14
2014
15
Functions
Keyword: function, endfunction
2014
16
2014
18
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
2014 Tasks and Functions 19
module function_calling(a, b,c); input a, b ; output c; wire c; function myfunction; input a, b; begin myfunction = (a+b); end endfunction assign c = myfunction (a,b);
endmodule
2014 Tasks and Functions 20
2014
21
always @(addr) begin left_addr =shift(addr, `LEFT_SHIFT); right_addr =shift(addr,`RIGHT_SHIFT); 2014 Tasks and Functions end
22
2014
23
// Define the function function automatic integer factorial; input [31:0] oper; begin if (oper >= 2) factorial = factorial (oper -1) * oper; //recursive call else factorial = 1 ; end endfunction // Call the function integer result; initial begin result = factorial(4); // Call the factorial of 4 $display("Factorial of 4 is %d", result); //Displays 24 end endmodule
2014 Tasks and Functions 24
Constant Functions:
A constant function is a regular Verilog HDL function, can be used instead of constants. module ram (...); parameter RAM_DEPTH = 256; input [clogb2(RAM_DEPTH)-1:0] addr_bus; //width of bus computed //by calling constant //function defined below //Result of clogb2 = 8 //input [7:0] addr_bus; //Constant function function integer clogb2(input integer depth); begin for(clogb2=0; depth >0; clogb2=clogb2+1) depth = depth >> 1; end endfunction endmodule 2014 Tasks and Functions
25
Signed Functions:
Signed functions allow signed operations to be performed on the function return values.
module top; -//Signed function declaration //Returns a 64 bit signed value function signed [63:0] compute_signed(input [63:0] vector); -- -endfunction -//Call to the signed function from the higher module if(compute_signed(vector) < -3) begin -end -2014 Tasks and Functions endmodule
26