; c02 Program Initialization Code for Commander X16
; Compatible with x16emu Release 34

;PETSCII Key Mappings
DELKEY  EQU $14   ;Delete/Backspace Key (Delete)
ESCKEY  EQU $03   ;Escape/Stop Key      (RUN/STOP)
RTNKEY  EQU $0D   ;Return/Enter Key     (RETURN)

;Zero Page System Variables - x16emu Release 34 Memory Map
;UNUSED $00-$01   ;Available to User
;       $02-$52   ;Used by Basic graphics commands
XMBANK  EQU $53   ;Extended Memory Bank 
XADRLO  EQU $54   ;Ext Memory Address LSB 
XADRHI  EQU $55   ;Ext Memory Address MSB 
SRCPTR  EQU $56   ;Source Pointer 
SRCLO   EQU $57   ;Source Pointer LSB 
SRCHI   EQU $58   ;Source Pointer MSB 
DSTPTR  EQU $59   ;Destination Pointer 
DSTLO   EQU $5A   ;Destination Pointer LSB 
DSTHI   EQU $5B   ;Destination Pointer MSB 
BLKLO   EQU $5C   ;Block Pointer LSB 
BLKHI   EQU $5D   ;Block Pointer MSB 
STKLO   EQU $5E   ;Stack Pointer LSB 
STKHI   EQU $5F   ;Stack Pointer MSB 
BFRLO   EQU $60   ;Buffer Pointer LSB 
BFRHI   EQU $61   ;Buffer Pointer MSB 
RANDOM  EQU $62   ;Random Number Storage 
RDSEED  EQU $63   ;Random Seed 
TEMP0   EQU $64   ;Temporary Variable 
TEMP1   EQU $65   ;Temporary Variable 
TEMP2   EQU $66   ;Temporary Variable 
TEMP3   EQU $67   ;Temporary Variable 
;       $68-7F    ;Available to User
;       $80-$83   ;Used by Kernal and DOS
;       $A4-$A8   ;Reserved for KERNAL/DOS/BASIC
;       $A9-$FF   ;Used by BASIC
  
;Other Variables - Top of Extended System RAM Area
BLKSLO  EQU $07F4 ;Block Start LSB 
BLKSHI  EQU $07F5 ;Block Start MSB 
BLKELO  EQU $07F6 ;Block End LSB 
BLKEHI  EQU $07F7 ;Block End MSB 
BLKLEN  EQU $07F8 ;Block Length 

STKSAV  EQU $07FA ;Machine Stack Storage 

STKSLO  EQU $07FC ;Stack Start LSB 
STKSHI  EQU $07FD ;Stack Start MSB 
STKELO  EQU $07FE ;Stack End LSB   
STKEHI  EQU $07FF ;Stack End MSB   

;Machine Language Basic Stub - Same as Commodore 64
        ORG $0801              ;Start of Basic Program 
BASIC:  DC  $0C, $08           ;Pointer to Next Line
        DC  $00, $00           ;Line Number (0)
        DC  $9E                ;SYS 
        DC  $20                ;' '
        DC  $32, $30, $36 ,$32 ;"2062" 
        DC  $00                ;End of Line Marker
        DC  $00, $00           ;End of Basic Program

START:  TSX         ;Get Stack Pointer
        STX STKSAV  ;and Save for Exit
        SEC         ;Set Screen Mode 
        LDA #0      ;to 40x30 Text
        JSR $FF5F   ;using SCRMOD
        JMP MAIN    ;Execute Program

EXIT:   LDX STKSAV  ;Retrieve Saved Stack Pointer
        TXS         ;and Restore It
        RTS         ;Return to BASIC

;Poll Keyboard for Character
POLKEY  EQU $FFE4   ;Aliased to Kernal GETIN Routine

;Get Character from Keyboard
GETKEY  EQU POLKEY  ;Aliased to POLKEY Routine

;Wait for Character from Keyboard
GETCHR: JSR GETKEY  ;Poll Keyboard
        BEQ GETCHR  ;If No Key, Loop
        RTS

;Print Character to Console
PUTCHR  EQU $FFD2   ;Aliased to CHROUT Routine

;Delete Previous Character
DELCHR: LDA #DELKEY ;Load Delete Character
        JMP PUTCHR  ;Print and Return

;Advance Character to Next line
NEWLIN: LDA #RTNKEY ;Load Return Character
        JMP PUTCHR  ;Print and Return

        INCLUDE "../include/prbyte.a02" ;PRBYTE and PRHEX routine
        INCLUDE "../include/putstr.a02" ;PUTSTR routine
