Verilog HDL 支持 $clog2 任务吗?

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

当我在程序中使用它时,生成了错误(不支持

$clog2
)。但我看到我们的 StackOverflowers 在他们的程序中使用了
$clog2
任务。请告诉我如何使用它。

verilog fpga system-verilog
4个回答
7
投票
Verilog 不支持

$clog2
。这是一个 SystemVerilog 系统任务。此外,系统任务是“不可综合的”。 但是,您可以创建一个

函数/宏

来输出给定数字的 log bsae 2 值。以下是一些用户定义实现的示例: 使用宏:

`define CLOG2(x) \ (x <= 2) ? 1 : \ (x <= 4) ? 2 : \ (x <= 8) ? 3 : \ (x <= 16) ? 4 : \ (x <= 32) ? 5 : \ (x <= 64) ? 6 : \ ..etc, .. (x <= 4294967296) ? 32 : \ -1 parameter MAX_VALUE = 42; parameter MAX_WIDTH = `CLOG2(MAX_VALUE);

使用
function


function [31:0] log2; input [31:0] value; integer i; reg [31:0] j; begin j = value - 1; log2 = 0; for (i = 0; i < 31; i = i + 1) if (j[i]) log2 = i+1; end endfunction initial begin $display("10 = %d", log2(10)); end

上面的两个例子都会产生可合成的代码。用户可以根据最大位宽要求扩展此代码。

因此,您可以更改编译器来编译SystemVerilog代码,也可以实现上述函数来制作用户自定义的日志代码。


6
投票
$clog2

is
受 Verilog 支持,但仅支持 Verilog-2005 (IEEE Std 1364-2005)。由于Verilog-2005与IEEE的SystemVerilog同时发布,因此通常被认为是SystemVerilog的增强。以下是两个将 $clog2 记录为 Verilog-2005 功能的源代码:

Sutherland HDL -
    打破 SystemVerilog 仅用于验证的神话
  • § 9.6 表达式大小函数 Xilinx -
  • Verilog $clog2 函数实施不当
  • Verilog-2005 主要是 Verilog 和 SystemVerilog 最终合并的中间版本(发生在 IEEE Std 1800-2009 中)。有些模拟器可能没有植入Verilog-2005,因为其中的所有内容都包含在SystemVerilog中。如果您的模拟器默认不运行 Verilog-2005,请参阅您的手册,其中可能包含启用它的选项。启用 SystemVerilog 是另一种选择,或者是 sharvil111 解决方案中描述的用户方法。


1
投票

例如,下面是一个可综合的语句:

logic [$clog2(WIDTH) - 1 : 0] addr;

    


0
投票

否则它可以在模拟中支持,但不可综合。

© www.soinside.com 2019 - 2024. All rights reserved.