2012-11-30 30 views

回答

6

實際上,您也是這樣做的,您只需要記住增加輸入值的寬度,以便爲輸出進位「騰出空間」。

(cout, sum) <= ('0'&in_a) + ('0'&in_b) + cin when(add='1') else ('0'&in_a) - ('0'&in_b) - cin; 

由於該行是非常,非常難看,很難理解,我建議整個事情轉化爲一個過程:

process(in_a, in_b, cin) begin 
    if(add='1') then 
     (cout, sum) <= ('0'&in_a) + ('0'&in_b) + cin; 
    else 
     (cout, sum) <= ('0'&in_a) - ('0'&in_b) - cin; 
    end if; 
end process; 

這是更清晰,至少有點。

編輯:

注意,在2008年VHDL這僅適用於早期版本中,你必須比你的輸入來創建一箇中介信號一個更廣泛的,結果分配到,然後提取cout和總和。

process(in_a, in_b, cin) 
    -- Assumes in_a and in_b have the same width, otherwise 
    -- use the wider of the two. 
    variable result : unsigned(in_a'length downto 0); 
begin 
    if(add='1') then 
     result := ('0'&in_a) + ('0'&in_b) + cin; 
    else 
     result := ('0'&in_a) - ('0'&in_b) - cin; 
    end if; 
    cout <= result(result'high); 
    sum <= result(result'high-1 downto 0); 
end process; 
相關問題