When to use = and when to use <= in Verilog, why mixing them breaks a design, and a step-by-step animation of two registers swapping.
By BitFlow · · 7 min read
The rule, first
In a clocked block (always @(posedge clk)), use nonblocking assignments: q <= d;
In a combinational block (always @(*)), use blocking assignments: y = a & b;
Do not mix the two styles in one block, and do not drive the same variable from two blocks.
The rest of this page is why that rule exists. It comes from how Verilog simulators schedule assignments, and from what you want the hardware to do.
What each one does
Blocking =
Nonblocking <=
When the value changes
Immediately, before the next line runs
At the end of the current time step, after every right-hand side has been read
Reads see
The updated value
The old value
Models
Wires and logic: results follow straight from inputs
Flip-flops: every register samples at the same clock edge
A blocking assignment finishes before the next statement starts, so statements in a block behave like a program. A nonblocking assignment says "this variable will take this value when the time step ends". All the right-hand sides in the step are evaluated first, and only then do the updates land together. That is exactly how flip-flops on one clock behave.
The swap that shows the difference
Start with a = 1 and b = 2 and ask for the two values to swap at a clock edge. Step through both versions.
> Loading the animation…
// Swaps: the right-hand sides are read before either update lands
always @(posedge clk) begin
a <= b;
b <= a;
end
// Does not swap: a is already b by the time b reads it
always @(posedge clk) begin
a = b;
b = a;
end
With nonblocking, both sides are read first, so a gets 2 and b gets 1. With blocking, a = b changes a at once, and b = a then reads the new a: both end up 2. Real flip-flops behave like the first version, so use <= for registers.
A register, line by line
This animation steps a register with a synchronous reset. Watch which lines run, and notice that q only changes when clk rises, even while d changes in between.
> Loading the animation…
A classic mistake: the shift register
// Intended: a 3-stage shift register
always @(posedge clk) begin
q1 = d; // blocking
q2 = q1;
q3 = q2;
end
Because each line sees the new value from the line above, d runs through all three stages in one clock edge, and the design is a single register in disguise. With <= each stage takes the old value of the previous one, and the data moves one stage per clock, as intended.
Why blocking in combinational blocks
always @(*) begin
t = a & b; // blocking: t is ready for the next line
y = t | c;
end
In combinational logic you want a later line to see the result of an earlier one, the same as a chain of gates. Blocking assignments do that. Using <= here can make a simulation read stale values and disagree with the synthesised hardware.
Watch outAlso give every output a value on every path through a combinational block (include the else branch and a default in case statements). Otherwise the tool infers a latch, which is rarely what you meant.
Questions
Which assignment should I use in an always @(posedge clk) block?+
Nonblocking (<=). It makes every register sample the old values at the clock edge and update together, which is how flip-flops behave.
Which assignment should I use in an always @(*) block?+
Blocking (=). Combinational logic should let a later statement see the result of an earlier one, like a chain of gates.
What happens if I mix blocking and nonblocking in the same block?+
Simulation can disagree with the hardware that gets synthesised, and the results can depend on the order of statements. Keep one style per block.