Verilog-位宽计算的系统函数$clog2
我的学记|刘航宇的博客

Verilog-位宽计算的系统函数$clog2

刘航宇
2年前发布 /正在检测是否收录...
温馨提示:
本文最后更新于2023年03月13日,已超过743天没有更新,若内容或图片失效,请留言反馈。

一、什么是$clog2

clog2 这是一个系统函数,第一次出现于Verilog-2005版本中,在IEEE中处在17.11.1节的math functions中,因为log2是2进制的对数,所以这个系统函数在电路设计的计算位宽时体现出了自身的方便性,需要注意的是,这里的$clog2是向上取整的一个系统函数,比如

二、$clog2的优势和案例

在老的IEEE verilog版本中,假如不用clog2去计算位宽,我们可能需要如下的function函数来进行位宽计算,这个函数本身很好理解,即通过移位去检测depth的位宽,之后我们需要再将计算得到的数字使用在端口定义的过程中。

function integer clog2(
    input           integer depth
    );
begin
    if(depth == 0)
        clog2 = 1;
    else if(depth != 0)
        for(clog2 = 0; depth > 0;clog2 = clog2 + 1)
            depth = depth >> 1;
end
endfunction

但是引入$clog2后,原function可以简化为如下的过程,很显然,通过对系统函数 $clog2的使用,我们大大减少了设计时端口宽度定义时需要code的量。

module clog2(a,b);

parameter depth = 2034;
input [$clog2(depth)-1:0] a;
output [$clog2(depth)-1:0]b;

//details about the design
endmodule

三、额外补充
在Xlinix的官网的“44586 - 13.2 Verilog $clog2 function implemented improperly”中,作者发现了13.2版本的Xlinix的ISE对clog2系统函数的错误计算,按照文章中所言:“The $clog2 function returns the ceiling of the logarithm to the base e (natural logarithm) rather than the ceiling of the logarithm to the base 2.”意味着13.2版本的ISE以e为底计算clog2,而非以2为底,官方的回复是ISE 13.2 仅支持Verilog-2001,这个问题在ISE 14.1中进行了修复,所以读者假如使用的开发套件是老版本的,或者不支持Verilog-2005,都有可能因为使用clog2产生问题,需注意。具体额外补充参考如下。
44586 - 13.2 Verilog $clog2 function implemented improperly

© 版权声明
THE END
喜欢就支持一下吧
点赞 1 分享 赞赏
评论 抢沙发
取消