1

我正在尝试用 VHDL 创建一个 ALU,但我很难实现几个操作。我已经实现了加法、减法和和或操作,但我想知道如何实现逻辑移位操作?ALU 是 32 位的,但任何设计都会受到赞赏。

4

1 回答 1

1

numeric_std包包含逻辑移位操作,包括shift_rightshift_left

function SHIFT_LEFT (ARG: UNSIGNED; COUNT: NATURAL) return UNSIGNED;
-- Result subtype: UNSIGNED(ARG'LENGTH-1 downto 0)
-- Result: Performs a shift-left on an UNSIGNED vector COUNT times.
--         The vacated positions are filled with '0'.
--         The COUNT leftmost elements are lost.

function SHIFT_RIGHT (ARG: UNSIGNED; COUNT: NATURAL) return UNSIGNED;
-- Result subtype: UNSIGNED(ARG'LENGTH-1 downto 0)
-- Result: Performs a shift-right on an UNSIGNED vector COUNT times.
--         The vacated positions are filled with '0'.
--         The COUNT rightmost elements are lost.

因此,基于此,您可以简单地编写如下代码:

library ieee;
use ieee.numeric_std.all;

architecture syn of mdl is
  signal arg   : std_logic_vector(31 downto 0);
  signal count : std_logic_vector( 4 downto 0);
  signal res_r : std_logic_vector(31 downto 0);
  signal res_l : std_logic_vector(31 downto 0);
begin
  res_r <= std_logic_vector(shift_right(unsigned(arg), to_integer(unsigned(count))));
  res_l <= std_logic_vector(shift_left(unsigned(arg), to_integer(unsigned(count))));
end architecture;

这些操作是可综合的,并且可以很好地映射到 FPGA 资源(如果那是您的目标设备)。

以前围绕 VHDL 移位/旋转运算符存在一些混淆,请参阅此链接,但它已在 VHDL-2008 中进行了清理。但是,为了向后兼容,上述建议是基于函数而不是运算符。

于 2014-02-03T20:37:49.850 回答