wire and reg
The keywords wire and reg are fundamental data types in classic Verilog (IEEE 1364). The keyword names often cause confusion for new designers. The keyword reg does not always synthesize into a physical register. This article explains the language rules, the driver constraints, and the synthesis outcomes for both types.
The wire Net Type
A wire models a physical electrical connection between two points. A wire does not store state. At all times, a wire reflects the value of its driving source.
You must drive a wire with a continuous assignment (assign) or connect it to an output port of a module instance. You cannot assign a wire inside an always or initial procedural block. An undriven wire resolves to high-impedance (z). An undeclared port defaults to type wire.
`timescale 1ns / 1ps
`default_nettype none
module wire_examples (
input wire [7:0] a,
input wire [7:0] b,
output wire [7:0] sum,
output wire [7:0] and_result
);
wire [7:0] internal_node;
assign internal_node = a ^ b;
assign sum = a + b;
assign and_result = internal_node & 8'hF0;
endmodule
When an input value changes, connected wires update immediately in an ideal simulation. In physical silicon, the signal updates after the gate propagation delay.
The reg Variable Type
The keyword reg indicates a procedural data variable. In classic Verilog, you must declare any signal as reg if you assign it inside an always or initial block.
You cannot drive a reg with a continuous assign statement or connect it to an output port of a submodule instance. The initial simulation value of an uninitialized reg is unknown (x). The reg keyword does not force the synthesis tool to create a flip-flop. The structure of the procedural block determines the synthesized hardware.
`timescale 1ns / 1ps
`default_nettype none
module reg_examples (
input wire clk,
input wire rst_n,
input wire [7:0] d,
output reg [7:0] q_ff, // Synthesizes to flip-flops
output reg [7:0] q_comb // Synthesizes to combinational logic
);
// Clocked block: edge-triggered flip-flops with asynchronous reset
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
q_ff <= 8'h00;
else
q_ff <= d;
end
// Combinational block: logic adder without storage
always @(*) begin
q_comb = d + 8'd1;
end
endmodule
The Fundamental Driver Rule
Verilog enforces strict rules regarding signal drivers:
| Data Type | Driven By | Illegal Driver |
|---|---|---|
wire |
assign statement or instance output port |
always or initial procedural block |
reg |
always or initial procedural block |
assign statement or instance output port |
To select the correct type, identify the driving construct:
- If an
assignstatement or instance output drives the signal, declare the signal aswire. - If an
alwaysblock assigns the signal, declare the signal asreg.
Hardware Synthesis Inference
The sensitivity list and assignment structure of an always block determine the synthesized hardware (Figure 1).
The synthesis tool infers three distinct hardware structures from procedural blocks:
- Flip-Flops: A sensitivity list with edge qualifiers, such as
always @(posedge clk), infers edge-triggered flip-flops. - Combinational Logic: An
always @(*)block that assigns values to an output under all input conditions infers pure combinational gates. - Latches: An
always @(*)block that omits assignments on specific execution paths infers level-sensitive latches.
The following module illustrates all three synthesis results:
`timescale 1ns / 1ps
`default_nettype none
module synthesis_inference (
input wire clk,
input wire en,
input wire [3:0] d,
output reg [3:0] ff_out, // Inferred flip-flops
output reg [3:0] comb_out, // Inferred combinational gates
output reg [3:0] latch_out // Inferred transparent latch
);
always @(posedge clk) begin
ff_out <= d;
end
always @(*) begin
comb_out = d + 4'd1;
end
// Incomplete assignment: latch inferred when en equals 0
always @(*) begin
if (en)
latch_out = d;
end
endmodule
Unintended latches cause timing closure issues in synchronous digital systems. Always assign combinational outputs on every code branch or provide a default value at the beginning of the block.
Additional Verilog Net Types
Verilog provides specialized net types for specific simulation and circuit modeling requirements:
| Net Type | Hardware Description |
|---|---|
wire |
Standard single-driver connection |
tri |
Equivalent to wire; indicates intended tri-state net |
wand, wor |
Wired-AND and Wired-OR bus connections |
supply0, supply1 |
Constant ground (0) or supply voltage (1) nets |
tri0, tri1 |
Pull-down or pull-up resistive nets |
integer |
32-bit signed variable for loop control and simulation |
real |
Double-precision floating-point type for simulation only |
Digital RTL design uses wire and reg almost exclusively. Cell libraries use supply0 and supply1 for power rail modeling.
Summary Decision Process
Figure 2 illustrates the selection procedure for signal data types:
- Identify the driver construct.
- Select
wirefor continuous assignments and instance outputs. - Select
regfor procedural assignments insidealwaysblocks. - Structure the
alwaysblock correctly to infer the desired hardware structure.
The SystemVerilog logic Type
SystemVerilog replaces both wire and reg with the unified logic data type. A logic variable accepts continuous assignments and procedural assignments. Module 9 introduces SystemVerilog extensions. Classic Verilog designs require explicit understanding of wire and reg.
Practice Exercise
Identify whether to declare each signal as wire or reg, and state the inferred hardware:
carry, driven byassign carry = a[7] & b[7];count, incremented inalways @(posedge clk)mux_sel, assigned inalways @(*) begin mux_sel = a | b; endresult, connected to the output port of a submodule instance
Solution
wire: Driven by a continuousassignstatement. Synthesizes to an AND gate.reg: Assigned inside a procedural block. The edge-triggered clock infers flip-flops.reg: Assigned inside a procedural block. Thealways @(*)construct infers an OR gate without storage.wire: Driven by an instance output port. An instance output cannot connect to areg.
The next article examines multi-bit vectors, bit selection, concatenation, and memory arrays.