URL
https://opencores.org/ocsvn/ft816float/ft816float/trunk
Subversion Repositories ft816float
[/] [ft816float/] [trunk/] [software/] [GetFloat.asm] - Rev 88
Compare with Previous | Blame | View Log
; ============================================================================; __; \\__/ o\ (C) 2022 Robert Finch, Waterloo; \ __ / All rights reserved.; \/_// robfinch<remove>@opencores.org; ||;;; BSD 3-Clause License; Redistribution and use in source and binary forms, with or without; modification, are permitted provided that the following conditions are met:;; 1. Redistributions of source code must retain the above copyright notice, this; list of conditions and the following disclaimer.;; 2. Redistributions in binary form must reproduce the above copyright notice,; this list of conditions and the following disclaimer in the documentation; and/or other materials provided with the distribution.;; 3. Neither the name of the copyright holder nor the names of its; contributors may be used to endorse or promote products derived from; this software without specific prior written permission.;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE; DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER; CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,; OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.;; ============================================================================;; Get a floating point number;_GetFloatGetChar:move.b (a0),d1add.l d0,a0rts_GetFloatIgnBlanks:.0001bsr _GetFloatGetCharcmpi.b #' ',d1beq .0001_GetFloatBackupChar:sub.l d0,a0rts;-------------------------------------------------------------------------------; Get fractional part of a number, 25 digits max, into a float register.;; Register Usage:; d1 = digit from input screen; d4 = digit count; d6 = digit scaling factor; fp1 = digit as float number; Returns:; fp0 = fraction;-------------------------------------------------------------------------------_GetFraction:link a2,#-28move.l _canary,24(sp)movem.l d1/d4/d6,(sp)fmove.x fp1,12(sp)clr.l d6 ; d6 = scale factorfmove.w #0,fp0 ; fract = 0.0moveq #24,d4.0002bsr _GetFloatGetCharcmpi.b #'0',d1blo .0001cmpi.b #'9',d1 ; make sure between 0 and 9bhi .0001subi.b #'0',d1fscale.w #1,fp0 ; fract * 10.0addq #1,d6 ; record scalingfmove.b d1,fp1 ; fp1 = digitfadd fp1,fp0 ; fract += digitaddq.w #1,d5 ; increment number of digits in numberdbra d4,.0002.0001bsr _GetFloatBackupCharneg d6fscale.l d6,fp0 ; fract /= scalemovem.l (sp),d1/d4/d6fmove.x 12(sp),fp1cchk 24(sp)unlk a2rts;-------------------------------------------------------------------------------; Get exponent part of a number, 4 digits max, into a float register.;; Register Usage:; d1 = digit from input screen; d2 = exponent; d3 = temp, number times 2; d4 = digit counter; Parameters:; fp0 = float number; Returns:; fp0 = float number with exponent factored in;-------------------------------------------------------------------------------_GetExponent:link a2,#-32move.l _canary,28(sp)movem.l d1/d2/d3/d4,(sp)fmove.x fp2,16(sp)clr.l d2 ; d2 = number = 0fmove.w #0,fp2 ; fp2 = exp = 0.0moveq #1,d3 ; d3 = exscale = 1bsr _GetFloatGetCharcmpi.b #'-',d1bne .0001neg.l d3 ; exscale = -1.0006bsr _GetFloatIgnBlanksbra .0002.0001cmpi.b #'+',d1beq .0006bsr _GetFloatBackupChar.0002moveq #3,d4 ; d4 = max 4 digits.0004bsr _GetFloatGetChar ; d1 = digit charcmpi.b #'0',d1blo .0003cmpi.b #'9',d1 ; ensure between 0 and 9bhi .0003subi.b #'0',d1add.l d2,d2 ; number *2move.l d2,d3lsl.l #2,d2 ; number *8add.l d3,d2 ; number *10ext.w d1ext.l d1add.l d1,d2 ; number + digitaddq.w #1,d5 ; increment number of digits in numberdbra d4,.0004.0003bsr _GetFloatBackupChar ; backup a charactermulu d3,d2 ; *1 or *-1ext.l d2fscale.l d2,fp2 ; exp * exmulfmul fp2,fp0 ; rval *= expmovem.l (sp),d1/d2/d3/d4fmove.x 16(sp),fp2cchk 28(sp)unlk a2rts;-------------------------------------------------------------------------------; Get an integer number, positive or negative, 25 digits max, into a float; register.;; Register Usage:; d1 = digit from input screen; d2 = digit down counter; d3 = sign of number '+' or '-'; fp1 = digit; Modifies:; a0,fp0; Returns:; a0 = updated buffer pointer; fp0 = integer number;-------------------------------------------------------------------------------_GetInteger:link a2,#-28move.l _canary,24(sp)movem.l d1/d2/d3,(sp)fmove.x fp1,12(sp)fmove.w #0,fp0moveq #24,d2 ; d2 = digit count (25 max)bsr _GetFloatIgnBlanksbsr _GetFloatGetChar ; get the sign of the numbercmpi.b #'+',d1beq .0002.0003cmpi.b #'-',d1bne .0004move.b #'-',d7.0002bsr _GetFloatGetChar.0004cmpi.b #'0',d1 ; only characters 0 to 9 validblo .0001cmpi.b #'9',d1bhi .0001subi.b #'0',d1fscale.w #1,fp0 ; number *10fmove.b d1,fp1 ; fp1 = digitfadd fp1,fp0addq.w #1,d5dbra d2,.0002.0001bsr _GetFloatBackupCharmovem.l (sp),d1/d2/d3fmove.x 12(sp),fp1cchk 24(sp)unlk a2rts;-------------------------------------------------------------------------------; Get a floating point number off the input screen.;; Parameters:; a0 = pointer to buffer containing string; d0 = stride of buffer (increment / decrement amount); Register Usage:; d1 = character from input screen; d5.lo = number of digits in number, d5.hi = number of characters fetched; Returns:; fp0 = number; a0 = updated buffer pointer; d0 = length of number >0 if a number;-------------------------------------------------------------------------------_GetFloat:link a2,#-32move.l _canary,28(sp)movem.l d1/d5/d7/a1,(sp)fmove.x fp2,16(sp)clr.l d5move.b #'+',d7 ; assume a positive numbermove.l a0,a1 ; a1 = copy of pointer to bufferbsr _GetInteger ; rval = integerfmove.x fp0,fp2bsr _GetFloatGetCharcmpi.b #'.',d1beq .0004.0005bsr _GetFloatBackupCharbra .0002.0004bsr _GetFractionfadd fp2,fp0 ; rval += fractionbsr _GetFloatGetCharcmpi.b #'e',d1 ; accept either 'e' or 'E' indicating exponentbeq .0001cmpi.b #'E',d1bne .0005.0001bsr _GetExponent ; factor exponent into fp0.0002cmpi.b #'-',d7 ; adjust number for signbne .0003fneg fp0.0003suba.l a0,a1 ; compute number of characters fetchedmove.w a1,d0 ; move it to d0.hiswap d0move.w d5,d0 ; return digit/character count in d0 (non zero for a number)movem.l (sp),d1/d5/d7/a1fmove.x 16(sp),fp2cchk 28(sp)unlk a2rts
