;-----------------------------------------------------------;
;                                                           ;
; Maths Pack                                                ;
; Version 1.0                                               ;
;                                                           ;
; Copyright (c)2001 TI-Calculator Programming Alliance      ;
; Written and Programmed by James Vernon <james@calc.org>   ;
; ICQ#: 71589304                                            ;
; http://tcpa.calc.org                                      ;
;                                                           ;
; You are allowed to use portions of this source as long as ;
; you give me credit. You are not allowed to release a      ;
; modified source or the compiled version of it without     ;
; my permission.                                            ;
;                                                           ;
;-----------------------------------------------------------;

;------------------------------------------------
; Ion include file
;------------------------------------------------
#include "ion.inc"

;------------------------------------------------
; GET_KEY equates
;------------------------------------------------
#include "get_key.inc"

;------------------------------------------------
; Constants
;------------------------------------------------
verMajor                        = '1'
verMinor                        = '0'
NUM_FUNCTIONS                   = 4

;------------------------------------------------
; Variables
;------------------------------------------------
vars                            = saferam1
functionNumber                  = vars+0        ; 1
op1Buffer                       = vars+1        ; 11
op2Buffer                       = vars+12       ; 11
op3Buffer                       = vars+23       ; 11
op4Buffer                       = vars+34       ; 11
op5Buffer                       = vars+45       ; 11
op6Buffer                       = vars+56       ; 11
getNumberBuffer                 = vars+67       ; 19
float1                          = vars+86       ; 11
float2                          = vars+97       ; 11
float3                          = vars+108      ; 11
float4                          = vars+119      ; 11
float5                          = vars+130      ; 11
float6                          = vars+141      ; 11
float7                          = vars+152      ; 11
float8                          = vars+163      ; 11
float9                          = vars+174      ; 11
float10                         = vars+185      ; 11

;------------------------------------------------
; Undefined equates
;------------------------------------------------
#ifdef TI83P
_acos                           = $40DE
_ckint                          = $4234
_ckop1pos                       = $4258
_formreal                       = $4999
_fpsquare                       = $4081
_fpsub                          = $406F
_invop1s                        = $408D
_op1exop2                       = $421F
_op2set4                        = $4195
_sqroot                         = $409C
_times2                         = $4066
#else
_acos                           = $4122
_ckint                          = $4322
_ckop1pos                       = $435A
_convop1                        = $4EFC
_cpop1op2                       = $4166
_formreal                       = $4D32
_fpdiv                          = $40C6
_fpsquare                       = $40A6
_fpsub                          = $408E
_invop1s                        = $40B6
_sqroot                         = $40CA
_times2                         = $4082
#endif

;------------------------------------------------
; Ion header
;------------------------------------------------
#ifdef TI83P
.org    progstart-2
.db     $BB,$6D
#else
.org    progstart
#endif
        ret
        jr      nc,start
.db     "Maths Pack v",verMajor,".",verMinor," - TCPA",0

;------------------------------------------------
; Start of Program
;------------------------------------------------
start:
        set     textwrite,(iy+sgrflags)
        xor     a
        ld      (functionNumber),a

menuLoop:
        bcall(_grbufclr)
        ld      hl,strMenu
        ld      de,0*256+23
        call    vPuts                           ; "MATHS PACK"
        ld      de,9*256+10
        call    vPuts                           ; "Copyright (c)2001 TCPA"
        ld      de,15*256+21
        call    vPuts                           ; "By James Vernon"
        ld      de,30*256+0
        call    vPuts                           ; "Total Functions:"
        ld      de,38*256+0
        call    vPuts                           ; "This Function:"
        ld      de,30*256+54
        ld      a,NUM_FUNCTIONS
        call    vDispASetPen                    ; Show total functions
        ld      de,45*256+0
        ld      a,(functionNumber)
        inc     a
        call    vDispASetPen                    ; Show current function number
        ld      hl,pencol
        dec     (hl)
        ld      hl,strFunctionName
        bcall(_vputs)                           ; ")  "
        ld      hl,(functionNumber)
        ld      h,0
        add     hl,hl
        add     hl,hl
        ld      de,functionPointers
        add     hl,de
        bcall(_ldhlind)
        bcall(_vputs)                           ; Show function name
        call    ionFastCopy

        call    waitKey
        cp      GK_2ND
        jr      z,runFunction
        cp      GK_LEFT
        jr      z,prevFunction
        cp      GK_RIGHT
        jr      z,nextFunction
        cp      GK_MODE
        jr      nz,menuLoop

quit:
        res     textwrite,(iy+sgrflags)
        ret

prevFunction:
        ld      hl,functionNumber
        ld      a,(hl)
        or      a
        jr      z,lastFunction
        dec     (hl)
        jp      menuLoop

nextFunction:
        ld      hl,functionNumber
        ld      a,(hl)
        cp      NUM_FUNCTIONS-1
        jr      z,firstFunction
        inc     (hl)
        jp      menuLoop

lastFunction:
        ld      (hl),NUM_FUNCTIONS-1
        jp      menuLoop

firstFunction:
        ld      (hl),0
        jp      menuLoop

runFunction:
        ld      hl,menuLoop
        push    hl                              ; Where to RET to when function finished
        bcall(_grbufclr)
        call    ionFastCopy
        ld      hl,0
        ld      (pencol),hl
        ld      (currow),hl
        ld      hl,(functionNumber)
        ld      h,0
        add     hl,hl
        add     hl,hl
        ld      de,functionPointers
        add     hl,de
        inc     hl
        inc     hl
        bcall(_ldhlind)
        jp      (hl)

; Other routines:
#include "routines.asm"


;------------------------------------------------
; Strings
;------------------------------------------------
strMenu:
.db     "MATHS PACK v",verMajor,".",verMinor,0
.db     "Copyright (c)2001 TCPA",0
.db     "By James Vernon",0
.db     "Total Functions:",0
.db     "This Function:",0

strFunctionName:
.db     ")  ",0

;------------------------------------------------
; Functions files
;------------------------------------------------
#include "surd.asm"
#include "quadslvr.asm"
#include "vecmag.asm"
#include "vecangle.asm"

;------------------------------------------------
; Pointers to all function names and their code
;------------------------------------------------
functionPointers:
.dw     surdName,surd
.dw     quadSolverName,quadSolver
.dw     vecMagName, vecMag
.dw     vecAngleName, vecAngle

.end
