我正在尝试使用 xilinx 工具在 VHDL 中定义和模拟一个 8 位移位器,它可以向左或向右移动或旋转 0、1、2 或 3 位位置。我正在尝试在 digilent basys 2 板上实现这一点。
我刚刚开始学习 vhdl 编程。有人可以帮忙吗?谁能告诉我 vhdl 代码应该是什么样子?
移位和旋转功能可以在 numeric_std 包中找到。下面是一些示例代码作为起点:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.numeric_std.all;
entity EXAMPLE is
Port (
clk : in STD_LOGIC; -- master clock
enable : in std_logic; -- when '1' --> rotate
dir : in STD_LOGIC; -- when '1': right, when '0': left
nr : in STD_LOGIC_VECTOR (1 downto 0); -- number of steps to rotate
din : in STD_LOGIC_VECTOR (7 downto 0); -- in vector
dout : out STD_LOGIC_VECTOR (7 downto 0)); -- out vector
end EXAMPLE;
architecture Behavioral of EXAMPLE is
begin
shifter: process(clk)
begin
if rising_edge(clk) then
if enable='1' then
if dir='0' then -- right
dout<=std_logic_vector(rotate_right(unsigned(din),to_integer(unsigned(nr))));
else -- left
dout<=std_logic_vector(rotate_left(unsigned(din), to_integer(unsigned(nr))));
end if;
end if;
end if;
end process shifter;
end Behavioral;
Rotate Right = bitvector(0) & bitvector(7 downto 1);
<br>Rotate Left = bitvector(6 downto 0) & bitvector(7);
使用串联,因为其他功能不可合成。
使用ieee.numeric_std
库中为此目的而设计的函数。
使用(或转换)适当signed
或unsigned
向量类型的输入。
这是这些功能的“原型”,因此您可以看到您得到了什么。
--============================================================================
-- Shift and Rotate Functions
--============================================================================
-- Id: S.1
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.
-- Id: S.2
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.
-- Id: S.3
function SHIFT_LEFT (ARG: SIGNED; COUNT: NATURAL) return SIGNED;
-- Result subtype: SIGNED(ARG'LENGTH-1 downto 0)
-- Result: Performs a shift-left on a SIGNED vector COUNT times.
-- The vacated positions are filled with '0'.
-- The COUNT leftmost elements are lost.
-- Id: S.4
function SHIFT_RIGHT (ARG: SIGNED; COUNT: NATURAL) return SIGNED;
-- Result subtype: SIGNED(ARG'LENGTH-1 downto 0)
-- Result: Performs a shift-right on a SIGNED vector COUNT times.
-- The vacated positions are filled with the leftmost
-- element, ARG'LEFT. The COUNT rightmost elements are lost.
--============================================================================
-- Id: S.5
function ROTATE_LEFT (ARG: UNSIGNED; COUNT: NATURAL) return UNSIGNED;
-- Result subtype: UNSIGNED(ARG'LENGTH-1 downto 0)
-- Result: Performs a rotate-left of an UNSIGNED vector COUNT times.
-- Id: S.6
function ROTATE_RIGHT (ARG: UNSIGNED; COUNT: NATURAL) return UNSIGNED;
-- Result subtype: UNSIGNED(ARG'LENGTH-1 downto 0)
-- Result: Performs a rotate-right of an UNSIGNED vector COUNT times.
-- Id: S.7
function ROTATE_LEFT (ARG: SIGNED; COUNT: NATURAL) return SIGNED;
-- Result subtype: SIGNED(ARG'LENGTH-1 downto 0)
-- Result: Performs a logical rotate-left of a SIGNED
-- vector COUNT times.
-- Id: S.8
function ROTATE_RIGHT (ARG: SIGNED; COUNT: NATURAL) return SIGNED;
-- Result subtype: SIGNED(ARG'LENGTH-1 downto 0)
-- Result: Performs a logical rotate-right of a SIGNED
-- vector COUNT times.