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_adderis the module being used, andha1is the name of this copy. Each copy is separate hardware..a(a)means “connect the child’s portato my signala”.s1,c1andc2are internal wires: they connect blocks insidefull_adderbut 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 ofi.{cout, sum} !== a + b + cincompares the two output bits against the arithmetic sum, so the testbench checks itself instead of you reading a table. (!==also catches unknownxvalues; 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 noneat 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.