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 case from the brief.
  • Predict what each style shows in simulation when an unlisted value arrives.
  • Explain why a full_case pragma lets simulation and gates disagree.
  • Find the output that still holds its value after a default item 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.

One Unlisted Code, Three AnswersThe lab bench sends modes 2, 6, 4, 7, 1 and 5. Rows 2 and 3 are the shipped decoder, in simulation and as the netlist Yosys wrote for it. Row 4 is the decoder with a default item.
012345mode264715gain (simulation)010011000010gain (gates)010000011100000100100001gain (default item)010000001100000000100000mode 6mode 7mode 5
Scroll horizontally ↔
  1. Cycle 0

    Mode 2 is listed. All three rows show gain 4.

  2. Cycle 1

    Mode 6 is not listed. Simulation keeps the old gain, 4. No item ran, so nothing changed gain.

  3. Cycle 1

    The gates show gain 1. The synthesizer was told the case was full, so it chose its own value here.

  4. Cycle 1

    With a default item, 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.

Three Answers to "what about the other codes?"Arrows lead from the question to an answer, then to the styles that state it.
  • Neutral
  • Step
  • Property
  • Assumption
  • Structure
Three answers to "what about the other codes?"Arrows lead from the question to an answer, then to the styles that state it. neutral Unlisted codes what should theoutput be? step A value I chose decode them too property Never happen,checked simulation reportsthem assumption Never happen,unchecked nobody reports them neutral Styles 1 and 2 default item,pre-case value neutral Style 3 unique or priority neutral Styles 4 and 5 x assignment,full_case answeransweranswerwritewritewrite
  1. neutral Unlisted codeswhat should the output be?
    • answer → A value I chose
    • answer → Never happen, checked
    • answer → Never happen, unchecked
  2. step A value I chosedecode them too
    • write → Styles 1 and 2
  3. neutral Styles 1 and 2default item, pre-case value
  4. property Never happen, checkedsimulation reports them
    • write → Style 3
  5. neutral Style 3unique or priority
  6. assumption Never happen, uncheckednobody reports them
    • write → Styles 4 and 5
  7. neutral Styles 4 and 5x assignment, full_case
  1. Decode them

    You pick the value. Simulation and gates agree on it for every code.

  2. Promise, with a check

    You promise the codes never arrive. The synthesizer is free there, and the simulator reports a broken promise.

  3. 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.

ToolWhat it did with unique case (mode) and mode 6
Icarus 12kept the old gain and printed WARNING: ... value is unhandled for priority or unique case statement, with the line of the case
Verilator 5.052stopped the run: Assertion failed in tb.dut: unique case, but none matched for '3'h6'
LintCASEINCOMPLETE and LATCH for unique; LATCH alone for priority
Yosys on this flow11 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.

ToolWhat it did with default: gain = 'x; and mode 6
Icarus 12gain=xxxx
Verilator 5.052gain=0000: it is a two-state simulator and has no x to show
Lintsilent
Yosys on this flowthe 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.

SpellingIcarus 12Verilator 5.052Yosys on this flow
case (mode) // synopsys full_caseignores it: holds the old gain, no messagechecks 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 messageignores it: holds the old gainacts 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:

SpellingSimulation of 011Gates on this flow
plain casezfirst match wins: id=14 cells, 2 levels; netlist gives 1
(* parallel_case *) casezid=1 on both simulators, no message2 cells, 1 level; netlist gives 3
casez (irq) // synopsys parallel_caseIcarus id=1; Verilator stops: synthesis parallel_case, but multiple matches found4 cells: the comment is ignored
unique0 casezIcarus id=1, no message; Verilator stops: unique0 case, but multiple matches found2 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

StyleSimulation of mode 6 (Icarus 12)Who reports mode 6Cells, latchesGates for modes 5 to 7Risk
None, always @*holds the old gainLint: LATCH13, 4 latchesholda latch nobody asked for
None, always_combholds the old gainSynthesize refuses the designnonenonenone: the flow stops you
1 default item0000nobody needs to9, 00000low
2 pre-case value0000nobody needs to9, 00000low
3 unique or priorityholds, with a warningthe simulator11, 00001medium: needs a test that sends the code
4 x assignmentxxxxa check for X, if one exists11, 00001medium: X-optimism can hide it
5 full_caseholds, no messagenobody11, 0 (attribute)0001high: 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

Design brief
PortDirectionWidthMeaning
modeinput[2:0]the amplifier mode
gainoutput[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=0000 for 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.

  1. Run as shipped

    For modes 6, 7 and 5, sim holds the last listed gain and gates shows 0001. Lint warns LATCH. Synthesize reports 11 cells and 0 latches.

  2. Take the attribute away

    Delete (* full_case *) and Synthesize: 13 cells, 4 of them latches. Now the gates hold too, as the simulation does. Change always @* to always_comb and the flow refuses the design.

  3. Try styles 3 and 4

    Write unique case (mode) and Run: Icarus prints a warning before each held line. Then add default: gain = 'x; and Run: sim=xxxx. Both build 11 cells.

  4. Meet the brief

    The brief asks for 0. Write the default item below. Run prints sim=0000 three times, Lint is silent, and Synthesize reports 9 cells.

Style 1 for this brief
9 cells (4 $_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
    endcase
Asked in interviews
  1. How do you avoid a latch in a case that does not list every value?

    Give the output a value on every path: a default item, or an assignment before the case. If the other values truly cannot occur, unique or priority says so and adds a simulation check.

  2. Why is full_case called 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.

  3. What is the difference between default: y = 'x and unique case?

    Both give the synthesizer freedom for the unlisted values. The x shows as an unknown in simulation, and later logic can hide it. unique makes 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.

RTLIcarus Verilog · in your browser · Lint: Verilator service · Synthesize: Yosys service
Edit the design, then Run, Lint or Synthesize
Expected: 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=0001

Original 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

Predict the outputThe case lists three of four values and carries a full_case attribute. The bench sends sel 1 and then sel 3. What does Icarus 12 print?
module tb;
  logic [1:0] sel;
  logic [3:0] y;
  always @*
    (* full_case *) case (sel)
      2'd0: y = 4'd3;
      2'd1: y = 4'd9;
      2'd2: y = 4'd6;
    endcase
  initial begin
    sel = 2'd1; #1;
    sel = 2'd3; #1;
    $display("y=%0d", y);
    $finish(0);
  end
endmodule
Spot the bugEach option is the body of one decoder. The brief says an unlisted sel gives 0. The bench checks the listed values and sends the unlisted one twice. Which option fails?
ConceptOn this flow, how did the gain decoder with a default item compare with the unique case version?
ConceptAn old block has case (mode) // synopsys full_case inside always @*, with three codes unlisted. What does Synthesize on this flow build?
ConceptA decoder leaves codes unlisted, and one of them arrives during a regression on Icarus. Which style produces a message that names the case statement?

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.

Before
always_comb
  unique0 case (mode)
    3'd0: gain = 4'd1;
    3'd1: gain = 4'd2;
  endcase
After
always_comb
  unique case (mode)
    3'd0: gain = 4'd1;
    3'd1: gain = 4'd2;
  endcase

Wrong 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 default item or a pre-case value gives the same result in simulation and in the gates.
  • unique and priority free 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_case pragma changes the gates and not the simulation. Each tool reads it differently.

Practice

Quick reference: always_comb

Report a problem with this page