Bcdadd4

2023. 6. 26. 15:57FPGA/HDLBits

728x90

You are provided with a BCD (binary-coded decimal) one-digit adder named bcd_fadd that adds two BCD digits and carry-in, and produces a sum and carry-out.

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

Instantiate 4 copies of bcd_fadd to create a 4-digit BCD ripple-carry adder. Your adder should add two 4-digit BCD numbers (packed into 16-bit vectors) and a carry-in to produce a 4-digit sum and carry out.

module top_module ( 
    input [15:0] a, b,
    input cin,
    output cout,
    output [15:0] sum );
    wire [15:0] cout_tmp;
    
    bcd_fadd fadd(a[3:0], b[3:0], cin, cout_tmp[0], sum[3:0]);
    
    assign cout = cout_tmp[12];
    
    generate 
        genvar i;
        for(i = 4; i < 16 ; i = i + 4) begin :add
            bcd_fadd fadd(a[i+3:i], b[i+3:i], cout_tmp[i-4], cout_tmp[i], sum[i+3:i]);
        end
    endgenerate
    
endmodule

'FPGA > HDLBits' 카테고리의 다른 글

Exams/2012 q1g  (0) 2023.06.26
Exams/m2014 q3  (0) 2023.06.26
Adder100  (0) 2023.06.26
Bcdadd100  (0) 2023.06.26
Adder100i  (0) 2023.06.25