Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 37 ยท Sequential Logic

Blocking vs Non-Blocking Assignments in Verilog

Understand Verilog's two procedural assignments: how = and <= are scheduled, why flip-flops need non-blocking, the shift-register trap, race conditions and the golden coding rules.

Verilog has two procedural assignment operators, = (blocking) and <= (non-blocking), and mixing them up is the most famous source of bugs for new hardware designers. The rule of thumb is simple, and this lesson explains why it is simple, using the way a simulator schedules events.

The two operators

VERILOG
a = b;     // blocking: the update happens immediately, the next statement sees it
a <= b;    // non-blocking: the update is scheduled for the end of the time step
  • With blocking assignments, statements in a block execute in order, each finishing before the next starts, like ordinary software.
  • With non-blocking assignments, the right-hand sides are all evaluated first, and all left-hand sides are updated together afterward. This mimics flip-flops sampling their inputs at the same clock edge and then changing together.

The scheduling picture

In one simulation time step the simulator works in regions. The essential ones for you:

  1. Active region: evaluates the right-hand sides, executes blocking assignments, and schedules non-blocking updates.
  2. NBA region (non-blocking assign update): applies all the scheduled non-blocking updates.
  3. Any events caused by those updates run next (same time step), and the cycle repeats until nothing changes.
flowchart LR
  A["Clock edge<br/>always blocks wake"] --> B["Evaluate every right-hand side"]
  B --> C["Blocking = updates now"]
  B --> D["Non-blocking <= scheduled"]
  D --> E["NBA region: apply all updates together"]
  E --> F["Combinational logic reacts"]

Non-blocking updates wait until all clocked blocks have read their inputs.

The shift-register trap

A three-stage shift register shows the difference.

VERILOG
// Non-blocking: three flip-flops in a chain (correct)
always @(posedge clk) begin
    q1 <= d;
    q2 <= q1;
    q3 <= q2;
end

// Blocking: q1, q2 and q3 all end up equal to d after ONE edge (wrong here)
always @(posedge clk) begin
    q1 = d;
    q2 = q1;
    q3 = q2;
end

In the non-blocking version, every right-hand side reads the old values, so data moves one stage per clock. In the blocking version, q1 changes before q2 = q1 runs, so the value rushes through all three in a single edge, giving one flip-flop's worth of behavior instead of three. Synthesis also builds hardware that differs from the simulation of the wrong code in subtle ways.

Swapping values

Non-blocking assignment makes swaps trivial, because both sides read the old values:

VERILOG
always @(posedge clk) begin
    a <= b;
    b <= a;      // a and b swap on each clock edge
end

With blocking assignments the same code would copy b into both.

The golden rules

  1. Clocked (sequential) blocks: use non-blocking <= for everything that becomes a flip-flop.
  2. Combinational blocks (always @*): use blocking =.
  3. Never mix both styles in the same block for the same variable.
  4. Never assign the same variable from more than one always block.

Why the combinational rule? In always @*, you want each statement to see the result of the previous one, like a chain of gates being evaluated in order.

VERILOG
always @* begin
    t = a & b;       // blocking: t is available on the next line
    y = t | c;
end

If you wrote t <= a & b; there, y would be computed from the old t in the same pass; the block would re-run when t updates, so the final result is right but simulation may glitch and lint tools complain.

Race conditions

A race occurs when the result depends on the arbitrary order in which the simulator runs two blocks. Classic example, a testbench that uses blocking assignment to drive an input at the same edge the design samples it:

VERILOG
always @(posedge clk) q <= d;          // design samples d
always @(posedge clk) d = d + 1;       // BAD: blocking, updates immediately

Whether q sees the old or new d depends on which block the simulator happens to run first. The cure is to drive stimulus with non-blocking assignments or to change inputs away from the active edge (for example just after it with a small delay, or on the falling edge).

Intra-assignment delays

VERILOG
q <= #2 d;     // evaluate d now, update q after 2 time units

This models clock-to-q delay in simulation and avoids races in some testbenches. It is ignored by synthesis.

Stand-in: two update orders in C

C is naturally "blocking". To emulate non-blocking behavior you evaluate all right-hand sides first and then assign, which is exactly what the NBA region does. This stand-in (not Verilog) runs the 3-stage shift register both ways with the input sequence 1, 0, 1.

C Stand-in: blocking vs non-blocking shift register (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

In the blocking columns all three values equal d immediately; in the non-blocking columns the 1 marches one position per clock, as real flip-flops do.

Stand-in: the swap (C)

C Stand-in: swapping with deferred updates (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Checklist before you write an always block

Question Answer
Is it clocked (posedge clk)? Use <=
Is it always @* or always_comb? Use =
Does a signal have more than one driver block? Fix it: one block per signal
Do you assign every output in a combinational block? Defaults first

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which assignment should a clocked always block use for flip-flops?

    Non-blocking assignments let every flip-flop sample its inputs before any of them changes.
  2. 2. In the blocking shift register, what do q1, q2 and q3 equal after one clock edge?

    Each blocking assignment updates immediately, so d rushes through all three variables in one edge.
  3. 3. Which assignment style belongs in always @*?

    Combinational blocks use blocking assignments so statements see each other's results in order.
  4. 4. What is a simulation race?

    Two blocks reading and writing the same signal at the same time without proper ordering can give different results on different simulators.

Summary

  • Blocking = updates immediately; non-blocking <= samples right-hand sides now and updates at the end of the time step.
  • Use <= in clocked blocks and = in combinational blocks; never mix them or drive a variable from two blocks.
  • Non-blocking assignments make shift registers and swaps behave like real flip-flops.
  • Races come from blocking assignments on signals shared across blocks at the same edge.

Next: Resets: Synchronous and Asynchronous