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;

--******************************************************************************
--** ENTITY bforward
--** 
--** Deskripsi: Unit bforward diposisikan untuk Branch Forwarding Unit.  
--**            ini digunakan untuk meneruskan nilai dari EX stage dan MEM stage ke
--**            ID stage sehingga branching tests dapat bekerja tanpa
--**            diperlukan untuk proses stall.
--******************************************************************************
ENTITY bforward IS
    port(exmRegWrite,memwbRegWrite          : IN STD_LOGIC;
         exmWriteReg, memwbWriteReg,ReadOne : IN STD_LOGIC_VECTOR(2 DOWNTO 0); 
         ReadTwo                            : IN STD_LOGIC_VECTOR(2 DOWNTO 0);
         buSelA, buSelB                     : OUT STD_LOGIC_VECTOR(1 DOWNTO 0));

END ENTITY bforward;

ARCHITECTURE bforward_behav OF bforward IS
BEGIN

    name : PROCESS(exmRegWrite,exmWriteReg,ReadOne,ReadTwo, 
                   exmWriteReg,memwbWriteReg) IS

    BEGIN
        --Jika Register dari EX/MEM stage akan dituliskan 
        IF(exmRegWrite='1') THEN
            IF(memwbRegWrite='0') THEN
            
                --Jika write register = read register one kemudian memilih 
                --write register 
                IF(exmWriteReg=ReadOne) THEN
                    buSelA <= "01";
                ELSE
                    buSelA <= "00";
                END IF;
                
                --Jika write register = read register two kemudian memilih 
                --write register    
                IF(exmWriteReg=ReadTwo) THEN
                    buSelB <= "01";
                ELSE
                    buSelB <= "00";
                END IF;
            ELSE
                --Berarti bahwa kedua exmRegWrite dan memwbRegWrite = 1
                --melaksanakan stuff yang sama;
                IF(exmWriteReg=ReadOne) THEN
                    buSelA <= "01";
                ELSE IF(memwbWriteReg=ReadOne) THEN
                    buSelA <= "10";
                ELSE
                    buSelA <= "00";
                END IF;
                END IF;

                IF(exmWriteReg=ReadTwo) THEN
                    buSelB <= "01";
                ELSE IF(memwbWriteReg=ReadTwo) THEN
                    buSelB <= "10";
                ELSE
                    buSelB <= "00";
                END IF;
                END IF;
            END IF;
        ELSE IF(memwbRegWrite='1') THEN
                    -- Ini berarti exstage adalah zero 
                IF(memwbWriteReg=ReadTwo) THEN
                    buSelB <= "10";
                ELSE
                    buSelB <= "00";
                END IF;

                IF(memwbWriteReg=ReadOne) THEN
                    buSelA <= "10";
                ELSE
                    buSelA <= "00";
                END IF;
        ELSE
                buSelA <= "00";
                buSelB <= "00";
        END IF;
        END IF;
    END PROCESS name;
END ARCHITECTURE bforward_behav;
