Trying to build a full adder using the theory found on wikipedia.
ie.
Sum = A XOR B XOR Cin Cout = A AND B OR C AND (A XOR B)
A and B are 32 bit
Here's my code
--Addition
for i in 0 to 31 loop
temp(i) <= a(i) XOR b(i) XOR cin;
if (((a(i) AND b(i)) OR (cin AND (a(i) XOR b(i)))) = '1') then
cin <= '1';
else
cin <= '0';
end if;
end loop;
cout <= cin;
When I simulate this, the cin is ignored. For example, 1011 + 0100 will give the right answer but 0001+0001 = 0.
Full CODE:
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
USE ieee.std_logic_arith.ALL;
USE ieee.std_logic_unsigned.ALL;
ENTITY alu IS
PORT(
a: IN STD_LOGIC_VECTOR(31 DOWNTO 0);
b : IN STD_LOGIC_VECTOR(31 DOWNTO 0);
op : IN STD_LOGIC_VECTOR( 2 DOWNTO 0);
result : OUT STD_LOGIC_VECTOR(31 DOWNTO 0);
cout : OUT STD_LOGIC;
zero : OUT STD_LOGIC);
END alu;
--CIN is being inored for some reason. Must figure that out, and then code will start working.
ARCHITECTURE description OF alu IS
signal temp,cin :STD_LOGIC_VECTOR(31 DOWNTO 0);
BEGIN
process(a,b,op)
begin
if (op = "000") then
temp <= (a AND b);
elsif (op = "001") then
temp <= (a OR b);
elsif (op = "010") then
--Addition
for i in 0 to 31 loop
temp(i) <= a(i) XOR b(i) XOR cin(i);
cin(i+1) <= ( (a(i) AND b(i)) OR (cin AND (a(i) XOR b(i))) )
end loop;
cout <= cin(31);
elsif (op = "110") then
--Subtraction
elsif (op = "100") then
temp <= to_stdlogicvector(to_bitvector(a) sll 1);
elsif (op = "101") then
temp <= to_stdlogicvector(to_bitvector(a) srl 1);
end if;
end process;
result <= temp;
END description;