Forum Discussion
Altera_Forum
Honored Contributor
12 years ago --- Quote Start --- VHDL only does what you tell it: for i in 0 DOWNTO 31 loop will loop a grand total of 0 times. for i in 31 DOWNTO 0 loop will loop a grand total of 32 times Hence why the top code works, and bottom code doesnt. --- Quote End --- Yes, you are absolutely right. Thank you so much for all your wonderful help Tricky! I started from scratch and build one part at a time according to you suggestions and it actually worked out pretty well. I am not sure if my subtraction work right but I am out of time for this one. I figure out how to shift right base on your hint also, thanks for that. here is what I have including some sims.. functional and timing. The propagation on the add subtract are crazy... sims: (most cased shown) https://www.alteraforum.com/forum/attachment.php?attachmentid=8410 sims: (emphasis on add and subtract) https://www.alteraforum.com/forum/attachment.php?attachmentid=8411
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;
ARCHITECTURE description OF alu IS
BEGIN
PROCESS (op,a,b)
VARIABLE carry:STD_LOGIC;
VARIABLE X:STD_LOGIC_VECTOR(31 DOWNTO 0);
VARIABLE sum:STD_LOGIC_VECTOR(31 DOWNTO 0);
BEGIN
zero<='0';
cout<='0';
CASE op IS
WHEN "000" =>
sum:= a AND b;
result<=sum;
WHEN "001" =>
result <= a OR b;
WHEN "010" =>
--sum <= a + b;
carry:='0';
FOR i IN 0 TO 31 LOOP
sum(i):=a(i) XOR b(i) XOR carry;
carry:=(a(i) AND b(i))OR(b(i) AND carry)OR(carry AND a(i));
END LOOP;
result<=sum;
cout<=carry;
WHEN "110" =>
--sum <= a - b;
carry:='0';
X:=((NOT b) + 1);
FOR i IN 0 TO 31 LOOP
sum(i):=a(i) XOR X(i) XOR carry;
carry:=(a(i) AND X(i))OR(X(i) AND carry)OR(carry AND a(i));
END LOOP;
--sum:=((NOT sum)+1);
result<=sum;
cout<=carry;
WHEN "100" =>
--sum <= a << 1;
sum:=a(30 DOWNTO 0) & '0';
result<=sum;
WHEN "101" =>
--sum <= a >> 1;
sum:='0' & b (31 DOWNTO 1);
result<=sum;
WHEN OTHERS =>
END CASE;
IF sum = "00000000000000000000000000000000" THEN zero<='1';
END IF;
END PROCESS;
END description;
if you have any advice on the subtract part of the code it would be most welcome : ) more specifically I'm not sure if 5-6 works correctly.. its gives me FFFFFF it should be one with overflow set?? if I take twos compliment of that I will get it, but no overflow ofcourse... but when I tried that my add got messed up for some reason, so I just left it. yeahh.