第五章 vhdl 编程

229
Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.html http://www.qcxy.hb.cn/dxx/Index.html 第第第 第第第 VHDL VHDL 第第 第第

Upload: zared

Post on 16-Mar-2016

92 views

Category:

Documents


2 download

DESCRIPTION

第五章 VHDL 编程. VHSIC—Very High Speed Integrated Circuit ( 1982 年) 由美国国防部 (DOD) 制定,以作为各合同商之间提交复杂电路设计文档的一种标准方案 . 1987 年被采纳为 IEEE 1076 标准 1993 年被更新为 IEEE 1164 标准. 第五章 、 VHDL 设计初步. VHDL: VHSIC Hardware Description Language. HDL 的出现是为了适应电子系统设计的日益复杂性。若以计算机软件的设计与电路设计做个类比:. 机器码好比晶体管 /MOS 管 ;. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第五章 第五章 VHDL VHDL 编程编程

Page 2: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

□VHSIC—Very High Speed Integrated Circuit( 1982 年)

·由美国国防部 (DOD) 制定,以作为各合同商之间提交复杂电路设计文档的一种标准方案 .·1987 年被采纳为 IEEE 1076 标准·1993 年被更新为 IEEE 1164 标准

第五章、 VHDL 设计初步

Page 3: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL: VHSIC Hardware Description Language

HDL 的出现是为了适应电子系统设计的日益复杂性。若以计算机软件的设计与电路设计做个类比:机器码好比晶体管 /MOS 管;汇编语言好比网表;HDL 语言就如同高级语言。

Page 4: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL: VHSIC Hardware Description Language三种常用的 HDL 语言:

1。 VHDL 硬件描述语言。它适用于电路行为级、 RTL 级的描述,功能强,规范性好;2。 Verilog 硬件描述语言。它适用于电路 RTL级、门级的描述,功能强、灵活性高;3。 ABEL 硬件描述语言。它也适用于电路

RTL 级、门级的描述,性能上比 Verilog 差。 VHDL和 Verilog 已成为 IEEE 标准。

Page 5: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL: VHSIC Hardware Description Language VHDL 在语法和风格上类似与现代高级编程语言。但要注意, VHDL 毕竟描述的是硬件,它包含许多硬件特有的结构。是用文字化方法描述电子电路与系统。

计算机语言描述的特点是: 顺序性;HDL 语言描述的特点是: 并行性。

Page 6: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL: VHSIC Hardware Description Language

VHDL 的程序结构特点:

库、程序包

实体结构体

元件、模块、系统的外部可视部分元件、模块、系统的内部不可视部分

实体内在功能的实现

工程设计

Page 7: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL: VHSIC Hardware Description Language

5.1 多路选择器的 VHDL 描述ab

ymux21

s

ENTITY mux21 IS PORT ( a, b, : IN BIT ; S : IN BIT ;

y : OUT BIT ) ; END mux21 ;

例 1.  2选1多路选择器的实体描述

Page 8: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL: VHSIC Hardware Description Language结构体描述 1

ARCHITECTURE behave OF mux21 ISBEGIN

Y <= a WHEN s=‘0’ ELSE b ;

END behave;S=0

S=1

ab

y

S

行为描述

Page 9: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL: VHSIC Hardware Description Language

结构体描述 2

d <= a AND (NOT s); e <= b AND s ; y <= d OR e ;

ARCHITECTURE behave OF mux21 IS

BEGIN

END behave;

SIGNAL d, e, : BIT ;--d,e 为内部信号

a

s

by

d

e原理图描述

Page 10: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL: VHSIC Hardware Description Language

结构体描述 3

y <= (a AND (NOT s)) OR ( b AND s) ;

ARCHITECTURE behave OF mux21 ISBEGIN

END behave;

布尔方程描述: y = as+bs

以上三种结构体描述都是并行语句

Page 11: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL: VHSIC Hardware Description Language结构体描述 4

IF s=‘0’ THEN

ARCHITECTURE behave OF mux21 ISBEGIN

END behave;

PROCESS( a, b,s )BEGIN

y<=a ;ELSE y<=b ;

END IF ;END PROCESS ;

进程描述

--a,b,s 为敏感表信号S=0

S=1

ab

y

S

Page 12: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL: VHSIC Hardware Description Language仿真结果如下:

mux21 仿真时序

Page 13: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL: VHSIC Hardware Description Language

5.2 寄存器的 VHDL 描述例 2 D 触发器 D

CLK

Q

LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;ENTITY DFF IS

PORT (CLK , D : IN_STD_LOGIC;Q : OUT_STD_LOGIC);

END DEF;

Page 14: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

PROCESS (CLK)

ARCHITECTURE behave OF DEF IS

BEGIN

END behave;

SIGNAL Q1 : STD_LOGIC ;

VHDL: VHSIC Hardware Description Language

BEGINIF CLK’EVENT AND CLK=‘1’

THEN Q1<=D;END IF;

Q <= Q1;END PROCESS;

Page 15: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法基本构成

VHDL 设计VHDL 文件程序包( Packages )声明在设计或实体中将用到的常数,数据类型,元件及子程序等实体( Entities ) 声明到其实体及其设计的接口,即定义本设计的输入 / 出端口

结构体( Architectures ) 定义了实体的实现。即电路的具体描述

Page 16: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL 设计VHDL 文件程序包( Packages )声明在设计或实体中将用到的常数,数据类型,元件及子程序等

VHDL基本结构与语法基本构成

Page 17: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法:

程序包 (Package)/库 (Library)

程序包定义了一组数据类型说明、常量说明、元件说明和子程序说明。以供其它多个设计实体引用。 库是专门存放预先编译好的程序包的地方,这样它们就可以在其它设计中被调用。

Page 18: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法:

程序包 (Package)/库 (Library)

在 VHDL 语言中,数据类型、常量与子程序可以在实体说明部分和结构体部分加以说明;而且实体说明部分所定义的类型,常量及子程序在相应的结构体中是可以被使用。但是,在一个实体的说明部分与结构体部分中定义的数据类型,常量及子程序对于其它实体的说明部分与结构体部分是不可见的。

Page 19: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

程序包 / 库就是为了使一组类型说明,常量说明和子程序说明对多个设计实体都成为可见的而提供的一种结构。它们如同 C 语言中的 *.H 文件,定义了一些类型说明, 函数一样。

VHDL基本结构与语法:

程序包 (Package)/库 (Library)

Page 20: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法:

程序包 (Package)/库 (Library)例: LIBRARY ieee;

USE ieee.std_logic_1164.ALL; USE ieee. std_logic_unsigned.ALL ; ieee是 ieee 标准库的标志名,两个 USE语 句 使 得 以 下 设 计 可 使 用 程 序 包std_logic_1164, std_logic_unsigned 中预定义的内容,如 std_logic , in , out 的定义。

Page 21: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法:

std 库, ieee 库中的程序包 库 名 程序包名 包中预定义内容 std standard VHDL类型,如 bit, bit_vector ieee std_logic_1164 定义 std_logic, std_logic_vector等 ieee numeric_std 定义了一组基于 std_logic_1164中定义的类型的

算术运算符,如“ +” “, -” ,SHL,SHR等。 ieee std_logic_arith 定义有符号与无符号类型,及基于这些类型上的

算术运算。 ieee std_logic_signed 定义了基于 std_logic与 std_logic_vector类型上的

有符号的算术运算。 ieee std_logic_unsigned 定义了基于 std_logic与 std_logic_vector类型上的

无符号的算术运算。

Page 22: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法基本构成VHDL 设计

VHDL 文件

实体( Entities ) 声明到其他实体及其他设计的接口,即定义本设计的输入 / 出端口

Page 23: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法:

实体( Entity ) VHDL 表达的所有设计均与实体有关,实体是设计中最基本的模块。 设计的最顶层是顶层实体。如果设计分层次,那么在顶级实体中将包含较低级别的实体。 实体中定义了该设计所需的输入 / 输出信号,信号的输入 / 输出类型被称为端口模式,同时,实体中还定义他们的数据类型。

Page 24: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法:

实体( Entity ) 实体的格式如下: entity<entity_name 实体名 >is port <port list for your design , 列出设计的输入 / 输出信号端口 > end<entity_name>;

Page 25: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法:

实体( Entity )例: ENTITY cntm16 IS -- 实体

PORT(ci : IN std_logic;nreset : IN std_logic;clk : IN std_logic; co : out std_logic;qcnt : buffer std_logic_vector(3 downto 0) );

END cntm16;

信号名 端口类型端口模式

Page 26: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法:

实体 : 端口模式( MODE )端口模式( MODE )有以下几种类型: IN ; OUT ; BUFFER ; INOUT端口模式可用下图说明:(黑框代表一个设计或模块)

INOUTIN OUT BUFFER

Page 27: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

实体 : 端口类型( TYPE ) 端口类型( TYPE )定义端口的数据类型,包括以下几种:

integer 用作循环的指针或常数,通常不用于 I/O 信号,例如: SIGNAL count :integer range 0 to 255 count <= count + 1

bit 可取值‘ 0’ 或‘ 1’ ; std_logic 工业标准的逻辑类型,取值‘ U’,‘X’,‘0’,‘1’,‘Z’, ‘W’, ‘L’, ‘H’ 和‘ -’ --由 IEEE std 1164 标准定义;

Page 28: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法:

实体 : 端口类型( TYPE ) std_logic_vector, std_logic 的组合,工业标准的逻辑类型定义总线,如: data: in std_logic_vector(7 downto 0);则 data 表示 8 根数据线。 如给 data 赋值 5AH 可写为: data<=“01011010”;或data<=X”5A”; X 表示 16 进制。

Page 29: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法基本构成VHDL 设计VHDL 文件

结构体( Architectures ) 定义了实体的实现。即电路的具体描述

Page 30: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法:

结构体( Architecture ) 所有能被仿真的实体都由一个结构体描述, 结构体描述实体的行为功能。即设计的功能,是实体中的具体逻辑。一个实体可以有多个结构体,一种结构体可能为行为描述,而另一种结构体可能为设计的结构描述或数据通道的描述。

Page 31: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法:

结构体( Architecture ) 一个完整的、能够被综合实现的VHDL 设计必须有一个实体和对应的结构体。一个实体和其对应结构体可构成一个完整的 VHDL 设计。一个实体可对应一个结构体或多个结构体。

结构体是 VHDL 设计中最主要部分,它一般由一些各子部分构成,如下图所示:

Page 32: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法:

结构体( Architecture )结构体( Architecture )声明区( Declarations )信号声明;声明用于该结构体的类型,常数,元件,子程序。并发语句信号赋值( Signal Assignments计算结果,并赋值给信号元件例化( Component Instantiations )调用另一个实体所描述的电路。即元件调用

过程调用( Procedure Calls调用一个预先定义好的一个算法。进程( Processes )定义一个新算法实现电路功能。在进程中赋值顺序语句。语句按放置的顺序执行。

Page 33: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法:

结构体( Architecture )一般格式如下:architecture< 结构体名 > of < 实体名 >is

-- 结构体声明区域 -- 声明结构体所用的内部信号及数据类型 -- 如果使用元件例化,则在此声明所用的元件 begin -- 以下开始结构体,用于描述设计的功能 --concurrent signal assignments 并行语句信号赋值 --processes 进程(顺序语句描述设计) --component instantiations 元件例化 end< 结构体名 >;

Page 34: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

5.3 1 位二进制全加器的 VHDL 设计例 3 、一个一位二进制全加器设计。

H_ADDERAB SO

CO

半加器逻辑原理图

A

B

CO

SO

&

&

1 、半加器

F_ADDERAINBIN

SUM

CY

CIN

Page 35: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法基本构成2 、全加器

H_ADDER

AB SO

CO H_ADDER

AB SO

CO

AIN

BIN

CY

SUMCIN

D

EF

AB

C

F_ADDERAINBIN

SUM

CY

CIN

Page 36: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

3 、 VHDL 语言描述LIBRARY IEEE ; -- 库,程序包调用 USE IEEE.STD_LOGIC_1164.ALL ; ENTITY OR2 IS -- 实体 OR2 描述 PORT (A , B : IN STD_LOGIC ; C:: OUT STD_LOGIC) ; END OR2 ; ARCHITECTURE ART1 OF OR2 IS -- 结构体描述 BEGIN C<=A OR B ; END ART1 ;

AB

C( 1 )或门

Page 37: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

LIBRARY IEEE ; USE IEEE.STD_LOGIC_1164.ALL ; ENTITY H_ADDER IS -- 实体 H_ADDER 描述 PORT (A , B ; IN STD_LOGIC ; CO, SO ; OUT STD_LOGIC) ; END H_ADDER ;ARCHITECTURE ART2 OF H_ADDER IS BEGIN -- 结构体描述 SO<=(A OR B) AND (A NAND B) ; CO<=NOT(A NAND B) ; END ART2

H_ADDER

AB SO

CO

A

B

CO

SO

&

&

( 2 )半加器描述1

Page 38: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

半加器结构体描述 2ARCHITECTURE ART2a OF H_ADDER IS

END ART2a;

SIGNAL abc : std_logic_vector(1 downto 0) ;

END case;

BEGIN

case abc iswhen “00” => so <=‘0’ ; co<=‘0’ ;when “01” => so <=‘1’ ; co<=‘0’ ;when “10” => so <=‘1’ ; co<=‘0’ ;when “11” => so <=‘0’ ; co<=‘1’ ;when other => null ;

END process;

abc <= a&b;process (abc)begin

相当于“ then”

Page 39: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法:VHDL运算符:连接运算符

& 连接,将两个对象或矢量连接成维数更大的矢量 , 它可给代码书写带来方便。例,为表达 a=‘1’ and b=‘0’ and c=‘1’概念,可定义一个变量: vabc=a & b & c ;则可用 vabc=“101” 表达上述内容。

Page 40: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

半加器结构体描述 3ARCHITECTURE ART2b OF H_ADDER IS

END ART2b;

SIGNAL abc, cso : std_logic_vector(1 downto 0) ;

END case;

BEGIN

case abc iswhen “00” => cso <=“00” ;when “01” => cso <=“01” ; when “10” => cso <=“01” ; when “11” => cso <=“10” ; when other => null ;

END process;

abc <= a&b; co <= cso(1) ; so <= cso(0);process (abc)begin 相当于“ then”

Page 41: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

LIBRARY IEEE ;USE IEEE.STD_LOGIC_1164.ALL ; ENTITY F_ADDER IS -- 实体 F_ADDER 描述 PORT (AIN, BIN, CIN : IN STD_LOGIC ; CY, SUM : OUT STD_LOGIC) ; END F_ADDER ;ARCHITECTURE ART3 OF F_ADDER ISCOMPONENT H_ADDER -- 元件调用声明 PORT(A, B : IN STD_LOGIC ; CO , SO : OUT_LOGIC) ; END COMPONENT;

F_ADDERAINBIN

SUM

CY

CIN

(3)全加器

--半加器端口

Page 42: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

3 、 VHDL 语言描述COMPONENT OR2 PORT(A, B : IN STD_LOGIC ; C : OUT_LOGIC) ; END COMPONENT ;SIGNAL D , E , F : STD_LOGIC ;BEGIN -- 元件连接申明 U1 : H_ADDER PORT MAP(A=>AIN , B=>BIN , CO=>D , SO=>E) ; U2 : H_ADDER PORT MAP(A=>E , B=>CIN , CO=>F, SO=>SUM) ; U3 : OR2 PORT MAP(A=>D , B=>F , C=>CY) ;END ART3 ;

H_ADDER

AB SO

CO H_ADDER

AB SO

CO

AIN

BIN

CY

SUMCIN

D

EF

AB

CU1U2

U3

Page 43: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

4 、说明( 1 )该设计包括三个实体 OR2——2 输入或非门; H-ADDER—— 半加器; F-ADDER—— 全加器。 其中: F-ADDER 是顶层实体,其它两个实体(子实体)为顶层实体服务。( 2 )每个实体含有:库、程序包和结构体。结构体对实体功能进行定义。

Page 44: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

( 3 )实体定义的元件可以通过 COMPONENT

END COMPONENT 元件调用申明, PORT MAP ()元件例化,信号 SIGNAL 的定义以及引脚连线进行元件装配。( 4 )例化名 u1, u2, u3 的端口映射语句,A=>AIN , B=>BIN , CO=>F, SO=>SUM表示端口名与内部信号相连及系统的外部的端口名相连。

Page 45: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第六章、 VHDL 设计进阶6.1 4位加法计数器的 VHDL 描述例 1 、 4位加法计数器

ENTITY CNT4 ISPORT ( CLK : IN BIT ; Q: BUFFER INTEGER RANG 15 DOWNTO 0 );END ;注意: 1、 Q 的端口模式为 BUFFER ;

2、 Q 的的数据类型为 INTEGER 。

CLKQ

4CNT4

Page 46: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL 中规定,算术操作符“ +” 、“ -” 的数据类型(除特殊说明,如重载函数的利用)只能是整形“ INTEGER” 。1 十进制整形35 十进制整形10E3 十进制整形16#D9# 十六进制整形8#562# 八进制整形2#11001001# 二进制整形整数的表达不加单引号。

第六章、 VHDL 设计进阶

Page 47: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

ARCHITECTURE bhv OF CNT4 ISBEGIN

IF CLK’EVENT AND CLK =‘1’ THENQ <= Q+1 ; --Q 具有 I/O 模式,整数类形END IF ;

END PROCESS ;END bhv ;

PROCESS (CLK)BEGIN

第六章、 VHDL 设计进阶CLK

Q4

CNT4

Page 48: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

例 2 、 4位加法计数器

ENTITY CNT4 ISPORT ( CLK : IN STD_LOGIC ;

Q: OUT STD_LOGIC_VECTOR (3 DOWNTO 0 ) ) ;

END ;

LIBRARY IEEE; -- 库,程序包调用USE IEEE..STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;

第六章、 VHDL 设计进阶

Page 49: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

ARCHITECTURE bhv OF CNT4 IS

BEGIN

IF CLK’EVENT AND CLK =‘1’ THENQ1 <= Q1+1 ;

END IF ;END PROCESS ;

END bhv ;

PROCESS (CLK)BEGIN

SIGNAL Q1 : STD_LOGIC_VECTOR (3 DOWNTO 0)

Q <=Q1 ;

第六章、 VHDL 设计进阶

Page 50: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

注意:在算式 Q1<=Q1+1 中, Q1 是逻辑向量STD_LOGIC_VECTOR ,而所加的 1 是整形数,数据类型是不同的,那为何能运算呢? 原因是在实体中使用了库: USE IEEE.STD_LOGIC_UNSIGNED.ALL该库允许对预定义的操作符“ +” 、“ -” 、“ *” 、“ =” 、“ <=” 、“ >” 、“ <” 、“ /=” 、“ AND” 、“MOD” 等,对相应的数据类型 INTEGER 、 STD_LOGIC和STD_LOGIC_VECTOR 的操作进行了重载,通过重新定义运算符的数据类型进行操作。

第六章、 VHDL 设计进阶

Page 51: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

仿真时序结果如下:

4位加法计数器工作时序图

第六章、 VHDL 设计进阶

Page 52: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

6.2 不同工作方式的时序电路设计例 3 :设计一个异步清零、有进位输入 /输出的四位二进制计数器 library ieee; -- 库,程序包调用use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;ENTITY cntm16 IS -- 计数器实体 PORT

(ci : IN std_logic;nreset : IN std_logic;clk : IN std_logic; co : out std_logic;qcnt : buffer std_logic_vector(3 downto 0));

END cntm16;

Page 53: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

ARCHITECTURE behave OF cntm16 IS -- 结构体BEGIN co<='1' when (qcnt="1111" and ci='1') else '0'; PROCESS (clk,nreset) -- 进程(敏感表)

BEGIN IF(nreset='0') THEN qcnt<="0000"; ELSIF (clk'EVENT AND clk = '1') THEN

if(ci='1') then qcnt<=qcnt+1; end if;

END IF; END PROCESS; END behave;

完成了一个 16 进制计数器的设计。

第六章、 VHDL 设计进阶

Page 54: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

仿真时序结果:

Page 55: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第六章、 VHDL 设计进阶

library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;ENTITY cntm10 IS PORT( clk, rst, en : IN std_logic;

co : out std_logic; qcnt :buffer std_logic_vector(3 downto 0)); END cntm10;

例 4 :设计一个异步清零、有使能输入的十进制计数器 .

Page 56: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

ARCHITECTURE behave OF cntm10 IS BEGINPROCESS (clk,rst)VARIABLE cqi :STD_LOGIC_VECTOR(3 DOWNTO 0);

BEGIN IF(rst=‘1') THEN cqi:=(others=>’0’); ELSIF (clk'EVENT AND clk = '1') THEN

if(en='1') then if cqi <“1001” THEN cqi :=cqi+1; ELSE cqi := (others=>’0’); end if; end if;

END IF;

第六章、 VHDL 设计进阶

Page 57: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

IF cqi =“1001” then co <= ‘1’; else co <=‘0’; end if; qcnt <= cqi; END PROCESS; END behave;

第六章、 VHDL 设计进阶

仿真时序结果:

Page 58: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第六章、 VHDL 设计进阶例 5 :设计一个带有并行输入置数,串行输出的移位寄存器 library ieee;use ieee.std_logic_1164.all;ENTITY shift IS PORT( clk, load : IN std_logic;

din : IN std_logic_vector(7 downto 0); qb : OUT); END shift;

Page 59: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

ARCHITECTURE behave OF shift IS BEGINPROCESS (clk,load)VARIABLE reg8 : std_logic_vector(7 downto 0);

BEGIN IF clk'EVENT AND clk = '1' THEN

if load=‘1’ then reg8 :=din; else reg8(6 downto 0) :=reg8 (7 downto 1); end if; end if; qb <= reg8(0) ; END PROCESS; END behave;

第六章、 VHDL 设计进阶

Page 60: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第六章、 VHDL 设计进阶仿真时序结果:

10011010 9A 11001101 CD 11100110 E6 11110011 F3

11111001 F9 11111100 FC 11111110 FE 11111111 FF

特点:算术移位,符号不变。

Page 61: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第六章、 VHDL 设计进阶6 . 3 数据对象 DATA OBJECTS

在逻辑综合中, VHDL 语言常用的数据对象为:1 、常量2 、变量3 、信号

Page 62: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

1 、常量 (constant) 常量在设计描述中保持某一规定类型的特定值不变 , 具有全局意义。如利用它可设计不同模值的计数器,模值存于一常量中,改变模值仅需改变此常量值。格式: CONSTANT 常量名:数据类型 := 表达式;例: constant buswidth: integer :=8;

constant fbt: std_logic_vector :=“01110010”;

第六章、 VHDL 设计进阶

Page 63: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

在实体、结构体、程序包、块、进程、子程序中可定义“常量”, 常量的可视性 , 取决于定义它的位置。在程序包中定义它,则具有全局可视性;在实体中定义,则对该实体下的所有结构体可见;若定义于结构体、块、进程、子程序中,则只能对用于其相应的结构体、块、进程、子程序中。

第六章、 VHDL 设计进阶

Page 64: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

2 、变量 (variable) 变量只在给定的进程中用于声明局部值或用于子程序中。变量的赋值是立即发生的不存在延迟。 格式:

VARIABLE 变量名:数据类型 := 初始值;例: variable A, B: INTEGER: =2

variable vabc: std_logic_vector(2 downto 0);

第六章、 VHDL 设计进阶

Page 65: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

3 、信号( Signal ) 用于声明内部信号,而非外部信号(外部信号对应为 in, out, inout, buffer ),其在元件之间起互联作用,它没有方向性,可以赋值给外部信号。 信号也可在状态机中表示状态变量。信号赋值符号为“ <=” 。 例: signal halfsum: std_logic_vector(7 downto 0);

第六章、 VHDL 设计进阶

Page 66: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

注意 :信号赋值与变量赋值是不同的。在进程中,信号赋值在进程结束时起作用,而变量赋值是立即起作用的。观察下面两个进程:

第六章、 VHDL 设计进阶

Page 67: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

若使用 synplify对 p1 进程进行逻辑综合可出现如下警告信息:“ Input a is unused”

p1: process(A,B,C) beginD<=A; --D 为外部定义的信号X<=B+D;D<=C;Y<=B+D; end process p1;

p2: process(A,B,C) variable D:std_logic; beginD:=A; --立即赋值X<=B+D;D:=C; --立即赋值Y<=B+D; end process p2; 此执行结果

第六章、 VHDL 设计进阶

在进程结束更新时, D的值为C.因此执行结果 :X<=B+C; Y<=B+C;

X<=B+A; Y<=B+C;

Page 68: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

信号( signal ) 变量( variabl

e )基本用法适用范围行为特性

用于电路中的信号连线在整个结构体中可用在进程的最后才赋值

用于进程中作局部数据存储单元只能在所定义的进程中使用立即赋值

信号与变量赋值语句功能的比较第六章、 VHDL 设计进阶

Page 69: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第六章、 VHDL 设计进阶例 6 信号赋值(信号赋值与变量赋值的比较) library ieee;use ieee.std_logic_1164.all;ENTITY dff3a IS PORT( clk, d1 : IN std_logic;

q1 : OUT std_logic); END dff3a;ARCHITECTURE behave OF dff3a IS

Signal a,b :std_logic;begin

Page 70: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

Process (clk) begin

If clk’event and clk=‘1’ then a <= d1; b <= a; q1 <= b;

end if; end process;

end behave;

第六章、 VHDL 设计进阶

Page 71: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

D

CLK

Q D

CLK

Q D

CLK

Q

clk

D1 Q1

第六章、 VHDL 设计进阶 信号赋值仿真的结果

Page 72: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

例 7 变量赋值(信号赋值与变量赋值的比较)

第六章、 VHDL 设计进阶 library ieee;use ieee.std_logic_1164.all;ENTITY dff3b IS PORT( clk, d1 : IN std_logic;

q1 : OUT std_logic); END dff3b;ARCHITECTURE behave OF dff3b IS

begin

Page 73: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

Process (clk)

begin If clk’event and clk=‘1’ then

a := d1; b := a; q1 <= b;

end if; end process;

end behave;

variable a, b : std _ logic ; 第六章、 VHDL 设计进阶

Page 74: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

D

CLK

QD1

clk

Q1

变量赋值仿真的结果第六章、 VHDL 设计进阶

Page 75: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

6 . 4 双向电路和三态控制电路设计 library ieee;use ieee.std_logic_1164.all;ENTITY tris IS PORT( enable : IN std_logic; datain : IN std_logic_vector(7 downto 0); dataout : OUT std_logic_vector(7 downto 0)); END tris;

例 8 :三态门设计

en

datain[7:0] dataout[7:0]8 8

Page 76: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第六章、 VHDL 设计进阶

else dataout <= “ZZZZZZZZ”;

ARCHITECTURE behave OF tris ISbeginProcess (enable,datain)

beginIf enable =‘1’ then dataout <= datain;

end if;end process;

end behave;en

datain[7:0] dataout[7:0]8 8

Page 77: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

6 . 5 进程语句结构 VHDL 常用语句分并行 (Concurrent) 语句和顺序 (Sequential) 语句,进程中的语句是顺序语句:

第六章、 VHDL 设计进阶

顺序语句( Sequential) : 顺序语句总是处于进程( PROCESS )的内部,并且从仿真的角度来看是顺序执行的。如 if-then-else 语句。

Page 78: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

结构体( Architecture )声明区( Declarations )信号声明;声明用于该结构体的类型,常数,元件,子程序。并发语句信号赋值( Signal Assignments计算结果,并赋值给信号元件例化( Component Instantiations )调用另一个实体所描述的电路。即元件调用

过程调用( Procedure Calls调用一个预先定义好的一个算法。进程( Processes )定义一个新算法实现电路功能。在进程中赋值顺序语句。语句按放置的顺序执行。

第六章、 VHDL 设计进阶

Page 79: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

进程( PROCESS )用于描述顺序( sequential )事件并且包含在结构中。一个结构体可以包含多个进程语句。以下为进程语句的构成:

第六章、 VHDL 设计进阶

Page 80: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

进程( Process )声明区( Declarations )声明内部变量;用于该进程的常数;元件;子程序。顺序语句

信 号 赋 值 ( <=)过程调用变量赋值( := )if 语句case 语句

loop 语句(循环)next 语句(跳过剩余循环)exit 语句(退出循环)wait 语句(等待时钟信号)null 语句(空语句 ,值保持不变 )

第六章、 VHDL 设计进阶

Page 81: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

process <sensitivity list 敏感信号表 >) begin -- 进程开始 end process; -- 进程结束 敏感表 (Sensitivity list) 包括进程的一些信号,当敏感表中的某个信号变化时进程才被激活。如十进制加法计数器器为例:

第六章、 VHDL 设计进阶

Page 82: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

PROCESS (clk,nreset) -- 进程(敏感表) BEGIN IF(nreset='0‘ OR qcnt=1001) THEN -- 顺序语句 qcnt<="0000";

ELSIF (clk'EVENT AND clk = '1') THEN if(ci='1') then

qcnt<=qcnt+1; end if;

END IF; --end if _reset END PROCESS;

-- 异步清零

第六章、 VHDL 设计进阶

Page 83: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

若为同步清零,敏感表中可无 nreset 信号,此时进程如下: PROCESS (clk) -- 进程(敏感表) BEGIN IF (clk'EVENT AND clk = '1') THEN -- 顺序语句

IF(nreset='0‘ OR qcnt=1001) THEN -- 同步清零 qcnt<="0000"; elsif(ci='1') then

qcnt<=qcnt+1; END IF; --end if _reset

END IF; END PROCESS;

第六章、 VHDL 设计进阶

Page 84: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

顺序 (Sequential) 语句:常用的顺序语句有: IF-THEN-ELSE 语句, CASE-WHEN 语句。此外还 有“ for-loop” 语 句 , 以 及 wait until ,wait on 语句等。 顺序语句总是处于进程( PROCESS )的内部,并且从仿真的角度来看是顺序执行的。

第六章、 VHDL 设计进阶

Page 85: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法:顺序语句: IF-THEN-ELSE

ENTITY mux41 IS PORT ( a0,a1,a2,a3 : IN std_logic; S : IN std_logic_vector(1 downto 0) ;

y : OUT std_logic ) ; END mux41 ;

例如: 4-1 多路选择器LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;

Page 86: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

else y<=a3; end if end process;end archmux;

if s="00" then y<=a0; elsif s="01" then y<=a1; elsif s="10" then y<=a2;

ARCHITECTURE archmux OF mux4 ISbegin

PROCESS (s,a0,a1,a2,a3) begin

Page 87: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法:顺序语句: CASE-WHENarchitecture archmux of mux4 is begin process(s,a0,a1,a2,a3) begin case s is when "00" => y<=a0; when "01" => y<=a1; when "10" => y<=a2; when others => y<=a3; end case; end process;end archmux;

相当于“ then”

Page 88: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

4-1 多路选择器仿真结果

Page 89: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法:顺序语句: for-loop例如:奇偶校验电路的 for-loop 描述library ieee ;use ieee.std_logic_1164.all ;entity parity isport (databus : in std_logic_vector(7 downto 0); even_num,odd_num : out std_logic) ;end parity ;architecture behave of parity is begin process(databus)

databus

even_numodd_num

Page 90: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法:顺序语句: for-loopvariable tmp: std_logic ;begin tmp:='0'; for i in 0 to 7 loop --for—loop 语句 tmp:=tmp xor databus(i); end loop; odd_num<=tmp; even_num<=not tmp; end process;end behave;

Page 91: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL基本结构与语法:顺序语句: while-loop

architecture behave of parity is begin process(databus)variable tmp: std_logic ;variable n: integer ;Begin n:=‘0’; tmp:='0';

奇偶校验电路的结构体描述 2 ( while-loop 描述)

Page 92: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

odd_num<=tmp; even_num<=not tmp;

end process;end behave;

while n<=7 loop --while—loop 语句 tmp:=tmp xor databus(n); n:=n+1; end loop

第六章、 VHDL 设计进阶

Page 93: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

例如:七人表决器 VHDL 描述library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;ENTITY vote7 IS

PORT( men : in std_logic_vector(6 downto 0);pass, stop: buffer std_logic);

END vote7;

Page 94: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

例如:七人表决器 VHDL 描述 顺序语句: for-loopARCHITECTURE behave OF vote7 ISBEGIN stop<= not pass;

PROCESS (men) variable temp:std_logic_vector(2 downto

0);BEGIN

temp:="000"; for i in 0 to 6 loop

Page 95: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

if(men(i)='1') then temp:=temp+1; else temp:=temp+0; end if; end loop; pass<=temp(2);

END PROCESS; END behave;

第六章、 VHDL 设计进阶

Page 96: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

常用电路: 1 、三 - 八译码器 A2 A1 A0 Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0

Page 97: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

LIBRARY ieee;USE ieee.std_logic_1164.all;USE ieee.std_logic_unsigned.all;

entity decoder is port (adrin : in std_logic_vector(2 downto 0); deout : out std_logic_vector(7 downto 0) );end decoder;

常用电路: 1 、三 - 八译码器:

A2A1A0

Y7Y6

Y0

3-8 decoder

Page 98: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

architecture behave of decoder is Begin Process(adrin) begin Case adrin is when “000”=>dout<=“00000001”; when “001”=>dout<=“00000010”; when “010”=>dout<=“00000100”; when “011”=>dout<=“00001000”; when “100”=>dout<=“00010000”; when “101”=>dout<=“00100000”; when “110”=>dout<=“01000000”; when “111”=>dout<=“10000000”; when others=>dout<=“xxxxxxxx”; End Case; End process;End behave;

-- 使用 CASE WHEN 查表方法

Page 99: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

常用电路: 2 、三 - 八译码器(带使能)A2 A1 A0 EN Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 0 1 0 0 1 0 1 0 0 0 0 0 1 0 0 0 1 1 1 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0 1 1 0 0 1 0 0 0 0 0 1 1 0 1 0 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0

Page 100: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

LIBRARY ieee;USE ieee.std_logic_1164.all;USE ieee.std_logic_unsigned.all;entity decoder_e is port ( A : in std_logic_vector(2 downto 0); en  : in std_logic; Y : out std_logic_vector(7 downto 0) );end decoder_e;

常用电路: 2 、三 - 八译码器(带使能):

A2A1A0

Y7Y6

Y0

3-8 decoder

en

Page 101: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

Architecture behave of decoder_e is signal sel :std_logic_vector(3 downto 0); Begin sel(0) <= en; sel(1) <= A(0); sel(2) <= A(1); sel(3) <= A(2); WITH sel SELECT Y<=“00000001” when “0001”; “00000010” when “0011”;

常用电路: 2 、三 - 八译码器(带使能)

查表方法

SEL <= A&en;

Page 102: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

“00000100” when “0101”; “00001000” when “0111”; “00010000” when “1001”; “00100000” when “1011”; “01000000” when “1101”; “10000000” when “1111”; “11111111” when others; End behave;

常用电路: 2 、三 - 八译码器(带使能)

Page 103: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

三 - 八译码器(带使能)的仿真波形

常用电路: 2 、三 - 八译码器(带使能)

Page 104: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

常用电路: 3 、优先编码器 (Priority encoder)

a b c d e f g h Priority encoder x x x x x x x 1 1 1 1 x x x x x x 1 0 1 1 0 x x x x x 1 0 0 1 0 1 x x x x 1 0 0 0 1 0 0 x x x 1 0 0 0 0 0 1 1 x x 1 0 0 0 0 0 0 1 0 x 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 x x x x x x x x Z Z Z

Page 105: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

LIBRARY ieee;USE ieee.std_logic_1164.all;entity encoder is port (a,b,c,d,e,f,g,h : in std_logic; codeout : out std_logic_vector(2 downto 0) );end encoder;

常用电路: 3 、优先编码器 (Priority encoder)

Page 106: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

architecture behave of encoder isbegin codeout<="111" when h='1' else "110" when g='1' else "101" when f='1' else "100" when e='1' else "011" when d='1' else "010" when c='1' else "001" when b='1' else "000" when a='1' else “ZZZ" ;end behave;

常用电路: 3 、优先编码器 (Priority encoder)

-- 并行语句

Page 107: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

优先编码器的仿真波形

常用电路: 3 、优先编码器 (Priority encoder)

Page 108: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

LIBRARY ieee;USE ieee.std_logic_1164.all;ENTITY comp IS PORT ( a,b :in std_logic_vector(7 downto 0);

aqualb,agrdb,alessb :out std_logic ); END comp;ARCHITECTURE behave OF comp ISBEGIN -- 并行语句 aqualb<='1' when a=b else '0'; agrdb<='1' when a>b else '0'; alessb<='1' when a<b else '0';END behave;

常用电路: 4 、八位比较器

Page 109: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

常用电路: 5 、带置数的双向移位寄存器library ieee ;use ieee.std_logic_1164.all ;ENTITY shifter IS

PORT(data : in std_logic_vector(7 downto 0) ;

sl_in, sr_in, reset, clk : IN std_logic ; mode : in std_logic_vector(1 downto 0) ;

qout : buffer std_logic_vector(7 downto 0)); END shifter ;ARCHITECTURE behave OF shifter IS signal q1,q0 : std_logic ;BEGIN PROCESS (clk)

BEGIN

Page 110: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

IF (clk'EVENT AND clk = '1') THEN if(reset='1') then qout<=(others=>'0') ; -- 同步清零 else case mode is when "01"=> qout<=sr_in & qout(7 downto 1) ; --右移 when "10"=> qout<=qout(6 downto 0) & sl_in ; --左移 when "11"=> qout<=data; ; -- 置数 when others=>null ; --“NULL” 表示无操作 end case ; end if ; END IF ; END PROCESS ; END behave ;

Page 111: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

LIBRARY ieee;USE ieee.std_logic_1164.all;USE ieee.std_logic_unsigned.all;ENTITY bcdadder IS PORT (op1, op2: in std_logic_vector(3 downto 0); result: out std_logic_vector(4 downto 0));END bcdadder;

常用电路: 6 、一位 BCD 码加法器

Page 112: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

ARCHITECTURE behavior OF bcdadder IS signal binadd :std_logic_vector(4 downto 0);

BEGIN binadd<=(‘0’ & op1) +(‘0’ & op2); --保存二进制之和 process(binadd) -- 进行加 6校正 begin if binadd>9 then result <= binadd+6; else result <= binadd; end if; end process; END behavior;

Page 113: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

library ieee;

use ieee.std_logic_1164.all;

USE ieee.std_logic_unsigned.all;

entity BIN is

port(B:in std_logic_vector(3 downto 0);

en:in std_logic;

G:out std_logic_vector(3 downto 0));

end BIN;

常用电路: 7 、二进制码—格雷码转换

Page 114: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

architecture GRAY of BIN is

begin

PROCESS(B,EN)

BEGIN

IF(EN='1') THEN

CASE B IS

WHEN "0000"=>G<="0000";--0 --0

WHEN "0001"=>G<="0001";--1 --1

WHEN "0011"=>G<="0010";--2 --3

常用电路: 7 、二进制码—格雷码转换

Page 115: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

WHEN "0010"=>G<="0011";--3 --2

WHEN "0110"=>G<="0100";--4 --6

WHEN "0111"=>G<="0101";--5 --7

WHEN "0101"=>G<="0110";--6 --5

WHEN "0100"=>G<="0111";--7 --4

WHEN "1100"=>G<="1000";--8 --12

WHEN "1101"=>G<="1001";--9 --13

WHEN "1111"=>G<="1010";--10 –15

WHEN "1110"=>G<="1011";--11 --14

常用电路: 7 、二进制码—格雷码转换

Page 116: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

WHEN "1010"=>G<="1100";--12 --10

WHEN "1011"=>G<="1101";--13 --11

WHEN "1001"=>G<="1110";--14 --9

WHEN "1000"=>G<="1111";--15 --8

WHEN OTHERS=>G<="XXXX";

END CASE;

ELSE

G<="0000";

END IF;

END PROCESS;

end GRAY;

Page 117: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第七章、 有限状态机设计7 . 1 、一般有限状态机的设计1 、状态机分类:按输出方式分 Mealy 型

Moore 型 输出仅为当前状态的函数

输出是当前状态和输入的函数

按结构分 单 进 程多 进 程

主进程和辅进程合一分时序、组合主进程,辅助进程

Page 118: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第七章、 有限状态机设计

按状态表达分符号状态机确定编码状态机

S0,S1,S2,…

000,001,010, …

按编码方式分顺序编码一位热码

二进制顺序编码等00001,00010,00100, …

其它编码 格雷码,循环码,等

Page 119: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第七章、 有限状态机设计2 、用户自定义数据类型

类型定义TYPE

Enumeration Types

Integer Types

Array Types

TYPE 数据类型名 IS 数据类型定义 OF 基本数据类型;TYPE st1 IS ARRAY ( 0 TO 15 ) OF STD_LOGIC ;例如:

Page 120: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第七章、 有限状态机设计TYPE 数据类型名 IS 数据类型定义;例如:TYPE m_state IS (st0,st1,st2,st3,st4,st5) ;SIGNAL present_state,next_state : m_state ;

SUBTYPE 子类型名 IS 数据类型定义 RANGE 范围;例如:SUBTYPE digits IS INTEGER RANGE 0 TO 9 ;

子类型 SUBTYPE 是 TYPE 定义的原数据类型的一个子集;

Page 121: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第七章、 有限状态机设计2 、一般状态机的设计a 、说明部分例如:

SIGNAL current_state,next_state : m_state ; …

ARCHITECTURE beh IS

TYPE m_state IS (st0,st1,st2,st3,st4,st5) ;

Page 122: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第七章、 有限状态机设计b 、主控时序进程 该部分是在 CLK 控制下,进行状态转换 : current_state <= next_state ;

c 、主控组合进程 该部分是确定 current_state, next_state 的取值,为时序进程提供参数;进行状态转换条件的判定;进行输出判定。d 、辅助进程 该部分是配合主控时序进程或主控组合进程,完成某种算法、输出数据锁存等。

Page 123: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第七章、 有限状态机设计例 7-1 双进程 Moore状态机

RST S0/5 S2/12 S3/14S1/8

00 00

else else else

11

11

else

State_inputs

State/output

clkreset

State inputs

2 4

Comb_outputs状态机

7 . 2、Moore 型有限状态机的设计

Page 124: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第七章、 有限状态机设计Library ieee; Use ieee.std_logic_1164.all;Entity s_machine is Port ( clk, reset : in std_logic; State_inputs : in std_logic_vector(0 to 1); comb_outputs : out integer range 0 to 15);

End s_machine; architecture behv of s_machine is

type fsm_st is (s0,s1,s2,s3); Signal current_state, next_state : fsm_st;

clkreset

State inputs

2 4

Comb_outputs状态机

Page 125: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

Beginreg: process (reset, clk)

beginif reset =‘1’ then current_state <= s0;elsif clk =‘1’ and clk’event then current_state <= next_state;

end if;end process;

com: process(current_state, state_inputs);begincase current_state is when s0 => comb_outputs <= 5;

if state_inputs =“00” then next_state <= s0;else next_state <= s1;

end if;

--主控时序进程

--主控组合进程

RST S0/5 S2/12 S3/14S1/8

00 00

else else else

11

11

else

Page 126: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

when s1 => comb_outputs <= 8; if state_inputs =“00” then next_state <= s1;else next_state <= s2;

end if;when s2 => comb_outputs <= 12; if state_inputs =“11” then next_state <= s0;else next_state <= s3;

end if;when s3 => comb_outputs <= 14; if state_inputs =“11” then next_state <= s3;else next_state <= s0;

end if;end case;

end process;end behv;

RST S0/5 S2/12 S3/14S1/8

00 00

else else else

11

11

else

Page 127: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第七章、 有限状态机设计仿真结果

RST S0/5 S2/12 S3/14S1/8

00 00

else else else

11

11

else

Page 128: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第七章、 有限状态机设计例 7-2 三进程有限Moore状态机——设计一个 A/D 采样控制器,控制 AD574 。AD574 基本参数: 12位分辨率,误差<1/2LSB, 25μs/ 次的采样速度,片内时钟和基准,可 12位或 8位转换,转换结果二进制码输出。AD574转换时序:

RC

STATUS25 μs

D[11:0] Z

CS

Page 129: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第七章、 有限状态机设计AD574 逻辑控制真值表:CE CS RC K12/8 A0 工作状态0 x x x x 禁止x 1 x x x 禁止1 0 0 x 0 启动 12 位转换1 0 0 x 1 启动 8 位转换1 0 1 1 x 12 位并行输出有效1 0 1 0 0 高 8 位并行输出有效1 0 1 0 1 低 4 位加尾随 4个 0 有效

Page 130: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第七章、 有限状态机设计

St0 St1 St2

St3St4

AD574控制流程设计AD574初始化

启动AD574

正在转换

等待

读结果锁存数据时序进程 组合进程 1

组合进程 2

辅助进程clk12

AD574

QCS

A0RC

k12/8

statusn_sc_s D[11:0]

locklock0

Page 131: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

Library ieee; Use ieee.std_logic_1164.all;Entity AD574 is Port ( D : in std_logic_vector(11 down to 0); clk, Status : in std_logic;

End AD574;

lock0 : out std_logic; CS, A0, RC, K12x8 : out std_logic;

Q : out std_logic_vector(11 down to 0));

AD574控制VHDL 设计

时序进程 组合进程 1

组合进程 2

辅助进程clk12

AD574

QCS

A0RC

k12/8

statusn_sc_s D[11:0]

locklock0

Page 132: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

architecture behave of AD574 is type states is (st0,st1,st2,st3,st4); Signal current_state, next_state : states :=st0; Signal regl : std_logic_vector(11 downto 0); Signal lock : std_logic; begin K12x8 <= ‘1’; lock0 <= lock ;

Com1: process (current_state, status)begin

Case current_state isWhen st0 => next_state <= st1;When st1 => next_state <= st2;

St0 St1 St2

St3St4

AD574初始化 启动AD574

正在转换

等待

读结果

锁存数据

Page 133: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第七章、 有限状态机设计When st2 => if (status=‘1’) then next_state <= st2;

else next_state <= st3;

When st3 => next_state <= st4;End if;

When st4 => next_state <= st0;When others => next_state <= st0;End case;End process com1;

Com2: process(current_state)beginCase current_state is

When st0=> CS <=‘1’; A0<=‘1’; RC<=‘1’; lock<=‘0’;

When st1=> CS <=‘0’; A0<=‘0’; RC<=‘0’; lock<=‘0’;

St0 St1 St2

St3St4

AD574初始化启动AD574

正在转换

等待

读结果锁存数据

Page 134: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

When st2=> CS <=‘0’; A0<=‘0’; RC<=‘0’; lock<=‘0’;When st3=> CS <=‘0’; A0<=‘0’; RC<=‘1’; lock<=‘0’;When st4=> CS <=‘0’; A0<=‘0’; RC<=‘1’; lock<=‘1’;When others=> CS <=‘1’; A0<=‘1’; RC<=‘1’; lock<=‘0’;End case;End process com2;Reg: process (clk)beginIf (clk’event and clk=‘1’) then current_state<=next_state;End if;

第七章、 有限状态机设计

End process reg;

St0 St1 St2

St3St4

Page 135: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

Q <= regl;End behave;

latch1 : process (lock)beginIf lock=‘1’; and lock’event then regl<=d;End if;

End process latch1;

第七章、 有限状态机设计

时序进程 组合进程 1

组合进程 2

辅助进程clk12

AD574

QCS

A0RC

k12/8

statusn_sc_s D[11:0]

locklock0

Page 136: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

AD574控制时序图

St0 St1 St2

St3St4

AD574初始化

启动AD574

正在转换

等待

读结果锁存数据

Page 137: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

CE CS RC K12/8 A0 工作状态0 x x x x 禁止x 1 x x x 禁止1 0 0 x 0 启动 12 位转换1 0 0 x 1 启动 8 位转换1 0 1 1 x 12 位并行输出有效1 0 1 0 0 高 8 位并行输出有效1 0 1 0 1 低 4 位加尾随 4个 0 有效

Page 138: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

7 . 3、Mealy 型有限状态机的设计第七章、 有限状态机设计

Mealy 型 输出是当前状态和输入的函数例 7-3 、 “ 1110” 序列检测器设计

0/11/0

1/0

sa1/0

0/0

0/0

0/0

sb

scsd

x/z

1/0

Page 139: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

library ieee;use ieee.std_logic_1164.all;ENTITY statem IS PORT ( clk : IN std_logic; x,reset : IN std_logic; z : out std_logic );END statem;

0/11/0

1/0

sa1/0

0/0

0/0

0/0

sb

scsd

x/z

1/0

第七章、 有限状态机设计

Page 140: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

ARCHITECTURE behave OF statem ISTYPE STATE_TYPE IS (sa, sb, sc,sd);SIGNAL state: STATE_TYPE;BEGIN PROCESS (clk,reset) BEGIN IF reset = '1' THEN state <= sa;ELSIF clk'EVENT AND clk = '1' THEN

第七章、 有限状态机设计

Page 141: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

CASE state ISWHEN sa => IF x='1' THEN state <= sb; else state<=sa; end if; WHEN sb => IF x='1' THENstate <= sc; else state<=sa; end if; WHEN sc => IF x='1' THEN state<=sd; else state<=sa; end if;

0/11/0

1/0

sa1/0

0/0

0/0

0/0

sb

scsd

x/z

1/0

Page 142: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

WHEN sd => IF x='1' THEN state <= sd; else state<=sa; END IF;END CASE;END IF; --resetEND PROCESS ; z<='1' when state=sd and x=‘0' else '0'; END behave;

0/11/0

1/0

sa1/0

0/0

0/0

0/0

sb

scsd

x/z

1/0

第七章、 有限状态机设计

Page 143: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

“1110” 序列检测器时序图

0/11/0

1/0

sa1/0

0/0

0/0

0/0

sb

scsd

x/z

1/0

Page 144: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第七章、 有限状态机设计7 . 4 、状态编码library ieee;use ieee.std_logic_1164.all;ENTITY four_oneb IS PORT ( clk : IN std_logic; x,reset : IN std_logic; z : out std_logic );END four_oneb;

例 7-4 、“ 1111” 序列检测器设计

0/01/0

1/0

sa1/0

0/0

0/0

0/0

sb

scsd

x/z

1/1

RESET

Page 145: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

ARCHITECTURE behave OF four_oneb IS signal state : std_logic_vector(1 downto 0); constant sa :std_logic_vector(1 downto 0) := "00"; constant sb :std_logic_vector(1 downto 0) := "01"; constant sc :std_logic_vector(1 downto 0) := "10"; constant sd :std_logic_vector(1 downto 0) := "11";BEGIN PROCESS (clk,reset) BEGINIF reset = '1' THEN state <= sa;ELSIF clk'EVENT AND clk = '1' THEN

sa 00 sb 01

sc 10 sd 11

第七章、 有限状态机设计

Page 146: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

CASE state IS WHEN sa => IF x='1' THEN state <= sb; else state<=sa; END IF; WHEN sb => IF x='1' THEN state <= sc; else state<=sa; END IF; WHEN sc => IF x='1' THEN state <=sd; else state<=sa; END IF; WHEN sd => IF x='1' THEN state <= sd; else state<=sa; END IF; when others => state <=sa;END CASE;END IF; --reset

0/01/0

1/0

sa1/0

0/0

0/0

0/0

sb

scsd

x/z

1/1

RESET

Page 147: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

IF clk'EVENT AND clk = '1' THEN case state is when sd => if x='1' then z<='1';else z<='0'; end if; when others => z<='0';end case;end if; end process;END behave;

第七章、 有限状态机设计

0/01/0

1/0

sa1/0

0/0

0/0

0/0

sb

scsd

x/z

1/1

RESET

Page 148: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

“1111” 序列检测器仿真时序图第七章、 有限状态机设计

Page 149: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_signed.all;ENTITY stack IS --16*8 PORT ( datain : IN std_logic_vector(7 downto 0); push,pop,reset,clk : IN std_logic; stackfull : out std_logic; dataout :buffer std_logic_vector(7 downto 0));END stack;

例 、堆栈( stack )设计第七章、 有限状态机设计

Page 150: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

ARCHITECTURE a OF stack IStype arraylogic is array (15 downto 0) of std_logic_vector(7 downto 0);signal data : arraylogic; -- 此处定 义 了 data 为 一 个 数 组168signal stackflag:std_logic_vector(15 downto 0);BEGIN

stackfull<=stackflag(0); process(clk,reset,pop,push) variable selfunction :std_logic_vector(1 downto 0); begin selfunction:=push&pop; if reset=‘1’ then

第七章、 有限状态机设计

Page 151: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

stackflag<=(other=>’0’); dataout<=(other=>’0’); for i in 0 to 15 loop data(i)<=“00000000”; end loop;

elsif clk’event and clk=‘1’ then case selfunction is when "10"=> --push data(15)<=datain; --每个 8位 stackflag<='1' & stackflag(15 downto 1); FOR i IN 0 to 14 LOOP

data(i)<=data(i+1); END LOOP;

第七章、 有限状态机设计

Page 152: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

when “01”=> --pop dataout<=data(15); stackflag<= stackflag(14 downto 0)&’0’; for i in 15 downto 1 loop data(i)<=data(i-1); end loop; when others =>null; end case; end if; end process;End a;

第七章、 有限状态机设计

Page 153: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

Page 154: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第八章、 VHDL 结构与要素1、 GENERIC 类属说明语句格式: generic (常数名:数据类型 [ :设定值 ] )

例 3 :设计一个可调参数(计数长度可调)的异步清零、有进位输入 / 输出的四位二进制计数器。

参数化元件可增加元件例化的灵活性。所谓参数化元件是指元件的规模(或特性)可以通过引用参数的形式指定的一类元件。例如,下面定义了一个位数可调的计数器:

Page 155: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;ENTITY cntmn IS generic( cntwidth: integer: =4 ) PORT

(ci : IN std_logic;nreset : IN std_logic;clk : IN std_logic

co : out std_logic; qcnt: buffer std_logic_vector(cntwidth-1 downto 0));END cntmn;

第八章、 VHDL 结构与要素

Page 156: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

ARCHITECTURE behave OF cntmn ISConstant allis1 : std_logic_vector(cntwidth-1 downto 0) : =(others=>’1’);BEGIN co<='1' when (qcnt=allis1 and ci='1') else '0'; PROCESS (clk,nreset)

BEGIN IF(nreset='0') THEN qcnt<=(others =>’0’); ELSIF (clk'EVENT AND clk = '1') THEN

if(ci='1') then qcnt<=qcnt+1; end if;

END IF; END PROCESS; END behave;

第八章、 VHDL 结构与要素

Page 157: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

可见,该计数器非参数的四位计数器相比,改动不大,其中在实体处增加了一行: generic(cntwidth: integer:=4);该 行 定 义 了 一 个 整 数 cntwidth 并 赋 初值‘ 4’ ,用它代替原来的固定的计数器长度,若想设计的计数器位数位 8位,仅需将 cntwidth 的初值赋为 8 : generic(cntwidth:integer:=8);若以此计数器为元件,则元件声明为:

第八章、 VHDL 结构与要素

Page 158: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

Component cntnbits IS generic(cntwidth:integer:=8); PORT( ci : IN std_logic;nreset : IN std_logic;clk : IN std_logic;

co : out std_logic;qcnt : buffer std_logic_vector(cntwidth-1

downto 0));END Component ;

第八章、 VHDL 结构与要素

Page 159: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

集合中各元素用’,’格开;others 必须出现在集合的最后。例: signal sa:std_logic_vector(7 downto 0); sa<=(‘1’,’0’,others=>’1’)其结果是给信号 sa 赋值“ 1011 1111 ” 。

VHDL 基本语法:others=>

第八章、 VHDL 结构与要素

Page 160: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

2 、子程序( SUBPROGRAM )子程序 函数( FUNCTION )

过程( PROCEDURE )子程序定 义

程序包( PACKAGE )结构体( architecture )进程( Process )

全局可见

子程序重 载 可有同名子程序 参数类型及返回数值不能同。

第八章、 VHDL 结构与要素

Page 161: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第八章、 VHDL 结构与要素• 函数( FUNCTION )定义格式:FUNCTION 函数名(参数表) RETURN 数据类型FUNCTION 函数名(参数表) RETURN 数据类型 IS

[ 说明部分 ]

BEGIN

顺序语句; END FUNCTION 函数名;

函数首

函数体

Page 162: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第八章、 VHDL 结构与要素例 8-2LIBRARY IEEE ;USE IEEE.STD_LOGIC_1164.ALL;PACKAGE packexp IS -- 定义程序包 FUNCTION max (a,b : IN STD_LOGIC_VECTOR) RETURN STD_LOGIC_VECTOR; FUNCTION funcl(a,b,c :REAL) -- 定义函数首 RETURN REAL; FUNCTION “*”(a,b :INTEGER) -- 定义函数首 RETURN INTEGER; FUNCTION as2(signal in1,in2 :REAL) -- 定义函数首 RETURN REAL;END;

函数首

Page 163: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第八章、 VHDL 结构与要素PACKAGE BODY packexp IS -- 定义函数体 FUNCTION max ( a, b : IN STD_LOGIC_VECTOR ) RETURN STD_LOGIC_VECTOR IS BEGIN IF a > b THEN RETURN a; ELSE TETURN b; END IF; END FUNCTION max;END;LIBRARY IEEE; -- 函数应用USE IEEE.STD_LOGIC_1164.ALL;USE WORK.packexp.ALL;

Page 164: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第八章、 VHDL 结构与要素ENTITY axamp IS PORT(dat1,dat2 : in std_logic_vector(3 downto 0); dat3,dat4 : in std_logic_vector(3 downto 0); out1,out2 : out std_logic_vector(3 downto 0)); end;ARCHITECTURE bhv OF axamp IS BEGIN out1 <= max(dat1,dat2);-- 函数并行调用 process(dat3,dat4) begin out2 <= max(dat3,dat4); -- 函数顺序调用 end process;End;

Page 165: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第八章、 VHDL 结构与要素• 重载函数( OVERLOADED FUNCTION )例 8-3LIBRARY IEEE ;USE IEEE.STD_LOGIC_1164.ALL;PACKAGE packexp IS -- 定义程序包 FUNCTION max (a,b : IN STD_LOGIC_VECTOR) RETURN STD_LOGIC_VECTOR; FUNCTION max(a,b : IN BIT _VECTOR) -- 定义函数首 RETURN BIT_VECTIR; FUNCTION max(a,b : INTEGER) -- 定义函数首 RETURN INTEGER;END;

Page 166: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第八章、 VHDL 结构与要素PACKAGE BODY packexp IS -- 定义函数体 FUNCTION max (a, b : IN STD_LOGIC_VECTOR ) RETURN STD_LOGIC_VECTOR IS BEGIN IF a > b THEN RETURN a; ELSE TETURN b; END IF; END FUNCTION max;FUNCTION max (a, b : IN INTEGER ) RETURN INTEGER IS BEGIN IF a > b THEN RETURN a; ELSE TETURN b; END IF; END FUNCTION max;

Page 167: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第八章、 VHDL 结构与要素FUNCTION max (a, b : IN BIT_VECTOR ) RETURN BIT_VECTOR IS BEGIN IF a > b THEN RETURN a; ELSE TETURN b; END IF; END FUNCTION max; END; -- 结束 PACKAGE BODYLIBRARY IEEE; --重载函数 max 调用USE IEEE.STD_LOGIC_1164.ALL;USE WORK.packexp.ALL;ENTITY axamp IS

Page 168: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第八章、 VHDL 结构与要素 PORT(a1,b1 : in std_logic_vector(3 downto 0); a2,b2 : in bit_vector(4 downto 0); a3,b3 : in integer ranger 0 to 15; c1 : out srd_logic_vector(3 downto 0); c2 : out bit_vector(4 downto 0); c3 : out integer range 0 to 15); end;ARCHITECTURE bhv OF axamp IS BEGIN c1 <= max(a1,b1);-- 函数并行调用 c2 <= max(a2,b2); c3 <= max(a3,b3); End;

Page 169: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第八章、 VHDL 结构与要素• 过程( PROCEDURE )定义格式:

PROCEDURE 过程名(参数表) --过程首PROCEDURE 过程名(参数表) IS --过程体 [ 说明部分 ] BEGIN 顺序语句; END PROCEDURE 过程名;PROCEDURE PRO1( VARIABLE a, b : INPUT REAL);PROCEDURE PRO2( CONSTANT a1 : IN INTEGER; VARIABLE b1 : OUT INTEGER);PROCEDURE PRO3( SINGAL sig : INPUT BIT);

过程首

Page 170: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第八章、 VHDL 结构与要素例 8-4Procedure prg1 (variable value : inout bit_vector(0 to 3)) IS

BEGIN

CASE value is

when “0000” => value :=”0101”;

when “0101” => value :=”0000”;

when others => value :=”1111”;

end case;

End procedure prg1;

Page 171: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第八章、 VHDL 结构与要素• 重载过程( OVERLOADED PROCEDURE )例 8-5Procedure calcu (v1,v2 : in real; signal out1 : inout integer);

Procedure calcu (v1,v2 : in integer; signal out1 : inout real); … calcu (20.15,1.42,sign1);-- 调用第一个重载 calcu calcu (23,320,sign2); ;-- 调用第二个重载 calcu …

Page 172: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

第八章、 VHDL 结构与要素

一个实体可用多个结构体描述,在具体综合时选择哪一个结构体来综合,则由配置来确定。即配置语句来安装连接具体设计(元件)到一个实体 -- 结构体对。配置被看作是设计的零件清单, 它描述对每个实体用哪一种行为,所以它非常象一个描述设计每部分用哪一种零件的清单。 配置语句举例:

3 配置( CONFIGURATION )

Page 173: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

-- 这是一个两位相等比较器的例子,它用四种不同描述来实现,即有四个结构体。 ENTITY equ2 IS PORT (a,b : IN std_logic_vector(1 downto 0); equ : OUTstd_logic ); END equ2;

-- 结构体一:用元件例化来实现:-- 结构体二:用布尔方程来实现:-- 结构体三:用行为描述来实现,用并行语句:-- 结构体四:用行为描述来实现,用顺序语句:

第八章、 VHDL 结构与要素

Page 174: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

-- 结构体一:用元件例化来实现:ARCHITECTURE netlist OF equ2 IS COMPONENT nor2 PORT (a, b : IN std_logic; C : OUT std_logic); END COMPONENT; COMPONENT xor2 PORT (a, b : IN std_logic; C : OUT std_logic); END COMPONENT;

第八章、 VHDL 结构与要素

Page 175: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

signal x : std_logic_vector(1 downto 0); BEGIN U1: xor2 PORT MAP(a(0), b(0), x(0)); U2: xor2 PORT MAP(a(1), b(1), x(1)); U3: nor2 PORT MAP(x(0), x(1), equ); END netlist;

第八章、 VHDL 结构与要素

Page 176: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

-- 结构体二:用布尔方程来实现:ARCHITECTURE equation of equ2 ISBEGIN equ<=(a(0) Xor b(0)) Nor (a(1) Xor b(1));END equation;

第八章、 VHDL 结构与要素

Page 177: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

-- 结构体三:用行为描述来实现,采用并行语句:ARCHITECTURE con_behave of equ2 ISBEGIN equ<=’1’ when a=b else ‘0’;END con_behave;

第八章、 VHDL 结构与要素

Page 178: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

-- 结构体四:用行为描述来实现,采用顺序语句:ARCHITECTURE seq_behave of equ2 ISBEGINprocess(a,b) begin if a=b then equ<=’1’; else equ<=0; end if; end process;END seq_behave;

第八章、 VHDL 结构与要素

Page 179: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

在上述的实例中,实体 equ2拥有四个结构体:netlist、 equation、 con_behave、 seq_behave ,若用其例化一个相等比较器 aequb ,那么实体究竟对应于哪个结构体呢?配置语句很灵活地解决了这个问题:如选用结构体 netlist, 则用CONFIGURATION aequb OF equ2 IS FOR netlist END FOR;END CONFIGURATION;

第八章、 VHDL 结构与要素

Page 180: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

如选用结构体 con_behave, 则用CONFIGURATION aequb OF equ2 IS FOR con_behave END FOR;END CONFIGURATION;

第八章、 VHDL 结构与要素

Page 181: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

以上四种结构体代表了三种描述方法: Netlist (网表)、 Equation (方程)、 Behavior (行为描述)。有时将它们称之为:behavioral (行为描述)反映一个设计的功能或算法,一般使用进程 process ,用顺序语句表达。dataflow (数据流描述)反映一个设计中数据从输入到输出的流向,使用并发语句描述。structural (结构描述)它最反映一个设计硬件方面特征,表达了内部元件间连接关系。使用元件例化来描述。

第八章、 VHDL 结构与要素

Page 182: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

顺序语句总是处于( PROCESS )进程、( FUNCTION )函数和过程( PROCEDURE )的内部 . 顺序语句总是处于进程( PROCESS)的内部,并且从仿真的角度来看是顺序执行的。

第九章、 VHDL 基本语句9.1 、顺序语句( Sequential )

Page 183: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

顺序 (Sequential) 语句:常用的顺序语句有: IF-THEN-ELSE 语句, CASE-WHEN 语句。此外还 有“ for-loop” 语 句 , 以 及 wait until ,wait on 语句等。

第九章、 VHDL 基本语句

Page 184: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

以 4-1的 MUX 为例:四选一的数据选择器的库声明、程序包声明及实体定义如下:library ieee;use ieee.std_logic_1164.all;entity mux4 is port ( s :in std_logic_vector(1 downto 0); a0,a1,a2,a3 :in std_logic;

y :out std_logic);end mux4;

以下以四选一的数据选择器为例说明各常用语句。

第九章、 VHDL 基本语句

Page 185: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

architecture archmux of mux4 is begin process(s,a0,a1,a2,a3) begin if s="00" then y<=a0; elsif s="01" then y<=a1; elsif s="10" then y<=a2;

else y<=a3; end if end process;end archmux;

第九章、 VHDL 基本语句用: IF-THEN-ELSE

Page 186: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

architecture archmux of mux4 is begin process(s,a0,a1,a2,a3) begin case s is when "00" => y<=a0; when "01" => y<=a1; when "10" => y<=a2; when others => y<=a3; end case; end process;end archmux;

第九章、 VHDL 基本语句

用: CASE-WHEN

Page 187: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

奇偶校验电路library ieee ;use ieee.std_logic_1164.all ;entity parity isport (databus : in std_logic_vector(7 downto 0); even_num,odd_num : out std_logic) ;end parity ;architecture behave of parity is begin process(databus)

第九章、 VHDL 基本语句

Page 188: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

variable tmp: std_logic ;begin tmp:='0'; for i in 0 to 7 loop tmp:=tmp xor databus(i); end loop; odd_num<=tmp; even_num<=not tmp; end process;end behave;

第九章、 VHDL 基本语句

用: for—loop

Page 189: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

architecture behave of parity is begin process(databus)variable tmp: std_logic ;variable n: integer ;Begin n:=‘0’; tmp:='0'; while n<=7 loop tmp:=tmp xor databus(n); n:=n+1; end loop;

odd_num<=tmp; even_num<=not tmp;

end process;end behave;

第九章、 VHDL 基本语句

用: WHILE—loop

Page 190: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

…L_X: FOR N IN 1 TO 8 LOOP S1: A(N):=‘0’; K:=0; L_Y: LOOP S2: B(K):=‘0’; NEXT L_X WHEN (E>F); S3: B(K+8):=‘0’; K:=K+1; NEXT LOOP L_Y;NEXT LOOP L_X;

用: next-loop

第九章、 VHDL 基本语句

Page 191: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

…PROCESS BEGIN WAIT UNTIL CLK=‘1’; AVE<=A; WAIT UNTIL CLK=‘1’; AVE<= AVE+A; WAIT UNTIL CLK=‘1’; AVE<= AVE+A; WAIT UNTIL CLK=‘1’; AVE<= (AVE+A)/4;END PROCESS;…

例、时钟控制 A 的四个数求平均的“进程”。用: wait-until

第九章、 VHDL 基本语句

Page 192: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

并行语句总是处于进程( PROCESS )的外部。所有并行语句都是并行执行的,即与它们出现的先后次序无关。 如when..else 语句并行语句包括:

·布尔方程·条件赋值(如 when—else— 语句)·例化语句

第九章、 VHDL 基本语句9.2 、并行语句( Concurrent )

Page 193: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

仍以 4-1的MUX 为例,四选一的数据选择器的库声明、程序包声明及实体定义如下:library ieee;use ieee.std_logic_1164.all;entity mux4 is port ( s :in std_logic_vector(1 downto 0); a0,a1,a2,a3 :in std_logic;

y :out std_logic);end mux4;

第九章、 VHDL 基本语句

Page 194: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

以布尔方程实现的结构体如下:architecture archmux of mux4 is begin y<=((((a0 and not(s(0))) or (a1 and s(0))))and not(s(1))) or (((a2 and not(s(0))) or (a3 and s(0))) and s(1));end archmux;

1030210100 )()( ssasassasay

第九章、 VHDL 基本语句

Page 195: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

条件赋值并行语句中条件赋值语句为:WITH-SELECT-WHEN 语句;WHEN-ELSE 语句。

第九章、 VHDL 基本语句

Page 196: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

--采用 WITH-SELECT-WHEN 实现的结构体architecture archmux of mux4 is begin with s select y<= a0 when "00",

a1 when "01",a2 when "10",a3 when others;

end archmux;

第九章、 VHDL 基本语句

Page 197: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

--采用 WITH-SELECT-WHEN 实现的结构体。 注意: WITH-SELECT-WHEN 语句必须指明所有互斥条件,在这里即“ s” 的所有 取 值 组 合 , 因 为 “ s” 的 类 型为“ std_logic_vector” , 取 值 组 合除了00,01,10,11 外还有 0x,0z,x1… 等。虽然这些取值组合在实际电路中不出现,但也应列出。为避免此麻烦可以用“ others” 代替其他各种组合。

第九章、 VHDL 基本语句

Page 198: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

--采用 WHEN-ELSE 实现的结构体 architecture archmux of mux4 is begin y<= a0 when s="00" else

a1 when s="01" elsea2 when s="10" elsea3 ;

end archmux;等等实现的方式。

第九章、 VHDL 基本语句

Page 199: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

块语句( Block )块语句主要是将一个复杂的设计划分成模块结构,然后可分别设计和调试并最后组装。Block 的语法格式:块名称: Block [ 数据对象定义区 ] 接口说明 类属说明Begin 并行语句End Block 块名称;

第九章、 VHDL 基本语句

Page 200: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

LIBRARY IEEE ;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOG_ARITH.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY exp IS PORT( A,B : IN Std_Logic; Carry,Sum,Borrow,Difference: OUT Std_Logic; END exp ARCHITECTURE Behave OF exp IS Begin_

第九章、 VHDL 基本语句

Page 201: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

Half_Adder: Block Begin Sum<=A Xor B; Carry<=A and B; END Block Half_Adder; Half_Subtractor: Block Begin Difference<=A Xor B; Borrow<=Not A and B; END Block Half_ Subtractor;END Behave;

第九章、 VHDL 基本语句

Page 202: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

元件及元件例化

层次化设计方法

第九章、 VHDL 基本语句

Page 203: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

元件也是完整的 VHDL 设计,作为底层设计,通过元件声明,使之可在其他模块中被调用,元件声明可放在程序包中,也可 在某个 设 计 的 结 构 体 中 声 明 。 元件例化指元件的调用。

第九章、 VHDL 基本语句一、元件及元件例化

Page 204: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

component< 元件实体名 > port (< 元件端口信息,同该元件实现时的实体的 port 部分 >); end component;

-- 元件例化: < 例化名 >:< 实体名,即元件名 > port map (< 端口列表 >);

第九章、 VHDL 基本语句

Page 205: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

例如,在一个设计中调用一个模为 10 的计数器cntm10 和一个七段译码器 decode47 构成如下电路:

则该调用过程即元件例化的 VHDL 描述如下:顶层文件为 cntvh10

第九章、 VHDL 基本语句

Page 206: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

library IEEE; use IEEE.std_logic_1164.all; entity cntvh10 IS port ( Rd,ci,clk :in std_logic; co : out std_logic; qout : out std_logic_vector(6 downto 0)); end cntvh10;

第九章、 VHDL 基本语句

Page 207: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

ARCHITECTURE arch OF cntvh10 IS -- 元件声明 Component decode47 is port (adr: in std_logic_vector(3 downto 0); decodeout:out std_logic_vector(6 downto 0));

end Component;

第九章、 VHDL 基本语句

Page 208: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

Component cntm10 is port (ci : IN std_logic;

nreset : IN std_logic; clk : IN std_logic

co : out std_logic; qcnt : buffer std_logic_vector(3 downto 0));

end Component;

第九章、 VHDL 基本语句

Page 209: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

signal qa : std_logic_vector(3 downto 0);BEGIN u1: cntm10 port map(ci,Rd,clk,co,qa); --元件例化 u2: decode47 port map(decodeout=>qout, adr=>qa);END arch;

VHDL 基本语法:元件及元件例化

Page 210: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

VHDL 基本语法:元件及元件例化

元件例化时的端口列表可按位置关联方法,如 u1 ,这种方法要求的实参(该设计中连接到端口的实际信号,如 ci,Rd 等)所映射的形参(元件的对外接口信号)的位置同元件声明中一样;元件例化时的端口列表也可按名称关联方法映射实参与形参,如 u2 。格式为(形参 1=> 实参 1,形参 2=> 实参 2,… . )。这种方法与位置无关。

Page 211: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

例子:设计一个模为 60的 8421BCD 计数器、七段译码器输出。 该题可分解为: 一个模为 60的 8421BCD 计数器; 一个七段译码器, 将计数器及译码器编译到库中,之后用元 件例化来实现顶层设计。

二、层次化设计方法

Page 212: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

1. 设计模为 60的 8421BCD 计数器2. 设计七段译码器3. 建立程序包 cntpkg4. 顶层设计

层次化设计方法

Page 213: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

1. 设计模为 60的 8421BCD 计数器,文件名 cntm60.vhdlibrary ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;ENTITY cntm60 IS

PORT( ci : IN std_logic;

nreset : IN std_logic;load : IN std_logic;d : IN std_logic_vector(7 downto 0);clk : IN std_logic;

co : out std_logic;qh : buffer std_logic_vector(3 downto 0);ql : buffer std_logic_vector(3 downto 0) );

END cntm60;

cinresetloadclkd[0..7]

coqh[0..3]

ql[0.. 3]

cntm60

Page 214: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

ARCHITECTURE behave OF cntm60 ISBEGIN co<='1' when (qh="0101" and ql="1001" and ci='1') else '0';

PROCESS (clk,nreset)BEGIN

IF(nreset='0') THEN qh<="0000"; ql<="0000";

ELSIF (clk'EVENT AND clk = '1') THEN if(load='1') then qh<=d(7 downto 4); ql<=d(3 downto 0);

异步清零

同步置数

Page 215: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

if(qh=5) then qh<="0000";

else qh<=qh+1;

end if; else

ql<=ql+1; end if;

END IF; --end if LOADEND IF; --end if _reset

END PROCESS; END behave;

elsif(ci='1') then if (ql=9) then ql<="0000";

高位加 1

低位加 1

低位进位高位进位

Page 216: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

2. 设计七段译码器 -- 文件名 decode47.vhd ,代码如下:library ieee;use ieee.std_logic_1164.all;entity decode47 is port (adr :in std_logic_vector(3 downto 0); decodeout :out std_logic_vector(6 downto 0));end decode47;

architecture truthtable of decode47 is begin process(adr) begin

adr[0..3] out[0..6]译码器

Page 217: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

case adr is when "0000"=> decodeout<= "1111110"; when "0001"=> decodeout<= "0110000"; when "0010"=> decodeout<= "1101101"; when "0011"=> decodeout<= "1111001"; when "0100"=> decodeout<= "0110011"; when "0101"=> decodeout<= "1011011"; when "0110"=> decodeout<= "1011111"; when "0111"=> decodeout<= "1110000"; when "1000"=> decodeout<= "1111111"; when "1001" => decodeout<= "1111011"; when others => decodeout<= “0000000"; end case; end process; end truthtable; 相当于“ then”

a

b

c

d

e

f g

a b c d e f g

Page 218: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

3.建立程序包 cntpkg

格式:PACKAGE 程序包名 IS 声明区END 程序包名 ;

将上面设计的计数器,译码器定义到程序包 cntpkg 中,以便其他程序调用。-- 文件名 cntpkg.vhd

VHDL 基本语法:层次化设计方法

Page 219: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

library ieee;use ieee.std_logic_1164.all;PACKAGE cntpkg IS -- Component Declaration Component cntm60 IS

port ( ci : INstd_logic;

nreset : IN std_logic; load : INstd_logic;

d : IN std_logic_vector(7 downto 0); clk : INstd_logic;

VHDL 基本语法:层次化设计方法:建立程序包 cntpkg

Page 220: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

co : out std_logic;qh : buffer std_logic_vector(3 downto 0);ql : buffer std_logic_vector(3 downto 0)

); end Component;Component decode47 is port (adr: in std_logic_vector(3 downto 0); decodeout:out std_logic_vector(6 downto 0)); end Component;END cntpkg; -- 程序包建立

VHDL 基本语法:层次化设计方法:建立程序包 cntpkg

Page 221: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

以元件调用方式实现设计。建立顶层文件 cnt.vhd 如下:library work; --work为 Synplify 中指定的用户库use work.cntpkg.all;library IEEE;use IEEE.std_logic_1164.all;entity cnt IS port ( nreset,load,ci,clk:in std_logic; d: in std_logic_vector(7 downto 0); co : out std_logic;

q1 : out std_logic_vector(6 downto 0); q2 : out std_logic_vector(6 downto 0));

end cnt;

4. 顶层设计

Page 222: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

ARCHITECTURE arch OF cnt ISsignal qa,qb : std_logic_vector(3 downto 0);BEGIN u1:cntm60 port map(ci,nreset,load,d,clk,co,qa,qb); u2:decode47 port map(qa,q1); u3:decode47 port map(qb,q2); END arch;

VHDL 基本语法:层次化设计方法 : 顶层设计

cinresetloadclkd[0..7]

coqh[0..3]

ql[0.. 3]

cntm60adr[0..3] out[0..6]译码

U3adr[0..3] out[0..6]译码

U2

qb

qa

q2

q1

U1

Page 223: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

上例中最上面两行: library work;

use work.cntpkg.all; 其中 work 为库名,由 Synplify 内建;cntpkg 为自建的 程 序 包 。 use 语 句 使 库work 的程序包 cntpkg 中所有 (all) 的内容对下面的设计都可见,亦即刚才设计的计数器,译码器可在下面的设计中被调用。

VHDL 基本语法:层次化设计方法 : 顶层设计

Page 224: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

<完 >

Page 225: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

课题介绍 :1. 出租车计价器设计和实现2. 电子钟设计

课程设计

Page 226: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

课题 1: 出租车计价器设计和实现要求 : (1) 计价器按 1.2元 /公里计费 ,超过 10公里后 , 则按 1.8元 /公里收费(2)起步价 6元 (3公里 ),超过 3公里后 ,计价累加 0.6元 ,10公里内以后每过 0.5公里累加 0.6元 .(3)过 10公里后 , 计价累加 0.9元 , 以后每过 0.5公里累加 0.9元 .(4)公里数 4位数字显示 ,精确到 0.1公里 ; 出租车计价 4位数字显示 ,精确到0.1 元

Page 227: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

课题 2: 电子钟设计要求 : (1) 电子种可以 12/24小时制切换(2) 电子钟具有准点报时功能(3) 分别显示“小时” ,“ 分” ,“秒”(4) 可以进行“分” ,“小时”调校 .

Page 228: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html

16周 : 上机调试 \ 下载17周 : 交课程设计报告

Page 229: 第五章   VHDL  编程

Department of Electronics and Information Science Department of Electronics and Information Science http://www.qcxy.hb.cn/dxx/Index.htmlhttp://www.qcxy.hb.cn/dxx/Index.html