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,casezandcasexdo 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.
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.- Cycle 0
Edge 2: the mode bit is unknown. Simulation shows
b, a clean value with no x in it. - Cycle 0
A chip that woke with the mode flop at 1 shows
a. Simulation never tried that case. - Cycle 2
Edge 4:
aequalsb, so all three rows agree. The mode bit does not matter here. - 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.
| Source | A small case | Icarus 12 shows | The chip has |
|---|---|---|---|
| A variable never written | logic [3:0] v; read at 1 ns | xxxx | 0 or 1 on each bit |
| A flop with no reset | mode_q in the lab | x until the first load | the value it woke with |
| An input left open | leaf u (.en(), ...) | z on en | a floating gate input: 0 or 1 |
| Two drivers that disagree | two assigns on one wire, 0 and 1 | x | a fight; the value depends on the drivers |
| An index past the end | vec[6] of a 4-bit vector, mem[6] of 4 words | x | whatever the address decode picks |
| A block with its power off | outputs forced to x by the power model | xxxx, and 0000 after an isolation AND | no valid level until power returns |
| An x the designer wrote | default: y = 'x; | x when that arm runs | the value synthesis chose |
Every row was run on Icarus 12. Low Power lesson W5 is the full home of the power-off row.
- Neutral
- Step
- Result
- Design
- Data
- The source
A flop with no reset holds x in simulation. In the chip it holds 0 or 1, and nobody chose which.
- The optimistic path
An
ifreads the x and runs itselsebranch. The result is clean, and it covers only one of the chip's two cases. - 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.
sel | a | b | if (sel) y = a; else y = b; | y = sel ? a : b; | The chip |
|---|---|---|---|---|---|
| x | 0 | 0 | 0 | 0 | 0 |
| x | 0 | 1 | 1 | x | 0 or 1 |
| x | 1 | 0 | 0 | x | 0 or 1 |
| x | 1 | 1 | 1 | 1 | 1 |
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.
| Statement | x or z in an item | x in the opcode | z in the opcode |
|---|---|---|---|
case | compared exactly | matches no 0/1 item | matches no 0/1 item |
casez | z and ? are wildcards | matches no 0/1 item | wildcard: can match |
casex | x, z and ? are wildcards | wildcard: can match | wildcard: can match |
case ... inside | x, z and ? are wildcards | not a wildcard | not 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.
| Expression | Icarus 12 | Why |
|---|---|---|
1'b0 & x | 0 | an AND with a 0 input is 0 |
1'b1 | x | 1 | an OR with a 1 input is 1 |
x && 1'b0, x || 1'b1 | 0, 1 | the logical forms follow the same rule |
&4'b0xx1, |4'b1xx0 | 0, 1 | one deciding bit settles a reduction |
1'b1 & x, 1'b0 | x, ^4'b10x1 | x | no input decides, so the x passes |
wand with drivers 0 and x | 0 | the net type resolves like an AND |
wor with drivers 1 and x | 1 | the net type resolves like an OR |
tri0, tri1 with no driver | 0, 1 | the 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.
| Case | Icarus 12 | The chip |
|---|---|---|
| Clock goes from 0 to x | @(posedge clk) runs | an edge, or none |
| Clock goes from x to 1 | @(posedge clk) runs again | an edge, or none |
| Clock goes from 0 to z | @(posedge clk) runs | an edge, or none |
Write mem[addr] <= d with addr = 2'bxx or 2'b1x | no word changes | one word is written, and nobody knows which |
Read mem[addr] with addr = 2'bxx | x | one 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
| Port | Direction | Width | Meaning |
|---|---|---|---|
| clk | input | 1 | clock |
| rst_n | input | 1 | reset, asynchronous, active-low |
| cfg_we | input | 1 | 1 loads the mode bit from cfg_d |
| cfg_d | input | 1 | the new mode bit |
| a, b | input | [3:0] | the two sources |
| y | output | [3:0] | registers a when the mode bit is 1, else b |
- Clock
- rising edge
- Reset
rst_nasynchronous, active-low. After reset the mode is 0, soyfollowsb- 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.
Run as shipped
Column
yis clean and equalswoke0. Columnwoke1differs at edges 2, 3 and 5. Lint prints nothing about the flop with no reset.Make it visible
Replace the
ifandelseonmode_qwithelse y <= mode_q ? a : b;and Run. Columnynow reads0xx1,x1x0,1001,xxxx: an x on each bit whereaandbdiffer.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.
Fix the cause
Give
mode_qthe 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
$_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
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.
What does
if (sel)do whenselis X?It runs the
elsebranch. The condition is true only for a known non-zero value. A ternary with the same select gives X where its two inputs differ.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.
casezorcase ... insideavoids 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.
Synthesize the design to measure its cells and inspect the mapped gates.
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=0001Original 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
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.
casex (op)
2'b1x: grant = 1'b1;
default: grant = 1'b0;
endcasecasez (op)
2'b1?: grant = 1'b1;
default: grant = 1'b0;
endcaseWrong 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.
ifruns itselsebranch for an unknown condition, andcaseruns itsdefault. Both give clean values that cover one of two cases.casextreats an X in the opcode as a wildcard. Usecasezorcase ... insidefor 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.