HDLBits – Module add

You are given a module add16 that performs a 16-bit addition. Instantiate two of them to create a 32-bit adder. One add16 module computes the lower 16 bits of the addition result, while the second add16 module computes the upper 16 bits of the result, after receiving the carry-out from the first adder. Your 32-bit adder does not need to handle carry-in (assume 0) or carry-out (ignored), but the internal modules need to in order to function correctly. (In other words, the add16 module performs 16-bit a + b + cin, while your module performs 32-bit a + b).

Note: See the HDLBits site for a diagram.


The key to this exercise is just implementing the circuit exactly as it’s depicted on the HDLBits website.

module top_module (
  input  logic [31:0] a, b,
  output logic [31:0] sum );
    // Define internal logic
    logic cout_low;
    logic [15:0] sum_high, sum_low;
    // Module instantiations
    add16 add16_low_i (
      .a    (a[15:0]),
      .b    (b[15:0]),
      .cin  ('0),
      .sum  (sum_low),
      .cout (cout_low) );
    add16 add16_high_i (
      .a    (a[31:16]),
      .b    (b[31:16]),
      .cin  (cout_low),
      .sum  (sum_high),
      .cout () );
    // Assign output logic
    assign sum = {sum_high, sum_low};
endmodule : top_module

The unused cout output for the add16_high_i adder instantiation can either be left unconnected, as it is above, or removed entirely from the port list.

Leave a Reply

Your email address will not be published. Required fields are marked *