VHDL语言状态机汇总课件.ppt

上传人:牧羊曲112 文档编号:1566407 上传时间:2022-12-06 格式:PPT 页数:72 大小:1.30MB
返回 下载 相关 举报
VHDL语言状态机汇总课件.ppt_第1页
第1页 / 共72页
VHDL语言状态机汇总课件.ppt_第2页
第2页 / 共72页
VHDL语言状态机汇总课件.ppt_第3页
第3页 / 共72页
VHDL语言状态机汇总课件.ppt_第4页
第4页 / 共72页
VHDL语言状态机汇总课件.ppt_第5页
第5页 / 共72页
点击查看更多>>
资源描述

《VHDL语言状态机汇总课件.ppt》由会员分享,可在线阅读,更多相关《VHDL语言状态机汇总课件.ppt(72页珍藏版)》请在三一办公上搜索。

1、第八章有限状态机FSM的设计 哈尔滨工业大学(威海) 信息工程学院电子工程系,第八章 有限状态机FSM的设计,1.状态机概念2.一般状态机3.摩尔状态机4.米利状态机5.状态机实例,一 .有限状态机的基本概念,有限状态机:指那些输出取决于过去输入部分和当前输入部分的时序逻辑电路。有限状态机一般有三部分:输入部分、输出部分和状态寄存器。Moore型有限状态机:输出信号仅与当前状态有关Mealy型有限状态机:输出信号不仅与当前状态有关,而且还与所有的输入信号有关。,状态机的特点,克服了纯硬件数字系统顺序方式控制不灵活的缺点。VHDL综合器易于优化易构成性能良好的时序逻辑模块结构模式简单、层次分明、

2、易读易懂、易排错利用同步时序和全局时钟线可实现高速FSM运行模式类似于CPU,易于进行顺序控制高可靠性,非法状态易控制,时序电路的结构与特点,1 状态机分类,根据输出函数的形式,可以分为moore机和mealy机两类。,一般状态机MOORE状态机MEALY状态机,1 状态机分类,FSM的表达形式,通常采用状态转换图表达电路信号的变化:,Moore: y = f(s) Mealy: y = f(s0,x),1 状态机分类,Moore型有限状态机的结构图,Mealy型有限状态机的结构图,FSM的设计要点,定义枚举类型表达不同的状态特点;设置信号表达现有状态和转换的状态;对每一个现态,利用选择语句,

3、根据控制条件x决定转换次态; 根据现态和x决定输出y。,1 状态机分类,状态机的基本操作,状态机内部状态转换。状态机的下一个状态由译码器根据当前状态和输入条件决定。产生输出信号序列。输出信号由输出译码器根据当前状态和输入条件决定。,1 状态机分类,为了能获得可综合的,高效的vhdl状态描述,建议使用枚举数据类型来定义状态机的状态,并使用多进程方式来描述状态机的内部逻辑。,一般情况下,一个进程描述时序逻辑,包括状态寄存器的工作和寄存器状态的输出。另一个进程描述组合逻辑,包括进程间状态值的传递逻辑以及状态以及状态转换值的输出。必要时还可以引入第三个进程完成其他的逻辑功能。,2.一般状态机,状态机的

4、编码方式,Binary、gray-code编码使用最少的触发器,较多的组合逻辑资源,而one-hot编码反之。 另一方面,对于小型设计使用Binary、gray-code更有效,而大型状态机使用one-hot编码更高效。,使用两段式状态机设计方法 设计状态机的方法多种多样,但总结起来有两大类:第一种,将状态转移和状态的操作、判断等写在一起;另一种是将状态转移单独写成一个部分,将状态的操作和判断写到另一个部分中。 第二种设计方法较好,将同步时序和组合逻辑分别放到不同的程序块中实现不仅仅便于阅读、理解、维护,更有利于综合器优化代码,利于用户添加合适的时序约束条件,利于布局布线器实现设计。,一般状态

5、机实例,library IEEE;use IEEE.STD_LOGIC_1164.all;entity s_machine is port( clk : in STD_LOGIC; rst : in STD_LOGIC; state_input : in STD_LOGIC_VECTOR(1 downto 0); comb_output : out STD_LOGIC_VECTOR(1 downto 0) );end s_machine;architecture s_machine of s_machine istype states is (st0,st1,st2,st3);signal c

6、ur_state,next_state : states;,2.一般状态机,beginprocess(clk,rst)begin if rst=1 then cur_statecomb_outputcomb_output=01; if state_input=00 then,next_statecomb_outputcomb_output=11; if state_input=11 then next_state=st3; else next_state=st0; end if; end case; end process;end s_machine;,111序列检测器,例:111序列检测器的

