RTL Design / Module 3: Verilog from Scratch
Lesson 3.4 ยท Lab

Blocking vs non-blocking โ€” the #1 beginner bug

Blocking (=) executes immediately โ€” the next line sees the new value. Non-blocking (<=) schedules updates for the end of the time step โ€” all RHS values are sampled first. Mix them in a clocked block and you get races, broken swaps, and silicon that doesn't match simulation.

The law

Combinational always @(*) โ†’ use =. Clocked always @(posedge clk) โ†’ use <=. No exceptions in synthesizable RTL.

1

How the simulator executes them

Blocking vs non-blocking timing

Blocking is sequential within the time step. Non-blocking samples all inputs, then commits all outputs.

OperatorNameWhen RHS is readWhen LHS updatesUse in
=BlockingImmediately before each lineImmediately โ€” next line sees italways @(*)
<=Non-blockingStart of time step (all at once)End of NBA region (all at once)always @(posedge clk)
Analogy

Non-blocking is a group photo โ€” everyone poses (sample) at the same moment, then the shutter fires (update) once. Blocking is a relay race โ€” each runner must finish before the next starts. Flops update in parallel, not in sequence.

2

LAB: Swap two registers โ€” wrong vs right

Goal: exchange values of a and b on each clock edge. This is the classic interview/debug lab.

swap_WRONG_blocking.v
// WRONG โ€” blocking in clocked always
reg [7:0] a, b;
always @(posedge clk) begin
  a = b;   // a gets b's value immediately
  b = a;   // b gets a's NEW value (= b) โ€” no swap!
end
// Result: both become the old b. a is lost.
swap_CORRECT_nonblocking.v
// CORRECT โ€” non-blocking samples old values first
reg [7:0] a, b;
always @(posedge clk) begin
  a <= b;   // schedule: a <- old_b
  b <= a;   // schedule: b <- old_a
end
// At NBA: a=old_b, b=old_a โ€” true swap!

Swap lab waveforms (a starts 0xAA, b starts 0x55)

โ–ถ Run swap lab in Simulator
Trap

Using a temp variable with blocking can work in sim but is not the idiomatic pattern and may confuse synthesis. Stick to <= for all clocked register updates.

3

LAB: Pipeline shift register

A 3-stage pipeline shifts data through registers each clock. Non-blocking naturally models parallel flop updates.

pipeline_shift.v
module pipeline_shift (
  input  wire        clk,
  input  wire        rst_n,
  input  wire [7:0]  din,
  output wire [7:0]  dout
);
  reg [7:0] stage1, stage2, stage3;

  always @(posedge clk or negedge rst_n) begin
    if (!rst_n) begin
      stage1 <= 8'd0;
      stage2 <= 8'd0;
      stage3 <= 8'd0;
    end else begin
      stage1 <= din;      // new data enters
      stage2 <= stage1;   // all RHS are OLD values
      stage3 <= stage2;
    end
  end

  assign dout = stage3;
endmodule
pipeline_shift_WRONG.v
// WRONG โ€” blocking collapses pipeline in one cycle!
always @(posedge clk) begin
  stage1 = din;
  stage2 = stage1;  // stage1 already updated!
  stage3 = stage2;  // all get din in ONE cycle
end
// Hardware: 1 register. Simulation: matches (but wrong intent).
Pipeline shift register

Non-blocking preserves one-cycle-per-stage behavior. Blocking bypasses intermediate stages.

Pipeline waves (din = 0x01, 0x02, 0x03). CORRECT lags 3 cycles. WRONG reaches dout same cycle.

4

Race condition example

When blocking assignments create dependencies within a clocked block, simulation order determines results โ€” but hardware has no such order.

