← ProblemsVerilog Language / More Verilog Features

100-digit BCD adder

5%loops-generatearithmetic

In binary-coded decimal (BCD), each decimal digit 0–9 is stored in its own 4-bit nibble, so a 100-digit BCD number occupies 400 bits. You are given a one-digit BCD full adder, bcd_fadd, which adds two BCD digits plus a carry-in and produces a one-digit BCD sum and a carry-out:

module bcd_fadd (
    input [3:0] a,
    input [3:0] b,
    input cin,
    output cout,
    output [3:0] sum );

Instantiate 100 copies of bcd_fadd (a generate loop is the sane way) to build a 100-digit BCD ripple-carry adder: digit i lives in bits [4*i+3 : 4*i] of a, b, and sum; the carry ripples from digit 0 up to digit 99; cout is the carry-out of the last digit.

Note on grading: the test stimulus drives arbitrary bit patterns — including nibbles above 9 that are not valid BCD digits. Don't try to special-case those; the expected behavior is defined as exactly "a chain of 100 bcd_fadd digits", so as long as you wire the provided module into a ripple chain, invalid inputs produce the same (matching) outputs as the reference.

The bcd_fadd definition is included in the starter code below the marker comment — leave it unmodified.