Skip to content
systemcoding.dev
Hardware

Blocking vs Non-Blocking Assignments in Verilog Explained

Learn when to use = and <= in Verilog, how the simulator schedules each, and what goes wrong when you mix them in clocked or combinational blocks.

7 min read
Summarize in AI
Flat illustration of two options weighed side by side, with the C, Linux and Rust logos.

Verilog has two assignment operators inside procedural blocks, = and <=, and picking the wrong one is the most common source of simulation and synthesis mismatches for newcomers. The rule is short: use <= for clocked logic and = for combinational logic. This post explains why that rule exists, using the scheduling model from IEEE 1364-2005, and shows concrete failures when it is broken. Examples simulate under Icarus Verilog 12.0.

The two operators

A blocking assignment, written =, evaluates its right-hand side and updates the left-hand side immediately. The next statement in the same block sees the new value. This is how assignment works in C, which is why it feels natural.

A non-blocking assignment, written <=, evaluates its right-hand side now but schedules the update for the end of the current time step. Statements after it in the same block still see the old value of the target.

assign_demo.vverilog
module assign_demo;
    reg [3:0] a, b, c, d;

    initial begin
        a = 4'd1;
        b = 4'd2;

        a = b;
        c = a;

        $display("blocking:     a=%0d c=%0d", a, c);

        d <= 4'd7;
        $display("non-blocking: d=%b (before the update)", d);
        #1 $display("non-blocking: d=%0d (after the update)", d);
    end
endmodule
Text
blocking:     a=2 c=2
non-blocking: d=xxxx (before the update)
non-blocking: d=7 (after the update)

With =, a becomes 2 immediately and c = a copies the new 2. With <=, the display right after the statement still sees d unassigned, which prints as xxxx, because the update lands later in the time step. A time step is the simulator's unit of work at one instant of simulated time, and it has ordered regions: the active events run first, then the non-blocking updates are applied.

Why clocked logic needs non-blocking

Real flip-flops on a shared clock all sample their inputs at the edge, and then all change. None of them sees another's new value in that same edge. Non-blocking assignment reproduces that. Look at a swap:

swap_good.vverilog
module swap_good (
    input  wire clk,
    input  wire load,
    output reg  [3:0] x,
    output reg  [3:0] y
);
    always @(posedge clk) begin
        if (load) begin
            x <= 4'd3;
            y <= 4'd9;
        end else begin
            x <= y;
            y <= x;
        end
    end
endmodule

On each edge after loading, x takes the old y and y takes the old x, so the values trade places every cycle. Two flip-flops, each fed by the other's output, which is exactly what the hardware does. Now write the same logic with blocking assignments:

swap_bad.vverilog
module swap_bad (
    input  wire clk,
    input  wire load,
    output reg  [3:0] x,
    output reg  [3:0] y
);
    always @(posedge clk) begin
        if (load) begin
            x = 4'd3;
            y = 4'd9;
        end else begin
            x = y;
            y = x;
        end
    end
endmodule

Here x = y copies the old y into x, then y = x copies that new x, which is the old y, into y. Both end up with the same value, and the swap is lost. A testbench shows the difference:

