簡體   English   中英

如何在 vhdl 中將位分成不同的信號?

[英]How to separate bits into different signals in vhdl?

我有以下想要轉換為 vhdl 的 verilog 代碼行:

assign   {cout,sum} = ( add ) ? ( in_a + in_b + cin ) : ( in_a - in_b - cin );

我將如何在 vhdl 中做到這一點?

實際上,您以相同的方式執行此操作,您只需要記住增加輸入值的寬度,以便為輸出進位“騰出空間”。

(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;

這至少更清晰一點。

編輯:

請注意,這僅適用於 VHDL 2008。對於早期版本,您必須創建一個比輸入更寬的中間信號,將結果分配給該信號,然后提取 cout 和 sum。

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM