VHDL语言程序的基本结构.ppt
1,第二章 VHDL语言程序的基本结构,2,本章内容:,VHDL语言设计的基本单元及其构成VHDL语言构造体的子结构描述块语句进程语句子程序语句包集合、库及配置,3,完整VHDL语言程序结构,存放已经编译的包集合、实体、构造体和配置。,声明在实体中将用到的常数定义、数据类型、函数定义和过程定义等。,定义所设计电路系统的外部接口。,描述电路内部的功能。一个实体可以对应很多个构造体,但在同一时间,只有一个构造体被使用。,决定哪一个构造体被使用。,4,2.1 VHDL语言设计的基本单元及其构成,VHDL语言程序是用于描述硬件连接的结构性程序,采用文本文件编写。硬件电路模块具有外部接口和内部结构。,VHDL用程序模块表达硬件模块:设定外部端口、设计内部结构。,5,VHDL语言程序设计的基本单元由实体说明(Entity Declaration)和构造体定义(Architecture Definition)两部分构成。,实体说明部分:规定设计单元的输入输出接口信号或引脚;它对应于电路外观图。构造体定义部分:定义设计单元的具体构造和操作(行为);它对应于电路原理图。,6,VHDL语言程序设计的基本单元,VHDL语言程序设计的基本单元由实体说明(Entity Declaration)和构造体定义(Architecture Definition)两部分构成。实体说明部分:规定设计单元的输入输出接口信号或引脚。构造体定义部分:定义设计单元的具体构造和操作(行为)。,7,architecture connect of mux issignal tmp:bit;begin cale:process(d0,d1,sel)is variable tmp1,tmp2,tmp3:bit;begin tmp1:=d0 and sel;tmp2:=d1 and(not sel);tmp3:=tmp1 or tmp2;tmp=tmp3;q=tmp after m;end process;end architecture connect;,二选一电路的VHDL语言描述,entity mux isgeneric(m:time:=1 ns);port(d0,d1,sel:in bit;q:out bit);end entity mux;,实体说明,构造体定义,8,1.实体说明,entity 实体名 is类属参数说明;-确定局部常量或实体时限端口说明;-确定输入/输出端口数量及类型end entity 实体名;,entity mux isgeneric(m:time:=1 ns);port(d0,d1,sel:in bit;q:out bit);end entity mux;,1)实体说明的基本格式:,9,VHDL语言不区分大小写;除了第一行 entity is 以外,每一句以分号“;”结束;编写程序时,一行可以含若干句(以分号间隔),一句也可以写若干行;在一句结束后,可以在“-”符号后接说明文字,有助于理解程序,不会对编译产生影响;单词之间必须使用空格;并列信号间使用逗号;根据不同的层次关系最好设定不同的缩进。,10,generic(类属常量名:类型:=静态表达式;类属常量名:类型:=静态表达式);,要点:(1)类属说明必须在端口说明之前,为设计实体和外部环境提供静态数据传输通道;(2)generic 为关键字,静态表达式为可选项;(3)示例:generic(m:time:=1 ns);q=tmp after m;,2)类属参数说明格式:,11,3)端口说明格式:,port(端口名,端口名:方向 数据类型名;端口名,端口名:方向 数据类型名);,端口说明是对基本设计实体(单元)与外部接口的描述,也可以说是对外部引脚信号的名称,数据类型和输入、输出方向的描述。,注:port为关键字,12,端口说明语法要点,(1)端口名是赋予每个外部引脚的名称;,命名规则:英文字母和数字构成,字母开头;可在名称中使用单个下划线符号_;字母不分大小写;名称应具有意义,方便记忆;名称不能重复使用;VHDL中的关键字保留字不能用做名称。,注:该命名规则适用于实体、端口、信号、变量、文件的命名,13,(2)端口方向:定义外部引脚的信号方向是输入还是输出。,14,15,(3)端口数据类型,所有端口都必须规定其数据类型,VHDL语言中有10种数据类型,在数字电路设计中最常用的类型为:bit 和 bit_vector。Bit:单个逻辑量Bit_vector:逻辑数组、总线逻辑量在VHDL语言的标准库IEEE库当中的包集合std_logic_1164提供的std_logic和std_logic_vector分别与bit和bit_vector对应,完全等效。只是在使用时要声明使用了该包集合。下面给出一个例子。,16,例2-1entity mu isport(d0,d1,sel:in bit;q:out bit;bus:out bit_vector(7 downto 0);end mu;,例2-2library ieee;;entity mu isport(d0,d1,sel:in std_logic;q:out std_logic;bus:out std_logic _vector(7 downto 0);end mu;,实体说明示例,17,2.构造体(结构体),具体指明基本设计单元的行为、元件及内部的连接关系,也就是说它定义了设计单元具体的功能。,architecture 构造体名 of 实体名 is 定义语句;-内部信号、常数、数据类型、函数等的定义 begin 并行处理语句;-构造体中所有语句同时执行,不以书写 顺序为执行顺序 end architecture 构造体名;,1)构造体的基本格式:,18,2)构造体的命名,每个构造体必须属于一个实体;每个构造体必须有一个名称:命名要符合命名规则命名可根据设计者 采用何种描述方式 来描述模块的功能来命名,给阅读程序的人带来方便。如:beh(行为描述,基本设计单元的数学模型描述)rtl(寄存器传输描述,数据流描述)str(结构描述,逻辑元件的连接)例:architecture str of mux2_1 is,19,3)定义语句位于is-begin 之间:,例:architecture beh of mux2_1 is signal nes1:bit;-内部连接信号无须说明方向 begin end architecture beh;,type declarations;类型说明 signal declarations;信号说明 constant declarations;常量说明 component declarations;元件说明 function definitions;函数说明 procedure definitions;过程说明,20,4)并行处理语句,begin-end 之间语句部分是各种并行语句,具体地描述构造体的行为及其连接关系,各语句处于并列状态,执行时不分先后次序。,entity mux is port(d0,d1,sel:in bit;q:out bit);end entity mux;,architecture dataflow of mux issignal q0,q1:bit;begin q0=d0 and sel;q1=not sel and d1;q=q0 or q1;end architecture dataflow;,21,并行语句,块语句:由一系列并行语句组成,从形式上划分出模块,改善程序的可读性。,进程语句:进程内部为顺序语句,而不同进程间则是并行执行的,进程只有在某个敏感信号发生变化时才会触发。,子程序调用:调用过程或函数,并将获得的结果赋给信号。,信号代入语句:将实体内的处理结果向定义的信号或端口进行赋值。,元件例化语句:调用其他设计实体描述的电路,将其作为本设计实体的一个元件,元件例化是实现层次化设计的重要语句。,22,练习,library ieee;use ieee.std_logic_1164.all;entity kdecoder38 is port(din:in std_logic_vector(2 downto 0);en:in std_logic;dout:out std_logic_vector(7 downto 0);end entity kdecoder38;,为3-8线译码器编写实体说明,23,练习,编写一个n选1数据选择器的实体说明,n在类属参数说明中设定,数据类型为整型integer,设n4,输入输出端口数据类型使用std_logic 和std_logic_vector。,library ieee;use;entity kmux_n is generic(n:integer:=4;m:integer:=2);port(din:in std_logic_vector(n-1 downto 0);sel:in std_logic_vector(m-1 downto 0);y:out std_logic);end entity kmux_n;,24,2.2 VHDL语言构造体的子结构描述,一个构造体可以用几个子结构来构成,即使用相对比较独立的几个模块来构成。VHDL有以下三种形式的子结构描述语句:块(block)语句结构;进程(process)语句结构;子程序(subprograms)语句结构。过程(procedure)语句函数(function)语句,25,1.块语句结构描述,architecture connect of mux issignal tmp1,tmp2,tmp3:bit;begin cale:block begin tmp1=d0 and sel;tmp2=d1 and(not sel);tmp3=tmp1 or tmp2;q=tmp3;end block cale;end architecture connect;,块结构名:blockbegin并发语句;end block 块结构名;,entity mux isport(d0,d1,sel:in bit;q:out bit);end entity mux;,1)格式,26,2)块和子原理图的关系,27,3)块内语句的并发性 begin到end block cale之间的语句为并发执行语句。4)卫式块(guarded block)实现块的执行控制:当某种条件满足时,块语句执行;否则,块语句不执行。,block 卫式布尔表达式,格式:,注意:卫式块不能进行逻辑综合!,28,entity latch is port(d,clk:in bit;q,qb:out bit);end entity latch;,architecture latch_guard of latch is begin g1:block(clk=1)begin q=guarded d after 5 ns;qb=guarded not(d)after 7 ns;end block g1;end architecture latch_guard;,卫式块举例,卫式布尔表达式,前卫关键字,只有卫式布尔表达式为真时,该语句执行,29,2.进程语句结构描述,进程名:process(敏感量列表)is 声明部分;begin 顺序语句;end process;,architecture connect of mux isbegin cale:process(d0,d1,sel)isvariable tmp1,tmp2,tmp3:bit;begin tmp1:=d0 and sel;tmp2:=d1 and(not sel);tmp3:=tmp1 or tmp2;q=tmp3;end process;end architecture connect;,entity mux isport(d0,d1,sel:in bit;q:out bit);end entity mux;,1)格式,30,2)进程内部语句的顺序性 进程结构中的语句是按顺序一条一条向下执行的,与C语言的执行一样。3)进程的启动 敏感量列表中任意一个信号发生变化就可以启动这个进程的执行。,31,sensitivity list includes all inputs used in the combinatorial logic,sensitivity list does not include the d input,only the clock or/and control signals,Sequential Process Sensitive to Specific Inputs(i.e.Clock and/or Control Signals)Example process(clr,clk),Combinatorial Process Sensitive to All Inputs Example process(a,b,sel),Types of Processes,32,例:具有两个进程的构造体,entity pros_com is port(event_a:in bit);end entity pros_com;architecture catch_ball of pros_com is signal to_a,to_b:bit:=0;begin,4)进程的同步描述 构造体中的多个进程可以进行通信、并行同步执行。,33,a:process(event_a,to_a)is begin if(event_aevent and event_a=1)or(to_aevent and to_a=1)then to_b=1 after 20 ns,0 after 30 ns;end if;end process a;,b:process(to_b)is begin if(to_bevent and to_b=1)then to_a=1 after 10 ns,0 after 20 ns;end if;end process b;end architecture catch_ball;,34,两个进程a,b,一个结构体中有两个进程a和b。当a进程处理结束,就使信号量to_b=1。to_b是进程B的输入信号,当进程B敏感到to_b有变化,且to_b=1时,则进程b就被启动;同样,进程B处理结束或当进程a敏感到to_a有变化,且to_a=1,则进程a就启动。如此反复循环,使两个进程能并发地同步工作。,35,3.子程序语句结构描述,子程序就是在主程序调用它以后能将处理结果返回主程序的程序模块,其含义和其它高级语言中的子程序概念相当。它可以反复调用,使用非常方便。子程序在调用时首先要进行初始化,执行结束后子程序就终止。再调用时要再进行初始化。因此子程序内部的值不能保持,子程序返回以后才能被再调用,它是个非重入的程序。子程序的两种类型:过程(procedure);函数(function).,36,1)过程语句,procedure 过程名(参量1:方向 类型;参量n:方向 类型)is 定义语句;begin顺序语句;end procedure 过程名;,(2)过程的调用格式,(1)语句格式,过程名(信号列表);,37,(3)procedure语法要点,a 过程依靠参量与主程序交换信息,参量说明紧跟在过程名后面的括号中;可视为多输入/多输出电路模块。b 参量方向为 in 输入 作为常量 out 输出 作为变量或信号 inout 输入输出 作为变量或信号 若要作为信号,则应在参量名前加注signal。,38,c 在主程序中,函数调用通常在表达式中,过程调用通常在语句中;调用结束时,输出量将赋值给调用时指定的信号或变量;d 过程内部可以定义局部使用的类型、常量、变量、函数、过程,不能定义信号;e 过程可以进行并行调用,也可以进行顺序调用;并行过程的参量应该为信号量;顺序过程的参量为变量,可以利用顺序赋值语句将输出结果传递给信号量;,39,(4)过程语句及过程调用举例,library ieee;use ieee.std_logic_1164.all;entity butnot1 is port(x,y:in std_logic;z:out std_logic);end entity butnot1;architecture str of butnot1 isprocedure kinv1(a:in std_logic;f:out std_logic)isbegin f:=not a;end procedure kinv1;,procedure kand21(a,b:in std_logic;f:out std_logic)isbegin f:=a and b;end procedure kand21;beginprocess(x,y)isvariable z1,temp:std_logic;begin kinv1(y,temp);kand21(x,temp,z1);z=z1;end process;end architecture str;,40,2)函数语句,function 函数名(参量1:类型;参量n:类型)return 数据类型名 is 定义语句;begin 顺序处理语句;return 返回变量名;end function 函数名;,(1)格式,41,(2)函数调用格式,函数名(信号列表);,(3)function语法要点,a 函数可以看作是一种多输入/单输出的电路模块;b 函数参量表中带有若干形式参量,在调用时由实际信号取代,实现主程序向函数的输入;在函数内部,信号被作为常量对待;,42,c 函数应用时,为了返回函数值,可在函数体中设置变量,在函数执行过程中为该变量赋值;然后用return语句将该变量返回(赋值给函数名);该变量应与规定的返回类型一致;在一次调用中,只有一个返回语句带回函数值;d 函数内部可以定义局部使用的类型、常量、变量、函数、过程;但不能定义信号;e 函数的可执行部分由顺序语句构成;不包含信号代入语句;f 函数通常在代入语句的表达式中调用,函数返回类型应与该表达式类型一致;,43,(4)函数语句及函数调用举例,library ieee;use ieee.std_logic_1164.all;entity kdec38 is port(x,y,z:in std_logic;f:out std_logic_vector(7 downto 0);end entity kdec38;,architecture d of kdec38 is function kand3(a,b,c:std_logic)return std_logic is variable result:std_logic;begin result:=a and b and c;return result;end function kand3;,函数,44,(4)函数语句及函数调用举例,begin f(0)=kand3(not x,not y,not z);f(1)=kand3(not x,not y,z);f(2)=kand3(not x,y,not z);f(3)=kand3(not x,y,z);f(4)=kand3(x,not y,not z);f(5)=kand3(x,not y,z);f(6)=kand3(x,y,not z);f(7)=kand3(x,y,z);end architecture d;,45,2.3 包集合、库及配置,1.库(Library)库是编译后数据的集合,存放包集合定义、实体定义、构造体定义和配置定义,其功能相当于操作系统中的目录,经过说明后,设计中就可以使用库中的数据,实现共享。比如,在前面图形输入的应用中,就采用了基本库中的单元进行设计。,46,1)库的使用,例:library ieee;use ieee.std_logic_1164.all;,library 库名;use 库名.包集合名.范围(或项目名);,(1)库说明语句格式,库的作用范围:从实体说明开始到它所属的构造体、配置为止。,47,每个实体都应独立进行库的说明;库的说明应该在实体之前;经过说明后,实体和结构体就可以调用库中的资源。,2)库的种类,ieee库 std库 asic库 work库 用户定义库,(1)ieee库:含有IEEE的标准包集合“std_logic_1164”以及一些大公司提供的包集合,如:synopsys提供的包集合“std_logic_unsigned”、“std_logic_arith”;使用前必须说明;,48,例:library ieee;use ieee.std_logic_1164.all;使用标准逻辑量的定义和运算;use;无符号数算术运算的定义;use;使用符号数算术运算的定义;(2)std库:std库是VHDL的标准库,含有“standard”包集合和“textio”包集合,使用包集合“standard”时不需要说明,“textio”包集合需进行说明;例:library std;use;,49,(3)asic库:由各公司提供,存放与逻辑门一一对应的实体,用于ASIC设计的门级仿真,使用时要加以说明。,例:library altera;use altera.maxplus2.all;library lpm;use;,50,(4)work库:work库为现行作业库,位于当前设计文件的指定保存目录;work库使用时不需要说明。(5)用户定义库:用户为自身设计需要所开发的程序包和实体等,也可以汇集在一起定义成一个库,这就是用户定义库或称用户库。在使用时要首先说明库名。,51,2.包集合(Package),用于罗列VHDL语言中使用的类型定义、信号定义、常数定义、元件定义、函数定义和过程定义等,方便不同设计模块中,公共定义的共享;1)数字电路设计中经常使用的包集合 ieee.std_logic_1164 逻辑量的定义 ieee.std_logic_arith 数据转换,逻辑判断 ieee.std_logic_unsigned 算术运算 std.textio 文本数据输入/输出格式,52,3)包集合的书写结构,包集合体(package body)是可选项:一般程序包标题列出所有项的名称;程序包体具体给出各项的细节。,package body 包集合名 is 说明语句;(完整定义)end package body包集合名;,(1)包集合标题:,(2)包集合体:,使用前必须采用use语句进行说明;,2)包集合的使用,package 包集合名 is 说明语句;(只有名称)end package 包集合名;,53,library ieee;use ieee.std_logic_1164.all;,package body upac isfunction conv_integer(x:std_logic_vector)return integer isvariable result:integer;begin result:=0;for i in xrange loop result:=result*2;case x(i)is when 0|L=null;when 1|H=result:=result+1;when others=null;end case;end loop;return result;end function conv_integer;end package body upac;,使用该用户自定义包集合的格式:use;,package upac isconstant k:integer:=4;subtype cpu_bus is std_logic_vector(k-1 downto 0);function conv_integer(x:std_logic_vector)return integer;end package upac;,54,3.配置(Configuration),在一个实体内可以编写多种不同的构造体,通过配置语句来进行选择;便于在仿真时进行性能对比试验,得到性能最佳的构造体。配置语句格式,configuration 配置名 of 实体名 is 说明语句;end configuration 配置名;,55,configuration 配置名 of 实体名 is for 选择的构造体名 end for;end configuration 配置名;,1)不含元件的构造体配置语句,例2-14 两种形式计数器的配置,library ieee;use ieee.std_logic_1164.all;entity counter is port(load,clear,clk:in std_logic;data_in:in integer;data_out:out integer);end entity counter;,56,architecture count_255 of counter isbegin end architecture count_255;,architecture count_64k of counter isbegin end architecture count_64k;,configuration small_count of counter is for count_225 end for;end configuration small_count;configuration big_count of counter is for count_64k end for;end configuration big_count;,57,2)包含元件的构造体配置语句,configuration 配置名 of 实体名 is for 选择的构造体名 for 元件标号:元件名 use configuration 库名.配置名;end for;for 元件标号:元件名 use entity 库名.实体名(构造体名);end for;end for;end configuration 配置名;,58,例2-15 反相器和三输入与门电路描述,library ieee;entity inv is port(a:in std_logic;b:out std_logic);end entity inv;architecture behave of inv is begin b=not(a)after 5 ns;end architecture behave;configuration invcon of inv is for behave end for;end configuration invcon;,配置,实体,构造体,59,library ieee;;entity and3 isport(a1,a2,a3:in std_logic;o1:out std_logic);end entity and3;architecture behave1 of and3 isbegin o1=a1 and a2 and a3 after 5 ns;end architecture behave1;architecture behave2 of and3 isbegin o1=a1 and a2 and a3 after 10 ns;end architecture behave2;,configuration and3con1 of and3 is for behave1 end for;end configuration and3con1;configuration and3con2 of and3 is for behave2 end for;end configuration and3con2;,两个不同延时的三输入与门,60,例2-16 构成译码器的程序,library ieee;use ieee.std_logic_1164.all;entity decode is port(a,b,en:in std_logic;q0,q1,q2,q3:out std_logic);end entity decode;architecture stu of decode is component inv is port(a:in std_logic;b:out std_logic);end component inv;,61,component and3 is port(a1,a2,a3:in std_logic;o1:out std_logic);end component and3;signal nota,notb:std_logic;begin i1:inv port map(a,nota);i2:inv port map(b,notb);a1:and3 port map(nota,en,notb,q0);a2:and3 port map(a,en,notb,q1);a3:and3 port map(nota,en,b,q2);a4:and3 port map(a,en,b,q3);end architecture stu;,62,例2-17 低层次配置的选择配置,configuration decode_llcon of decode is for stu for i1:inv use configuration work.invcon;end for;for i2:inv use configuration work.invcon;end for;for a1:and3 use configuration work.and3con1;end for;for others:and3 use configuration work.and3con2;end for;end for;end configuration decode_llcon;,配置名,63,例2-18 实体与构造体对应的配置,configuration decode_eacon of decode is for stu for all:inv use entity work.inv(behave);end for;for a1:and3 use entity work.and3(behave1);end for;for others:and3 use entity work.and3(behave2);end for;end for;end configuration decode_eacon;,实体名,构造体名,64,作业3,P274.T1、T2、T3、T6、T7、T8、T10,