📄 fadd.vhd
字号:
package commonConstants is constant wordSize: integer := 32; constant adrLength: integer := 16;end package commonConstants;library ieee;use IEEE.STD_LOGIC_1164.ALL;use IEEE.STD_LOGIC_ARITH.ALL;use IEEE.STD_LOGIC_UNSIGNED.ALL;use work.commonConstants.all;-- FP Adder (32-bit: 1 bit sign, 8 bits exponent, 23 bits mantissa)-- positive = 0, negtive = 1package pack_CC_fun is function fadd (x : std_logic_vector(wordSize-1 downto 0); y : std_logic_vector(wordSize-1 downto 0) -- z : std_logic_vector (31 downto 0); -- testflag :out std_logic; -- ov: out std_logic ) return STD_LOGIC_VECTOR; end pack_CC_fun;package BODY pack_CC_fun is function fadd (x : std_logic_vector(wordSize-1 downto 0); y : std_logic_vector(wordSize-1 downto 0) -- z : std_logic_vector (31 downto 0); -- testflag :out std_logic; -- ov: out std_logic ) return STD_LOGIC_VECTOR isvariable z : std_logic_vector (wordSize-1 downto 0);-- process(x,y) -- variable declarationvariable m1,m2:std_logic_vector (47 downto 0); -- temporary storage of -- mantissa including implicit '1' variable tempm:std_logic_vector (47 downto 0); -- temporary storage of mantissa variable tempm2:std_logic_vector (93 downto 0);-- -- temporary storage of final mantissavariable m:std_logic_vector(22 downto 0); -- final mantissa resultvariable e:std_logic_vector (7 downto 0); -- resultant exponentvariable e1,e2:std_logic_vector(8 downto 0); -- temporary storage of exponentsvariable tempe:std_logic_vector(8 downto 0); -- temporary resultant exponentvariable s,xs,ys : std_logic; -- final sign bit,s and signs as and bs of a and bvariable bias:std_logic_vector(8 downto 0); -- exponent biasvariable i:integer;variable j:integer; begin m1 := "0000000000000000000000001" & x(22 downto 0); -- m1, mantissa of 'x' including implicit '1' m2 := "0000000000000000000000001" & y(22 downto 0); -- m2, mantissa of 'y' including implicit '1' e1 := '0' & x(30 downto 23); -- exponent of x to e1 (add in leading zero to -- e1 and e2 for extra precision and for -- overflow detection) e2 := '0' & y(30 downto 23); -- exponent of y to e2 xs := x(31); -- sign of x ys := y(31); -- sign of y --ov <= '0'; -- set overflow flag to cleared (no overflow, no error) tempm:="000000000000000000000000000000000000000000000000"; tempm2:="0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; -- clear mantissa for calculation tempe:="000000000"; -- clear exponent for calculation --********** add the mantissas **********-- if e1(8 downto 0) = "000000000" then if m1(22 downto 0) = "00000000000000000000000" then s := ys; e := e2(7 downto 0); m := m2(22 downto 0); else s := '0'; e := "00000000"; m := "00000000000000000000000"; end if; elsif e1(8 downto 0) = "011111111" then s := '0'; e := "11111111"; m := "00000000000000000000000"; --ov <= '1'; -- x error (NAN or infinity) else if e2(8 downto 0) = "000000000" then if m2(22 downto 0) = "00000000000000000000000" then s := xs; e := e1(7 downto 0); m := m1(22 downto 0); else s := '0'; e := "00000000"; m := "00000000000000000000000"; end if; elsif e2(8 downto 0) = "011111111" then s := '0'; e := "11111111"; m := "00000000000000000000000"; --ov <= '1'; -- y error else if (e1 = e2 and m1 = m2 ) then if xs /= ys then s := '0'; e := "00000000"; m := "00000000000000000000000"; --ov <= '0'; else tempm := m1 + m2; s := xs XOR ys; m := tempm(23 downto 1); e := e1(7 downto 0); e := e + "00000001"; --ov <= '0'; end if; else if xs = ys then if e1 = e2 then tempm := m1 + m2; s := xs XOR ys; m := tempm(23 downto 1); e := e1(7 downto 0); e := e + "00000001"; --ov <= '0'; elsif e1 > e2 then s := xs XOR ys; bias := e1 - e2; if bias > "00010111" then e := e1(7 downto 0); m := m1(22 downto 0); --ov <= '0'; -- y almost zero else i := 0; loop e2 := e2 + "000000001"; --------------------------------------- j := 0; loop m1(46 - j) := m1(45 - j); j := j + 1; exit when j = 46; end loop; m1(0) := '0'; ----------Shift Mantissa--------------- i := i + 1; exit when e1 = e2; end loop; tempm := m1 + m2; if tempm( 24 + i ) = '1' then e2 := e2 + "000000001"; m := tempm( 23 + i downto i + 1 ); e := e2(7 downto 0); --ov <= '0'; else m := tempm( 22 + i downto i ); e := e2(7 downto 0); --ov <= '0'; end if; end if; else s := xs XOR ys; bias := e2 - e1; if bias > "00010111" then e := e2(7 downto 0); m := m2(22 downto 0); --ov <= '0'; -- x almost zero else i := 0; loop e1 := e1 + "000000001"; --------------------------------------- j := 0; loop m2(46 - j) := m2(45 - j); j := j + 1; exit when j = 46; end loop; m2(0) := '0'; ----------Shift Mantissa--------------- i := i + 1; exit when e1 = e2; end loop; tempm := m1 + m2; if tempm( 24 + i ) = '1' then e1 := e1 + "000000001"; m := tempm( 23 + i downto i + 1 ); e := e1(7 downto 0); --ov <= '0'; else m := tempm( 22 + i downto i ); e := e1(7 downto 0); --ov <= '0'; end if; end if; end if; else -- xs /= ys if e1 > e2 then s := xs; bias := e1 - e2; if bias > "000010111" then e := e1(7 downto 0); m := m1(22 downto 0); --ov <= '0'; -- y almost zero else i := 0; loop e2 := e2 + "000000001"; j := 0; loop m1(46 - j) := m1(45 - j); j := j + 1; exit when j = 46; end loop; m1(0) := '0'; i := i + 1; exit when e1 = e2; end loop; ----------Shift Mantissa--------------- tempm := m1 - m2; tempm2( 70 downto 23 ) := tempm( 47 downto 0 ); j := 0; e1 := e1 + "000000001"; loop m := tempm2( 45 + i - j downto 23 + i - j ); j := j + 1; e1 := e1 - "000000001"; exit when tempm2( 47 + i - j ) = '1'; end loop; e := e1(7 downto 0); ----------Shift Mantissa--------------- end if; elsif e1 < e2 then s := ys; bias := e2 - e1; if bias > "000010111" then e := e1(7 downto 0); m := m2(22 downto 0); --ov <= '0'; -- x almost zero else i := 0; loop e1 := e1 + "000000001"; j := 0; loop m2(46 - j) := m2(45 - j); j := j + 1; exit when j = 46; end loop; m2(0) := '0'; i := i + 1; exit when e1 = e2; end loop; ----------Shift Mantissa--------------- tempm := m2 - m1; tempm2( 70 downto 23 ) := tempm( 47 downto 0 ); j := 0; e2 := e2 + "000000001"; loop m := tempm2( 45 + i - j downto 23 + i - j ); j := j + 1; e2 := e2 - "000000001"; exit when tempm2( 47 + i - j ) = '1'; end loop; e := e2(7 downto 0); ----------Shift Mantissa--------------- end if; else -- e1 = e2 -- m1 /= m2 ---------------------------------------------------- if m1 > m2 then s := xs; tempm := m1 - m2; e := e1( 7 downto 0 ); m := tempm(22 downto 0); ----------------------------------------------------------------- elsif m1 < m2 then s := ys; tempm := m2 - m1; e := e2( 7 downto 0 ); m := tempm(22 downto 0); else s := '0'; tempm := "000000000000000000000000000000000000000000000000"; e := "00000000"; m := tempm(22 downto 0); end if; end if; end if; end if; end if; end if; z := s & e & m;return z;--end process; end fadd;end pack_CC_fun;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -