ステートマシン / ムーア型 / "abc"の順に文字が入力されたらフラグを立てる

pp.407-409

ムーア(Moore)型のFSM (有限ステートマシン)は、その出力が現在の状態にのみ依存するFSMのこと(p.68)。

"abc" (0x61, 0x62, 0x63)の順に文字が入力されたらフラグを立てる(入力をセットして、クロック、入力をセットして、クロック、......)。

library ieee;
use ieee.std_logic_1164.all;

entity string_detector is
    port(
        clk, rst: in  std_logic;
        x       : in  character;
        y       : out std_logic
    );
end entity;

architecture rtl of string_detector is
    type state_type is (idle, char1, char2, char3); -- 列挙型を宣言。
    signal pr_state, nx_state: state_type;
begin
    
   -- 現在の状態pr_stateを格納するためのレジスタ
    process(clk, rst)
    begin
        if    rst              then pr_state <= idle;
        elsif rising_edge(clk) then pr_state <= nx_state;
        end if;
    end process;
    
   -- 次の状態nx_stateを決定するための論理回路
    process(all)
    begin
        case pr_state is
            when idle  =>
                if    x = 'a' then nx_state <= char1;
                else               nx_state <= idle;
                end if;
            when char1 =>
                if    x = 'b' then nx_state <= char2;
                elsif x/= 'a' then nx_state <= idle;
                else               nx_state <= char1;
                end if;
            when char2 =>
                if    x = 'c' then nx_state <= char3;
                elsif x = 'a' then nx_state <= char1;
                else               nx_state <= idle;
                end if;
            when char3 =>
                if    x = 'a' then nx_state <= char1;
                else               nx_state <= idle;
                end if;
        end case;
    end process;
    
   -- フラグ出力用の論理回路(レジスタには通さない)
    process(all)
    begin
        case pr_state is
            when idle to char2 => y <= '0'; -- 状態がidle~char2であったらフラグを立てない。
            when char3         => y <= '1'; -- 状態がchar3まで進んだらフラグを立てる。
        end case;   
    end process;
    
end architecture;