Blocking vs Non-Blocking Assignment
The = vs <= distinction is the single most common source of bugs for digital designers and a mandatory technical interview topic. Master the IEEE 1364 Stratified Event Queue, eliminate race conditions, and test the interactive simulator below.
โก The Verilog Golden Rule
Use = in always @(*) combinational blocks ยท Use <= in always @(posedge clk) sequential blocks
Never mix them in the same always block. Never assign the same variable from multiple blocks.
Click "Step Clock (โclk)" to watch how Verilog evaluates the 3-stage shift register (q1, q2, q3) in real time. Notice how the buggy blocking code collapses into a 1-clock pass-through!
1. Blocking Assignment (=)
Blocking assignments use the = operator. They are called blocking because the execution of the procedural block is blocked until the assignment evaluates and updates the variable on the left-hand side (LHS).
In other words, statements execute sequentially in order, exactly like procedural code in C or Python. The very next statement in the same block immediately sees the updated value.
// Correct use of = in combinational logic always @(*) begin temp = a & b; // temp updates immediately out = temp | c; // 'out' evaluates with the newly calculated 'temp' end
The sequential execution of blocking assignments in combinational always @(*) blocks is intentional and mathematically necessary. It allows you to create intermediate values and reuse them in subsequent calculations without synthesizing unintended hardware latches.
2. Non-Blocking Assignment (<=)
Non-blocking assignments use the <= operator. They are called non-blocking because they evaluate the right-hand side (RHS) expression immediately, but they do not block subsequent lines from executing.
Instead, the assignment update is scheduled into the Non-Blocking Assignment (NBA) event queue. All pending NBA updates in the design are applied simultaneously at the very end of the current simulation timestep.
// Correct use of <= in sequential clocked logic always @(posedge clk) begin b <= a; // RHS evaluates: captures current 'a' c <= b; // RHS evaluates: captures current 'b' (OLD value!) // End of timestep (NBA region): 'b' and 'c' update concurrently end
3. The IEEE 1364 Verilog Event Scheduler
To understand why race conditions occur, you must understand the IEEE 1364 Verilog Stratified Event Queue. Within each discrete simulation time step, the simulator processes events through four primary regions:
-
Active Events Region: Most Verilog code executes here. The simulator evaluates continuous assignments (
assign), evaluates RHS expressions, executes procedural blocking statements (=), and displays messages ($display). -
Inactive Events Region: Processes events that have explicit zero-delay procedural controls (e.g.
#0 a = b;). -
Non-Blocking Assign (NBA) Events Region: Updates the LHS variables of all non-blocking assignments (
<=) whose RHS expressions were evaluated in the Active Region. Because all RHS evaluations are complete before the NBA region updates anything, all flip-flops sample pre-clock values simultaneously! -
Postponed / Monitor Region: Executes
$strobeand$monitorcalls after all logic updates in the current timestep have settled.
<=) were explicitly designed into Verilog to mirror this exact physical behavior in event-driven software simulation.
4. Race Condition Example: Register Swap
Consider two concurrent always blocks trying to swap two register values on every clock tick. Because Verilog simulators are free to execute concurrent always blocks in any arbitrary order, using blocking assignments creates an unpredictable race condition:
// TWO SEPARATE ALWAYS BLOCKS WITH BLOCKING ASSIGNMENT: always @(posedge clk) a = b; // blocking always @(posedge clk) b = a; // blocking // UNDEFINED BEHAVIOR: // If Block 1 runs first: 'a' gets old 'b', then Block 2 runs and 'b' gets the NEW 'a'! (Both end up with 'b') // If Block 2 runs first: 'b' gets old 'a', then Block 1 runs and 'a' gets the NEW 'b'! (Both end up with 'a') // Outcome depends entirely on simulator tool vendor and compile flags!
// CLEAN AND ORDER-INDEPENDENT: always @(posedge clk) a <= b; // Active Region: schedules 'a' to get old 'b' always @(posedge clk) b <= a; // Active Region: schedules 'b' to get old 'a' // NBA Region: Both updates apply concurrently! // 'a' takes old 'b' and 'b' takes old 'a'. Perfect ping-pong swap on every cycle!
5. The Classic Shift Register Collapse Bug
This is the single most tested question in semiconductor technical interviews. Imagine designing a 3-stage shift register (pipelined delay line):
always @(posedge clk) begin b = a; // 'b' is overwritten with 'a' IMMEDIATELY c = b; // 'c' reads the NEW 'b' (which is already 'a'!) d = c; // 'd' reads the NEW 'c' (which is also 'a'!) end // In simulation: All 3 registers update to 'a' on a SINGLE clock edge! // In synthesis: Yosys/Design Compiler synthesizes 3 flip-flops in series. // Result: CATASTROPHIC SIMULATION / SYNTHESIS MISMATCH!
6. Synthesis & Simulation Summary Table
| Feature | Blocking (=) | Non-Blocking (<=) |
|---|---|---|
| Execution Order | Sequential, procedural (immediate update) | Concurrent, scheduled (NBA region update) |
| Subsequent Code Sees | Newly assigned value immediately | Previous (pre-clock) value until timeslot ends |
Combinational Blocks always @(*) |
โ Correct (Models combinational logic) | โ Dangerous (Causes simulation slowdowns & loops) |
Sequential Blocks always @(posedge clk) |
โ Disastrous (Causes race conditions & synthesis mismatches) | โ Correct (Models physical CMOS flip-flops) |
| Mixing in Same Always Block | โ NEVER mix = and <= in the same always block! | |
| Synthesis Mapping | Combinational logic gates / intermediate wires | D-type Flip-Flops with clock enable |
7. Cliff Cummings' 8 Golden Rules of Verilog Assignments
In his seminal award-winning SNUG paper ("Nonblocking Assignments in Verilog GUI, Synthesis and Simulation Issues"), EDA authority Cliff Cummings codified the 8 fundamental guidelines followed by every tier-1 semiconductor company:
- When modeling sequential logic, use non-blocking assignments (
<=). - When modeling latches, use non-blocking assignments (
<=). - When modeling combinational logic with an
alwaysblock, use blocking assignments (=). - When modeling both sequential and combinational logic within the same
alwaysblock, use non-blocking assignments (<=). - Do not mix blocking and non-blocking assignments in the same
alwaysblock. - Do not make assignments to the same variable from more than one
alwaysblock. - Use
$strobeor$monitorto display values assigned with the non-blocking assignment operator. - Do not make assignments using
#0delays.