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

entity test_accumulator is end entity;
architecture bench of test_accumulator 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.add_sub_accumulator 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;
        function signed_byte(x : natural) return integer is
        begin
            if x < 128 then return x; else return x-256; end if;
        end function;
        variable mathematical,raw : integer;
        variable expected_flags : std_logic_vector(1 downto 0);
    begin
        for mode in 0 to 1 loop
            for s in 0 to 255 loop
                for a in 0 to 255 loop
                    key(0) <= '0'; wait for 1 ns;
                    assert leds(9 downto 8)="00" and h3=digit(0) and h2=digit(0)
                        and h1=digit(0) and h0=digit(0) report "FAIL asynchronous reset" severity failure;
                    key(0) <= '1'; sw <= std_logic_vector(to_unsigned(s,10)); step;
                    sw <= std_logic_vector(to_unsigned(a,10)); step;
                    sw <= std_logic_vector(to_unsigned(mode*256,10)); wait for 1 ns;
                    if mode = 1 then
                        mathematical := signed_byte(s)-signed_byte(a); raw := s-a; expected_flags(0) := '1' when s >= a else '0';
                    else
                        mathematical := signed_byte(s)+signed_byte(a); raw := s+a; expected_flags(0) := '1' when raw > 255 else '0';
                    end if;
                    expected_flags(1) := '1' when mathematical < -128 or mathematical > 127 else '0';
                    assert leds(7 downto 0) = std_logic_vector(to_unsigned(raw mod 256, 8)) and
                        h3=digit(a/16) and h2=digit(a mod 16) and h1=digit(s/16) and h0=digit(s mod 16)
                        report "FAIL preview/display S=" & to_hstring(to_unsigned(s, 8)) & " A=" & to_hstring(to_unsigned(a, 8))
                            & " subtract=" & integer'image(mode) & " expected preview=" & to_hstring(to_unsigned(raw mod 256, 8))
                            & " observed=" & to_hstring(leds(7 downto 0)) severity failure;
                    step;
                    assert leds(9 downto 8) = expected_flags
                        report "FAIL flags S=" & to_hstring(to_unsigned(s, 8)) & " A=" & to_hstring(to_unsigned(a, 8))
                            & " subtract=" & integer'image(mode) & " expected V/C=" & to_string(expected_flags)
                            & " observed=" & to_string(leds(9 downto 8)) severity failure;
                    assert h3=digit(0) and h2=digit(0) and h1=digit((raw mod 256)/16) and h0=digit(raw mod 16)
                        report "FAIL registered display S=" & integer'image(s) & " A=" & integer'image(a) severity failure;
                end loop;
            end loop;
        end loop;
        report "PASS all accumulator states, operand pairs, flags and display outputs";
        wait;
    end process;
end architecture;
