You built muxes, adders, and ALU slices — all combinational. But one missing else, one forgotten default, or one legacy always @(a) sensitivity list and synthesis quietly inserts a latch. This lesson teaches you to see the pattern, fix it with default assignments, and let lint catch it before tapeout.
Combinational logic must produce a defined output for every input combination, on every evaluation. If the tool cannot prove that, it keeps the old value — and that "memory" is a latch.
A latch is not magic — it is the tool preserving a signal when your RTL forgot to drive it.
| RTL pattern | What synthesis sees | Result |
|---|---|---|
always @(*) with full branches | Pure combinational cloud | ✅ Gates only |
case missing default | Output holds when sel hits uncovered value | ❌ Latch on out |
if without final else | Output undefined for some input combos | ❌ Latch on out |
always @(a) — partial sensitivity | Re-evaluates only when a changes | ❌ Latch + sim/synth mismatch |
| Default assign then override | Every path assigns out | ✅ Latch-free |
Think of combinational logic as a vending machine: every coin combination must dispense something. An incomplete case is a machine that sometimes keeps the last snack in the slot because you never told it what to do — that "keeping" is the latch.
case — bad vs goodThe most common latch bug in student RTL: a case that covers the opcodes you tested in sim, but not all bit patterns synthesis must handle.
// BAD — latch when sel = 2'b11
module bad_mux3 (
input wire [1:0] sel,
input wire [7:0] a, b, c,
output reg [7:0] out
);
always @(*) begin
case (sel)
2'b00: out = a;
2'b01: out = b;
2'b10: out = c;
// sel=11: out not assigned → latch inferred
endcase
end
endmodule
// GOOD — explicit default branch
module good_mux3 (
input wire [1:0] sel,
input wire [7:0] a, b, c,
output reg [7:0] out
);
always @(*) begin
case (sel)
2'b00: out = a;
2'b01: out = b;
2'b10: out = c;
default: out = 8'h00; // safe known value
endcase
end
endmodule
// GOOD — default assignment pattern (preferred in large case blocks)
module good_mux3b (
input wire [1:0] sel,
input wire [7:0] a, b, c,
output reg [7:0] out
);
always @(*) begin
out = 8'h00; // default FIRST — latch impossible
case (sel)
2'b00: out = a;
2'b01: out = b;
2'b10: out = c;
endcase
end
endmodule
Assigning inside only some branches of an if/else if chain without a final else creates the same latch. if (en) out = a; with no else is a classic interview question — and a real silicon bug.
if/else chainsPriority encoders and conditional muxes often use if/else if. Without a covering else, the last signal driven "sticks."
// BAD — latch when req = 4'b0000 always @(*) begin if (req[0]) grant = 4'b0001; else if (req[1]) grant = 4'b0010; else if (req[2]) grant = 4'b0100; else if (req[3]) grant = 4'b1000; // no else — grant holds when req=0 end
// GOOD — default assignment covers all inputs always @(*) begin grant = 4'b0000; // no request → no grant if (req[0]) grant = 4'b0001; else if (req[1]) grant = 4'b0010; else if (req[2]) grant = 4'b0100; else if (req[3]) grant = 4'b1000; end
Assign every output to a safe value first, then override in branches. Works for case, if, and FSM next-state logic.
Use default: in case or final else in if chains. Document whether default is error, idle, or don't-care.
always @(a) bugBefore always @(*), engineers hand-wrote sensitivity lists. Miss one input and simulation lies while synthesis builds different hardware.
Golden rule: Use always @(*) or always_comb (SystemVerilog) for all combinational blocks. Never hand-pick signals unless you are writing a testbench.
// BAD — only re-evaluates when 'a' changes; b and sel ignored in sensitivity always @(a) begin if (sel) out = a + b; else out = a - b; end // Simulation: out stuck until 'a' toggles even if sel/b change // Synthesis: may still infer combo logic OR latch depending on tool
// GOOD — automatic full sensitivity always @(*) begin if (sel) out = a + b; else out = a - b; end // SystemVerilog equivalent: // always_comb begin // if (sel) out = a + b; // else out = a - b; // end
| Sensitivity style | Sim behavior | Synth behavior | Verdict |
|---|---|---|---|
always @(a) | Misses updates when b/sel change | Tool-dependent; often warns | ❌ Never in RTL |
always @(a or b or sel) | Correct if list is complete | Usually OK | ⚠️ Fragile — easy to forget new signal |
always @(*) | Full automatic sensitivity | Combinational intent clear | ✅ Default choice |
full_case / parallel_case — myths, lightlyVerilog pragmas // synopsys full_case and // synopsys parallel_case tell the synthesizer "trust me, this case is complete / non-overlapping." They do not fix incomplete RTL — they silence warnings.
full_case says all input values are covered. If you lie, synthesis may optimize away "unreachable" logic that simulation still hits.
parallel_case says branches don't overlap. Wrong pragma → priority logic you didn't intend.
Modern teams prefer provably full case with default over pragmas. Let formal/lint prove coverage instead of commenting your way past warnings.
// DANGEROUS — pragma hides latch warning but logic is still incomplete
always @(*) begin
case (sel) // synopsys full_case
2'b00: out = a;
2'b01: out = b;
// 2'b10, 2'b11 uncovered — latch may still exist!
endcase
end
// Fix the RTL; don't silence the linter.
Run lint on every commit. Latch inference is a high-severity finding because latches break timing closure and often indicate a logic bug.
| Tool | Typical warning | What it means |
|---|---|---|
| SpyGlass / VC Lint | Latch inferred / INFERRED_LATCH | Combo block doesn't assign all outputs in all paths |
| Verilator lint | COMBLOOP / incomplete assignment | Signal may retain value — check always block |
| Design Compiler | LINT-1 inferred latch on reg | Change to default-assign pattern or add branch |
| Vivado | [Synth 8-327] latch inferred | Same root cause — incomplete assignment |
Pro tip: Treat "latch inferred" as a blocking issue in code review — same severity as multiply-driven nets. Zero tolerance in combinational datapath modules.
Paste each snippet into the simulator. Synthesis-oriented lint may not run in-browser, but you can reason about which signals are incomplete.
// LAB 1 — Fix the latch in this 2-to-4 decoder
module lab1_decoder (
input wire [1:0] addr,
output reg [3:0] dec
);
always @(*) begin
case (addr)
2'b00: dec = 4'b0001;
2'b01: dec = 4'b0010;
2'b10: dec = 4'b0100;
// TODO: add missing branch or default
endcase
end
endmodule
// Fix: default dec = 4'b0000; OR 2'b11: dec = 4'b1000;
// LAB 2 — ALU op decode with incomplete if chain
module lab2_alu_slice (
input wire [1:0] op,
input wire [7:0] a, b,
output reg [7:0] y
);
always @(*) begin
if (op == 2'b00) y = a + b;
else if (op == 2'b01) y = a - b;
else if (op == 2'b10) y = a & b;
// op==11: y not assigned → latch
end
endmodule
// Fix: y = 8'h00; at top, or else y = a | b;
// LAB 3 — Reference solution pattern for lab2
localparam OP_ADD = 2'b00, OP_SUB = 2'b01, OP_AND = 2'b10, OP_OR = 2'b11;
always @(*) begin
y = 8'h00;
case (op)
OP_ADD: y = a + b;
OP_SUB: y = a - b;
OP_AND: y = a & b;
OP_OR: y = a | b;
default: y = 8'h00;
endcase
end
always @(*) — never partial sensitivity listsreg output in combo logic has a value on every pathdefault/else on every branch structurefull_case pragma as a substitute for real coverageout = 0; then override) is the safest latch-prevention pattern.always @(*) fixes sensitivity; it does not fix incomplete branches — you need both.