Advertisement
Randune1

ALU

Jan 12th, 2023 (edited)
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 2.14 KB | Source Code | 0 0
  1. entity ALU is
  2.     Port ( RdData1 : in  STD_LOGIC_VECTOR (15 downto 0);
  3.            RdData2 : in  STD_LOGIC_VECTOR (15 downto 0);
  4.               Clk : in STD_LOGIC;
  5.            ALUOP : in  STD_LOGIC_VECTOR (2 downto 0);
  6.            Y : out  STD_LOGIC_VECTOR (15 downto 0);
  7.               C_En : in STD_LOGIC;
  8.               Z_En : in STD_LOGIC;
  9.               OV_En : in STD_LOGIC;
  10.               N_En : in STD_LOGIC;
  11.               C : out STD_LOGIC;
  12.               Z : out STD_LOGIC;
  13.               OV : out STD_LOGIC;
  14.               N : out STD_LOGIC;
  15.               CarryIN : in STD_LOGIC;
  16.               SLlit : in STD_LOGIC_VECTOR (3 downto 0);
  17.               BTST_bit4 : in STD_LOGIC_VECTOR (3 downto 0)
  18.               );
  19. end ALU;
  20.  
  21. architecture Behavioral of ALU is
  22.     signal result   : std_logic_vector(16 downto 0);
  23.     signal zero     : std_logic;
  24.     signal overflow: std_logic;
  25.     signal null_word:std_logic_vector(16 downto 0);
  26.  
  27. begin
  28.  
  29.     null_word <= x"0000"&b"0";
  30.    
  31.     with ALUOP select
  32.         result <=   ((b"0"&RdData1) + (b"0"&RdData2)) when "000",
  33.                         (("0"&RdData1) + ("0"&(not RdData2)) + 1) when "001",
  34.                         ((b"0"&RdData1) and (b"0"&RdData2)) when "010",
  35.                         ((b"0"&RdData1) or (b"0"&RdData2)) when "011",
  36.                         (b"0"&RdData2(15 - conv_integer(SLlit) downto 0)&null_word(conv_integer(SLlit) - 1 downto 0)) when "100",
  37.                         (b"0"&RdData2 + 1) when "101",
  38.                         (b"0"&RdData2) + (b"0"&(not RdData1)) + 1 - (not CarryIN) when "110",
  39.                         x"0000"&b"0" when others;
  40.                        
  41.         Y <= result(15 downto 0);
  42.        
  43.         zero <= (not (RdData2(conv_integer(BTST_bit4)))) when ALUOP = "111" else
  44.                     '1' when result(15 downto 0) = x"0000"
  45.                     else '0';
  46.                    
  47.         Z <= zero when rising_edge(Clk) and Z_En = '1';
  48.        
  49.         N <= result(15) when rising_edge(Clk) and N_En = '1';
  50.        
  51.         overflow <= '1' when    ((ALUOP = "000" and RdData1(15) = RdData2(15) and result(15) = (not RdData1(15))) or
  52.                                          (ALUOP = "001" and RdData1(15) = (not RdData2(15)) and result(15) = RdData2(15)) or
  53.                                          (ALUOP = "101" and RdData2 = (not result)) or
  54.                                          (ALUOP = "110" and RdData1(15) = (not RdData2(15)) and result(15) = RdData1(15))) else
  55.                         '0';
  56.                        
  57.         OV <= overflow when rising_edge(Clk) and OV_En = '1';
  58.        
  59.         C <= result(16) when rising_edge(Clk) and C_En = '1';
  60. end Behavioral;
  61.  
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement