; Gets page pageNum ($bootPage) on progName
; By Andree Chea

.nolist
; Hack to see if we're building for CSE
#define IS_CSE bootPage > 80h

#if IS_CSE
#include "ti84pcse.inc"
_ClrScrnFull .equ _maybe_ClrScrnFull
#else
#include "ti83plus.inc"
#endif
.list

.org userMem-2
#if IS_CSE
    .db tExtTok, tAsm84CCmp
#else
	.db t2ByteTok, tasmCmp
#endif

	ld hl,saveVariable
	rst 20h					;rMOV9TOOP1
	bcall(_ChkFindSym)
	ld hl,ErrDefined
	jr nc,DisplayError
	;ret nc					;already exists
	ld hl,saveVariable
	rst 20h					;rMOV9TOOP1
	ld hl,$4000				;1 page
	bcall(_EnoughMem)
	ld hl,ErrMemory
	jr c,DisplayError
	;ret c					;Not enough memory
	ex de,hl				;hl = $4000
	bcall(_CreateAppVar)
	inc de
	inc de					;de -> data

	ld a,bootPage
	ld hl,$4000
	ld bc,$4000
	bcall(_FlashToRam)
	
	ld hl,txtSuccess
	;ret

DisplayError:
	push hl
	bcall(_ClrScrnFull)
	ld hl,$0000
	ld (curRow),hl
	pop hl
	bcall(_PutS)
	bcall(_NewLine)
	ret

txtSuccess:
	    ;0123456789ABCDEF
	.db "AppVar D",progName,pageNum,"  "
	.db "created.",$00

ErrMemory:
	    ;0123456789ABCDEF
	.db "You do not have "
	.db "16KBs of RAM    "
	.db "free.",$00

ErrDefined:
	    ;0123456789ABCDEF
	.db "AppVar D",progName,pageNum,"  "
	.db "already exists.",$00

saveVariable:
	.db AppVarObj,"D",progName,pageNum,$00

.end