Verilog中8位Carry Lookahead Adder的问题

时间:2018-05-22 04:17:49

标签: verilog

https://github.com/reduxjs/redux/blob/master/docs/advanced/ExampleRedditAPI.md 我是Verilog编程的新手。我试图将8位Carry Lookahead Adder放在一起,作为构建64位CLA的一步。基本上,我实现它的方式是使用2个4位CLA"块"创建8位CLA。我将提供我的代码,然后解释我遇到的问题。

以下代码:

// 4-BIT CLA CODE
module CLA4Bit(A, B, carryIn, carryOut, PG, GG, Sum);
    input[3:0] A, B;
    input carryIn;
    output carryOut;

    output PG;
    output GG;

    output[3:0] Sum;

    wire[3:0] G, P, C;

    assign G = A & B;
    assign P = A ^ B;
    assign Sum = P ^ C;

    assign C[0] = carryIn;

    assign C[1] = G[0] | (P[0] & C[0]);
    assign C[2] = G[1] | (P[1] & G[0]) | (P[1] & P[0] & C[0]);
    assign C[3] = G[2] | (P[2] & G[1]) | (P[2] & P[1] & G[0]) | (P[2] & P[1] & P[0] & C[0]);

    assign PG = P[3] & P[2] & P[1] & P[0];
    assign GG = G[3] | (P[3] & G[2]) | (P[3] & P[2] & G[1]) | (P[3] & P[2] & P[1] & G[0]);
endmodule

// 8-BIT CLA CODE BELOW
module CLA8Bit(A, B, carryIn, carryOut, Sum);

    // 8-bit wire for the inputs A and B
    input[7:0] A, B;

    // Wire for the ORIGINAL carry-in
    input carryIn;

    // Wire for the carryOut
    output carryOut;

    // Wire that carries the Sum of this CLA
    output[7:0] Sum;

    // Wires for the propagate of the first 4-bit block (p3)
    // and the second (p7)
    wire p3, p7;

    // Wires for the generate of the first 4-bit block (g3)
    // and the second (g7)
    wire g3, g7;

    // Wires for the carry of the first block (c3) and the
    // second (c7)
    wire c3, c7;

    // The two 4-bit CLA blocks that make up the 8-bit CLA

    CLA4Bit block1(A[3:0], B[3:0], carryIn, c3, p3, g3, Sum[3:0]);

    CLA4Bit block2(A[7:4], B[7:4], c3, c7, p7, g7, Sum[7:4]);
endmodule

我写了一个基本的测试平台来测试我的代码:

module CLA_TB();

// TEST THE 8-BIT CLA

    // Inputs
    reg[7:0] A;
    reg[7:0] B;
    reg carryIn;

    // Outputs
    wire carryOut;
    wire[7:0] Sum;
    wire PG;
    wire GG;

    // Instantiate the 8-bit CLA
    CLA8Bit CLA8BitDUT (
    .A(A),
    .B(B),
    .carryIn(carryIn),
    .carryOut(carryOut),
    .Sum(Sum)
    );

    // Initialize the testbench signals
    initial
        begin

        // Start with the carryIn set to 0
        assign carryIn = 0;

        // The standard first test. Set
        // A = b0000 0001 and B = b0000 0001
        // Answer should be Sum = b0000 0010
        assign A = 8'b00000001;
        assign B = 8'b00000001;

        #20

        // Next, set A = b0001 1011 and
        // B = b1101 0111. Answer should
        // be Sum = b1111 0010 = hF2.
        assign A = 8'b00011011;
        assign B = 8'b11010111;

        #20

        // Finally, try setting the carryIn
        // to 1 and then test A = b0111 1011
        // and B = b1101 0011. Answer should be
        // Sum = 0100 1111 w/ overflow carry
        assign carryIn = 1'b1;
        assign A = 8'b01111011;
        assign B = 8'b11010011;

        #20

        $finish;

        end

endmodule

所以问题是,在我的测试平台模拟中(我使用ModelSim),Sum的前4位(对应于8位CLA模块中的第一个4位CLA实例)被赋予X在Wave页面中。不过,第二个4位添加得很好。

在做了一些研究之后,我发现当一根导线有多个驱动器(信号源?)时,Verilog会显示X&#39。但是,我没有看到任何地方向8位CLA模块中的第一个4位CLA实例发送多个信号。此外,如果这样的事情是原因,那么我也不知道为什么它不会发生在第二组4位上,因为两个4位CLA的设置非常相似。 / p>

为什么会这样?

1 个答案:

答案 0 :(得分:1)

  当导线有多个驱动程序时,

X显示在Verilog中

这是真的,但这只是故事的一部分。还有其他情况会产生X'es:

  • 如果没有给出一个值,那么它将是X.
  • 如果在表达式中使用Z,则会生成X.

您的波形中有一些明显的“Z”(蓝色)线。
如果您将信号关注回原点:您的4位加法器永远不会为carryOut赋值。
然后你在CLA8Bit中犯了同样的错误。

如果你在模拟中看到'Z':跳过它! 99.9%的时间你有一根没有给出价值的电线!