Wednesday, October 24, 2007

16bit BCD adder

sometimes you just want a BCD adder instead of a binary adder. here is a 16bit one that works.

//put the cla and #1 adders together to make them easier to merge
module bcdadd4(
output co,
output [15:0] s,
input [15:0] a,b,
input cin);
wire p_up,g_up;
wire [3:0] p,g;
wire [3:0] carry;

pfa_bcd add[3:0](.p(p),.g(g),.s(s),.a(a),.b(b),.cin({carry[2:0], cin}));

cla localcla(.p_up(p_up),.g_up(g_up),.cout(carry),.p(p),.g(g),.cin(cin));

assign co = carry[3];

module pfa_bcd(output reg p, g, output reg[3:0] s, input cin, input[3:0] a, b);
always @(*) begin
s <= (a+b+cin) % 10;
p <= (a+b == 9);
g <= (a+b > 9);

No comments: