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

while and do-while loop: Using while in Verification: Polling with a Timeout

SystemVerilog Control Flow & Loops

Runnable example from the SystemVerilog while and do-while loop tutorial (Using while in Verification: Polling with a Timeout)

Open in IDE
testbench.sv
module tb;
  bit clk, ready;
  int cycles = 0;

  always #5 clk = ~clk;              // rising edges at 5, 15, 25 ... ns
  initial #42 ready = 1;             // stands in for the DUT raising ready

  initial begin
    // Check once per clock; give up after 100 cycles
    while (!ready && cycles < 100) begin
      @(posedge clk);                // advances time, so ready can change
      cycles++;
    end

    if (ready) $display ("[%0t] ready after %0d cycles", $time, cycles);  // [45] ready after 5 cycles
    else       $display ("[%0t] TIMEOUT waiting for ready", $time);
    $finish;
  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.