Chapter 6 - Verilog Tasks and Functions
Chapter 6 - Verilog Tasks and Functions
Definition of Functions
Definition of Tasks
Functions can not include timing delays, like @, # delay, wait which
means that functions should be executed in "zero" time delay.
Functions can have any number of inputs but can not have output or
inout argument.
UIT Circuit Design with HDL - Chapter 6 5
Functions
The variables declared within the function are local to that function.
- The order of declaration within the function defines how the variables
passed to the function by the caller are used.
Functions can take, drive, and source global variables, when no local
variables are used.
When local variables are used, basically output is assigned only at the
end of function execution.
Functions can call other functions, but can not call tasks.
<variable_declaration(s)>
<statements>
end
endfunction
func_name (<argument(s)>);
The same function can be used more than once within the
same module.
Source: http://www.asic-world.com/verilog/task_func1.html#Task
UIT Circuit Design with HDL - Chapter 6 13
Function rules - remember
A function:
shall not contain any time-controlled statements (#,@, or wait);
shall not enable tasks;
shall contain at least one input argument
shall not have any argument declared as output or inout
shall not have any nonblocking assignments or procedural
continuous assignments;
shall not have any event triggers
Source: http://www.asic-world.com/verilog/task_func1.html#Task
Source: http://www.asic-world.com/verilog/task_func1.html#Task
UIT Circuit Design with HDL - Chapter 6 16
Tasks
Tasks can take, drive and source global variables, when no
local variables are used.
- When local variables are used, basically output is assigned
only at the end of task execution.
Tasks can call another task or function.
Tasks can be used for modeling both combinational and
sequential logic.
A Task must be specifically called with a statement, it cannot
be used within an expression as a function can.
Source: http://www.asic-world.com/verilog/task_func1.html#Task
UIT Circuit Design with HDL - Chapter 6 17
Tasks
Keywords: task, endtask
<statement(s)>
end // if begin used!
endtask
module task_global();
task convert;
begin
temp_out = (9/5) *( temp_in + 32);
end
endtask
endmodule
Source: http://www.asic-world.com/verilog/task_func1.html#Task
UIT Circuit Design with HDL - Chapter 6 22
Task example -
Usage of Module Local/Task Global Variables
module sequence; task init_sequence;
reg clock; clock = 1'b0;
endtask
initial
begin task asymmetric_sequence;
<main> begin
end #12 clock = 1'b0;
#5 clock = 1'b1;
initial #3 clock = 1'b0;
init_sequence; #10 clock = 1'b1;
end
always endtask
asymmetric_sequence;
endmodule
Source: http://www.asic-world.com/verilog/task_func1.html#Task
UIT Circuit Design with HDL - Chapter 6 27
Task example 2 (cont.) -
CPU Read Task file (separate file)
task cpu_read;
input [7:0] address;
output [7:0] data;
begin
$display ("%g CPU Read task with address : %h", $time, address);
$display ("%g -> Driving CE, RD and ADDRESS on to bus", $time);
@ (posedge clk);
addr = address;
ce = 1;
rd = 1;
@ (negedge clk);
data = data_rd;
@ (posedge clk);
addr = 0;
ce = 0;
rd = 0;
$display ("%g CPU Read data : %h", $time, data);
$display ("======================");
end
endtask
Source: http://www.asic-world.com/verilog/task_func1.html#Task
UIT Circuit Design with HDL - Chapter 6 28
Task example 2 (cont.) -- -
Module memory model (DUT)
module mem_model (data_rd, data_wr, addr, rd, wr, ce);
output reg [7:0] data_rd;
input [7:0] data_wr;
input [7:0] addr;
input rd, wr, ce;
//==================================
reg [7:0] mem [0:255];
They are considered part of the Verilog HDL. These system tasks and
functions are divided into some categories as follows:
- Display tasks : $display, $monitor, $strobe, $writ, $dumpfile,
$dumpvars…
- File I/O tasks : $fclose, $fdisplay, $swrite, $fread, $sdf_annotate,
$readmemb, $readmemh…
- Simulation control tasks: $finish, $stop
- Math functions: $ln, $log10, $exp, $sqrt, $sin, $cos, $asin, $acos…
(many more in Verilog IEEE standard…)