Accidental Latches
A transparent latch stores state while its enable signal is low. If any execution path through a combinational always @(*) block fails to assign a value to an output variable, the synthesis tool infers a transparent latch. Unintended latches degrade timing closure and create glitch-sensitive race conditions.
Mechanisms of Latch Inference
Logic synthesis tools assume that unassigned variables must retain their previous values. Memory retention in level-sensitive blocks infers a physical latch. Two coding patterns cause this defect:
1. Incomplete if Statements
`timescale 1ns / 1ps
`default_nettype none
module incomplete_if (
input wire sel,
input wire a, b,
output reg y
);
always @(*) begin
if (sel)
y = a;
// When sel equals 0, y is not assigned: infers a transparent latch
end
endmodule
When sel is 0, variable y retains its old value. The synthesis tool infers a latch with sel as the level-sensitive enable and a as data. Adding an explicit else clause infers a 2-to-1 multiplexer:
always @(*) begin
if (sel)
y = a;
else
y = b; // Multiplexer: y assigned under all input conditions
end
2. Incomplete case Statements
module incomplete_case (
input wire [1:0] sel,
input wire [3:0] a, b, c,
output reg [3:0] y
);
always @(*) begin
case (sel)
2'b00: y = a;
2'b01: y = b;
2'b10: y = c;
// Value 2'b11 is omitted: y retains previous value
endcase
end
endmodule
Adding an explicit default branch eliminates the inferred latch:
case (sel)
2'b00: y = a;
2'b01: y = b;
2'b10: y = c;
default: y = 4'hx; // Covers 2'b11 and unknown simulator states
endcase
Figure 1 shows the hardware difference between a multiplexer and an inferred latch:
The Default Assignment Prevention Pattern
Instead of adding complex else branches to nested statements, assign default values to all outputs at the beginning of the always @(*) block:
`timescale 1ns / 1ps
`default_nettype none
module priority_enc_clean (
input wire [3:0] req,
input wire en,
output reg [1:0] grant,
output reg valid
);
always @(*) begin
// Top-level default assignments guarantee evaluation on every path
grant = 2'b00;
valid = 1'b0;
if (en) begin
if (req[3]) begin grant = 2'b11; valid = 1'b1; end
else if (req[2]) begin grant = 2'b10; valid = 1'b1; end
else if (req[1]) begin grant = 2'b01; valid = 1'b1; end
else if (req[0]) begin grant = 2'b00; valid = 1'b1; end
end
end
endmodule
Because default assignments execute unconditionally, every variable receives a value before conditional branches evaluate. This pattern prevents latch inference.
Synthesis Tool Latch Warnings
Always inspect synthesis logs for latch inference messages:
- Yosys:
Warning: Latch inferred for signal `incomplete_if.y' from process `incomplete_if.$proc...'. - Xilinx Vivado:
[Synth 8-327] inferring latch for variable 'y' [incomplete_if.v:10] - Intel Quartus Prime:
Warning (10240): Verilog HDL Always Construct warning at incomplete_if.v(10): inferring latch(es) for variable "y"
Configure automated build scripts to fail if synthesis logs contain latch warnings.
Intentional Latch Applications
Specialized digital architectures use transparent latches intentionally:
- Integrated clock gating cells (ICGs) to eliminate clock enable glitches.
- High-performance time-borrowing pipelines.
- Standard asynchronous bus interfaces.
When designing intentional latches, document the architecture in comments and verify static timing constraints explicitly.
Decoder Refactoring Example
`timescale 1ns / 1ps
`default_nettype none
// Defective: Incomplete branches infer five transparent latches
module status_dec_buggy (
input wire [2:0] opcode,
output reg wr_en, rd_en, err, ack, stall
);
always @(*) begin
case (opcode)
3'b000: begin wr_en = 0; rd_en = 0; err = 0; ack = 1; stall = 0; end
3'b001: begin wr_en = 1; rd_en = 0; err = 0; ack = 0; stall = 0; end
3'b010: begin wr_en = 0; rd_en = 1; err = 0; ack = 0; stall = 0; end
3'b011: begin wr_en = 0; rd_en = 0; err = 1; ack = 0; stall = 0; end
endcase
end
endmodule
// Correct: Default assignments eliminate all latches
module status_dec_clean (
input wire [2:0] opcode,
output reg wr_en, rd_en, err, ack, stall
);
always @(*) begin
wr_en = 1'b0;
rd_en = 1'b0;
err = 1'b0;
ack = 1'b0;
stall = 1'b0;
case (opcode)
3'b000: ack = 1'b1;
3'b001: wr_en = 1'b1;
3'b010: rd_en = 1'b1;
3'b011: err = 1'b1;
3'b100: stall = 1'b1;
default: ; // Remaining opcodes retain default 0 values
endcase
end
endmodule
Verification Testbench
`timescale 1ns / 1ps
`default_nettype none
module tb_status_dec;
reg [2:0] opcode;
wire wr_en, rd_en, err, ack, stall;
status_dec_clean dut (
.opcode(opcode),
.wr_en(wr_en), .rd_en(rd_en), .err(err), .ack(ack), .stall(stall)
);
integer i;
initial begin
$dumpfile("tb_status_dec.vcd");
$dumpvars(0, tb_status_dec);
for (i = 0; i < 8; i = i + 1) begin
opcode = i[2:0];
#5;
$display("opcode=%03b wr=%b rd=%b err=%b ack=%b stall=%b",
opcode, wr_en, rd_en, err, ack, stall);
end
opcode = 3'b111; #5;
opcode = 3'b000; #5;
$finish;
end
endmodule
Practice Exercise
Identify whether each procedural block infers a latch. State which signal latches and describe how to correct it:
// Block 1
always @(*) begin
if (mode == 2'b00) out = in_a;
if (mode == 2'b01) out = in_b;
if (mode == 2'b10) out = in_c;
end
// Block 2
always @(*) begin
led_r = 1'b0;
led_g = 1'b0;
led_b = 1'b0;
case (color)
2'b00: led_r = 1'b1;
2'b01: led_g = 1'b1;
2'b10: led_b = 1'b1;
2'b11: begin led_r = 1'b1; led_g = 1'b1; end
endcase
end
// Block 3
always @(*) begin
next_state = IDLE;
case (state)
IDLE: if (start) next_state = RUNNING;
RUNNING: if (done) next_state = IDLE;
endcase
end
// Block 4
always @(*) begin
if (a > b) begin
max_val = a;
min_val = b;
end else if (a < b) begin
max_val = b;
min_val = a;
end
end
Solution
- Block 1: Infers a latch on
out. Whenmode == 2'b11, no branch executes. Add an initial assignmentout = in_a;at the top of the block. - Block 2: Clean combinational logic. All outputs receive default values and all input conditions are covered.
- Block 3: Clean combinational logic. Variable
next_statereceives default valueIDLEbefore thecaseevaluation. - Block 4: Infers latches on both
max_valandmin_val. Whena == b, neither branch executes. Add top-level default assignments:max_val = a; min_val = a;.
This concludes Section 5.2. Section 5.3 analyzes conditional statements (if-else, case, casez) and compares priority chains with parallel decode logic.