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 <= resize(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 step_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 board of step_cpu is
    signal high_byte, low_byte : std_logic_vector(7 downto 0);
    signal input_word, view : unsigned(15 downto 0);
    signal done, busy : std_logic;
begin
    process(KEY)
    begin
        if KEY(3) = '0' then high_byte <= (others => '0');
        elsif rising_edge(KEY(1)) then high_byte <= SW(7 downto 0); end if;
    end process;
    process(KEY)
    begin
        if KEY(3) = '0' then low_byte <= (others => '0');
        elsif rising_edge(KEY(2)) then low_byte <= SW(7 downto 0); end if;
    end process;
    input_word <= unsigned(high_byte & low_byte);
    core : entity work.course_cpu port map(KEY(0), KEY(3), SW(9), input_word,
                                           unsigned(SW(2 downto 0)), view, done, busy);
    LEDR(9 downto 8) <= done & busy;
    LEDR(7 downto 0) <= std_logic_vector(view(15 downto 8)) when SW(8) = '1'
                         else std_logic_vector(view(7 downto 0));
end architecture;
