Cypress Semiconductor VHDL Training
Download
Report
Transcript Cypress Semiconductor VHDL Training
Logic Design With
补充内容:硬件描述语言
VHDL
8.1 VHDL概述
V HSIC (Very High Speed Integrated Circuit-超高速集成电路)
Hardware Description Language(硬件描述语言)
VHDL is a Design Description Language(设计)
VHDL is a Simulation Language (仿真)
It is an IEEE Standard Language (国际标准)
VHDL or HDL的优势
Very Fast Time-to-Market(高效性)
Allows designers to quickly develop designs requiring tens of
thousands of logic gates or more
Provides powerful high-level constructs for describing complex logic
Supports modular design methodology and multiple levels of
hierarchy
One language for design and simulation(功能强大丰富)
Allows creation of device-independent designs that are portable to
multiple PLD vendors(可移植性强)
8.2 VHDL 设计方式
VHDL设计称为实体,包括 ENTITY(实体)描述(可以简称实体) 和
ARCHITECTURE(结构体)
ENTITY 描述了电路的 I/O接口情况
ARCHITECTURE 描述了具体功能的实现情况
实体
实体1
结构体
实体
结构体
实体2
实体3
结构体
结构体
8.2.1 实体描述(实体)
A “BLACK BOX”
The ENTITY describes the I/O of the black box (I/O端口描述)
black_box
rst
co
d[7..0]
clk
q[7..0]
例 8.1 Entity declaration
实体名,一般要和设计文件同名
ENTITY black_box IS PORT (
clk, rst: IN Bit;
d:
IN Bit_vector(7 DOWNTO 0);
q:
OUT Bit_vector(7 DOWNTO 0);
co:
OUT Bit);
END black_box;
black_box
rst
大写/黑体
是关键字
co
d[7..0]
clk
PORTS have an associated(属性): name(名字), mode(模式), and type(类型)
q[7..0]
IEEE-1164版本(相对于IEEE-1076版本)
A package created to solve the limitations of the BIT type
Nine values instead of just two ('0' and '1')
STD_LOGIC and STD_LOGIC_VECTOR are now the industry standard
logic type for digital design
STD_LOGIC and STD_LOGIC_VECTOR are used instead of BIT and
BIT_VECTOR when a multi-valued logic system is required
STD_LOGIC and STD_LOGIC _VECTOR must be used when tri-state
logic (Z) is required
To be able to use this new type, you need to add 2 lines to your code:
LIBRARY ieee;
类似C语言的
USE ieee.std_logic_1164.ALL;
Include语句
例8.2 Entity Declaration
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
ENTITY black_box IS PORT (
clk, rst: IN Std_logic;
d:
IN Std_logic_vector(7 DOWNTO 0);
q:
OUT Std_logic_vector(7 DOWNTO 0);
co:
OUT Std_logic);
END black_box;
black_box
MODE
TYPE
rst
co
d[7..0]
clk
如果数值只出现“0”“1”,可以用Bit代替之
q[7..0]
8.2.2 The Architecture(结构体)
Architectures describe what is in the black box (i.e. the function or
behavior of entities)
Descriptions can be either a combination of
Structural descriptions (结构描述--逻辑图)
• Instantiations of building blocks (placement of components
like a schematic and their connections)
Behavioral descriptions (行为描述)
• Algorithmic (算法or “high-level”) descriptions:
IF a = b THEN state <= state5;
• Boolean equations(布尔方程,数据流描述)
x <= (a OR b) AND c;
just
(1)结构描述
结构描述是侧重描述设计单元之间的硬件连接关系。这种描述
方式等效于原理图(逻辑图),相当于将原理图用文字描述出来了,因此具有原理图
设计方法的特点,描述方式繁琐,主要适用于系统模块之间的关系描述。
(2)数据流描述
数据流描述方式也称为寄存器传输级(RTL,Register Transfer
Level)描述,它既显式地表示了设计单元的行为,又隐含了该设计单元的结构。主要
使用并行信号赋值语句来描述这种信号间的数据流转。当语句中输入信号的值发生改
变时,赋值语句就被激活,随着这种语句对电路行为的描述,大量的有关这种结构的
信息也从这种逻辑描述中显现出来。数据流描述能比较直观地表达底层逻辑行为。
(3)行为描述
行为描述是描述电路的功能或者说电路行为,而没有直接指明
或涉及实现这些行为的硬件结构,包括硬件特性、连线方式、逻辑行为方式等。行为
描述只表示数据在输入与输出之间进行转换的行为,不包括任何结构的信息。行为描
述主要使用顺序语句,以算法的形式描述数据的变化和传送。行为描述的优点在于抽
象层次高,只需要描述清楚输入与输出的行为,而不需要花费跟多的精力关注设计功
能的具体电路结构,可以大大的提高设计效率。
例8.3 结构体形式
结构体名
ARCHITECTURE a OF black_box IS
BEGIN
实体名
[并行处理语句]
END a;
一个实体可以包含多个结构体(名字不同),但只有其中一个最终生成逻辑电路;可以
通过configuration语句进行配置(但并不是所有设计软件都支持,Max-plusII不支持)。
HDL学习提示
(1)用硬件电路设计思想来编写HDL 学好HDL的关键是充分理解HDL语句和硬件电
路的关系。编写HDL就是在描述一个电路,所以设计一段HDL程序以后,应当对生成的
电路有一些大体上的了解, 而不能用纯软件的设计思路来编写硬件描述语言。 要做到
这一点,需要同学们多实践、多思考、多总结。
(2)HDL的可综合性 HDL有两种用途:系统仿真和硬件实现。如果程序只用于仿真
,那么几乎所有的语法和编程方法都可以使用。但如果HDL程序是用于硬件实现,那么
设计者必须保证程序“可综合性”(程序的功能可以用硬件电路实现)。不可综合的
HDL语句在软件综合时将被忽略或者报错。所以应当牢记一点“所有的HDL描述都可以
用于仿真,但不是所有的HDL描述都能用硬件实现”。
(3)语法掌握贵在精而不在多 30%的基本HDL语句就可以完成95%以上的电路设计
,很多生僻的语句并不能被所有的综合软件所支持,在程序移植或者更换软件平台时,
容易产生兼容性问题,也不利于其他人阅读和修改。建议多用心钻研常用语句,理解这
些语句的硬件含义,这比多掌握几个新语法更实用。
8.3 VHDL Statements
There are two types of statements, Concurrent(并行) and Sequential(串行)
1、Concurrent Statements (means in parallel)
Concurrent statements are “executed” concurrently (at the same time)
The order of concurrent statements is not important
例8.4 并行语句
For example, suppose we had the following 2 lines of code:
x <= a AND b;
y <= x NAND C;
This will produce exactly the same result as:
y <= x NAND C;
x <= a AND b;
a
b
x
c
The order that you write the statements makes no difference,
because they are concurrent (working in parallel)
y
2、Sequential Statements (means in series)
Sometimes we need to model complex functions. In that case, we
can use an “algorithm” or a model to describe the function. This is
done with Sequential Statements
With Sequential statements, the ORDER of the statements is
important (example later)
Therefore, we use a process(进程) to mark the beginning and end
of a block of sequential statements
Each completed process is considered to be one big concurrent
statement (there can be many processes inside one architecture)
3、VHDL “Process” ?
Processes are either awake or asleep (active or inactive(状态保持))
A process normally has a sensitivity list(敏感参数表)
When a signal in that sensitivity list changes value, the process
wakes up and all of the sequential statements are “executed”
At the end of the process, all the outputs are updated and the process goes
back to sleep until the next time a signal changes in the sensitivity list
无限循环
8.4 VHDL Designs
8.4.1 组合逻辑电路—8位加法器
8.4.2 触发器
8.4.3 时序逻辑电路—计数器和串行数据检测电路
LIBRARY IEEE; USE IEEE.Std_logic_1164.ALL; --加载库和程序包
ENTITY adder_8bits IS
PORT (
op1, op2 :IN Integer range 0 to 255; --定义两个8位加数
c
:OUT Bit;
--定义进位输出
sum
:OUT Integer); --定义和输出变量,可加上range 0 to 255
END adder_8bits;
--------------并行实现------------------------------------------------------------------------------------ARCHITECTURE a1 OF adder_8bits IS
SIGNAL tmp: Integer;
--定义signal类型变量,便于实现
BEGIN
tmp <= op1 + op2;
--加法运算,这3条语句顺序可以颠倒
c <= '1' WHEN tmp>255 else '0'; --获得进位信号
sum<=tmp-256 WHEN tmp>255 else tmp; --限制和的数值范围(不会出现减法电路!)
END a1;
--------------串行实现------------------------------------------------------------------------------------ARCHITECTURE a2 OF adder_8bits IS
BEGIN
PROCESS(op1,op2)
--敏感参数是op1和op2
Variable num :Integer; --定义变量
BEGIN
num :=op1 + op2;
IF num >255 THEN c<='1';ELSE c<='0'; END IF; --进位
IF num >255 THEN sum <= num-256; ELSE sum<=num; END IF; --限制和的数值范围
END PROCESS; END a2;
其
它
实
现
方
法
OP1
1
2
111
0
OP1
0
168
211
255
Sum
1
170
66
255
C
八位加法器仿真图
ENTITY regdff IS PORT (
d, clk
:IN BIT;
q
:OUT BIT);
END regdff;
--实体说明,定义输入和输出端口
------------边沿触发器第一种实现------------------------------------------------------------------------ARCHITECTURE a1 OF regdff IS
BEGIN
PROCESS (clk)
--时钟脉冲触发
BEGIN
IF (clk'EVENT AND clk = '1') THEN --定义上升沿触发
q <= d;
--特性方程,也可以直接通过算法实现
END IF;
END PROCESS;
END a1;
-----------第二种实现------------------------------------------------------------------------------------ARCHITECTURE a2 OF regdff IS
BEGIN
PROCESS
--没有敏感信号表有,但内部WAIT语句
BEGIN
WAIT UNTIL (clk'EVENT AND clk = '1'); --定义上升沿触发
q <= d;
END PROCESS;
END a2;
ENTITY mycounter IS PORT(
clk, clear :IN Bit;
c
: OUT Bit;
qc
: OUT Integer range 0 to 15);
Constant modulus: Integer :=10;
END mycounter;
--设定计数器模数为10
ARCHITECTURE a OF mycounter IS
BEGIN
PROCESS (clk, clear)
Variable cnt
: Integer range 0 to 16;
--定义用于计数的变量
BEGIN
IF (clear=‘0’) THEN cnt:= 0; c<=‘0’;
--异步清零
ELSIF (clk'EVENT AND clk = '1') THEN cnt:=cnt+1; --加法计数
IF cnt=modulus-1 TEHN c<=‘1’; END IF;
--进位信号赋值
IF cnt=modulus then cnt :=0; c<='0'; END IF;
END IF;
qc<=cnt;
--赋值到输出
END PROCESS;
End a;
其
它
实
现
方
法
clk
clear
c
qc
0
1
2
3
4
5
6
计数器仿真图
7
8
9
0
1
串行数据检测器:连续输入3个或以上个“1”,输出为“1”,否则为“0”。
1/0
1/0
0/0
X/Y
S0
S1
0/0
X/Y
00
0/0
0/0
1/0
01
0/0
0/0
1/0
S2
11
1/1
1/1
(a)原始状态图
(b)编码后状态图
1/0
ENTITY stmch5 IS PORT(
clk, x, rst : IN Bit;
0/0
S0
S1
Y
: OUT Bit;
0/0
st
: OUT Bit_vector(1 downto 0));--输出状态编码
0/0
1/0
END stmch5;
Architecture behave5 of stmch5 is
S2
Type state_values is (s0, s1, s2);
--定义枚举类型
Signal state : state_values;
--定义signal类型枚举变量
1/1
Begin
pro1: Process (clk, rst)
Begin
--进程1,时序逻辑
If rst = '1' then
state <= s0; y <='0';
--异步清零
elsif clk'event and clk='1' then
Case state is
--实现的是状态转换和结果输出
When s0 => if x = '1' then state <= s1; y <='0'; else state <= s0; y <='0'; end if;
When s1 => if x = '1' then state <= s2; y <='0'; else state <= s0; y <='0'; end if;
When s2 => if x = '1' then state <= s2; y <='1'; else state <= s0; y <='0'; end if;
When others => state <= s0; y <=‘0’;
--自启动
End case;
--进程2,将时序状态译码输出
End if;
pro2: process(state) --状态state改变,输出改变
End process;
Begin
Case state is
When s0 => st <="00";
(End behave5;)
When s1 => st <="01";
When s2 => st <="11";
When others => st <="10";
End case; End process; End behave5;
X/Y
CLK
Reset
x
y
State
s0
s1
s2
s0
s1
s2
串行数据检测器仿真图
s0
s1
s2
s0
D触发器VHDL代码-电平触发器
Process(clk, d)
Begin
If clk= ’1’
Then q<=d;
End if;
End process;
--敏感信号表中有D输入变量
--电平触发型寄存器
有clk和d俩个敏感信号,也就是说当clk变化的时候,即clk上升沿的情况下将当前
的d送入q;而当d变化的时候,启动process,此时clk=‘1’作为一个条件,满足这个条
件则把d送入q,否则跳出process保持原值,综合起来看clk='1'是作为一个条件,当
clk='1'时,也就是当clk为高电平的时候将d送入q,就产生了电平触发
Library IEEE;
USE IEEE.Std_logic_1164.all;
USE ieee.std_logic_unsigned.all;
--std_logic类型支持
--std_logic“+”运算支持
Entity adder_8bits Is
Port (cin:in std_logic;
a,b:in std_logic_vector(7 downto 0);
s:out std_logic_vector(7 downto 0);
cout:out std_logic);
End adder_8bits;
Architecture behav Of adder_8bits Is
Signal sint:std_logic_vector(8 downto 0);
Signal aa:std_logic_vector(8 downto 0);
Signal bb:std_logic_vector(8 downto 0);
Begin
aa<='0'&a(7 downto 0); --赋值,“&”是连接运算符
bb<='0'&b(7 downto 0);
sint<=aa+bb+cin;
s(7 downto 0)<=sint(7 downto 0);--“和”赋值
cout<=sint(8); --进位赋值
End behav;
返回
Library ieee; Use ieee.std_logic_1164.all; Use ieee.std_logic_unsigned.all;
Entity counter_4bits Is
Port( cp,rd,ld,ep,et
:in std_logic;
d
:in std_logic_vector(3 downto 0);
c
:out std_logic;
q
:out std_logic_vector(3 downto 0));
End counter_4bits;
Architecture a Of counter_4bits Is
Signal tmp:std_logic_vector(3 downto 0);
Begin
Process(cp,rd,ld,ep,et)
Begin
If rd=‘0’ then tmp<=“0000”; --异步清零
elsif cp‘event and cp=’1‘ then --上升沿触发
If ld=‘0’ then tmp<=d; --同步置数
elsif ep=‘1’ and et=‘1’ then --使能端有效
if tmp="1111" then tmp<="0000"; else tmp<=tmp+1;end if;
if tmp="1110" then c<='1'; else c<='0';end if;
End if;
End if;
q<=tmp;
End process; End a;
返回