【问题标题】:Trouble with 8-bit Carry Lookahead Adder in VerilogVerilog 中 8 位进位超前加法器的问题
【发布时间】:2018-05-22 04:17:48
【问题描述】:

我是 Verilog 编程的新手。我正在尝试组合一个 8 位进位超前加法器,作为构建 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 位相加就好了。

经过一番研究,我发现当一根电线有多个驱动器(信号源?)时,X 会显示在 Verilog 中。但是,在 8 位 CLA 模块中,我没有看到任何地方向我的第一个 4 位 CLA 实例发送多个信号。另外,如果是这样的原因,那么我不知道为什么第二组 4 位也不会发生这种情况,因为两个 4 位 CLA 的设置非常相似。

为什么会这样?

【问题讨论】:

    标签: verilog


    【解决方案1】:

    当电线有多个驱动程序时,X 会显示在 Verilog 中

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

    • 如果没有给 reg 赋值,它将是 X。
    • 如果在表达式中使用 Z,它将产生一个 X。

    您的波形有一些明显的“Z”(蓝色)线。
    如果您跟踪信号返回它们的来源:您的 4 位加法器永远不会为 carryOut 分配值。
    然后你在 CLA8Bit 中犯了同样的错误。

    如果您在模拟中看到“Z”:跳上去! 99.9% 的时间你的电线没有被赋予价值!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多