FSM(有限状態マシン)の設計

琉球大学情報工学科 和田 知久



例題5:


例題5−1:3つのブロック(次状態、現状態、出力)による記述例。

fsm21.vhd

entity FSM21 is
  port ( Clock, Reset : in std_logic;
       Control : in std_logic;
       Y : out integer range 0 to 4 );
end entity FSM21;

architecture RTL of FSM21 is
  type StateType is (ST0, ST1, ST2, ST3);
  signal CurrentState, NextState : StateType;
begin

  ----------------------
  -- NEXT STATE LOGIC
  ----------------------
  COMB: process (Control, CurrentState)
  begin
    case CurrentState is
      when ST0 => NextState <= ST1;
      when ST1 =>
        if (Control ='1') then NextState <= ST2;
        else NextState <= ST3;
      when ST2 => NextState <= ST3;
      when ST3 => NextState <= ST0;
      when others => NextState <= ST0;
    end case;
  end process COMB;
  ----------------------
  -- CURRENT STATE LOGIC
  ----------------------
  SEQ: process (Clock, Reset)
  begin
    if (Reset ='1') then CurrentState <= ST0;
    elsif rising_edge(Clock) then CurrentState <= NextState;
    end if;
  end process SEQ;

  ----------------------
  -- OUTPUT LOGIC (MOORE TYPE)
  ----------------------
  with CurrentState select
    Y <= 1 when ST0,
       2 when ST1,
       3 when ST2,
       4 when ST3,
       1 when others;
end architecture RTL;   


例題5−2:現状態と次状態を同一ブロックで記述した例。

fsm22.vhd

entity FSM22 is
  port ( Clock, Reset : in std_logic;
      Control    : in std_logic
      Y        :
out integer range 0 to 4);
end entity FSM22;

architecture RTL of FSM22 is
  type StateType is (ST0, ST1, ST2, ST3);
  signal STATE : StateType;
begin
  ---------------------
  -- CURRENT & NEXT STATE LOGIC
  ---------------------

  NEXT_CURR: process (Clock, Reset)
  begin
    if (Reset='1') then
      STATE <= ST0;
    elsif rising_edge(Clock) then
      case (STATE) is
        when ST0 => STATE <= ST1;
        when ST1 =>
          if (Control ='1') then STATE <= ST2;
          elsif STATE <= ST3;
          end if;
        when ST2 => STATE <= ST3;
        when ST3 => STATE <= ST0;
        when others => null;
      end case;
    end if;
  end process NEXT_CURR;

  ----------------------
  -- OUTPUT LOGIC (MOORE TYPE)
  ----------------------
  with CurrentState select
    Y <= 1 when ST0,
       2 when ST1,
       3 when ST2,
       4 when ST3,
       1 when others;
end architecture RTL;

 


例題5−3:次状態と出力を1つのブロックで記述した例。

fsm23.vhd

entity FSM23 is
  port ( Clock, Reset : in std_logic;
      Control    : in std_logic;
      Y        : out integer range 0 to 4);
end entity FSM23;

architecture RTL of FSM23 is
  type StateType is (ST0, ST1, ST2, ST3);
  signal CurrentState, NextState : StateType;
begin
  ---------------------
  -- NEXT & OUTPUT LOGIC
  ---------------------
  COMB: process (Control, CurrentState)
  begin
    case CurrentState is
      when ST0 =>
        Y <= 1;
        NextState <= ST1;
      when ST1 =>
        Y <= 2;
        if (Control='1') then NextState <= ST2;
        else NextState <= ST3;
        end if;
      when ST2 =>
        Y <= 3;
        NextState <= ST3;
      when ST3 =>
        Y <= 4;
        NextState <= ST0;
      when others =>
        Y <= 1;
        NextState <= ST0;
    end case;
  end process COMB:

  ---------------------
  -- CURRENT STATE LOGIC
  ---------------------
  SEQ: process(Clock,Reset)
  begin
    if (Reset='1') then
      CurrentState <= ST0;
    elsif rising_edge(Clock) then
      CurrentState <= NextState;
    end if;
  end process SEQ;
end architecture RTL;

 


例題5−4:すべてを1つのブロックで記述した例。(悪い例)

fsm24.vhd

entity FSM24 is
  port ( Clock, Reset : in std_logic;
      Control    : in std_logic;
      Y        : out integer range 0 to 4 );
end entity FSM24;

architecture RTL of FSM24 is
begin
  ALL_IN_1: process (Clock, Reset)
    type StateType is (ST0, ST1, ST2, ST3);
    variable State : StateType;
begin
  if (Reset='1') then
    State := ST0;
  elsif rising_edge(Clock) then
    case State is
      when ST0 => State := ST1;
      when ST1 =>
        if (Control ='1') then State := ST2;
        else State := ST3;
        end if;
      when ST2 => State := ST3;
      when ST3 => State := ST0;
      when others => State := ST0;
    end case;
  end if;

  case State is
    when ST0 => Y <=1;
    when ST1 => Y <=2;
    when ST2 => Y <=3;
    when ST3 => Y <=4;
    when others => Y <=1;
  end case;
end architecture RTL;

 


例題5−5:合成すると余計なFFが生成される例。(悪い例)

fsm2bad.vhd

entity FSM2BAD is
  port (Clock, Reset : in std_logic;
      Control   : in std_logic;
      Y       : out integer range 1 to 4);
end entity FSM2BAD;

architecture RTL of FSM2BAD is
begin
  ALL_IN_1: processor(Clock, Reset)
    type StateType is (ST0, ST1, ST2, ST3);
    variable STATE : StateType := ST0;
  begin
    if (Reset ='1') then
      Y <= 1;
      STATE := ST0;
    elsif rising_edge(Clock) then
      case (STATE) is
        when ST0 => Y <= 1;  -- elsif文の中で、Yに対してFFが生成。
                 STATE := ST1;
        when ST1 => Y <= 2;
                 if (Control ='1') then
                  STATE := ST2;
                 else STATE := ST3;
                 end if;
        when ST2 => Y <= 3;
                 STATE := ST3;
        when ST3 => Y <= 4;
                 STATE := ST0;
       end case;
     end if;
  end process ALL_IN_1;
end architecture RTL;