//
// Part a) Design halfadder module
//
// Description: A half adder adds two single bits (a and b)
// and produces a sum and a carry output.
module halfadder(
input a, b, // 1-bit inputs
output sum, carry // 1-bit outputs
);
// Assign sum using XOR operation.
assign sum = a ^ b;
// Assign carry using AND operation.
assign carry = a & b;
endmodule
//
// Part b) Design fulladder module
//
// Description: A full adder adds three single bits (a, b, and cin)
// and produces a sum and a carry output (cout).
// It can be built from two half adders and an OR gate.
module fulladder(
input a, b, cin, // 1-bit inputs (cin is carry-in)
output sum, cout // 1-bit outputs (cout is carry-out)
);
// Intermediate wires to connect the half adders.
wire ha1_sum, ha1_carry, ha2_carry;
// First half adder to add input 'a' and 'b'.
halfadder ha1 (
.a(a),
.b(b),
.sum(ha1_sum),
.carry(ha1_carry)
);
// Second half adder to add the sum from the first half adder and the carry-in.
halfadder ha2 (
.a(ha1_sum),
.b(cin),
.sum(sum), // Final sum output
.carry(ha2_carry)
);
// The final carry-out is the OR of the carries from both half adders.
assign cout = ha1_carry | ha2_carry;
endmodule
//
// Part c) Design Top-Level-Entity adder4bit
//
// Description: This module implements a 4-bit ripple-carry adder.
// It uses one halfadder for the least significant bit (LSB) and
// three fulladders for the remaining bits.
module adder4bit(
input [3:0] a, b, // 4-bit input values
output [3:0] sum, // 4-bit sum output
output cout // 1-bit final carry-out
);
// Intermediate wires for the carry between the adders.
wire c0, c1, c2;
// LSB Adder (Bit 0): Use a halfadder since there is no initial carry-in.
halfadder ha (
.a(a[0]),
.b(b[0]),
.sum(sum[0]),
.carry(c0)
);
// Bit 1 Adder: Use a fulladder with the carry from the previous stage.
fulladder fa1 (
.a(a[1]),
.b(b[1]),
.cin(c0),
.sum(sum[1]),
.cout(c1)
);
// Bit 2 Adder: Use a fulladder.
fulladder fa2 (
.a(a[2]),
.b(b[2]),
.cin(c1),
.sum(sum[2]),
.cout(c2)
);
// MSB Adder (Bit 3): Use a fulladder. The cout from this is the final carry.
fulladder fa3 (
.a(a[3]),
.b(b[3]),
.cin(c2),
.sum(sum[3]),
.cout(cout) // Final carry-out of the 4-bit addition
);
endmodule
//
// Testbench for the 4-bit Adder
//
// Description: This module tests the adder4bit module by providing
// various input values and displaying the results.
module adder4bit_tb;
// Declare variables to connect to the 4-bit adder module.
reg [3:0] a_tb, b_tb;
wire [3:0] sum_tb;
wire cout_tb;
// Instantiate the module under test (UUT).
adder4bit uut (
.a(a_tb),
.b(b_tb),
.sum(sum_tb),
.cout(cout_tb)
);
// Initial block to define the sequence of test inputs.
initial begin
// Display a header for the simulation output.
$display("Time\t a\t b\t cout\t sum");
// Test case 1: 3 + 2 = 5
a_tb = 4'd3; b_tb = 4'd2; #10;
$display("%g\t %d\t %d\t %b\t %d", $time, a_tb, b_tb, cout_tb, sum_tb);
// Test case 2: 7 + 1 = 8
a_tb = 4'd7; b_tb = 4'd1; #10;
$display("%g\t %d\t %d\t %b\t %d", $time, a_tb, b_tb, cout_tb, sum_tb);
// Test case 3: 9 + 8 = 17 (results in a carry-out)
a_tb = 4'd9; b_tb = 4'd8; #10;
$display("%g\t %d\t %d\t %b\t %d", $time, a_tb, b_tb, cout_tb, sum_tb);
// Test case 4: 15 + 15 = 30 (maximum values with carry)
a_tb = 4'b1111; b_tb = 4'b1111; #10;
$display("%g\t %d\t %d\t %b\t %d", $time, a_tb, b_tb, cout_tb, sum_tb);
// End the simulation.
$finish;
end
endmodule