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

entity test_display is end;
architecture bench of test_display is
    type digits_t is array(0 to 5) of std_logic_vector(6 downto 0);
    type integers_t is array(0 to 5) of natural;
    constant glyphs : integers_t := (63,6,91,79,102,109);
    signal clk : std_logic := '0';
    signal key : std_logic_vector(3 downto 0) := "1110";
    signal switches, lamps : std_logic_vector(9 downto 0) := (others=>'0');
    signal digits : digits_t;
begin
    clk <= not clk after 1 ns;
    dut : entity work.display_cpu port map(clk,key,switches,lamps,
        digits(0),digits(1),digits(2),digits(3),digits(4),digits(5));
    process
        variable expected : digits_t;
        procedure advance(n : positive) is
        begin for j in 1 to n loop wait until falling_edge(clk); end loop; end procedure;
        procedure write_and_check(selector : natural) is
        begin
            switches <= std_logic_vector(to_unsigned(512+selector,10)); advance(160);
            if selector<6 then expected(selector) := not std_logic_vector(to_unsigned(glyphs(selector),7)); end if;
            assert expected=digits report "FAIL write selector " & integer'image(selector) severity failure;
        end procedure;
    begin
        for first in 0 to 7 loop
            for second in 0 to 7 loop
                key<="1110"; switches<=(others=>'0'); advance(5);
                expected:=(others=>(others=>'1'));
                assert digits=expected report "FAIL reset" severity failure;
                key<="1111"; write_and_check(first);
                switches<=std_logic_vector(to_unsigned(first,10)); advance(20);
                switches<=std_logic_vector(to_unsigned(second,10)); advance(120);
                assert digits=expected report "FAIL retention during halt" severity failure;
                write_and_check(second);
            end loop;
        end loop;
        report "PASS all 64 ordered address pairs, retention, halt and reset"; finish;
    end process;
end architecture;
