接上页ARCHITECTUREbhvOFaxampISBEGINcl<=max(al,bI)--对函数max(a,b:INSTDLOGICVECTOR)的调用c2<=max(a2,b2);--对函数max(a,b:INBITVECTOR)的调用例8-4是程包年saaasgieX数iaab部分数结构G说R明部分制出了4个函数的函数首,在程ND体部分只列出了对应的部分内容,程序包体部分的UNSIGNED()函数是从USEIEEE.stdlogicarith库中调用的。在程序包体中的最大整型数检出函数maximum函数只有函数体,没有函数首,因为它只在程序包中调用。【例8-4】程序包首LIBRARYIEEE:USEIEEE.std logic1164.all:USEIEEE.std logic arith.all:PACKAGESTDLOGIC UNSIGNEDiSfunction"+"(L:STD LOGIC VECTOR;R:INTEGER)return STD LOGIC VECTOR:function"+"(L:INTEGER;R:STD LOGIC VECTOR)return STD LOGIC VECTOR:function"+"(L:STDLOGICVECTOR;R:STDLOGIC)return STD LOGIC VECTOR:function SHR(ARG:STD LOGIC VECTOR;COUNT:STDLOGIC VECTOR)returnSTD LOGICVECTOR;endSTD LOGIC_UNSIGNED;接下页
ARCHITECTURE bhv OF axamp IS BEGIN c1 <= max(a1,b1); -对函数max( a,b :IN STD_LOGIC_VECTOR)的调用 c2 <= max(a2,b2); -对函数max( a,b :IN BIT_VECTOR) 的调用 c3 <= max(a3,b3); -对函数max( a,b :IN INTEGER) 的调用 END; 接上页 【例8-4】 LIBRARY IEEE ; - 程序包首 USE IEEE.std_logic_1164.all ; USE IEEE.std_logic_arith.all ; PACKAGE STD_LOGIC_UNSIGNED is function "+" (L : STD_LOGIC_VECTOR ; R : INTEGER) return STD_LOGIC_VECTOR ; function "+" (L : INTEGER; R : STD_LOGIC_VECTOR) return STD_LOGIC_VECTOR ; function "+" (L : STD_LOGIC_VECTOR ; R : STD_LOGIC ) return STD_LOGIC_VECTOR ; function SHR (ARG : STD_LOGIC_VECTOR ; COUNT : STD_LOGIC_VECTOR ) return STD_LOGIC_VECTOR ; . end STD_LOGIC_UNSIGNED ; 接下页 例8-4是程序包use IEEE.std_logic_UNSIGNED中的部分函数结构,说明部分只列出了4个函数的函数 首,在程序包体部分只列出了对应的部分内容,程序包体部分的UNSIGNED()函数是从USE IEEE.std_logic_arith库中调用的。在程序包体中的最大整型数检出函数maximum函数只有函数体,没 有函数首,因为它只在程序包中调用
接上页程序包体LIBRARYIEEE:useIEEE.std logic 1164.all:use IEEE.std logic_arith.all ;packagebodySTD LOGIC UNSIGNEDisfunction maximum(L,R:INTEGER)return INTEGER isbeginifL>Rthen returnL;elsereturn R;endif;end;function"+"(L:STD LOGICVECTOR;R:INTEGER)return STDLOGIC VECTOR isVariableresult:STDLOGIC_VECTOR(L'range);BeginUNSIGNED(L)+R ;result:=return stdlogic_vector(result)end;endSTD LOGICUNSIGNED:
LIBRARY IEEE ; - 程序包体 use IEEE.std_logic_1164.all ; use IEEE.std_logic_arith.all ; package body STD_LOGIC_UNSIGNED is function maximum (L, R : INTEGER) return INTEGER is begin if L R then return L; else return R; end if; end; function "+" (L : STD_LOGIC_VECTOR ; R : INTEGER) return STD_LOGIC_VECTOR is Variable result : STD_LOGIC_VECTOR (L’range) ; Begin result := UNSIGNED(L) + R ; return std_logic_vector(result) ; end ; . end STD_LOGIC_UNSIGNED ; 接上页
8.3 VHDL子程序8.3.3VHDL转换函数表8-1IEEE库类型转换函数表功能函数名程序包:STDLOGIC1164由bit_vector类型的A转换为std_logic_vectorto_stdlogicvector(A)to_bitvector(A)由stdlogicvector转换为bit_vector由bit转换成stdlogicto_stdlogic (A)to_bit(A)由stdlogic转换成bit程序包:STD_LOGIC_ARITHconv_std_logic_vector(A,位长)将整数integer转换成stdlogicvector类型,A是整数conv_integer(A)将std_logic_vector转换成整数integer程序包:STDLOGICUNSIGNED由std_logic_vector转换成integerconv_integer(A)
8.3 VHDL子程序 8.3.3 VHDL转换函数 函数名 功能 程序包: STD_LOGIC_1164 to_stdlogicvector(A) 由bit_vector类型的A转换为std_logic_vector to_bitvector(A) 由std_logic _vector转换为bit_vector to_stdlogic (A) 由bit转换成std_logic to_bit(A) 由std_logic转换成bit 程序包: STD_LOGIC_ARITH conv_std_logic_vector(A, 位长) 将整数integer转换成std_logic_vector类型,A是整数 conv_integer(A) 将std_logic_vector转换成整数integer 程序包: STD_LOGIC_UNSIGNED conv_integer(A) 由std_logic_vector转换成integer 表8-1 IEEE库类型转换函数表
8.3 VHDL子程序8.3.3VHDL转换函数【例8-5】LIBRARYIEEE;USE IEEE.std logic 1164.ALL;ENTITY exg ISPORT (a,b : in bit_vector(3 downto O);q out std_logic_vector(3 downto 0));end;architecture rtl of exg isbeging<=to_stdlogicvector(a and b);--将位矢量数据类型转换成标准逻辑位矢量数据end;
8.3 VHDL子程序 8.3.3 VHDL转换函数 【例8-5】 LIBRARY IEEE; USE IEEE. std_logic_1164.ALL; ENTITY exg IS PORT (a,b : in bit_vector(3 downto 0); q : out std_logic_vector(3 downto 0)); end ; architecture rtl of exg is begin q<= to_stdlogicvector(a and b);-将位矢量数据类型转换成标准逻辑位矢量数据 end;
8.3 VHDL子程序8.3.3VHDL转换函数【例8-6】LIBRARY IEEE;USE IEEE.STD LOGIC 1164.ALL:USEIEEE.STDLOGICARITH.ALL;--注意使用了此程序包ENTITYaxamp ISPoRT(a,b,c:INintegerrangeoto15q:ouT std_logic_vector(3 downto 0));END;ARCHITECTURE bhv OF axamp ISBEGINq<=conv_std_logic_vector(a,4)when conv_integer(c)=8 elseconv_std_logic_vector(b,4);END;
8.3 VHDL子程序 8.3.3 VHDL转换函数 【例8-6】 LIBRARY IEEE; USE IEEE.STD_LOGIC_1164.ALL; USE IEEE.STD_LOGIC_ARITH.ALL;-注意使用了此程序包 ENTITY axamp IS PORT(a,b,c : IN integer range 0 to 15 ; q : OUT std_logic_vector(3 downto 0) ); END; ARCHITECTURE bhv OF axamp IS BEGIN q <= conv_std_logic_vector(a,4) when conv_integer(c)=8 else conv_std_logic_vector(b,4) ; END;