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 logic or bit for 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.

One Counter, Three DeclarationsThe lab bench, 1 ns after edges 2 to 6. The counter has no reset in any row. The last row declares the flops with a start value of 9, one way a chip can wake.
01234clkencount (logic)xxxxcount (bit)0001001000110100count (woke at 9)1010101111001101x, 1 or 10?pause
Scroll horizontally ↔
  1. Cycle 0

    Edge 2, the first beat. Declared logic, the count is unknown, and unknown plus 1 is still unknown.

  2. Cycle 0

    Declared bit, the count started at 0 and now shows 1. Nothing in the design made it 0.

  3. Cycle 0

    A chip whose flops woke at 9 shows 10. The bit simulation matched one chip out of sixteen.

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

Where Each Value Comes FromArrows run from a cause to the value simulation shows, and from that value to what the chip has.
  • Device
  • Neutral
  • Design
  • Structure
Where each value comes fromArrows run from a cause to the value simulation shows, and from that value to what the chip has. device A gate drives thewire an output, a flop, aconstant neutral Nothing drives it an open input, atri-state turned off neutral The simulatorcannot tell never written, twodrivers, a bad index neutral 0 or 1 known neutral Z high impedance neutral X unknown design The chip every wire reads as 0or 1 showsshowsshowsthe same levelfloats to 0 or 1is 0 or 1
  1. device A gate drives the wirean output, a flop, a constant
    • shows → 0 or 1
  2. neutral 0 or 1known
    • the same level → The chip
  3. design The chipevery wire reads as 0 or 1
  4. neutral Nothing drives itan open input, a tri-state turned off
    • shows → Z
  5. neutral Zhigh impedance
    • floats to 0 or 1 → The chip
  6. neutral The simulator cannot tellnever written, two drivers, a bad index
    • shows → X
  7. neutral Xunknown
    • is 0 or 1 → The chip
  1. Known

    A driven wire shows 0 or 1, and the chip has the same level.

  2. Z

    A wire with no driver shows Z. In the chip its voltage drifts, and the gate that reads it sees 0 or 1.

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

LiteralIn 4 bitsIn 8 bitsIn 16 bitsRule
'00000000000000000000000000000fill: every bit of the context
'11111111111111111111111111111fill
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxfill
'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzfill
10001000000010000000000000001a 32-bit number, cut or extended
'h1FF1111111111110000000111111111unsized: cut on the left to fit
4'd20010000000100000000000000010020 does not fit in 4 bits: it is cut to 4
4'b10001000000010000000000000001short digits: zeros fill the size
4'bxxxxx0000xxxx000000000000xxxxa leading x fills the size, and zeros fill the rest
4'bz1zzz10000zzz1000000000000zzz1a leading z fills the size
4'sb10011001111110011111111111111001the 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.

DeclarationStatesBefore the first writeAfter = 4'b1x0z
logic [3:0] v;fourxxxx1x0z
integer v;fourxkeeps x and z
bit [3:0] v;two00001000
int v;two0x 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.

Three Readers of One Start ValueArrows show what each tool does with logic [3:0] count_q = 4'd9; on a flop with no reset.
  • Neutral
  • Tool
  • Result
  • Storage
  • Structure
Three readers of one start valueArrows show what each tool does with `logic [3:0] count_q = 4'd9;` on a flop with no reset. neutral count_q = 4'd9 a value at thedeclaration tool Simulator Icarus 12 tool Synthesis for anASIC Yosys 0.70, genericcells tool Synthesis for anFPGA a device withpower-up values result Starts at 9 1010 after the firstbeat storage A plain flop wakes at 0 or 1 result Loaded atpower-up from the bitstream runs oncekept as a notekept as a notetime 0no pin for itused
  1. neutral count_q = 4'd9a value at the declaration
    • runs once → Simulator
    • kept as a note → Synthesis for an ASIC
    • kept as a note → Synthesis for an FPGA
  2. tool SimulatorIcarus 12
    • time 0 → Starts at 9
  3. result Starts at 91010 after the first beat
  4. tool Synthesis for an ASICYosys 0.70, generic cells
    • no pin for it → A plain flop
  5. storage A plain flopwakes at 0 or 1
  6. tool Synthesis for an FPGAa device with power-up values
    • used → Loaded at power-up
  7. result Loaded at power-upfrom the bitstream
  1. Simulation

    The value is applied once at time 0. The lab bench printed count=1010 after the first beat.

  2. An ASIC

    On this flow the flop stays $_DFFE_PP_, a flop with an enable and no reset. The netlist keeps init = 1001 as an attribute on the wire, and no cell reads it.

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

Design brief
PortDirectionWidthMeaning
clkinput1clock
rst_ninput1reset, asynchronous, active-low
eninput11 counts one beat at this edge
countoutput[3:0]beats so far, modulo 16
wrapoutput11 when count is 15
Clock
rising edge
Reset
rst_n asynchronous, active-low. After reset count is 0
Corner cases
the first beat after reset. Done when: Run prints count=0001 at edge 2 with count declared logic, and Synthesize reports 11 cells
  1. Run as shipped

    Every line reads count=xxxx wrap=x. Lint says only that rst_n is not used. That is the real finding.

  2. Hide it

    Change output logic [3:0] count to output bit [3:0] count and Run. The lines read 0001, 0010, 0011, 0011, 0100. The design did not change.

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

  4. Fix the cause

    Put logic back and add the reset shown below. Run prints the same five lines as the bit version. The flops become $_DFFE_PN0P_.

The fix: a reset on the counter
Synthesize reports 11 cells, 4 flops and 3 levels of logic, as before. Lint is silent.
  always_ff @(posedge clk or negedge rst_n)
    if (!rst_n)  count <= 4'd0;
    else if (en) count <= count + 4'd1;
Asked in interviews
  1. What is the difference between logic and bit?

    logic is four-state and starts at X. bit is two-state and starts at 0. A bit flop with no reset looks reset in simulation, so use logic for design signals.

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

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

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

Original 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

Predict the outputA 12-bit variable is assigned the sized literal 4'bx. What does the bench print?
module tb;
  logic [11:0] v;
  initial begin
    v = 4'bx;
    $display("v=%b", v);
    $finish(0);
  end
endmodule
Spot the bugEach option is the body of a counter that must start at 0. The bench resets it, counts three edges, resets it again and counts one more. Which option fails?
ConceptWhich declaration gives a variable that reads X before anything writes it?
ConceptA 12-bit signal is assigned the literal 4'sb1001. Which bits does it hold?
ConceptOn this flow, what does the synthesizer build from logic [3:0] count_q = 4'd9; on a flop with no reset?

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.

Before
logic [WIDTH-1:0] mask;
assign mask = 'hFFFF_FFFF;   // 32 ones, whatever WIDTH is
After
logic [WIDTH-1:0] mask;
assign mask = '1;            // WIDTH ones

Wrong 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, 'x and 'z to fill any width.
  • logic starts at X and bit starts 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

Report a problem with this page