Skip to content

Verilog · Chapter 14.6.2 · Behavioural Modeling

While Loop in Verilog — Condition Loops & the Static-Bound Requirement

The while loop repeats its body as long as a condition is true, making it a condition-controlled loop in contrast to the count-controlled for loop. Its synthesizability follows the same unrolling rule as every loop. A while loop synthesizes only if it is statically bounded, meaning the condition reduces to a fixed iteration count known at elaboration, such as a loop variable counting up to a constant. A while loop driven by a runtime condition, one that waits while a signal holds or iterates an input-dependent number of times, cannot be unrolled and is testbench-only. In practice, synthesizable iteration is almost always written with for, whose static bound is explicit, while while loops appear mainly in testbenches where data-dependent looping is fine. This page drills the while loop and its static-bound requirement.

Foundation9 min readVerilogwhile loopConditionTestbench

Chapter 14 · Section 14.6.2 · Behavioural Modeling

1. The Engineering Problem

A while loop repeats on a condition — and whether it synthesizes depends entirely on whether that condition is statically bounded:

A while loop synthesizes only if it is statically bounded (the condition reduces to a fixed iteration count at elaboration). A data-dependent while is testbench-only.

2. Mental Model — Loop While a Condition Holds; Static to Synthesize

3. The While Loop

while.v
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
   // statically bounded (synthesizable) — equivalent to a for loop:
   i = 0;
   while (i < WIDTH) begin           // WIDTH static → bounded
       y[i] = a[i] & b[i];
       i = i + 1;
   end
 
   // data-dependent (testbench-only) — iterate until a runtime condition:
   while (!done) begin
       drive_stimulus();
       @(posedge clk);
   end

The first while counts a loop variable to a static limit — it unrolls like a for (and is usually written as a for). The second loops on a runtime condition (!done) — testbench-only, since the iteration count depends on simulation behaviour. A while needs its body to make progress toward the condition, or it loops forever.

4. Common Mistakes

  1. Data-dependent while in synthesizable design — not synthesizable; use for with a static bound (§2).
  2. No progress toward the condition — an infinite loop (a simulation hang).
  3. Using while where for is clearer — count loops read better as for (§3).

5. Debugging Lab

One while-loop debug post-mortem

Pitfall — a while loop deadlocks on a condition that never changes
Buggy Code
task wait_for_grant;
  begin
      // Intent: hold here until the bus grant arrives, then proceed.
      while (grant == 1'b0) begin
          // ... no clock advance, no event, nothing that lets time pass ...
          checks = checks + 1;          // spins, but time never moves
      end
      do_transfer();
  end
endtask

// 'grant' is driven by another part of the design that only updates on a
// clock edge. But this while body contains NO timing control (no @(posedge
// clk), no #delay, no wait) — so the loop spins in a SINGLE simulation
// instant, never yielding control. Time cannot advance, so the clock never
// ticks, so 'grant' can never become 1. The loop condition is therefore
// permanently false: the simulation HANGS (or appears frozen at one time),
// and do_transfer() is never reached.
Symptom

The simulation stops making progress — the time stamp stops advancing, the testbench produces no further output, and the run eventually times out. There is no error message; it simply freezes. A waveform shows simulation time stuck at a single value while one task spins.

Root Cause

A while loop only re-checks its condition between iterations of its body; it does NOT let simulation time advance on its own. This loop waits for 'grant' to change, but 'grant' can only change when the clock advances — and the loop body contains no timing control to LET the clock advance. So the loop spins forever within one instant: it never yields control back to the scheduler, time is frozen, and the very event it is waiting for can never occur. A while loop whose exit depends on a signal it never gives time to change is a deadlock.

The fix is to put a timing control in the loop body so each iteration consumes time and lets the rest of the design (including the clock and 'grant') run — or, for a pure level wait, use 'wait(grant)' which suspends until the level is reached without spinning.

Fix
task wait_for_grant;
  begin
      // Option A — advance a clock each iteration so 'grant' can update:
      while (grant == 1'b0)
          @(posedge clk);               // yields time; clock ticks; grant moves
      do_transfer();
  end
endtask

// Option B — a level wait, which suspends until the condition holds:
//    wait (grant);  do_transfer();
//
// Either way the loop must let simulation time pass so the awaited signal can
// actually change. A while that polls a signal must contain a timing control;
// otherwise it deadlocks the simulation. (For a pure level condition, prefer
// wait — 14.4.3.)

6. Interview Q&A

7. Exercises

Exercise 1 — Synthesizable?

Which synthesize: (a) while (i < 8) ... with i counting up; (b) while (!ready) ... with ready a runtime signal?

Exercise 2 — Convert to for

Rewrite the statically-bounded while in §3 as a for loop.

8. Summary

The while loop repeats on a condition:

  • Synthesizable only if statically bounded (condition reduces to a fixed count); data-dependent while is testbench-only.
  • Prefer for for synthesizable iteration (explicit static bound); use while in testbenches.
  • Must make progress toward the condition, or it loops forever.

The next sub-topic is the fixed-count loop: Chapter 14.6.3 Repeat Loop drills repeat — repeating a body a fixed number of times.

Standards & specifications

Governing standard
IEEE Std 1364 (Verilog)(opens IEEE in a new tab)

Defines the Verilog language and its simulation semantics, including the event scheduling model. Synthesis support is defined by tools, not by this standard.

This page also covers RTL structure, verification approach and debugging technique. Those are engineering practice built on the standard, not requirements the standard itself imposes.

Where this fits

Part of the Verilog HDL curriculum.