; Program initialization code for Oric-1

;System Specific ASCII Key Mappings
DELKEY  EQU $7F     ;Delete/Backspace Key (Delete)
ESCKEY  EQU $1B     ;Escape/Stop Key      (Escape)
RTNKEY  EQU $0D     ;Return/Enter Key     ($0D=Carriage Return)

;Zero Page Locations
SRCLO   EQU $10     ;Source String Pointer (stdio.asm)
SRCHI   EQU $11
DSTLO   EQU $12     ;Destination String Pointer (string.asm)
DSTHI   EQU $13
BFRLO   EQU $14     ;Work Buffer Pointer
BFRHI   EQU $15

TEMP0   EQU $16     ;Temporary Storage
TEMP1   EQU $17
TEMP2   EQU $18
TEMP3   EQU $19

RDSEED  EQU $1A     ;Pseudo-RANDOM Seed
RANDOM  EQU $1B     ;Pseudo-RANDOM Number Storage

;       EQU $1C     ;Unused
;       EQU $1D     ;Unused

BLKLEN  EQU $1F     ;Block Length
BLKSLO  EQU $20     ;Block Start Address
BLKSHI  EQU $21
BLKELO  EQU $22     ;Block End Address
BLKEHI  EQU $23

STKSLO  EQU $24 ;Stack Start LSB
STKSHI  EQU $25 ;Stack Start MSB
STKELO  EQU $26 ;Stack End LSB  
STKEHI  EQU $27 ;Stack End MSB  


;Oric-1 Tape images have the file extension ".tap"
;and have the following header prepended to the code
;   $16,$16,$16, $16    Tape Syncronization Pattern
;   $24                 Tape Syncronization Byte
;   $00,$00             Filler Bytes
;   $00                 Basic Program
;   $00                 No Autostart ($80 for Autostart)
;   $xx,$xx             MSB, LSB of End Address
;   $05,$01             MSB, LSB of Start Address
;   $00                 Filler Byte
;   "filename"          Up to 16 ASCII Characters
;   $00                 Strint Terminator    

;The End Address is actually one higher than the last
;byte of the Basic program and the first byte of Basic
;variable space. so the compiled program need to have
;one dummy byte added to the end.

        ORG $0501   ;Basic Program Start Address

BASIC:  .DC $0C,$05 ;Address of Next Line
        .DC $00,$00 ;Basic Line Number (0)
        .DC $BF     ;Token for Basic Call Command
        .DC $20     ;Space
        .DC "1294"  ;Address of START Label
        .DC $00     ;End of Line
        .DC $00,$00 ;End of Program Marker

START:  LDA #127    ;Set Caps Lock Off
        STA $020C
        LDA #$0B    ;Turn Cursor On, Keyclick Off
        STA $026A   ;and Protect Columns 0 & 1
        JMP MAIN    ;Execute Program

;Poll Character from Keyboard
POLKEY: INC RDSEED  ;Cycle the Random Seed
        JMP $E905   ;Execute GETORKB

;Read Character from Console
GETKEY  EQU POLKEY  ;Aliased to POLKEY

;Wait for Character from Console
GETCHR: JSR GETKEY  ;Calls GETKEY
        BEQ GETCHR  ;  until a non-zero is returned
        RTS         

;Delete Previous Character
DELCHR: LDA #DELKEY ;Load Delete Character
        BNE PUTCHR  ;and Print to Screem

;Advance Cursor to Next line
NEWLIN  EQU $CB9F   ;Basic NEWLINE Routine ($CBF0 for ATMOS)
;       LDA #$0D    ;Load Carriage Return
;       JSR PUTCHR  ;and Print It
;       LDA #$0A    ;Load Line Feed and fall through to PRCHR

;Print Character to Screen
PUTCHR: TAX         ;Transfer Character to X Register
        JMP $F409   ;Execute VDU Routine

;Print String to Screen
PUTSTR: TXA         ;Copy LSB to A6
        JMP $CCB0   ;Execute BASIC String Print Routine

;Get End of Free Memory
RAMTOP: LDX $A6     ;Load HIMEM LSB
        LDY $A7     ;Load HIMEM MSB
        RTS

;Exit Program and Return to Operating System or Monitor
EXIT:   LDA #255    ;Set Caps Lock On
        STA $020C
        JMP $C003   ;BASIC Warm Start

        INCLUDE "../include/prbyte.a02" ;PRBYTE and PRHEX routines
