; C02 library stdio.h02 assembly language subroutines
; Requires external routines GETKEY, PRCHR, DELCHR, NEWLIN, and SETSRC
; external zero page location SRCPTR
; and external constants DELKEY, ESCKEY, and RTNKEY

        SUBROUTINE STDIO

;char getc() - GET Character from keyborad
;Sets: System Dependent
;Uses: System Dependent
;Affects:  System Dependent
;Returns: A = Character Code of Key 
GETC    EQU GETCHR      ;Alias to external GETCHR Routine

;void putc(c) - PUT Character to screen
;Args: Character code to display 
;Sets: System Dependent
;Uses: System Dependent
;Affects:  System Dependent
;Returns: System Dependent
PUTC    EQU PUTCHR     ;Alias to external PUTCHR Routine

;char gets(&s) - GET String input from keyboard
;Args: Y,X = Address of String
;Sets: SRCPTR = Address of String
;Uses: TEMP3
;Affects: X,N,Z,C
;Returns: A,Y = Number of Characters in String
GETS:   JSR SETSRC      ;Initialize Source String
.GSLOOP STY TEMP3       ;Save Y Index
        JSR GETC        ;Get Keypress
        CMP #DELKEY     ;If Delete
        BNE .GSESC      ;Then  
        TYA             ;  If Offset is Zero
        BEQ .GSLOOP     ;    Get Next Character
        DEY             ;  Else Decrement Offset
        JSR DELCHR      ;    Delete Previous Character
        JMP .GSLOOP     ;    and Get Next Character
.GSESC  CMP #ESCKEY     ;Else If Escape
        BNE .GSCR       ;Then  
        LDY #$FF        ;  Return -1
        BNE .GSRTNY     
.GSCR   CMP #RTNKEY     ;Else If Not Carriage Return 
        BEQ .GSDONE
        JSR PUTC        ;  Echo Character
        LDY TEMP3       ;Restore Y Index
        STA (SRCPTR),Y  ;  Store Character at offset
        INY             ;  increment offset and
        BPL .GSLOOP     ;  loop if less than 128
.GSDONE JSR NEWLIN      ;Else Advance Cursor to Next Line
        LDY TEMP3       ;Restore Y Index
        LDA #$00        ;  Terminate String        
        STA (SRCPTR),Y  ;  and 
.GSRTNY TYA             ;  Return String Length
        RTS

;char puts(&s) - PUT String to screen
;Args: Y,X = Address of String
;Calls: PUTS
;Affects: N,Z,C
;Returns: A,Y = Number of Characters in String
PUTS:   LDA #$00       ;Set Start Position to 0
                       ;and fall into putsub

;char putsub(n, &s) - PUT SUBstring to screen
;Args: A = Starting Position in String
;      Y,X = Address of String
;Sets: SRCPTR = Address of String
;Calls: PUTC
;Affects: N,Z,C
;Returns: A,Y = Number of Characters in String
PUTSUB: JSR SETSRC     ;Initialize Source String
        TAY            ;Initialize character offset
.PSLOOP LDA (SRCPTR),Y ;Read next character in string
        BEQ .PSDONE    ;If Not 0
        JSR PUTC       ;  Print character at offset,
        INY            ;  increment offset, and
        BPL .PSLOOP    ;  loop if less than 128
.PSDONE TYA           ;Return number of
        RTS            ;  characters printed

;char putln(&s) - PUT LiNe to screen
;Args: Y,X = Address of String
;Calls: PUTS and NEWLIN
PUTLN:  JSR PUTS       ;Write string to screen
        JMP NEWLIN     ;Execute external NEWLINe routine and return

        ENDSUBROUTINE