2013-04-28 71 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