-- SPDX-License-Identifier: MIT
-- Select Airport Wind Station. SW0 holds the controller in reset.
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity wind_interlock is
    generic (CLOCKS_PER_TICK : positive := 50000000);
    port (CLOCK_50 : in std_logic;
          V_SW : in std_logic_vector(9 downto 0);
          V_GPIO : inout std_logic_vector(35 downto 23);
          LEDR : out std_logic_vector(9 downto 0));
end entity;

architecture rtl of wind_interlock is
    type motion is (stopped, aligning, generating, stopping);
    signal state : motion;
    signal reset_pipe : std_logic_vector(1 downto 0);
    signal sensors_meta, sensors : std_logic_vector(2 downto 0);
    signal divider : natural range 0 to CLOCKS_PER_TICK - 1;
    signal reset, steady, gust, aligned, active, yaw, generator_on, runway_alert : std_logic;
    signal shown_state : std_logic_vector(1 downto 0);
begin
    process(CLOCK_50, V_SW(0))
    begin
        if V_SW(0) = '1' then reset_pipe <= "11";
        elsif rising_edge(CLOCK_50) then reset_pipe <= reset_pipe(0) & '0';
        end if;
    end process;
    reset <= reset_pipe(1);

    -- Weather readings continue while the operator holds SW0.
    process(CLOCK_50)
    begin
        if rising_edge(CLOCK_50) then
            sensors_meta <= V_GPIO(30 downto 29) & V_GPIO(24);
            sensors <= sensors_meta;
        end if;
    end process;
    steady <= '1' when sensors(2 downto 1) = "10" else '0';
    gust <= '1' when sensors(2 downto 1) = "11" else '0';
    aligned <= sensors(0);

    process(CLOCK_50)
    begin
        if rising_edge(CLOCK_50) then
            if reset = '1' then
                state <= stopped;
                divider <= 0;
            elsif divider = CLOCKS_PER_TICK - 1 then
                divider <= 0;
                if state = stopping then state <= stopped;
                elsif steady = '1' then
                    if aligned = '1' then state <= generating;
                    else state <= aligning;
                    end if;
                elsif state /= stopped then state <= stopping;
                end if;
            else divider <= divider + 1;
            end if;
        end if;
    end process;

    active <= '1' when reset = '0' and state /= stopped else '0';
    yaw <= '1' when active = '1' and state = aligning and steady = '1' and aligned = '0' else '0';
    generator_on <= '1' when active = '1' and state = generating and steady = '1' and aligned = '1' else '0';
    runway_alert <= gust and not reset;
    V_GPIO(26) <= yaw;
    V_GPIO(27) <= '0';
    V_GPIO(32) <= generator_on;
    V_GPIO(34) <= active;
    V_GPIO(31) <= runway_alert;
    with state select shown_state <= "00" when stopped, "01" when aligning,
                                    "10" when generating, "11" when stopping;
    LEDR(1 downto 0) <= "00" when reset = '1' else shown_state;
    LEDR(9 downto 2) <= '0' & reset & aligned & steady & runway_alert & active & generator_on & yaw;
end architecture;
