2013-07-03 58 views

回答

19

如果作爲簽名的(2的補碼)的8位值被解釋,則一般與標準的VHDL轉換方法是使用IEEE numeric_std庫:

library ieee; 
use ieee.numeric_std.all; 

architecture sim of tb is 
    signal slv_8 : std_logic_vector(8 - 1 downto 0); 
    signal slv_16 : std_logic_vector(16 - 1 downto 0); 
begin 
    slv_16 <= std_logic_vector(resize(signed(slv_8), slv_16'length)); 
end architecture; 

因此,首先將std_logic_vector被轉換轉換爲有符號值,然後應用調整大小,這將簽署擴展有符號值,並且結果最終轉換回std_logic_vector。

轉換過程非常漫長,但具有通用性,即使目標長度稍後發生變化也能正常工作。

屬性「長度簡單地返回slv_16 std_logic_vector的長度,從而16.

對於無符號表示代替簽字,它可以使用的代替unsignedsigned來完成,因此與此代碼:

slv_16 <= std_logic_vector(resize(unsigned(slv_8), slv_16'length)); 
+0

我試圖從16位調整到8位:resized_var1:= std_logic_vector(resize(unsigned(Kp)* unsigned(integration1)),8);我得到錯誤:「類型轉換(對std_logic_vector)不能有聚合操作數。」爲什麼它不起作用? –

+0

因爲,8應該在resize()的parens裏面而不是std_logic_vector()。 –

4
architecture RTL of test is 
    signal s8: std_logic_vector(7 downto 0); 
    signal s16: std_logic_vector(15 downto 0); 
begin 
    s16 <= X"00" & s8; 
end; 
+0

什麼是線5「X」是什麼意思? –

+2

'x'表示「十六進制」。所以x「00」基本上是「00000000」二進制。 – Passepartout

+0

如果我想將「11111111」轉換爲「1111111111111111」 –

2

爲了完整起見,另一種方式是偶爾有用:

-- Clear all the slv_16 bits first and then copy in the bits you need. 
process (slv_8) 
begin 
    slv_16 <= (others => '0'); 
    slv_16(7 downto 0) <= slv_8; 
end process; 

我還沒有我必須這樣做才能回憶起來,但在更復雜的情況下,我需要這樣做:將一些相關信號複製成更大,更復雜的記錄是一次。

+1

符號擴展是用第一個賦值完成的:slv_16 <= (others => slv_8(7));' –

+0

這在功能上是正確的,但我認爲如果你想要符號擴展,你應該使用正確的數字類型(即'簽署'),然後使用'resize'函數 - 如你在你的回答中所建議的:) –

1

此處理,而無需修改零的寬度轉換如果任std_logic_vector變化:

architecture RTL of test is 
    signal s8: std_logic_vector(7 downto 0); 
    signal s16: std_logic_vector(15 downto 0) := (others => '0'); 
begin 
    s16(s8'range) <= s8; 
end;