2015-03-31 35 views
0

這裏是一個級聯全加器的通用代碼。Vhdl通用fulladder代碼

問題是,fulladder的結果出現一個事件延遲(我的意思是當我改變input1 & inputs2出現以前輸入的結果)。我知道,如果我在沒有進程的情況下編寫代碼,這種延遲不會發生,但是我需要編寫一個通用的fulladder,並且沒有辦法編寫沒有進程和for循環的通用代碼。

所以我問是否有人可以幫助我修復代碼,以便輸出結果毫不遲疑地顯示結果!

LIBRARY IEEE; 
USE IEEE.STD_LOGIC_1164.ALL; 

entity adders is 
    generic(
     numberOfInputs : Integer := 4 
    ); port(
     enable : in std_logic; 
     cin  : in std_logic; 
     inputs1 : in std_logic_vector(numberOfInputs-1 downto 0); 
     inputs2 : in std_logic_vector(numberOfInputs-1 downto 0); 
     outputs : out std_logic_vector(numberOfInputs downto 0) 
    ); 
end entity adders; 

architecture Generic_Adder of adders is 
    signal Couts:std_logic_vector(numberOfInputs downto 0); 
    signal temp1:std_logic_vector(numberOfInputs-1 downto 0); 
    signal temp2:std_logic_vector(numberOfInputs-1 downto 0); 
    signal temp3:std_logic_vector(numberOfInputs-1 downto 0); 

begin 

    temp2<=inputs1; 
    temp3<=inputs2; 
    couts(0)<= cin; 

    Sum:process(temp2,temp3,cin,enable,Couts) is 
    begin 
     for count in 0 to numberOfInputs-1 loop 
      temp1(count) <= (temp2(count) xor temp3(count)); 
      outputs(count) <= Couts(count) xor temp1(count); 
      Couts(count+1) <= (temp2(count) and temp3(count)) or(couts(count) and temp1(count));--cout(count) is the previuos cout becuase the first cout is cin 
     end loop; 
    end process; 

    outputs(numberOfInputs) <= Couts(numberOfInputs); 
end Generic_Adder; 

回答

3

temp1信號的過程中被使用,但它是在敏感列表丟失。因此

變化temp1不會觸發過程的重新評價,並且將temp1新值不反映在其他驅動信號,直到另一信號觸發的重新評估,因此,你可能會經歷一個「延遲」。

通過將temp1添加到靈敏度列表或者按照Bill Lynch的建議重寫或者如果您使用的是VHDL-2008和兼容的編譯器來解決此問題,那麼靈敏度列表可以是process (all) ...

此外,由於在進程外循環驅動outputs以及在進程外循環驅動outputs(numberOfInputs),所以遇到了與「最長靜態前綴」有關的VHDL問題。結果是outputs(numberOfInputs)的驅動程序爲'U',過程中驅動程序爲Couts(numberOfInputs)。基於std_logic分辨率函數的結果值是「U」的值。解決這個問題

的一種方式,飼養過程時,是移動outputs(numberOfInputs)的過程中,如:

 ... 
    end loop; 
    outputs(numberOfInputs) <= Couts(numberOfInputs); 
end process; 

這個答案https://stackoverflow.com/a/18248941/2352082有大約VHDL最長前綴靜的更多信息。

+1

或者如果他們正在使用一個新的編譯器:'process(all)'。另外在靈敏度列表中不需要'cin'和'enable'。我不確定如果他們想要實際使用「使能」位並保持輸出,他們如何在沒有鎖存器的情況下編寫它。 – 2015-03-31 14:08:35

+1

你也可以用'temp1'作爲變量而不是信號。在這種情況下,它不一定是一個向量,在簡單的std_logic中就可以。 – 2015-03-31 14:26:37

+0

在輸出中沒有延遲,但輸出(1)和輸出(2)未知(u)!! ??我該如何解決? – user2988239 2015-03-31 15:25:27

1

您可以使用生成的語句寫的過程聲明之外的迭代代碼。或許,這將是這個樣子:

LIBRARY IEEE; 
USE IEEE.STD_LOGIC_1164.ALL; 

entity adders is 
    generic(
     numberOfInputs : Integer := 4 
    ); port(
     enable : in std_logic; 
     cin  : in std_logic; 
     inputs1 : in std_logic_vector(numberOfInputs-1 downto 0); 
     inputs2 : in std_logic_vector(numberOfInputs-1 downto 0); 
     outputs : out std_logic_vector(numberOfInputs downto 0) 
    ); 
end entity adders; 

architecture Generic_Adder of adders is 
    signal carry : std_logic_vector(numberOfInputs  downto 0); 
    signal result : std_logic_vector(numberOfInputs - 1 downto 0); 
begin 

    carry(0) <= cin; 

    for I in 0 to numberOfInputs-1 generate 
    begin 
     result(I) <= inputs1(I) xor inputs2(I) xor carry(I); 
     carry(I+1) <= 
      (inputs1(I) and inputs2(I)) or 
      (inputs1(I) and carry(I)) or 
      (inputs2(I) and carry(I)); 
    end generate; 

    outputs <= carry(numberOfInputs) & result; 

end Generic_Adder; 
+0

感謝您的幫助?? Ididn't不知道vhdl有這樣的事情! – user2988239 2015-03-31 15:23:11

+1

@BillLynch:在'result(I)'中'+'應該是'xor'。 – 2015-03-31 16:21:24

+0

@MortenZilmer:謝謝! – 2015-03-31 16:34:11