Verilog - Chapter8 - Task and Function
Verilog - Chapter8 - Task and Function
LECTURE
Subject: VERILOG
Hardware Design Language
Chapter8: Tasks and Functions
3
Tasks versus Functions in Verilog
• Procedures/Subroutines/Functions in SW
programming languages
– The same functionality, but put in many different
places
• Verilog equivalence:
– Tasks and Functions
– function and task (~ function and subroutine)
– Used in behavioral modeling
– Part of design hierarchy Hierarchical name
4
Differences between...
• Functions • Tasks
– Can enable (call) just another – Can enable (call) other tasks and
function (not task) functions
– May execute in non-zero simulation
– Execute in 0 simulation time
time
– No timing control statements – May contain any timing control
allowed ( next slide ) statements
– At lease one input, none output – May have arbitrary (none, one or
many) inputs, outputs, or inouts
– Return only a single value – Do not return any value
– Not synthesizable
– Synthesizable
5
Timing Control
• Delay #
Used to delay statement by specified amount of simulation time
always
begin
#10 clk = 1;
#10 clk = 0;
end
• Event Control @
Delay execution until event occurs
Event may be single signal/expression change
Multiple events linked by or
always @(posedge clk) always @(x or y)
begin begin
q <= d; s = x ^ y;
end c = x & y;;
end 6
Similarity
• Both
– are defined in a module
– are local to the module
– can have local variables (registers, but not nets) and events
– contain only procedural statements (NOT continuous
statements)
– do not contain initial or always statements, they are only
blocked by “begin …end”.
– are called from initial or always statements or others task
and functions
7
Task and function usage
8
Tasks
9
Tasks (cont’d)
• Task declaration and invocation
– Declaration syntax
task <task_name>;
<I/O declarations>
<variable and event declarations>
begin // if more than one statement needed
<statement(s)>
end // if begin used!
endtask
10
Tasks (cont’d)
11
Tasks (cont’d)
• I/O declaration in modules vs. tasks
– Both used keywords: input, output, inout
– In modules, represent ports
• connect to external signals
– In tasks, represent arguments
• pass values to and from the task
12
Task Examples:
Use of input and output arguments
module operation; task bitwise_oper;
parameter delay = 10;
reg [15:0] A, B; output [15:0] ab_and, ab_or,
reg [15:0] AB_AND, AB_OR, AB_XOR; ab_xor;
input [15:0] a, b;
initial
$monitor( …);
begin
#delay ab_and = a & b;
initial ab_or = a | b;
begin
… ab_xor = a ^ b;
end end
endtask
always @(A or B)
begin
bitwise_oper(AB_AND, AB_OR,
AB_XOR, A, B);
end endmodule
13
Task Examples :
Use of module local variables
task init_sequence;
module sequence;
begin
reg clock;
clock = 1'b0;
end
initial endtask
begin
… task asymmetric_sequence;
end begin
#12 clock = 1'b0;
initial #5 clock = 1'b1;
#3 clock = 1'b0;
init_sequence;
#10 clock = 1'b1;
end
always endtask
asymmetric_sequence;
endmodule
14
Functions
• Keyword: function, endfunction
• Can be used if the procedure
– does not have any timing control constructs
– returns exactly a single value
– does not have any output
– has at least one input argument
15
Functions (cont’d)
• Function Declaration and Invocation
– Declaration syntax:
16
Functions (cont’d)
• Function Declaration and Invocation
– Invocation syntax (call):
<func_name> (<argument(s)>);
17
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
18
Function Examples: Parity Generator
always @(addr)begin
left_addr =shift(addr, `LEFT_SHIFT);
right_addr =shift(addr,`RIGHT_SHIFT);
end 20
Tasks and Functions Summary
21