Skip to content

Instantly share code, notes, and snippets.

@yuhei1horibe
Created April 25, 2019 04:56
Show Gist options
  • Select an option

  • Save yuhei1horibe/760001cab644f40929e71a595860d59a to your computer and use it in GitHub Desktop.

Select an option

Save yuhei1horibe/760001cab644f40929e71a595860d59a to your computer and use it in GitHub Desktop.
module half_adder(
input a,
input b,
output y,
output cout);
assign cout = a & b; // Carry over
assign y = a ^ b;
endmodule
module full_adder(
input a,
input b,
input cin,
output y,
output cout
);
wire xor_ab;
assign xor_ab = a ^ b;
assign y = xor_ab ^ cin;
assign cout = (xor_ab & cin) | (a & b);
endmodule
module adder4(
input [3:0]a,
input [3:0]b,
output [4:0]y);
wire carry[3:0];
half_adder U_0(
.a (a[0]),
.b (b[0]),
.y (y[0]),
.cout (carry[0]));
generate
genvar i;
for(i = 1; i < 4; i = i + 1) begin
full_adder U_i(
.a (a[i]),
.b (b[i]),
.cin (carry[i - 1]),
.y (y[i]),
.cout (carry[i]));
end
endgenerate
assign y[4] = carry[3];
endmodule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment