entity ALARM is
	Port (CLK, R, MS, FS, C : in bit;
         MM, FM : out bit);
end ALARM;

architecture BEHAV of ALARM is
  type STATES is (S_INIT, S_WAIT, S_FRONT, S_MOTION0, S_MOTION1, S_MOTION2);
  signal NEXT_STATE, CURRENT_STATE : STATES;
begin
  process (CURRENT_STATE, C, MS, FS)
  begin
    case (CURRENT_STATE) is
      when S_INIT => NEXT_STATE <= S_WAIT;
      when S_WAIT => if (FS = '1') then
                       NEXT_STATE <= S_FRONT;
                     elsif (MS = '1') then
                       NEXT_STATE <= S_MOTION0;
                     else
                       NEXT_STATE <= S_WAIT;
                     end if;
      when S_FRONT => if (C = '1') then
                        NEXT_STATE <= S_WAIT;
                      else
                        NEXT_STATE <= S_FRONT;
                      end if;
      when S_MOTION0 => NEXT_STATE <= S_MOTION1;
      when S_MOTION1 => NEXT_STATE <= S_MOTION2;
      when S_MOTION2 => NEXT_STATE <= S_WAIT;
    end case;
  end process;

  process (R, CLK) 
  begin
    if (R = '1') then
      CURRENT_STATE <= S_INIT;
    elsif (CLK'event and CLK = '1') then
      CURRENT_STATE <= NEXT_STATE;
    end if;
  end process;

  process (CURRENT_STATE)
  begin
    case CURRENT_STATE is
      when S_INIT | S_WAIT => MM <= '0';
                              FM <= '0';
      when S_FRONT => MM <= '0';
                      FM <= '1';
      when S_MOTION0 | S_MOTION1 | S_MOTION2 => MM <= '1';
                                                FM <= '0';
    end case;
  end process;

end BEHAV;