swap_tb.vverilog
`timescale 1ns/1ps
module swap_tb;
    reg clk = 1'b0;
    reg load = 1'b1;
    wire [3:0] gx, gy, bx, by;

    swap_good g (.clk(clk), .load(load), .x(gx), .y(gy));
    swap_bad  b (.clk(clk), .load(load), .x(bx), .y(by));

    always #5 clk = ~clk;

    initial begin
        @(posedge clk); #1 load = 1'b0;
        repeat (3) begin
            @(posedge clk); #1;
            $display("good x=%0d y=%0d | bad x=%0d y=%0d", gx, gy, bx, by);
        end
        $finish;
    end
endmodule
Text
good x=9 y=3 | bad x=9 y=9
good x=3 y=9 | bad x=9 y=9
good x=9 y=3 | bad x=9 y=9

The good version swaps each cycle. The bad version collapses both registers to 9 on the first edge and stays there. The synthesis result of the bad version depends on the tool, which is the real danger: the simulation looks wrong here, but in subtler cases it looks right while the hardware does something else.

Why combinational logic needs blocking

A combinational block describes a function of its inputs, with no storage. Inside it, you often compute intermediate values and use them in later lines, which needs the blocking behaviour:

alu_flags.vverilog
module alu_flags (
    input  wire [7:0] a,
    input  wire [7:0] b,
    output reg  [8:0] sum,
    output reg        zero,
    output reg        carry
);
    always @* begin
        sum   = {1'b0, a} + {1'b0, b};
        carry = sum[8];
        zero  = (sum[7:0] == 8'd0);
    end
endmodule

Each statement uses the value computed by the one above it. If these were non-blocking, carry and zero would see the previous evaluation's sum, and the block would be sensitive to its own outputs in a confusing way. Blocking assignments match the meaning: the lines read like a small calculation. The adder uses nine-bit operands built by concatenating a zero, so that the carry-out lands in bit 8.

The rules, stated once

The Verilog community's long-standing guidelines, popularised in Clifford Cummings' paper on this topic, reduce to a short list:

  1. Use non-blocking assignments to model sequential logic, in always @(posedge clk) blocks.

  2. Use blocking assignments to model combinational logic, in always @* blocks.

  3. Do not mix blocking and non-blocking assignments in the same always block.

  4. Do not assign the same variable from more than one always block.

  5. Do not use # delays in synthesisable assignments.

Rule 4 matters because two blocks driving one reg is a multiple-driver conflict that simulates in an order-dependent way and is rejected or mis-synthesised by tools.

What the scheduler actually does

The standard divides each time step into regions. The active region holds events that evaluate blocking assignments and the right-hand sides of non-blocking ones. The non-blocking assign region comes later, and applies the scheduled updates. If those updates wake other blocks, the simulator iterates, running active events again at the same simulated time.

At a clock edge, all always @(posedge clk) blocks start in the active region. Each evaluates its right-hand sides using the values from before the edge. Only after all of them have been evaluated do the updates apply. That is why a chain of non-blocking registers acts as a shift register, and a chain of blocking ones collapses into one stage.

chain.vverilog
module chain (
    input  wire clk,
    input  wire din,
    output reg  q1,
    output reg  q2,
    output reg  q3
);
    always @(posedge clk) begin
        q1 <= din;
        q2 <= q1;
        q3 <= q2;
    end
endmodule

Three flip-flops in series, giving a three-cycle delay. With = instead, din would reach q3 in one cycle.

A race you can reproduce

Even when each block uses a single operator, you can create a race by mixing styles across blocks. If one block updates a clock-derived signal with = and another block uses it as a clock, the order of evaluation decides what the second block sees.

race.vverilog
module race (
    input  wire clk,
    output reg  q
);
    reg clk2;
    always @(posedge clk) clk2 = ~clk2;
    always @(posedge clk2) q <= ~q;
endmodule

Here clk2 is a divided clock built with a blocking assignment in the same time step as the edge that triggers it. Designers avoid this by using a clock enable, as the guide to Verilog in this cluster shows, and by using <= whenever a signal changes on a clock edge.

How to check your own code

A review habit catches most violations. Read each always block and classify it by its sensitivity list. If it contains posedge or negedge, every assignment to a signal that outlives the block, meaning anything read elsewhere, should be <=. If it is @*, every assignment should be =. Any block that fails the check is either a mistake or an unusual design that deserves a comment.

Tools help as well. Verilator, which compiles Verilog to C++, reports a BLKSEQ warning when a blocking assignment appears in a clocked block, and a COMBDLY warning when a delayed or non-blocking assignment appears in a combinational one. These are lint warnings, off by default in some configurations, and worth turning on. Yosys reports a different class of problem: a design where simulation and synthesis disagree shows up when you simulate the netlist that synthesis produced and compare it with the source simulation. A mismatch between the two is nearly always an assignment-style bug or a missing default.

Temporary variables inside a clocked block

One legitimate use of = in a clocked block is a local temporary that never leaves the block. For example, computing an intermediate value to avoid repeating an expression. Even there, many teams forbid it, because the rule "everything in a clocked block is <=" is easier to enforce than a rule with exceptions. If you need a temporary, declare it as a wire with assign outside the block, or compute it in a separate combinational block, and the clocked block stays pure.

What synthesis does with each style

A synthesis tool does not run the scheduler. It reads the structure. For a clocked block, it sees an edge in the sensitivity list and builds a flip-flop for every signal assigned in the block, with the right-hand side feeding the flip-flop's input. It generally treats = and <= the same way at this point, which is exactly why the mismatch exists: the simulator honours the difference, and the synthesised hardware always behaves as if every assignment were non-blocking. The code that simulates with blocking assignments in a clocked block therefore describes hardware that may not match what was simulated. Writing <= makes the simulation and the hardware agree by construction, and that agreement is the whole reason for the rule.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.