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

entity test_ram is end entity;
architecture bench of test_ram is
    signal sw : std_logic_vector(9 downto 0) := (others => '0');
    signal key : std_logic_vector(3 downto 0) := (others => '1');
    signal h5, h4, h3, h2, h1, h0 : std_logic_vector(6 downto 0);
    type glyphs is array (0 to 15) of natural;
    constant seg : glyphs := (64,121,36,48,25,18,2,120,0,16,8,3,70,33,6,14);
begin
    dut : entity work.inferred_ram port map (sw,key,h5,h4,h3,h2,h1,h0);
    process
        procedure step is
        begin key(0) <= '0'; wait for 1 ns; key(0) <= '1'; wait for 1 ns; end procedure;
        variable value, old_value : natural := 0;
    begin
        for pass in 0 to 15 loop
            for address in 0 to 31 loop
                value := (address + address/16 + pass) mod 16;
                sw <= std_logic_vector(to_unsigned(512 + 16*address + value, 10)); step;
                if pass > 0 then assert unsigned(h0) = seg(old_value) report "FAIL changed on write" severity failure; end if;
            end loop;
            for address in 31 downto 0 loop
                sw <= std_logic_vector(to_unsigned(16*address + ((address+3) mod 16),10)); wait for 1 ns;
                if address < 31 or pass > 0 then
                    assert unsigned(h0) = seg(old_value) report "FAIL unclocked read" severity failure;
                end if;
                step;
                value := (address + address/16 + pass) mod 16;
                assert unsigned(h0) = seg(value) and unsigned(h2) = seg((address+3) mod 16)
                    and unsigned(h4) = seg(address mod 16) and unsigned(h5) = seg(address/16)
                    and unsigned(h1) = 127 and unsigned(h3) = 127
                    report "FAIL pass " & integer'image(pass) & " address " & integer'image(address) severity failure;
                old_value := value;
            end loop;
        end loop;
        report "PASS 512 writes and 512 reads, address and data displays, registered hold"; wait;
    end process;
end architecture;
