library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; -- This is a simple 16-bit ALU which supports 13 operations. -- Below is a mapping between opcodes and operations. -- 'a' and 'b' are the two inputs to the ALU. -- Opcodes are in hexadecimal. -- -- 0: a + b (addition) -- 1: a - b (subtraction) -- 2: a * b (multiplication) -- 3: a / b (division) -- 4: ++a (increment) -- 5: --a (decrement) -- 6: ~a (bitwise NOT) -- 7: a & b (bitwise AND) -- 8: a ^ b (bitwise XOR) -- 9: a >> b (shift right with zero padding) -- A: a << b (shift left) -- B: a ror b (rotate right) -- C: a rol b (rotate left) -- D, E, F: 0 (always zero) entity ALU is port( a, b: in unsigned(15 downto 0); -- two input numbers ctrl: in unsigned(3 downto 0); -- opcode r: out unsigned(15 downto 0)); -- result end ALU; architecture ALU_impl of ALU is begin with ctrl select r <= a + b when X"0", -- addition a - b when X"1", -- subtraction resize(a * b, 16) when X"2", -- multiplication a / b when X"3", -- division a + 1 when X"4", -- increment a - 1 when X"5", -- decrement not a when X"6", -- bitwise NOT a and b when X"7", -- bitwise AND a xor b when X"8", -- bitwise XOR a srl to_integer(b) when X"9", -- shift right with zero padding a sll to_integer(b) when X"A", -- shift left a ror to_integer(b) when X"B", -- rotate right a rol to_integer(b) when X"C", -- rotate left X"0000" when others; end ALU_impl;