$rtoi() 不是常量系统函数

$rtoi() is not a constant system function

我想设置计数器常量的大小:

localparam MAX_COUNT = ((debounce_per_ms * clk_freq)) + 1;
parameter MAX_COUNT_UPPER = $rtoi($floor($log10(MAX_COUNT)/$log10(2)));

这在 XST (ise) 和 verilator 上运行良好,但在 Icarus 中我遇到了这个错误:

src/button_deb.v:20: error: $rtoi() is not a constant system function.

我可以使用 "integer" 类型解决这个问题:

parameter integer MAX_COUNT_UPPER = $floor($log10(MAX_COUNT)/$log10(2));

但这在验证器中给我一个警告:

$ verilator -cc src/button_deb.v
%Warning-REALCVT: src/button_deb.v:20: Implicit conversion of real to integer
%Warning-REALCVT: Use "/* verilator lint_off REALCVT */" and lint_on around source to disable this message.
%Error: Exiting due to 1 warning(s)
%Error: Command Failed /usr/local/bin/verilator_bin -cc src/button_deb.v

你觉得有什么好的方法可以做到这一点,并且兼容Icarus、verilator和Xst吗?

$log10(MAX_COUNT)/$log10(2) 是一种在您没有可用的 log2 时计算 log2 的方法。

在具有 $log10() 可用的 Verilog 系统中,您还应该具有 $log2().

floor函数是四舍五入成整数,这样会向下舍入。更常见的是希望四舍五入以了解覆盖该数量的位置所需的位数。

为此我们有 Ceiling log2 $clog2()。这不是您的函数的精确替代品,因为它向上取整,而不是向下取整。

示例:

parameter RAM_DEPTH      = 10;
parameter RAM_ADDR_WIDTH = $clog2(RAM_DEPTH);

以下应该也对您有用(无需制作整数类型):

parameter MAX_COUNT_UPPER = $floor($log10(MAX_COUNT)/$log10(2));