Draft preview. Lessons are being written and reviewed.

Digital Design / Module 3: Verilog that means hardware

15. Blocking and non-blocking assignments

Understand the classic swap and the scheduler behind it.

Lecture coming soon

See it move

The idea

= (blocking) takes effect immediately, so the next line sees the new value. <= (non-blocking) evaluates its right side now but updates the left side later, together with all the other non-blocking updates of that time step.

The classic swap

always @(posedge clk) begin
  a <= b;
  b <= a;
end

Both right sides are read first, then both updates happen. The values swap. This describes exactly what two flops do when each feeds the other: at the edge, both capture what was at their inputs just before it.

With = the first line destroys a before the second reads it, so both end up with the old value of b. The animation above steps through both.

The rule

  • Clocked logic: use <=.
  • Combinational logic: use =.
  • Never mix them in one block.

These rules aren't about style. They make simulation match the hardware and remove race conditions.

Why order would otherwise matter

always @(posedge clk) begin
  q1 = d;     // wrong: blocking in a clocked block
  q2 = q1;
end

q2 receives d in the same step, so a two-stage collapses into one stage. With <=, it works in any order you write the lines.

Races between blocks

Two clocked blocks using = that read each other's outputs give different results depending on which block the simulator happens to run first. Different simulators can legally disagree. Non-blocking assignments remove this race, because all the reads finish before any update.

Common mistake

Thinking that <= adds delay. It doesn't add time; it only defers the update to later in the same time step. Combinational logic can also show extra simulation activity if written with <=, so keep to the rule.

Key ideas

  • Non-blocking right-hand sides are sampled first, then updated together.
  • Use <= in clocked blocks and = in combinational blocks.
  • Mixing them creates simulation races.

Your exercise

Swap two registers without a temporary, then explain why it works.

Find the bug

This code has one deliberate bug. Read the symptom, then click the line you think is wrong.

What goes wrong: This should delay d by two clocks, but q2 changes just one clock after d.

Interview questions

coreWhat does a<=b; b<=a; do on a clock edge?

basicIn a clocked block, a = 0 and b = 1. After an edge runs a = b; b = a; with blocking assignments, what are a and b?

basicState the rule for choosing = or <=, and why it exists.

coreThis is meant to be a 3-stage shift register. What does it actually do?

always @(posedge clk) begin
  q1 = d;
  q2 = q1;
  q3 = q2;
end

coreThe same shift register written in reverse order with blocking assignments works in simulation. Is it acceptable?

always @(posedge clk) begin
  q3 = q2;
  q2 = q1;
  q1 = d;
end

coren is 5 before a clock edge. What is n after this block runs?

always @(posedge clk) begin
  n <= n + 8'd1;
  n <= n + 8'd2;
end

coreTwo clocked blocks use blocking assignments: one does a = b;, the other b = a;. What happens at the clock edge?

advancedThis combinational block uses <=. Does it still give the right yy, and what is the cost?

always @(*) begin
  t  <= a & b;
  yy <= t | c;
end

advancedA testbench changes d with always @(posedge clk) d = ~d; while the design does q <= d on the same edge. Why is this risky?

Practise these 9 questions with animations