LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
--
-- code | ALU operation
--------+------------------------
-- 000 | addition without carry
-- 001 | addition with carry
-- 010 | subtract without carry
-- 011 | subtract with carry
-- 100 | bitwise OR
-- 101 | bitwise AND
-- 110 | compare (subtract without carry)
-- 111 | bitwise XOR
--
ENTITY alu IS
GENERIC(N: natural := 8);
PORT(code: IN std_logic_vector( 2 DOWNTO 0); -- function code
op1: IN std_logic_vector(N-1 DOWNTO 0); -- left operand
op2: IN std_logic_vector(N-1 DOWNTO 0); -- right operand
cin: IN std_logic; -- carry input
res: OUT std_logic_vector(N-1 DOWNTO 0); -- result output
cout: OUT std_logic); -- carry output
END alu;
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
USE ieee.std_logic_unsigned.ALL;
ARCHITECTURE behaviour OF alu IS
BEGIN
process(op1,op2,code, cin)
variable temp: std_logic_vector(N DOWNTO 0); -- höchstes bit als cout
begin
case code is
when "000" => -- add without carry
temp := op1 + op2;
when "001" => -- add with carry
temp := op1 + op2 + cin;
when "010" => -- sub without carry
temp := op1 - op2;
when "011" => -- sub with carry
temp := op1 - op2 - cin;
when "100" => -- bitwise OR
temp := op1 or op2;
when "101" => -- bitwise AND
temp := op1 and op2;
when "110" => -- compare (subtract without carry)
temp := op1 - op2; -- wenn temp = 0 ohne carry dann sind operanden gleich?!
when "111" => -- bitwise XOR
temp := op1 xor op2;
when others => -- eig nicht möglich
--temp := op1 - b;
end case;
cout <= temp(N); -- übertrag ausgeben
res <= temp(N-1 downto 0); -- ergebnis ausgeben
end process;
END behaviour;