Draft preview. Lessons are being written and reviewed.

Digital Design / Module 3: Verilog that means hardware

13. wire, reg, logic and 4-state values

Avoid the most common confusion in Verilog.

Lecture coming soon

See it move

The idea

Simulation uses four values, not two. 0 and 1 are the usual logic levels. x means the simulator doesn't know the value, and z means nothing is driving the wire.

wire, reg and logic

The names are misleading. reg does not mean a register. It only means the signal is assigned inside a procedural block.

reg y;
always @(*) y = a & b;   // y is a reg, yet this is just an AND gate

wire z;
assign z = a & b;        // the same hardware

SystemVerilog's logic replaces both in most code. It can be assigned from either kind of construct, and tools complain if it has more than one driver. That catches real bugs. Use wire when a net truly has multiple drivers, such as a tri-state bus.

How x behaves

An unknown spreads, unless a known input decides the output anyway. 0 & x is 0, because an AND with a 0 input must be 0. 1 & x is x. This is useful: a reset you forgot shows up as x across your waveform.

Vectors and signedness

wire [7:0] a is eight bits with bit 7 as the MSB. Vectors are unsigned unless you declare them signed. If any operand in an expression is unsigned, the whole expression is treated as unsigned, which can silently break a comparison.

Common mistake

Trusting x to tell you everything. The expression if (x_signal) treats x as false and takes the else branch, so an unknown can disappear in simulation while real silicon does something else. This is called X-optimism. Reset everything that needs it, and check with === in testbenches where you must detect x.

Key ideas

  • 'reg' does not guarantee a register.
  • SystemVerilog 'logic' replaces most uses, with a single driver.
  • X means unknown and Z means undriven.

Your exercise

Predict the output when one input of an AND is X and the other is 0, then 1.

Find the bug

This code has one deliberate bug. Read the symptom, then click the line you think is wrong.

What goes wrong: 5 < 9 works, but -1 < 1 reports false.

Interview questions

coreDoes reg always synthesize to a flip-flop?

coreWhat is 0 AND X?

basicWhat is the difference between x and z in Verilog simulation?

basicWhat are 1 | x, 0 | x and 1 ^ x?

coresel is x in simulation. What does this block give for y, with a = 1 and b = 0?

always @(*)
  if (sel) y = a;
  else     y = b;

coreA testbench checks if (dout == 1'bx) to detect an unknown output. Why does that never fire, and what should it use?

coreWhy does this fail to compile, and what are the two fixes?

wire y;
always @(*) y = a & b;

coreTwo drivers share one wire through tri-state buffers, as shown. What does t show when only the first is enabled with v1 = 1, when both are enabled with different values, and when neither is enabled?

assign t = en1 ? v1 : 1'bz;
assign t = en2 ? v2 : 1'bz;

advancedx is a signed 8-bit wire holding -4. Why does y hold 253 instead of -3?

wire signed [7:0]  x = -8'sd4;
wire signed [15:0] y = x + 1'b1;

advancedA counter has no reset. In simulation it stays xxxx forever. In silicon it counts. Which is more truthful, and what should you do?

always @(posedge clk)
  cnt <= cnt + 4'd1;   // no reset

Practise these 10 questions with animations