race_example.v
reg [3:0] count;
always @(posedge clk) begin
  count = count + 1;   // blocking: reads NEW or OLD count?
                       // Answer: reads value AFTER previous
                       // line in SAME block โ€” but there's only one line
                       // so it reads pre-update count... usually.
                       // Add another line and it breaks:
  if (count == 4'd5)
    count = 4'd0;      // order matters! race with line above
end

// SAFE version:
always @(posedge clk) begin
  if (count == 4'd5)
    count <= 4'd0;
  else
    count <= count + 1;
end

Why it matters: Synthesis tools may reorder or optimize logic differently than your simulation execution order. Non-blocking eliminates this class of bugs by design.

5

Complete rules table

ContextOperatorReason
always @(*) combinational=Outputs must reflect inputs immediately; no clock edge
always @(posedge clk) sequential<=Models parallel flop updates on clock edge
Testbench initial blocks=Procedural stimulus โ€” no hardware mapping
Multiple reg updates same cycle<=All see old values โ€” swap, pipeline, FSM
Single temp in combo always=Intermediate values within combo block
assign statementsN/A (no = or <=)Continuous drive โ€” always "parallel"
โœ…

Do

Use <= for every assignment in clocked always. Use = for every assignment in combo always. Be consistent โ€” no mixing in one block.

โŒ

Don't

Don't use = in always @(posedge clk). Don't use <= in always @(*) (works in sim, wrong style, confuses tools).

6

LAB: Full testbench to verify

swap_tb.v
module swap_tb;
  reg        clk, rst_n;
  reg  [7:0] a, b;
  wire [7:0] a_out, b_out;

  // Instantiate CORRECT swap module
  swap_correct dut (.clk(clk), .rst_n(rst_n), .a(a), .b(b));

  initial clk = 0;
  always #5 clk = ~clk;

  initial begin
    rst_n = 0; a = 8'hAA; b = 8'h55;
    #12 rst_n = 1;
    #10;  // one swap cycle
    if (a !== 8'h55 || b !== 8'hAA)
      $error("Swap failed: a=%h b=%h", a, b);
    else
      $display("PASS: swap OK a=%h b=%h", a, b);
    $finish;
  end
endmodule

module swap_correct (
  input wire clk, rst_n,
  output reg [7:0] a, b
);
  initial begin a = 8'hAA; b = 8'h55; end
  always @(posedge clk or negedge rst_n) begin
    if (!rst_n) begin a <= 8'hAA; b <= 8'h55; end
    else begin a <= b; b <= a; end
  end
endmodule
โ–ถ Run full TB in Simulator
Debug workflow

When waves don't match expectations: (1) check blocking vs non-blocking, (2) check sensitivity list, (3) check reset polarity. This fixes >50% of beginner bugs.

7

Reading waveforms for NBA bugs

In GTKWave or your simulator, look for these signatures:

  • Signal skips expected value: blocking read an already-updated sibling register
  • Pipeline has zero latency: blocking chained assignments in one clocked block
  • Glitch then settle: sometimes blocking "works" in sim but fails on silicon corner cases
  • Delta-cycle activity: multiple updates within one timestamp โ€” sign of blocking in seq logic
GTKWave debug checklist: 1. Add clk, rst_n, and all regs in the always block 2. Zoom to posedge โ€” values should change ONCE per edge 3. If reg changes before posedge settles โ†’ suspect blocking 4. Compare RTL line numbers with wave cursor โ€” trace the always block
Trap

Mixing = and <= in the same clocked always block. Some simulators allow it; synthesis may warn; behavior is unpredictable. Pick one operator per block.

8

SystemVerilog enforcement

SystemVerilog's always_ff and always_comb encode these rules into the language:

SV constructEnforcesAssignment
always_combNo clocks, complete sensitivity= only (linted)
always_ffClock/reset only in sensitivity<= only (linted)
  • I can explain why swap needs <=
  • I can build a 3-stage pipeline with correct latency
  • I know = in combo, <= in clocked โ€” always
  • I can spot pipeline-collapse bugs in waves
  • I never mix = and <= in one clocked block

Up next โ€” Lesson 3.5: Operators & vectors โ€” arithmetic, shifts, concatenation, and width rules that interact with everything you've learned.

๐ŸŽฏ What to remember

  • = blocking: immediate, sequential โ€” for always @(*) only.
  • <= non-blocking: sample all, update all โ€” for always @(posedge clk).
  • Swap and pipeline labs prove why โ€” blocking collapses parallel updates.
  • This is the #1 beginner bug; check it first in every debug session.
  • Use always_ff/always_comb in SV to make violations compile errors.
Advertisement