7、功能是连续输入三个或三个以上的1时,电路输出为1,其余情况下输出为0。,s0:初始状态,电路还未收到一个有效1 s1:收到一个1后的状态S2:连续收到两个1后的状态 s3:连续收到三个1个后的状态,111序列检测器程序,library IEEE;use IEEE.STD_LOGIC_1164.all;entity s_machine is port( clk : in STD_LOGIC; rst : in STD_LOGIC; din : in STD_LOGIC; comb_output : out STD_LOGIC );end s_machine;architecture s_mach

8、ine of s_machine istype states is (st0,st1,st2,st3);signal cur_state,next_state : states;,beginprocess(clk,rst)begin if rst=1 then cur_statecomb_outputcomb_output=0; if din=1 then,next_state if din=1 then next_state if state_input=1 then next_state=st3; comb_output=1; else next_state=st0; comb_outpu

9、t=0; end if; end case; end process;end s_machine;,3.摩尔状态机,Moore状态机的输出只与有限状态自动机的当前状态有关,与输入信号的当前值无关。 Moore有限状态机在时钟CLOCK脉冲的有效边沿后的有限个门延后,输出达到稳定值。即使在一个时钟周期内输入信号发生变化,输出也会在一个完整的时钟周期内保持稳定值而不变。输入对输出的影响要到下一个时钟周期才能反映出来。,Moore有限状态机最重要的特点就是将输入与输出信号隔离开来。,Moore机设计:例1,例1 简单的Moore状态机设计状态转换图如下所示,要求设置reset控制,能直接使状态处于s

10、0。,3.摩尔状态机,library ieee; use ieee.std_logic_1164.all;entity statmach is port(clk,input,reset: in std_logic; output: out std_logic);end statmach;,Moore机设计:例1,3.摩尔状态机,architecture beh of statmach is type state_type is (s0,s1); -采用枚举法设置状态 signal st: state_type; -表达当前的状态begin process(clk) -设置时钟控制模块 begi

11、n if reset=1 then st=s0;-异步复位 elsif clkevent and clk=1 then -边沿检测,Moore机设计:例1,3.摩尔状态机,case st is -根据现态决定下一状态 when s0 = st if input=1 then st=s0; end if; end case; end if; end process; output=1 when st=s1 else0; -根据状态决定输出end beh;,Moore机设计:例1,3.摩尔状态机,例2 Moore状态机的设计该状态机有5个状态,转换图如下所示:其中输入控制ID为4位二进制数,在图中

