type-conversion - to_unsigned 做什么?

标签 type-conversion vhdl unsigned twos-complement signed-integer

有人可以向我解释 VHDL 的 to_unsigned 是如何工作的,或者确认我的理解是正确的吗? 例如:

C(30 DOWNTO 0) <= std_logic_vector (to_unsigned(-30, 31))

这是我的理解:

  • -30 是一个带符号的值,以位表示为 1111111111100010
  • 应反转所有位并向其添加“1”以构建 C 的值
  • 0000000000011101+0000000000000001 == 0000000000011111

最佳答案

在 IEEE 包 numeric_std 中,TO_UNSIGNED 的声明:

  -- Id: D.3
  function TO_UNSIGNED (ARG, SIZE: NATURAL) return UNSIGNED;
  -- Result subtype: UNSIGNED(SIZE-1 downto 0)
  -- Result: Converts a non-negative INTEGER to an UNSIGNED vector with
  --         the specified SIZE.

您不会找到带有声明为整数类型的参数或大小的已声明函数 to_unsigned。后果是什么?

让我们把它放在 Minimal, Complete, and Verifiable example 中:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity what_to_unsigned is
end entity;

architecture does of what_to_unsigned is
    signal C:   std_logic_vector (31 downto 0);
begin

    C(30 DOWNTO 0) <= std_logic_vector (to_unsigned(-30, 31));

end architecture;

VHDL 分析器会给我们一个错误:

ghdl -a what_to_unsigned.vhdl
what_to_unsigned.vhdl:12:53: static constant violates bounds
ghdl: compilation error

并告诉我们 -30(第 12 行:字符 53)越界。这意味着在这种情况下,转换为 universal_integer 的数字文字不会在函数 to_unsigned 中转换为类型 natural

不同的工具可能会以更图形化的方式告诉我们:

nvc -a what_to_unsigned.vhdl  
** Error: value -30 out of bounds 0 to 2147483647 for parameter ARG  
  File what_to_unsigned.vhdl, Line 12  
        C(30 DOWNTO 0) <= std_logic_vector (to_unsigned(-30, 31));  
                                                        ^^^

并且实际上告诉我们在源代码中发现错误的位置。

可以肯定地说,您认为 to_unsigned 所做的与分析器认为的不同。

VHDL 是一种强类型语言,您试图为 IEEE 包中声明的函数 TO_UNSIGNED 中的参数 ARG 提供一个值以放置该值超出范围的位置numeric_std.

NATURAL 类型在包标准中声明,并通过推断声明库 std 使其可见;使用 std.standard.all;在上下文子句中。 (参见 IEEE Std 1076-2008,13.2 设计库):

Every design unit except a context declaration and package STANDARD is assumed to contain the following implicit context items as part of its context clause:

library STD, WORK; use STD.STANDARD.all;

在 16.3 Package STANDARD 中发现的 natural 声明:

subtype NATURAL is INTEGER range 0 to INTEGER'HIGH;

声明为 NATURAL 的值是 INTEGER 的子类型,具有不包括负数的受限范围。

在这里,您可以看到您有能力通过访问符合 VHDL 标准的工具并引用 IEEE Std 1076-2008,IEEE 标准 VHDL 语言引用手册来回答这个问题。

TL:DR;详情
您可能会注意到 9.4 静态表达式、9.4.1 常规允许在分析期间评估局部静态表达式:

Certain expressions are said to be static. Similarly, certain discrete ranges are said to be static, and the type marks of certain subtypes are said to denote static subtypes.

There are two categories of static expression. Certain forms of expression can be evaluated during the analysis of the design unit in which they appear; such an expression is said to be locally static. Certain forms of expression can be evaluated as soon as the design hierarchy in which they appear is elaborated; such an expression is said to be globally static.

可能有一些符合标准的工具在分析期间不会评估本地静态表达式。 “可以”是允许的而不是强制的。上述代码示例中演示的两个 VHDL 工具利用了该权限。在这两个工具中,命令行参数 -a 告诉工具分析提供的文件,如果成功,则将其插入当前工作库(默认为 WORK,请参阅 13.5 分析顺序,13.2 设计库)。

在详细说明局部静态表达式时评估边界检查的工具通常是纯粹解释性的,甚至可以通过单独的分析过程来克服。

VHDL 语言可用于在附件 D 指定的范围内的正式证明中使用的设计模型的正式规范 潜在不可移植的构造以及仅依赖函数时(参见 4.Subprograms and包,4.1 概述)。

VHDL 兼容工具保证提供相同的结果,尽管没有错误消息的标准化,也没有对工具实现方法的限制。

关于type-conversion - to_unsigned 做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33264066/

相关文章:

vhdl - 从同一语句分配两个信号

c - C 中的有符号和无符号

python - Matplotlib:没有字符串和轴反转的分类图

linux - Linux 上的快速 xls 转换

vhdl - 无法推断寄存器...在...,因为它在时钟边沿之外不保存其值

c - (新手) strstr() 返回带有无符号参数的 null

c - unsigned long 的确切取值范围是多少?

c# - 从字符串转换日期时间的语法错误

C++ int 限制的提升动机

vhdl - 等效于 VHDL 中的#ifdef 用于模拟/综合分离?