Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------------------------------------------------------------------
- -- Logicko projektovanje racunarskih sistema 1
- -- 2011/2012
- -- Lab 7
- --
- -- Control unit
- --
- -- author: Branislav Nikolic,e13592
- ----------------------------------------------------------------------------------
- library IEEE;
- use IEEE.STD_LOGIC_1164.ALL;
- use IEEE.STD_LOGIC_UNSIGNED.ALL;
- entity control_unit is
- Port ( iCLK : in STD_LOGIC;
- inRST : in STD_LOGIC;
- iINSTR : in STD_LOGIC_VECTOR (14 downto 0);
- iSIGN : in STD_LOGIC;
- iZERO : in STD_LOGIC;
- iCARRY : in STD_LOGIC;
- oREG_WE : out STD_LOGIC_VECTOR (7 downto 0);
- oA_WE : out STD_LOGIC;
- oB_WE : out STD_LOGIC;
- oC_WE : out STD_LOGIC;
- oIR_WE : out STD_LOGIC;
- oPC_IN : out STD_LOGIC_VECTOR (15 downto 0);
- oPC_EN : out STD_LOGIC;
- oPC_LOAD : out STD_LOGIC;
- oMUXA_SEL : out STD_LOGIC_VECTOR (3 downto 0);
- oMUXB_SEL : out STD_LOGIC_VECTOR (3 downto 0);
- oALU_SEL : out STD_LOGIC_VECTOR (3 downto 0);
- oMEM_WE : out STD_LOGIC;
- oPHASE : out STD_LOGIC_VECTOR (1 downto 0));
- end control_unit;
- architecture Behavioral of control_unit is
- type tSTATES is (FETCH, DECODE, EXECUTE, WBACK);
- signal sSTATE : tSTATES;
- signal JMP: std_logic;
- begin
- -- Main FSM --
- process (iCLK, inRST) begin
- if (inRST = '0') then
- sSTATE <= FETCH;
- elsif (iCLK'event and iCLK = '1') then
- case (sSTATE) is
- when FETCH =>
- sSTATE <= DECODE;
- when DECODE =>
- sSTATE <= EXECUTE;
- when EXECUTE =>
- sSTATE <= WBACK;
- when WBACK =>
- sSTATE <= FETCH;
- end case;
- end if;
- end process;
- -- Output function --
- process (iINSTR, sSTATE, iZERO, iSIGN, iCARRY) begin
- case (sSTATE) is
- -- FETCH phase, instruction is written in IR
- when FETCH =>
- oREG_WE <= "00000000";
- oA_WE <= '0';
- oB_WE <= '0';
- oC_WE <= '0';
- oIR_WE <= '1';
- oPC_IN <= "0000000000000000";
- oPC_EN <= '0';
- oPC_LOAD <= '0';
- oMUXA_SEL <= "0000";
- oMUXB_SEL <= "0000";
- oALU_SEL <= "0000";
- oMEM_WE <= '0';
- -- DECODE phase, operands are written in A and B, MUX's select operands
- when DECODE =>
- oREG_WE <= "00000000";
- oA_WE <= '1';
- oB_WE <= '1';
- oC_WE <= '0';
- oIR_WE <= '0';
- oPC_IN <= "0000000000000000";
- oPC_EN <= '0';
- oPC_LOAD <= '0';
- if (iINSTR(14 downto 13) = "10") then
- -- LOAD --
- oMUXA_SEL <= "1000";
- else
- -- other instructions --
- oMUXA_SEL <= '0' & iINSTR(5 downto 3);
- end if;
- oMUXB_SEL <= '0' & iINSTR(2 downto 0);
- oALU_SEL <= "0000";
- oMEM_WE <= '0';
- -- EXECUTE phase, ALU performs operation and result is written in C
- when EXECUTE =>
- if (JMP='1')then oC_WE <='0';
- else
- oC_WE <= '1';
- end if;
- oREG_WE <= "00000000";
- oA_WE <= '0';
- oB_WE <= '0';
- oIR_WE <= '0';
- oPC_IN <= "0000000000000000";
- oPC_EN <= '0';
- oPC_LOAD <= '0';
- oMUXA_SEL <= "0000";
- oMUXB_SEL <= '0' & iINSTR(2 downto 0); -- kept for STORE instruction --
- oALU_SEL <= iINSTR(12 downto 9);
- oMEM_WE <= '0';
- -- WRITE_BACK phase, result is written in register or memory
- when WBACK =>
- if (JMP='1')then
- oREG_WE <= "00000000";
- oPC_IN <="0000000" &iINSTR(8 downto 0);
- oPC_LOAD <= '1';
- elsif (iINSTR(14 downto 13) /= "11") then
- -- all instructions except STORE --
- case (iINSTR(8 downto 6)) is
- when "000" => oREG_WE <= "00000001";
- when "001" => oREG_WE <= "00000010";
- when "010" => oREG_WE <= "00000100";
- when "011" => oREG_WE <= "00001000";
- when "100" => oREG_WE <= "00010000";
- when "101" => oREG_WE <= "00100000";
- when "110" => oREG_WE <= "01000000";
- when "111" => oREG_WE <= "10000000";
- when others => oREG_WE <= "00000000";
- end case;
- else
- -- STORE--
- oREG_WE <= "00000000";
- end if;
- oA_WE <= '0';
- oB_WE <= '0';
- oC_WE <= '0';
- oIR_WE <= '0';
- oPC_IN <= "0000000000000000";
- oPC_LOAD <= '0';
- oPC_EN <= '1';
- oMUXA_SEL <= "0000";
- oMUXB_SEL <= '0' & iINSTR(2 downto 0); -- kept for STORE instruction --
- oALU_SEL <= "0000";
- if (iINSTR(14 downto 13) = "11") then
- -- STORE --
- oMEM_WE <= '1';
- else
- -- other instructions --
- oMEM_WE <= '0';
- end if;
- end case;
- end process;
- PROCESS(iINSTR, iSIGN, iZERO,iCARRY)BEGIN
- CASE iINSTR(14 DOWNTO 9)IS
- WHEN "010000"=> JMP<='1';
- WHEN "010001"=> ---JMP IF ZERO
- IF(iZERO='1')THEN JMP<='1';
- ELSE JMP<='0';
- END IF;
- WHEN "010010"=> ---JMP IF SIGN
- IF(iSIGN='1')THEN JMP<='1';
- ELSE JMP<='0';
- END IF;
- WHEN "010011"=> ---JMP IF CARRY
- IF(iCARRY='1')THEN JMP<='1';
- ELSE JMP<='0';
- END IF;
- WHEN "010101"=> ---JMP IF NOT ZERO
- IF(iZERO='0')THEN JMP<='1';
- ELSE JMP<='0';
- END IF;
- WHEN "010110"=> ---JMP IF NOT SIGN
- IF(iSIGN='0')THEN JMP<='1';
- ELSE JMP<='0';
- END IF;
- WHEN "010111"=> ---JMP IF NOT CARRY
- IF(iCARRY='0')THEN JMP<='1';
- ELSE JMP<='0';
- END IF;
- WHEN OTHERS=> JMP<='0';
- END CASE;
- END PROCESS;
- -- port for LCD handler --
- oPHASE <= "00" when sSTATE = FETCH else
- "01" when sSTATE = DECODE else
- "10" when sSTATE = EXECUTE else
- "11" when sSTATE = WBACK else
- "00";
- end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement