X-Optimism: When RTL Hides an Unknown

Find where RTL simulation turns an unknown into a clean 0 or 1, and predict what the chip can do there.

You Will Be Able To

  • Explain what X means in simulation and name the places it comes from.
  • Predict what if, case, casez and casex do with an unknown value.
  • Find a flop with no reset that a passing simulation hides.
  • Choose a spelling that lets an unknown reach the outputs.
Lesson
R10.5
Time
30 min
Runs on
Your browser
Check
5 questions

A result register picks its source with a mode bit. The mode flop has no reset. Every simulation passed. On the bench, some chips produced the wrong result until software wrote the mode register.

One RTL, Three ReadingsThe lab bench, 1 ns after edges 2 to 7. y (RTL sim) is the plain simulation. The other two rows are the same design with the mode flop set to 0 and to 1 at time 0.
012345clka00111100100111110001b01010110100100001000y (RTL sim)010101101001000011110001y (woke at 0)010101101001000011110001y (woke at 1)00111100100111110001differdifferdiffermode written
Scroll horizontally ↔
  1. Cycle 0

    Edge 2: the mode bit is unknown. Simulation shows b, a clean value with no x in it.

  2. Cycle 0

    A chip that woke with the mode flop at 1 shows a. Simulation never tried that case.

  3. Cycle 2

    Edge 4: a equals b, so all three rows agree. The mode bit does not matter here.

  4. Cycle 4

    Edge 6: software wrote the mode bit one edge earlier. From here all three rows agree.

Simulation matched one of the two chips and showed no warning about the other. That is X-optimism.

What X Means, and Where It Comes From

A wire in a chip carries 0 or 1. X is not a third voltage. It is the simulator saying "I cannot tell whether this is 0 or 1". Z means that nothing drives the wire.

An unknown has to start somewhere. The table lists the usual sources, with what Icarus 12 printed for each.

SourceA small caseIcarus 12 showsThe chip has
A variable never writtenlogic [3:0] v; read at 1 nsxxxx0 or 1 on each bit
A flop with no resetmode_q in the labx until the first loadthe value it woke with
An input left openleaf u (.en(), ...)z on ena floating gate input: 0 or 1
Two drivers that disagreetwo assigns on one wire, 0 and 1xa fight; the value depends on the drivers
An index past the endvec[6] of a 4-bit vector, mem[6] of 4 wordsxwhatever the address decode picks
A block with its power offoutputs forced to x by the power modelxxxx, and 0000 after an isolation ANDno valid level until power returns
An x the designer wrotedefault: y = 'x;x when that arm runsthe value synthesis chose

Every row was run on Icarus 12. Low Power lesson W5 is the full home of the power-off row.

Two Things Can Happen to an UnknownArrows show the unknown value moving through the logic. Simulation picks one of the two paths for each construct.
  • Neutral
  • Step
  • Result
  • Design
  • Data
Two things can happen to an unknownArrows show the unknown value moving through the logic. Simulation picks one of the two paths for each construct. neutral Source of X a flop with no reset step Optimisticconstruct if, casex, 0 & x, anedge step Pessimisticconstruct the ternary, ==, + result A clean 0 or 1 one of the chip's twocases result X at the output a test can see it design The chip takes 0 or 1, eitherone xxhides itpasses it on0 or 1
  1. neutral Source of Xa flop with no reset
    • x → Optimistic construct
    • x → Pessimistic construct
    • 0 or 1 → The chip
  2. design The chiptakes 0 or 1, either one
  3. step Optimistic constructif, casex, 0 & x, an edge
    • hides it → A clean 0 or 1
  4. result A clean 0 or 1one of the chip's two cases
  5. step Pessimistic constructthe ternary, ==, +
    • passes it on → X at the output
  6. result X at the outputa test can see it
  1. The source

    A flop with no reset holds x in simulation. In the chip it holds 0 or 1, and nobody chose which.

  2. The optimistic path

    An if reads the x and runs its else branch. The result is clean, and it covers only one of the chip's two cases.

  3. The pessimistic path

    A ternary reads the same x and gives x where its inputs differ. A test that checks the output now fails.

