VHDL:找出/报告整数的位宽/长度(相对于 std_logic_vector)?
假设我需要一个信号来表示从 0 到 5 的数字;显然这需要 3 位 std_logic 来表示(即如果 MAXVAL=5,则 bitwidth= {wcalc "floor(logtwo($MAXVAL))+1"
}) 。
我知道我可以这样做:
SIGNAL myLogicVector : STD_LOGIC_VECTOR(2 downto 0) := 5;
用它我可以显式指定一个由三个 std_logic“位”组成的数组,并设置初始值;然后我可以使用 REPORT 打印出长度(在本例中为 3):
report("Bit width of myLogicVector is "& integer'image(myLogicVector'length));
到目前为止,一切顺利。但是,假设我使用整数(数字)类型:
SIGNAL myInteger : NATURAL range 0 to 5 := 5;
我猜测这里的“编译器”(“合成器”)会自动推断它需要 3 位存储长度,因为整数的值范围在 0 到 5 之间。如果是这种情况,我的问题是:是否有可能以某种方式在报告中打印出这个位的宽度/长度/大小?
当然,诀窍在于,像这样的东西:
report("Bit width of myInteger is "& integer'image(myInteger'length));
... 将失败(例如,使用“HDLParsers:3389 - 属性'length的前缀必须是数组对象”),因为只要据我所知,所有这些属性,如 'length
和 'range
只适用于数组 (了解 VHDL 属性),而整数(自然)不是数组 - 它是一个数字 :) (VHDL 向量整数转换问题)
再次,我知道我可能会利用 log2 (从最大值计算无符号变量的宽度?< /a>) - 但我想要的只是快速查看(在综合期间)“合成器”为最终综合设计分配了多少“位”,从而大概知道多少“位”就最终 FPGA 资源而言(特别是如果我使用“泛型”以某种方式计算整数的特定最大值)。
嗯,预先感谢您的任何回复, 干杯!
编辑:一些上下文:我正在使用 ISE Webpack 9.2;我尝试使用“通用”变量/常量作为参数,然后使用方程来计算计数器的最大值。我猜这个计算发生在“编译”时(在 ISE 中是“综合”,而不是“实现设计”),因此我希望在此处出现报告消息(事实上我得到了因此,对于 std_logic_vector 来说,在综合日志中 - 然而,对我来说相同的报告消息也在行为模拟开始时出现,这很好)。
这些报告消息的目标是确保我的方程正确,并且合成器不会尝试推断 32 位计数器 - 即使我只想从 0 计数到 5 :)
Say I need a signal to represent numbers from 0 to 5; obviously this needs 3 bits of std_logic to be represented (i.e if MAXVAL=5, then bitwidth= {wcalc "floor(logtwo($MAXVAL))+1"
}).
I'm aware I could do:
SIGNAL myLogicVector : STD_LOGIC_VECTOR(2 downto 0) := 5;
with which I'd explicitly specify an array of three std_logic 'bits', and set initial value; then I could use REPORT to print out the length (in this case, 3):
report("Bit width of myLogicVector is "& integer'image(myLogicVector'length));
So far, so good. But, let's say I use an integer (number) type instead:
SIGNAL myInteger : NATURAL range 0 to 5 := 5;
I'm guessing that here the 'compiler' ('synthesizer') would automatically infer that it needs 3 bits of storage length, as this integer is ranged with values between 0 and 5. If that is the case, my question is: is it possible to somehow print out this bit width/length/size in a REPORT?
The trick is, of course, that something like this:
report("Bit width of myInteger is "& integer'image(myInteger'length));
... will fail (say, with "HDLParsers:3389 - Prefix of attribute 'length must be an array object"), since as far as I gather, all these attributes like 'length
and 'range
are applicable only to arrays (Understanding VHDL Attributes), whereas an integer (natural) is not an array - it is a number :) (VHDL vector integer conversion question)
Again, I'm aware I could possibly utilize a log2 (Computing the width of an unsigned variable from maximum value?) - but what I'd like is just to see quickly (during synthesis) how many 'bits' the 'synthesizer' allocated for an eventual synthesized design, and thus approx how much would be used in terms of final FPGA resources (especially if I'd use 'generics' to somehow calculate a particular max value for an integer).
Well, thanks in advance for any responses,
Cheers!
EDIT: a bit of context: I'm using ISE Webpack 9.2; I'm trying to use 'generic' variables/constants as parameters, and then use equations to calculate max values for counters. This calculation, I guess occurs at 'compile' time (which would be 'Synthesize' in ISE - not 'Implement Design'), and so it is here where I want the report messages to occur (and I in fact got them so, for std_logic_vector proper, in the synthesis log - however, the same report messages for me occur at start of behavioral simulation too, which is fine).
And the goal of these report messages is to make sure both that my equations are OK, and that the synthesizer will not try to infer a 32-bit counter - even if I want to count just from 0 to 5 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这可以通过如下方式解决:
首先,声明一个新的(子)类型,表示应该用于
myInteger
的范围。 (然后将 myInteger 声明为该类型的信号,而不是直接声明整数范围的信号)。然后,使用声明的范围作为索引范围来声明一个虚拟数组类型,然后使用that的
'length
:This can be solved as follows:
First, declare a new (sub)type representing the range that should be used for
myInteger
. (Then declaremyInteger
as a signal of this type, not directly of the integer range).Then, declare a dummy array type using the declared range as index range, and then use the
'length
of that:原则上,VHDL 整数的表示是未定义的。
在实践中,您通常可以假设综合工具将使用 2 的补码表示,同时考虑范围约束。因此,范围约束和实现的位宽度之间的关系很简单,尽管从 VHDL 内报告位宽度并非如此。
In principle, the representation of a VHDL integer is undefined.
In practice, you can normally assume that a synthesis tool will use a 2's complement representation, taking into account the range constraint. Therefore, the relation between the range constraint and the implemented bit width is straightforward, even though reporting the bit width from within VHDL is not.
根据规范,它应该,但不是必需的。来自 IEEE 1076.6-2004:
我可以肯定地说,至少有一个综合工具不以这种方式使用范围,因为我必须调试形式验证错误比较。
对于 Xilinx FPGA,XST 综合报告将告诉哪些存储元件具有未使用的位,但不告诉简单的连线。
According to the spec it should, but it is not required. From IEEE 1076.6-2004:
I can say definitively at least one synthesis tool does not use range in this manner as I had to debug the formal verification miscompares.
For Xilinx FPGAs, the XST synthesis report will tell which storage elements have unused bits, but not simple wires.
基本数学:
位宽 = ceil(log2(my_integer + 1))
但 VHDL 需要更多努力...
内联代码:
作为函数:< /strong>
您可以将这些函数放入辅助函数包中。
Basic maths:
Bit width = ceil(log2(my_integer + 1))
But VHDL requires a bit more effort...
Inline code:
As a function:
You can put these functions in a package of helper functions.