Non-Blocking Assignments
In clocked sequential circuits, blocking assignments create simulation race conditions between concurrent blocks. The non-blocking assignment operator (<=) resolves this race hazard. The language specifies a two-phase execution mechanism to guarantee that simulation behavior matches physical silicon flip-flops.
The Two-Phase Evaluation Mechanism
When a clock edge occurs, all sensitive procedural blocks trigger simultaneously. With non-blocking assignments (<=), the simulation time step executes in two distinct phases:
- Phase 1: Evaluation: The simulator executes active blocks. For each non-blocking statement
q <= d, the simulator evaluates the right-hand operanddimmediately. The simulator records this sampled value and schedules an update for target registerq. The current value ofqdoes not change during Phase 1. - Phase 2: Update: After all active procedural blocks finish Phase 1 evaluation, the simulator updates all scheduled targets simultaneously.
During Phase 1, every concurrent block reads signal values as they existed before the clock edge. Signal updates land together during Phase 2. The execution order of concurrent blocks within Phase 1 does not alter simulation outcomes.
Canonical Edge-Triggered Flip-Flop
`timescale 1ns / 1ps
`default_nettype none
module dff (
input wire clk,
input wire d,
output reg q
);
always @(posedge clk) begin
q <= d;
end
endmodule
At the rising clock edge, the simulator samples input d and schedules register q. At the conclusion of the time step, q assumes the sampled value. This behavior models physical setup and hold sampling in hardware flip-flops.
Single-Cycle Register Swap
Consider swapping the values of two registers on a single clock cycle.
Using blocking assignments fails because statements update immediately:
// Incorrect: Blocking assignments overwrite source data
always @(posedge clk) begin
a = b;
b = a; // Reads updated value of a; both registers end with value of b
end
Non-blocking assignments perform the swap without temporary variables:
always @(posedge clk) begin
a <= b; // Samples current value of b
b <= a; // Samples current value of a; a has not updated yet
end
| Execution Step | Action Performed | Variable State |
|---|---|---|
| Phase 1, Line 1 | Sample b; schedule update $a \leftarrow b_{old}$ |
$a$ retains $a_{old}$ |
| Phase 1, Line 2 | Sample $a_{old}$; schedule update $b \leftarrow a_{old}$ | $b$ retains $b_{old}$ |
| Phase 2 | Apply scheduled updates simultaneously | $a = b_{old}$, $b = a_{old}$ |
Both registers exchange data simultaneously, matching physical cross-coupled flip-flops.
Three-Stage Synchronous Pipeline
`timescale 1ns / 1ps
`default_nettype none
module pipeline_3stage #(
parameter WIDTH = 8
) (
input wire clk,
input wire rst,
input wire [WIDTH-1:0] data_in,
output wire [WIDTH-1:0] stage3_out
);
reg [WIDTH-1:0] stage1, stage2, stage3;
always @(posedge clk) begin
if (rst) begin
stage1 <= {WIDTH{1'b0}};
stage2 <= {WIDTH{1'b0}};
stage3 <= {WIDTH{1'b0}};
end else begin
stage1 <= data_in;
stage2 <= stage1; // Samples previous cycle value of stage1
stage3 <= stage2; // Samples previous cycle value of stage2
end
end
assign stage3_out = stage3;
endmodule
Consider sequential data samples A, B, and C arriving on successive clock edges:
- Edge 1: Phase 1 samples
data_inas A. Phase 2 updatesstage1to A. - Edge 2: Phase 1 samples
data_inas B and samplesstage1as A. Phase 2 updatesstage1to B andstage2to A. - Edge 3: Phase 1 samples
stage2as A. Phase 2 updatesstage3to A.
Data progresses through the pipeline exactly one stage per clock cycle (Figure 1).
If you write this pipeline using blocking assignments (=), all three stages capture data_in on the very first clock cycle.
Procedural Assignment Guidelines
- Clocked Sequential Blocks: Use non-blocking assignments (
<=) exclusively in all blocks with edge triggers (always @(posedge clk)). - Combinational Blocks: Use blocking assignments (
=) exclusively in level-sensitive blocks (always @(*)). - Never Mix Operators: Do not mix
=and<=within the same procedural block. Mixing assignment operators introduces unpredictable simulator-dependent scheduling errors.
Standard Flip-Flop Templates
Synchronous Reset D Flip-Flop
`timescale 1ns / 1ps
`default_nettype none
module dff_sync_reset #(
parameter WIDTH = 1
) (
input wire clk,
input wire rst, // Synchronous active-high reset
input wire [WIDTH-1:0] d,
output reg [WIDTH-1:0] q
);
always @(posedge clk) begin
if (rst)
q <= {WIDTH{1'b0}};
else
q <= d;
end
endmodule
Asynchronous Reset D Flip-Flop
`timescale 1ns / 1ps
`default_nettype none
module dff_async_reset #(
parameter WIDTH = 1
) (
input wire clk,
input wire rst_n, // Asynchronous active-low reset
input wire [WIDTH-1:0] d,
output reg [WIDTH-1:0] q
);
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
q <= {WIDTH{1'b0}};
else
q <= d;
end
endmodule
Including negedge rst_n in the sensitivity list instructs the synthesis tool to connect the reset signal directly to the physical asynchronous clear pin of the standard cell flip-flop.
Practice Exercise
Predict the variable states across two successive clock edges for both circuit configurations:
// Configuration A: Concurrent register swap across two blocks
always @(posedge clk) begin
a <= b;
end
always @(posedge clk) begin
b <= a;
end
// Configuration B: Two-stage shift register in a single block
always @(posedge clk) begin
b <= d_in;
c <= b;
end
Initial states:
- Configuration A:
a = 3,b = 7. - Configuration B:
b = 0,c = 0,d_in = 5(held constant).
Solution
-
Configuration A:
- Edge 1: Phase 1 samples
b = 7anda = 3. Phase 2 commits updates:a = 7,b = 3. - Edge 2: Phase 1 samples
b = 3anda = 7. Phase 2 commits updates:a = 3,b = 7. - Result: Registers exchange values on every clock cycle.
- Edge 1: Phase 1 samples
-
Configuration B:
- Edge 1: Phase 1 samples
d_in = 5forband samples previous $b = 0$ forc. Phase 2 commits updates:b = 5,c = 0. - Edge 2: Phase 1 samples
d_in = 5forband samples updated $b = 5$ forc. Phase 2 commits updates:b = 5,c = 5. - Result: Input value
d_inpropagates to outputcafter two clock cycles.
- Edge 1: Phase 1 samples
The next article explores the IEEE Verilog stratified event scheduler, analyzing active, inactive, and NBA execution queues.