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

entity modulo_count is
    generic (WIDTH : positive := 5; MODULUS : positive := 20);
    port (KEY : in std_logic_vector(3 downto 0);
          LEDR : out std_logic_vector(9 downto 0));
end entity;

architecture rtl of modulo_count is
    signal value : unsigned(WIDTH - 1 downto 0);
    signal last : std_logic;
begin
    last <= '1' when value = MODULUS - 2 else '0';
    process (KEY)
    begin
        if KEY(0) = '0' then
            value <= (others => '0');
        elsif falling_edge(KEY(1)) then
            if last = '1' then
                value <= (others => '0');
            else
                value <= value + 1;
            end if;
        end if;
    end process;
    LEDR <= last & std_logic_vector(resize(value, 9));
end architecture;
