3

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.

0

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.