VHDL: Multiplier works...except for one number! maybe its in my adder?

Discussion in 'Embedded Systems and Microcontrollers' started by Zeppelin1007, Oct 13, 2012.

  1. Zeppelin1007

    Thread Starter New Member

    Mar 6, 2012
    hey guys. Pulling my long beautiful hair out over this one

    My class was assigned to write an ALU using mostly binary arithmetic, rather VHDL's built in functions. kind of a pain but rather educational. We're given 2 3bit inputs, plus a 3 bit input for selecting the functions.

    So my ALU works. Except for one little tiny part on multiplication. Everything works beautifuly, except 7 x7. I dont get it.

    The top half of my code, checks to see bit of the input_B, and AND's that to bring it down. i set up my vectors to go X, Y, Z

    I would have preferred to simply read it as (Iput_A AND Input_B(0)) so on and so fourth, but this apparently isnt allowed? It errors out. Anyways. I "bypassed" that by using If Then.

    The loop/lines of crazy math are the adders. This is where im thinking i have an issue?


    Code ( (Unknown Language)):
    2.     101   --Input_A
    3.     011   --Input_B
    4.     --------
    5.     101  --X
    6.   1010  --Y
    7.  10100  --Z
    8. ---------------
    9. 001111 --Sum
    10. ^
    11. |
    12. Carry(0)
    Code ( (Unknown Language)):
    3.     function Multiplication (A_Input, B_Input: std_ulogic_Vector(2 downto 0)) Return Std_ulogic_vector is
    4.             Variable x, y, z: std_ulogic_vector(4 downto 0):="00000";
    5.             variable sum, carry: std_ulogic_vector(5 downto 0):="000000";
    7.         begin                        --For whatever reason, VHDL won't directly compare a bit in a vector, so we manually break it down
    8. --Compares the bits in B_Input and AND's A_Input down
    9.             if (B_Input(0) = '0') then
    10.                 x(2 downto 0):= ("000" AND A_Input);
    11.             else
    12.                 x(2 downto 0):= ("111" AND A_Input);
    13.             end if;
    15.             if (B_Input(1) = '0') then
    16.                 y(3 downto 1):= ("000" AND A_Input);
    17.             else
    18.                 y(3 downto 1):= ("111" AND A_Input);
    19.             end if;
    21.             if (B_Input(2) = '0') then
    22.                 z(4 downto 2):= ("000" AND A_Input);
    23.             else
    24.                 z(4 downto 2):= ("111" AND A_Input);
    25.             end if;
    28.             sum(0):=x(0); --Sets up bit(0) of the answer
    30.             for I in 1 to 4 loop --Finds bits 1 through 4 for the final result
    32.             sum(I):= x(I) xor y(I) xor z(I) xor carry(I-1);
    33.                 carry(I):= (x(I) and y(I)) or (y(I) and z(I)) or (z(I) and x(I)) or (carry(I-1) AND x(I))
    34.                 OR (carry(I-1) and y(I)) OR (carry(I-1) and z(I));
    35.             end loop;
    38.      sum(5):=carry(4);
    39.         return sum;
    40.         end Multiplication;
    But, this worked, until i enter 111 x 111

    SO i tried to break it down completely manually in case i had an incremenet wrong with the loop

    Code ( (Unknown Language)):
    2.   sum(0):=x(0);
    3.             sum(1):=x(1) xor y(1) xor z(1);
    4.           carry(1):=(x(1) and y(1)) OR (y(1) and z(1));
    6.            sum(2):=x(2) xor y(2) xor z(2) xor carry(1);
    7.            carry(2):=(x(2) and y(2)) OR (y(2) and z(2)) or (carry(1) and x(2)) or (carry(1) and y(2)) or (carry(1) and z(2));
    9.           sum(3):=x(3) xor y(3) xor z(3) xor carry(2);
    10.           carry(3):=(x(3) and y(3)) OR (y(3) and z(3)) or (carry(2) and x(3)) or (carry(2) and y(3)) or (carry(2) and z(3));
    12.           sum(4):=x(4) xor y(4) xor z(4) xor carry(3);
    13.          carry(4):=(x(4) and y(4)) OR (y(4) and z(4)) or (carry(3) and x(4)) or (carry(3) and y(4)) or (carry(3) and z(4));
    15. sum(5):= (x(4) and y(4)) or (y(4) and z(4)) or (x(4) and z(4)) or (carry(4) and x(4)) or (carry(4) and y(4)) or (carry(4) and z(4));
    Alas, same result. Everything works perfectly. EXCEPT the entering of 111x111. The out put i SHOULD get is 110001. What i get is 101001. Where did i botch up? obvously my 4th bit isnt carrying over on this number?

    So what am i doing wrong guys?

    I know not the cleanest and most efficient of code, but im tryin!

    Thanks in advance!