【正文】
USE 。 PACKAGE example IS FUNCTION convert(a0,a1:std_logic) RETURN integer。 FUNCTION convert(a0,a1,a2:std_logic)RETURN integer。 FUNCTION convert(a0,a1,a2,a3:std_logic)RETURN integer。 END example。 PACKAGE body example IS FUNCTION convert(a0,a1:std_logic)RETURN integer IS VARIABLE result:integer:=0。 第 4章 VHDL程序設(shè)計基礎(chǔ) 50 BEGIN IF (a0=?1?) THEN result:=result+1。 END IF IF (a1=?1?) THEN result:=result+2。 END IF END convert。 FUNCTION convert(a0,a1,a2:std_logic)RETRUN integer IS VARIABLE result:integer:=0。 BEGIN result:=convert(a0,a1)。 IF (a2=?1?) THEN result:=result+4。 END IF。 第 4章 VHDL程序設(shè)計基礎(chǔ) 51 RETURN(result)。 END convert。 FUNCTION convert(a0,a1,a2,a3:std_logic)RETURN integer IS VARIABLE resutl:integer:=0。 BEGIN result:=convert(a0,a1,a2)。 IF (a3=?1?) THEN result:=result+8。 END IF RETURN(result)。 END convert。 END example。 第 4章 VHDL程序設(shè)計基礎(chǔ) 52 ( 3)函數(shù)返回類型的重載 是指被重載的同名函數(shù)的返回值類型是不同的。這時 VHDL編譯器將會根據(jù)函數(shù)調(diào)用過程中的函數(shù)返回類型來選擇與之一致的函數(shù)。 LIBRARY IEEE; USE 。 PACKAGE example IS FUNCTION max(i1,i2:std_logic_vector) RETURN std_logic_vector。 FUNCTION max(i1,i2:std_logic_vector) RETURN bit_vector。 END example。 第 4章 VHDL程序設(shè)計基礎(chǔ) 53 PACKAGE body example IS FUNCTION max (i1,i2:std_logic_vector) RETURN std_logic_vector IS VARIABLE tmp:std_logic_vector(i1?RANGE)。 BEGIN IF(i1i2) THEN tmp:=i1。 ELSE tmp:=i2。 END IF。 RETURN(tmp)。 END max 第 4章 VHDL程序設(shè)計基礎(chǔ) 54 FUNCTION max (i1,i2:std_logic_vector) RETURN bit_vector IS VARIABLE tmp:std_logic_vector(i1?RANGE)。 BEGIN IF (i1i2) THEN tmp:=i1。 ELSE tmp:=i2。 END IF RETURN(to_bitvector(tmp))。 END max END example。 第 4章 VHDL程序設(shè)計基礎(chǔ) 55 三、運算符重載 重載運算符是指功能由用戶定義,并且可對各種類型的數(shù)據(jù)進行運算的運算符。 在 IEEE1076標(biāo)準(zhǔn)中, “ +”運算符僅被定義為用于標(biāo)量類型中的整數(shù)、浮點數(shù)和物理類型的加運算。然而設(shè)計人員在設(shè)計過程中,往往要對 std_logic_vector或者是 bit_vector類型的數(shù)據(jù)進行加運算,因此必須定義一個針對這些類型的函數(shù),這就是運算符的重載。 LIBRARY IEEE。 USE 。 USE 。 PACKAGE example IS FUNCTION“+”(1:integer。r:integer)RETURN integer。 第 4章 VHDL程序設(shè)計基礎(chǔ) 56 FUNCTION “+”(1:bit_vector。r:bit_vector)RETURN integer。 FUNCTION“+”(1:std_logic_vector。r:std_logic_vector) RETURN integer。 END example。 PACKAGE body example IS FUNCTION vector_to_int (a:IN bit_vector) RETURN integer IS VARIALBE result,tmp:integer:=0。 BEGIN FOR i IN a ?low TO a?high LOOP tmp:=0。 IF (a(i)=?1?) THEN tmp:=2**(ia?low)。 END IF 第 4章 VHDL程序設(shè)計基礎(chǔ) 57 result:=result+tmp。 END LOOP。 RETURN(result)。 END vector_to_int。 FUNCTION “+”(1:integer。r:integer)RETURN integer IS BEGIN RETURN(1+r)。 END。 FUNCTION“+”(1:bit_vector。r:bit_vector) RETURN integer IS BEGIN RETURN(vector_to_int(1)+vector_to_int(r))。 END。 第 4章 VHDL程序設(shè)計基礎(chǔ) 58 FUNCTION“+”(1:std_logic_vector。r:std_logic_vector) RETURN integer IS BEGIN RETURN (conv_integer(1)+con_integer(r))。 END。 END example。 四、別名(替換名) 別名的用途是對已有的對象定義一個替換名。 ALIAS 替換名:子類型表示符 IS 目標(biāo)名; 第 4章 VHDL程序設(shè)計基礎(chǔ) 59 ALIAS 語句可以在 ARCHITECTURE說明部分、 ENTITY說明部分、 PROCESS說明部分、 PACKAGE說明部分、 PACKAGE BODY語句而后 SUBPROGRAM說明部分。 別名為指令的每個字段提供命名的機制,并且可由別名直接去引用這個字段。 SIGNAL instruction:bit_vector(31 DOWNTO 0)。 ALAIS opcode:bit_vector(3 DOWNTO 0) IS instruction (31 DOWNTO 28)。 ALAIS source_reg:bit_vector(4 DOWNTO 0) IS instruction(27 DOWNTO 23)。 ALAIS targe_reg:bit_vector(4DOWNTO 0) IS instruction (322 DOWNTO 18)。 第 4章 VHDL程序設(shè)計基礎(chǔ)