VHDL中conv_integer的使用

1 vhdl

我正在尝试编写一些代码,这些代码只会向左或向右移动 32 位向量,其中 5 位输入将用于移位量 ( shamt)。我遇到的问题是试图将 an 转换std_logic_vectorinteger. 我的代码是这样的:

library ieee;
use ieee.STD_LOGIC_1164.all;
use ieee.STD_LOGIC_ARITH.all;

entity shiftlogical is
  port(x     : in  std_logic_vector(31 downto 0);
       shamt : in  std_logic_vector( 4 downto 0);
       y     : out std_logic_vector(31 downto 0));
end shiftlogical;

architecture beh of shiftlogical is
  signal shift : integer;
  signal temp  : std_logic_vector(31 downto 0);
begin
  shift <= conv_integer(unsigned(shamt));
  temp  <= x(shift downto 0);
  y     <= temp;
end beh;
Run Code Online (Sandbox Code Playgroud)

我知道代码不完整,但为了测试一些想法,我试图将"00010"(2)传递到 中shamt,但结果是 -2147483648。但是我无法弄清楚它为什么会这样做,也无法在网上找到任何显示与我正在做的事情不同的资源。我非常感谢任何帮助。

Kev*_*eau 5

-2147483648 (-2**31) 是整数的默认初始值,是其范围内最左边、最负的值。它表明信号分配shift尚未执行。很可能是因为它是一个连续的分配,并且没有shamt导致它更新的事件。

std_logic_arith不是 IEEE 标准库。您应该使用to_integer()fromieee.numeric_std代替。将数字端口保留为unsignedsigned以便您的意图明确并最小化类型转换也是有益的。此外,您不能直接分配xto的可变长度切片,temp因为它们的长度不匹配。您应该使用resize()(来自 numeric_std)将长度扩展回 32 位或重新考虑您的方法。