Advertisement
Mikestriken

Main_ALU

Apr 7th, 2023 (edited)
1,896
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 4.01 KB | None | 0 0
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3. use IEEE.NUMERIC_STD.ALL;
  4.  
  5. entity main_alu is
  6.     port (
  7.         Left_Operand_Reg1     : in  std_logic_vector(31 downto 0); -- AKA register 1 output
  8.         Right_Operand_Reg2_Imm_Shamt  : in std_logic_vector(31 downto 0); -- AKA register 2/imm/shamt output
  9.         ALU_Control : in std_logic_vector(3 downto 0);
  10.        
  11.         Result  : out std_logic_vector(31 downto 0);
  12.         Z_Flag : out std_logic
  13.     );
  14. end entity main_alu;
  15.  
  16. architecture dataflow of main_alu is
  17.     -- Constants
  18.     constant ALU_AND : std_logic_vector(3 downto 0) := "0000";
  19.     constant ALU_OR : std_logic_vector(3 downto 0) := "0001";
  20.     constant ALU_ADD : std_logic_vector(3 downto 0) := "0010";
  21.     --constant ALU_XOR : std_logic_vector(3 downto 0) := "0011";
  22.     constant ALU_SUB : std_logic_vector(3 downto 0) := "0110";
  23.     constant ALU_PASS_Reg2_Imm_FLAGS : std_logic_vector(3 downto 0) := "0111";
  24.     constant ALU_PASS_Reg1_FLAGS : std_logic_vector(3 downto 0) := "1000";
  25.     --constant ALU_ADD_UNSIGNED : std_logic_vector(3 downto 0) := "1010";
  26.     --constant ALU_NOR : std_logic_vector(3 downto 0) := "1100";
  27.     constant ALU_SHIFT_LEFT : std_logic_vector(3 downto 0) := "1101";
  28.     --constant ALU_SUB_UNSIGNED : std_logic_vector(3 downto 0) := "1110";
  29.     constant ALU_SHIFT_RIGHT : std_logic_vector(3 downto 0) := "1111";
  30.    
  31.     -- Signals
  32.     signal Internal_Result : std_logic_vector(31 downto 0);
  33.     signal Internal_Z_Flag : std_logic;
  34. begin
  35.     -- Add result together
  36.     with ALU_Control select
  37.         Internal_Result <= Left_Operand_Reg1 and Right_Operand_Reg2_Imm_Shamt when ALU_AND, -- AND
  38.            
  39.                            Left_Operand_Reg1 or Right_Operand_Reg2_Imm_Shamt when ALU_OR, -- OR
  40.                          
  41.                            std_logic_vector(signed(Left_Operand_Reg1) + signed(Right_Operand_Reg2_Imm_Shamt)) when ALU_ADD, -- add
  42.                          
  43.                            --Left_Operand_Reg1 xor Right_Operand_Reg2_Imm_Shamt when ALU_XOR, -- XOR, not used
  44.                          
  45.                            std_logic_vector(signed(Left_Operand_Reg1) - signed(Right_Operand_Reg2_Imm_Shamt)) when ALU_SUB, -- subtract
  46.                          
  47.                            Left_Operand_Reg1 when ALU_PASS_Reg2_Imm_FLAGS, -- Pass Register 2/imm, Set Z Flag
  48.                          
  49.                            Right_Operand_Reg2_Imm_Shamt when ALU_PASS_Reg1_FLAGS, -- Pass Register 1
  50.                          
  51.                            --std_logic_vector(unsigned(Left_Operand_Reg1) + unsigned(Right_Operand_Reg2_Imm_Shamt)) when ALU_ADD_UNSIGNED, -- add unsigned, not used
  52.                          
  53.                            --not(Left_Operand_Reg1 or Right_Operand_Reg2_Imm_Shamt) when ALU_NOR, -- NOR, not used
  54.                          
  55.                            std_logic_vector(shift_left(signed(Left_Operand_Reg1), to_integer(unsigned(Right_Operand_Reg2_Imm_Shamt)))) when ALU_SHIFT_LEFT, -- Shift Left Logical
  56.                          
  57.                            --std_logic_vector(unsigned(Left_Operand_Reg1) - unsigned(Right_Operand_Reg2_Imm_Shamt)) when ALU_SUB_UNSIGNED, -- subtract unsigned, not used
  58.                          
  59.                            std_logic_vector(shift_right(signed(Left_Operand_Reg1), to_integer(unsigned(Right_Operand_Reg2_Imm_Shamt)))) when ALU_SHIFT_RIGHT, -- Shift Right Logical
  60.                          
  61.                            (others => 'X') when others;
  62.                  
  63.         -- Set Flags          
  64.         Internal_Z_Flag <= '1' when (ALU_Control = ALU_PASS_Reg2_Imm_FLAGS or ALU_Control = ALU_PASS_Reg1_FLAGS) and to_integer(signed(Internal_Result)) = 0 else
  65.                   '0' when (ALU_Control = ALU_PASS_Reg2_Imm_FLAGS or ALU_Control = ALU_PASS_Reg1_FLAGS) and to_integer(signed(Internal_Result)) /= 0
  66.                   else Internal_Z_Flag;
  67.                  
  68.        -- Final ALU_ADD
  69.        Result <= Internal_Result;
  70.        Z_Flag <= Internal_Z_Flag;
  71.        
  72. end architecture dataflow;
Tags: Main_ALU
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement