library ieee;
use ieee.std_logic_1164.all;

entity ELEVATOR_CONTROL is
  port(FB1, CALL1, FB2, CALL2, FS1, FS2, DC : in std_logic;
       N1, N2 , CLK: in std_logic;
       R1, R2, UP, DOWN, DO : out std_logic);
end ELEVATOR_CONTROL;

architecture BEHAV of ELEVATOR_CONTROL is
  type STATE_TYPE is (F12, F22, F21, F11);
  signal CURRENT_STATE, NEXT_STATE : STATE_TYPE;
begin
  process (FB1, CALL1, FB2, CALL2, FS1, FS2, DC,
           N1, N2, CURRENT_STATE)
  begin
    if (CURRENT_STATE = F12) then
      if (FS2 = '0') then
        UP <= '1';
        NEXT_STATE <= F12;
      else
        R2 <= '1';
        DO <= '1';
        NEXT_STATE <= F22;
      end if;
    elsif (CURRENT_STATE = F22) then
      NEXT_STATE <= F22;
      if (N2 = '1') then
        R2 <= '1';
        DO <= '1';
      elsif (N1 = '1') then
        if (DC = '1') then
          DOWN <= '1';
          NEXT_STATE <= F21;
        end if;
      end if;
    elsif (CURRENT_STATE = F21) then
      if (FS1 = '1') then
        R1 <= '1';
        DO <= '1';
        NEXT_STATE <= F11;
      else
        DOWN <= '1';
        NEXT_STATE <= F21;
      end if;
    elsif (CURRENT_STATE = F11) then
      NEXT_STATE <= F11;
        if (N1 = '1') then
          R1 <= '1';
          DO <= '1';
        elsif (N2 = '1') then
          if (DC = '1') then
            UP <= '1';
            NEXT_STATE <= F12;
          end if;
        end if;
    end if;
  end process;
  process (CLK)
  begin
    if (CLK = '1' and CLK'event) then
      CURRENT_STATE <= NEXT_STATE;
    end if;
  end process;
end BEHAV;


