library ieee;
use ieee.std_logic_1164.all;

--******************************************************************************
--**** ENTITY idex_c
--**** Deskripsi: Entity menguraikan IDEX control register. Nilai
--****            ditulis ke control register pada clock positif.
--****            Jika IDEX register dibersihkan kemudian semua control
--****            lines diset kenilai NOL.
--******************************************************************************
ENTITY idex_c IS
    PORT(idexFlush, clk                     : IN STD_LOGIC;
         exmemIn                            : IN STD_LOGIC;
         wbIn                               : IN STD_LOGIC_VECTOR(2 DOWNTO 0);
         alu_selIn                          : IN STD_LOGIC;
         ex_selIn                           : IN STD_LOGIC_VECTOR(1 DOWNTO 0);
         alu_funcIn                         : IN STD_LOGIC_VECTOR(3 DOWNTO 0);
         msweIn                             : IN STD_LOGIC_VECTOR(1 DOWNTO 0);
         jalIn                              : IN STD_LOGIC;
         exmem                              : OUT STD_LOGIC;
         wb                                 : OUT STD_LOGIC_VECTOR(2 DOWNTO 0);
         alu_sel                            : OUT STD_LOGIC;
         alu_func                           : OUT STD_LOGIC_VECTOR(3 DOWNTO 0);
         ex_sel                             : OUT STD_LOGIC_VECTOR(1 DOWNTO 0);
         idexmsweOut                        : OUT STD_LOGIC_VECTOR(1 DOWNTO 0);
         jal_control                        : OUT STD_LOGIC);
END ENTITY idex_c;

ARCHITECTURE idexc_behav OF idex_c IS
BEGIN
    name : PROCESS(clk) IS
        VARIABLE regValue : std_logic_vector(13 DOWNTO 0);
    BEGIN
        
        IF(clk='1') THEN
            IF(idexFlush='1') THEN
                regValue := "00000000000000";
            ELSE
                regValue(13 DOWNTO 0) := jalIn & exmemIn & wbIn(2 DOWNTO 0) & 
                alu_selIn & ex_selIn(1 DOWNTO 0) & alu_funcIn(3 DOWNTO 0) & 
                msweIn(1 DOWNTO 0);
            END IF;
        END IF;

        jal_control <= regValue(13);
        exmem <= regValue(12);
        wb <= regValue(11 DOWNTO 9);
        alu_sel <= regValue(8);
        ex_sel <= regValue(7 DOWNTO 6);
        alu_func <= regValue(5 DOWNTO 2);
        idexmsweOut <= regValue(1 DOWNTO 0);
        
    END PROCESS name;
END ARCHITECTURE idexc_behav;
