2 ; _printf: Basic layer for all printf type functions.
4 ; Ullrich von Bassewitz, 21.10.2000
9 .import popax, pushax, pusheax, decsp6, push1, axlong, axulong
12 .import _strlower, _strlen
13 .importzp sp, ptr1, ptr2, tmp1, regbank, sreg
17 ; ----------------------------------------------------------------------------
18 ; We will store variables into the register bank in the zeropage. Define
19 ; equates for these variables.
21 ArgList = regbank+0 ; Argument list pointer
22 Format = regbank+2 ; Format string
23 OutData = regbank+4 ; Function parameters
25 ; ----------------------------------------------------------------------------
26 ; 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
64 jmp (OutFunc) ; fout (OutData, &CharArg, 1)
66 ; ----------------------------------------------------------------------------
67 ; Decrement the argument list pointer by 2
77 ; ----------------------------------------------------------------------------
78 ; Get an unsigned int or long argument depending on the IsLong flag.
81 lda IsLong ; Check flag
82 bne GetLongArg ; Long sets all
83 jsr GetIntArg ; Get an integer argument
84 jmp axulong ; Convert to unsigned long
86 ; ----------------------------------------------------------------------------
87 ; Get an signed int or long argument depending on the IsLong flag.
90 lda IsLong ; Check flag
91 bne GetLongArg ; Long sets all
92 jsr GetIntArg ; Get an integer argument
93 jmp axlong ; Convert to long
95 ; ----------------------------------------------------------------------------
96 ; Get a long argument from the argument list. Returns 0 in Y.
99 jsr GetIntArg ; Get high word
103 ; Run into GetIntArg fetching the low word
105 ; ----------------------------------------------------------------------------
106 ; Get an integer argument from the argument list. Returns 0 in Y.
117 ; ----------------------------------------------------------------------------
118 ; Read an integer from the format string. Will return zero in Y.
123 sty ptr1+1 ; Start with zero
124 @Loop: lda (Format),y ; Get format string character
125 sub #'0' ; Make number from ascii digit
126 bcc @L9 ; Jump if done
128 bcs @L9 ; Jump if done
130 ; Skip the digit character
134 ; Add the digit to the value we have in ptr1
136 pha ; Save digit value
142 rol ptr1+1 ; * 4, assume carry clear
149 rol ptr1+1 ; * 10, assume carry clear
151 adc ptr1 ; Add digit value
155 bcs @Loop ; Branch always
157 ; We're done converting
160 ldx ptr1+1 ; Load result
164 ; ----------------------------------------------------------------------------
165 ; Put a character into the argument buffer and increment the buffer index
172 ; ----------------------------------------------------------------------------
173 ; Get a pointer to the current buffer end and push it onto the stack
183 ; ----------------------------------------------------------------------------
184 ; Push OutData onto the software stack
191 ; ----------------------------------------------------------------------------
192 ; Output Width pad characters
204 ; ----------------------------------------------------------------------------
205 ; Output the argument itself: outfunc (d, str, arglen);
218 ; ----------------------------------------------------------------------------
219 ; ltoa: Wrapper for _ltoa that pushes all arguments
221 ltoa: sty Base ; Save base
222 jsr pusheax ; Push value
223 jsr PushBufPtr ; Push the buffer pointer...
224 lda Base ; Restore base
225 jmp _ltoa ; ultoa (l, s, base);
228 ; ----------------------------------------------------------------------------
229 ; ultoa: Wrapper for _ultoa that pushes all arguments
231 ultoa: sty Base ; Save base
232 jsr pusheax ; Push value
233 jsr PushBufPtr ; Push the buffer pointer...
234 lda Base ; Restore base
235 jmp _ultoa ; ultoa (l, s, base);
238 ; ----------------------------------------------------------------------------
243 ; Save the register bank variables into the save area
245 pha ; Save low byte of ap
252 ; Get the parameters from the stack
254 pla ; Restore low byte of ap
255 sta ArgList ; Argument list pointer
258 jsr popax ; Format string
262 jsr popax ; Output descriptor
266 ; Initialize the output counter in the output descriptor to zero
274 ; Get the output function from the output descriptor and remember it
283 ; Start parsing the format string
286 lda Format ; Remember current format pointer
292 @L1: lda (Format),y ; Get next char
293 beq @L2 ; Jump on end of string
294 cmp #'%' ; Format spec?
298 inc Format+1 ; Bump high byte of pointer
299 bne @L1 ; Branch always
301 ; Found a '%' character or end of string. Update the Format pointer so it is
302 ; current (points to this character).
304 @L2: tya ; Low byte of offset
310 ; Calculate, how many characters must be output. Beware: This number may
311 ; be zero. A still contains the low byte of the pointer.
318 ora FCount ; Is the result zero?
319 beq @L4 ; Jump if yes
321 ; Output the characters that we have until now. To make the call to out
322 ; faster, build the stack frame by hand (don't use pushax)
343 jsr CallOutFunc ; Call the output function
345 ; We're back from out(), or we didn't call it. Check for end of string.
347 @L4: jsr GetFormatChar ; Get one char, zero in Y
348 tax ; End of format string reached?
349 bne NotDone ; End not reached
351 ; End of format string reached. Restore the zeropage registers and return.
360 ; Still a valid format character. Check for '%' and a '%%' sequence. Output
361 ; anything that is not a format specifier. On intro, Y is zero.
366 lda (Format),y ; Check for "%%"
368 bne FormatSpec ; Jump if really a format specifier
369 jsr IncFormatPtr ; Skip the second '%'
370 @L1: jsr Output1 ; Output the character...
371 jmp MainLoop ; ...and continue
373 ; We have a real format specifier
374 ; Format is: %[flags][width][.precision][mod]type
375 ; Y is zero on entry.
379 ; Initialize the flags
383 @L1: sta FormatVars,x
387 ; Start with reading the flags if there are any. X is $FF which is used
391 lda (Format),y ; Get next char...
411 @L4: jsr IncFormatPtr
412 jmp ReadFlags ; ...and start over
414 ; Done with flags, read the pad char. Y is still zero if we come here.
422 lda (Format),y ; Read current for later
425 ; Read the width. Even here, Y is still zero. A contains the current character
426 ; from the format string
432 jsr GetIntArg ; Width is an additional argument
435 @L1: jsr ReadInt ; Read integer from format string...
437 stx Width+1 ; ...and remember in Width
439 ; Read the precision. Even here, Y is still zero.
441 sty Prec ; Assume Precision is zero
443 lda (Format),y ; Load next format string char
444 cmp #'.' ; Precision given?
445 bne ReadMod ; Branch if no precision given
448 jsr IncFormatPtr ; Skip the '.'
450 cmp #'*' ; Variable precision?
452 jsr IncFormatPtr ; Skip the '*'
453 jsr GetIntArg ; Get integer argument
456 @L1: jsr ReadInt ; Read integer from format string
460 ; Read the modifiers. Y is still zero.
465 beq @L1 ; Read and ignore this one
467 beq @L1 ; Read and ignore this one
469 beq @L1 ; Read and ignore this one
471 beq @L1 ; Read and ignore this one
476 @L1: jsr IncFormatPtr
479 ; Initialize the argument buffer pointers. We use a static buffer (ArgBuf) to
480 ; assemble strings. A zero page index (BufIdx) is used to keep the current
481 ; write position. A pointer to the buffer (Str) is used to point to the the
482 ; argument in case we will not use the buffer but a user supplied string.
483 ; Y is zero when we come here.
486 sty BufIdx ; Clear BufIdx
492 ; Skip the current format character, then check it (current char in A)
503 jsr GetIntArg ; Get the argument (promoted to int)
504 sta Buf ; Place it as zero terminated string...
506 sta Buf+1 ; ...into the buffer
520 lda AddBlank ; Add a blank for positives?
523 @L2: lda AddSign ; Add a plus for positives (precedence)?
530 jsr GetSignedArg ; Get argument as a long
531 ldy sreg+1 ; Check sign
538 @Int1: ldy #10 ; Base
539 jsr ltoa ; Push arguments, call _ltoa
542 ; Is it a count pseudo format?
548 ; It is a count pseudo argument
552 stx ptr1+1 ; Get user supplied pointer
554 lda (OutData),y ; Low byte of OutData->ccount
557 lda (OutData),y ; High byte of OutData->ccount
561 ; Check for an octal digit
567 ; Integer in octal representation
569 jsr GetSignedArg ; Get argument as a long
570 ldy AltForm ; Alternative form?
571 beq @Oct1 ; Jump if no
572 pha ; Save low byte of value
578 ora Prec+1 ; Check if value or Prec != 0
582 pla ; Restore low byte
584 @Oct1: ldy #8 ; Load base
585 jsr ltoa ; Push arguments, call _ltoa
588 ; Check for a pointer specifier (%p)
594 ; It's a pointer. Use %#x conversion
597 stx IsLong ; IsLong = 0;
599 stx AltForm ; AltForm = 1;
601 bne IsHex ; Branch always
603 ; Check for a string specifier (%s)
611 jsr GetIntArg ; Get 16bit argument
616 ; Check for an unsigned integer (%u)
622 ; It's an unsigned integer
624 jsr GetUnsignedArg ; Get argument as unsigned long
626 jsr ultoa ; Push arguments, call _ultoa
629 ; Check for a hexadecimal integer (%x)
637 ; Hexadecimal integer
639 IsHex: pha ; Save the format spec
647 @L1: jsr GetUnsignedArg ; Get argument as an unsigned long
649 jsr ultoa ; Push arguments, call _ultoa
651 pla ; Get the format spec
652 cmp #'x' ; Lower case?
656 jsr _strlower ; Make characters lower case
659 ; Unknown format character, skip it
664 ; We have the argument, do argument string formatting
668 ; ArgLen = strlen (Str);
672 jsr _strlen ; Get length of argument
676 ; if (Prec && Prec < ArgLen) ArgLen = Prec;
690 ; if (Width > ArgLen) {
691 ; Width -= ArgLen; /* padcount */
695 ; Since width is used as a counter below, calculate -(width+1)
712 ; /* Do padding on the left side if needed */
714 ; /* argument right justified */
716 ; fout (d, &padchar, 1);
725 ; Output the argument itself
729 ; /* Output right padding bytes if needed */
731 ; /* argument left justified */
733 ; fout (d, &padchar, 1);
742 ; Done, parse next chars from format string
747 ; ----------------------------------------------------------------------------
748 ; Local data (all static)
752 ; Save area for the zero page registers
755 ; Stuff from OutData. Is used as a vector and must be aligned
758 ; One character argument for OutFunc
772 BufIdx: .byte 0 ; Argument string pointer
773 FormatVarSize = * - FormatVars
775 ; Argument buffer and pointer