Home / Learn Verilog / Your First Module: Ports, Assign and Instantiation

Your First Module: Ports, Assign and Instantiation

Write a small module, then use it from a testbench.

The short versionWrite a small module, then use it from a testbench.

Theory in plain words

half_adder modulehalf_adderainputbsumoutputcarry
half_adder module

In plain terms

A module declares its ports and describes behaviour. Another module instantiates it, like placing a chip on a board and wiring its pins.

Why we use it

Every Verilog design is a tree of modules. If you understand one module and its instance, you understand the whole language structure.

Under the hood

Declare ports in the module header. Inside, describe behaviour with assign or always. Instantiate with the module name, an instance name and the connections.

Real-world use

  • All designs
  • Reusable building blocks
  • Hierarchical design

Half adder

half_adder + testbenchVerilog: edit and run

The console prints the truth table. The instance is named ha. The connections .a(a) mean "module port a is connected to signal a".

  1. Write module name(ports);
  2. Describe behaviour (assign or always).
  3. End with endmodule.
  4. In another module, instantiate: name inst(.port(signal), ...);
Careful:Do not forget the semicolon after the port list and after each assign. Missing semicolons are the most common beginner error.

Quick quiz

1. half adder sum is...

2. A module instance needs...