CH11_套件函式与程序

上传人:z*** 文档编号:243098237 上传时间:2024-09-15 格式:PPT 页数:22 大小:1.04MB
返回 下载 相关 举报
CH11_套件函式与程序_第1页
第1页 / 共22页
CH11_套件函式与程序_第2页
第2页 / 共22页
CH11_套件函式与程序_第3页
第3页 / 共22页
点击查看更多>>
资源描述
,按一下以編輯母片標題樣式,按一下以編輯母片文字樣式,第二層,第三層,第四層,第五層,*,1,套件(,Packages)、,函式(,Functions),與程序(,Procedures),第十一章,儒林圖書公司,TB061,VHDL,數位電路設計實務教本,使用,Quartus II,套件(,Packages),2,Package,是一種設計單元,它用來宣告一些,VHDL,中可使用的物件,讓使用者可以將,VHDL,中的,Functions、Procedures,及資料型別的定義用一個,Package,包裝起來,而凡是在套件(,Package),內所定義和宣告的東西就變成是公開的,可以在全程式中使用。,Package,套件名稱,IS,-,套件宣告部份,套件宣告部份,END,套件名稱;,Package Body,套件名稱,IS,-,套件主體,套件主體之內容,END,套件名稱;,套件(,Packages),的使用,3,USE,work,.,my_package,.,all,而當我們的,VHDL,程式需要使用某一個套件裡所定義的函式或程序時,其語法如下:,USE,目錄名稱.套件名稱.指定的項目;,最簡單的例子就是在,VHDL,程式設計中一定會用使到的:,USE,ieee,.,std_logic_1164,.,all,上述的語法說明我們打算使用目前工作目錄(,Work Directory),下的,my_package,套件裡所有的副程式與資料型別。,其意即為使用,IEEE,目錄下的,std_logic_1164,套件內所有的副程式與資料型別。因此,若我們設計的,VHDL,程式需要用到自己所建立的,my_package,套件時,語法如下:,套件(,Packages)Example1,4,LIBRARY,ieee;,USE,ieee.std_logic_1164.all;,-,宣告在,Package,中所使用到的,Package,PACKAGE,comp,IS,-,在,Package,中宣告最小值比較器函式,function,minimum (constant a,b: integer),return,integer;,END,comp;,USE,ieee.std_logic_1164.all;,package,body,comp,is,-,在套件主體區中描述函式的內容,function,minimum (constant a,b: integer),return,integer,is,variable,c: integer;,-,區域變數宣告,begin,if,a b,then,c := a;,-,最小值為,a,else,c := b;,-,最小值為,b,end if;,return,c;,-,傳回其中之最小者,end,minimum;,end,comp;,套件(,Packages)Example1(,續),5,USE,p.all;,-,宣告使用之前建立的,Package,ENTITY,test_comp,is,PORT,( x,y :,IN,integer;,z :,OUT,integer);,END,test_comp;,ARCHITECTURE,a,OF,test_comp,IS,BEGIN,z=minimum (x,y);,-,呼叫使用,minimum,函式,END,a;,套件(,Packages)Example2,6,LIBRARY,ieee;,USE,ieee.std_logic_1164.all;,PACKAGE,my_package1,IS,constant,c:std_logic_vector:=00000011;,-,套件1中宣告常數,c,的值為00000011,END,my_package1,;,-,LIBRARY,ieee;,USE,ieee.std_logic_1164.all;,PACKAGE,my_package2,IS,constant,c:std_logic_vector:=00000100;,-,套件2中宣告常數,c,的值為00000100,END,my_package2,;,-,套件(,Packages)Example2(,續),7,LIBRARY,ieee;,USE,ieee.std_logic_1164.all;,USE,ieee.std_logic_unsigned.all;,USE,ieee.std_logic_arith.all;,Entity,exam_packm,IS,generic,(num_bits:integer:=8);,port,( A,B :,in,std_logic_vector(num_bits-1 downto 0);,Y1,Y2 :,out,std_logic_vector(num_bits-1 downto 0);,end,exam_packm;,Architecture,a,of,exam_packm,IS,Begin,Y1=A+work,.my_package1,.c;,-,使用,my_package1,中所宣告的常數,c (00000011),Y2=B+work,.my_package2,.c;,-,使用,my_package2,中所宣告的常數,c (00000100),end,a;,函式(,Functions),的語法,8,函式(,Functions),的宣告語法如下:,Function,函式名稱 (輸入參數:資料型別),Return,輸出參數型別;,函式(,Functions),的主體內容語法如下:,Function,函式名稱 (輸入參數:資料型別),Return,輸出參數型別,IS,函式內的區域變數(,Local Variable),宣告區,Begin,函式內的主體內容區,Return,輸出之參數名稱;,END,函式名稱;,函式(,Functions),的呼叫,9,函式(,Functions)-Example1,10,LIBRARY,ieee;,USE,ieee.std_logic_1164.all;,PACKAGE,my_package,IS,function,carry (bit1,bit2,bit3: std_logic),return,std_logic;,END,my_package;,LIBRARY,ieee;,USE,ieee.std_logic_1164.all;,package,body,my_package,is,function,carry (bit1,bit2,bit3: std_logic),return,std_logic,IS,variable,result:std_logic;,begin,result:=(bit1 and bit2) or (bit1 and bit3) or (bit2 and bit3);,return,result;,end,;,end,my_package;,函式(,Functions)-Example1 (,續),11,LIBRARY,ieee;,USE,ieee.std_logic_1164.all;,USE,work.my_package.all; -,-,使用目前工作目錄下的,my_package,套件,ENTITY,carry_pack,is,PORT,( a,b,c :,IN,std_logic;,Cout :,OUT,std_logic);,END,carry_pack;,ARCHITECTURE,a,OF,carry_pack,IS,BEGIN,Process,(a,b,c),Begin,Cout=carry(a,b,c);,End process;,END,a;,函式(,Functions)-Example2,12,LIBRARY,ieee;,USE,ieee.std_logic_1164.all;,entity,proc,is,port,( s :,in,integer range 0 to 15;,A :,in,integer range 0 to 31;,B :,in,integer range 0 to 31;,Result :,out,integer range 0 to 63);,end,proc;,Architecture,a,of,proc,IS,Function,Sum( A :,in,integer range 0 to 15;,-,兩整數相加函式,B :,in,integer range 0 to 15),Return,Integer,IS,Begin,Return,A+B;,End,Sum;,Function,Diff( A :,in,integer range 0 to 15;,-,兩整數相減函式,B :,in,integer range 0 to 15),Return,Integer,IS,Begin,Return,A-B;,End,Diff;,函式(,Functions)-Example2(,續),13,begin,process,(s),begin,if,(s=0),then,Result=Sum(A,B);,-s=0,時呼叫兩整數相加函式,elsif,(s=1),then,Result=Diff(A,B);,-s=1,時呼叫兩整數相減函式,else,Result=63;,-,當,s,為0和1以外條件時輸出值為63,end if,;,end process,;,end,a;,函式(,Functions)-Example3,14,LIBRARY,ieee;,USE,ieee.std_logic_1164.all;,ENTITY,function_calls,IS,PORT,( A,B,C,D,E:,IN,STD_LOGIC;,Y1,Y2,Y3 :,OUT,STD_LOGIC);,END,function_calls;,ARCHITECTURE,a,OF,function_calls,IS,function,Fn1(F1,F2,F3,F4:std_logic),return,std_logic,is,variable,temp:std_logic;,begin,temp:=(F1 xor F2) xor (F3 xor F4);,Return,temp;,end function,Fn1;,BEGIN,process,(A,B,C,D,E),begin,Y1=,Fn1,(A,B,C,D) xor E;,-,呼叫,Fn1,函式(使用位置對應表示法),Y2C,F4=D,F1=A,F2=B) xor E;,-,呼叫,Fn1,函式(使用名稱對應表示法),Y3D,F3=C);,-,呼叫,Fn1,函式(位置與名稱對應混合法),end process,;,end,a;,函式(,Functions)-Example4,15,ARCHITECTURE,a,OF,function_calle,IS,function,Fn1(F1,F2,F3,F4:std_logic),return,std_logic,is,variable,temp:std_logic;,begin,temp:=(F1 xor F2) xor (F3 xor F4);,Return,temp;,end function,Fn1;,BEGIN,Y1=,Fn1,(A1,B1,C1,D1) xor m1 xor m2;,process,(A1,B1,C1,D1,A2,B2,C2,D2,m1,m2),begin,Y2C2,F4=D2,F1=A2,F2=B2) xor m1 xor m2;,Y3D3,F3=C3) xor m1 xor m2;,end process,;,end,a;,函式(,Functions),應用-型別轉換(,Type Conversion),16,VHDL,是一種資料型別檢查非常嚴格的語言,不同的資料型別彼此之間不能直接作直接設定敘述或運算,;,因此我們必需採用型別轉換的函式將其資料型別轉換成完全相同的型別才能進一步作運算處理。,Library,IEEE;,Use,ieee.std_logic_1164.all;,entity,example,is,port,(in_x :,in,BIT;,out_y :,out,BOOLEAN);,end,example;,architecture,a,of,example,is,function,bit_to_boolean(in_bit :,in,BIT),return,BOOLEAN,is,variable,c: boolean;,begin,if,in_bit = 1,then,c:=TRUE;,else,c:=FALSE;,end if,;,return,c;,end,bit_to_boolean;,begin,out_y =,bit_to_boolean,(in_x);,end,a;,Example:,輸入為,BIT,而輸出傳回值為,BOOLEAN,的函式設計,函式(,Functions),應用-型別轉換(,Type Conversion),17,Altera Quartus II,在其,IEEE Library,裡提供有,std_logic_arith Package,,已經內含下面幾種格式轉換函式:,conv_integer,:,將,integer、unsigned、signed,或,std_ulogic,轉換成整數(,integer)。,conv_unsigned,:,將,integer、unsigned、signed,或,std_ulogic,轉換成,Unsigned,數值。,conv_std_logic_vector,:,將,integer、unsigned、signed,或,std_ulogic,轉換成,std_logic_vector。,函式(,Functions),應用-型別轉換簡例,18,LIBRARY,ieee;,USE,ieee.std_logic_1164.all;,USE,ieee.std_logic_arith.all;,-,指定使用,std_logic_arith,套件,entity,adder_t,is,port,(A,B:,in,unsigned(7 downto 0);,-A,B,宣告為,unsigned,的陣列輸入,result:,out,integer);,-result,宣告為整數(,integer),輸出,end,adder_t;,Architecture,a,of,adder_t,IS,begin,result=conv_integer(A+B);,-,將,A+B,運算結果由,unsigned,轉換成整數 輸出到,result,end,a;,函式(,Functions),應用-型別轉換簡例,19,USE,ieee.std_logic_1164.all;,USE,ieee.std_logic_arith.all;,-,指定使用,std_logic_arith,套件,entity,adder_con,is,port,(A,B:,in,unsigned(7 downto 0);,result:,out,std_logic_vector(7 downto 0);,end,adder_con;,Architecture,a,of,adder_con,IS,begin,result=conv_std_logic_vector(A+B,7);,-,本函式要設定待轉換的運算數,及,SIZE,位元等參數,end,a;,程序(,Procedures),的語法,20,程序(,Procedures),的傳回值則可以不限於一個(與函式最大的不同),在,VHDL,語言中,程序(,Procedure),的宣告語法如下:,Procedure,程序名稱 (,Signal,訊號,A:,資料型別;,Signal,訊號,B:,資料型別;,.,Signal,訊號,E:,資料型別;,Signal,訊號,M:,OUT,資料型別;,Signal,訊號,N:,OUT,資料型別 ),IS,Begin,程序的主體內容,END Procedure;,程序(,Procedures)Example:,四位元加法器的架構,21,ENTITY,adder4,IS,PORT,( a,b:,in,std_logic_vector(3 downto 0);,cin:,in,std_logic;,sum:,out,std_logic_vector(3 downto 0);,cout:,out,std_logic);,END,adder4;,ARCHITECTURE,structural,OF,adder4,IS,procedure,full_adder,(a,b,c,:,in,std_logic;,sum,cout,:,out,std_logic),IS,begin,sum:=a xor b xor c;,cout:=(a and b) or (a and c) or (b and c);,end,;,BEGIN,process,(a,b,cin),variable,result:std_logic_vector(3 downto 0);,variable,carry:std_logic;,begin,full_adder,(a(0),b(0),cin,result(0),carry,);,-,呼叫一位元全加法器程序,full_adder,(a(1),b(1),carry,result(1),carry,);,-,呼叫一位元全加法器程序,full_adder,(a(2),b(2),carry,result(2),carry,);,-,呼叫一位元全加法器程序,full_adder,(a(3),b(3),carry,result(3),carry,);,-,呼叫一位元全加法器程序,sum=result;,cout data := A;,when,01 = data := B;,when,10 = data := C;,when others,=data := D;,end case,;,end,MUX4x1;,begin,process,variable,Z : std_logic;,begin,MUX4x1(S,D0,D1,D2,D3,Z);,Y = Z ;,End process,;,end,a;,
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 小学资料


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!