Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

Functions & Tasks: Tasks Can Consume Time

SystemVerilog Advanced Features

Runnable example from the SystemVerilog Functions & Tasks tutorial (Tasks Can Consume Time)

Open in IDE
testbench.sv
module tb;
  logic clk = 0;
  always #5 clk = ~clk;        // 10ns period clock

  logic [7:0] bus = 8'hA5;

  // Waits for "cycles" clock edges, then samples bus through an output port
  task automatic wait_cycles_and_read(input int cycles, output logic [7:0] data);
    repeat (cycles) @(posedge clk);
    data = bus;
  endtask

  initial begin
    logic [7:0] captured;

    $display("t=%0t : calling task", $time);
    wait_cycles_and_read(3, captured);
    $display("t=%0t : captured = 0x%0h", $time, captured);
  end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.