------------------------------------------------------------ -- Square Root Calculator (FLEX10k) -- < fpsqrt.vhd > -- 1998/11/11 (Wed) -- yamaoka@tube.ee.uec.ac.jp ------------------------------------------------------------ library IEEE; use IEEE.std_logic_1164.all; use IEEE.std_logic_arith.all; use IEEE.std_logic_unsigned.all; package MATH is function sqrt( FA : std_logic_vector ) return std_logic_vector; end MATH; package body MATH is function sqrt( FA : std_logic_vector ) return std_logic_vector is variable MA : std_logic_vector(25 downto 0); variable MQ : std_logic_vector(24 downto 0); variable EQ : std_logic_vector(7 downto 0); variable TMP1, TMP2, REMAIN : std_logic_vector(27 downto 0); begin EQ := FA(30 downto 23) - "01111111"; EQ := ( EQ(7) & EQ(7 downto 1) ) + "01111111"; if FA(23) = '1' then MA := "01" & FA(22 downto 0) & '0'; else MA := '1' & FA(22 downto 0) & "00"; end if; TMP1 := "00000000000000000000000000" & MA( 25 downto 24 ); TMP2 := "0000000000000000000000000001"; for I in 0 to 24 loop REMAIN := TMP1 - TMP2; if REMAIN(27) = '0' then MQ(24-I) := '1'; if I <= 11 then TMP1( (I+3) downto 0 ) := REMAIN( (I+1) downto 0 ) & MA( (23-(2*I)) downto (22-(2*I))); else TMP1( (I+3) downto 0 ) := REMAIN( (I+1) downto 0 ) & "00"; end if; TMP2( (I+3) downto 0 ) := TMP2( (I+2) downto 1 ) & "01"; TMP2(2) := '1'; else MQ(24-I) := '0'; if I <= 11 then TMP1( (I+3) downto 0 ) := TMP1( (I+1) downto 0 ) & MA( (23-(2*I)) downto (22-(2*I))); else TMP1( (I+3) downto 0 ) := TMP1( (I+1) downto 0 ) & "00"; end if; TMP2( (I+3) downto 0 ) := TMP2( ( I+2 ) downto 1) & "01"; end if; end loop; MQ := MQ + "0000000000000000000000001"; return '0' & EQ & MQ(23 downto 1); end sqrt; end MATH;