2025-02-03 09:54:19 -07:00
Modules are the building block through which Verilog is built.
Each module can be thought of as a black box with a series of inputs, and a series of outputs. Changing the input changes the outputs.
Module definitions are started with the `module` keyword, and closed with the `endmodule` keyword.
2025-02-03 09:59:19 -07:00
## Syntax
2025-02-03 09:54:19 -07:00
The general syntax of a module is as follows:
```verilog
2025-02-03 11:44:23 -07:00
// This line is referred to as the *module header*
2025-02-03 09:54:19 -07:00
module < name > ([port_list]);
// Contents of the module
endmodule
2025-02-03 09:59:19 -07:00
// The port list is optional
module < name > ;
// Contents
endmodule
```
Below is an example of the structure of a half adder module:
```verilog
module half_adder(
input a,
2025-02-03 11:19:08 -07:00
input b,
2025-02-03 09:59:19 -07:00
output sum_bit,
output carry_bit
);
// ------- snip ------------
endmodule
```
## Ports
Ports are a set of signals that act as input and outputs for a particular module.
There are 3 kinds of ports:
- `input` : Input ports can only receive values from the outside. `input` ports cannot be written to.
- `output` : Output ports can be written to, but not read from.
- `inout` : Inout ports can send *and* receive values.
2025-02-03 11:24:08 -07:00
Ports can be declared in the port list, or in the module body. Ports declared in the port list can optionally omit their type and only declare a name, to be specified within the body of the module:
2025-02-03 11:19:08 -07:00
```verilog
module half_adder(
a,
b,
sum_bit,
carry_bit
);
input a;
input b;
output sum_bit;
output carry_bit;
2025-02-03 11:24:08 -07:00
// ----------- snip -----------
endmodule
```
The full type of a port can also be defined within the portlist:
```verilog
```verilog
module half_adder(
input wire a,
input wire b,
output wire sum_bit,
output wire carry_bit
);
input a;
input b;
output sum_bit;
output carry_bit;
// ----------- snip -----------
2025-02-03 11:19:08 -07:00
endmodule
```
2025-02-03 09:59:19 -07:00
### Port types
2025-02-03 10:04:19 -07:00
If no type is defined, ports are implicitly defined as *nets* of type `wire` .
> In verilog, the term *net* refers to network, and it refers to a connection that joins two or more devices together.
2025-02-03 11:19:08 -07:00
Ports can be a vector type:
```verilog
module test(a, b, c);
2025-02-03 11:24:08 -07:00
input [7:0] a;
input [7:0] b;
output [7:0] c;
// -------- snip ---------
2025-02-03 11:19:08 -07:00
endmodule
2025-02-03 11:29:24 -07:00
```
# Instantiation
2025-02-03 11:34:23 -07:00
Larger designs can be built by using multiple smaller modules.
Modules can be *instantiated* within other modules and ports, and these *instances* can be connected with other signals.
These port connections can be defined by an *ordered list* , or by *name* .
### By Ordered List
```verilog
module submodule (input x, y, z, output o);
// ------- snip -------
endmodule
module parent;
wire a, b, c;
wire o;
// Similar to C, the type of the module is first, followed by
// the name of the module instance.
submodule foo (a, b, c, o);
2025-02-03 11:39:23 -07:00
endmodule
2025-02-03 11:34:23 -07:00
```
### By Name
Ports can also be joined by explicitly defining the name.
2025-02-03 11:39:23 -07:00
Syntactically, this is done with a dot (`.` ), followed by the port name defined by the design, followed by the signal name to connect, wrapped in parenthesis (`.x(a)` ).
```verilog
module submodule (input x, y, z, output o);
// ------------snip-----------------
endmodule
module parent;
wire a, b, c;
wire o;
submodule foo (
.x(a),
.y(b),
.z(c),
.o(o)
);
```
Because association is done by name, the order of definition does not matter.
### Unconnected ports
2025-02-03 11:44:23 -07:00
Ports that are not connected to any wire by the parent module will have a value of high impedance, and is considered unknown/undefined.