简体   繁体   English

四位纹波进位加法器在特定输入上失败

[英]Four Bit Ripple Carry Adder Failing on Specific Inputs

I am implementing a four bit ripple carry adder and I am getting an error for the following tests:我正在实现一个四位纹波进位加法器,但在以下测试中出现错误:

input1 = 1000; input2 = 0110; carry_in = 1
input1 = 1000; input2 = 0001; carry_in = 1

Here is my code:这是我的代码:

four-bit-adder.v四位加法器.v

`include "one-bit-adder.v"
module four_bit_adder(input [0:3] x, input [0:3] y, input carry_in, output [0:3] sum, output carry_out, output overflow);
    wire c1, c2, c3;
    one_bit_adder add1(x[0], y[0], carry_in, sum[0], c1);
    one_bit_adder add2(x[1], y[1], c1, sum[1], c2);
    one_bit_adder add3(x[2], y[2], c2, sum[2], c3);
    one_bit_adder add4(x[3], y[3], c3, sum[3], carry_out);
    assign overflow = c3 ^ carry_out;
endmodule

one-bit-adder.v一位加法器.v

module one_bit_adder (input a, input b, input cin, output s, output cout);
    assign s = (a ^ b) ^ cin;
    assign cout = (a && b) || (a && cin) || (b && cin);
endmodule

four-bit-adder-test.v四位加法器测试.v

`include "four-bit-adder.v"
module four_bit_adder_test;
    reg [0:3]x;
    reg [0:3]y;
    reg carry_in;
    wire [0:3]sum;
    wire carry_out;
    wire overflow;
    four_bit_adder adder(x, y, carry_in, sum, carry_out, overflow);

    initial begin
        if (!$value$plusargs("x=%b", x)) begin
            $display("ERROR: please specify +x=<value> to start.");
            $finish;
        end   
        if (!$value$plusargs("y=%b", y)) begin
            $display("ERROR: please specify +y=<value> to start.");
            $finish;
        end
        if (!$value$plusargs("carry_in=%b", carry_in)) begin
            $display("ERROR: please specify +carry_in=<value> to start.");
            $finish;
        end
        
        $display("Expected: %b + %b + %b = %b", x, y, carry_in, {x + y + carry_in});
        #1 // let output propagate
        if ( {carry_out, sum} == x + y + carry_in ) // concatentates carry_out to the left of sun
            $display("PASSED: sum=%b, carry_out=%b", sum, carry_out);
        else    
            $display("FAILED: sum=%b, carry_out=%b", sum, carry_out);
    end

endmodule

I double checked all my circuits and they look fine.我仔细检查了我所有的电路,它们看起来很好。

Replace [0:3] with [3:0] everywhere in your code (design and testbench), and it will pass.在您的代码(设计和测试平台)的任何地方将[0:3]替换为[3:0] ,它将通过。

The four_bit_adder treats [0] as the LSB, which means your bus signals should do so as well. four_bit_adder[0]视为 LSB,这意味着您的总线信号也应该这样做。

Also, it is more customary to declare signals as [3:0] .此外,更习惯于将信号声明为[3:0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM