VHDL硬件描述语言基础
简介
基本结构
基本数据类型
设计组合电路
设计时序电路
设计状态机
大规模电路的层次化设计
Function and Procedure
简介--背景
传统数字电路设计方法不适合设计大规模的系统 。 工程师不容易理解原理图设计的功能 。
众多软件公司开发研制了具有自己特色的电路硬 件 描 述 语 言 ( Hardware Description
Language,HDL),存在着很大的差异,工程师一旦选用某种硬件描述语言作为输入工具,就被束缚在这个硬件设计环境之中 。 因此,硬件设计工程师需要一种强大的,标准化的硬件描述语言,作为可相互交流的设计环境 。
简介--背景
美国国防部在 80年代初提出了 VHSIC( Very
High Speed Integrated Circuit)计划,其目标之一是为下一代集成电路的生产,实现阶段性的工艺极限以及完成 10万门级以上的设计,建立一项新的描述方法。 1981年提出了一种新的
HDL,称之为 VHSIC Hardware Description
Language,简称为 VHDL,这种语言的成就有两个方面:
描述复杂的数字电路系统
成为国际的硬件描述语言标准
VHDL的优点
用于设计复杂的、多层次的设计。支持设计库和设计的重复使用
与硬件独立,一个设计可用于不同的硬件结构,
而且设计时不必了解过多的硬件细节。
有丰富的软件支持 VHDL的综合和仿真,从而能在设计阶段就能发现设计中的 Bug,缩短设计时间,降低成本。
更方便地向 ASIC过渡
VHDL有良好的可读性,容易理解。
VHDL与计算机语言的区别
运行的基础
– 计算机语言是在 CPU+ RAM构建的平台上运行
– VHDL设计的结果是由具体的逻辑、触发器组成的数字电路
执行方式
– 计算机语言基本上以串行的方式执行
– VHDL在总体上是以并行方式工作
验证方式
– 计算机语言主要关注于变量值的变化
– VHDL要实现严格的时序逻辑关系
--eqcomp4 is a four bit equality comparator
Library IEEE;
use IEEE.std_logic_1164.all;
entity eqcomp4 is
port(a,b:in std_logic_vector(3 downto 0);
equal,out std_logic);
end eqcomp4;
architecture dataflow of eqcomp4 is
begin
equal <=?1? when a=b else?0?;
End dataflow;
VHDL 大小写不敏感
eqcomp4.vhd
包实体构造体文件名和实体名一致每行;结尾关键字 begin
关键字 end后跟实体名关键字 end后跟构造体名库实体( Entity)
描述此设计功能输入输出端口( Port)
在层次化设计时,Port为模块之间的接口
在芯片级,则代表具体芯片的管脚
A[3..0]
B[3..0]
equal
Entity eqcomp4 is
port(a,b,in std_logic_vector(3 downto 0);
equal:out std_logic
);
end eqcomp4;
实体--端口的模式
输入( Input)
输出( Output)
双向( Inout):可代替所有其他模式,
但降低了程序的可读性,一般用于与
CPU的数据总线接口
缓冲( Buffer):与 Output类似,但允许该管脚名作为一些逻辑的输入信号
Out与 Buffer的区别
Entity test1 is
port(a,in std_logic;
b,c,out std_logic
);
end test1;
architecture a of test1 is
begin
b <= not(a);
c <= b;--Error
end a;
Entity test2 is
port(a,in std_logic;
b,buffer std_logic;
c,out std_logic
);
end test2;
architecture a of test2 is
begin
b <= not(a);
c <= b;
end a;
结构体( Architecture)
描述实体的行为
结构体有三种描述方式
– 行为描述 (behavioral)
– 数据流描述 (dataflow)
– 结构化描述 (structural)
结构体--行为描述
Architecture behavioral of eqcomp4 is
begin
comp,process (a,b)
begin
if a=b then
equal <=?1?;
else
equal <=?0?;
end if;
end process comp;
end behavioral ;
高层次的功能描述,
不必考虑在电路中到底是怎样实现的。
结构体--数据流描述描述输入信号经过怎样的变换得到输出信号
Architecture dataflow1 of eqcomp4 is
begin
equal <=?1? when a=b else?0?;
end dataflow1;
Architecture dataflow2 of eqcomp4 is
begin
equal <= not(a(0) xor b(0))
and not(a(1) xor b(1))
and not(a(2) xor b(2))
and not(a(3) xor b(3));
end dataflow2;
当 a和 b的宽度发生变化时,需要修改设计,当宽度过大时,设计非常繁琐结构体--结构化描述
architecture struct of eqcomp4 is
begin
U0:xnor2 port map(a(0),b(0),x(0));
U1:xnor2 port map(a(1),b(1),x(1));
U2:xnor2 port map(a(2),b(2),x(2));
U3:xnor2 port map(a(3),b(3),x(3));
U4:and4 port map(x(0),x(1),x(2),x(3),equal);
end struct;
类似于电路的网络表,将各个器件通过语言的形式进行连接,与电路有一一对应的关系。
一般用于大规模电路的层次化设计时。
三种描述方式的比较描述方式 优点 缺点 适用场合结构化描述连接关系清晰,电路模块化清晰电路不易理解、
繁琐、复杂电路层次化设计数据流描述布尔函数定义明白不易描述复杂电路,修改不易小门数设计行为描述 电路特性清楚明了进行综合效率相对较低大型复杂的电路模块设计
VHDL标识符( Identifiers)
基本标识符由字母、数字和下划线组成
第一个字符必须是字母
最后一个字符不能是下划线
不允许连续 2个下划线
保留字 (关键字 )不能用于标识符
大小写是等效的
VHDL数据对象( Data Objects)
常数( Constant)
– 固定值,不能在程序中被改变
– 增强程序的可读性,便于修改程序
– 在综合后,连接到电源和地
– 可在 Library,Entity,Architecture,Process
中进行定义,其有效范围也相应限定
Constant data_bus_width,integer,= 8;
VHDL数据对象( Data Objects)
信号( Signals)
– 代表连线,Port也是一种信号
– 没有方向性,可给它赋值,也可当作输入
– 在 Entity中和 Architecture中定义
– 设定的初始值在综合时没有用,只是在仿真时在开始设定一个起始值。在 Max+ PlusII中被忽略。
– 用 <= 进行赋值
signal count,bit_vector(3 downto 0):=“0011”;
VHDL数据对象( Data Objects)
变量( Variable)
– 临时数据,没有物理意义
– 只能在 Process和 Function中定义,并只在其内部有效
– 要使其全局有效,先转换为 Signal。
– 用,= 进行赋值
variable result,std_logic,=?0?;
信号与变量的区别
architecture rtl of start is
signal count,integer range 0 to 7;
begin
process(clk)
begin
if (clk'event and clk='1') then
count <= count + 1;
if(count=0) then
carryout <= '1';
else
carryout <= '0';
end if;
end if;
end process;
end rtl;
architecture rtl of start is
begin
process(clk)
variable count,integer range 0 to 7;
begin
if (clk'event and clk='1') then
count,= count + 1;
if(count=0) then
carryout <= '1';
else
carryout <= '0';
end if;
end if;
end process;
end rtl;
信号与变量的区别
architecture a of start is
signal tmp,std_logic;
begin
process(a_bus)
begin
tmp <= '1';
for i in 3 downto 0 loop
tmp <= a_bus(i) and tmp;
end loop;
carryout <= tmp;
end process;
end a;
architecture a of start is
begin
process(a_bus)
variable tmp:std_logic;
begin
tmp,= '1';
for i in 3 downto 0 loop
tmp,= a_bus(i) and tmp;
end loop;
carryout <= tmp;
end process;
end a;
VHDL数据类型
标量类型( Scalar)
– 枚举( Enumeration)
– 整数( Integer)
– 浮点数( Float)
– 物理( Physical)
复合类型( Composite)
VHDL数据类型--枚举
列举数据对象可能存在的值,一般用于定义状态机的状态
– Type states is (idle,start,running,pause,stop)
– Signal current_state,states;
IEEE1076标准中预定义了两个枚举类型
– Type boolean is (False,True)
– Type bit is (?0?,?1?)
– Signal a,bit;
VHDL数据类型--枚举
IEEE1164标准中预定义了一个枚举类型
Type std_logic is(?U?,?X?,?0?,?1?,?Z?,
W?,?L?,?H?,?-?);
– 该类型能比较全面地包括数字电路中信号会出现的几种状态,因此一般情况把这种类型代替 bit
– Signal a,std_logic;
– 注意,这里的大小写是敏感的
VHDL数据类型
整数、浮点数
– 方便用于数值方面的运算:加减乘除
– 整数范围,-231 ~ 231 –1,经常用于计数器
– 实数范围,-1.0E38~ +1.0E38,不被
Max+ PLusII支持
Variable a,integer range –255 to +255;
物理类型
– 主要用于调试
VHDL数据类型--复合类型
Array Types
– 多个相同类型成员组成的队列,一般用于定义数据总线、地址总线等。
Signal a,std_logic_vector(7 downto 0);
a <= B“00111010”; a <= X,3A”;
– 可自定义复合类型
Type word is array (15 downto 0) of bit;
Signal b,word;
Type table8x4 is array (0 to 7,0 to 3) of bit;
VHDL数据类型--复合类型
Record Types
– 相同或不同类型的元素组成,类似 C中的结构
– 具有模型抽象能力,用于描述一个功能模块
– Type iocell is record
Enable,bit;
DataBus,bit_vector(7 downto 0);
end record;
singal bus,iocell;
bus.Enable <=?1?;
bus.DataBus <=,00110110”;
VHDL数据类型及子类型
Types And Subtypes
VHDL是强类型语言,必须用类型转换函数才能进行不同类型之间的转换
– type byte_size is integer range 0 to 255;
– signal a,byte_size;
– signal b,integer range 0 to 255;
– if a=b then ……
采用以下方式
subtype byte_size is integer range 0 to 255;
属性( Attributes)
提供 Entity,Architecture,Type和 Signals的信息。
有许多预定义的值、信号和范围的属性
一个最常用的属性是’ event
– if clk?event and clk=?1? then
left,’ right,?high,?low,?length
– type count is integer range 0 to 127
– count?left = 0; count?right = 127;
– count?high = 127; count?low = 0;
– count?length = 128;
VHDL运算符
逻辑运算符
AND,OR,NAND,NOR,XOR,NOT
关系运算符
=,/=,<,>,<=,>=
算术运算符
+,-,*,/
并置 (连接 )运算符
&
组合电路--并行语句( Concurrent)
并行语句位于 Process外面,同时执行,不分位置的先后顺序
并行语句包括:
– 布尔等式,<=
– With-select-when
– When-else
布尔等式
A <= s(0) and s(1);
B <= not(y);
组合电路--并行语句
With-select-when语句
With Sel_signal select
Signal_name <= a when Sel_signal_1,
b when Sel_signal_2,
c when Sel_signal_3,…
x when Sel_signal_x;
Signal s,std_logic_vector(1 downto 0);
Signal a,b,c,d,x,std_logic;
With s select
x <= a when,00”,
b when,01”,
c when,10”,
d when others;
组合电路--并行语句
When-else语句
Signal_name <= a when condition1 else
b when condition2 else
c when condition3 else …
x ;
x <= a when s=“00” else
b when s=“01” else
c when s=“10” else
d;
Signal a,b,c,d:std_logic;
Signal w,x,y,z:std_logic;
x <= w when a=?1? else
x when b=?1? else
y when c=?1? else
z when d=?1? else
0?;
组合电路--并行语句
实现优先级编码器
encode <=,111” when D(7) =?1? else
“110” when D(6) =?1? else
“101” when D(5) =?1? else
“100” when D(4) =?1? else
“011” when D(3) =?1? else
“010” when D(2) =?1? else
“001” when D(1) =?1? else
“000” when D(0) =?1? else
“000”;
组合电路--并行语句
When-else语句条件语句可以是一个简单的表达式
With-select-when则不能采用表达式作为条件
a <=,0000” when state=idle and state=?1? else
“0001” when state=idle and state=?0? else
b when state=running and state=?1? else
a;
组合电路--顺序语句
( Sequential)
Process,Function,Procedure中的语句都是顺序执行,以 Process为例
Process与 Process之间,与其他并行语句之间都是并行的关系
– If-then-else
– Case-when
组合电路--顺序语句
If-then-else
– If(condition1) then do something;
elsif(condition2) then …
else do something different;
end if;
组合电路--顺序语句
Process(addr)
Begin
step <=?0?;
if(addr = X,F”) then
step <=?1?;
end if;
End process;
Process(addr)
Begin
if(addr = X,F”) then
step <=?1?;
else
step <=?0?;
end if;
End process;Process(addr)
Begin
if(addr = X,F”) then
step <=?1?;
end if;
End process;
Step <= addr(3) * addr(2) *
Addr(1) * addr(0) + step
组合电路--顺序语句
用于作地址译码
– InRam <=?0?; Periph1 <=?0?; Periph2 <=?0?;
OutRam<=?0?; EEPRom <=?1?;
If addr >= X,0000” and addr <X,4000” then
InRam <=?1?;
elsif addr >= X,4000” and addr <X,4008” then
Periph1 <=?1?;
elsif addr >= X,4008” and addr <X,4010” then
Periph2 <=?1?;
elsif addr >= X,8000” and addr <X,C000” then
OutRam <=?1?;
elsif addr >= X,C000” then
EEPRom <=?1?;
end if;
组合电路--顺序语句
Case-when
case sel_signal is
when value_1 =>
(do sth)
when value_2 =>
(do sth)

when value_last =>
(do sth)
end case;
组合电路--顺序语句
实现数码管译码器
Process(address)
begin
case address is
when,0000” => decode <= X,3F”
when,0001” => decode <= X,60”
when,0010” => decode <= X,B5”
when,0011” => decode <= X,F4”
……
when others => decode <= X,00”;
end case;
end process;
几种语句的比较语句 With-select-
when
When-else If-else Case-when
选择条件 一个信号的不同值,互斥多个信号多种组合,不必互斥多个信号多种组合,
不必互斥一个信号的不同值,互斥语句属性 并行 并行 顺序 顺序用途 编码、译码、
多路选择器优先编码器,
地址译码器优先编码器,地址译码器编码、译码、
多路选择器状态机同步时序逻辑电路
Process(clk)
begin
if(clk?event and clk=?1?) then
q <= d;
end if;
end process;
Process(clk)
begin
if(clk=?1?) then
q <= d;
end if;
end process;
D触发器 缓冲器实现 T触发器
Process(clk)
begin
if(clk?event and clk=?1?) then
if(t =?1?) then
q <= not(q);
else
q <= q;
end if;
end if;
end process;