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
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.