在modelsim工具中使用shortreal + shortrealtobits + bitstoshortreal组合时的模拟不匹配

问题描述 投票:0回答:1

这是最小的可重现问题:

文件:top.sv

module top(input [31:0] in1, output [31:0] out1);
    assign out1 = in1;
endmodule

文件 top_tb.sv

module top_tb;

shortreal in1_real;
shortreal out1_tb;
shortreal out_dut;

logic [31:0] in1_logic;
logic [31:0] out1_logic;
logic check;

initial in1_real = 2.1;
initial out1_tb = 2.1;


assign #5 in1_logic = $shortrealtobits(in1_real);

top inst_top(.in1(in1_logic),
          .out1(out1_logic));

assign #10 out_dut = $bitstoshortreal(out1_logic);
assign #30 check = 1;

always_comb begin
    if ((check == 1) && (out1_tb != out_dut)) begin
        $display("%t Not matching!!", $time);
    end
end

endmodule

当我在 modelsim 中运行这个 dut 和 tb 的模拟时,我并不期望

out1_tb
out_dut
之间有任何不匹配,但我看到不匹配。当我检查波形时,
out1_tb
out_dut
仅显示为 2.1,我仍然看到模拟不匹配。

使用设计和 tesbench 代码链接到 EDA Playground:https://edaplayground.com/x/WDiC

点击“运行”时,可以在日志中看到“不匹配”

floating-point verilog simulation system-verilog modelsim
1个回答
0
投票

比较实际值时,应使用公差值。例如,我在下面的代码中将公差设置为 0.1。如果您的数字之间的差异的绝对值大于 0.1,则认为这些数字彼此不同。

always @(posedge clk) begin
    if ((check == 1) && compare(out1_tb, out_dut)) begin
    //if ((check == 1) && (out1_tb != out_dut)) begin
        $display("%t Not matching!! %g %g", $time, out1_tb, out_dut);
    end
end

function bit compare (shortreal r1, r2);
    shortreal diff = (r1>=r2) ? r1-r2 : r2-r1; // Absolute value
    return (diff > 0.1) ? 1 : 0;
endfunction
© www.soinside.com 2019 - 2024. All rights reserved.