hdlbits-Bcdadd100

Bcdadd100

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

1
2
3
4
5
6
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 to create a 100-digit BCD ripple-carry adder. Your adder should add two 100-digit BCD numbers (packed into 400-bit vectors) and a carry-in to produce a 100-digit sum and carry out.

Hint

An instance array or generate statement would be useful here.

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module top_module( 
input [399:0] a, b,
input cin,
output cout,
output [399:0] sum );

reg [399:0] cout_inside;
assign cout = cout_inside[396];
bcd_fadd fadd(a[3:0], b[3:0], cin, cout_inside[0], sum[3:0]);

generate
genvar i;
for (i=4;i<400;i=i+4) begin :add100
bcd_fadd fadd(a[i+3:i], b[i+3:i], cout_inside[i-4], cout_inside[i], sum[i+3:i]);
end
endgenerate
endmodule

hdlbits-Bcdadd100
http://456-xiao.github.io/2024/08/25/hdlbits-Bcdadd100/
作者
xyh
发布于
2024年8月25日
许可协议