Read the diagram as text
  • Source of X: a flop with no reset
  • Optimistic construct: if, casex, 0 & x, an edge
  • Pessimistic construct: the ternary, ==, +
  • A clean 0 or 1: one of the chip's two cases
  • X at the output: a test can see it
  • The chip: takes 0 or 1, either one
  • Source of X → Optimistic construct: x
  • Source of X → Pessimistic construct: x
  • Optimistic construct → A clean 0 or 1: hides it
  • Pessimistic construct → X at the output: passes it on
  • Source of X → The chip: 0 or 1

This lesson is about the top path. Lesson R10.6 covers the bottom one, where simulation shows more X than the chip has.

The Optimistic if

The rule for if is short. The condition is true only when it is a known non-zero value. For x or z the else branch runs.

The chip has no such rule. Its select wire is 0 or 1, so it may take either branch.

selabif (sel) y = a; else y = b;y = sel ? a : b;The chip
x00000
x011x0 or 1
x100x0 or 1
x11111

Measured on Icarus 12. The if column is always clean. The ternary column is x exactly where the chip's two cases differ.

The two spellings build the same gates. On this flow the lab design is 9 cells either way: 4 $_MUX_ and 5 flops. Only the simulation differs.

An unconnected input does the same thing. Its value is z, the if takes the else branch, and the block looks as if the input were 0.

case, casez and casex

A case statement compares all four values exactly. An opcode with an x bit equals no item that is written with 0s and 1s. The default arm runs. With no default, nothing runs and the output keeps its old value.

casez adds wildcards: a z or ? bit is not compared. casex treats x the same way. Both apply the rule to the opcode as well as to the items. That second part is the trap.

case and casez: an unknown opcode matches nothing

always_comb
  casez (op)
    2'b01:   hit = 1'b1;
    default: hit = 1'b0;
  endcase

Icarus 12 with op = 2'b0x: hit=0 for case and for casez. With op = 2'b0z, casez gives hit=1: a floating bit is a wildcard too.

casex: an unknown opcode matches the first item it can

always_comb
  casex (op)
    2'b01:   hit = 1'b1;
    default: hit = 1'b0;
  endcase

Icarus 12: hit=1 for op = 2'b0x, for 2'bxx and for 2'b0z. An opcode that nobody drove decodes as a valid command.

Statementx or z in an itemx in the opcodez in the opcode
casecompared exactlymatches no 0/1 itemmatches no 0/1 item
casezz and ? are wildcardsmatches no 0/1 itemwildcard: can match
casexx, z and ? are wildcardswildcard: can matchwildcard: can match
case ... insidex, z and ? are wildcardsnot a wildcardnot a wildcard

Rows 1 to 3 measured on Icarus 12. case ... inside keeps the wildcards on the item side only.

Operators That Swallow an X

Some operators can give a known result from an unknown input. The result is correct logic. It still hides the x from every later stage.

ExpressionIcarus 12Why
1'b0 & x0an AND with a 0 input is 0
1'b1 | x1an OR with a 1 input is 1
x && 1'b0, x || 1'b10, 1the logical forms follow the same rule
&4'b0xx1, |4'b1xx00, 1one deciding bit settles a reduction
1'b1 & x, 1'b0 | x, ^4'b10x1xno input decides, so the x passes
wand with drivers 0 and x0the net type resolves like an AND
wor with drivers 1 and x1the net type resolves like an OR
tri0, tri1 with no driver0, 1the net type supplies a level

All measured on Icarus 12. Each row is true of real gates as well.

This optimism is often what you want. A synchronous reset is an AND in front of a flop: q <= rst_n & d. With rst_n at 0 the flop loads 0 even if d is unknown. The isolation AND in the first table works the same way.

It hurts when the deciding input is there by accident. A mask that happens to be 0 during the test hides an unknown data bus, and the test passes.

Edges and Addresses

Two more places turn an unknown into an action.

CaseIcarus 12The chip
Clock goes from 0 to x@(posedge clk) runsan edge, or none
Clock goes from x to 1@(posedge clk) runs againan edge, or none
Clock goes from 0 to z@(posedge clk) runsan edge, or none
Write mem[addr] <= d with addr = 2'bxx or 2'b1xno word changesone word is written, and nobody knows which
Read mem[addr] with addr = 2'bxxxone of the words

