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
whileloop synthesizes only if it is statically bounded (the condition reduces to a fixed iteration count at elaboration). A data-dependentwhileis testbench-only.
2. Mental Model — Loop While a Condition Holds; Static to Synthesize
3. The While Loop
// 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);
endThe 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
- Data-dependent
whilein synthesizable design — not synthesizable; useforwith a static bound (§2). - No progress toward the condition — an infinite loop (a simulation hang).
- Using
whilewhereforis clearer — count loops read better asfor(§3).
5. Debugging Lab
One while-loop debug post-mortem
Pitfall — a while loop deadlocks on a condition that never changes
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.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.
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.
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
whileis testbench-only. - Prefer
forfor synthesizable iteration (explicit static bound); usewhilein 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.
Related Tutorials
- For Loop — Chapter 14.6.1; the preferred synthesizable count loop.
- Loops — Chapter 14.6; the loop overview and unrolling concept.
- Level-Sensitive Timing Controls — Chapter 14.4.3;
wait, a testbench alternative to a data-dependentwhile. - Testbench Creation Techniques — Chapter 9.5; where data-dependent loops are used.
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.