gpt4 book ai didi

vhdl - unsigned和std_logic_vector之间的区别

转载 作者:行者123 更新时间:2023-12-03 13:46:17 25 4
gpt4 key购买 nike

谁能告诉我下面书面声明之间的区别。

signal A: **unsigned**(3 downto 0);
signal B: **std_logic_vector**(3 downto 0);

最佳答案

std_logic_vectorunsigned都是std_logic的不受约束的数组。与signed类型一样。 std_logic_vectorstd_logic_1164包中声明; unsignedsigned在包numeric_std中声明。这三种类型都是相同的。唯一的区别是他们的名字。

那么,这有什么意义呢?一个例子很好地说明了这一点:


variable U : unsigned(3 downto 0);
variable S : signed(3 downto 0);
variable I : integer;

然后
U := "1111";
I := to_integer(U);

导致 I的值为15,而
S := "1111";
I := to_integer(S);

导致 I的值为-1。这是因为 unsigned类型用于表示无符号数字,该数字只能为正数。因此, "1111"表示数字15。但是, signed类型也必须能够表示负数,并且对于 signed类型, "1111"表示-1(因为此类型使用二进制补码表示形式)。

因此,您可以看到相同的函数 to_integer-在使用 "1111"调用时返回两个不同的结果-15还是-1,具体取决于参数是 unsigned还是 signed类型。因此,即使它们之间的唯一区别是它们的名称,您也可以看到同时具有这两种类型的意义。

实际上,有两种 to_integer函数,而不是一种。一个带有 unsigned参数;另一个(标识为 to_integer)采用 signed参数。如您所见,它们的行为确实有所不同。编译器可以根据参数的类型来决定需要调用哪个函数。这种想法使编译器可以根据参数的类型在不同的(但名称相同的函数)之间进行选择,这被称为重载。它在软件语言中很常见。

那么, std_logic_vector呢?假设您写了:
variable V : std_logic_vector(3 downto 0);
variable I : integer;

然后
V:= "1111";
I := to_integer(V);

您希望 to_integer函数产生什么结果? 15还是-1?上面的代码是非法的,这个难题得以解决-它不会编译。它不会编译,因为没有为 to_integer定义的 std_logic_vector函数的版本- to_integer类型的 std_logic_vector函数不会重载。

因此,如果只需要表示正数,则最好使用 unsigned类型。如果需要表示负数,则需要使用 signed类型。如果您不太在意,因为您的位模式不是数字,或者因为您没有对它进行任何数学运算(您只是将其从一个地方传输到另一个地方),那么最好使用 std_logic_vector

https://www.edaplayground.com/x/2Qq4

关于vhdl - unsigned和std_logic_vector之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44558711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com