如何创建一个数组来存储测试台中的整数?

How to create an array to store integers in a testbench?

我正在检查我所有的 Verilog 模块并为它们创建良好的测试平台。我已经完成了更大的项目,但我想更好地编写测试平台并将测试平台上传到项目存储库。

我有以下代码用于测试我拥有的模块的测试平台。该模块采用 16 位输入,将其修整为 10 位(来自加速度计的数据输入,仅使用 10 位,但输入是 16 位以保持一致性),然后通过输出数字将 10 位有符号数转换为十进制个位、十位、百位和千位。

这是我的测试台代码(注意:Decimal_Data 代表“可用”的 10 位数据):

module Binary_to_Decimal_TB();

reg [15:0] Accel_Data = 16'd0;

wire [3:0] ones;
wire [3:0] tens;
wire [3:0] hundreds;
wire [3:0] thousands;
wire negative;

wire [9:0] Decimal_Data;

integer i;

integer j = 0;

Binary_to_Decimal BtD(.Accel_Data(Accel_Data), .Decimal_Data(Decimal_Data),
.ones(ones), .tens(tens), .hundreds(hundreds), .thousands(thousands), .negative(negative)); 

initial
begin
    for (i = 0; i < 2047; i = i + 1)
    begin
        Accel_Data = i;
        #2;
        if (Decimal_Data < 512)
        begin
            if (((ones * 1) + (tens * 10) + (hundreds * 100) + (thousands * 1000)) != 4 * Decimal_Data)
                j = j + 1;
        end
        else if (Decimal_Data == 512)
        begin
            if (((ones * 1) + (tens * 10) + (hundreds * 100) + (thousands * 1000)) != 0)
                j = j + 1;
        end
        else
        begin
            if (((ones * 1) + (tens * 10) + (hundreds * 100) + (thousands * 1000)) != 2048 - (4 * (Decimal_Data - 512)))
                j = j + 1;   
        end
    end
    $display("Conversion mismatches:", j);
    $finish;
end
endmodule

它正常工作,转换后的数据表示的数字与原始 2 的补码 10 位数字之间的不匹配为零。

但是,我想这样写,如果有错误,它会在输入和输出不匹配的地方保存二进制数。在 C++ 中,我会创建一个数组,并在检测到不匹配的地方用二进制数动态分配它(因为我们不知道如果设计有问题会有多少不匹配)。

更清楚,现在我可以看到发生了多少不匹配错误。我想实现一种方法来查看 错误发生的位置

我知道我会在递增 j 的条件下写入数组,但我不知道如何在 Verilog 中创建用于此目的的数组。

另外,我听说 SystemVerilog 更适合验证,所以也许 SystemVerilog 中有一些东西可以用来完成这个?我还没有真正研究过 SystemVerilog,但我确实想学习它。

在 SystemVerilog 中,您可以创建一个动态队列来存储不匹配的结果。

logic [15:0] mismatches [$];
...
        if (Decimal_Data < 512)
        begin
            if (((ones * 1) + (tens * 10) + (hundreds * 100) + (thousands * 1000)) !== 4 * Decimal_Data) begin
                j = j + 1;
                mismatches.push_back(Accel_Data);
            end
        end
...
    $display("Conversion mismatches:", j);
    foreach (mismatches[i]) $display(mismatches[i]);
    $finish;

请参阅 IEEE 标准 1800-2017,第 7.10 节队列


由于您正在比较 4 个状态值,因此您应该使用大小写不等式运算符 (!==),如上所示。此外,由于您正在相互比较 DUT 输出,因此您还应该使用 $isunknown.

检查是否存在 xz

此外,您的测试台中有通用代码,可以组合使用。当您添加更多检查代码时,这一点尤其重要。例如:

logic [15:0] mismatches [$];
integer expect_data;

initial
begin
    for (i = 0; i < 2047; i = i + 1)
    begin
        Accel_Data = i;
        #2;

        if (Decimal_Data < 512)
        begin
            expect_data = 4 * Decimal_Data;
        end
        else if (Decimal_Data == 512)
        begin
            expect_data = 0;
        end
        else
        begin
            expect_data = 2048 - (4 * (Decimal_Data - 512));
        end

        if (((ones * 1) + (tens * 10) + (hundreds * 100) + (thousands * 1000)) !== expect_data) begin
            j = j + 1;
            mismatches.push_back(Accel_Data);
        end
    end
    $display("Conversion mismatches:", j);
    foreach (mismatches[i]) $display(mismatches[i]);
    $finish;
end
endmodule

出于好奇,为什么不打印出现的不匹配?无需存储它们并在以后打印出来,因为它们都是非综合代码。示例:

logic [15:0] mismatches [$];
...
        if (Decimal_Data < 512)
        begin
            if (((ones * 1) + (tens * 10) + (hundreds * 100) + (thousands * 1000)) !== 4 * Decimal_Data) begin
                j = j + 1;
                $display("ERROR: Mistmatch (expected=%d) (actual=%d) @%t", 4*Decimal_data, ones+(tens*10)+(hundreds*100)+(thousands*1000), $time);
            end
        end
...
    $display("Conversion mismatches:", j);
    $finish;