Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 37 ยท Modules and Data Types

wire, reg, integer and Other Verilog Data Types

Understand Verilog nets and variables: wire versus reg, the four logic values 0 1 x z, integer, real, time, tri-state, and a runnable C model of four-state signals.

The most confusing part of Verilog for newcomers is wire versus reg. The names suggest "a wire" and "a register", but that is only half the story. This lesson explains what the two kinds of data really are, introduces the four logic values, and covers the other types you will use.

Four-state logic

A hardware signal is not just 0 or 1. Verilog models four values:

Value Meaning
0 Logic low, false
1 Logic high, true
x Unknown or uninitialized (could be 0 or 1)
z High impedance: nothing is driving the wire

You will see x at time zero for every reg before it is assigned, and when two drivers fight (one drives 1, another 0). z appears on tri-state buses and on an input pin that nothing is connected to. Seeing x in a waveform usually means a bug: a missing reset, an unconnected port or a conflict.

Nets: wire

A net models a physical connection. It has no memory: its value is whatever its driver currently drives. The wire is the common net type.

VERILOG
wire a, b, y;
assign y = a & b;     // y follows a and b

wire [7:0] data;      // an 8-bit bus

A net must be driven by a continuous assignment, a primitive's output or a module output. If nothing drives it, it is z. The same wire can be read by many places.

Other net types exist (tri, wand, wor, supply0, supply1), but wire covers most uses.

Variables: reg

A reg (register-type variable) can hold a value and is assigned inside procedural blocks (always, initial):

VERILOG
reg q;
always @(posedge clk) q <= d;   // q remembers d

reg [3:0] result;
always @* result = a + b;       // reg, but no flip-flop!

Here is the key point: reg does not always mean a hardware register. It only says "this is a variable assigned in a procedural block." Whether it becomes a flip-flop or plain logic depends on the kind of block:

  • In always @(posedge clk), a reg becomes a flip-flop.
  • In always @* (combinational), a reg becomes just wires and gates.

SystemVerilog fixes the naming problem: logic replaces both for most uses (see the Verilog track for the SystemVerilog chapter). In chapter 7 you will use logic throughout.

The assignment rule

You assign it with It must be
assign (continuous) a wire
Procedural block (always, initial) a reg
Module/primitive output connection a wire

Breaking this gives an error such as "reg is not a valid l-value" or "wire is not a valid left-hand side". Remembering the table avoids most such errors.

integer, real, time

  • integer is a 32-bit signed variable, mostly used as a loop counter in testbenches and for loops (for (i = 0; i < 8; i = i + 1)). It is synthesizable as a loop index.
  • real holds a floating-point number. It is simulation only; hardware has no real type.
  • time is a 64-bit variable holding simulation time, assigned from $time.
VERILOG
integer i;
real    ratio;
time    t_start;

initial begin
    t_start = $time;
    ratio = 3.14159;
end

Signed values

By default, nets and regs are unsigned. Add signed to treat a vector as two's complement:

VERILOG
reg signed [7:0] temp;   // -128 to 127
wire signed [7:0] a;

Mixing signed and unsigned operands in one expression makes the whole expression unsigned, a frequent trap (the lesson on operators shows examples).

Tri-state and z

A tri-state buffer drives a wire when enabled and releases it (high impedance) otherwise. Several buffers can share one wire if only one is enabled at a time:

VERILOG
assign bus = en_a ? data_a : 8'bz;
assign bus = en_b ? data_b : 8'bz;

If both enables are on with different data, the wire becomes x. Tri-states are used for I/O pads; inside an FPGA or ASIC core they are generally avoided in favor of multiplexers.

Memories

A memory is an array of regs:

VERILOG
reg [7:0] ram [0:255];      // 256 words of 8 bits

always @(posedge clk) begin
    if (we) ram[addr] <= din;
    dout <= ram[addr];
end

Synthesizers can map such arrays to block RAM. Note the order: the width goes before the name, the depth after.

Stand-in: four-state values in C

C has no x or z, but we can model a four-state bit and its resolution rules to see how conflicts produce x. This is not Verilog; it is a small model with a resolution function for two drivers on one wire.

C Stand-in: four-state wire resolution (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Notice that 0 & x is 0: an AND gate with a low input is low whatever the other input is. Verilog's operators follow the same logic, which is why x does not always spread through a design.

Practical guidelines

  • Use wire for assign and connections; use reg for anything assigned in always.
  • Initialize or reset every register so it does not start as x in simulation (and so hardware starts in a known state).
  • Use signed only when you mean two's complement arithmetic, and keep both operands signed.
  • Avoid real, time and tri-state in synthesizable core logic.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which values can a Verilog logic signal take?

    Four-state logic includes unknown (x) and high impedance (z).
  2. 2. A reg assigned in always @* becomes:

    reg only means procedural assignment; a combinational always block produces gates, not storage.
  3. 3. Which target type does assign require?

    Continuous assignments drive nets, usually wire.
  4. 4. What value does an undriven wire have?

    With no driver, a net floats at high impedance, z.
  5. 5. What does 0 & x evaluate to?

    An AND with a 0 input is 0 regardless of the other input.

Summary

  • Verilog signals have four states: 0, 1, x (unknown) and z (undriven).
  • wire is a net driven by assign or outputs; reg is a variable assigned in procedural blocks.
  • reg does not always mean a flip-flop; the block type decides.
  • integer, real and time serve loops and simulation; real and time are not synthesizable.
  • Memories are arrays of regs; tri-state is for pads, not core logic.

Next: Numbers and Literals