1
0
mirror of https://github.com/RevCurtisP/C02.git synced 2024-11-25 21:33:44 +00:00

Fixed putint(), restructured printf() code

This commit is contained in:
Curtis F Kaylor 2020-09-29 15:41:41 -04:00
parent a3a0b13c6f
commit 7b0b3621e2

View File

@ -3,18 +3,21 @@
ANYKEP: DC "PRESS ANY KEY...",0 ANYKEP: DC "PRESS ANY KEY...",0
; ;
;char anykey() - wait for character with ANY KEY prompt ;char anykey(nls) - wait for character with ANY KEY prompt
;Args: A = Print Newlines (0 = No, otherwise Yes)
;Calls: GETCPR, NEWLIN - Print Newline to Screen ;Calls: GETCPR, NEWLIN - Print Newline to Screen
;Affects: C,N,Z ;Affects: C,N,Z
;Returns: A = Character code of keypress ;Returns: A = Character code of keypress
ANYKEY: JSR NEWLIN ;Start at Beginning of Next Line ANYKEY: JSR NEWLIN ;Start at Beginning of Next Line
LDY #>ANYKEP ;Load Prompt High Byte .NONL LDY #>ANYKEP ;Load Prompt High Byte
LDX #<ANYKEP ;Load Prompt Low Byte LDX #<ANYKEP ;Load Prompt Low Byte
;Drop into GETCPR ;Drop into GETCPR
;char getcpr(&s) - GET Character with PRompt ;char getcpr(nls, &s) - GET Character with PRompt
;Args: Y,X = Address of Prompt String ;Args: A = Print Newlines (0 = No, otherwise Yes)
; Y,X = Address of Prompt String
;Sets: TEMP0 - Character code of keypress ;Sets: TEMP0 - Character code of keypress
; TEMP1 - Newline Flag
;Calls: PUTS - Put String to Screen ;Calls: PUTS - Put String to Screen
; GETC - Get Character from Keyboard ; GETC - Get Character from Keyboard
; NEWLIN - Print Newline to Screen ; NEWLIN - Print Newline to Screen
@ -25,7 +28,7 @@ GETCPR: JSR PUTS ;Print Prompt
STA TEMP0 ;Save Key Code STA TEMP0 ;Save Key Code
JSR NEWLIN ;Move to Next Line JSR NEWLIN ;Move to Next Line
JSR NEWLIN ;Generate Blank Line JSR NEWLIN ;Generate Blank Line
LDA TEMP0 ;Load Key Code .NONLS LDA TEMP0 ;Load Key Code
RTS RTS
;void putbin(b) - PUT Binary ;void putbin(b) - PUT Binary
@ -42,14 +45,14 @@ PUTBIN: LDY #$FF ;Set Bitmask to All 1's
PUTMSK: STA TEMP2 ;Save Byte PUTMSK: STA TEMP2 ;Save Byte
STY TEMP1 ;Save Bitmask STY TEMP1 ;Save Bitmask
LDX #8 ;Print 8 Binary Digits LDX #8 ;Print 8 Binary Digits
PUTMSL: LDA #0 ;Clear Accumulator .MASKL LDA #0 ;Clear Accumulator
ASL TEMP2 ;Shift Top Bit Out of Byte ASL TEMP2 ;Shift Top Bit Out of Byte
ADC #$30 ;Convert to '0' or '1' ADC #$30 ;Convert to '0' or '1'
ASL TEMP1 ;Shift Top Bit Out of Mask ASL TEMP1 ;Shift Top Bit Out of Mask
BCC PUTMSS ;If Set BCC .MASKP ;If Set
JSR PUTCHR ; Print Digit JSR PUTCHR ; Print Digit
PUTMSS: DEX ;Decrement Index .MASKP DEX ;Decrement Index
BNE PUTMSL ;Loop if Not Done BNE .MASKL ;Loop if Not Done
RTS RTS
;void putdel(b) - PUT DEcimal Left justified ;void putdel(b) - PUT DEcimal Left justified
@ -58,19 +61,41 @@ PUTMSS: DEX ;Decrement Index
; TEMP1 - tens digit ; TEMP1 - tens digit
; TEMP2 - hundreds digit ; TEMP2 - hundreds digit
; TEMP3 - number that was printed ; TEMP3 - number that was printed
;putdem - Alternate Entry Point if number is already in TEMP3 ;.putdel - Alternate Entry Point if number is already in TEMP3
;putdeh - Alternate Entry Point to print only padding spaces ;.spaces - Alternate Entry Point to print only padding spaces
PUTDEL: STA TEMP3 PUTDEL: STA TEMP3
PUTDEM: JSR PUTDEC ;Print Decimal Representation of number .PUTDEL JSR PUTDEC ;Print Decimal Representation of number
LDA TEMP3 ; LDA TEMP3 ;
PUTDEH: CMP #100 ;If Number < 100 .SPACES CMP #100 ;If Number < 100
BCS PUTDET ; BCS .ONES ;
JSR PUTSPC ; Print a Space JSR PUTSPC ; Print a Space
LDA TEMP3 ; LDA TEMP3 ;
PUTDET: CMP #10 ; If Number < 10 .ONES CMP #10 ; If Number < 10
BCS PUTDEX ; BCS .RETURN ;
JSR PUTSPC ; Print another Space JSR PUTSPC ; Print another Space
PUTDEX: RTS .RETURN RTS
;void putdeh(b) - PUT DEcimal Hundred
;Args: A = number to print
;Sets: TEMP0 - ones digit
; TEMP1 - tens digit
; TEMP2 - hundreds digit
PUTDEH: JSR CUBCD ;Convert Accumulator to Unpacked BCD
LDX #2 ;Print 2 Digits
BNE .DEZLP
;void putdez(b) - PUT DEcimal Zero filled
;Args: A = number to print
;Sets: TEMP0 - ones digit
; TEMP1 - tens digit
; TEMP2 - hundreds digit
PUTDEZ: JSR CUBCD ;Convert Accumulator to Unpacked BCD
LDX #3 ;Print 3 Digits
.DEZLP LDA TEMP0-1,X ;Get Byte
JSR PUTDGT ;Print Digit
DEX ;Decrement Counter
BNE .DEZLP ; And Loop if Not Done
RTS
;void putder(b) - PUT DEcimal Right justified ;void putder(b) - PUT DEcimal Right justified
;Args: A = number to print ;Args: A = number to print
@ -79,7 +104,7 @@ PUTDEX: RTS
; TEMP2 - hundreds digit ; TEMP2 - hundreds digit
; TEMP3 - number that was printed ; TEMP3 - number that was printed
PUTDER: STA TEMP3 PUTDER: STA TEMP3
PUTDES: JSR PUTDEH .PUTDER JSR .SPACES
LDA TEMP3 LDA TEMP3
;void putdec(b) - PUT DECimal ;void putdec(b) - PUT DECimal
@ -89,73 +114,79 @@ PUTDES: JSR PUTDEH
; TEMP2 - hundreds digit ; TEMP2 - hundreds digit
PUTDEC: JSR CUBCD ;Convert Accumulator to Unpacked BCD PUTDEC: JSR CUBCD ;Convert Accumulator to Unpacked BCD
LDA TEMP2 ;Get High Byte LDA TEMP2 ;Get High Byte
BEQ PUTDE1 ;If Not Zero BEQ .DIGIT1 ;If Not Zero
JSR PUTDGT ; Print Digit JSR PUTDGT ; Print Digit
PUTDE1: LDA TEMP1 ;Get Low Byte .DIGIT1 LDA TEMP1 ;Get Low Byte
BNE PUTDE2 ;If Not Zero BNE .DIGIT2 ;If Not Zero
CMP TEMP2 ; and Hundreds CMP TEMP2 ; and Hundreds
BEQ PUTDE3 ; not Zero BEQ .DIGIT3 ; not Zero
PUTDE2: JSR PUTDGT ; Print Digit .DIGIT2 JSR PUTDGT ; Print Digit
PUTDE3: LDA TEMP0 ;Get Low Byte .DIGIT3 LDA TEMP0 ;Get Low Byte
PUTDGT: ORA #$30 ;Convert to ASCII digit PUTDGT: ORA #$30 ;Convert to ASCII digit
JMP PUTCHR ;And Print JMP PUTCHR ;And Print
;puthex(b) - PUT HEXadecimal ;puthex(b) - PUT HEXadecimal
PUTHEX EQU PRBYTE ;Print Byte as Hexadecimal PUTHEX EQU PRBYTE ;Print Byte as Hexadecimal
;putint(&word) - PUT INTeger ;putinr(&.HEXW) - PUT INteger Right justified
PUTINT: JSR CVIBCD ;Convert Integer to Packed BCD PUTINR: SEC ;Mode = Justified
BCS .PUTINT ;Print Integer
;putint(&.HEXW) - PUT INTeger
PUTINT: CLC ;Mode = Not Justified
.PUTINT JSR CVIBCD ;Convert Integer to Packed BCD
LDY #4 ;Set Initial Digit Number LDY #4 ;Set Initial Digit Number
PUTINZ: JSR UPBCDI ;Unpack Digit X .IZERO JSR UPBCDI ;Unpack Digit X
BNE PUTINS ;If Zero BNE .IDIGIT ;If Zero
DEY ; Decrement Digit Number BCC .INEXT ; If Justified
BNE PUTINZ ; If Not Zero Loop JSR PUTSPC ; Print a Space
BEQ PUTDGT ; Else Print Digit and Return .INEXT DEY ; Decrement Digit Number
PUTINL: JSR UPBCDI ;Unpack Digit X BNE .IZERO ; and If Not Zero Loop
PUTINS: JSR PUTDGT ;Print Digit .ILOOP JSR UPBCDI ;Unpack Digit X
.IDIGIT JSR PUTDGT ;Print Digit
DEY ;Decrement Digit Number DEY ;Decrement Digit Number
BPL PUTINL ;Loop if >= Zero BPL .ILOOP ;Loop if >= Zero
RTS RTS
;putnyb(b) - PUT NYBble ;putnyb(b) - PUT NYBble
PUTNYB EQU PRHEX ;Print Nybble as Hexadecimal PUTNYB EQU PRHEX ;Print Nybble as Hexadecimal
;putsqb(&word) - PUT SesQuiByte ;putsqb(&.HEXW) - PUT SesQuiByte
;Args: Y = High Nybble ;Args: Y = High Nybble
; X = Low Byte ; X = Low Byte
;Calls: PRBYTE = Print Byte ;Calls: PUTHEX = Print Byte
; PRHEZ = Print Hex Digit ; PRHEX = Print Hex Digit
; SAVRXY = Save X and Y Registers ; SAVRXY = Save X and Y Registers
;Affects: A,Y,X,N,Z,C ;Affects: A,Y,X,N,Z,C
PUTSQB: JSR SAVRXY ;Save Address PUTSQB: JSR SAVRXY ;Save Address
PUTSQA: LDA TEMP2 ;Load Address MSB .PUTSQB LDA TEMP2 ;Load Address MSB
JSR PRHEX ;Print High Nybble JSR PRHEX ;Print High Nybble
LDA TEMP1 ;Load Address LSB LDA TEMP1 ;Load Address LSB
JMP PRBYTE ;Print and Return` JMP PUTHEX ;Print and Return`
;putexh(&word) - PUT EXtended Hexadecimal ;putexh(&.HEXW) - PUT EXtended Hexadecimal
;Args: A = Value Extended Byte ;Args: A = Value Extended Byte
; Y = Value High Byte ; Y = Value High Byte
; X = Value Low Byte ; X = Value Low Byte
;Calls: PRBYTE = Print Byte ;Calls: PUTHEX = Print Byte
; SAVRXY = Save X and Y Registers ; SAVRXY = Save X and Y Registers
; PUTWRA = Put Word (Alternate Entry Point) ; .PUTWRD = Put .HEXW (Alternate Entry Point)
;Affects: A,Y,X,N,Z,C ;Affects: A,Y,X,N,Z,C
PUTEXH: JSR SAVRXY ;Save High and Low Bytes PUTEXH: JSR SAVRXY ;Save High and Low Bytes
JSR PRBYTE ;Print Extended Byte JSR PUTHEX ;Print Extended Byte
JMP PUTWRA ;Print High and Low Bytes JMP .PUTWRD ;Print High and Low Bytes
;putwrd(&word) - PUT WoRD ;putwrd(&.HEXW) - PUT .HEXW
;Args: Y = Word MSB ;Args: Y = .HEXW MSB
; X = Word LSB ; X = .HEXW LSB
;Calls: PRBYTE = Print Byte ;Calls: PUTHEX = Print Byte
; SAVRXY = Save X and Y Registers ; SAVRXY = Save X and Y Registers
;Affects: A,Y,X,N,Z,C ;Affects: A,Y,X,N,Z,C
PUTWRD: JSR SAVRXY ;Save Word PUTWRD: JSR SAVRXY ;Save .HEXW
PUTWRA: LDA TEMP2 ;Load Word MSB .PUTWRD LDA TEMP2 ;Load .HEXW MSB
JSR PRBYTE ;Print as Hexadecimal JSR PUTHEX ;Print as Hexadecimal
LDA TEMP1 ;Load Word LSB LDA TEMP1 ;Load .HEXW LSB
JMP PRBYTE ;Print and Return` JMP PUTHEX ;Print and Return`
;Print a Space ;Print a Space
PUTSPC: LDA #32 ;Load Space Character PUTSPC: LDA #32 ;Load Space Character
@ -181,97 +212,116 @@ PUTRPT: JSR PUTCHR ;Print Space Character
;Returns: A,Y = Total number of characters printed ;Returns: A,Y = Total number of characters printed
PRINTF: STA TEMP3 ;Save Byte to Format PRINTF: STA TEMP3 ;Save Byte to Format
JSR SETSRC ;Initialize Source String JSR SETSRC ;Initialize Source String
PRINTL: LDA (SRCLO),Y ;Read next character in string .FLOOP LDA (SRCLO),Y ;Read next character in string
BEQ PRINTX ;If Not 0 BEQ .FDONE ;If Not 0
CMP #'% ;' If Format Specified CMP #'%' ; If Format Specified
BEQ PRINTP ; Jump to Formatter BEQ .FEVAL ; Jump to Formatter
PRINTC: JSR PUTCHR ; Print character at offset, .FPUTC JSR PUTCHR ; Print character at offset,
PRINTY: INY ; increment offset, and .FNEXT INY ; increment offset, and
BPL PRINTL ; loop if less than 128 BPL .FLOOP ; loop if less than 128
PRINTX: RTS ; characters printed .FDONE TYA ;Return Number of
RTS ; characters printed
;Process Format Specifier ;Process Format Specifier
PRINTP: INY ;Increment Offset .FEVAL: INY ;Increment Offset
LDA (SRCLO),Y ;Get Formatting Character LDA (SRCLO),Y ;Get Formatting Character
BEQ PRINTX ;If NUL, then Exit BEQ .FDONE ;If NUL, then Exit
CMP #'% ;'If Percent Sign CMP #'%' ;If Percent Sign
BEQ PRINTC ; Print it and Continue BEQ .FPUTC ; Print it and Continue
AND #$DF ;Convert to Upper Case AND #$DF ;Convert to Upper Case
CMP #'L ;'If "l" or "L" .HNDRD CMP #'H' ;If "z" or "Z"
BNE PRINTR BNE .ZEROF
LDA TEMP3 ; Load Byte to Format LDA TEMP3 ; Load Byte to Format
JSR PUTDEM ; Print Left Justified JSR PUTDEH ; Print Zero Filled
JMP PRINTY ; and Continue Printing String JMP .FNEXT ; and Continue .INTGRng String
PRINTR: CMP #'R ;'If "r" or "R" .ZEROF CMP #'Z' ;If "z" or "Z"
BNE PRINTD BNE .LJUST
LDA TEMP3 ; Load Byte to Format LDA TEMP3 ; Load Byte to Format
JSR PUTDES ; Print Right Justified JSR PUTDEZ ; Print Zero Filled
JMP PRINTY ; and Continue Printing String JMP .FNEXT ; and Continue .INTGRng String
PRINTD: CMP #'D ;'Else If "d" or "D" .LJUST CMP #'L' ;If "l" or "L"
BNE PRINTG BNE .RJUST
LDA TEMP3 ; Load Byte to Format
JSR .PUTDEL ; Print Left Justified
JMP .FNEXT ; and Continue .INTGRng String
.RJUST CMP #'R' ;If "r" or "R"
BNE .DECML
LDA TEMP3 ; Load Byte to Format
JSR .PUTDER ; Print Right Justified
JMP .FNEXT ; and Continue .INTGRng String
.DECML CMP #'D' ;Else If "d" or "D"
BNE .NYBBL
LDA TEMP3 ; Load Byte to Format LDA TEMP3 ; Load Byte to Format
JSR PUTDEC ; Print as Decimal JSR PUTDEC ; Print as Decimal
JMP PRINTY ; and Continue Printing String JMP .FNEXT ; and Continue .INTGRng String
PRINTG: CMP #'G ;'Else If "g" or "G" .NYBBL CMP #'Y' ;Else If "y" or "Y"
BNE PRINTH BNE .HEXB
LDA TEMP3 ; Load Byte to Format LDA TEMP3 ; Load Byte to Format
JSR PUTNYB ; Print as Low Nybble as Hexadecimal JSR PUTNYB ; Print as Low Nybble as Hexadecimal
JMP PRINTY ; and Continue Printing String JMP .FNEXT ; and Continue .INTGRng String
PRINTH: CMP #'H ;'Else If "h" or "H" .HEXB CMP #'X' ;Else If "x" or "Y"
BNE PRINTB BNE .BNRY
LDA TEMP3 ; Load Byte to Format LDA TEMP3 ; Load Byte to Format
JSR PUTHEX ; Print as Hexadecimal JSR PUTHEX ; Print as Hexadecimal
JMP PRINTY ; and Continue Printing String JMP .FNEXT ; and Continue .INTGRng String
PRINTB: CMP #'B ;'Else If "b" or "B" .BNRY CMP #'B' ;Else If "b" or "B"
BNE PRINTN BNE .NEWLN
STY TEMP0 ; Save Index STY TEMP0 ; Save Index
LDA TEMP3 ; Load Byte to Format LDA TEMP3 ; Load Byte to Format
JSR PUTBIN ; Print as Binary JSR PUTBIN ; Print as Binary
LDY TEMP0 ; Restore Index LDY TEMP0 ; Restore Index
JMP PRINTY ; and Continue Printing String JMP .FNEXT ; and Continue .INTGRng String
PRINTN: CMP #'N ;'Else If "n" or "N" .NEWLN CMP #'N' ;Else If "n" or "N"
BNE PRINTS BNE .STRNG
STY TEMP0 ; Save Index STY TEMP0 ; Save Index
JSR NEWLIN ; Execute Newline Function JSR NEWLIN ; Execute Newline Function
LDY TEMP0 ; Restore Index LDY TEMP0 ; Restore Index
JMP PRINTY ; and Continue Printing String JMP .FNEXT ; and Continue .INTGRng String
PRINTS: CMP #'S ;'Else If "s" or "S" .STRNG CMP #'S' ;Else If "s" or "S"
BNE PRINTQ BEQ .FDEST ; Print Destination String
STY TEMP0 ; Save Index .SQBYT CMP #'Q' ;Else If "w" or "W"
JSR PUTDST ; Print Destination String BEQ .FDEST ; Print Sesquibyte as Hex
LDY TEMP0 ; Restore Index .HEXW CMP #'W' ;Else If "w" or "W"
JMP PRINTY ; BEQ .FDEST ; Print Word as Hex
PRINTQ: CMP #'Q ;'Else If "w" or "W" .INTGR CMP #'I' ;Else If "i" or "I"
BNE PRINTW BEQ .FDEST ; Print Decimal Integer
STY TEMP0 ; Save Index .IJUST CMP #'J' ;Else If "j" or "J"
JSR SAVDST ; Save Destination Address BEQ .FDEST ; Print Right-Justified Decimal
JSR PUTSQA ; Print MSB and LSB as Hex .FECHO LDA TEMP3 ;Else
LDY TEMP0 ; Restore Index JMP .FPUTC ; Print Raw Byte and Continue
JMP PRINTY ;
PRINTW: CMP #'W ;'Else If "w" or "W" ;Process Value in DSTPTR
BNE PRINTI .FDEST TAX ;Save Specifier
STY TEMP0 ; Save Index TYA ;Save Index
JSR SAVDST ; Save Destination Address
JSR PUTWRA ; Print MSB and LSB as Hex
LDY TEMP0 ; Restore Index
JMP PRINTY ;
PRINTI: CMP #'I ;'Else If "i" or "I"
BNE PRINTZ
TYA ; Save Index
PHA PHA
JSR GETDST ; Get Integer in DSTLO, DSTHI TXA ;Restore Specifier
JSR PUTINT ; Print Integer as Decimal JSR GETDST ;Get Integer in DSTLO, DSTHI
PLA ; Restore Index CMP #'I' ;If 'i' or 'I'
BNE .FDESTJ
JSR PUTINT ; Print Integer Right Justified
BMI .FSKIP
.FDESTJ CMP #'J' ;Else If 'J' or 'J'
BNE .FDESTQ
JSR PUTINR ;Print Integer Right Justified
BMI .FSKIP
.FDESTQ CMP #'Q' ;Else If 'q' or 'Q'
BNE .FDESTW
JSR .PUTSQB ; Print MSB and LSB as Hex
BNE .FSKIP
.FDESTW CMP #'W' ;Else If 'w' or 'J'
BNE .FDESTS
JSR PUTWRD ; Print MSB and LSB as Hex
BNE .FSKIP
.FDESTS JSR PUTDST ;Else Print Destination String
.FSKIP PLA ;Restore Index
TAY TAY
JMP PRINTY ; JMP .FNEXT
PRINTZ: LDA TEMP3 ;Else
JMP PRINTC ; Print Raw Byte and Continue
;char putdst() ;char putdst()
PUTDST: LDY #0 ;Initialize character offset PUTDST LDY #0 ;Initialize character offset
PUTDSL: LDA (DSTLO),Y ;Read next character in string .DLOOP LDA (DSTLO),Y ;Read next character in string
BEQ PUTDSX ;If Not 0 BEQ .DRETY ;If Not 0
JSR PUTC ; Print character at offset, JSR PUTC ; Print character at offset,
INY ; increment offset, and INY ; increment offset, and
BPL PUTDSL ; loop if less than 128 BPL .DLOOP ; loop if less than 128
PUTDSX: TYA ;Return number of .DRETY TYA ;Return number of
RTS ; characters printed RTS ; characters printed