2018-02-02 23:02:06 +00:00
|
|
|
; C02 library stdiox.h02 assembly language subroutines
|
|
|
|
|
2018-08-14 18:14:32 +00:00
|
|
|
ANYKEP: DC "PRESS ANY KEY...",0
|
2018-07-27 18:11:39 +00:00
|
|
|
|
2018-07-30 00:40:31 +00:00
|
|
|
;
|
|
|
|
;char anykey() - wait for character with ANY KEY prompt
|
|
|
|
;Calls: GETCPR, NEWLIN - Print Newline to Screen
|
|
|
|
;Affects: C,N,Z
|
|
|
|
;Returns: A = Character code of keypress
|
2018-07-27 18:11:39 +00:00
|
|
|
ANYKEY: JSR NEWLIN ;Start at Beginning of Next Line
|
2018-07-30 00:40:31 +00:00
|
|
|
LDY #>ANYKEP ;Load Prompt High Byte
|
|
|
|
LDX #<ANYKEP ;Load Prompt Low Byte
|
2018-07-27 18:11:39 +00:00
|
|
|
;Drop into GETCPR
|
|
|
|
|
2018-07-30 00:40:31 +00:00
|
|
|
;char getcpr(&s) - GET Character with PRompt
|
|
|
|
;Args: Y,X = Address of Prompt String
|
|
|
|
;Sets: TEMP0 - Character code of keypress
|
|
|
|
;Calls: PUTS - Put String to Screen
|
|
|
|
; GETC - Get Character from Keyboard
|
|
|
|
; NEWLIN - Print Newline to Screen
|
|
|
|
;Affects: C,N,Z
|
|
|
|
;Returns: A = Character code of keypress
|
|
|
|
GETCPR: JSR PUTS ;Print Prompt
|
|
|
|
JSR GETC ;Wait for and Return Keypress
|
|
|
|
STA TEMP0 ;Save Key Code
|
|
|
|
JSR NEWLIN ;Move to Next Line
|
2018-07-27 18:11:39 +00:00
|
|
|
JSR NEWLIN ;Generate Blank Line
|
2018-07-30 00:40:31 +00:00
|
|
|
LDA TEMP0 ;Load Key Code
|
|
|
|
RTS
|
2018-07-27 18:11:39 +00:00
|
|
|
|
2018-09-17 23:44:47 +00:00
|
|
|
;void putbin(b) - PUT Binary
|
|
|
|
;Args: A = number to print
|
|
|
|
;Sets: TEMP0 = 0
|
|
|
|
;Affects: A,Y,C,N,X
|
|
|
|
PUTBIN: STA TEMP0 ;Save Number
|
|
|
|
LDX #8 ;Print 8 Binary Digits
|
|
|
|
PUTBIL: LDA #0
|
|
|
|
ASL TEMP0 ;Shift Top Bit Out
|
|
|
|
ADC #$30 ;Convert to '0' or '1'
|
|
|
|
JSR PUTCHR ;Print Digit
|
2019-05-27 21:12:10 +00:00
|
|
|
DEX ;Decrement Index
|
2018-09-17 23:44:47 +00:00
|
|
|
BNE PUTBIL ;Loop if Not Done
|
|
|
|
RTS
|
|
|
|
|
2018-07-30 00:40:31 +00:00
|
|
|
;void putdel(b) - PUT DEcimal Left justified
|
2018-07-08 05:38:36 +00:00
|
|
|
;Args: A = number to print
|
|
|
|
;Sets: TEMP0 - ones digit
|
|
|
|
; TEMP1 - tens digit
|
|
|
|
; TEMP2 - hundreds digit
|
|
|
|
; TEMP3 - number that was printed
|
|
|
|
;putdem - Alternate Entry Point if number is already in TEMP3
|
|
|
|
;putdeh - Alternate Entry Point to print only padding spaces
|
2018-02-02 23:02:06 +00:00
|
|
|
PUTDEL: STA TEMP3
|
2018-07-08 05:38:36 +00:00
|
|
|
PUTDEM: JSR PUTDEC ;Print Decimal Representation of number
|
|
|
|
LDA TEMP3 ;
|
|
|
|
PUTDEH: CMP #100 ;If Number < 100
|
|
|
|
BCS PUTDET ;
|
|
|
|
JSR PUTSPC ; Print a Space
|
|
|
|
LDA TEMP3 ;
|
|
|
|
PUTDET: CMP #10 ; If Number < 10
|
|
|
|
BCS PUTDEX ;
|
|
|
|
JSR PUTSPC ; Print another Space
|
2018-02-02 23:02:06 +00:00
|
|
|
PUTDEX: RTS
|
|
|
|
|
2018-07-30 00:40:31 +00:00
|
|
|
;void putder(b) - PUT DEcimal Right justified
|
2018-07-08 05:38:36 +00:00
|
|
|
;Args: A = number to print
|
|
|
|
;Sets: TEMP0 - ones digit
|
|
|
|
; TEMP1 - tens digit
|
|
|
|
; TEMP2 - hundreds digit
|
|
|
|
; TEMP3 - number that was printed
|
2018-02-02 23:37:19 +00:00
|
|
|
PUTDER: STA TEMP3
|
|
|
|
PUTDES: JSR PUTDEH
|
|
|
|
LDA TEMP3
|
2018-07-08 05:38:36 +00:00
|
|
|
|
2018-07-30 00:40:31 +00:00
|
|
|
;void putdec(b) - PUT DECimal
|
2018-07-08 05:38:36 +00:00
|
|
|
;Args: A = number to print
|
|
|
|
;Sets: TEMP0 - ones digit
|
|
|
|
; TEMP1 - tens digit
|
|
|
|
; TEMP2 - hundreds digit
|
2018-02-02 23:02:06 +00:00
|
|
|
PUTDEC: JSR CUBCD ;Convert Accumulator to Unpacked BCD
|
|
|
|
LDA TEMP2 ;Get High Byte
|
|
|
|
BEQ PUTDE1 ;If Not Zero
|
2019-05-27 21:12:10 +00:00
|
|
|
JSR PUTDGT ; Print Digit
|
2018-02-02 23:02:06 +00:00
|
|
|
PUTDE1: LDA TEMP1 ;Get Low Byte
|
|
|
|
BNE PUTDE2 ;If Not Zero
|
|
|
|
CMP TEMP2 ; and Hundreds
|
|
|
|
BEQ PUTDE3 ; not Zero
|
2019-05-27 21:12:10 +00:00
|
|
|
PUTDE2: JSR PUTDGT ; Print Digit
|
2018-02-02 23:02:06 +00:00
|
|
|
PUTDE3: LDA TEMP0 ;Get Low Byte
|
2019-05-27 21:12:10 +00:00
|
|
|
PUTDGT: ORA #$30 ;Convert to ASCII digit
|
|
|
|
JMP PUTCHR ;And Print
|
2018-02-02 23:02:06 +00:00
|
|
|
|
2018-07-30 00:40:31 +00:00
|
|
|
;puthex(&word) - PUT HEXadecimal
|
|
|
|
PUTHEX EQU PRBYTE ;Print Byte as Hexadecimal
|
|
|
|
|
2019-05-27 21:12:10 +00:00
|
|
|
;putint(&word) - PUT INTeger
|
|
|
|
PUTINT: JSR CVIBCD ;Convert Integer to Packed BCD
|
|
|
|
LDY #4 ;Set Initial Digit Number
|
|
|
|
JSR UPBCDI ;Unpack Digit X
|
|
|
|
PUTINZ: BNE PUTINS ;If Zero
|
|
|
|
DEY ; Decrement Digit Number
|
|
|
|
BNE PUTINZ ; If Not Zero Loop
|
|
|
|
BEQ PUTDGT ; Else Print Digit and Return
|
|
|
|
PUTINL: JSR UPBCDI ;Unpack Digit X
|
|
|
|
PUTINS: JSR PUTDGT ;Print Digit
|
|
|
|
DEY ;Decrement Digit Number
|
|
|
|
BPL PUTINL ;Loop if >= Zero
|
|
|
|
RTS
|
|
|
|
|
2018-07-30 00:40:31 +00:00
|
|
|
;putwrd(&word) - PUT WoRD
|
|
|
|
;Args: Y = Word MSB
|
|
|
|
; X = Word LSB
|
|
|
|
;Calls: PUTS = Put String
|
|
|
|
; PRBYTE = Print Byte
|
|
|
|
; SAVRXY = Save X and Y Registers
|
|
|
|
;Affects: A,Y,X,N,Z,C
|
|
|
|
PUTWRD: JSR SAVRXY ;Save Address
|
|
|
|
PUTWRA: LDA TEMP2 ;Load Address MSB
|
|
|
|
JSR PRBYTE ;Print as Hexadecimal
|
|
|
|
LDA TEMP1 ;Load Address LSB
|
|
|
|
JMP PRBYTE ;Print and Return`
|
|
|
|
|
2018-02-02 23:02:06 +00:00
|
|
|
;Print a Space
|
2018-02-02 23:37:19 +00:00
|
|
|
PUTSPC: LDA #32 ;Load Space Character
|
2018-09-17 23:44:47 +00:00
|
|
|
JMP PUTCHR ;and Print it
|
2018-07-19 04:27:50 +00:00
|
|
|
|
2018-07-30 00:40:31 +00:00
|
|
|
;void printf(b, &s) - PRINT Formatted byte and/or string
|
|
|
|
;Args: A = byte to format
|
2018-07-08 05:38:36 +00:00
|
|
|
; Y,X = address of formatting string
|
2018-07-30 00:40:31 +00:00
|
|
|
;Uses: DSTLO,DSTHI = Address of %S string
|
|
|
|
;Sets: SRCLO,SRCHI = Address of formatting string
|
|
|
|
; TEMP3 - number to format
|
|
|
|
;Destroys: TEMP0,TEMP1,TEMP2
|
|
|
|
;Returns: A,Y = Total number of characters printed
|
2018-07-19 04:27:50 +00:00
|
|
|
PRINTF: JSR SETSRC ;Initialize Source String
|
|
|
|
STA TEMP3 ;Save Byte to Format
|
|
|
|
PRINTL: LDA (SRCLO),Y ;Read next character in string
|
|
|
|
BEQ PRINTX ;If Not 0
|
|
|
|
CMP #'% ;' If Format Specified
|
|
|
|
BEQ PRINTP ; Jump to Formatter
|
2018-09-17 23:44:47 +00:00
|
|
|
PRINTC: JSR PUTCHR ; Print character at offset,
|
2018-07-19 04:27:50 +00:00
|
|
|
PRINTY: INY ; increment offset, and
|
|
|
|
BPL PRINTL ; loop if less than 128
|
|
|
|
PRINTX: RTS ; characters printed
|
2018-02-02 23:02:06 +00:00
|
|
|
;Process Format Specifier
|
2018-07-19 04:27:50 +00:00
|
|
|
PRINTP: INY ;Increment Offset
|
|
|
|
LDA (SRCLO),Y ;Get Formatting Character
|
|
|
|
BEQ PRINTX ;If NUL, then Exit
|
|
|
|
CMP #'% ;'If Percent Sign
|
|
|
|
BEQ PRINTC ; Print it and Continue
|
|
|
|
AND #$DF ;Convert to Upper Case
|
|
|
|
CMP #'L ;'If "l" or "L"
|
2018-02-02 23:37:19 +00:00
|
|
|
BNE PRINTR
|
2018-07-19 04:27:50 +00:00
|
|
|
LDA TEMP3 ; Load Byte to Format
|
|
|
|
JSR PUTDEM ; Print Left Justified
|
2018-09-17 23:44:47 +00:00
|
|
|
JMP PRINTY ; and Continue Printing String
|
2018-07-19 04:27:50 +00:00
|
|
|
PRINTR: CMP #'R ;'If "r" or "R"
|
2018-02-02 23:02:06 +00:00
|
|
|
BNE PRINTD
|
2018-07-19 04:27:50 +00:00
|
|
|
LDA TEMP3 ; Load Byte to Format
|
|
|
|
JSR PUTDES ; Print Right Justified
|
2018-09-17 23:44:47 +00:00
|
|
|
JMP PRINTY ; and Continue Printing String
|
2018-07-19 04:27:50 +00:00
|
|
|
PRINTD: CMP #'D ;'Else If "d" or "D"
|
2018-02-02 23:02:06 +00:00
|
|
|
BNE PRINTH
|
2018-07-19 04:27:50 +00:00
|
|
|
LDA TEMP3 ; Load Byte to Format
|
|
|
|
JSR PUTDEC ; Print as Decimal
|
2018-09-17 23:44:47 +00:00
|
|
|
JMP PRINTY ; and Continue Printing String
|
2018-07-19 04:27:50 +00:00
|
|
|
PRINTH: CMP #'H ;'Else If "h" or "H"
|
2018-09-17 23:44:47 +00:00
|
|
|
BNE PRINTB
|
2018-07-19 04:27:50 +00:00
|
|
|
LDA TEMP3 ; Load Byte to Format
|
2018-07-30 00:40:31 +00:00
|
|
|
JSR PUTHEX ; Print as Hexadecimal
|
2018-09-17 23:44:47 +00:00
|
|
|
JMP PRINTY ; and Continue Printing String
|
|
|
|
PRINTB: CMP #'B ;'Else If "b" or "B"
|
|
|
|
BNE PRINTN
|
|
|
|
LDA TEMP3 ; Load Byte to Format
|
|
|
|
JSR PUTBIN ; Print as Binary
|
|
|
|
JMP PRINTY ; and Continue Printing String
|
2018-08-14 18:14:32 +00:00
|
|
|
PRINTN: CMP #'N ;'Else If "n" or "N"
|
|
|
|
BNE PRINTS
|
2018-12-10 22:23:04 +00:00
|
|
|
STY TEMP0 ; Save Index
|
2018-08-14 18:14:32 +00:00
|
|
|
JSR NEWLIN ; Execute Newline Function
|
2018-12-10 22:23:04 +00:00
|
|
|
LDY TEMP0 ; Restore Index
|
2018-09-17 23:44:47 +00:00
|
|
|
JMP PRINTY ; and Continue Printing String
|
2018-07-30 00:40:31 +00:00
|
|
|
PRINTS: CMP #'S ;'Else If "s" or "S"
|
|
|
|
BNE PRINTW
|
2018-07-19 04:27:50 +00:00
|
|
|
STY TEMP0 ; Save Index
|
|
|
|
JSR PUTDST ; Print Destination String
|
|
|
|
LDY TEMP0 ; Restore Index
|
|
|
|
JMP PRINTY ;
|
2018-07-30 00:40:31 +00:00
|
|
|
PRINTW: CMP #'W ;'Else If "w" or "W"
|
2019-05-27 21:12:10 +00:00
|
|
|
BNE PRINTI
|
2018-07-30 00:40:31 +00:00
|
|
|
STY TEMP0 ; Save Index
|
|
|
|
JSR SAVDST ; Save Destination Address
|
|
|
|
JSR PUTWRA ; Print MSB and LSB as Hex
|
|
|
|
LDY TEMP0 ; Restore Index
|
|
|
|
JMP PRINTY ;
|
2019-05-27 21:12:10 +00:00
|
|
|
PRINTI: CMP #'I ;'Else If "i" or "I"
|
|
|
|
BNE PRINTZ
|
|
|
|
TYA ; Save Index
|
|
|
|
PHA
|
|
|
|
JSR GETDST ; Get Integer in DSTLO, DSTHI
|
|
|
|
JSR PUTINT ; Print Integer as Decimal
|
|
|
|
PLA ; Restore Index
|
|
|
|
TAY
|
|
|
|
JMP PRINTY ;
|
2018-09-17 23:44:47 +00:00
|
|
|
PRINTZ: LDA TEMP3 ;Else
|
2018-07-19 04:27:50 +00:00
|
|
|
JMP PRINTC ; Print Raw Byte and Continue
|
|
|
|
|
2018-08-14 18:14:32 +00:00
|
|
|
;char putdst()
|
2018-07-19 04:27:50 +00:00
|
|
|
PUTDST: LDY #0 ;Initialize character offset
|
|
|
|
PUTDSL: LDA (DSTLO),Y ;Read next character in string
|
|
|
|
BEQ PUTDSX ;If Not 0
|
|
|
|
JSR PUTC ; Print character at offset,
|
|
|
|
INY ; increment offset, and
|
|
|
|
BPL PUTDSL ; loop if less than 128
|
|
|
|
PUTDSX: TYA ;Return number of
|
|
|
|
RTS ; characters printed
|