Modules and Ports
Section 4.1 explained the purpose of hardware description languages. This section explains the syntax of the Verilog language. Every Verilog design file begins with the module keyword after the file comments. Every digital design, from a basic decoder to a complete system-on-chip, contains modules.
Definition of a Module
A module is an independent block of digital hardware. It contains a defined set of interface connections named ports. The module body defines the functional relationship between inputs and outputs.
A module corresponds directly to a block in a schematic diagram. The body contains three primary constructs:
- Continuous assignments (
assign) for combinational logic expressions. - Procedural blocks (
always,initial) for sequential or complex logic statements. - Module instantiations that connect sub-modules hierarchically.
Hierarchical instantiation organizes complex hardware designs. For example, a 32-bit ALU instantiates four 8-bit slices. A processor top-level module instantiates the ALU, the register file, and the instruction decoder. Signals move across module boundaries through input and output ports.
The ANSI Port Declaration
Verilog provides two syntaxes for a module header. This course uses the ANSI C-style syntax introduced in IEEE 1364-2001. The following example shows a parameterized adder with labeled elements:
`timescale 1ns / 1ps
`default_nettype none
module adder #(
parameter WIDTH = 8 // Parameter with default value
)(
input wire [WIDTH-1:0] a, // Direction, net type, range, name
input wire [WIDTH-1:0] b,
input wire cin,
output wire [WIDTH-1:0] sum,
output wire cout
);
assign {cout, sum} = a + b + cin; // WIDTH+1 bits on left side
endmodule
The module adder statement starts the declaration and specifies the module identifier. Store this module in a file named adder.v. The #(...) block contains optional module parameters. Parameters are compile-time constants that a higher-level module can override.
The (...) block contains the port list. Each port entry defines a direction, a net type, a bit range, and a port name. The endmodule keyword closes the module without a semicolon.
The range [WIDTH-1:0] with a default width of 8 specifies eight bits, indexed [7:0]. Standard hardware designs use the [N-1:0] indexing pattern for an N-bit signal.
Port Directions
The port direction defines the signal flow relative to the module boundary:
| Direction | Driven By | Read By |
|---|---|---|
input |
External circuit | Internal module |
output |
Internal module | External circuit |
inout |
Internal or external | Internal or external |
Most digital designs require only input and output ports. The inout port represents a bidirectional tri-state net. Different drivers control the net at different times while inactive drivers set high impedance (Z). Examples include the I²C data line and parallel external memory buses.
In FPGA designs, use inout ports only at the top-level boundary for physical I/O pins. For internal module communication, use separate input and output nets. Do not use internal bidirectional tri-state signals.
The Verilog-1995 Port Style
Legacy codebases, textbooks, and older IP libraries use the Verilog-1995 syntax:
// Verilog-1995 style: shown for recognition only
module adder (a, b, cin, sum, cout);
parameter WIDTH = 8;
input [WIDTH-1:0] a;
input [WIDTH-1:0] b;
input cin;
output [WIDTH-1:0] sum;
output cout;
assign {cout, sum} = a + b + cin;
endmodule
In the Verilog-1995 syntax, each port name appears twice: once in the port list and once in the declarations. This repetition increases maintenance errors when modifying ports. Always write new designs with the modern ANSI syntax.
Mapping Block Diagrams to Modules
Figure 1 shows a 4-bit adder block. Every incoming arrow becomes an input port. Every outgoing arrow becomes an output port. Arrow widths define the signal bit ranges.
`timescale 1ns / 1ps
`default_nettype none
module adder_4bit (
input wire [3:0] a,
input wire [3:0] b,
input wire cin,
output wire [3:0] sum,
output wire cout
);
assign {cout, sum} = a + b + cin; // 5-bit sum result
endmodule
The port list matches the schematic diagram directly. This direct correspondence simplifies design verification and schematic conversion.
Naming Conventions
Use lowercase letters and underscores for module names, such as adder_4bit, uart_tx, and fifo_sync. Identifiers must start with an alphabetic character or an underscore. For example, 4bit_adder is an invalid identifier. Verilog identifiers are case-sensitive.
Use matching names for the module and its source file. Place one top-level module in each source file to support automated tool flows. Use uppercase characters for parameter names, such as DATA_WIDTH. Use the prefix u_ for module instance names, as described in Section 4.4.
A missing endmodule keyword causes a common syntax error. When endmodule is missing, the parser includes subsequent text in the unclosed module. The compiler then reports an unexpected error in later code. Always verify that every module declaration ends with endmodule.
Module Testbenches
To simulate a module, wrap the module inside a testbench module. A testbench module contains no external ports. The testbench instantiates the device under test (DUT), generates input stimuli in an initial block, and observes output responses:
`timescale 1ns / 1ps
`default_nettype none
module tb_adder_4bit;
reg [3:0] a, b;
reg cin;
wire [3:0] sum;
wire cout;
adder_4bit u_dut (
.a (a),
.b (b),
.cin (cin),
.sum (sum),
.cout(cout)
);
initial begin
a = 4'd5; b = 4'd3; cin = 1'b0; // 5 + 3 = 8
#10;
a = 4'd9; b = 4'd8; cin = 1'b1; // 9 + 8 + 1 = 18, cout = 1
#10;
$finish;
end
endmodule
The .port(signal) syntax creates a named port connection. Always use named port connections instead of positional connections. Named connections prevent wiring errors when port orders change in the module declaration.
Practice Exercise
Design a comparator module. The module accepts two inputs of WIDTH bits (default value is 4). The module produces three output flags: eq, gt, and lt. Write the complete module definition.
Solution
`timescale 1ns / 1ps
`default_nettype none
module comparator #(
parameter WIDTH = 4
)(
input wire [WIDTH-1:0] a,
input wire [WIDTH-1:0] b,
output wire eq,
output wire gt,
output wire lt
);
assign eq = (a == b);
assign gt = (a > b);
assign lt = (a < b);
endmodule
Exactly one output signal remains high at any given time. Verify this condition during functional simulation.
This section defined the module interface structure. The next article examines internal signal types and explains the difference between wire and reg types.