Latest

Verilog Fundamentals · Lesson 2

Modules and Hierarchy: Building a Full Adder

Build a half adder, then connect two of them into a full adder, the way real chips are built from smaller blocks.

4 min read · What you'll learn below

In this lesson you will:
  • Write modules with multiple inputs and outputs
  • Instantiate one module inside another
  • Connect ports by name and avoid common wiring mistakes
  • Test a design exhaustively with a loop in a testbench

Big designs are made of small ones

A processor is not written as one giant block. It is a hierarchy: the CPU contains an ALU, the ALU contains adders, and the adders contain gates. In Verilog, each block is a module, and you place copies of modules inside other modules. This is called instantiation.

In this lesson we’ll build a full adder from two half adders, exactly as you would on paper.

Step 1: the half adder

A half adder adds two bits and produces a sum and a carry:

a b sum carry
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1

So sum = a XOR b and carry = a AND b:

// half_adder.v
module half_adder (
  input  wire a,
  input  wire b,
  output wire sum,
  output wire carry
);
  assign sum   = a ^ b;
  assign carry = a & b;
endmodule

Step 2: the full adder from two half adders

A full adder adds three bits, a, b and a carry-in cin. Use one half adder to add a + b, a second to add that partial sum to cin, and OR the two carries:

// full_adder.v
module full_adder (
  input  wire a,
  input  wire b,
  input  wire cin,
  output wire sum,
  output wire cout
);
  wire s1, c1, c2;   // internal wires connecting the blocks

  half_adder ha1 (.a(a),  .b(b),   .sum(s1),  .carry(c1));
  half_adder ha2 (.a(s1), .b(cin), .sum(sum), .carry(c2));

  assign cout = c1 | c2;
endmodule

Let’s read the instantiation line:

half_adder ha1 (.a(a), .b(b), .sum(s1), .carry(c1));
//  ^module  ^instance name   ^ .port_of_child(signal_in_parent)
  • half_adder is the module being used, and ha1 is the name of this copy. Each copy is separate hardware.
  • .a(a) means “connect the child’s port a to my signal a”.
  • s1, c1 and c2 are internal wires: they connect blocks inside full_adder but are not ports.

Named vs positional connections

Verilog also lets you connect ports by position:

half_adder ha1 (a, b, s1, c1);   // positional: order must match exactly

This is shorter but fragile. If someone reorders the ports of half_adder, every positional instance silently connects the wrong signals. Always use named connections (.port(signal)). Industry coding guidelines require it for exactly this reason.

Step 3: test every input combination

Three inputs give only 2³ = 8 combinations, so we can test all of them. A for loop in the testbench makes this easy:

// full_adder_tb.v
`timescale 1ns/1ps
module full_adder_tb;
  reg  a, b, cin;
  wire sum, cout;
  integer i, errors;

  full_adder dut (.a(a), .b(b), .cin(cin), .sum(sum), .cout(cout));

  initial begin
    errors = 0;
    for (i = 0; i < 8; i = i + 1) begin
      {a, b, cin} = i[2:0];          // unpack i into three bits
      #10;
      if ({cout, sum} !== a + b + cin) begin
        $display("ERROR: a=%b b=%b cin=%b -> cout=%b sum=%b", a, b, cin, cout, sum);
        errors = errors + 1;
      end
    end
    if (errors == 0) $display("PASS: all 8 cases correct");
    $finish;
  end
endmodule

Two useful tricks appear here:

  • {a, b, cin} = i[2:0]; uses concatenation { } to assign three signals at once from the bits of i.
  • {cout, sum} !== a + b + cin compares the two output bits against the arithmetic sum, so the testbench checks itself instead of you reading a table. (!== also catches unknown x values; more on that in the next lesson.)

Run it with all three files:

iverilog -o fa_sim half_adder.v full_adder.v full_adder_tb.v
vvp fa_sim
PASS: all 8 cases correct

Common mistakes

  • Forgetting to declare internal wires. By default, Verilog quietly creates a 1-bit wire for an undeclared name, which hides typos. Add `default_nettype none at the top of your files to turn this into an error.
  • Reusing an instance name. Every instance inside a module needs a unique name (ha1, ha2).
  • Driving an output from two places. Each wire should have exactly one driver.

Try it yourself: build a 4-bit ripple-carry adder by instantiating four full_adders, chaining each cout into the next cin. Ports: input [3:0] a, b, input cin, output [3:0] sum, output cout. (Vectors like [3:0] are covered properly in the next lesson, but try it now.)

Key takeaways

  • Designs are built hierarchically by instantiating modules inside other modules.
  • Each instance is separate hardware with a unique name.
  • Use named port connections and declare every internal wire.
  • Good testbenches check results automatically and cover every case they can.

Next lesson: Verilog’s data types: wire vs reg, vectors and how to write numbers.