2013-04-28 63 views
0

我有以下问题:我要实现8位左移位器,使一个移位到左,它的代码是:循环8位移位器,VHDL

entity left_shift is 

generic (N: integer := 8); 

Port(
    Databitsin : in STD_LOGIC_vector(N-1 downto 0); 
    Databitsout : out STD_LOGIC_vector(N-1 downto 0); 
    Carry: out std_logic 
); 

end left_shift; 

architecture Behavioral_l of left_shift is 
begin 
    Databitsout(N-1 downto 1)<= Databitsin(N-2 downto 0); 
    Carry<=Databitsin(N-1); 
end Behavioral_l; 

然后我需要实现另一个具有使一个右移

entity Right_shift is 
generic (N: integer := 8); 
Port(
    Databitsin : in STD_LOGIC_vector(N-1 downto 0); 
    Databitsout : out STD_LOGIC_vector(N-1 downto 0); 
    Carry: out std_logic 
); 

end Right_shift; 

architecture Behavioral of Right_shift is 
begin 
    Databitsout(N-2 downto 0)<= Databitsin(N-1 downto 1); 
    Carry<=Databitsin(0); 
end Behavioral; 

现在,我必须建立具有使用这些2个组件,使循环移位(左,右)的主模块。 我该怎么做?

回答

1

有不同的方式来实现循环移位(=旋转!)。如果你添加一个方向选择器Dir,你可以在一个代码中有两个方向。

实施例1,

加上 “使用IEEE.NUMERIC_STD.all” 尽量使用numeric_std包功能:

Databitsout<=std_logic_vector(rotate_right(unsigned(Databitsin),1)) when Dir='0' else 
      std_logic_vector(rotate_left(unsigned(Databitsin),1));     

前。 2个

使用std_logic_vectors直接:

Databitsout<=Databitsin(0) & Databitsin(N-1 downto 1) when Dir='0' else 
      Databitsin(N-2 downto 0) & Databitsin(N-1); 

进位标志是在两个相同的:

Carry<=  Databitsin(0) when Dir='0' else 
      Databitsin(N-1); 
+0

非常感谢您的帮助 – user2280448 2013-04-30 00:22:04

1

这听起来很像功课,但从未在少:

首先,你为什么要使用两种成分?优雅的解决方案是编写一个可向左或向右移动的组件。

如果您有一些非常好的理由按照您的建议来做事情,请尝试在两者的数据生成信号之间实例化和多路复用,具体取决于所需的模块。要进行循环移位而不是线性移位,需要将进位位合并到逻辑向量数组的合适末端。

+0

是感谢名单,但如何!? – user2280448 2013-04-28 21:36:05

+1

@ user2280448如果你有问题的更新,你应该编辑问题而不是其中的一个答案。 – Yaur 2013-04-28 21:51:27