alu.vhd 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. use work.mem_pkg.all;
  5. use work.core_pkg.all;
  6. use work.op_pkg.all;
  7. -- ATTENTION: zero flag is only valid on SUB and SLT(U)
  8. entity alu is
  9. port (
  10. op : in alu_op_type;
  11. A, B : in data_type;
  12. R : out data_type := (others => '0');
  13. Z : out std_logic := '0'
  14. );
  15. end alu;
  16. architecture rtl of alu is
  17. begin
  18. opcomp : process(all)
  19. begin
  20. Z <= '-';
  21. case op is
  22. when ALU_NOP =>
  23. R <= B;
  24. when ALU_SLT =>
  25. if signed(A) < signed(B) then
  26. R <= std_logic_vector(to_unsigned(1, R'LENGTH));
  27. else
  28. R <= std_logic_vector(to_unsigned(0, R'LENGTH));
  29. end if;
  30. Z <= not R(0);
  31. when ALU_SLTU =>
  32. if unsigned(A) < unsigned(B) then
  33. R <= std_logic_vector(to_unsigned(1, R'LENGTH));
  34. else
  35. R <= std_logic_vector(to_unsigned(0, R'LENGTH));
  36. end if;
  37. Z <= not R(0);
  38. when ALU_SLL =>
  39. R <= std_logic_vector(shift_left(unsigned(A), to_integer(unsigned(B(4 downto 0)))));
  40. when ALU_SRL =>
  41. R <= std_logic_vector(shift_right(unsigned(A), to_integer(unsigned(B(4 downto 0)))));
  42. when ALU_SRA =>
  43. R <= std_logic_vector(shift_right(signed(A), to_integer(unsigned(B(4 downto 0)))));
  44. when ALU_ADD =>
  45. R <= std_logic_vector(signed(A) + signed(B));
  46. when ALU_SUB =>
  47. R <= std_logic_vector(signed(A) - signed(B));
  48. if A = B then Z <= '1';
  49. else Z <= '0';
  50. end if;
  51. when ALU_AND =>
  52. R <= A and B;
  53. when ALU_OR =>
  54. R <= A or B;
  55. when ALU_XOR =>
  56. R <= A xor B;
  57. end case;
  58. end process;
  59. end architecture;