No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ALU.vhd 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ----------------------------------------------------------------------------------
  2. -- Company:
  3. -- Engineer:
  4. --
  5. -- Create Date: 13.04.2021 10:07:41
  6. -- Design Name:
  7. -- Module Name: ALU - Behavioral
  8. -- Project Name:
  9. -- Target Devices:
  10. -- Tool Versions:
  11. -- Description:
  12. --
  13. -- Dependencies:
  14. --
  15. -- Revision:
  16. -- Revision 0.01 - File Created
  17. -- Additional Comments:
  18. --
  19. ----------------------------------------------------------------------------------
  20. library IEEE;
  21. use IEEE.STD_LOGIC_1164.ALL;
  22. use IEEE.STD_LOGIC_UNSIGNED.ALL;
  23. -- Uncomment the following library declaration if using
  24. -- arithmetic functions with Signed or Unsigned values
  25. use IEEE.NUMERIC_STD.ALL;
  26. -- Uncomment the following library declaration if instantiating
  27. -- any Xilinx leaf cells in this code.
  28. --library UNISIM;
  29. --use UNISIM.VComponents.all;
  30. entity ALU is
  31. Generic (Nb_bits : Natural);
  32. Port ( A : in STD_LOGIC_VECTOR (Nb_bits-1 downto 0);
  33. B : in STD_LOGIC_VECTOR (Nb_bits-1 downto 0);
  34. OP : in STD_LOGIC_VECTOR (2 downto 0);
  35. S : out STD_LOGIC_VECTOR (Nb_bits-1 downto 0);
  36. N : out STD_LOGIC;
  37. O : out STD_LOGIC;
  38. Z : out STD_LOGIC;
  39. C : out STD_LOGIC);
  40. end ALU;
  41. architecture Behavioral of ALU is
  42. signal A9 : STD_LOGIC_VECTOR (Nb_bits downto 0);
  43. signal B9 : STD_LOGIC_VECTOR (Nb_bits downto 0);
  44. signal ADD : STD_LOGIC_VECTOR (Nb_bits downto 0);
  45. signal SUB : STD_LOGIC_VECTOR (Nb_bits downto 0);
  46. signal MUL : STD_LOGIC_VECTOR ((2*Nb_bits)-1 downto 0);
  47. signal intern_N : STD_LOGIC;
  48. signal intern_Z : STD_LOGIC;
  49. constant ZERO_N : STD_LOGIC_VECTOR (Nb_bits downto 0) := (others => '0');
  50. constant ZERO_N1 : STD_LOGIC_VECTOR (Nb_bits downto 0) := (others => '0');
  51. begin
  52. A9 <= '0' & A;
  53. B9 <= '0' & B;
  54. ADD <= A9 + B9;
  55. SUB <= A9 - B9;
  56. MUL <= A * B;
  57. S <= ADD (Nb_bits-1 downto 0) when OP = "001" else
  58. SUB (Nb_bits-1 downto 0) when OP = "010" else
  59. MUL (Nb_bits-1 downto 0) when OP = "011" else
  60. -- Add division
  61. (0 => intern_N, others => '0') when OP = "101" else
  62. (0 => '1', others => '0') when OP = "110" and intern_Z = '0' and intern_N = '0' else
  63. (0 => intern_Z, others => '0') when OP = "111" else
  64. (others => '0');
  65. intern_N <= SUB (Nb_bits);
  66. intern_Z <= '1' when (SUB = ZERO_N1) else
  67. '0';
  68. N <= intern_N;
  69. O <= '0' when (MUL ((2*Nb_bits)-1 downto Nb_bits) = ZERO_N) else
  70. '1';
  71. Z <= intern_Z;
  72. C <= ADD (Nb_bits);
  73. end Behavioral;