verilogsystem-verilogfpgahdlquartus

Verilog full adder


snippet from HDLbits.01xz.net

This is the Verilog question. I have written the code according to the image, but I'm getting mismatch in the output, can I get some assistance?

module add1 ( input a, input b, input cin,   output sum, output cout );
    assign sum = a ^ b ^ cin;
    assign cout = (a & b)|(a & cin)|(b & cin);
endmodule
 
module top_module (
    input [31:0] a,
    input [31:0] b,
    output [31:0] sum
);
    wire carry [31:0]; 
    wire cout1,cout2;
add1 fa1(a[0],b[0],1'b0,sum[0],carry[0]);
genvar i;
    generate
        for(i = 1; i < 16; i = i + 1) begin:adders
            add1 fa2(a[i],b[i],carry[i-1],sum[i],carry[i]);
        end
    endgenerate
    add16 fa2(a[15:0],b[15:0],carry[14],sum[15:0],cout1);
    add16 fa3(a[31:16],b[31:16],carry[15],sum[31:16],cout2);
endmodule

I tried write a separate module for add16, but it didn't work and tried rearranging the code block.


Solution

  • There is never a reason to use anything more complicated than a single line of code for an adder in Verilog:

    module top_module (
        input [31:0] a,
        input [31:0] b,
        output [31:0] sum
    );
        assign sum = a + b;
    endmodule