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
244 pha ; Save low byte of ap
251 ; Get the parameters from the stack
253 pla ; Restore low byte of ap
254 sta ArgList ; Argument list pointer
257 jsr popax ; Format string
261 jsr popax ; Output descriptor
265 ; Initialize the output counter in the output descriptor to zero
273 ; Get the output function from the output descriptor and remember it
282 ; Start parsing the format string
285 jsr GetFormatChar ; Get one char, zero in Y
286 tax ; End of format string reached?
287 bne NotDone ; Continue of end not reached
289 ; End of format string reached. Restore the zeropage registers and return.
298 ; Still a valid format character. Check for '%' and a '%%' sequence. Output
299 ; anything that is not a format specifier. On intro, Y is zero.
304 lda (Format),y ; Check for "%%"
306 bne FormatSpec ; Jump if really a format specifier
307 jsr IncFormatPtr ; Skip the second '%'
308 @L1: jsr Output1 ; Output the character...
309 jmp MainLoop ; ...and continue
311 ; We have a real format specifier
312 ; Format is: %[flags][width][.precision][mod]type
313 ; Y is zero on entry.
317 ; Initialize the flags
321 @L1: sta FormatVars,x
325 ; Start with reading the flags if there are any. X is $FF which is used
329 lda (Format),y ; Get next char...
349 @L4: jsr IncFormatPtr
350 jmp ReadFlags ; ...and start over
352 ; Done with flags, read the pad char. Y is still zero if we come here.
360 lda (Format),y ; Read current for later
363 ; Read the width. Even here, Y is still zero. A contains the current character
364 ; from the format string
370 jsr GetIntArg ; Width is an additional argument
373 @L1: jsr ReadInt ; Read integer from format string...
375 stx Width+1 ; ...and remember in Width
377 ; Read the precision. Even here, Y is still zero.
379 sty Prec ; Assume Precision is zero
381 lda (Format),y ; Load next format string char
382 cmp #'.' ; Precision given?
383 bne ReadMod ; Branch if no precision given
386 jsr IncFormatPtr ; Skip the '.'
388 cmp #'*' ; Variable precision?
390 jsr IncFormatPtr ; Skip the '*'
391 jsr GetIntArg ; Get integer argument
394 @L1: jsr ReadInt ; Read integer from format string
398 ; Read the modifiers. Y is still zero.
403 beq @L1 ; Read and ignore this one
405 beq @L1 ; Read and ignore this one
407 beq @L1 ; Read and ignore this one
409 beq @L1 ; Read and ignore this one
414 @L1: jsr IncFormatPtr
417 ; Initialize the argument buffer pointers. We use a static buffer (ArgBuf) to
418 ; assemble strings. A zero page index (BufIdx) is used to keep the current
419 ; write position. A pointer to the buffer (Str) is used to point to the the
420 ; argument in case we will not use the buffer but a user supplied string.
421 ; Y is zero when we come here.
424 sty BufIdx ; Clear BufIdx
430 ; Skip the current format character, then check it (current char in A)
441 jsr GetIntArg ; Get the argument (promoted to int)
442 sta Buf ; Place it as zero terminated string...
444 sta Buf+1 ; ...into the buffer
458 lda AddBlank ; Add a blank for positives?
461 @L2: lda AddSign ; Add a plus for positives (precedence)?
468 jsr GetSignedArg ; Get argument as a long
469 ldy sreg+1 ; Check sign
476 @Int1: ldy #10 ; Base
477 jsr ltoa ; Push arguments, call _ltoa
480 ; Is it a count pseudo format?
486 ; It is a count pseudo argument
490 stx ptr1+1 ; Get user supplied pointer
492 lda (OutData),y ; Low byte of OutData->ccount
495 lda (OutData),y ; High byte of OutData->ccount
499 ; Check for an octal digit
505 ; Integer in octal representation
507 jsr GetSignedArg ; Get argument as a long
508 ldy AltForm ; Alternative form?
509 beq @Oct1 ; Jump if no
510 pha ; Save low byte of value
516 ora Prec+1 ; Check if value or Prec != 0
520 pla ; Restore low byte
522 @Oct1: ldy #8 ; Load base
523 jsr ltoa ; Push arguments, call _ltoa
526 ; Check for a string specifier (%s)
534 jsr GetIntArg ; Get 16bit argument
539 ; Check for an unsigned integer (%u)
545 ; It's an unsigned integer
547 jsr GetUnsignedArg ; Get argument as unsigned long
549 jsr ultoa ; Push arguments, call _ultoa
552 ; Check for a hexadecimal integer (%x)
560 ; Hexadecimal integer
562 @IsHex: pha ; Save the format spec
570 @L1: jsr GetUnsignedArg ; Get argument as an unsigned long
572 jsr ultoa ; Push arguments, call _ultoa
574 pla ; Get the format spec
575 cmp #'x' ; Lower case?
579 jsr _strlower ; Make characters lower case
582 ; Unsigned format character, skip it
587 ; We have the argument, do argument string formatting
591 ; ArgLen = strlen (Str);
595 jsr _strlen ; Get length of argument
599 ; if (Prec && Prec < ArgLen) ArgLen = Prec;
613 ; if (Width > ArgLen) {
614 ; Width -= ArgLen; /* padcount */
618 ; Since width is used as a counter below, calculate -(width+1)
635 ; /* Do padding on the left side if needed */
637 ; /* argument right justified */
639 ; fout (d, &padchar, 1);
648 ; Output the argument itself
652 ; /* Output right padding bytes if needed */
654 ; /* argument left justified */
656 ; fout (d, &padchar, 1);
665 ; Done, parse next chars from format string
670 ; ----------------------------------------------------------------------------
671 ; Local data (all static)
675 ; Save area for the zero page registers
678 ; Stuff from OutData. Is used as a vector and must be aligned
681 ; One character argument for OutFunc
695 BufIdx: .byte 0 ; Argument string pointer
696 FormatVarSize = * - FormatVars
698 ; Argument buffer and pointer