﻿/*
１ビットのフルアダー(全加算器)を４つ使用した
４ビットリップルキャリーアダー

最下位のキャリー入力はゼロで、各桁のキャリー出力が
上位のキャリー入力となっている
*/

module adder_ripple( a, b, q, co );
input   [3:0]   a, b;
output  [3:0]   q;
output  co; 
wire    [3:0]   cout;

fulladd add0 ( a[0], b[0],    1'b0, q[0], cout[0] );
fulladd add1 ( a[1], b[1], cout[0], q[1], cout[1] );
fulladd add2 ( a[2], b[2], cout[1], q[2], cout[2] );
fulladd add3 ( a[3], b[3], cout[2], q[3], cout[3] );

assign co = cout[3];

endmodule

// フルアダー
module fulladd( A, B, CIN, Q, COUT );
input   A, B, CIN;
output  Q, COUT;

assign Q    = A ^ B ^ CIN;
assign COUT = (A & B) | (B & CIN) | (CIN & A);

endmodule
