Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 24th, 2012  |  syntax: VHDL  |  size: 1.86 KB  |  hits: 31  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. LIBRARY ieee;
  3. USE ieee.std_logic_1164.ALL;
  4. --
  5. -- code | ALU operation
  6. --------+------------------------
  7. -- 000  | addition without carry
  8. -- 001  | addition with carry
  9. -- 010  | subtract without carry
  10. -- 011  | subtract with carry
  11. -- 100  | bitwise OR
  12. -- 101  | bitwise AND
  13. -- 110  | compare (subtract without carry)
  14. -- 111  | bitwise XOR
  15. --
  16.  
  17. ENTITY alu IS
  18.    GENERIC(N: natural := 8);
  19.    PORT(code: IN  std_logic_vector(  2 DOWNTO 0); -- function code
  20.         op1:  IN  std_logic_vector(N-1 DOWNTO 0); -- left operand
  21.         op2:  IN  std_logic_vector(N-1 DOWNTO 0); -- right operand
  22.         cin:  IN  std_logic;                      -- carry input
  23.         res:  OUT std_logic_vector(N-1 DOWNTO 0); -- result output
  24.         cout: OUT std_logic);                     -- carry output
  25. END alu;
  26.  
  27. LIBRARY ieee;
  28. USE ieee.std_logic_1164.ALL;
  29. USE ieee.std_logic_unsigned.ALL;
  30.  
  31. ARCHITECTURE behaviour OF alu IS
  32.  
  33. BEGIN
  34.  
  35. process(op1,op2,code, cin)
  36.         variable temp: std_logic_vector(N DOWNTO 0); -- höchstes bit als cout
  37.         begin
  38.                 case code is
  39.                         when "000" => -- add without carry
  40.                                 temp := op1 + op2;
  41.                                
  42.                                
  43.                         when "001" => -- add with carry
  44.                                 temp := op1 + op2 + cin;
  45.                                                
  46.                         when "010" => -- sub without carry
  47.                                 temp := op1 - op2;
  48.                        
  49.                         when "011" => -- sub with carry
  50.                                 temp := op1 - op2 - cin;
  51.                        
  52.                         when "100" => -- bitwise OR
  53.                                 temp := op1 or op2;
  54.                        
  55.                         when "101" => -- bitwise AND
  56.                                 temp := op1 and op2;
  57.                        
  58.                         when "110" => -- compare (subtract without carry)
  59.                                 temp := op1 - op2; -- wenn temp = 0 ohne carry dann sind operanden gleich?!
  60.                        
  61.                         when "111" => -- bitwise XOR
  62.                                 temp := op1 xor op2;
  63.                                                        
  64.                         when others => -- eig nicht möglich
  65.                                 --temp := op1 - b;
  66.                 end case;
  67.        
  68.         cout <= temp(N); -- übertrag ausgeben
  69.         res <= temp(N-1 downto 0); -- ergebnis ausgeben
  70.         end process;
  71.  
  72.  
  73.    
  74. END behaviour;