12、表达为16进制数;,Moore机设计:例2,3.摩尔状态机,library ieee;use ieee.std_logic_1164.all;entity moore2 is port(clk,rst: in std_logic;id: in std_logic_vector(3 downto 0);y: out std_logic_vector(1 downto 0);end moore2;,Moore机设计:例2,3.摩尔状态机,architecture beh of moore2 is signal st: std_logic_vector(2 downto 0);-状态表达 -各状态命

13、名并根据输出的特点进行赋值 constant s0:std_logic_vector(2 downto 0):=000; constant s1:std_logic_vector(2 downto 0):=010; constant s2:std_logic_vector(2 downto 0):=011; constant s3:std_logic_vector(2 downto 0):=110; constant s4:std_logic_vector(2 downto 0):=111;begin,Moore机设计:例2,rocess(clk,rst) -状态转移关系begin if rs

14、t=1 then st if id=x3 then st st if id=x7 then st=s3; end if;,Moore机设计:例2,3.摩尔状态机,when s3= if id=x7 then st if id=xb then stst=s0; end case; end if; end process;y=st(1 downto 0); -输出方程end beh;,Moore机设计:例2,3.摩尔状态机,例3 简单状态机设计,初始态:z=0连续2个触发沿A=1,则z=1;若z=1且b=1, 则z保持1。,Moore机设计:例3,3.摩尔状态机,例3 简单状态机设计,Moore机

15、设计:例3,3.摩尔状态机,architecture beh of smexamp is type sreg_type is (init,a0,a1,ok0,ok1); signal sreg:sreg_type;begin process(clk) begin if clkevent and clk=1 then case sreg is when init = if a=0 then sreg=a0; elsif a=1 then sreg=a1; end if;,Moore机设计:例3,when a0 = if a=0 then sreg if a=0 then sreg if a=0

16、then sreg=ok0; elsif a=1 and b=0 then sreg=a1; elsif a=1 and b=1 then sreg=ok1; end if;,Moore机设计:例3,when ok1 = if a=0 and b=0 then sregsreg=init; end case; end if; end process;,Moore机设计:例3,with sreg select -根据状态决定输出 z= 0 when init|a0|a1, 1 when ok0|ok1, 0 when others;end beh;,综合结果如下:,Moore机设计:例3,通过对

17、状态图的分析,可以简化:,使用1个寄存器专门存放触发时的输入a;则状态可以减少到3个;结构也简化了。,Moore机设计:例4,architecture beh of smexamp istype sreg_type is (init,looking,ok);signal sreg:sreg_type;signal lasta:std_logic;begin process(clk) begin if clkevent and clk=1 then lasta=a;,Moore机设计:例4,case sreg is when init = sreg if a=lasta then sreg if

18、 b=1 then sregsreg=init;end case;,Moore机设计:例4,end if; end process; with sreg select z= 1 when ok, 0 when others;end beh;,Moore机设计:例4,Mealy状态机与Moore有限状态机不同,Mealy有限状态机的输出不单与当前状态有关,而且与输入信号的当前值有关。,Mealy有限状态机的输出直接受输入信号的当前值影响,而输入信号可能在一个时钟周期内任意时刻变化,这使得Mealy有限状态机对输入的响应发生在当前时钟周期,比Moore有限状态机对输入信号的响应要早一个周期。因此,

19、输入信号的噪声可能影响在输出的信号。,4.米利状态机,FSM的设计:Mealy机,设计要点:用输入和状态控制进程;用case语句分别选择每一个状态;用if语句确定输入条件,指定相应的下一状态和输出值;输出立即赋值(使用一个进程);状态等待时钟条件满足再进行赋值(使用另一个进程);,4.米利状态机,例 Mealy状态机设计该状态机具有4个状态,输入x,输出z;状态转换图如下所示;,Mealy机设计:例1,4.米利状态机,library ieee;use ieee.std_logic_1164.all;entity mealy is port(x,clk: in std_logic; z: out

20、 std_logic);end mealy;architecture beh of mealy is type state is (s0,s1,s2,s3);-设置状态类型 signal current_state,next_state: state; -现态与次态begin,Mealy机设计:例1,4.米利状态机,aaa:process(current_state,x) -决定次态和输出 begin case current_state is when s0= if x=0 then z if x=0 then z=0;next_state=s0; else z=0;next_state=s

21、2; end if;,Mealy机设计:例1,when s2= if x=0 then z if x=0 then z=0;next_state=s3; else z=1;next_state=s1; end if; end case; end process aaa; -这个进程与时钟无关,Mealy机设计:例1,4.米利状态机,sync:process -将现态转移为次态 begin wait until clkevent and clk=1; current_state=next_state; end process sync; -这个进程受时钟控制end beh;,Mealy机设计:例

22、1,4.米利状态机,4 状态机实例,4.米利状态机,控制器程序,library IEEE;use IEEE.STD_LOGIC_1164.all;use ieee.std_logic_unsigned.all;entity adtosram is port( clk : in STD_LOGIC; -工作时钟 rst : in STD_LOGIC; -复位 eoc : in STD_LOGIC; -转换结束标志 din : in STD_LOGIC_VECTOR(7 downto 0); oe : out STD_LOGIC; start : out STD_LOGIC; ale : out

23、STD_LOGIC;-地址锁存允许 cs : out STD_LOGIC; rd : out STD_LOGIC; wr : out STD_LOGIC; adda : out STD_LOGIC; -AD地址线最低位 ram_din : out STD_LOGIC_VECTOR(7 downto 0); address : out STD_LOGIC_VECTOR(12 downto 0) );end adtosram;,architecture adtosram of adtosram istype ad_states is (st0,st1,st2,st3,st4,st5,st6,st7

24、);type writ_states is (start_write,write1,write2,write3,write_end);signal ram_current_state,ram_next_state:writ_states;signal adc_current_state,adc_next_state:ad_states;signal adc_end:std_logic; signal lock: std_logic; -转换后数据输出锁存信号signal enable:std_logic; -ad转换允许信号,高电平有效signal addres_plus:std_logic;

25、-sram地址加1时钟信号signal adc_data:std_logic_vector(7 downto 0);signal addres_cnt:std_logic_vector(12 downto 0);beginadda=1; -选择通道1rd=1; - 禁止写sramadc:process(adc_current_state,eoc,enable) ad转换组合电路进程beginif rst=1 then adc_next_state=st0;elsecase adc_current_state is,when st0=alealealealeale=0;start=0; oe=0

26、;lock=0; adc_end=0;if eoc=1 then adc_next_state=st5;-转化结束,转下一状态,else adc_next_statealealealeadc_next_state=st0;end case; end if;end process adc;,ad_state:process(clk) -ad转换时序进程beginif clkevent and clk=1 thenadc_current_state=adc_next_state;end if;end process ad_state;data_lock:process(lock)beginif l

27、ock=1 and lockevent thenadc_data=din;end if;end process data_lock;writ_state:process(clk,rst) -sram数据写入控制 begin if rst=1 then ram_current_state=start_write; elsif clkevent and clk=1 then ram_current_state=ram_next_state;end if;end process writ_state;,ram_write:process(ram_current_state,adc_end)begin

28、case ram_current_state is when start_write=cscscs=1; wr=1; -打开sram片选信号enable=0; addres_plus=0; -禁止ad转换address=addres_cnt; -输出13位地址,ram_dincscs=1; wr=1;enable=1; -打开ad转换允许开关addres_plus=0; -地址加1时钟脉冲结束ram_next_state=start_write;end case;end process ram_write;counter:process(addres_plus) -地址计数器加1进程begin

29、if rst=1 then addres_cnt=0000000000000;elsif addres_plusevent and addres_plus=1 thenaddres_cnt=addres_cnt+1;end if;end process counter; end adtosram;,ADC0809采集控制模块的设计,5.状态机实例,ADC0809功能简介,ADC0809是CMOS的8位A/D转换器,片内有8路模拟开关,可控制8个模拟量中的一个进入转换器中。ADC0809的分辨率为8位,转换时间约100 s,含锁存控制的8路多路开关,输出由三态缓冲器控制,单5 V电源供电。,AD

30、C0809数据采集电路图,ADC0809的管脚及主要控制信号时序图,ADC0809采集控制状态图,源码实现,LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;ENTITY ADCINT IS PORT ( D : IN STD_LOGIC_VECTOR(7 DOWNTO 0); -0809的8位转换数据输出 CLK ,EOC : IN STD_LOGIC; -CLK是转换工作时钟 LOCK1, ALE, START, OE, ADDA : OUT STD_LOGIC; Q : OUT STD_LOGIC_VECTOR(7 DOWNTO 0) );END ADCI

31、NT;,ADC0809采集控制状态图,源码实现,ARCHITECTURE behav OF ADCINT ISTYPE states IS (st0, st1, st2, st3,st4,st5,st6) ; -定义各状态子类型 SIGNAL current_state, next_state: states :=st0 ; SIGNAL REGL : STD_LOGIC_VECTOR(7 DOWNTO 0); SIGNAL LOCK : STD_LOGIC; - 转换后数据输出锁存时钟信号 BEGIN ADDA = 1; LOCK1 =LOCK; PRO: PROCESS(current_s

32、tate,EOC) BEGIN -规定各状态转换方式,ADC0809采集控制状态图,源码实现,CASE current_state IS WHEN st0 = ALE ALE ALE ALE=0;START=0;OE=0;LOCK=0; IF (EOC=1) THEN next_state = st3; -测试EOC的下降沿 ELSE next_state = st4; END IF ;,ADC0809采集控制状态图,源码实现,WHEN st4= ALE ALE ALE ALE=0;START=0;OE=0;LOCK=0;next_state = st0; END CASE ; END PRO

33、CESS PRO ;,ADC0809采集控制状态图,源码实现,PROCESS (CLK) BEGIN IF ( CLKEVENT AND CLK=1) THEN current_state = next_state; - 在时钟上升沿,转换至下一状态 END IF; END PROCESS; - 由信号current_state将当前状态值带出此进程,进入进程PRO PROCESS (LOCK) - 此进程中,在LOCK的上升沿,将转换好的数据锁入 BEGIN IF LOCK=1 AND LOCKEVENT THEN REGL = D ; END IF; END PROCESS ; Q = R

34、EGL;END behav;,ADC0809采集控制状态图,交通灯控制器,St0St1St2St3st0,延时,library ieee;use ieee.std_logic_1164.all;entity traffic1 is port(clk,reset:in std_logic; red1,yellow1,green1,red2,yellow2,green2:out std_logic);end traffic1;architecture rtl of traffic1 is type state_t is (st0,st1,st2,st3,st4,st5,st6,st7); sign

35、al state,nextstate:state_t;begin process(reset,clk) begin if reset=1 then state=st0; elsif clkevent and clk=1 then state=nextstate; end if; end process;,process(state)begin case state is when st0=red1red1red1red1red1=1;yellow1=0;green1=0; red2=0;yellow2=0;green2=1; nextstate=st5;,when st5=red1red1red1=1;yellow1=0;green1=0; red2=0;yellow2=1;green2=0; nextstate=st0;end case; end process;end rtl;,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号