Blocking vs Non-Blocking Assignments in Verilog: The Rule That Prevents Race Conditions
Why = and <= behave differently in Verilog, how the simulator's event queue explains it, and the two coding rules that keep simulation and synthesis in agreement.
Almost every Verilog beginner writes a shift register with = instead of <=, sees it pass in one simulator and fail in another, and learns the hard way that these two operators are not interchangeable. This article explains what each one really does and gives you two rules that remove the problem for good.
The two operators
- Blocking (
=): the right-hand side is evaluated and the left-hand side is updated immediately, before the next statement runs. Statements execute in order, like software. - Non-blocking (
<=): the right-hand side is evaluated immediately, but the update is scheduled for later in the same time step. Every non-blocking assignment in the block sees the old values.
A shift register, written both ways
// Intended: a 3-stage shift register d -> q1 -> q2 -> q3
// WRONG: blocking assignments
always @(posedge clk) begin
q1 = d;
q2 = q1; // q1 already holds d here
q3 = q2; // q2 already holds d here
end
Because each line updates immediately, d races through all three registers on a single clock edge. Instead of a 3-cycle delay you get all three outputs loading d at once, and synthesis typically merges them into one flip-flop. The pipeline you meant to build is gone.
// RIGHT: non-blocking assignments
always @(posedge clk) begin
q1 <= d;
q2 <= q1; // reads the OLD q1
q3 <= q2; // reads the OLD q2
end
Every right-hand side is sampled first, then all registers update together. This is exactly how real flip-flops behave: they all capture on the same edge, using the values that existed just before it.
Why it works: the event queue
The Verilog standard (IEEE 1364, carried into IEEE 1800 SystemVerilog) divides each simulation time step into regions. The two that matter here are:
| Region | What happens |
|---|---|
| Active | Blocking assignments, continuous assignments, evaluation of the RHS of non-blocking assignments |
| NBA (non-blocking assign) update | Left-hand sides of non-blocking assignments are updated |
The simulator finishes all active events before it touches the NBA region. So when several always @(posedge clk) blocks trigger on the same edge, they all read register values before any of them change. The result no longer depends on which block the simulator happens to run first, and that removes the race.
With blocking assignments in multiple clocked blocks, the order in which the simulator runs those blocks is not defined by the standard. Two simulators, or even two versions of one simulator, can legally produce different results.
The two rules
- Use non-blocking (
<=) in sequential logic: anyalways @(posedge clk)oralways_ffblock. - Use blocking (
=) in combinational logic:always @(*)oralways_combblocks.
And a corollary: never mix = and <= on the same variable, and never assign the same variable from more than one always block.
Why blocking is right for combinational logic
Inside a combinational block you want software-like ordering, because intermediate values feed later statements:
always @(*) begin
sum = a ^ b;
carry = a & b;
out = sum ^ cin; // uses the new sum, as intended
end
With <=, the last line would read the old sum. The block then re-triggers when sum changes and eventually settles, so it only works by accident, costs extra simulation passes, and makes the code misleading to read.
Common mistake: the incomplete combinational block
A related bug is forgetting to assign an output on every path of a combinational block:
always @(*) begin
if (sel) y = a; // no else: y must "remember" its value
end
Synthesis infers a latch to hold y when sel is 0. Fix it with a default assignment at the top of the block (y = b;) or a complete if/else. In SystemVerilog, always_comb makes tools warn about this.
Key takeaways
=updates immediately,<=updates at the end of the time step.- Sequential logic gets
<=, combinational logic gets=. - Following these two rules makes your RTL simulate the same way the synthesised hardware behaves, which is the whole point of RTL.
Chip Weekly · free
Get the week's chip news and a free RTL interview sample
One short email a week with the top semiconductor and VLSI news and our newest lessons. Sign up and download 4 pages from our RTL Design interview kit right away.