Understanding Tasks and Functions in Verilog
Understanding Tasks and Functions in Verilog
Functions
by
Swaroop D
USN:4MH21EC104
Overview of Tasks & Functions
Tasks can execute over time, incorporating delays, Tasks do not return a value and are typically used for
blocking statements, and wait conditions. procedural operations.
Enhance-Readability
Functions:
Combinational Logic
Instantaneous Return Value
Required
Functions execute instantly,
without delays or blocking Functions must return a
statements. value, which is typically
used in assignments.
Example of Task: Delay
and Blocking
task delay_and_wait;
#100; // Delay of 100 time units
wait(ready); // Wait for signal 'ready' to be high
endtask
Example of Function:
Logic Calculation
function logic [3:0] add4bit;
input logic [3:0] a, b;
add4bit = a + b;
endfunction
SystemVerilog
Enhancements: Void
Cast
function logic [3:0] add4bit;
input logic [3:0] a, b;
add4bit = a + b;
endfunction
void main;
void'(add4bit(2, 3)); // Call function and discard
return value
endfunction
Choosing Tasks and Functions
1 Sequential Logic 2 Combinational Logic
Use a task for operations that involve time or Use a function for logic calculations and
blocking statements. instantaneous operations.
Key Takeaways
Time
Tasks can take time, while functions are instantaneous.
Return Value
Functions return values, while tasks do not.
SystemVerilog
Use void casting to call functions without using the return value.