A 4-bit ALU from the Gates Up
This article implements the 4-bit arithmetic logic unit (ALU) from Section 2.2 using purely structural Verilog. We construct the design hierarchically from low-level gate primitives up to the complete ALU datapath (Figure 1). Each sub-module is independently verified before higher-level integration.
1. Half Adder
The half adder computes single-bit sum and carry outputs:
$$sum = a \oplus b \qquad c_{out} = ab$$
`timescale 1ns / 1ps
`default_nettype none
module half_adder (
input wire a, b,
output wire sum,
output wire cout
);
xor g_xor (sum, a, b);
and g_and (cout, a, b);
endmodule
The half adder uses two gate primitives. It serves as the elementary building block for full adders.
2. 1-Bit Full Adder
The 1-bit full adder combines two half adders and a single OR gate:
`timescale 1ns / 1ps
`default_nettype none
module full_adder (
input wire a, b, cin,
output wire sum,
output wire cout
);
wire s1; // a XOR b
wire c1; // a AND b
wire c2; // s1 AND cin
half_adder u_ha1 (.a(a), .b(b), .sum(s1), .cout(c1));
half_adder u_ha2 (.a(s1), .b(cin), .sum(sum), .cout(c2));
or g_or (cout, c1, c2);
endmodule
The internal carry conditions c1 and c2 are mutually exclusive. When c1 equals 1, s1 equals 0, forcing c2 to 0. A basic 2-input OR gate combines both carry terms correctly.
3. 4-Bit Ripple-Carry Adder-Subtractor
In two’s-complement arithmetic, subtraction equals $a + \overline{b} + 1$. Bitwise XOR gates conditionally invert operand b when control signal sub asserts high:
`timescale 1ns / 1ps
`default_nettype none
module add_sub_4bit (
input wire [3:0] a, b,
input wire sub, // 0 = Add; 1 = Subtract
output wire [3:0] result,
output wire cout,
output wire overflow // Signed arithmetic overflow flag
);
wire [3:0] b_xored;
wire c0, c1, c2, c3, c4;
assign c0 = sub; // Initial carry-in provides +1 for two's complement
xor g_xor0 (b_xored[0], b[0], sub);
xor g_xor1 (b_xored[1], b[1], sub);
xor g_xor2 (b_xored[2], b[2], sub);
xor g_xor3 (b_xored[3], b[3], sub);
full_adder u_fa0 (.a(a[0]), .b(b_xored[0]), .cin(c0), .sum(result[0]), .cout(c1));
full_adder u_fa1 (.a(a[1]), .b(b_xored[1]), .cin(c1), .sum(result[1]), .cout(c2));
full_adder u_fa2 (.a(a[2]), .b(b_xored[2]), .cin(c2), .sum(result[2]), .cout(c3));
full_adder u_fa3 (.a(a[3]), .b(b_xored[3]), .cin(c3), .sum(result[3]), .cout(c4));
assign cout = c4;
// Signed overflow occurs when carry-in to MSB differs from carry-out of MSB
xor g_ov (overflow, c3, c4);
endmodule
In 4-bit signed arithmetic, valid values span $-8$ to $+7$. Adding $+5$ and $+3$ produces $+8$, which overflows to $-8$. The XOR gate g_ov compares carry bits c3 and c4 to assert the signed overflow flag.
4. 4-Bit Bitwise Logic Unit
The logic unit computes four parallel bitwise operations using gate arrays:
`timescale 1ns / 1ps
`default_nettype none
module logic_unit_4bit (
input wire [3:0] a, b,
output wire [3:0] out_and,
output wire [3:0] out_or,
output wire [3:0] out_xor,
output wire [3:0] out_not
);
and g_and [3:0] (out_and, a, b); // Synthesizes 4 parallel AND gates
or g_or [3:0] (out_or, a, b); // Synthesizes 4 parallel OR gates
xor g_xor [3:0] (out_xor, a, b); // Synthesizes 4 parallel XOR gates
not g_not [3:0] (out_not, a); // Synthesizes 4 parallel inverters
endmodule
The bracketed range [3:0] after the instance name instantiates four parallel logic gates in a single concise statement.
5. Top-Level 4-Bit ALU Module
The top-level module instantiates arithmetic and logic units, multiplexing the selected output based on operational code op:
`timescale 1ns / 1ps
`default_nettype none
module alu_4bit (
input wire [3:0] a, b,
input wire [2:0] op,
output reg [3:0] result,
output wire zero,
output wire cout_arith,
output wire overflow
);
wire [3:0] add_result, sub_result;
wire cout_add, overflow_add;
wire cout_sub, overflow_sub;
wire [3:0] and_result, or_result, xor_result, not_result;
add_sub_4bit u_adder (
.a (a),
.b (b),
.sub (1'b0),
.result (add_result),
.cout (cout_add),
.overflow(overflow_add)
);
add_sub_4bit u_subber (
.a (a),
.b (b),
.sub (1'b1),
.result (sub_result),
.cout (cout_sub),
.overflow(overflow_sub)
);
logic_unit_4bit u_logic (
.a (a),
.b (b),
.out_and(and_result),
.out_or (or_result),
.out_xor(xor_result),
.out_not(not_result)
);
// Multiplexer selecting active operation
always @(*) begin
case (op)
3'b000: result = add_result;
3'b001: result = sub_result;
3'b010: result = and_result;
3'b011: result = or_result;
3'b100: result = xor_result;
3'b101: result = not_result;
default: result = 4'h0;
endcase
end
assign zero = ~|result;
assign cout_arith = op[0] ? cout_sub : cout_add;
assign overflow = op[0] ? overflow_sub : overflow_add;
endmodule
Operation Code (op) |
Selected Function |
|---|---|
3'b000 |
Addition ($a + b$) |
3'b001 |
Subtraction ($a - b$) |
3'b010 |
Bitwise AND ($a \ & \ b$) |
3'b011 |
Bitwise OR ($a \ | \ b$) |
3'b100 |
Bitwise XOR ($a \oplus b$) |
3'b101 |
Bitwise NOT ($\sim a$) |
In physical hardware, all sub-modules evaluate continuously in parallel. The output multiplexer selects the active calculation result.
Signal Trace: Addition of 5 and 3
Let a = 4'b0101 (5), b = 4'b0011 (3), and op = 3'b000:
- Inside
u_adder, control netsubis 0. Vectorb_xoredequals4'b0011, and initial carryc0is 0. - Bit 0: Inputs
a=1,b=1,cin=0. Generatessum=0, carryc1=1. - Bit 1: Inputs
a=0,b=1,cin=1. Generatessum=0, carryc2=1. - Bit 2: Inputs
a=1,b=0,cin=1. Generatessum=0, carryc3=1. - Bit 3: Inputs
a=0,b=0,cin=1. Generatessum=1, carryc4=0.
The addition produces add_result = 4'b1000 (decimal 8). Flag zero evaluates to 0. Unsigned carry cout_arith is 0. Signed overflow evaluates to c3 ^ c4 = 1 ^ 0 = 1. In 4-bit signed two’s-complement arithmetic, $+8$ exceeds the allowable positive range, correctly asserting the overflow flag.
Complete Verification Testbench
`timescale 1ns / 1ps
`default_nettype none
module tb_alu_4bit;
reg [3:0] a, b;
reg [2:0] op;
wire [3:0] result;
wire zero, cout_arith, overflow;
alu_4bit u_dut (
.a (a),
.b (b),
.op (op),
.result (result),
.zero (zero),
.cout_arith (cout_arith),
.overflow (overflow)
);
initial begin
$dumpfile("alu_waves.vcd");
$dumpvars(0, tb_alu_4bit);
a = 4'd5; b = 4'd3; op = 3'b000; #10;
$display("ADD: %0d + %0d = %0d (zero=%b, cout=%b, ov=%b)",
a, b, result, zero, cout_arith, overflow);
a = 4'd15; b = 4'd1; op = 3'b000; #10;
$display("ADD: %0d + %0d = %0d (zero=%b, cout=%b, ov=%b)",
a, b, result, zero, cout_arith, overflow);
a = 4'd5; b = 4'd3; op = 3'b001; #10;
$display("SUB: %0d - %0d = %0d (zero=%b, cout=%b, ov=%b)",
a, b, result, zero, cout_arith, overflow);
a = 4'd4; b = 4'd4; op = 3'b001; #10;
$display("SUB: %0d - %0d = %0d (zero=%b, cout=%b, ov=%b)",
a, b, result, zero, cout_arith, overflow);
a = 4'hA; b = 4'hC; op = 3'b010; #10;
$display("AND: 0x%h & 0x%h = 0x%h (zero=%b)", a, b, result, zero);
a = 4'hA; b = 4'hC; op = 3'b011; #10;
$display("OR: 0x%h | 0x%h = 0x%h (zero=%b)", a, b, result, zero);
a = 4'hA; b = 4'hC; op = 3'b100; #10;
$display("XOR: 0x%h ^ 0x%h = 0x%h (zero=%b)", a, b, result, zero);
a = 4'hA; b = 4'h0; op = 3'b101; #10;
$display("NOT: ~0x%h = 0x%h (zero=%b)", a, result, zero);
#5;
$display("Internal carry c2 in u_adder: %b", tb_alu_4bit.u_dut.u_adder.c2);
$finish;
end
endmodule
Simulation Command and Console Log
iverilog -o alu_sim half_adder.v full_adder.v add_sub_4bit.v logic_unit_4bit.v alu_4bit.v tb_alu_4bit.v
vvp alu_sim
ADD: 5 + 3 = 8 (zero=0, cout=0, ov=1)
ADD: 15 + 1 = 0 (zero=1, cout=1, ov=0)
SUB: 5 - 3 = 2 (zero=0, cout=1, ov=0)
SUB: 4 - 4 = 0 (zero=1, cout=1, ov=0)
AND: 0xa & 0xc = 0x8 (zero=0)
OR: 0xa | 0xc = 0xe (zero=0)
XOR: 0xa ^ 0xc = 0x6 (zero=0)
NOT: ~0xa = 0x5 (zero=0)
Internal carry c2 in u_adder: 1
Practice Exercise
Extend the 4-bit ALU by adding bitwise NAND (op = 3'b110) and bitwise NOR (op = 3'b111) functions.
Solution
- Update
logic_unit_4bitwith two new output ports and gate arrays:
output wire [3:0] out_nand,
output wire [3:0] out_nor
);
nand g_nand [3:0] (out_nand, a, b);
nor g_nor [3:0] (out_nor, a, b);
- Update
alu_4bitby adding intermediate nets and multiplexer cases:
wire [3:0] nand_result, nor_result;
logic_unit_4bit u_logic (
.a (a),
.b (b),
.out_and (and_result),
.out_or (or_result),
.out_xor (xor_result),
.out_not (not_result),
.out_nand(nand_result),
.out_nor (nor_result)
);
always @(*) begin
case (op)
3'b000: result = add_result;
3'b001: result = sub_result;
3'b010: result = and_result;
3'b011: result = or_result;
3'b100: result = xor_result;
3'b101: result = not_result;
3'b110: result = nand_result;
3'b111: result = nor_result;
endcase
end
This completes structural modeling. Section 4.5 introduces dataflow modeling using continuous assign statements and logic expressions.