From a1294860cd8bbd7550da603c449076af3d596ab0 Mon Sep 17 00:00:00 2001 From: Curtis F Kaylor Date: Fri, 2 Feb 2018 18:02:06 -0500 Subject: [PATCH] Added putder() to stdiox.a02 --- include/stdiox.a02 | 82 ++++++++++++++++++++++++++++++++++++++++++++++ include/stdiox.h02 | 13 ++++++++ include/stdlib.a02 | 56 +++++++++++++++++++------------ 3 files changed, 130 insertions(+), 21 deletions(-) create mode 100644 include/stdiox.a02 create mode 100644 include/stdiox.h02 diff --git a/include/stdiox.a02 b/include/stdiox.a02 new file mode 100644 index 0000000..f565e02 --- /dev/null +++ b/include/stdiox.a02 @@ -0,0 +1,82 @@ +; C02 library stdiox.h02 assembly language subroutines + +;Print Byte as Left Justified Decimal Number +;putdel(b) +PUTDEL: STA TEMP3 +PUTDEM: JSR PUTDEC ;Alternate Entry Point + LDA TEMP3 +PUTDEH: CMP #100 + BCS PUTDE0 + JSR PUTSPC +PUTDE0: CMP #10 + BCS PUTDEX + JSR PUTSPC +PUTDEX: RTS + +;Print Byte as Right Justified Decimal Number +;putder(b) +PUTDER: CMP #100 + BCS PUTDE0 + JSR PUTSPC +PUTDE0: CMP #10 + BCS PUTDEC + JSR PUTSPC +;Print Byte as Decimal Number +;putdec(b) +PUTDEC: JSR CUBCD ;Convert Accumulator to Unpacked BCD + LDA TEMP2 ;Get High Byte + BEQ PUTDE1 ;If Not Zero + JSR PUTDEP ; Convert Low Nybble +PUTDE1: LDA TEMP1 ;Get Low Byte + BNE PUTDE2 ;If Not Zero + CMP TEMP2 ; and Hundreds + BEQ PUTDE3 ; not Zero +PUTDE2: JSR PUTDEP ; Convert It +PUTDE3: LDA TEMP0 ;Get Low Byte +PUTDEP: ORA #$30 ;Convert to ASCII digit + JSR PRCHR ;And Print + RTS + +;Print a Space +PUTSPC: PHA ;Save Accumulator + LDA #32 ;Load Space Character + JSR PRCHR ;and Print it + PLA ;Restore Accumulator + RTS + +;Print Byte in Formatted String +;printf(b, &s) +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 PRINTS ; Jump to Formatter +PRINTC: JSR PRCHR ; Print character at offset, +PRINTY: INY ; increment offset, and + BPL PRINTL ; loop if less than 128 +PRINTX: RTS ; characters printed +;Process Format Specifier +PRINTS: 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 #'R ;If "d" or "D" + BNE PRINTD + LDA TEMP3 ; Load Byte to Format + JSR PUTDER ; Print as Decimal + JMP PRINTY ; and Continue Printing Screen +PRINTD: CMP #'D ;If "d" or "D" + BNE PRINTH + LDA TEMP3 ; Load Byte to Format + JSR PUTDEC ; Print as Decimal + JMP PRINTY ; and Continue Printing Screen +PRINTH: CMP #'H ;Else If "h" or "H" + BNE PRINTB + LDA TEMP3 ; Load Byte to Format + JSR PRBYTE ; Print as Hexadecimal + JMP PRINTY ; and Continue Printing Screen +PRINTB: LDA TEMP3 ;Otherwise + JMP PRINTC ; Print Raw Byte and Continue diff --git a/include/stdiox.h02 b/include/stdiox.h02 new file mode 100644 index 0000000..205e15e --- /dev/null +++ b/include/stdiox.h02 @@ -0,0 +1,13 @@ +/****************************************** + * stdiox - Extended I/O Routines for C02 * + ******************************************/ + +/* Print Byte as Decimal Number * + * Args: b - Number to print */ +void putdec(); + +/* Print Formatted Byte to Screen * + * Args: b - byte to format * + * &s - formatting string */ +void printf(); + \ No newline at end of file diff --git a/include/stdlib.a02 b/include/stdlib.a02 index 0bddc1d..3b1a411 100644 --- a/include/stdlib.a02 +++ b/include/stdlib.a02 @@ -123,39 +123,54 @@ ATOCL: LDA (SRCLO),Y ;Get Next Character ATOCX: LDA TEMP0 ;Load Result RTS ;And Return -;Convert Byte to ASCII +;Convert Byte to ASCII String ;Uses SRCLO, srchi, TEMP0, TEMP1, TEMP2 ;Affects A,X,Y,N,Z -CTOA: JSR SETSRC ;Initialize Source String - JSR CVBCD ;Convert Accumulator to BCD - ;LDY #$00 ;Set String Offset to 0 +CTOA: JSR SETSRC ;Initialize Source String + JSR CUBCD ;Convert Accumulator to Unpacked BCD LDA TEMP2 ;Get High Byte - BEQ CTOAL ;If Not Zero + BEQ CTOA1 ;If Not Zero JSR CTOAN ; Convert Low Nybble -CTOAL: LDA TEMP1 ;Get Low Byte +CTOA1: LDA TEMP1 ;Get Low Byte + BNE CTOA2 ;If Not Zero + CMP TEMP2 ; and Hundreds + BEQ CTOA3 ; not Zero +CTOA2: JSR CTOAN ; Convert It +CTOA3: LDA TEMP0 ;Get Low Byte + JSR CTOAN ;and Convert Low Nybble + LDA #$00 + BEQ CTOAX ;Terminate String +CTOAN: ORA #$30 ;Convert to ASCII digit +CTOAX: STA (SRCLO),Y ;Store in StringCTOAS: STA (SRCLO),Y ;Store in String + INY ;and Increment Offset + RTS + +;Unpack 3-Digit BCD Number +;Input: TEMP1 = Low Byte +; TEMP2 = High Nybble +;Output: TEMP0 = Ones Digit +; TEMP1 = Tens Digit +; TEMP2 = Hundreds Digit +;Affects: A,N,Z +;Returns: X = 0 +CUBCD: JSR CVBCD ;Convert Accumulator to BCD +UPBCD: LDA TEMP1 ;Get Low Byte + AND #$0F ;Strip High Nybbleet + STA TEMP0 ;Save in Ones + LDA TEMP1 ;Get Low Byte LSR ;Shift High Nybble LSR ; into Low Nybble LSR LSR - BNE CTOA2 ;If Not Zero - CMP TEMP2 ; and High Byte - BEQ CTOA3 ; not Zero -CTOA2: JSR CTOAN ; Convert It -CTOA3: LDA TEMP1 ;Get Low Byte - JSR CTOAN ;and Convert Low Nybble - LDA #$00 - BEQ CTOAX ;Terminate String -CTOAN: AND #$0F ;Strip High Nybble - ORA #$30 ;Convert to ASCII digit -CTOAX: STA (SRCLO),Y ;Store in String - INY ;and Increment Offset + STA TEMP1 ;Save in Tens RTS + ;Convert Binary Number in Accumulator to Binary Coded Decimal -;Uses TEMP0 +;Uses: TEMP0 ;Returns BCD in TEMP1,temp and A,X = 0 CVBCD: STA TEMP0 ;Save Binary Value - LDA #0 ;Clear BCD Bytes +CVBCDT: LDA #0 ;Clear BCD Bytes STA TEMP1 STA TEMP2 LDX #8 ;Process 8 bits of Binary @@ -173,4 +188,3 @@ CVBCDL: ASL TEMP0 ;Shift High Bit Into Carry CLD ;Clear Decimal Mode CLI ;Enable Interrupts RTS -