Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 37 ยท Modules and Data Types

Verilog Modules, Ports and Parameters

Learn how Verilog modules are declared and instantiated: port directions, ANSI port style, named connections, parameters, localparam, hierarchy and a runnable C parameter stand-in.

A module is the basic building block of Verilog: a named piece of hardware with a defined interface. Big designs are trees of modules, each instantiating smaller ones. In this lesson you learn how to declare ports, how to instantiate a module, how to make a module configurable with parameters, and how hierarchy works.

Declaring a module

VERILOG
module name (port_list);
    // declarations and behavior
endmodule

Modern code uses ANSI-style ports, where direction, type and width appear inside the port list:

VERILOG
module adder4 (
    input  wire [3:0] a,
    input  wire [3:0] b,
    output wire [4:0] sum
);
    assign sum = a + b;
endmodule

There are three port directions:

Direction Meaning
input Driven from outside; read inside
output Driven inside; read outside
inout Bidirectional (tri-state buses, pads)

A module may also have no ports at all, like a testbench.

The older style lists names in parentheses and declares their directions in the body. It is still valid and you will meet it in older code, but write ANSI style in new code.

Port types

An input is always a net (a wire). An output defaults to a wire too, but must be declared output reg if you assign it inside an always or initial block:

VERILOG
module counter (
    input  wire       clk,
    input  wire       rst_n,
    output reg  [7:0] count
);
    always @(posedge clk or negedge rst_n)
        if (!rst_n) count <= 8'd0;
        else        count <= count + 8'd1;
endmodule

In SystemVerilog you simply write output logic [7:0] count for both cases.

Instantiating a module

To use a module inside another one, create an instance:

VERILOG
module top (
    input  wire [3:0] x, y,
    output wire [4:0] total
);
    adder4 u_add (
        .a   (x),
        .b   (y),
        .sum (total)
    );
endmodule
  • adder4 is the module type; u_add is the instance name (every instance needs one).
  • .a(x) is a named connection: port a of the instance connects to signal x.
  • A positional form adder4 u_add (x, y, total); also works but breaks silently if someone reorders the ports. Prefer named connections.
  • An unconnected output can be written .sum(); an unconnected input should be tied explicitly to a constant.

Every instance creates its own copy of the hardware. Instantiating adder4 twice builds two adders.

flowchart TB
  top["top"] --> u1["u_add : adder4"]
  top --> u2["u_sub : subtractor"]
  u2 --> u3["u_inv : inverter"]

A design hierarchy: each box is an instance, and the top module is the root.

Width matching

If you connect a 4-bit signal to a 5-bit port, Verilog zero-pads or truncates silently by default. This is a classic source of bugs. Simulators and lint tools (Verilator's -Wall) can warn about it. Match widths deliberately.

Parameters

A parameter makes a module configurable, like a template argument.

VERILOG
module adder #(
    parameter WIDTH = 8
) (
    input  wire [WIDTH-1:0] a, b,
    output wire [WIDTH:0]   sum
);
    assign sum = a + b;
endmodule

adder #(.WIDTH(16)) u_wide (.a(p), .b(q), .sum(r));
adder u_default (.a(m), .b(n), .sum(s));   // WIDTH = 8
  • #( ... ) lists the parameters before the ports.
  • Override at instantiation with #(.WIDTH(16)).
  • localparam declares a constant that cannot be overridden from outside. Use it for derived values like localparam DEPTH = 1 << ADDR_BITS;.
  • Parameters are fixed at elaboration time (before simulation or synthesis), so they can set widths and generate hardware.

Hierarchy and signal names

In simulation, you can refer to a signal inside an instance with a dot path, such as top.u_add.sum (a hierarchical reference). It is handy in testbenches and debugging, but not synthesizable across modules in a design.

Stand-in: parameterized width in C

This is not Verilog. It models a parameterized adder: the width decides the mask, and the carry-out appears as bit WIDTH. It shows why a WIDTH-bit adder needs a WIDTH+1-bit sum.

C Stand-in: a WIDTH-parameterized adder (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.

Hands-on idea: reading a hierarchy

Given the design below, how many half_adder blocks are created? Two full_adder instances each contain two half adders, so four in total. Hierarchy multiplies hardware: count instances when estimating area.

VERILOG
module two_bit_adder (input wire [1:0] a, b, output wire [2:0] s);
    wire c0;
    full_adder fa0 (.a(a[0]), .b(b[0]), .cin(1'b0), .sum(s[0]), .cout(c0));
    full_adder fa1 (.a(a[1]), .b(b[1]), .cin(c0),   .sum(s[1]), .cout(s[2]));
endmodule

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which port declaration lets you assign the output inside an always block (Verilog-2001)?

    Procedural assignments need a variable type, so the output is declared reg (or logic in SystemVerilog).
  2. 2. Why prefer named port connections?

    Named connections make the mapping explicit and robust.
  3. 3. How do you override a parameter at instantiation?

    Parameter overrides go in #( ) between the module name and the instance name.
  4. 4. What is localparam for?

    localparam creates constants derived inside the module that instantiators cannot change.

Summary

  • A module is a named block of hardware with input, output and inout ports; use ANSI port lists.
  • Instantiate with named connections; each instance is its own copy of the hardware.
  • Mind width mismatches, which are silently padded or truncated.
  • parameter configures a module at elaboration time; localparam holds fixed derived constants.
  • A design is a hierarchy of instances rooted at the top module.

Next: wire, reg and Other Data Types