【问题标题】:How to create an array to store integers in a testbench?如何创建一个数组以在测试台中存储整数?
【发布时间】:2021-09-24 19:35:56
【问题描述】:

我正在检查我所有的 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,但我确实想学习它。

【问题讨论】:

    标签: verilog system-verilog test-bench


    【解决方案1】:

    在 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 Std 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
    

    【讨论】:

      【解决方案2】:

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

      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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-24
        • 2020-11-10
        • 2016-04-28
        • 2019-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多