2012-04-18 80 views
2

我試圖定義VHDL的功能,但我得到VHDL功能無法編譯

Error: tst.vhd(4): near "subtype": syntax error

這是這是第一次,我在VHDL和我的編碼代碼

subtype word10 is bit_vector(9 downto 0); 
subtype word8 is bit_vector(7 downto 0); 

function tst (input : in word10) return word10 is 
    variable tmp : word10; 
    -- code here 
    begin 

    return tmp; 
end tst; 

entity tester is 
end; 

architecture tst of tester is 
begin 
end; 

不能弄清楚錯誤是什麼。

任何想法?

回答

4

問題是你正試圖定義(亞型和功能)需要一個庫單元(包裝或實體)或其他一些機構,不只是掛在自己的內部被宣佈的事情。嘗試將聲明添加到測試實體(即:經過「實體測試儀是」行):

entity tester is 
    subtype word10 is bit_vector(9 downto 0); 
    subtype word8 is bit_vector(7 downto 0); 

    function tst (input : in word10) return word10 is 
     variable tmp : word10; 
     -- code here 
     begin 

     return tmp; 
    end tst; 
end tester; 

正是你宣佈你的亞型和功能將取決於在您需要的可見範圍。如果您需要在整個設計中使用它們,它們通常會聚集在一起並在包裝中聲明。

1

的問題是,你必須定義SUBTYPE裏面去定義和begin之間的功能

不過,我不知道你是否意識到一個事實,即VHDL是不是有點像一種編程語言,但更多的是設計語言。如果你想綜合你的代碼,那麼你應該小心在你的情況下是否可以合成函數。

+0

如果我繞過'subtype'事情。 'function tst(input:in bit_vector(9 downto 0))return bit_vector(7 downto 0)is'我得到了類似的關於函數的錯誤。我正在爲fpga編寫此代碼,因此您認爲它可能不起作用?我很困惑。 '錯誤:tst.vhd(4):附近「功能」:語法error' – kechapito 2012-04-18 13:11:06

+0

你應該避免一切都像在VHDL的功能,因爲它沒有像C.使用'entities'端口聲明和'architecture'的行爲申報,僅此而已。並且要小心諸如'wait'等語句,因爲這些語句不能合成。一本好的(最好的IMO)書籍:Peter Ashenden設計的VHDL指南。 – 2012-04-18 13:16:18

+0

我將實施VHDL加密算法,我需要類似的功能,否則代碼將變得相當混亂。我閱讀了有關組件,但我認爲這不是我需要的。 – kechapito 2012-04-18 14:51:00

1

subtype S和function s的通常在package S和package body申報的 - 如果你試圖編譯代碼,是的話,是的,它會失敗。

嘗試(剛剛輸入了我的頭頂,所以有可能是語法拼寫錯誤,但它應該給你正確的想法):

package mypkg is 
    subtype word10 is bit_vector(9 downto 0); 
    subtype word8 is bit_vector(7 downto 0); 
    function tst (input : in word10) return word10; 
end package; 
package body mypkg 
    function tst (input : in word10) return word10 is 
     variable tmp : word10; 
    begin 
     -- code here 
     return tmp; 
    end tst; 
end package body; 

爲了「跑」的任何代碼(這是有點代碼用詞不當,其意在說明硬件),你還需要一個entity爲了再打電話給你tst功能模擬器內,以「精心」的。實體是爲VHDL積木更加使用,用於塊內的捕捉行爲過程 - 函數和過程經常使用的實體和過程中捕捉到經常使用的功能,如在軟件世界。