;===============================================================================
;                               SPLat Controls.                                ;
;                          Product Development Group                           ;
;                            Melbourne,  AUSTRALIA                             ;
;===============================================================================
;PURPOSE:
; A collection of utilities
;
;===============================================================================
;===============================================================================
;           Copyright (c) 2014 SPLat Controls. All rights reserved.            ;
;                                                                              ;
;        THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SPLat Controls.        ;
;               The copyright notice above does not evidence any               ;
;              actual or intended publication of such source code.             ;
;===============================================================================

;===============================================================================
;
;<INITSEG>
;
;===============================================================================


;===============================================================================
;
;<CONSTSEG>
;
;===============================================================================


;===============================================================================
;
;<MEQUSEG>
;RAM storage
;
;===============================================================================


;===============================================================================
;
;<IOSEG>
;I/O Assignments
;
;===============================================================================


;===============================================================================
;
;AUTOMATIC RAM
;
;===============================================================================
;<DEF_SEM>

;<DEF_BYTE>

;<DEF_WORD>

;<DEF_TIME24>

;<DEF_FLOAT>

;<DEF_BLOCK>


;===============================================================================
;
;<CODESEG>
;
;===============================================================================

;===============================================================================
;DESCRIPTION:
; A helper the allows a subroutine to return using a goto, eg "GoIfXlt 3,Return"
;PARAMETERS:
;  -> Nil
;RETURNS:
; <-  Nil
;===============================================================================
Return:
   Return


;===============================================================================
;DESCRIPTION:
; Calculates Q % W = W
;PARAMETERS:
;  -> Q: number (clobbered)
;  -> W: divisor
;RETURNS:
; <-  W: result
;===============================================================================
fMod:
   QtoU           4                                      ;save
   WtoU           8                                      ;save
   GoSub          fIntDiv
   UtoQ           8                                      ;get original W
   fMul
   UtoQ           4
   fSub                                                  ;calc remainder
   Return


;===============================================================================
;DESCRIPTION:
; Calculates (int)(Q / W) = W
;PARAMETERS:
;  -> Q: number (clobbered)
;  -> W: divisor
;RETURNS:
; <-  W: result
;===============================================================================
fIntDiv:
   fDiv
   WtoU           0                                      ;temp save
   fAbs
   fLoadQ         0.5
   fGoIfWgeQ      _fIntDoAdj
   fLoadW         0.0
   Return
_fIntDoAdj
   UtoW           0
   fGoIfNeg       _fInfDoUnRound                         ;round down as..
   fLoadQ         -0.5
_fInfDoUnRound
   fAdd                                                  ;..fixToU will round up, thus U will contain the integer
   fixToU         0
   floatFromU     0
   Return


;===============================================================================
;DESCRIPTION:
; Saves the NV pointers (uses UV registers)
;PARAMETERS:
;  -> Nil
;RETURNS:
; <-  Nil
;===============================================================================
NVSavePtrs:
   WtoU           7                                   ;save W
   NVfGetPtrW
   WtoU           0
   NVPushRecLen
   PopU           4
   NVPushRecNum
   PopU           5
   NVPushPage
   PopU           6
   UtoW           7                                   ;restore W
   Return


;===============================================================================
;DESCRIPTION:
; Restores the NV pointers following a call to NVSavePtrs
;PARAMETERS:
;  -> Nil
;RETURNS:
; <-  Nil
;===============================================================================
NVRestorePtrs:
   WtoU           7                                   ;save W
   PushU          6
   NVPopPage
   UtoW           0
   NVfPutPtrW
   PushU          4
   NVPopRecLen
   PushU          5
   NVPopRecNum
   UtoW           7                                   ;restore W
   Return


;===============================================================================
;DESCRIPTION:
; Calculates a polynomial of any order.  The control table format is:
;  NV0Byte     N     ;The order of the polynomial
;  NV0fNum     An    ;The X^n coefficient
;  NV0fNum     An-1  ;The X^(n-1) coefficient
;   ....
;  NVfNum      A1    ;The X coefficient
;  NVfNum      A0    ;The constant
;
; Example:
; The polynomial Y = 1.4 * x^4 - 1.3 * x^3 + 1.2 * x^2 - 1.1 * x  + 1
; is a 4th order polynomial. It's control table would be
;PolyTab:
;  NV0Byte  4     ;Fourth order
;  NV0fNum  1.4   ;Coefficient for x^4
;  NV0fNum  -1.3  ;Coefficient for x^3
;  NV0fNum  1.2   ;Coefficient for x^2
;  NV0fNum  -1.1  ;Coefficient for x^1
;  NV0fNum  1.0   ;Constant
;PARAMETERS:
;  -> W The argument value (normally called X)
;  -> NVEMPtr Control table address
;RETURNS:
; <-  W the y result
;===============================================================================
PolyCalculate:
   WtoU           0                                   ;save argument
   NVSetpage      0
   NVSetRecNum    0
   NVSetRecLen    4
   NVPushByte     0                                   ;get the order of the poly to X
   XtoI
   NVAddPtr       1                                   ;now points to first coefficient
   NVfReadW       0                                   ;get first coefficient
_PolyCalcLoop
   NVAdvPtr                                           ;ready for next time
   UtoQ           0                                   ;argument
   fMul
   NVfReadQ       0                                   ;get next coefficient
   fAdd
   DecI
   GoIfInz        _PolyCalcLoop
   Return


;===============================================================================
;
;<DEBUG>
;
;===============================================================================


;===============================================================================
;
;<TESTSEG>
;
;===============================================================================


