;Apple 1 program initialization code for c02 programs

;System Specific ASCII Key Mappings
DELKEY  EQU $08     ;Delete/Backspace Key (Left Arrow)
ESCKEY  EQU $1B     ;Escape/Stop Key      (Escape)
RTNKEY  EQU $0D     ;Return/Enter Key     (Return)

;Zero Page Variables (*=System Variable)
DSTLO   EQU $06     ;Destination String/Array Pointer
DSTHI   EQU $07
BLKLO   EQU $08     ;Block Segment Pointer (block.a02)
BLKHI   EQU $09
PTRLO   EQU $1D     ;System Pointer (pointer.a02)
PTRHI   EQU $1E
RANDOM  EQU $1F     ;Random Number
INVFLG  EQU $32     ;*Inverse Flag: $3F=Blinking, $7F=Inverse, $FF=Normal 
RDSEED  EQU $4E     ;O/S Random Number Low Byte
;       EQU $4F     ;O/S Random Number High Byte
SRCLO   EQU $71     ;Source String/Array Pointer
SRCHI   EQU $72
;           $E3     ;Unused
BLKSLO  EQU $EB     ;Block Start Address
BLKSHI  EQU $EC
BLKELO  EQU $ED     ;Block End Address
BLKEHI  EQU $EE
BLKLEN  EQU $EF     ;Block Segment Length    
BFRLO   EQU $FA     ;Work Buffer Pointer
BFRHI   EQU $FB
TEMP0   EQU $FC     ;Temporary Storage
TEMP1   EQU $FD
TEMP2   EQU $FE
TEMP3   EQU $FF

;Page 3 Vectors
WARMST  EQU $3D0    ;Jump vector to DOS warm start
COLDST  EQU $3D3    ;Jump vector to DOS cold start

;I/O Locations
KBD     EQU $C000   ;Keyboard Data
AKD     EQU $C010   ;Keyboard Strobe Register

;BASIC Routines
SHWCUR  EQU $CC4C   ;Display Cursor
UPDATE  EQU $CC70   ;Flash Cursor
STORY   EQU $C3B3   ;Store Accumulator at Current Screen Position

;Monitor Routines
PRBLNK  EQU $F94C   ;Print 3 blanks
PRBLNX  EQU $F94C   ;Print X blanks
PRBLAX  EQU $F94C   ;Print character in A followed by X-1 blanks
GETCHR  EQU $FD0C   ;Waits for keypress and return in A
KEYIN   EQU $FD1B   ;Waits for keypress and cycle random-number generator
RDCHAR  EQU $FD35   ;Read keyboard (processing escapes)
GETLIN  EQU $FD6A   ;Get Line from Keyboard into Input Buffer
CROUT   EQU $FD8E   ;Performs a carriage return
PRBYTE  EQU $FDDA   ;Print Accumulator as Hexadecimal Number
PRHEX   EQU $FDE3   ;Print Low Nybble of Accumulator as Hex Digit
COUT    EQU $FDED   ;Print Character to Current Output Device
COUT1   EQU $FDF0   ;Print Character to Screen
BELL    EQU $FF3A   ;Ring Bell through COUT
MONZ    EQU $FF69   ;Enter monitor (<Control-C>, <Return> leaves the monitor)

ECHO    EQU COUT1   ;Print Character
EXIT    EQU WARMST  ;Return to Monitor

        ORG $0C00   ;Safe Area for Machine Language

START:  JMP MAIN    ;Execute Program

;Subroutine Poll Keyboard
PLKEY:  LDA #0      ;Clear Accumulator
        BIT KBD     ;Check Keyboard Strobe Bit
        BPL PLKEYR  ;If Key Pressed  
        LDA KBD     ;  Load Key Value
        STA AKD     ;  Clear Strobe
PLKEYR: RTS

;Read Keyboard
GETKEY  JSR POLKEY  ;Poll Keyboard
        BEQ GETKEY  ;Loop if No Key	
        AND #$7F    ;Strip High Bit
        RTS

;Print Character to Screen
PUTCHR ORA #$80    ;Set High Bit
        CMP #$E0    ;
        BCC PRCHRX  ;If Lower Case 
        AND #$1F    ;  Convert to Inverse
PRCHRX: JMP ECHO    ;Alias to Monitor Routine

;Delete Previous Character
DELCHR: LDX #2      ;Two Characters Total
        LDA #$88    ;Load Backspace Character
        JSR PRBLAX  ;Print Accumulator and X-1 Blanks
        LDA #$88    ;Load Backspace Character
        JMP PUTCHR  ;and Print it

;Advance Character to Next line
NEWLIN   EQU CROUT  ;Alias to Monitor Routine

;Initialize Destination String Pointer and Index
SETDST: STX DSTLO   ;Save Destination String Pointer
        STY DSTHI
        RTS

;Initialize Source String Pointer and Index
SETSRC: STX SRCLO   ;Save Source String Pointer
        STY SRCHI
        LDY #$00    ;Initialize Index Into String
        RTS

;Retrieve Source String Pointer
GETSRC: LDX SRCLO
        LDY SRCHI
        RTS
