vhdl语法格式(1)

合集下载

VHDL语法结构、数据类型与基本命令语句.

VHDL语法结构、数据类型与基本命令语句.

A(3) A(2) A(1)
B(0) B(1) B(2)
A(0)
B(3)

序列信号的分解与合并
VHDL的语法是可接收分解与合并,其中”&”表合并动作
Architecture a of Test is Signal A : Std_logic_vector(3 downto 0); Signal B : Std_logic_vector(0 to 3); Signal C : Std_logic_vector(0 to 1); Signal D : Std_logic_vector(1 downto 0);
Nand 与非门
Xnor 或非门

/=
<
<=
小于等于
>
>=
大于等于
说明
等于
不等于
小于
大于
注:VHDL的赋值符号不是“=”(在C语言中使用),而逻辑关系等 于运算则使用“=”(C语言中使用“==”)。

列举数据类型
使用这种数据类型是希望用这样的类型定义,将所有的元素 表列出来。 定义:Type 列举名称 is (元素1,元素2…….); 主要是提高程序的可读性。

实数(Real)
定义:Type Real is range -1.7E38 to 1.7E38 无法综合
数据对象定义:常数、信号、变量

常数(Constant)
将数据对象定义为常数,是希望该常数所代表的数据部分不 会改变。 定义:Constant 常数名称:数据类型:=设置值 命名说明:1、第一个字母是英文字母 2、最后一个字符不可以为底线符号。 3、常数名称不可以与语法保留字相同。

vhdl基本语法 ifdef

vhdl基本语法 ifdef

VHDL(VHSIC Hardware Description Language)是一种硬件描述语言,用于描述数字电路和系统。

它是由美国国防部(DOD)于20世纪80年代初期的VHSIC(Very High Speed Integrated Circuits)项目中开发的。

VHDL的基本语法包括一些常用的关键字和结构,其中ifdef是其中之一。

ifdef是VHDL中的一个条件编译指令,用于根据给定的条件来判断是否包含某段代码。

在VHDL中,ifdef语句的基本语法如下:```vhdl`ifdef condition-- code to be included if condition is true`else-- code to be included if condition is false`endif```在上面的代码中,condition是一个条件表达式,可以是一个参数、宏定义或者其他已经定义过的条件。

如果condition为真,则会编译`ifdef和`endif之间的代码;如果condition为假,则会编译`else和`endif之间的代码。

使用ifdef可以让我们根据不同的条件来控制代码的编译和包含,这在一些复杂的项目中非常有用。

下面是使用ifdef的一个简单示例:```vhdl`ifdef DEBUGreport "Debug mode is enabled";`elsereport "Debug mode is disabled";`endif```在上面的代码中,如果DEBUG宏被定义了,那么report语句"Debug mode is enabled"将被编译进去;否则将编译进去"Debug mode is disabled"。

除了`ifdef语句外,VHDL还提供了一些其他的条件编译指令,如`ifndef(如果给定条件为假则编译)、`elsif(如果前面的条件为假则继续判断下一个条件)、`elsif和`endif。

VHDL语言的基本语法解析

VHDL语言的基本语法解析

2、变量( Variable )
变量是一个局部变量,它只能在进程语句、函数语句和过程 语句结构中使用,用作局部数据存储。变量常用在实现某种算 法的赋值语句中。
一般格式:
VARIABLE 变量名:数据类型 约束条件:= 表达式;
例: VARIABLE x, y: INTEGER;
--定义x,y为整数变量
SIGNAL Y ,Z :BIT;
Y<=A(M) ;
--M是不可计算型下标表示
Z<=B(3) ;
--3是可计算型下标表示
C (0 TO 3)<=A (4 TO 7) ; -以段的方式进行赋值
C (4 TO 7)<=A (0 TO 3) ; -以段的方式进行赋值
二、VHDL语言的数据对象
1、常数( Constant )
常量的使用范围取决于它被定义的位置 :
(1)程序包中定义的常量具有最大的全局化特性,可以用在调 用此程序包的所有设计实体中;
(2)设计实体中定义的常量,其有效范围为这个实体定义的所 有的结构体;
(3)设计实体中某一结构体中定义的常量只能用于此结构体;
(4)结构体中某一单元定义的常量,如一个进程中,这个常量 只能用在这一进程中。
5,678,0,156E2(=15600) , 45_234_287(=45234287)
(2) 实数文字: 实数文字也都是十进制的数,但必 须带有小数点,如: 188.993, 88_670_551.453_909(=88670551.453909) ,
1.0,44.99E-2(=0.4499) ,1.335,0.0
?16#F.01#E+2ቤተ መጻሕፍቲ ባይዱ--( 十六进制数表示,等于 3841.00)

关于vhdl语言中,下列对进程语句及语法规则

关于vhdl语言中,下列对进程语句及语法规则

一、进程语句在VHDL语言中,进程语句是描述数字电路行为的重要语法结构。

它可以用来描述数字电路的工作方式和行为模式,是VHDL语言中最基本的并发结构。

1. 进程语句的基本格式进程语句的基本格式如下:```vhdlprocess (sensitivity_list)begin-- 进程语句的具体实现end process;```其中,sensitivity_list表示进程敏感列表,可以是一个或多个信号。

当敏感列表中的信号发生变化时,进程将被激活,执行其中的语句。

2. 进程语句中的敏感信号在进程语句中,敏感信号是指当这些信号的值发生变化时,进程将被激活。

通常情况下,敏感信号是数字电路中的输入信号或者状态信号。

在进程语句中,可以通过在process语句的括号中列出敏感信号来指定敏感信号,如:```vhdlprocess (clk, rst)begin-- 进程语句的具体实现end process;```在这个例子中,当clk信号或者rst信号发生变化时,进程将被激活。

二、语法规则在VHDL语言中,进程语句遵循一定的语法规则,以确保正确、可读性强的代码编写。

1. 进程语句的begin和end在进程语句的begin和end之间,可以编写进程的具体实现。

在begin和end之间的代码将被视为进程语句的一部分,会在进程激活时执行。

2. 进程语句中的并发语句在进程语句的begin和end之间,可以包含一系列的并发语句,用来描述数字电路的行为。

这些并发语句可以是赋值语句、条件语句、循环语句等,用来描述数字电路的逻辑功能。

3. 进程语句的敏感信号在进程语句的括号中可以列出敏感信号,用来指定当这些信号发生变化时,进程将被激活。

敏感信号的正确使用是进程语句正确执行的重要保证。

通过对进程语句和语法规则的了解,可以更加深入地理解VHDL语言中数字电路的描述和实现方式。

合理的使用进程语句和遵循正确的语法规则,可以编写出高质量、可维护、易读的VHDL代码,提高数字电路设计的效率和质量。

VHDL语法简单情况总结

VHDL语法简单情况总结

VHDL语法简单总结一个VHDL程序代码包含实体(entity)、结构体(architecture)、配置(configuration)、程序包(package)、库(library)等。

一、数据类型1.用户自定义数据类型使用关键字TYPE,例如:TYPE my_integer IS RANGE -32 TO 32;–用户自定义的整数类型的子集TYPE student_grade IS RANGE 0 TO 100;–用户自定义的自然数类型的子集TYPE state IS (idle, forward, backward, stop);–枚举数据类型,常用于有限状态机的状态定义一般来说,枚举类型的数据自动按顺序依次编码。

2.子类型在原有已定义数据类型上加一些约束条件,可以定义该数据类型的子类型。

VHDL不允许不同类型的数据直接进行操作运算,而某个数据类型的子类型则可以和原有类型数据直接进行操作运算。

子类型定义使用SUBTYPE关键字。

3.数组(ARRAY)ARRAY是将相同数据类型的数据集合在一起形成的一种新的数据类型。

TYPE type_name IS ARRAY (specification) OF data_type;–定义新的数组类型语法结构SIGNAL signal_name: type_name [:= initial_value];–使用新的数组类型对SIGNAL,CONSTANT, VARIABLE进行声明例如:TYPE delay_lines IS ARRAY (L-2 DOWNTO 0) OF SIGNED (W_IN-1 DOWNTO 0);–滤波器输入延迟链类型定义TYPE coeffs IS ARRAY (L-1 DOWNTO 0) OF SIGNED (W_COEF-1 DOWNTO 0);–滤波器系数类型定义SIGNAL delay_regs: delay_lines; –信号延迟寄存器声明CONSTANT coef: coeffs := ( ); –常量系数声明并赋初值4.端口数组在定义电路的输入/输出端口时,有时需把端口定义为矢量阵列,而在ENTITY中不允许使用TYPE进行类型定义,所以必须在包集(PACKAGE)中根据端口的具体信号特征建立用户自定义的数据类型,该数据类型可以供包括ENTITY在内的整个设计使用。

VHDL语法简单总结

VHDL语法简单总结

VHDL语法(yǔfǎ)简单总结VHDL语法简单(jiǎndān)总结VHDL语法(yǔfǎ)简单总结一个(yī ɡè)VHDL程序代码包含实体(shítǐ)(entity)、结构(jiégòu)体(architecture)、配置(pèizhì)(configuration)、程序包(package)、库(library)等。

一、数据类型1.用户自定义数据类型使用关键字TYPE,例如:TYPE my_integer IS RANGE -32 TO 32;–用户自定义的整数类型的子集TYPE student_grade IS RANGE 0 TO 100;–用户自定义的自然数类型的子集TYPE state IS (idle, forward, backward, stop);–枚举数据类型,常用于有限状态机的状态定义一般来说,枚举类型的数据自动按顺序依次编码。

2.子类型在原有已定义数据类型(lèixíng)上加一些约束条件,可以定义(dìngyì)该数据类型的子类型。

VHDL不允许不同类型的数据直接进行操作运算,而某个数据类型的子类型则可以和原有类型数据直接进行操作运算。

子类型定义使用(shǐyòng)SUBTYPE关键字。

3.数组(ARRAY)ARRAY是将相同数据类型的数据集合在一起(yīqǐ)形成的一种新的数据类型。

TYPE type_name IS ARRAY (specification) OF data_type;–定义新的数组类型(lèixíng)语法结构SIGNAL signal_name: type_name [:=initial_value];–使用新的数组类型对SIGNAL,CONSTANT, VARIABLE进行声明例如:TYPE delay_lines IS ARRAY (L-2 DOWNTO 0) OF SIGNED (W_IN-1 DOWNTO 0);–滤波器输入延迟链类型定义TYPE coeffs IS ARRAY (L-1 DOWNTO 0) OF SIGNED (W_COEF-1 DOWNTO 0);–滤波器系数(xìshù)类型定义SIGNAL delay_regs: delay_lines; –信号延迟(yánchí)寄存器声明CONSTANT coef: coeffs := ( ); –常量(chángliàng)系数声明并赋初值4.端口数组在定义电路的输入/输出端口时,有时需把端口定义为矢量阵列,而在ENTITY中不允许使用TYPE进行类型定义,所以(suǒyǐ)必须在包集(PACKAGE)中根据端口的具体信号特征建立用户自定义的数据类型,该数据类型可以供包括ENTITY在内的整个设计使用。

VHDL数据类型vhdl语法

VHDL数据类型vhdl语法

二、VHDL数据类型与数据对象
例如: Signal Y : Std_logic_vector(7 downto 0); Signal X : Integer range 0 to 255; Y<= CONV_STD_LOGIC_VECTOR(X,8);
二、VHDL数据类型与数据对象 CONV_INTEGER 将数据类型 UNSIGNED, SIGNED转换为 INTEGER 类型. CONV_UNSIGNED 将数据类型INTEGER, SIGNED转换为 UNSIGNED 类型. CONV_SIGNED 将数据类型INTEGER, UNSIGNED转换为 SIGNED类型. CONV_STD_LOGIC_VECTOR 将数据类型INTEGER, UNSIGNED, SIGNED, STD_LOGIC转换为STD_LOGIC_VECTOR 类型.
二、VHDL数据类型与数据对象
library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; --必需定义+ entity exam1 is port (ip : in std_logic_vector(3 downto 0); op : out std_logic_vector(3 downto 0)); end exam1; architecture m1 of exam1 is constant num : integer := 6; begin op <= ip + num; end m1;
architecture bh1 of full_bit_adder is begin y <= ((not a)and (not b)and ci) or ((not a)and (b) and (not ci)) or ((a)and (not b)and(not ci)) or ((a) and (b)and(ci)); cout <= (b and ci) or (a and ci) or (a and b); end bh1;

第5章VHDL语言基础(续)

第5章VHDL语言基础(续)
[标号]:WHILE 条件 LOOP --顺序语句块;
END LOOP [标号];
… VARIABLE tmp: STD_LOGIC; VARIABLE i: INTEGER; … i := 0; WHILE ( i < 8 ) LOOP
tmp := tmp XOR a(i); i := i +1; END LOOP ;
5.2.1 IF语句
IF (条件) THEN 语句块;
[ELSIF (条件) THEN 语句块;]
[ELSE 语句块;]
END IF;
IF语句的三种形式(一)
IF (条件) THEN 语句块;
END IF;
IF (clk’event and clk = ‘1’) THEN Q <= D ;
END IF;
END a;
5.2 VHDL顺序语句
顺序语句(sequential)
VHDL 常用语句
并发语句(concurrent)
顺序语句:处于进程(process)的内部, 从仿真的角度看是顺序执行的,类似于C、 Basic等执行方式。
并发语句:处于进程(process)的外部, 是并行执行的,即与它们出现的先后次序 无关。
--顺序语句块; EDN LOOP;
语法格式(二):递增 FOR I IN 起始值 TO 结束值 LOOP
--顺序语句块; END LOOP;
ASUM: FOR I IN 1 to 9 LOOP Sum <= I + Sum; -- Sum初值为0 END LOOP ASUM;
WHILE … LOOP语句
END IF;
A
B C
E
D
2 s
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

上篇基础元素目录:数据类型数据对象运算符语句基本程序结构电路描述方式数据类型预定义类型bitbit_victorintegerstd_logicstd_logic_victor自定义类型枚举类型 type 新数据类型 is (元素1, 元素2, ...)例定义 type state_type is (s1, s2, s3. s4); -- 定义一个新类型state_type引用 signal state : state_type; -- 定义一个信号state,类型为state_type 数组类型 type 数组 is array (范围) of 数据类型;例定义 type byte is array (7 downto 0) of bit; -- 定义一个8bit的数组type word is array (31 downto 0) of bit; -- 定义一个32bit的数组数据对象端口声明端口 : in | out 数据类型; -- 端口在特性上等同于信号,但赋值在entity的port中赋值端口 <= 表达式;信号声明signal 信号 : 数据类型;赋值信号 <= 表达式;变量声明varable 变量 : 数据类型;赋值变量 := 表达式;常数声明常数 : 数据类型 := 数值;运算符算术运算 +, -, *并置运算 &关系运算 =, /=, <, <=, >, >=逻辑运算 and, or, not, nand, nor, xor, xnor语句并行语句⑴信号赋值语句简单信号赋值语句信号 <= 表达式;选择信号赋值语句 with 选择表达式 select信号 <= 表达式1 when 选择值1,表达式2 when 选择值2,......表达式n when others;条件信号赋值语句信号 <= 表达式1 when 条件关系式1 else表达式2 when 条件关系式2 else......表达式n when 条件n else表达式;⑵过程调用语句过程 (实参);⑶函数调用语句信号 <= 函数 (实参);⑷元件例化语句元件声明 component 元件实体 -- 将一个实体声明为元件 port (端口声明);end component;元件引用按位置引用标号 : 元件实体 port map (连接端口1, 连接端口2, ...);按名称引用标号 : 元件实体 port map (元件端口1 >= 连接端口1, 元件端口2 >= 连接端口2, ...);⑸生成语句格式 1 [标号:] for 循环变量 in 取值范围 generate声明语句,begin并行语句,end generate [标号];取值范围: 表达式 to 表达式; -- 递增方式,如1 to 5表达式 downto 表达式 ; -- 递减方式,如5 downto 1格式 2 [标号:] if 条件关系式 generate声明语句;begin并行语句,end generate [标号] ,⑹块语句块标号: block [(保护条件)]接口声明;类属声明;begin并行语句; -- 被保护的变量前需加上保留字guarded end block 块标号;带保护的块语句举例: entity latch isport( d, clk : in bit;q, qb : out bit);end latch;achetectire latch_guard of latch isbeginb1 : block(clk = 1)beginq <= guarded d after 5 ns;qb <= guarded not(d) after 7 ns;end block b1;end latch_guard⑺进程语句 [标号:] process (敏感信号)[声明语句;] --常量,变量,信号 begin顺序语句;end process [标号:];顺序语句⑴ 赋值语句 -- 在进程中信号 <= 表达式;变量 := 表达式;⑵ 流程控制语句if语句格式 1: if 条件关系式 then顺序语句;end if;格式 2: if 条件关系式 then顺序语句;else顺序语句;end if;格式 3: if 条件关系式1 then顺序语句;elsif 条件关系式2 then顺序语句;......else顺序语句;end if;case 语句 -- case 语句中,条件值有3种形式:值,值1 | 值2 |...| 值n,值 TO 值 -- 最后一行的顺序语句若为null,则有意引入锁存器case 条件表达式 iswhen 条件值 => 顺序语句;......when others => 顺序语句;end case;for_loop 语句 [标号]:for 循环变量 in 值 to 值 loop;顺序语句;end loop [标号];时钟边沿描述上升沿时钟event and时钟 = 1| rising_edge (时钟)下降沿时钟event and时钟 =0| falling_edge (时钟)程序基本结构-- 主程序与元件程序在同一文件中,library ieee;use 主程序entity 实体名 is --实体名必须与文件名相同port (端口声明;);end entity work1;architecture struc of work1 is[声明语句;] --常量,变量,信号,元件,函数等begin并行语句;end architecture struc;电路描述方式行为描述方式以用状态机描述电路为典型数据流 ( 寄存器 ) 描述方式即用逻辑表达式描述电路结构描述方式以用元件复用的方式描述电路为典型下篇复合元素和状态机目录元件 ---------- 1 单文件元件2 多文件元件函数 ---------- 3 单文件函数4 多文件函数过程 ---------- 5 单文件过程6 多文件过程moorl 状态机 -- 7 二进程moorl状态机8 三进程moorl状态机meaky 状态机 -- 9 二进程mealy状态机10 三进程mealy状态机状态机实例 ---- 11 交通灯之一12 交通灯之二附录 ---------- 13 状态转移图14 用户库的格式和用法单文件元件-- 主程序与元件程序在同一文件中,library ieee;use 主程序entity work1 isport ( r,s,t,u : in std_logic;v : out std_logic);end entity work1;architecture struc of work1 iscomponent ym -- 将实体ym声明为元件 port ( a,b : in std_logic;c : out std_logic);end component ym;component hm -- 将实体hm声明为元件 port ( a,b : in std_logic;c : out std_logic);end component hm;signal temp1,temp2 : std_logic;begin u1 : ym port map ( r, s, temp1 ); -- 元件例化 u2 : ym port map ( t, u, temp2 );u3 : hm port map ( temp1, temp2, v );end architecture struc;-- ym元件实体定义程序library ieee;use ym isport ( a,b : in std_logic;c : out std_logic);end entity ym;architecture ym1 of ym isbeginc <= a and b;end architecture ym1;-- hm元件实体定义程序library ieee;use hm isport ( a,b : in std_logic;c : out std_logic);end entity hm;architecture hm1 of hm isbeginc <= a or b;end architecture hm1;多文件元件-- 主程序文件和定义元件的程序文件都要添加到工程中-- 主程序文件,不需要...声明用户库文件library ieee;use zhu_map isport ( r,s,t,u : in std_logic;v : out std_logic);end entity zhu_map;architecture niu of zhu_map iscomponent ymport ( a,b : in std_logic;c : out std_logic);end component ym;component hmport ( a,b : in std_logic;c : out std_logic);end component hm;signal temp1,temp2 : std_logic;beginu1 : ym port map ( r, s, temp1 ); -- 元件例化 u2 : ym port map ( t, u, temp2 );u3 : hm port map ( temp1, temp2, v );end architecture niu;-- 定义元件实体的程序文件-- ym元件实体定义程序library ieee;use ym isport ( a,b : in std_logic;c : out std_logic);end entity ym;architecture ym1 of ym isbeginc <= a and b;end architecture ym1;-- hm元件实体定义程序library ieee;use hm isport ( a,b : in std_logic;c : out std_logic);end entity hm;architecture hm1 of hm isbeginc <= a or b;end architecture hm1;单文件函数library ieee;use func isport ( din1,din2 : in std_logic_vector( 0 to 3 );dout : out std_logic_vector( 0 to 3 ));end entity;architecture a of func is-- 定义函数function ls_xj ( d1, d2 : in std_logic_vector( 0 to 3 )) return std_logic_vector is variable temp : std_logic_vector( 0 to 3 );begintemp := d1 + d2;return temp;end function;-- 定义函数结束begindout <= ls_xj ( din1, din2 ); --调用函数end architecture;多文件函数-- 主程序文件和定义函数的程序文件都要添加到工程中-- 主程序文件,必须声明用户库文件library ieee;use -- 作为用户库entity zhu_func isport ( din1,din2 : in std_logic_vector( 0 to 3 );dout : out std_logic_vector( 0 to 3 ) );end;architecture niu of zhu_func isbegindout <= ls_xj ( din1, din2 ); -- 调用函数end;-- 定义函数的文件library ieee;use use_func is -- 声明function ls_xj ( d1, d2: in std_logic_vector( 0 to 3 ) ) return std_logic_vector;end use_func;package body use_func is -- 程序体function ls_xj ( d1, d2 : in std_logic_vector( 0 to 3 )) return std_logic_vector is variable temp : std_logic_vector( 0 to 3 );begintemp := d1 and d2;return temp;end function;end use_func;单文件过程library ieee;use call_proce isport ( d1 : in integer range 0 to 31;d2 : in integer range 0 to 31;fout : out integer range 0 to 31 );end;architecture a of call_proce is-- 过程定义procedure jfq ( din1, din2 : in integer range 0 to 31;dout : out integer range 0 to 31 ) isbegindout := din1 + din2;end;-- 过程定义结束beginprocess ( d1, d2 )variable fo : integer range 0 to 31;beginjfq ( d1, d2, fo ); -- 调用过程fout <= fo;end process;end;多文件过程-- 主程序文件和定义过程的程序文件都要添加到工程中-- 主程序文件,必须声明用户库文件library ieee;use -- 作为用户库entity zhu_proc isport ( d1, d2 : in integer range 0 to 31;fout : out integer range 0 to 31);end;architecture niu of zhu_proc isbeginprocess ( d1, d2 )variable fo : integer range 0 to 31;beginjfq ( d1, d2, fo ); -- 调用过程fout <= fo;end process;end;-- 定义过程的文件library ieee;use use_proc is -- 声明procedure jfq ( din1 : in integer range 0 to 31;din2 : in integer range 0 to 31;dout : out integer range 0 to 31 );end use_proc;package body use_proc is -- 程序体procedure jfq ( din1, din2 : in integer range 0 to 31;dout : out integer range 0 to 31 ) isbegindout := din1 + din2;end jfq;end use_proc;二进程moorl状态机library ieee;use moorl_1 isport ( reset : in std_logic;clock : in std_logic;din : in std_logic;dout : out std_logic_vector ( 2 downto 0 ) );end entity;architecture statemachine of moorl_1 istype state_type is ( s0, s1, s2, s3 );signal state : state_type;beginprocess( reset, clock ) -- 变换状态begin if reset = '1' thenstate <= s0;elsif rising_edge( clock ) thencase state iswhen s0 => if din = '1' thenstate <= s1;end if;when s1 => if din = '1' thenstate <= s2;end if;when s2 => if din = '1' thenstate <= s3;end if;when s3 => if din = '1' thenstate <= s0;elsestate <= s1;end if;end case;end if;end process;process( state ) -- 输出begincase state iswhen s0 => dout <= "001";when s1 => dout <= "011";when s2 => dout <= "101";when s3 => dout <= "111";end case;end process;end;三进程moorl状态机library ieee;use moorl_2 isport ( reset : in std_logic;clock : in std_logic;din : in std_logic;dout : out std_logic_vector( 2 downto 0 ) );end entity;architecture statemachine of moorl_2 istype state_type is ( s0, s1, s2, s3 );signal presentstate : state_type;signal nextstate : state_type;beginprocess ( reset, clock ) -- 更新当前状态 beginif reset = '1' thenpresentstate <= s0;elsif rising_edge ( clock ) thenpresentstate <= nextstate;end if;end process;process ( presentstate, din ) -- 生成下一个状态begincase presentstate iswhen s0 => if din = '1' thennextstate <= s1;elsenextstate <= s0;end if;--dout <= "001";when s1 => if din = '1' thennextstate <= s2;elsenextstate <= s1;end if;--dout <= "011";when s2 => if din = '1' thennextstate <= s3;elsenextstate <= s2;end if;--dout <= "101";when s3 => if din = '1' thennextstate <= s0;elsenextstate <= s1;--dout <= "111";end if;end case;end process;process ( presentstate ) -- 输出begincase presentstate iswhen s0 => dout <= "001";when s1 => dout <= "011";when s2 => dout <= "101";when s3 => dout <= "111";end case;end process;end;二进程mealy状态机library ieee;use mealy_1 isport ( reset : in std_logic;clock : in std_logic;din : in std_logic;dout : out std_logic_vector ( 2 downto 0 ) );end entity;architecture statemachine of mealy_1 istype state_type is ( s0, s1, s2, s3 );signal state : state_type;beginprocess ( reset, clock ) -- 变换状态beginif reset = '1' thenstate <= s0;elsif rising_edge ( clock ) thencase state iswhen s0 => if din = '1' then state <= s1;end if;when s1 => if din = '1' then state <= s2;end if;when s2 => if din = '1' then state <= s3;end if;when s3 => if din = '1' then state <= s0;elsestate <= s1;end if;end case;end if;end process;process ( state, din ) -- 输出 begincase state iswhen s0 => if din='0' thendout <="000";elsedout <="001";end if;when s1 => if din='0' thendout <="010";elsedout <="011";end if;when s2 => if din='0' thendout <="100";elsedout <="101";end if;when s3 => if din='0' thendout <="110";else dout <="111";end if;end case;end process;end architecture;三进程mealy状态机library ieee;use mealy_2 isport ( reset : in std_logic;clock : in std_logic;din : in std_logic;dout : out std_logic_vector( 2 downto 0 ) );end entity;architecture statemachine of mealy_2 istype state_type is ( s0, s1, s2, s3 );signal presentstate : state_type;signal nextstate : state_type;beginprocess ( reset, clock ) -- 更新当前状态 beginif reset = '1' thenpresentstate <= s0;elsif rising_edge ( clock ) thenpresentstate <= nextstate;end if;end process;process ( presentstate, din ) -- 生成次态 begincase presentstate iswhen s0 => if din ='1' thennextstate <= s1;elsenextstate <= s0;end if;when s1 => if din ='1' thennextstate <= s2;elsenextstate <= s1;end if;when s2 => if din ='1' thennextstate <= s3;elsenextstate <= s2;end if;when s3 => if din = '1' thennextstate <= s0;elsenextstate <= s1;end if;end case;end process;process ( presentstate, din ) -- 输出 begincase presentstate iswhen s0 => if din = '0' thendout <= "000";elsedout <= "001";end if;when s1 => if din = '0' thendout <= "010";elsedout <= "011";end if;when s2 => if din = '0' thendout <= "100";elsedout <= "101";end if;when s3 => if din = '0' thendout <= "110";elsedout <= "111";end if;end case;end process;end;用状态机设计交通灯之一-- 这是一个简单的状态机设计实例,根据时钟变换路口-- 的红,绿,黄三个信号灯,从程序中可以看出,时钟-- 的周期至少应为,例如30秒。

相关文档
最新文档