Module 4: Introduction to Verilog HDL · 4.4 Structural Modeling

Port Connections in Detail

Port connections link higher-level signals to internal submodule terminals. When signal and port widths match, binding is straightforward. When widths differ or multiple instances share wide buses, you must apply explicit connection rules to prevent silent functional bugs.

Port Width Mismatches

Verilog permits bit-width differences between a port and its connected net. Compilers issue warnings and proceed with elaboration. If the connecting signal is wider, the compiler truncates upper bits. If the connecting signal is narrower, the compiler zero-extends upper bits:

wire [7:0] wide_data = 8'hAB;
some_module u1 (.data_in(wide_data));    // 4-bit port receives 4'hB; upper bits discarded

wire [3:0] narrow = 4'hF;
some_module u2 (.data_in(narrow));       // 8-bit port receives 8'h0F (zero-extended)

Unintended width mismatches cause truncated address buses and dropped sign bits. Treat port width warnings as critical errors. Use explicit part-selects to document intentional bus slicing:

some_module u1 (
    .data_in(wide_data[3:0]),     // Explicit slice eliminates compiler warnings
    .ctrl   (narrow),
    .result (result_bus[11:4])
);

Slicing Data Buses Across Parallel Instances

Digital systems frequently slice wide buses across parallel processing units:

`timescale 1ns / 1ps
`default_nettype none

module byte_parallel_processor (
    input  wire [31:0] data_bus,
    output wire [31:0] result_bus
);
    byte_proc u_b0 (.byte_in(data_bus[7:0]),   .byte_out(result_bus[7:0]));
    byte_proc u_b1 (.byte_in(data_bus[15:8]),  .byte_out(result_bus[15:8]));
    byte_proc u_b2 (.byte_in(data_bus[23:16]), .byte_out(result_bus[23:16]));
    byte_proc u_b3 (.byte_in(data_bus[31:24]), .byte_out(result_bus[31:24]));
endmodule

The four instances cover all 32 bits without overlapping bit indices (Figure 1). Each output bit possesses exactly one driving source.

A bus sliced across four instances
Figure 1: Non-overlapping bit slicing of wide data buses across parallel instances

Connecting Sized Constants

Match constant literal bit widths exactly to the target port width:

uart_tx u_uart (
    .clk      (clk),
    .rst_n    (rst_n),
    .baud_div (16'd868),     // Explicit 16-bit constant matches port width
    .parity_en(1'b0),        // Single-bit control flag
    .stop_bits(2'b01),       // 2-bit configuration literal
    .data     (tx_data),
    .start    (tx_start),
    .tx       (tx_pin)
);

To drive a 4-bit input high, write 4'hF rather than 1'b1. The literal 1'b1 zero-extends to 4'h1, leaving upper bits low.

Managing Unused Terminals

  • Unused Outputs: Leave port parentheses empty (.cout()) or connect a dedicated wire. Leaving output ports unconnected is safe; synthesis tools eliminate unread internal logic.
  • Unused Inputs: Tie every unused input port to a defined logic level (1'b0 or 1'b1). Unconnected inputs float at high-impedance (z), which converts to unknown (x) in downstream gates.
adder u_adder (
    .a       (op_a),
    .b       (op_b),
    .cin     (1'b0),      // Input tied low to prevent floating logic
    .sum     (result),
    .overflow()           // Output left empty deliberately
);

The Single-Driver Rule

Standard net types (wire) permit only one active driver per net. Connecting multiple output terminals to a single wire creates electrical contention:

// Defect: Multi-driver contention on single net
wire bad_net;
driver_a u_a (.y(bad_net));
driver_b u_b (.y(bad_net));

// Correct: Separate nets combined through intentional logic
wire out_a, out_b;
driver_a u_a (.y(out_a));
driver_b u_b (.y(out_b));
assign combined = out_a | out_b;

In simulation, opposing drivers resolve to x. In physical silicon, conflicting drivers cause short circuits, high current dissipation, and device degradation.

Declaring Named Intermediate Nets

Declare dedicated intermediate wires rather than placing complex logic expressions directly into port arguments:

wire [3:0] alu_operand_b;      // Operand inverted during subtraction
wire       ripple_carry_in;    // Two's-complement carry-in flag

assign alu_operand_b   = sub_mode ? ~data_b : data_b;
assign ripple_carry_in = sub_mode;

rca_4bit u_adder (
    .a   (data_a),
    .b   (alu_operand_b),
    .cin (ripple_carry_in),
    .sum (result),
    .cout(carry_out)
);

Explicit intermediate nets document design intent, appear in simulation waveform viewers, and simplify static timing path analysis.

Practice Exercise

A nibble_alu module has a SIGNED parameter, 4-bit inputs a and b, a 2-bit control op, and outputs result, carry, and overflow. Instantiate two units:

  1. One unit for low nibbles with SIGNED = 0.
  2. One unit for high nibbles with SIGNED = 1. Combine outputs into an 8-bit byte_result bus, share a 2-bit alu_op, and leave both overflow outputs unconnected.
Solution
wire [7:0] byte_result;
wire [1:0] alu_op;
wire       carry_low, carry_high;

nibble_alu #(.SIGNED(0)) u_low (
    .a       (byte_a[3:0]),
    .b       (byte_b[3:0]),
    .op      (alu_op),
    .result  (byte_result[3:0]),
    .carry   (carry_low),
    .overflow()
);

nibble_alu #(.SIGNED(1)) u_high (
    .a       (byte_a[7:4]),
    .b       (byte_b[7:4]),
    .op      (alu_op),
    .result  (byte_result[7:4]),
    .carry   (carry_high),
    .overflow()
);

Each instance drives an independent 4-bit slice of byte_result. Every output bit satisfies the single-driver rule.

The next article examines hierarchical system design, parameter passing, and iterative module replication using generate loops.

← Module Instantiation Hierarchy and Generate →