2 ; _printf: Basic layer for all printf type functions.
4 ; Ullrich von Bassewitz, 21.10.2000
9 .import popax, pushax, pusheax, push1, axlong, axulong
12 .import _strlower, _strlen
14 .importzp sp, ptr1, tmp1, regbank, sreg
18 ; ----------------------------------------------------------------------------
19 ; We will store variables into the register bank in the zeropage. Define
20 ; equates for these variables.
22 ArgList = regbank+0 ; Argument list pointer
23 Format = regbank+2 ; Format string
24 OutData = regbank+4 ; Function parameters
26 ; ----------------------------------------------------------------------------
27 ; Other zero page cells
34 ; ----------------------------------------------------------------------------
35 ; Get one character from the format string and increment the pointer. Will
47 ; ----------------------------------------------------------------------------
48 ; Output a pad character: outfunc (d, &padchar, 1)
53 ; ----------------------------------------------------------------------------
54 ; Call the output function with one character in A
63 jmp (OutFunc) ; fout (OutData, &CharArg, 1)
65 ; ----------------------------------------------------------------------------
66 ; Decrement the argument list pointer by 2
76 ; ----------------------------------------------------------------------------
77 ; Get an unsigned int or long argument depending on the IsLong flag.
80 lda IsLong ; Check flag
81 bne GetLongArg ; Long sets all
82 jsr GetIntArg ; Get an integer argument
83 jmp axulong ; Convert to unsigned long
85 ; ----------------------------------------------------------------------------
86 ; Get an signed int or long argument depending on the IsLong flag.
89 lda IsLong ; Check flag
90 bne GetLongArg ; Long sets all
91 jsr GetIntArg ; Get an integer argument
92 jmp axlong ; Convert to long
94 ; ----------------------------------------------------------------------------
95 ; Get a long argument from the argument list. Returns 0 in Y.
98 jsr GetIntArg ; Get high word
102 ; Run into GetIntArg fetching the low word
104 ; ----------------------------------------------------------------------------
105 ; Get an integer argument from the argument list. Returns 0 in Y.
116 ; ----------------------------------------------------------------------------
117 ; Read an integer from the format string. Will return zero in Y.
122 sty ptr1+1 ; Start with zero
123 @Loop: lda (Format),y ; Get format string character
124 sub #'0' ; Make number from ascii digit
125 bcc @L9 ; Jump if done
127 bcs @L9 ; Jump if done
129 ; Skip the digit character
133 ; Add the digit to the value we have in ptr1
135 pha ; Save digit value
141 rol ptr1+1 ; * 4, assume carry clear
148 rol ptr1+1 ; * 10, assume carry clear
150 adc ptr1 ; Add digit value
154 bcs @Loop ; Branch always
156 ; We're done converting
159 ldx ptr1+1 ; Load result
163 ; ----------------------------------------------------------------------------
164 ; Put a character into the argument buffer and increment the buffer index
171 ; ----------------------------------------------------------------------------
172 ; Get a pointer to the current buffer end and push it onto the stack
182 ; ----------------------------------------------------------------------------
183 ; Push OutData onto the software stack
190 ; ----------------------------------------------------------------------------
191 ; Output Width pad characters
203 ; ----------------------------------------------------------------------------
204 ; Output the argument itself: outfunc (d, str, arglen);
217 ; ----------------------------------------------------------------------------
218 ; ltoa: Wrapper for _ltoa that pushes all arguments
220 ltoa: sty Base ; Save base
221 jsr pusheax ; Push value
222 jsr PushBufPtr ; Push the buffer pointer...
223 lda Base ; Restore base
224 jmp _ltoa ; ultoa (l, s, base);
227 ; ----------------------------------------------------------------------------
228 ; ultoa: Wrapper for _ultoa that pushes all arguments
230 ultoa: sty Base ; Save base
231 jsr pusheax ; Push value
232 jsr PushBufPtr ; Push the buffer pointer...
233 lda Base ; Restore base
234 jmp _ultoa ; ultoa (l, s, base);
237 ; ----------------------------------------------------------------------------
242 ; Save the register bank variables into the save area
250 ; Get the parameters from the stack
252 jsr popax ; Argument list pointer
256 jsr popax ; Format string
260 jsr popax ; Output descriptor
264 ; Initialize the output counter in the output descriptor to zero
272 ; Get the output function from the output descriptor and remember it
281 ; Start parsing the format string
284 jsr GetFormatChar ; Get one char, zero in Y
285 tax ; End of format string reached?
286 bne NotDone ; Continue of end not reached
288 ; End of format string reached. Restore the zeropage registers and return.
297 ; Still a valid format character. Check for '%' and a '%%' sequence. Output
298 ; anything that is not a format specifier. On intro, Y is zero.
303 lda (Format),y ; Check for "%%"
305 bne FormatSpec ; Jump if really a format specifier
306 jsr IncFormatPtr ; Skip the second '%'
307 @L1: jsr Output1 ; Output the character...
308 jmp MainLoop ; ...and continue
310 ; We have a real format specifier
311 ; Format is: %[flags][width][.precision][mod]type
312 ; Y is zero on entry.
316 ; Initialize the flags
320 @L1: sta FormatVars,x
324 ; Start with reading the flags if there are any. X is $FF which is used
328 lda (Format),y ; Get next char...
348 @L4: jsr IncFormatPtr
349 jmp ReadFlags ; ...and start over
351 ; Done with flags, read the pad char. Y is still zero if we come here.
359 lda (Format),y ; Read current for later
362 ; Read the width. Even here, Y is still zero. A contains the current character
363 ; from the format string
369 jsr GetIntArg ; Width is an additional argument
372 @L1: jsr ReadInt ; Read integer from format string...
374 stx Width+1 ; ...and remember in Width
376 ; Read the precision. Even here, Y is still zero.
378 sty Prec ; Assume Precision is zero
380 lda (Format),y ; Load next format string char
381 cmp #'.' ; Precision given?
382 bne ReadMod ; Branch if no precision given
385 jsr IncFormatPtr ; Skip the '.'
387 cmp #'*' ; Variable precision?
389 jsr IncFormatPtr ; Skip the '*'
390 jsr GetIntArg ; Get integer argument
393 @L1: jsr ReadInt ; Read integer from format string
397 ; Read the modifiers. Y is still zero.
402 beq @L1 ; Read and ignore this one
404 beq @L1 ; Read and ignore this one
406 beq @L1 ; Read and ignore this one
408 beq @L1 ; Read and ignore this one
413 @L1: jsr IncFormatPtr
416 ; Initialize the argument buffer pointers. We use a static buffer (ArgBuf) to
417 ; assemble strings. A zero page index (BufIdx) is used to keep the current
418 ; write position. A pointer to the buffer (Str) is used to point to the the
419 ; argument in case we will not use the buffer but a user supplied string.
420 ; Y is zero when we come here.
423 sty BufIdx ; Clear BufIdx
429 ; Skip the current format character, then check it (current char in A)
440 jsr GetIntArg ; Get the argument (promoted to int)
441 sta Buf ; Place it as zero terminated string...
443 sta Buf+1 ; ...into the buffer
457 lda AddBlank ; Add a blank for positives?
460 @L2: lda AddSign ; Add a plus for positives (precedence)?
467 jsr GetSignedArg ; Get argument as a long
468 ldy sreg+1 ; Check sign
475 @Int1: ldy #10 ; Base
476 jsr ltoa ; Push arguments, call _ltoa
479 ; Is it a count pseudo format?
485 ; It is a count pseudo argument
489 stx ptr1+1 ; Get user supplied pointer
491 lda (OutData),y ; Low byte of OutData->ccount
494 lda (OutData),y ; High byte of OutData->ccount
498 ; Check for an octal digit
504 ; Integer in octal representation
506 jsr GetSignedArg ; Get argument as a long
507 ldy AltForm ; Alternative form?
508 beq @Oct1 ; Jump if no
509 pha ; Save low byte of value
515 ora Prec+1 ; Check if value or Prec != 0
519 pla ; Restore low byte
521 @Oct1: ldy #8 ; Load base
522 jsr ltoa ; Push arguments, call _ltoa
525 ; Check for a string specifier (%s)
533 jsr GetIntArg ; Get 16bit argument
538 ; Check for an unsigned integer (%u)
544 ; It's an unsigned integer
546 jsr GetUnsignedArg ; Get argument as unsigned long
548 jsr ultoa ; Push arguments, call _ultoa
551 ; Check for a hexadecimal integer (%x)
559 ; Hexadecimal integer
561 @IsHex: pha ; Save the format spec
569 @L1: jsr GetUnsignedArg ; Get argument as an unsigned long
571 jsr ultoa ; Push arguments, call _ultoa
573 pla ; Get the format spec
574 cmp #'x' ; Lower case?
578 jsr _strlower ; Make characters lower case
581 ; Unsigned format character, skip it
586 ; We have the argument, do argument string formatting
590 ; ArgLen = strlen (Str);
594 jsr _strlen ; Get length of argument
598 ; if (Prec && Prec < ArgLen) ArgLen = Prec;
612 ; if (Width > ArgLen) {
613 ; Width -= ArgLen; /* padcount */
617 ; Since width is used as a counter below, calculate -(width+1)
634 ; /* Do padding on the left side if needed */
636 ; /* argument right justified */
638 ; fout (d, &padchar, 1);
647 ; Output the argument itself
651 ; /* Output right padding bytes if needed */
653 ; /* argument left justified */
655 ; fout (d, &padchar, 1);
664 ; Done, parse next chars from format string
669 ; ----------------------------------------------------------------------------
670 ; Local data (all static)
674 ; Save area for the zero page registers
677 ; Stuff from OutData. Is used as a vector and must be aligned
680 ; One character argument for OutFunc
694 BufIdx: .byte 0 ; Argument string pointer
695 FormatVarSize = * - FormatVars
697 ; Argument buffer and pointer