Five Ways to Avoid a Latch, Compared
Choose how a decoder treats the codes it does not list. Predict what each choice does in simulation and in the gates.
You Will Be Able To
- Choose one of five styles for the unlisted values of a
casefrom the brief. - Predict what each style shows in simulation when an unlisted value arrives.
- Explain why a
full_casepragma lets simulation and gates disagree. - Find the output that still holds its value after a
defaultitem was added.
- Lesson
- R11.5
- Time
- 30 min
- Runs on
- Your browser
- Check
- 5 questions
A gain decoder lists five of its eight mode codes. Modes 5, 6 and 7 "never happen". One line told the synthesizer so. Then a glitch sent mode 6.
default item.- Cycle 0
Mode 2 is listed. All three rows show gain 4.
- Cycle 1
Mode 6 is not listed. Simulation keeps the old gain, 4. No item ran, so nothing changed
gain. - Cycle 1
The gates show gain 1. The synthesizer was told the case was full, so it chose its own value here.
- Cycle 1
With a
defaultitem, simulation and gates both show 0, the value the designer wrote.
Rows 2 and 3 are the same source file. The test passed on row 2. The chip is row 3.
The Question Behind the Latch
A case that lists only some values leaves a question open. What should the output be for the others? If the code gives no answer, the output keeps its old value. That is memory, and memory in combinational logic is a latch (lesson R1.3).
There are three honest answers, and five ways to write them.
- Neutral
- Step
- Property
- Assumption
- Structure
- Decode them
You pick the value. Simulation and gates agree on it for every code.
- Promise, with a check
You promise the codes never arrive. The synthesizer is free there, and the simulator reports a broken promise.
- Promise, with no check
The synthesizer is just as free. Style 4 shows an X in simulation. Style 5 shows nothing at all.
Read the diagram as text
- Unlisted codes: what should the output be?
- A value I chose: decode them too
- Never happen, checked: simulation reports them
- Never happen, unchecked: nobody reports them
- Styles 1 and 2: default item, pre-case value
- Style 3: unique or priority
- Styles 4 and 5: x assignment, full_case
- Unlisted codes → A value I chose: answer
- Unlisted codes → Never happen, checked: answer
- Unlisted codes → Never happen, unchecked: answer
- A value I chose → Styles 1 and 2: write
- Never happen, checked → Style 3: write
- Never happen, unchecked → Styles 4 and 5: write
Choose the answer from the brief first. The style follows from it.
Styles 1 and 2: Give the Value Yourself
Style 1: a default item
always_comb
case (mode)
3'd0: gain = 4'd1;
// ... modes 1 to 4 ...
default: gain = 4'd0;
endcase
Mode 6 gives 0000 on Icarus, on Verilator and in the netlist. 9 cells, 3 levels. Lint is silent.
Style 2: a value before the case
always_comb begin
gain = 4'd0;
case (mode)
3'd0: gain = 4'd1;
// ... modes 1 to 4 ...
endcase
end
The same results and the same 9 cells. Lint still warns CASEINCOMPLETE, because it looks at the case alone.
Style 2 has one more use. It also covers an arm that forgets an output, which style 1 does not.
Style 3: unique and priority
unique case and priority case both promise that some item always matches. unique also promises that only one does (lesson R1.5). The synthesizer may then build anything for the unlisted codes.
| Tool | What it did with unique case (mode) and mode 6 |
|---|---|
| Icarus 12 | kept the old gain and printed WARNING: ... value is unhandled for priority or unique case statement, with the line of the case |
| Verilator 5.052 | stopped the run: Assertion failed in tb.dut: unique case, but none matched for '3'h6' |
| Lint | CASEINCOMPLETE and LATCH for unique; LATCH alone for priority |
| Yosys on this flow | 11 cells, 0 latches, 3 levels. Its netlist gives 0001 for modes 5, 6 and 7 |
priority case gave the same 11 cells. Verilator's message for it reads priority case, but non-match found.
The 11 cells are a surprise: the free version is larger than the 9 cells of style 1. Freedom for the optimizer is not a smaller circuit by rule. Measure before you pay for it with a promise.
Style 4: Assign x
Before unique existed, designers wrote default: gain = 'x;. To the synthesizer an x means "any value will do". To the simulator it is a visible unknown.
| Tool | What it did with default: gain = 'x; and mode 6 |
|---|---|
| Icarus 12 | gain=xxxx |
| Verilator 5.052 | gain=0000: it is a two-state simulator and has no x to show |
| Lint | silent |
| Yosys on this flow | the same 11 cells as unique, and the same 0001 for modes 5, 6 and 7 |
The X is useful only while something can see it. X-optimism hides it at the next if.
Style 5: The full_case Pragma
A pragma is an instruction to one tool, hidden where other tools do not look. full_case tells a synthesizer to treat the case as complete. Simulators were never meant to read it.
| Spelling | Icarus 12 | Verilator 5.052 | Yosys on this flow |
|---|---|---|---|
case (mode) // synopsys full_case | ignores it: holds the old gain, no message | checks it: synthesis full_case, but non-match found for '3'h6' | ignores it: 13 cells with 4 $_DLATCH_P_ |
(* full_case *) case (mode) | ignores it: holds the old gain, no message | ignores it: holds the old gain | acts on it: 11 cells, 0 latches, 0001 for modes 5, 6 and 7 |
Both spellings with always @*. The comment forms // synthesis full_case and // pragma full_case gave the same 13 cells on this flow.
Three tools read one line in three ways. That is the danger. Sutherland and Cummings report the classic case: a synthesizer acts on the comment and the simulator does not. This flow shows the same split with the attribute spelling, and the lab runs it.
parallel_case is the pragma's twin. It promises that no two items match. Here is a casez with items ??1, ?1? and 1?? and the input 011:
| Spelling | Simulation of 011 | Gates on this flow |
|---|---|---|
plain casez | first match wins: id=1 | 4 cells, 2 levels; netlist gives 1 |
(* parallel_case *) casez | id=1 on both simulators, no message | 2 cells, 1 level; netlist gives 3 |
casez (irq) // synopsys parallel_case | Icarus id=1; Verilator stops: synthesis parallel_case, but multiple matches found | 4 cells: the comment is ignored |
unique0 casez | Icarus id=1, no message; Verilator stops: unique0 case, but multiple matches found | 2 cells, 1 level; netlist gives 3 |
The 2-cell netlist ORs the items. Only Verilator reported the overlap, and only for the comment and for unique0.
Five Styles, One Table
| Style | Simulation of mode 6 (Icarus 12) | Who reports mode 6 | Cells, latches | Gates for modes 5 to 7 | Risk |
|---|---|---|---|---|---|
None, always @* | holds the old gain | Lint: LATCH | 13, 4 latches | hold | a latch nobody asked for |
None, always_comb | holds the old gain | Synthesize refuses the design | none | none | none: the flow stops you |
| 1 default item | 0000 | nobody needs to | 9, 0 | 0000 | low |
| 2 pre-case value | 0000 | nobody needs to | 9, 0 | 0000 | low |
3 unique or priority | holds, with a warning | the simulator | 11, 0 | 0001 | medium: needs a test that sends the code |
| 4 x assignment | xxxx | a check for X, if one exists | 11, 0 | 0001 | medium: X-optimism can hide it |
5 full_case | holds, no message | nobody | 11, 0 (attribute) | 0001 | high: simulation and gates differ in silence |
All measured on the gain decoder. "Gates" is the netlist Yosys wrote, simulated on Icarus with every code.
Use style 1 or 2 unless the brief says otherwise. Use style 3 when the codes truly cannot occur and you measured a gain. Styles 4 and 5 are for reading old code.
The Lab: One Decoder, Five Styles
| Port | Direction | Width | Meaning |
|---|---|---|---|
| mode | input | [2:0] | the amplifier mode |
| gain | output | [3:0] | modes 0 to 4 give 1, 2, 4, 8 and 12; any other mode gives 0 |
- Latency
- combinational
- Corner cases
- modes 5, 6 and 7. Done when: Run prints
sim=0000for modes 6, 7 and 5, Lint is silent, and Synthesize reports 9 cells and 0 latches
The bench prints two columns. sim is your file, simulated. gates is a recording: the netlist that Yosys wrote for the file as shipped. The recording does not change when you edit.
Run as shipped
For modes 6, 7 and 5,
simholds the last listed gain andgatesshows0001. Lint warnsLATCH. Synthesize reports 11 cells and 0 latches.Take the attribute away
Delete
(* full_case *)and Synthesize: 13 cells, 4 of them latches. Now the gates hold too, as the simulation does. Changealways @*toalways_comband the flow refuses the design.Try styles 3 and 4
Write
unique case (mode)and Run: Icarus prints a warning before each held line. Then adddefault: gain = 'x;and Run:sim=xxxx. Both build 11 cells.Meet the brief
The brief asks for 0. Write the
defaultitem below. Run printssim=0000three times, Lint is silent, and Synthesize reports 9 cells.
Style 1 for this brief
$_ANDNOT_, 3 $_NAND_, 1 $_MUX_, 1 $_NOR_), 0 latches, 3 levels. always_comb
case (mode)
3'd0: gain = 4'd1;
3'd1: gain = 4'd2;
3'd2: gain = 4'd4;
3'd3: gain = 4'd8;
3'd4: gain = 4'd12;
default: gain = 4'd0; // modes 5, 6 and 7: mute
endcaseAsked in interviews
How do you avoid a latch in a
casethat does not list every value?Give the output a value on every path: a
defaultitem, or an assignment before thecase. If the other values truly cannot occur,uniqueorprioritysays so and adds a simulation check.Why is
full_casecalled dangerous?It is a comment that the synthesizer acts on and the simulator ignores. The gates then differ from the RTL that was verified, and no tool reports it.
What is the difference between
default: y = 'xandunique case?Both give the synthesizer freedom for the unlisted values. The x shows as an unknown in simulation, and later logic can hide it.
uniquemakes the simulator report the value when it arrives.
Try It
The bench sends six modes, 1 ns apart. Modes 6, 7 and 5 are not listed. In the sim column the decoder holds the gain of the mode before, because no item assigns it.
The gates column is the recorded netlist of the same file. Yosys acted on the attribute and built no latch, and its 11 cells give 0001 for every unlisted mode.
Synthesize reports 11 cells, 0 flops, 0 latches and 3 levels. Lint warns CASEINCOMPLETE and LATCH, because it reads the block the way the simulator does.
Synthesize the design to measure its cells and inspect the mapped gates.
mode=2 sim=0100 gates=0100 mode=6 sim=0100 gates=0001 mode=4 sim=1100 gates=1100 mode=7 sim=1100 gates=0001 mode=1 sim=0010 gates=0010 mode=5 sim=0010 gates=0001Original design · 11 cells · 0 flip-flops · 0 latches · 3 logic levels. Synthesis uses Yosys; counts depend on the toolchain.
Read included file: tb.sv
`timescale 1ns/1ps
// gain_dec_gates: a recording. This is the netlist the pinned Yosys wrote for main.sv as shipped
// (write_verilog -noattr after the site's synthesis script): 11 cells, no latch. It does not change
// when you edit main.sv; Synthesize shows the gates of your edit.
module gain_dec_gates (input wire [2:0] mode, output wire [3:0] gain);
wire _00_, _01_, _02_, _03_, _04_, _05_, _06_;
assign _05_ = mode[1] | ~(mode[2]);
assign _06_ = mode[0] | _05_;
assign gain[0] = _01_ & _04_;
assign _00_ = mode[2] | ~(mode[1]);
assign _01_ = mode[0] ? _00_ : _05_;
assign gain[3] = ~_01_;
assign _02_ = mode[0] | _00_;
assign gain[2] = ~(_06_ & _02_);
assign _03_ = mode[2] | mode[1];
assign gain[1] = mode[0] & ~(_03_);
assign _04_ = mode[0] ? _03_ : _00_;
endmodule
module tb;
logic [2:0] mode;
logic [3:0] gain;
wire [3:0] gates;
gain_dec dut (.mode(mode), .gain(gain)); // your RTL, simulated
gain_dec_gates rec (.mode(mode), .gain(gates)); // the recorded netlist of the shipped file
task automatic show(input logic [2:0] m);
mode = m; #1;
$display("mode=%0d sim=%b gates=%b", m, gain, gates);
endtask
initial begin
show(3'd2); show(3'd6); show(3'd4); show(3'd7); show(3'd1); show(3'd5);
$finish(0);
end
endmodule
Check Yourself
Common Mistakes
Wrong Add unique to a case only to make the latch go away.
unique is a promise that an unlisted value never arrives, and the synthesizer builds on that promise.
Fix Use unique when the promise is true and a test will see its warning. Otherwise write a default item.
Wrong Use unique0 to avoid the latch.
unique0 only says that no two items match, so a value with no item still leaves the output unassigned.
Fix Use unique or priority for that promise. On this flow the unique0 decoder was refused, like the plain one.
always_comb
unique0 case (mode)
3'd0: gain = 4'd1;
3'd1: gain = 4'd2;
endcasealways_comb
unique case (mode)
3'd0: gain = 4'd1;
3'd1: gain = 4'd2;
endcaseWrong Copy // synopsys full_case from an old block and expect every tool to read it the same way.
A pragma is a comment, and each tool decides for itself whether to act on it.
Fix Replace it with a default item, or with unique or priority if the brief allows the promise.
Wrong Assign x in the default item and look for the X only at the block's outputs.
An if in a later stage can turn that X into a clean value before it reaches an output.
Fix If you assign x, also check the signal itself with an assertion (lesson R10.5).
Remember
- An unlisted value needs an answer: a value you chose, a checked promise, or an unchecked one.
- A
defaultitem or a pre-case value gives the same result in simulation and in the gates. uniqueandpriorityfree the synthesizer and make the simulator report a broken promise.- An x assignment frees the synthesizer too, and later logic can hide the X.
- A
full_casepragma changes the gates and not the simulation. Each tool reads it differently.
Practice
Quick reference: always_comb