Four-State Values, Literals and Uninitialized Variables
Read 0, 1, X and Z correctly, size literals to the width you meant, and keep a missing reset visible.
You Will Be Able To
- Explain what each of the four values means in simulation and in the chip.
- Predict the bits a literal gives in a 4-bit, an 8-bit and a wider context.
- Choose
logicorbitfor a signal and say what the choice hides. - Find a flop that relies on a start value the chip does not have.
- Lesson
- R9.1
- Time
- 25 min
- Runs on
- Your browser
- Check
- 5 questions
A beat counter had no reset. Its first simulation showed xxxx on every line, so the designer changed one word: logic became bit. The count now started at 0 and every test passed. The chip counted from whatever its flops woke with.
- Cycle 0
Edge 2, the first beat. Declared
logic, the count is unknown, and unknown plus 1 is still unknown. - Cycle 0
Declared
bit, the count started at 0 and now shows 1. Nothing in the design made it 0. - Cycle 0
A chip whose flops woke at 9 shows 10. The
bitsimulation matched one chip out of sixteen. - Cycle 4
Edge 6: the three rows never meet. Only a reset makes them agree.
The logic row looks worst and tells the truth: nobody set this counter.
Four Values, Two of Them Real
A wire in a chip is at 0 or at 1. SystemVerilog adds two more values for simulation. Z means that nothing drives the wire. X means that the simulator cannot tell whether the wire is 0 or 1.
- Device
- Neutral
- Design
- Structure
- Known
A driven wire shows 0 or 1, and the chip has the same level.
- Z
A wire with no driver shows Z. In the chip its voltage drifts, and the gate that reads it sees 0 or 1.
- X
X is a statement about the simulator. The chip holds 0 or 1 there, and the simulation does not know which.
Read the diagram as text
- A gate drives the wire: an output, a flop, a constant
- Nothing drives it: an open input, a tri-state turned off
- The simulator cannot tell: never written, two drivers, a bad index
- 0 or 1: known
- Z: high impedance
- X: unknown
- The chip: every wire reads as 0 or 1
- A gate drives the wire → 0 or 1: shows
- Nothing drives it → Z: shows
- The simulator cannot tell → X: shows
- 0 or 1 → The chip: the same level
- Z → The chip: floats to 0 or 1
- X → The chip: is 0 or 1
Synthesis reads an X that you assign as "either value will do". Lesson R11.5 uses that on purpose.
An unknown spreads through arithmetic. On Icarus 12, 4'b10x1 + 4'd1 printed xxxx: one unknown bit made the whole sum unknown. Lesson R10.5 covers the constructs that do the opposite and hide it.
Literals: Size, Base and Fill
A literal such as 8'hA5 has three parts: a size in bits, a base letter and the digits. Leave the size out and the literal is at least 32 bits wide. That default causes most literal bugs.
| Literal | In 4 bits | In 8 bits | In 16 bits | Rule |
|---|---|---|---|---|
'0 | 0000 | 00000000 | 0000000000000000 | fill: every bit of the context |
'1 | 1111 | 11111111 | 1111111111111111 | fill |
'x | xxxx | xxxxxxxx | xxxxxxxxxxxxxxxx | fill |
'z | zzzz | zzzzzzzz | zzzzzzzzzzzzzzzz | fill |
1 | 0001 | 00000001 | 0000000000000001 | a 32-bit number, cut or extended |
'h1FF | 1111 | 11111111 | 0000000111111111 | unsized: cut on the left to fit |
4'd20 | 0100 | 00000100 | 0000000000000100 | 20 does not fit in 4 bits: it is cut to 4 |
4'b1 | 0001 | 00000001 | 0000000000000001 | short digits: zeros fill the size |
4'bx | xxxx | 0000xxxx | 000000000000xxxx | a leading x fills the size, and zeros fill the rest |
4'bz1 | zzz1 | 0000zzz1 | 000000000000zzz1 | a leading z fills the size |
4'sb1001 | 1001 | 11111001 | 1111111111111001 | the s makes it signed: the top bit extends |
Every cell was printed by Icarus 12 after an assignment to a 4-, 8- and 16-bit variable.
Two rules explain the table. A sized literal is built at its own size first, then cut or extended to the context. A fill literal has no size of its own and takes the context width.
So an x or z digit fills only up to the literal's size. 4'bx is four unknown bits, and a wider signal gets zeros above them. To make a whole signal unknown, write 'x.
bit Against logic
A four-state type can hold 0, 1, X and Z. A two-state type can hold only 0 and 1. The type decides what a variable shows before anything writes it.
| Declaration | States | Before the first write | After = 4'b1x0z |
|---|---|---|---|
logic [3:0] v; | four | xxxx | 1x0z |
integer v; | four | x | keeps x and z |
bit [3:0] v; | two | 0000 | 1000 |
int v; | two | 0 | x and z become 0 |
Measured on Icarus 12. reg behaves like logic; byte, shortint and longint behave like int.
A two-state variable turns every x and z it receives into 0. The unknown is gone from that point on, and so is the warning it carried.
logic: the missing reset shows
output logic [3:0] count
// ...
always_ff @(posedge clk) // no reset
if (en) count <= count + 4'd1;
Icarus 12 prints count=xxxx wrap=x at every edge. The test fails, and the cause is one search away.
bit: the missing reset hides
output bit [3:0] count
// ...
always_ff @(posedge clk) // no reset
if (en) count <= count + 4'd1;
Icarus 12 prints 0001, 0010, 0011. The test passes. Synthesize reports the same 11 cells and the same flops with no reset.
Use logic for every signal in a design. Keep bit and int for loop counters, parameters and testbench code, where no flop stands behind the name.
A Start Value at the Declaration
SystemVerilog lets a declaration carry a value: logic [3:0] count_q = 4'd9;. This is in-line initialization. The simulator applies it once, at time 0, before any clock edge.
logic [3:0] count_q = 4'd9; on a flop with no reset.- Neutral
- Tool
- Result
- Storage
- Structure
- Simulation
The value is applied once at time 0. The lab bench printed
count=1010after the first beat. - An ASIC
On this flow the flop stays
$_DFFE_PP_, a flop with an enable and no reset. The netlist keepsinit = 1001as an attribute on the wire, and no cell reads it. - An FPGA
Many FPGA flops load a chosen value when the device is configured. Those flows read the same attribute and use it.
Read the diagram as text
- count_q = 4'd9: a value at the declaration
- Simulator: Icarus 12
- Synthesis for an ASIC: Yosys 0.70, generic cells
- Synthesis for an FPGA: a device with power-up values
- Starts at 9: 1010 after the first beat
- A plain flop: wakes at 0 or 1
- Loaded at power-up: from the bitstream
- count_q = 4'd9 → Simulator: runs once
- count_q = 4'd9 → Synthesis for an ASIC: kept as a note
- count_q = 4'd9 → Synthesis for an FPGA: kept as a note
- Simulator → Starts at 9: time 0
- Synthesis for an ASIC → A plain flop: no pin for it
- Synthesis for an FPGA → Loaded at power-up: used
The same line is a real start value on one target and a simulation-only promise on another.
Lint on this flow warns PROCASSINIT: a variable with a start value is also written in a procedure. For an ASIC, treat that warning as "this flop needs a reset".
A start value also runs only once. A reset in the middle of a run cannot bring it back. The practice problems therefore reset a design that has already been running.
Real Numbers Stay in Constants
A real literal such as 50.0 or 1.0e6 is a floating-point number. Gates cannot hold one. Synthesis accepts a real value only where it can finish the arithmetic before it builds anything.
A real expression in a constant
module tick_gen #(
parameter real CLK_MHZ = 50.0,
parameter real BAUD = 115200.0
) (input logic clk, rst_n, output logic tick);
localparam int DIV = int'(CLK_MHZ * 1.0e6 / BAUD + 0.5);
// ... a counter from 0 to DIV - 1 ...
Yosys 0.70 on this flow: builds. DIV is 434, and the divider is 48 cells with 10 flops.
A real variable in the logic
module scale (input logic [7:0] a,
output logic [7:0] y);
real r;
always_comb begin
r = a * 0.75;
y = int'(r);
end
endmodule
Yosys 0.70 on this flow: refused, with Feature unimplemented from the slang front end. Scale with integers and a shift.
The Lab: One Word That Hid the Bug
| Port | Direction | Width | Meaning |
|---|---|---|---|
| clk | input | 1 | clock |
| rst_n | input | 1 | reset, asynchronous, active-low |
| en | input | 1 | 1 counts one beat at this edge |
| count | output | [3:0] | beats so far, modulo 16 |
| wrap | output | 1 | 1 when count is 15 |
- Clock
- rising edge
- Reset
rst_nasynchronous, active-low. After resetcountis 0- Corner cases
- the first beat after reset. Done when: Run prints
count=0001at edge 2 withcountdeclaredlogic, and Synthesize reports 11 cells
Run as shipped
Every line reads
count=xxxx wrap=x. Lint says only thatrst_nis not used. That is the real finding.Hide it
Change
output logic [3:0] counttooutput bit [3:0] countand Run. The lines read0001,0010,0011,0011,0100. The design did not change.Check the gates
Synthesize both. Each is 11 cells with 4
$_DFFE_PP_flops: an enable and no reset. The type changed the simulation only.Fix the cause
Put
logicback and add the reset shown below. Run prints the same five lines as thebitversion. The flops become$_DFFE_PN0P_.
The fix: a reset on the counter
always_ff @(posedge clk or negedge rst_n)
if (!rst_n) count <= 4'd0;
else if (en) count <= count + 4'd1;Asked in interviews
What is the difference between
logicandbit?logicis four-state and starts at X.bitis two-state and starts at 0. Abitflop with no reset looks reset in simulation, so uselogicfor design signals.What does X mean in a simulation?
The simulator cannot tell whether the value is 0 or 1. The chip has one of the two. X is not a voltage.
How do you write all ones for any width?
With the fill literal
'1. It takes the width of its context. A literal such as'hFFFF_FFFFis 32 bits and leaves a wider signal with zeros on top.
Try It
The bench resets, then sends three beats, a pause and one more beat. The counter ignores rst_n, so count is still unknown at edge 2. Unknown plus 1 is unknown, and so is the compare that drives wrap.
Synthesize reports 11 cells: 4 $_DFFE_PP_, 3 $_XOR_, 3 $_AND_ and 1 $_NOT_, with 0 latches and 3 levels of logic. $_DFFE_PP_ is a flop with an enable and no reset pin.
Synthesize the design to measure its cells and inspect the mapped gates.
e2 en=1 count=xxxx wrap=x e3 en=1 count=xxxx wrap=x e4 en=1 count=xxxx wrap=x e5 en=0 count=xxxx wrap=x e6 en=1 count=xxxx wrap=xOriginal design · 11 cells · 4 flip-flops · 0 latches · 3 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: a real edge, so an asynchronous reset acts before any clock
int edge_n = -1;
always @(posedge clk) edge_n = edge_n + 1;
logic en = 0;
logic [3:0] count;
logic wrap;
beat_count dut (.*);
// Advance to the next rising edge, wait for the design to settle, then print one line.
task automatic tick();
@(posedge clk); #1;
if (rst_n) $display("e%0d en=%b count=%b wrap=%b", edge_n, en, count, wrap);
endtask
initial begin
#1 rst_n = 0; tick(); tick(); rst_n = 1; // asserted at 1 ns, edges 0 and 1 in reset, released 1 ns after edge 1
en = 1; tick(); tick(); tick(); // three beats
en = 0; tick(); // a pause
en = 1; tick(); // one more beat
$finish(0); // quiet: no "$finish called" line
end
endmodule
Check Yourself
Common Mistakes
Wrong Write all ones for a parameterized width as a hex literal such as 'hFFFF_FFFF.
The literal has a fixed number of bits, so a wider signal gets zeros above it.
Fix Use the fill literal '1. It takes the width of the signal it is assigned to.
logic [WIDTH-1:0] mask;
assign mask = 'hFFFF_FFFF; // 32 ones, whatever WIDTH islogic [WIDTH-1:0] mask;
assign mask = '1; // WIDTH onesWrong Read an X in the waveform as a voltage between 0 and 1.
X is the simulator saying it cannot tell which of 0 and 1 the wire has.
Fix Ask where the unknown started: a variable nobody wrote, two drivers, or an index past the end.
Wrong Give an ASIC flop its start value at the declaration, as in logic [3:0] count_q = 4'd9;.
The start value runs once in simulation, and a standard-cell flop has no pin that could load it.
Fix Use a reset for the start value. Keep in-line values for FPGA flows that load them at power-up.
Remember
- A chip wire is 0 or 1. Z means no driver, and X means the simulator cannot tell.
- A sized literal is built at its own size, then cut or extended. Use
'0,'1,'xand'zto fill any width. logicstarts at X andbitstarts at 0. A two-state flop with no reset looks reset in simulation.- A start value at the declaration runs once in simulation. An ASIC flop has no pin for it, so use a reset.
- Real numbers may compute a constant. They cannot be a signal.
Practice
Quick reference: logic, wire and reg