Skip to main content

SystemVerilog Tasks and Functions

· loading · loading · · ·
HDL SystemVerilog HDL
Axolot Logic
Author
Axolot Logic
Digital Design Engineer
Table of Contents
SystemVerilog - This article is part of a series.
Part 7: This Article

πŸ”§ SystemVerilog Tasks and Functions
#

SystemVerilog provides tasks and functions to help you write reusable blocks of code. These are used extensively in both RTL modeling and testbench development for calculations, procedural modeling, stimulus generation, and communication between components.


πŸ“Œ Overview
#

Feature Function Task
Returns Single value None (void) or multiple via outputs
Time Zero simulation time May consume simulation time
Usage Pure computation Interaction with other modules, delays, file I/O
Inputs Inputs only Inputs, outputs, inouts
Call Style Called like a function Called like a procedure

🧠 When to Use
#

Use Case Use Function? Use Task?
Pure computation (math) βœ…
Delays (e.g., #5, @posedge) βœ…
File operations, $display, $fopen βœ…
Return multiple values βœ…
Assertions or comparisons βœ…
Clocking block usage βœ…

πŸ” Function Syntax
#

A function returns a single value and completes in zero simulation time.

function int add(int a, int b);
  return a + b;
endfunction

int result = add(3, 5);
  • Can’t contain #delay, @, wait, or time-consuming operations
  • Must return exactly one value
  • All inputs are passed by value

πŸ”„ Task Syntax
#

A task can do everything a function can’t β€” including delays and returning multiple outputs.

task automatic delay_and_add(input int a, b, output int sum);
  #5;
  sum = a + b;
endtask

int x;
delay_and_add(2, 3, x);
  • Can use #, @, wait, $display, etc.
  • Inputs/outputs can be passed by value, reference, or inout
  • Supports zero or more return values

πŸ“₯ Input, Output, Inout
#

Type Description
input Read-only inside task/function
output Assigned inside task and returned
inout Bi-directional; use with care
task operate(input int a, inout int b, output int result);
  b += a;
  result = a * b;
endtask

🧠 automatic vs static
#

By default, tasks and functions in Verilog are static β€” shared among calls. In SystemVerilog, you can use automatic to allocate separate stack frames per call.

task automatic do_something();
  int counter = 0;
  counter++;
  $display("Counter = %0d", counter);
endtask

βœ… Always use automatic for recursive calls, reentrant tasks, or concurrent testbenches.


πŸ§ͺ Example: Function in RTL Design
#

module alu(input logic [3:0] a, b, output logic [4:0] result);

  function logic [4:0] add;
    input logic [3:0] x, y;
    add = x + y;
  endfunction

  assign result = add(a, b);

endmodule

πŸ§ͺ Example: Task in Testbench
#

task automatic print_banner(string msg);
  $display("\n=====================");
  $display("  %s", msg);
  $display("=====================\n");
endtask

initial begin
  print_banner("Simulation Started");
end

🚦 Synthesis vs Simulation
#

Feature Synthesizable Notes
Functions with only combinational logic βœ… Useful for ALUs, comparators
Tasks with delays or $display ❌ Use only in testbenches
Tasks with only logic assignments βœ… (with care) Must be static and bounded

βœ… Best Practices
#

  • Use functions for combinational, stateless logic
  • Use tasks in testbenches for procedural control and time-based actions
  • Use automatic to avoid side effects and shared state
  • Avoid tasks with delay in synthesizable modules
  • Prefer named arguments for clarity:
my_task(.a(10), .b(20), .result(my_var));

πŸ”š Summary
#

Keyword Returns Value Allows Delay Multiple Outputs Preferred In
function βœ… ❌ ❌ RTL, utility math
task ❌ (uses output) βœ… βœ… Testbenches, control

SystemVerilog tasks and functions allow you to write modular, reusable, and intent-expressive code β€” which is critical in both design and verification.


SystemVerilog - This article is part of a series.
Part 7: This Article

Related

SystemVerilog Arrays
· loading · loading
HDL SystemVerilog HDL
SystemVerilog `fork...join`
· loading · loading
HDL SystemVerilog HDL
Blocking vs Non-Blocking Assignments
· loading · loading
HDL SystemVerilog HDL
SystemVerilog Data Types
· loading · loading
HDL SystemVerilog HDL
SystemVerilog Intoduction
· loading · loading
HDL SystemVerilog HDL
SystemVerilog Loops & Control Flow Statements
· loading · loading
HDL SystemVerilog HDL