library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity timed_word is
    generic (CYCLES_PER_SECOND : positive := 50000000);
    port (CLOCK_50 : in std_logic;
          SW : in std_logic_vector(9 downto 0);
          HEX3, HEX2, HEX1, HEX0 : out std_logic_vector(6 downto 0));
end entity;

architecture rtl of timed_word is
    signal interval : unsigned(25 downto 0);
    signal position : unsigned(2 downto 0);
    signal controls_meta, controls : std_logic_vector(1 downto 0);
    subtype segments is std_logic_vector(6 downto 0);
    type message_array is array (0 to 3) of segments;
    constant message_text : message_array := ("1111111", "0100001", "0000110", "1111001");
    signal displays : message_array;
begin
    process(CLOCK_50)
    begin
        if rising_edge(CLOCK_50) then
            controls_meta <= SW(9 downto 8);
            controls <= controls_meta;
            if controls(0) = '1' then
                interval <= (others => '0');
                position <= (others => '0');
            elsif controls(1) = '0' then
                if interval = CYCLES_PER_SECOND - 1 then
                    interval <= (others => '0');
                    if position = 3 then
                        position <= (others => '0');
                    else
                        position <= position + 1;
                    end if;
                else
                    interval <= interval + 1;
                end if;
            end if;
        end if;
    end process;
    -- TODO: use the current position when selecting the source character.
    columns : for column in 0 to 3 generate
        displays(column) <= message_text(column);
    end generate;
    HEX3 <= displays(0);
    HEX2 <= displays(1);
    HEX1 <= displays(2);
    HEX0 <= displays(3);
end architecture;
