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

entity course_cpu is
    port (clk, reset_n, run : in std_logic;
          data_in : in unsigned(15 downto 0);
          probe : in unsigned(2 downto 0);
          view : out unsigned(15 downto 0);
          done, busy : out std_logic);
end entity;

architecture rtl of course_cpu is
    type phase_type is (fetch, execute_op, calculate, write_back);
    type register_file is array (0 to 7) of unsigned(15 downto 0);
    signal phase : phase_type;
    signal registers : register_file;
    signal ir, accumulator, result, operand, bus_value : unsigned(15 downto 0);
    signal op : unsigned(2 downto 0);
    signal destination : natural range 0 to 7;
begin
    op <= ir(15 downto 13);
    destination <= to_integer(ir(11 downto 9));
    operand <= unsigned(resize(signed(ir(8 downto 0)), 16)) when ir(12) = '1'
               else registers(to_integer(ir(2 downto 0)));
    process(phase, op, ir, operand, registers, destination, result, data_in)
    begin
        case phase is
            when fetch => bus_value <= data_in;
            when execute_op =>
                case op is
                    when "000" => bus_value <= operand;
                    when "001" => bus_value <= ir(7 downto 0) & x"00";
                    when others => bus_value <= registers(destination);
                end case;
            when calculate => bus_value <= operand;
            when write_back => bus_value <= result;
        end case;
    end process;
    view <= registers(to_integer(probe));
    busy <= '0' when phase = fetch else '1';
    done <= '1' when (phase = execute_op and (op = 0 or op = 1)) or phase = write_back else '0';
    process(clk, reset_n)
    begin
        if reset_n = '0' then
            phase <= fetch;
            ir <= (others => '0');
            accumulator <= (others => '0');
            result <= (others => '0');
            registers <= (others => (others => '0'));
        elsif rising_edge(clk) then
            case phase is
                when fetch =>
                    if run = '1' then ir <= bus_value; phase <= execute_op; end if;
                when execute_op =>
                    case op is
                        when "000" | "001" =>
                            registers(destination) <= bus_value; phase <= fetch;
                        when "010" | "011" =>
                            accumulator <= bus_value; phase <= calculate;
                        when others => phase <= fetch;
                    end case;
                when calculate =>
                    if op = 3 then result <= accumulator - bus_value;
                    else result <= accumulator + bus_value; end if;
                    phase <= write_back;
                when write_back =>
                    registers(destination) <= bus_value; phase <= fetch;
            end case;
        end if;
    end process;
end architecture;

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

entity rom_cpu is
    port (SW : in std_logic_vector(9 downto 0); KEY : in std_logic_vector(3 downto 0);
          LEDR : out std_logic_vector(9 downto 0));
end entity;

architecture rtl of rom_cpu is
    signal address : unsigned(4 downto 0);
    signal instruction, selected, reg_value : unsigned(15 downto 0);
    signal done, busy : std_logic;
    function rom_word(location : unsigned(4 downto 0)) return unsigned is
    begin
        case to_integer(location) is
            -- BEGIN PROGRAM WORDS
            when 0 => return x"1407";
            when 1 => return x"1BFE";
            when 2 => return x"4405";
            when 3 => return x"7408";
            when 4 => return x"3880";
            when 5 => return x"0E02";
            when 6 => return x"0000";
            when 7 => return x"0000";
            when 8 => return x"0000";
            when 9 => return x"0000";
            when 10 => return x"0000";
            when 11 => return x"0000";
            when 12 => return x"0000";
            when 13 => return x"0000";
            when 14 => return x"0000";
            when 15 => return x"0000";
            when 16 => return x"0000";
            when 17 => return x"0000";
            when 18 => return x"0000";
            when 19 => return x"0000";
            when 20 => return x"0000";
            when 21 => return x"0000";
            when 22 => return x"0000";
            when 23 => return x"0000";
            when 24 => return x"0000";
            when 25 => return x"0000";
            when 26 => return x"0000";
            when 27 => return x"0000";
            when 28 => return x"0000";
            when 29 => return x"0000";
            when 30 => return x"0000";
            when 31 => return x"0000";
            -- END PROGRAM WORDS
            when others => return x"0000";
        end case;
    end function;
begin
    process(KEY, SW)
    begin
        if SW(0) = '0' then
            address <= (others => '0');
            instruction <= (others => '0');
        elsif rising_edge(KEY(0)) then
            instruction <= rom_word(address);
            address <= address + 2;
        end if;
    end process;
    core : entity work.course_cpu port map(KEY(1), SW(0), SW(9), instruction,
                                           unsigned(SW(7 downto 5)), reg_value, done, busy);
    selected <= instruction when SW(4) = '1' else reg_value;
    LEDR(9 downto 8) <= done & SW(9);
    LEDR(7 downto 0) <= std_logic_vector(selected(15 downto 8)) when SW(8) = '1'
                         else std_logic_vector(selected(7 downto 0));
end architecture;