A read through an unknown address shows x. A write through it leaves the memory looking untouched.

The Lab: Make the Hidden X Show

Design brief
PortDirectionWidthMeaning
clkinput1clock
rst_ninput1reset, asynchronous, active-low
cfg_weinput11 loads the mode bit from cfg_d
cfg_dinput1the new mode bit
a, binput[3:0]the two sources
youtput[3:0]registers a when the mode bit is 1, else b
Clock
rising edge
Reset
rst_n asynchronous, active-low. After reset the mode is 0, so y follows b
Corner cases
the edges after reset and before software writes the mode. Done when: the three columns of Run agree at every edge, and Synthesize still reports 9 cells

The bench holds the design three times. dut is the plain simulation. woke0 and woke1 have the mode flop set at time 0, the two ways a chip can wake.

  1. Run as shipped

    Column y is clean and equals woke0. Column woke1 differs at edges 2, 3 and 5. Lint prints nothing about the flop with no reset.

  2. Make it visible

    Replace the if and else on mode_q with else y <= mode_q ? a : b; and Run. Column y now reads 0xx1, x1x0, 1001, xxxx: an x on each bit where a and b differ.

  3. Check the gates

    Synthesize both spellings. Each is 9 cells, 5 flops, 1 level of logic. You changed what simulation shows and left the hardware alone.

  4. Fix the cause

    Give mode_q the reset shown below and Run. The three columns agree at every edge. Synthesize still reports 9 cells.

The fix: a reset on the mode bit
The mode flop becomes $_DFFE_PN0P_, a flop with an enable and an asynchronous reset. The total stays at 9 cells.
  always_ff @(posedge clk or negedge rst_n)   // the mode bit resets to 0: b is the default source
    if (!rst_n)      mode_q <= 1'b0;
    else if (cfg_we) mode_q <= cfg_d;
Asked in interviews
  1. What is X-optimism?

    Simulation turns an unknown input into a known 0 or 1 result. The chip may take the other value, so a passing test covers only one of two cases.

  2. What does if (sel) do when sel is X?

    It runs the else branch. The condition is true only for a known non-zero value. A ternary with the same select gives X where its two inputs differ.

  3. Why do design teams ban casex?

    It treats an X or Z in the case expression as a wildcard. An unknown opcode then matches an item, and the unknown is hidden. casez or case ... inside avoids the X part.

Try It

The bench changes its inputs 1 ns after an edge and prints 1 ns after the next one. Column y is the plain simulation. It equals woke0 on every line, because an unknown mode_q runs the else branch.

At edge 5 the bench writes the mode bit, and y shows it from edge 6. Before that write, woke1 is the chip that simulation never showed.

Synthesize reports 9 cells: 4 $_MUX_, 4 $_DFF_PN0_ and 1 $_DFFE_PP_, the mode flop with an enable and no reset. There are 0 latches and 1 level of logic.

RTLIcarus Verilog · in your browser · Lint: Verilator service · Synthesize: Yosys service
Edit the design, then Run, Lint or Synthesize
Expected: e2 a=0011 b=0101 | y=0101 | woke0=0101 woke1=0011 e3 a=1100 b=0110 | y=0110 | woke0=0110 woke1=1100 e4 a=1001 b=1001 | y=1001 | woke0=1001 woke1=1001 e5 a=1111 b=0000 | y=0000 | woke0=0000 woke1=1111 e6 a=1111 b=0000 | y=1111 | woke0=1111 woke1=1111 e7 a=0001 b=1000 | y=0001 | woke0=0001 woke1=0001

Original design · 9 cells · 5 flip-flops · 0 latches · 1 logic levels. Synthesis uses Yosys; counts depend on the toolchain.

