Module 38 min

Wires, Regs, and the Four Values

wire vs reg, 0/1/x/z, and vectors

Signals in Verilog do not just hold 0 or 1. They can hold four values, and knowing them saves you hours of confusion.

ValueMeansEveryday picture
0Logic lowSwitch off, no voltage
1Logic highSwitch on, full voltage
xUnknownWe genuinely do not know - could be 0 or 1
zHigh impedanceWire not connected to anything (floating)

When a simulation shows x, it is not an error message - it is the tool honestly telling you "this wire was never given a value." That is a clue, not noise.

wire vs reg - the question every beginner asks

These are just two kinds of storage for signals, and the rule for which to use is mechanical once you learn it.

TypeWhat it isUse it when...
wireA plain connection, must be continuously drivenYou assign it with assign, or connect it between modules
regA variable that holds its value until changedYou assign it inside an always block
Pro tip

The name "reg" is misleading. A reg is NOT always a hardware register or flip-flop. It is just a variable you are allowed to assign inside an always block. Whether it becomes a flip-flop depends on HOW you write that block, which you will learn in lesson 7. For now: assign goes with wire, always goes with reg.

Buses - more than one bit

Real signals are often several bits wide (a number, an address). You declare the width with a range in square brackets.

verilog
wire [7:0] data;   // an 8-bit bus, bits 7 down to 0
reg  [3:0] count;  // a 4-bit value (holds 0 to 15)

// [7:0] means the most significant bit is bit 7,
// the least significant bit is bit 0. This is the
// normal, recommended order.

Writing numbers

verilog
4'b1010   // 4 bits, binary  -> decimal 10
8'hFF     // 8 bits, hex     -> decimal 255
8'd255    // 8 bits, decimal -> 255
1'b0      // a single 0 bit

// format is: <width>'<base><value>
// b = binary, h = hex, d = decimal, o = octal
Watch out

If you forget the width and just write 255, Verilog assumes a 32-bit number. Mixing widths silently is a real source of bugs. Be explicit about width whenever it matters.