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

entity test_modulo_count is end entity;
architecture bench of test_modulo_count is
    signal keys : std_logic_vector(3 downto 0) := (others => '1');
    signal twenty, five : std_logic_vector(9 downto 0);
    function word(n, modulus : natural) return std_logic_vector is
        variable value : unsigned(9 downto 0);
    begin
        value := to_unsigned(n mod modulus, 10);
        if n mod modulus = modulus - 1 then value(9) := '1'; end if;
        return std_logic_vector(value);
    end function;
begin
    normal : entity work.modulo_count port map(keys, twenty);
    short_period : entity work.modulo_count generic map(3, 5) port map(keys, five);
    process
        procedure check(n : natural) is
        begin
            assert twenty = word(n, 20) and five = word(n, 5)
                report "FAIL at count " & integer'image(n) severity failure;
        end procedure;
    begin
        wait for 2 ns; keys(0) <= '0'; wait for 2 ns; check(0); keys(0) <= '1';
        for n in 1 to 42 loop
            wait for 2 ns; keys(1) <= '0'; wait for 3 ns; check(n);
            keys(1) <= '1'; wait for 3 ns; check(n);
        end loop;
        keys(0) <= '0'; wait for 2 ns; check(0);
        keys(1) <= '0'; wait for 2 ns; check(0); keys(1) <= '1'; keys(0) <= '1';
        wait for 2 ns; keys(1) <= '0'; wait for 2 ns; check(1);
        report "PASS two moduli, 42 press/release pairs and asynchronous reset";
        wait;
    end process;
end architecture;
