这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » 软件与操作系统 » 状态机分享(转帖)

共12条 1/2 1 2 跳转至

状态机分享(转帖)

工程师
2008-03-19 09:38:27     打赏
对EDA来说,状态机是一个非常重要的部分.如果能灵活地运用状态机,那么写程序就可以游刃有余了!

状态机一般分为三种类型:
Moore型状态机:次态=f(现状,输入),输出=f(现状);
Mealy型状态机:次态=f(现状,输入),输出=f(现状,输入);
混合型状态机。

Moore型与Mealy型两种状态机的不同点在于,Moore型状态机的输出信号是直接由状态寄存器译码得到,而Mealy型状态机则是以现时的输入信号结合即将变成次态的现态,编码成输出信号。

以下是我收藏的一些程序,让大家更深刻地了解!
莫尔型状态机
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;

architecture archmoore2 of moore2 is
        signal state: std_logic_vector(2 downto 0);
-- State assignment is such that 2 LSBs are outputs
constant state0: std_logic_vector(2 downto 0) := "000";
constant state1: std_logic_vector(2 downto 0) := "010";
constant state2: std_logic_vector(2 downto 0) := "011";
constant state3: std_logic_vector(2 downto 0) := "110";
constant state4: std_logic_vector(2 downto 0) := "111";
begin
moore: process (clk, rst) 
        begin
                if rst='1' then 
                        state <= state0;
                elsif (clk'event and clk='1') then
                        case state is

                                when state0 =>
                                        if id = x"3" then
                                                state <= state1;
                                        else
                                                state <= state0;
                                        end if;
                                when state1 =>
                                        state <= state2;
                                when state2 =>
                                        if id = x"7" then
                                                state <= state3;
                                        else
                                                state <= state2;
                                        end if;
                                when state3 =>
                                        if id < x"7" then 
                                                state <= state0;
                                        elsif id = x"9" then
                                                state <= state4;
                                        else
                                                state <= state3;
                                        end if;
                                when state4 =>
                                        if id = x"b" then
                                                state <= state0;
                                        else
                                                state <= state4;
                                        end if;
                                when others =>
                                        state <= state0;
                        end case;
                end if;
        end process;

--assign state outputs (equal to state std_logics)

y <= state(1 downto 0);
end archmoore2;

 

library ieee;
use ieee.std_logic_1164.all;

entity moore1 is port(
        clk, rst:       in std_logic;
        id:             in std_logic_vector(3 downto 0);
        y:              out std_logic_vector(1 downto 0));
end moore1;

architecture archmoore1 of moore1 is
        type states is (state0, state1, state2, state3, state4);
        signal state: states;
begin
moore: process (clk, rst)  --this process defines the next state only
        begin
                if rst='1' then 
                        state <= state0;
                elsif (clk'event and clk='1') then
                        case state is
                                when state0 =>
                                        if id = x"3" then
                                                state <= state1;
                                        else
                                                state <= state0;
                                        end if;
                                when state1 =>
                                        state <= state2;
                                when state2 =>
                                        if id = x"7" then
                                                state <= state3;
                                        else
                                                state <= state2;
                                        end if;
                                when state3 =>
                                        if id < x"7" then 
                                                state <= state0;
                                        elsif id = x"9" then
                                                state <= state4;
                                        else
                                                state <= state3;
                                        end if;
                                when state4 =>
                                        if id = x"b" then
                                                state <= state0;
                                        else
                                                state <= state4;
                                        end if;
                        end case;
                end if;
        end process;

--assign state outputs concurrently;
y <= "00" when (state=state0) else
     "10" when (state=state1 or state=state3) else
     "11";
end archmoore1;




关键词: 状态机     分享     转帖     logic     vector     st    

工程师
2008-04-17 17:27:56     打赏
2楼
好孤单的帖子

工程师
2008-04-25 17:13:19     打赏
3楼
沙发是我 板凳也是我

菜鸟
2008-05-04 16:43:21     打赏
4楼
楼主能否解释一下,或者加点注释什么的
不太明白呢

菜鸟
2008-05-06 14:52:43     打赏
5楼

用的是硬件描述语言吧?


菜鸟
2008-05-21 11:44:20     打赏
6楼
状态机,不是很明白

菜鸟
2008-06-27 14:21:48     打赏
7楼
与 deer_yl 兄的想法差不多,希望楼主说明您的开发环境,使用的工具。使大家可以分享您的成果或收藏。

菜鸟
2008-07-11 18:11:30     打赏
8楼


菜鸟
2010-05-29 19:25:33     打赏
9楼

迷茫


菜鸟
2010-05-30 11:40:57     打赏
10楼
最近写的代码就有这个~

共12条 1/2 1 2 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]