techmore.in

Verilog Wires

In Verilog, wires are one of the primary ways to connect different parts of a design and are used to model combinational logic. A wire represents a connection between components that continuously drives a value from one point to another. Unlike reg types, wires cannot store values; they simply carry signals from one place to another.

Here's a detailed overview of Verilog wires:

1. Basic Definition of Wire

A wire is used to represent a signal that connects different modules or logic elements. The value on a wire is continuously driven by the logic or connections it is connected to.

Syntax:

verilog
wire signal_name;

Example:

verilog
module simple_wire_example ( input wire a, input wire b, output wire c ); assign c = a & b; // Wire "c" carries the result of a AND b endmodule

2. Characteristics of Wire

  • Wires cannot store values. They reflect the value driven by some other source, such as a continuous assignment (assign statement) or another module’s output.
  • Wires are used for combinational logic, where signals are driven continuously and change as inputs change.
  • You cannot assign a value to a wire inside an always block (that's the role of reg).

3. Continuous Assignment with Wire

Wires are typically driven using the assign statement. The value of the wire is updated continuously as the inputs change.

Syntax:

verilog
assign wire_name = expression;

Example:

verilog
module example ( input wire a, input wire b, output wire sum, output wire carry ); assign sum = a ^ b; // XOR logic for sum assign carry = a & b; // AND logic for carry endmodule

In this example, the sum wire continuously carries the result of a XOR b, and the carry wire continuously carries the result of a AND b.

4. Connecting Wires Between Modules

Wires are often used to connect the output of one module to the input of another.

Example:

verilog
module and_gate ( input wire a, input wire b, output wire c ); assign c = a & b; // AND logic endmodule module or_gate ( input wire a, input wire b, output wire c ); assign c = a | b; // OR logic endmodule module top_module ( input wire x, input wire y, output wire result ); wire and_result; // Instantiate and connect the AND gate and_gate u1 ( .a(x), .b(y), .c(and_result) ); // Instantiate and connect the OR gate or_gate u2 ( .a(and_result), .b(x), .c(result) ); endmodule

In this example, the wire and_result is used to connect the output of the and_gate to the input of the or_gate.

5. Wire Vectors (Multi-bit Wires)

Wires can be declared as vectors (multi-bit wires) to represent buses or wide data paths.

Syntax:

verilog
wire [N:0] wire_name;
  • N:0 defines the width of the wire, where N is the most significant bit (MSB) and 0 is the least significant bit (LSB).
  • A 1-bit wire represents a single line, whereas a multi-bit wire can represent buses (e.g., data buses, address buses).

Example:

verilog
module bus_example ( input wire [7:0] data_in, // 8-bit input wire output wire [7:0] data_out // 8-bit output wire ); assign data_out = data_in; // Pass the 8-bit data endmodule

In this example, both data_in and data_out are 8-bit wide wires.

6. Tri-state Wires (Inout)

A tri-state wire is used for bidirectional data buses, where the wire can either drive a value or remain in a high-impedance (Z) state, allowing other modules to drive the bus.

Example:

verilog
module tri_state_example ( inout wire data, // Bidirectional wire input wire control // Control signal ); assign data = (control) ? 1'bZ : 1'b0; // Tri-state control endmodule

In this example, the data wire is set to high-impedance (1'bZ) when control is high, and drives 0 otherwise.

7. Common Use Cases for Wires

  • Combinational logic: Wires are extensively used for combinational logic where the outputs depend only on the current inputs.
  • Connecting modules: Wires are essential for interconnecting different modules in a hierarchical design.
  • Bus representation: Wires can be used to represent multi-bit buses, allowing for the modeling of wide data paths.
  • Tri-state logic: Wires are also used in tri-state buffers and bidirectional buses, allowing a signal to either be driven or left floating.

8. Differences Between Wire and Reg

Featurewirereg
PurposeRepresents a connection; does not store a valueStores values (used for procedural assignments)
Assigning ValuesDriven by continuous assignments (assign statement) or module outputsAssigned inside procedural blocks (always, initial)
UsageUsed in combinational logic and to connect modulesUsed in sequential logic (e.g., flip-flops, registers)

Summary

In Verilog, wires are fundamental for connecting different parts of a design and modeling combinational logic. Key aspects include:

  • Wires carry signals and cannot store values.
  • Wires are used with continuous assignments (assign statement) for combinational logic.
  • Wire vectors (multi-bit wires) allow you to represent buses or wide data paths.
  • Wires can be used in bidirectional (tri-state) buses.

Understanding how to use wires correctly is essential for effectively describing and connecting hardware components in Verilog designs.