2015-02-12 45 views
0

因此,我使用以下VHDL代碼來實現僅使用2:1多路複用器,反相器(翻轉位)和全加器的Nbit加法器/減法器。當第一個加法器有i_Control的進位時,我有問題將加法器的執行連接到下一個加法器。任何幫助將不勝感激 :)。連接執行結構VHDL中的加法器/減法器

library IEEE; 
use IEEE.std_logic_1164.all; 
use work.all; 

entity add_subtract is 
    generic(N : integer := 16); 
    port(i_M : in std_logic_vector(N-1 downto 0); 
     i_N : in std_logic_vector(N-1 downto 0); 
     i_Control : in std_logic_vector(N-1 downto 0); 
     o_S : out std_logic_vector(N-1 downto 0)); 

end add_subtract; 

architecture structure of add_subtract is 

component bit_adder 
    port(i_X  : in std_logic; 
      i_Y  : in std_logic; 
      i_Cin : in std_logic; 
      o_Ss : out std_logic; 
     o_Couts : out std_logic); 
end component; 

component inverter 
    port(i_A : in std_logic; 
     o_F : out std_logic); 
end component; 

component bit_mux 
    port(i_X : in std_logic; 
     i_Y : in std_logic; 
      i_S : in std_logic; 
      o_N : out std_logic); 
end component; 

signal compvalue, muxvalue, addervalue : std_logic_vector(N-1 downto 0); 
signal sel, carry : std_logic_vector(N-1 downto 0); 
signal k : integer := 0; 
begin 

carry(0) <= i_Control(0); 

G1: for i in 0 to N-1 generate 
one_comp: inverter 
    port map(i_A  => i_N(i), 
     o_F  => compvalue(i)); 

mux: bit_mux 
    port map(i_X  => i_N(i), 
     i_Y  => compvalue(i), 
     i_S  => i_Control(i), 
     o_N  => muxvalue(i)); 

struct_adder: bit_adder 
    port map(i_X  => i_M(i), 
     i_Y  => muxvalue(i), 
     i_Cin => carry(i), 
     o_Ss => o_S(i), 
     o_Couts => carry(i)); 

end generate; 

end structure; 
+1

家庭作業。沒有人像這樣實現一個減法器。只需輸入'c <= a-b;' – Philippe 2015-02-12 06:38:23

回答

0

使進陣列一個較長:

signal carry : std_logic_vector(N downto 0); -- was N-1 

並改變這一點:

 o_Couts => carry(i)); 

這樣:

 o_Couts => carry(i+1)); 

在生成的語句,而留下i_Cin按原樣進行輸入關聯。

如果最後一次執行不通過輸出端口傳送,網絡將在合成過程中被吃掉。

+0

感謝您的回覆。你的建議得到加法器/減法器正常工作:) – 2015-02-13 03:35:19