Advertisement
nex036ara

control_unit1

Jan 21st, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 6.91 KB | None | 0 0
  1.  
  2. ----------------------------------------------------------------------------------
  3. -- Logicko projektovanje racunarskih sistema 1
  4. -- 2011/2012
  5. -- Lab 7
  6. --
  7. -- Control unit
  8. --
  9. -- author: Branislav Nikolic,e13592
  10. ----------------------------------------------------------------------------------
  11.  
  12. library IEEE;
  13. use IEEE.STD_LOGIC_1164.ALL;
  14. use IEEE.STD_LOGIC_UNSIGNED.ALL;
  15.  
  16. entity control_unit is
  17.     Port ( iCLK : in  STD_LOGIC;
  18.            inRST : in  STD_LOGIC;
  19.            iINSTR : in  STD_LOGIC_VECTOR (14 downto 0);
  20.            iSIGN : in  STD_LOGIC;
  21.            iZERO : in  STD_LOGIC;
  22.            iCARRY : in  STD_LOGIC;
  23.            oREG_WE : out  STD_LOGIC_VECTOR (7 downto 0);
  24.            oA_WE : out  STD_LOGIC;
  25.            oB_WE : out  STD_LOGIC;
  26.            oC_WE : out  STD_LOGIC;
  27.            oIR_WE : out  STD_LOGIC;
  28.            oPC_IN : out  STD_LOGIC_VECTOR (15 downto 0);
  29.            oPC_EN : out  STD_LOGIC;
  30.            oPC_LOAD : out  STD_LOGIC;
  31.            oMUXA_SEL : out  STD_LOGIC_VECTOR (3 downto 0);
  32.            oMUXB_SEL : out  STD_LOGIC_VECTOR (3 downto 0);
  33.            oALU_SEL : out  STD_LOGIC_VECTOR (3 downto 0);
  34.            oMEM_WE : out  STD_LOGIC;
  35.            oPHASE : out  STD_LOGIC_VECTOR (1 downto 0));
  36. end control_unit;
  37.  
  38. architecture Behavioral of control_unit is
  39.  
  40.     type tSTATES is (FETCH, DECODE, EXECUTE, WBACK);
  41.     signal sSTATE : tSTATES;
  42.      signal JMP: std_logic;
  43.  
  44. begin
  45.  
  46.     -- Main FSM --
  47.     process (iCLK, inRST) begin
  48.         if (inRST = '0') then
  49.             sSTATE <= FETCH;
  50.         elsif (iCLK'event and iCLK = '1') then
  51.             case (sSTATE) is
  52.                 when FETCH =>
  53.                     sSTATE <= DECODE;
  54.                 when DECODE =>
  55.                     sSTATE <= EXECUTE;
  56.                 when EXECUTE =>
  57.                     sSTATE <= WBACK;
  58.                 when WBACK =>
  59.                     sSTATE <= FETCH;
  60.             end case;
  61.         end if;
  62.     end process;
  63.    
  64.     -- Output function --
  65.     process (iINSTR, sSTATE, iZERO, iSIGN, iCARRY) begin
  66.         case (sSTATE) is
  67.        
  68.             -- FETCH phase, instruction is written in IR
  69.             when FETCH =>
  70.                 oREG_WE <= "00000000";
  71.                 oA_WE <= '0';
  72.                 oB_WE <= '0';
  73.                 oC_WE <= '0';
  74.                 oIR_WE <= '1';
  75.                 oPC_IN <= "0000000000000000";
  76.                 oPC_EN <= '0';
  77.                 oPC_LOAD <= '0';
  78.                 oMUXA_SEL <= "0000";
  79.                 oMUXB_SEL <= "0000";
  80.                 oALU_SEL <= "0000";
  81.                 oMEM_WE <= '0';
  82.                
  83.             -- DECODE phase, operands are written in A and B, MUX's select operands
  84.             when DECODE =>
  85.                 oREG_WE <= "00000000";
  86.                 oA_WE <= '1';
  87.                 oB_WE <= '1';
  88.                 oC_WE <= '0';
  89.                 oIR_WE <= '0';
  90.                 oPC_IN <= "0000000000000000";
  91.                 oPC_EN <= '0';
  92.                 oPC_LOAD <= '0';
  93.                 if (iINSTR(14 downto 13) = "10") then
  94.                     -- LOAD --
  95.                     oMUXA_SEL <= "1000";
  96.                 else
  97.                     -- other instructions --
  98.                     oMUXA_SEL <= '0' & iINSTR(5 downto 3);
  99.                 end if;
  100.                 oMUXB_SEL <= '0' & iINSTR(2 downto 0);
  101.                 oALU_SEL <= "0000";
  102.                 oMEM_WE <= '0';
  103.                
  104.             -- EXECUTE phase, ALU performs operation and result is written in C
  105.             when EXECUTE =>
  106.                 if (JMP='1')then oC_WE <='0';
  107.                             else
  108.                                 oC_WE <= '1';
  109.                      end if;
  110.                      
  111.                      oREG_WE <= "00000000";
  112.                 oA_WE <= '0';
  113.                 oB_WE <= '0';
  114.              
  115.            
  116.                 oIR_WE <= '0';
  117.                 oPC_IN <= "0000000000000000";
  118.                 oPC_EN <= '0';
  119.                 oPC_LOAD <= '0';
  120.                 oMUXA_SEL <= "0000";
  121.                 oMUXB_SEL <= '0' & iINSTR(2 downto 0);  -- kept for STORE instruction --
  122.                 oALU_SEL <= iINSTR(12 downto 9);
  123.                 oMEM_WE <= '0';
  124.                
  125.             -- WRITE_BACK phase, result is written in register or memory
  126.             when WBACK =>
  127.                     if (JMP='1')then
  128.                         oREG_WE <= "00000000";
  129.                         oPC_IN <="0000000" &iINSTR(8 downto 0);
  130.                         oPC_LOAD <= '1';
  131.                    
  132.                    
  133.                 elsif (iINSTR(14 downto 13) /= "11") then
  134.                     -- all instructions except STORE --
  135.                     case (iINSTR(8 downto 6)) is
  136.                         when "000" => oREG_WE <= "00000001";
  137.                         when "001" => oREG_WE <= "00000010";
  138.                         when "010" => oREG_WE <= "00000100";
  139.                         when "011" => oREG_WE <= "00001000";
  140.                         when "100" => oREG_WE <= "00010000";
  141.                         when "101" => oREG_WE <= "00100000";
  142.                         when "110" => oREG_WE <= "01000000";
  143.                         when "111" => oREG_WE <= "10000000";
  144.                         when others => oREG_WE <= "00000000";
  145.                     end case;
  146.                 else
  147.                     -- STORE--
  148.                     oREG_WE <= "00000000";
  149.                 end if;
  150.                 oA_WE <= '0';
  151.                 oB_WE <= '0';
  152.                 oC_WE <= '0';
  153.                 oIR_WE <= '0';
  154.                      oPC_IN <= "0000000000000000";
  155.                      oPC_LOAD <= '0';
  156.                      oPC_EN <= '1';
  157.                      oMUXA_SEL <= "0000";
  158.                 oMUXB_SEL <= '0' & iINSTR(2 downto 0);  -- kept for STORE instruction --
  159.                 oALU_SEL <= "0000";
  160.                 if (iINSTR(14 downto 13) = "11") then
  161.                     -- STORE --
  162.                     oMEM_WE <= '1';
  163.                 else
  164.                     -- other instructions --
  165.                     oMEM_WE <= '0';
  166.                 end if;
  167.         end case;
  168.     end process;
  169.    
  170.      
  171.      PROCESS(iINSTR, iSIGN, iZERO,iCARRY)BEGIN
  172.             CASE iINSTR(14 DOWNTO 9)IS
  173.            
  174.            
  175.             WHEN "010000"=> JMP<='1';
  176.                    
  177.             WHEN "010001"=>   ---JMP IF ZERO
  178.                         IF(iZERO='1')THEN JMP<='1';
  179.                             ELSE JMP<='0';
  180.                         END IF;
  181.                    
  182.             WHEN "010010"=> ---JMP IF SIGN
  183.                         IF(iSIGN='1')THEN JMP<='1';
  184.                             ELSE JMP<='0';
  185.                         END IF;
  186.             WHEN "010011"=> ---JMP IF CARRY
  187.                         IF(iCARRY='1')THEN JMP<='1';
  188.                             ELSE JMP<='0';
  189.                         END IF;
  190.             WHEN "010101"=> ---JMP IF NOT ZERO
  191.                         IF(iZERO='0')THEN JMP<='1';
  192.                             ELSE JMP<='0';
  193.                         END IF;
  194.  
  195.             WHEN "010110"=> ---JMP IF NOT SIGN
  196.                         IF(iSIGN='0')THEN JMP<='1';
  197.                             ELSE JMP<='0';
  198.                         END IF;
  199.                    
  200.             WHEN "010111"=> ---JMP IF NOT CARRY
  201.                         IF(iCARRY='0')THEN JMP<='1';
  202.                                 ELSE JMP<='0';
  203.                         END IF;
  204.             WHEN OTHERS=> JMP<='0';
  205. END CASE;
  206.     END PROCESS;                           
  207.                                
  208.                                
  209.     -- port for LCD handler --
  210.     oPHASE <= "00" when sSTATE = FETCH else
  211.               "01" when sSTATE = DECODE else
  212.               "10" when sSTATE = EXECUTE else
  213.               "11" when sSTATE = WBACK else
  214.               "00";
  215.  
  216. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement