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

entity test_product is end entity;
architecture bench of test_product is
    type digits_t is array (0 to 15) of std_logic_vector(6 downto 0);
    constant digit : digits_t := ("1000000", "1111001", "0100100", "0110000", "0011001", "0010010", "0000010", "1111000",
        "0000000", "0010000", "0001000", "0000011", "1000110", "0100001", "0000110", "0001110");
    signal sw : std_logic_vector(9 downto 0) := (others => '0');
    signal key : std_logic_vector(3 downto 0) := (others => '1');
    signal leds : std_logic_vector(9 downto 0);
    signal h3,h2,h1,h0 : std_logic_vector(6 downto 0);
begin
    dut : entity work.registered_product port map(sw,key,leds,h3,h2,h1,h0);
    process
        procedure step is
        begin
            key(1) <= '0'; wait for 1 ns; key(1) <= '1'; wait for 1 ns;
        end procedure;
        procedure check_product(stored_a, stored_b : natural) is
            constant number : natural := stored_a * stored_b;
        begin
            assert h3=digit((number/4096) mod 16) and h2=digit((number/256) mod 16)
                and h1=digit((number/16) mod 16) and h0=digit(number mod 16)
                report "FAIL A=" & integer'image(stored_a) & " B=" & integer'image(stored_b)
                    & " expected product " & integer'image(number) severity failure;
        end procedure;
        variable old_b : natural := 0;
    begin
        key(0) <= '0'; step; key(0) <= '1';
        for a in 0 to 255 loop
            sw <= std_logic_vector(to_unsigned(512+a,10)); step;
            assert unsigned(leds)=a report "FAIL A enable" severity failure;
            for b in 0 to 255 loop
                sw <= std_logic_vector(to_unsigned(256+b,10)); wait for 1 ns;
                assert unsigned(leds)=old_b report "FAIL input isolation" severity failure;
                step; check_product(a,old_b);
                assert unsigned(leds)=b report "FAIL B enable" severity failure;
                sw <= (others => '0'); step; check_product(a,b);
                assert unsigned(leds)=0 report "FAIL inactive display" severity failure;
                old_b := b;
            end loop;
        end loop;
        key(0) <= '0'; wait for 1 ns; check_product(255,255);
        step; check_product(0,0);

        key(0) <= '1';
        sw <= "10" & x"FF"; step;
        sw <= "01" & x"80"; step;
        sw <= "11" & x"FF"; wait for 1 ns;
        assert unsigned(leds)=255 report "FAIL both enables must display stored A" severity failure;
        step; check_product(255,128);
        sw <= (others => '0'); step; check_product(255,255);
        sw <= "11" & x"00"; step; check_product(255,255);
        assert unsigned(leds)=0 report "FAIL both enables must load zero into A" severity failure;
        sw <= (others => '0'); step; check_product(0,0);
        report "PASS 65536 operand pairs, operand enables, output latency and synchronous reset";
        wait;
    end process;
end architecture;
