library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_signed.all;
use ieee.std_logic_unsigned.all;

--*****************************************************************************
--**** Prefetch Buffer
--****
--**** Deskripsi: Prefetch buffer digunakan untuk menyimpan instruksi yang
--****            diambil dari memori. Prefetch buffer diset pada nilai 4.
--*****************************************************************************
ENTITY prefcon IS
    PORT(readAddress, lastfetched         : IN STD_LOGIC_VECTOR(15 DOWNTO 0);
         miss, mem_taken                  : IN STD_LOGIC;
         we, pcenable                     : OUT STD_LOGIC;
         fetch                            : OUT STD_LOGIC_VECTOR(15 DOWNTO 0));
END ENTITY prefcon;

ARCHITECTURE prefcon_behav OF prefcon IS
BEGIN
    name : PROCESS(readAddress, lastfetched, miss, mem_taken) IS
    BEGIN
        IF(mem_taken='0') THEN
            IF(miss='1') THEN
                we <= '1';
                pcenable <= '0';
                fetch <= readAddress;
            ELSE
                we <= '0';
                pcenable <= '1';
                fetch <= UNSIGNED(lastfetched) + 1;
            END IF;
        ELSE
            IF(miss='1') THEN
                we <= '0';
                pcenable <= '0';
                fetch <= readAddress;
            ELSE
                we <= '0';
                pcenable <= '1';
                fetch <= UNSIGNED(lastfetched) + 1;
            END IF;
        END IF;
    
    END PROCESS name;
END ARCHITECTURE prefcon_behav;
