Blocking Assignments
The blocking assignment operator (=) evaluates its right-hand expression and assigns the result to the target variable immediately. Subsequent statements in the procedural block pause execution until the assignment completes. This article explains how blocking assignments function in combinational logic and why they create race conditions in sequential blocks.
Execution Semantics of Blocking Assignments
The term “blocking” describes internal procedural execution flow within a single begin-end block:
always @(*) begin
temp = a & b; // Line 1: Evaluates and updates temp immediately
y = temp | c; // Line 2: Reads updated value of temp
end
Line 1 completes its assignment before Line 2 begins. Line 2 reads the newly updated value of temp. Within an individual block, statements execute sequentially from top to bottom.
This procedural ordering simplifies combinational logic description. Designers can calculate intermediate variables across sequential steps:
always @(*) begin
c1 = a[0] & b[0];
c2 = (a[1] & b[1]) | (a[1] & c1) | (b[1] & c1);
sum[0] = a[0] ^ b[0];
sum[1] = a[1] ^ b[1] ^ c1;
sum[2] = a[2] ^ b[2] ^ c2;
end
Sequential Syntax Synthesizes Combinational Gates
Although the source code reads sequentially, logic synthesis tools generate a pure combinational circuit. The intermediate variable temp disappears from the final gate netlist:
$$y = temp \mid c, \quad temp = a ,&, b \quad \implies \quad y = (a ,&, b) \mid c$$
The synthesis tool substitutes intermediate variables into a unified boolean equation. The resulting hardware consists of combinational logic gates without storage registers.
4-to-1 Multiplexer Implementation
`timescale 1ns / 1ps
`default_nettype none
module mux4to1 #(
parameter WIDTH = 8
) (
input wire [WIDTH-1:0] d0, d1, d2, d3,
input wire [1:0] sel,
output reg [WIDTH-1:0] y
);
always @(*) begin
case (sel)
2'b00: y = d0;
2'b01: y = d1;
2'b10: y = d2;
2'b11: y = d3;
default: y = {WIDTH{1'bx}}; // Explicit default prevents latches
endcase
end
endmodule
The block executes whenever an input transitions. All assignments use blocking operators (=), synthesizing into a combinational multiplexer tree.
Parameterized Priority Encoder with for Loop
Procedural blocking assignments simplify priority logic modeling:
`timescale 1ns / 1ps
`default_nettype none
module priority_encoder #(
parameter N = 8
) (
input wire [N-1:0] req,
output reg [$clog2(N)-1:0] grant,
output reg valid
);
integer i;
always @(*) begin
grant = {$clog2(N){1'b0}}; // Default assignment: no active requests
valid = 1'b0;
for (i = 0; i < N; i = i + 1) begin
if (req[i]) begin
grant = i[$clog2(N)-1:0]; // Higher indices overwrite lower indices
valid = 1'b1;
end
end
end
endmodule
The loop iterates from index 0 upward. Later active requests overwrite earlier assignments to grant. The highest active index wins priority. The default assignments at the top of the block guarantee assignment on all paths, preventing unintended transparent latches.
Hazards in Clocked Sequential Blocks
Using blocking assignments in clocked sequential blocks creates simulation race hazards:
always @(posedge clk) begin
q = d;
end
always @(posedge clk) begin
r = q + 1;
end
Both blocks trigger on the identical clock edge. If the simulator executes the first block first, r receives d + 1. If the simulator executes the second block first, r receives the previous cycle value of q plus 1. The result varies across different simulator tools.
Consider a multi-stage shift register implemented with blocking assignments:
// Incorrect: Blocking assignments collapse pipeline stages
always @(posedge clk) begin
stage1 = data_in;
stage2 = stage1; // Immediately receives new data_in
stage3 = stage2; // Immediately receives new data_in
end
Because assignments block immediately, all three registers latch data_in within a single clock cycle. The pipeline collapses into a single register stage.
Mathematical Representation
- Combinational Logic: Represents a pure mathematical function of current inputs: $y = f(a, b, c)$. Blocking assignments (
=) model this immediate evaluation. - Sequential Logic: Represents a state transition between discrete clock cycles: $q[n] = f(q[n-1], d[n])$. Non-blocking assignments (
<=) model this synchronous sample-and-hold behavior.
Verification Testbench
`timescale 1ns / 1ps
`default_nettype none
module tb_priority_encoder;
parameter N = 8;
reg [N-1:0] req;
wire [$clog2(N)-1:0] grant;
wire valid;
priority_encoder #(.N(N)) uut (
.req(req),
.grant(grant),
.valid(valid)
);
initial begin
$dumpfile("tb.vcd");
$dumpvars(0, tb_priority_encoder);
req = 8'b0000_0000; #10; $display("req=%b grant=%0d valid=%b", req, grant, valid);
req = 8'b0000_0001; #10; $display("req=%b grant=%0d valid=%b", req, grant, valid);
req = 8'b0010_0000; #10; $display("req=%b grant=%0d valid=%b", req, grant, valid);
req = 8'b0001_0110; #10; $display("req=%b grant=%0d valid=%b", req, grant, valid);
req = 8'b1111_1111; #10; $display("req=%b grant=%0d valid=%b", req, grant, valid);
$finish;
end
endmodule
Execution Log
req=00000000 grant=0 valid=0
req=00000001 grant=0 valid=1
req=00100000 grant=5 valid=1
req=00010110 grant=4 valid=1
req=11111111 grant=7 valid=1
Practice Exercise
Evaluate each procedural block and classify its assignment usage:
// Block A
always @(*) begin
t1 = x ^ y;
t2 = x & y;
z = t1 | t2;
end
// Block B
always @(posedge clk) begin
if (en)
q = d;
end
// Block C
always @(posedge clk) begin
stage1 = data_in;
stage2 = stage1;
stage3 = stage2;
end
// Block D
always @(*) begin
if (sel)
out = in_a;
end
Solution
- Block A: Correct. Combinational block using blocking assignments with intermediate variables. Synthesizes into
z = x | y. - Block B: Incorrect. Clocked sequential block must use non-blocking assignments (
<=). A blocking assignment creates race conditions when external blocks readq. - Block C: Incorrect. Cascaded blocking assignments update immediately, causing all three pipeline registers to capture
data_inon the same clock edge. Use non-blocking assignments (<=). - Block D: Inferred latch bug. When
selis 0,outis unassigned and retains previous state. Add an explicitelseclause or top-level default assignment.
The next article examines non-blocking assignments (<=) and the two-phase simulation cycle that eliminates sequential race conditions.