Read included file: tb.sv
`timescale 1ns/1ps
module tb;
  logic clk = 0;
  always #5 clk = ~clk;                   // rising edges at 5, 15, 25, ...
  logic rst_n = 1;                        // falls at 1 ns, rises 1 ns after edge 1
  int edge_n = -1;
  always @(posedge clk) edge_n = edge_n + 1;
  logic       cfg_we = 0, cfg_d = 0;
  logic [3:0] a = 0, b = 0;
  logic [3:0] y, y_woke0, y_woke1;

  pick_mode dut   (.*);                   // RTL simulation: mode_q starts at x
  pick_mode woke0 (.y(y_woke0), .*);      // a chip whose mode flop woke at 0
  pick_mode woke1 (.y(y_woke1), .*);      // a chip whose mode flop woke at 1
  initial begin woke0.mode_q = 1'b0; woke1.mode_q = 1'b1; end   // the bench reaches in by name: keep mode_q

  task automatic tick();
    @(posedge clk); #1;
    if (rst_n) $display("e%0d a=%b b=%b | y=%b | woke0=%b woke1=%b", edge_n, a, b, y, y_woke0, y_woke1);
  endtask
  initial begin
    #1 rst_n = 0; tick(); tick(); rst_n = 1;
    a = 4'b0011; b = 4'b0101; tick();
    a = 4'b1100; b = 4'b0110; tick();
    a = 4'b1001; b = 4'b1001; tick();                           // a equals b
    a = 4'b1111; b = 4'b0000; cfg_we = 1; cfg_d = 1; tick();    // software writes the mode bit
    cfg_we = 0;               tick();
    a = 4'b0001; b = 4'b1000; tick();
    $finish(0);
  end
endmodule

Check Yourself

Predict the outputA clock starts at 0, becomes unknown for 1 ns, returns to 0 and then rises to 1. The block counts rising edges. What does the bench print?
module tb;
  logic clk = 0;
  int   n = 0;
  always @(posedge clk) n = n + 1;
  initial begin
    #1 clk = 1'bx;
    #1 clk = 1'b0;
    #1 clk = 1'b1;
    #1 $display("n=%0d", n);
    $finish(0);
  end
endmodule
Spot the bugEach option is the body of one decoder: hit is 1 for opcode 01. The bench checks opcodes 01 and 10, then sends 0x and requires that hit is not 1. Which option fails?
ConceptIn the lab as shipped, what does a chip show at edge 2 if its mode flop woke at 1?
ConceptWhere is optimism the correct model of the hardware?
ConceptAn always_comb block holds case (op) with items for 2'b01 and 2'b10 and a default: y = 1'b0;. In simulation op is 2'bxx. What is y?

Common Mistakes

Wrong Treat a simulation with no X at the outputs as proof that every flop is initialized.

An optimistic construct can turn an unknown into a known value before it reaches an output.

Fix Check the state itself: look for X on the flops after reset, as the lab does with mode_q.

Wrong Use casex so that the don't-care bits of an opcode are short to write.

casex also treats an X in the opcode itself as a wildcard, so an unknown opcode matches an item.

Fix Write the wildcard items with casez and ?, or with case ... inside where the tool accepts it.

Before
casex (op)
  2'b1x:   grant = 1'b1;
  default: grant = 1'b0;
endcase
After
casez (op)
  2'b1?:   grant = 1'b1;
  default: grant = 1'b0;
endcase

Wrong Assume an X on a clock or an address stops the design until the signal is known.

A change from 0 to X counts as a rising edge, so the flops load on it.

Fix Treat a clock, reset, enable or address that can be X as a bug of its own, and trace its source.

Wrong Declare the state bit to get rid of the X.

A two-state variable starts at 0 in simulation, while the flop in the chip still wakes at 0 or 1.

Fix Keep design state four-state (logic) and give it a reset or a load before its first use.

Remember

  • X is the simulator's "unknown". A chip holds 0 or 1 there, and nobody chose which.
  • if runs its else branch for an unknown condition, and case runs its default. Both give clean values that cover one of two cases.
  • casex treats an X in the opcode as a wildcard. Use casez or case ... inside for wildcard items.
  • A 0 on an AND or a 1 on an OR hides an X. That is right for a reset and wrong for an accident.
  • A change from 0 to X is a rising edge, and a write through an unknown address changes nothing in simulation.

Practice

Report a problem with this page