verilog 中的日志值
谁能告诉我如何计算设计文件中参数的对数值?
我遇到这样的情况,我有一个像这样的循环的生成:
parameter N=8;
genvar i,m;
generate
for(m=1;m<N;m=m*2)
begin :m_loop
// I have an instance here
// "in" is an input of N bits, "out" is an output N bits
Inst u(in(i+log2(m)),in(i+1+log2(m)),out(i+log2(m)));
end
endgenerate
有没有办法执行 log2(m),其中 m 作为 2 的幂变化(2^0,2^1,2^2,直到否)?如果没有,
我正在考虑引入一个新变量,例如 m1,它是 0,1,2,...(即 log2(m))。
在这种情况下,m1应该随着每m同时变化,即当m=1时,m1=0; m=2,m1=1;
m=4,m1=2; m=8,m1=3 等等。
Can anyone tel me how to calculate the log value of a parameter in a design file?
I am coming across such a situation where I have a generate for a loop like this:
parameter N=8;
genvar i,m;
generate
for(m=1;m<N;m=m*2)
begin :m_loop
// I have an instance here
// "in" is an input of N bits, "out" is an output N bits
Inst u(in(i+log2(m)),in(i+1+log2(m)),out(i+log2(m)));
end
endgenerate
Is there a way to do log2(m), where m varies as powers of 2 (2^0,2^1,2^2, till N)? If not,
I was thinking of introducing a new variable, m1, say, which is 0,1,2,... (i.e log2(m)).
In such a case m1 should vary with simultaneously every m, ie, when m=1,m1=0; m=2,m1=1;
m=4,m1=2; m=8,m1=3 and so on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑到这显然不是为了综合,并且 N 只有 8,你可以生成(使用一对嵌套的 for 循环)一个 2^N 值的数组,每个值包含逆对数 - 那么你可以索引到与“m”一起得到答案。
您甚至可以使用比 Verilog 更简单的语言生成表的源代码,然后加载它。
更新:
如果您只感兴趣,RAM DEPTH 的地址宽度也可能相关求 2 的精确幂的答案。
Given this is obviously not intended to be synthesised, and that N is only 8, could you just generate (with a pair of nested for loops) an array of 2^N values, each containing the inverse log - then you could just index into that with 'm' to get the answer.
You could even generate the source for a table using a less awful language than Verilog, and just load it it.
Update:
ADDRESS WIDTH from RAM DEPTH might be relevant too, if you're only interested in answers for exact powers of two.
您想要的是对数底数 2 的上限。
如果您的模拟器支持 IEEE 1364-2005 或任何 IEEE 1800,则使用
$clog2
。例如:如果您仅限于 IEEE 1364-2001,则使用带有已知参数的“纯函数”作为输入来分配另一个参数。 “纯函数”被定义为仅由其输入计算输出的函数。以下是基于 IEEE 1800-2012 第 13.4.3 节。自 1364-2001 年以来的所有 Verilog (& SystemVerilog) 版本都使用相同的示例。 1800-2012 是唯一可以免费下载的版本来自 IEEE。
相同的
clogb2()
函数应该适用于genvar
类型。What you want is the ceiling of the log base 2.
If your simulator supports IEEE 1364-2005 or any IEEE 1800 then use
$clog2
. Ex.:If your limited to IEEE 1364-2001, then use a 'pure function' with a know parameter as the input to assign another parameter. A 'pure function' is defined as a function who's output only calculated by its inputs. The following is a snip-it based on the example in IEEE 1800-2012 section 13.4.3. It is the same example is used in all Verilog (& SystemVerilog) versions since 1364-2001. 1800-2012 is the is the only version you can download for free from IEEE.
The same
clogb2()
function should work withgenvar
types.