VHDL:找出/报告整数的位宽/长度(相对于 std_logic_vector)?

发布于 2024-10-20 11:27:55 字数 1802 浏览 2 评论 0原文

假设我需要一个信号来表示从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦醒灬来后我 2024-10-27 11:27:56

这可以通过如下方式解决:

首先,声明一个新的(子)类型,表示应该用于 myInteger 的范围。 (然后将 myInteger 声明为该类型的信号,而不是直接声明整数范围的信号)。

然后,使用声明的范围作为索引范围来声明一个虚拟数组类型,然后使用that'length

subtype myInteger_range is natural range 0 to 5;
signal myInteger : myInteger_range;
type myInteger_dummy_array is array (myInteger_range) of boolean; -- or whatever
report integer'image(myInteger_dummy_array'length); --> "6"

This can be solved as follows:

First, declare a new (sub)type representing the range that should be used for myInteger. (Then declare myInteger 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:

subtype myInteger_range is natural range 0 to 5;
signal myInteger : myInteger_range;
type myInteger_dummy_array is array (myInteger_range) of boolean; -- or whatever
report integer'image(myInteger_dummy_array'length); --> "6"
糖粟与秋泊 2024-10-27 11:27:55

原则上,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.

所谓喜欢 2024-10-27 11:27:55

我猜这里是“编译器”
('合成器') 会自动
推断它需要 3 位存储
长度,因为该整数的范围为
0 到 5 之间的值

根据规范,它应该,但不是必需的。来自 IEEE 1076.6-2004:

1.3 术语

单词表示必须严格遵守的强制性要求,以符合标准,并且不得
允许偏差(应等于要求)。这个词应该
用于表示优先采取某种行动,但
不一定需要;

8.3.1.2 整数类型

建议综合工具应该转换信号或
具有整数子类型指示的变量
相应的位向量。如果范围不包含负数
值,该项目应具有无符号二进制表示形式。如果
范围包含一个或多个负值,该项目应该有一个
二进制补码实现。向量应该具有最小的
宽度与这些表示一致。

综合工具应支持整数类型以及正数、负数和
无约束(通用)整数
其边界位于 –2 147 483 648 至 +2 147 483 647 范围内(包括边界值)
成功映射 32 位二进制补码数)

示例:“INTEGER range 9 to 10”应使用
4 位的等效向量长度,就像已定义一样
子类型指示为“INTEGER range 0 to 15”。

我可以肯定地说,至少有一个综合工具以这种方式使用范围,因为我必须调试形式验证错误比较。

对于 Xilinx FPGA,XST 综合报告将告诉哪些存储元件具有未使用的位,但不告诉简单的连线。

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

According to the spec it should, but it is not required. From IEEE 1076.6-2004:

1.3 Terminology

The word shall indicates mandatory requirements strictly to be followed in order to conform to the standard and from which no
deviation is permitted (shall equals is required to). The word should
is used to indicate that a certain course of action is preferred but
not necessarily required;

8.3.1.2 Integer Types

It is recommended that a synthesis tool should convert a signal or
variable that has an integer subtype indication to a
corresponding vector of bits. If the range contains no negative
values, the item should have an unsigned binary representation. If the
range contains one or more negative values, the item should have a
twos-complement implementation. The vector should have the smallest
width consistent with these representations.

The synthesis tool shall support integer types and positive, negative, and
unconstrained (universal) integers
whose bounds lie within the range –2 147 483 648 to +2 147 483 647 inclusive (the range
that successfully maps 32 bit twos-complement numbers)

Example: “INTEGER range 9 to 10” should be synthesized using an
equivalent vector length of 4 bits, just as if it had been defined
with a subtype indication of “INTEGER range 0 to 15”.

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.

错爱 2024-10-27 11:27:55

是否可以以某种方式在报告中打印出该位的宽度/长度/大小?

基本数学:

位宽 = ceil(log2(my_integer + 1))

但 VHDL 需要更多努力...

内联代码:

use ieee.math_real.all;
...
report "Bit width of myInteger is " & integer'image(integer(ceil(log2(real(myInteger) + real(1)))));

作为函数:< /strong>

use ieee.math_real.all;
...
-- Returns the bit width of i as a positive integer.
function BitWidth(constant i: in positive) return positive is
begin
    return positive(ceil(log2(real(i) + real(1))));  -- Add one to prevent errors when i = 2**n
end function;

-- Overload for returning the string representation of the bit width of i.
function BitWidth(constant i: in positive) return string is
    variable bit_width: positive := BitWidth(i);
begin
    return positive'image(bit_width);
end function;

signal myInteger: positive := 8;
...
report "Bit width of myInteger is " & BitWidth(myInteger);

您可以将这些函数放入辅助函数包中。

is it possible to somehow print out this bit width/length/size in a REPORT?

Basic maths:

Bit width = ceil(log2(my_integer + 1))

But VHDL requires a bit more effort...

Inline code:

use ieee.math_real.all;
...
report "Bit width of myInteger is " & integer'image(integer(ceil(log2(real(myInteger) + real(1)))));

As a function:

use ieee.math_real.all;
...
-- Returns the bit width of i as a positive integer.
function BitWidth(constant i: in positive) return positive is
begin
    return positive(ceil(log2(real(i) + real(1))));  -- Add one to prevent errors when i = 2**n
end function;

-- Overload for returning the string representation of the bit width of i.
function BitWidth(constant i: in positive) return string is
    variable bit_width: positive := BitWidth(i);
begin
    return positive'image(bit_width);
end function;

signal myInteger: positive := 8;
...
report "Bit width of myInteger is " & BitWidth(myInteger);

You can put these functions in a package of helper functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文