www.pudn.com > project_2.zip > real_add.vhd, change:2016-01-05,size:14397b


library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity real_add is
    Port (clk, en: in std_logic; cin1, cin2: in std_logic_vector(31 downto 0); c_en: out std_logic; cout: out std_logic_vector(31 downto 0));
end real_add;

architecture Behavioral of real_add is
signal state: std_logic_vector(1 downto 0);
begin
    p1: process(clk, en)
    variable tmp: std_logic;
    begin
--        if(en'event and en='1')then
--            state<="01";
--        elsif(clk'event and clk='1' and state/="00")then
--            state<=state+1;
--        end if;
        if(clk'event and clk='1')then
            if(en='1' and tmp/='1')then
                state<="01";
            elsif(state/="00")then
                state<=state+1;
            end if;
            tmp:=en;
        end if;
    end process p1;
    
    p2: process(state)
    variable v0, v1, v2: std_logic_vector(7 downto 0);
    variable v3, j: integer;
    variable v4, v5, v6: std_logic_vector(24 downto 0);
    begin
        case state is
            when "01"=>
                v1:=cin1(30 downto 23);
                v2:=cin2(30 downto 23);
                v4(24 downto 23):="01";
                v4(22 downto 0):=cin1(22 downto 0);
                v5(24 downto 23):="01";
                v5(22 downto 0):=cin2(22 downto 0);
                if(v1>v2)then
                    v0:=v1;
                    v3:=conv_integer(v1-v2);
                    if(v3>23)then
                        v5:=(others=>'0');
                    elsif(v3=1)then
                        v5(22 downto 0):=v5(23 downto 1);
                        v5(23):='0';
                    elsif(v3=2)then
                        v5(21 downto 0):=v5(23 downto 2);
                        v5(23 downto 22):=(others=>'0');
                    elsif(v3=3)then
                        v5(20 downto 0):=v5(23 downto 3);
                        v5(23 downto 21):=(others=>'0');
                    elsif(v3=4)then
                        v5(19 downto 0):=v5(23 downto 4);
                        v5(23 downto 20):=(others=>'0');
                    elsif(v3=5)then
                        v5(18 downto 0):=v5(23 downto 5);
                        v5(23 downto 19):=(others=>'0');
                    elsif(v3=6)then
                        v5(17 downto 0):=v5(23 downto 6);
                        v5(23 downto 18):=(others=>'0');
                    elsif(v3=7)then
                        v5(16 downto 0):=v5(23 downto 7);
                        v5(23 downto 17):=(others=>'0');
                    elsif(v3=8)then
                        v5(15 downto 0):=v5(23 downto 8);
                        v5(23 downto 16):=(others=>'0');
                    elsif(v3=9)then
                        v5(14 downto 0):=v5(23 downto 9);
                        v5(23 downto 15):=(others=>'0');
                    elsif(v3=10)then
                        v5(13 downto 0):=v5(23 downto 10);
                        v5(23 downto 14):=(others=>'0');
                    elsif(v3=11)then
                        v5(12 downto 0):=v5(23 downto 11);
                        v5(23 downto 13):=(others=>'0');
                    elsif(v3=12)then
                        v5(11 downto 0):=v5(23 downto 12);
                        v5(23 downto 12):=(others=>'0');
                    elsif(v3=13)then
                        v5(10 downto 0):=v5(23 downto 13);
                        v5(23 downto 11):=(others=>'0');
                    elsif(v3=14)then
                        v5(9 downto 0):=v5(23 downto 14);
                        v5(23 downto 10):=(others=>'0');
                    elsif(v3=15)then
                        v5(8 downto 0):=v5(23 downto 15);
                        v5(23 downto 9):=(others=>'0');
                    elsif(v3=16)then
                        v5(7 downto 0):=v5(23 downto 16);
                        v5(23 downto 8):=(others=>'0');
                    elsif(v3=17)then
                        v5(6 downto 0):=v5(23 downto 17);
                        v5(23 downto 7):=(others=>'0');
                    elsif(v3=18)then
                        v5(5 downto 0):=v5(23 downto 18);
                        v5(23 downto 6):=(others=>'0');
                    elsif(v3=19)then
                        v5(4 downto 0):=v5(23 downto 19);
                        v5(23 downto 5):=(others=>'0');
                  elsif(v3=20)then
                        v5(3 downto 0):=v5(23 downto 20);
                        v5(23 downto 4):=(others=>'0');
                   elsif(v3=21)then
                        v5(2 downto 0):=v5(23 downto 21);
                        v5(23 downto 3):=(others=>'0');
                   elsif(v3=22)then
                        v5(1 downto 0):=v5(23 downto 22);
                        v5(23 downto 2):=(others=>'0');
                   elsif(v3=23)then
                        v5(0):=v5(23);
                        v5(23 downto 1):=(others=>'0');
                   end if;
--                    else
--                        v5(23-v3 downto 0):=v5(23 downto v3);
--                        v5(23 downto 23-v3+1):=(others => '0');
                   -- end if;
                elsif(v1<v2)then
                    v0:=v2;
                    v3:=conv_integer(v2-v1);
                    if(v3>23)then
                        v4:=(others=>'0');
                    elsif(v3=1)then
                        v4(22 downto 0):=v4(23 downto 1);
                        v4(23):='0';
                    elsif(v3=2)then
                        v4(21 downto 0):=v4(23 downto 2);
                        v4(23 downto 22):=(others=>'0');
                    elsif(v3=3)then
                        v4(20 downto 0):=v4(23 downto 3);
                        v4(23 downto 21):=(others=>'0');
                    elsif(v3=4)then
                        v4(19 downto 0):=v4(23 downto 4);
                        v4(23 downto 20):=(others=>'0');
                    elsif(v3=5)then
                        v4(18 downto 0):=v4(23 downto 5);
                        v4(23 downto 19):=(others=>'0');
                    elsif(v3=6)then
                        v4(17 downto 0):=v4(23 downto 6);
                        v4(23 downto 18):=(others=>'0');
                    elsif(v3=7)then
                        v4(16 downto 0):=v4(23 downto 7);
                        v4(23 downto 17):=(others=>'0');
                    elsif(v3=8)then
                        v4(15 downto 0):=v4(23 downto 8);
                        v4(23 downto 16):=(others=>'0');
                    elsif(v3=9)then
                        v4(14 downto 0):=v4(23 downto 9);
                        v4(23 downto 15):=(others=>'0');
                    elsif(v3=10)then
                        v4(13 downto 0):=v4(23 downto 10);
                        v4(23 downto 14):=(others=>'0');
                    elsif(v3=11)then
                        v4(12 downto 0):=v4(23 downto 11);
                        v4(23 downto 13):=(others=>'0');
                    elsif(v3=12)then
                        v4(11 downto 0):=v4(23 downto 12);
                        v4(23 downto 12):=(others=>'0');
                    elsif(v3=13)then
                        v4(10 downto 0):=v4(23 downto 13);
                        v4(23 downto 11):=(others=>'0');
                    elsif(v3=14)then
                        v4(9 downto 0):=v4(23 downto 14);
                        v4(23 downto 10):=(others=>'0');
                    elsif(v3=15)then
                        v4(8 downto 0):=v4(23 downto 15);
                        v4(23 downto 9):=(others=>'0');
                    elsif(v3=16)then
                        v4(7 downto 0):=v4(23 downto 16);
                        v4(23 downto 8):=(others=>'0');
                    elsif(v3=17)then
                        v4(6 downto 0):=v4(23 downto 17);
                        v4(23 downto 7):=(others=>'0');
                    elsif(v3=18)then
                        v4(5 downto 0):=v4(23 downto 18);
                        v4(23 downto 6):=(others=>'0');
                    elsif(v3=19)then
                        v4(4 downto 0):=v4(23 downto 19);
                        v4(23 downto 5):=(others=>'0');
                    elsif(v3=20)then
                        v4(3 downto 0):=v4(23 downto 20);
                        v4(23 downto 4):=(others=>'0');
                    elsif(v3=21)then
                        v4(2 downto 0):=v4(23 downto 21);
                        v4(23 downto 3):=(others=>'0');
                    elsif(v3=22)then
                        v4(1 downto 0):=v4(23 downto 22);
                        v4(23 downto 2):=(others=>'0');
                    elsif(v3=23)then
                        v4(0):=v4(23);
                        v4(23 downto 1):=(others=>'0');
                    end if;
--                    else
--                        v4(23-v3 downto 0):=v4(23 downto v3);
--                        v4(23 downto 23-v3+1):=(others => '0');
                    --end if;
                else
                    v0:=v1;
                end if;
                c_en<='0';
            when "10"=>
                if(cin1(31)=cin2(31))then
                    v6:=v4+v5;
                    if(v6(24)='1')then
                        v0:=v0+1;
                        cout(22 downto 0)<=v6(23 downto 1);
                    else
                        cout(22 downto 0)<=v6(22 downto 0);
                    end if;
                    cout(31)<=cin1(31);
                    cout(30 downto 23)<=v0;
                else
                    if(v4>v5)then
                        v6:=v4-v5;
                        cout(31)<=cin1(31);
                    elsif(v4<v5)then
                        v6:=v5-v4;
                        cout(31)<=cin2(31);
                    end if;
                    
                    if(v5/=v4)then
                        j:=23;
                        while j>0 and v6(j)/='1' and v0/="00000000" loop
                            v0:=v0-1;
                            j:=j-1;
                        end loop;
                        case j is
                        when 1=>
                            cout(22)<=v6(0);
                            cout(21 downto 0)<=(others=>'0');
                        when 2=>
                            cout(22 downto 21)<=v6(1 downto 0);
                            cout(20 downto 0)<=(others=>'0');
                        when 3=>
                            cout(22 downto 20)<=v6(2 downto 0);
                            cout(19 downto 0)<=(others=>'0');
                        when 4=>
                            cout(22 downto 19)<=v6(3 downto 0);
                            cout(18 downto 0)<=(others=>'0');
                        when 5=>
                            cout(22 downto 18)<=v6(4 downto 0);
                            cout(17 downto 0)<=(others=>'0');
                        when 6=>
                            cout(22 downto 17)<=v6(5 downto 0);
                            cout(16 downto 0)<=(others=>'0');
                        when 7=>
                            cout(22 downto 16)<=v6(6 downto 0);
                            cout(15 downto 0)<=(others=>'0');
                        when 8=>
                            cout(22 downto 15)<=v6(7 downto 0);
                            cout(14 downto 0)<=(others=>'0');
                            
                        when 9=>
                            cout(22 downto 14)<=v6(8 downto 0);
                            cout(13 downto 0)<=(others=>'0');
                        when 10=>
                            cout(22 downto 13)<=v6(9 downto 0);
                            cout(12 downto 0)<=(others=>'0');
                        when 11=>
                            cout(22 downto 12)<=v6(10 downto 0);
                            cout(11 downto 0)<=(others=>'0');
                        when 12=>
                            cout(22 downto 11)<=v6(11 downto 0);
                            cout(10 downto 0)<=(others=>'0');
                       when 13=>
                            cout(22 downto 10)<=v6(12 downto 0);
                            cout(9 downto 0)<=(others=>'0');
                        when 14=>
                            cout(22 downto 9)<=v6(13 downto 0);
                            cout(8 downto 0)<=(others=>'0');
                            
                        when 15=>
                            cout(22 downto 8)<=v6(14 downto 0);
                            cout(7 downto 0)<=(others=>'0');
                        when 16=>
                            cout(22 downto 7)<=v6(15 downto 0);
                            cout(6 downto 0)<=(others=>'0');
                        when 17=>
                            cout(22 downto 6)<=v6(16 downto 0);
                            cout(5 downto 0)<=(others=>'0');
                       when 18=>
                            cout(22 downto 5)<=v6(17 downto 0);
                            cout(4 downto 0)<=(others=>'0');
                       when 19=>
                            cout(22 downto 4)<=v6(18 downto 0);
                            cout(3 downto 0)<=(others=>'0');
                            
                        when 20=>
                            cout(22 downto 3)<=v6(19 downto 0);
                            cout(2 downto 0)<=(others=>'0');
                        when 21=>
                           cout(22 downto 2)<=v6(20 downto 0);
                           cout(1 downto 0)<=(others=>'0');
                       when 22=>
                           cout(22 downto 1)<=v6(21 downto 0);
                           cout(0)<='0';
                       when 23=>
                           cout(22 downto 0)<=v6(22 downto 0);
                       when others=>
                       end case;
--                        cout(22 downto 23-j)<=v6(j-1 downto 0);
--                      cout(22-j downto 0)<=(others=>'0');
                       cout(30 downto 23)<=v0;
                    else
                        cout(22 downto 0)<=(others=>'0');
                        cout(30 downto 23)<="00000000";
                        cout(31)<='0';
                    end if;
                end if;
                c_en<='1';
            when "11"=>
                c_en<='0';
            when others=>
                c_en<='0';
        end case;
    end process p2;

end Behavioral;