2000-11-30 23:04:24 +00:00
|
|
|
;
|
|
|
|
; _printf: Basic layer for all printf type functions.
|
|
|
|
;
|
2012-11-04 15:23:05 +00:00
|
|
|
; Ullrich von Bassewitz, 2000-10-21
|
2000-11-30 23:04:24 +00:00
|
|
|
;
|
2012-11-04 15:23:05 +00:00
|
|
|
|
|
|
|
.include "zeropage.inc"
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.export __printf
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.import popax, pushax, pusheax, decsp6, push1, axlong, axulong
|
|
|
|
.import _ltoa, _ultoa
|
|
|
|
.import _strlower, _strlen
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.macpack generic
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; We will store variables into the register bank in the zeropage. Define
|
|
|
|
; equates for these variables.
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
ArgList = regbank+0 ; Argument list pointer
|
|
|
|
Format = regbank+2 ; Format string
|
|
|
|
OutData = regbank+4 ; Function parameters
|
2000-12-01 09:36:57 +00:00
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Other zero page cells
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
Base = ptr1
|
|
|
|
FSave = ptr1
|
|
|
|
FCount = ptr2
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
.code
|
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Get one character from the format string and increment the pointer. Will
|
|
|
|
; return zero in Y.
|
|
|
|
|
|
|
|
GetFormatChar:
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy #0
|
|
|
|
lda (Format),y
|
2000-11-30 23:04:24 +00:00
|
|
|
IncFormatPtr:
|
2013-05-09 11:56:54 +00:00
|
|
|
inc Format
|
|
|
|
bne @L1
|
|
|
|
inc Format+1
|
|
|
|
@L1: rts
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Output a pad character: outfunc (d, &padchar, 1)
|
|
|
|
|
|
|
|
OutputPadChar:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda PadChar
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Call the output function with one character in A
|
|
|
|
|
|
|
|
Output1:
|
2013-05-09 11:56:54 +00:00
|
|
|
sta CharArg
|
|
|
|
jsr PushOutData
|
|
|
|
lda #<CharArg
|
|
|
|
ldx #>CharArg
|
|
|
|
jsr pushax
|
|
|
|
jsr push1
|
|
|
|
jmp CallOutFunc ; fout (OutData, &CharArg, 1)
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Decrement the argument list pointer by 2
|
|
|
|
|
|
|
|
DecArgList2:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda ArgList
|
|
|
|
sub #2
|
|
|
|
sta ArgList
|
|
|
|
bcs @L1
|
|
|
|
dec ArgList+1
|
|
|
|
@L1: rts
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Get an unsigned int or long argument depending on the IsLong flag.
|
|
|
|
|
|
|
|
GetUnsignedArg:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda IsLong ; Check flag
|
|
|
|
bne GetLongArg ; Long sets all
|
|
|
|
jsr GetIntArg ; Get an integer argument
|
|
|
|
jmp axulong ; Convert to unsigned long
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Get an signed int or long argument depending on the IsLong flag.
|
|
|
|
|
|
|
|
GetSignedArg:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda IsLong ; Check flag
|
|
|
|
bne GetLongArg ; Long sets all
|
|
|
|
jsr GetIntArg ; Get an integer argument
|
|
|
|
jmp axlong ; Convert to long
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Get a long argument from the argument list. Returns 0 in Y.
|
|
|
|
|
|
|
|
GetLongArg:
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr GetIntArg ; Get high word
|
|
|
|
sta sreg
|
|
|
|
stx sreg+1
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Run into GetIntArg fetching the low word
|
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Get an integer argument from the argument list. Returns 0 in Y.
|
|
|
|
|
|
|
|
GetIntArg:
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr DecArgList2
|
|
|
|
ldy #1
|
|
|
|
lda (ArgList),y
|
|
|
|
tax
|
|
|
|
dey
|
|
|
|
lda (ArgList),y
|
|
|
|
rts
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
2000-12-01 09:36:57 +00:00
|
|
|
; Read an integer from the format string. Will return zero in Y.
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2000-12-01 09:36:57 +00:00
|
|
|
ReadInt:
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy #0
|
|
|
|
sty ptr1
|
|
|
|
sty ptr1+1 ; Start with zero
|
|
|
|
@Loop: lda (Format),y ; Get format string character
|
|
|
|
sub #'0' ; Make number from ascii digit
|
|
|
|
bcc @L9 ; Jump if done
|
|
|
|
cmp #9+1
|
|
|
|
bcs @L9 ; Jump if done
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2000-12-01 09:36:57 +00:00
|
|
|
; Skip the digit character
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr IncFormatPtr
|
2000-12-01 09:36:57 +00:00
|
|
|
|
|
|
|
; Add the digit to the value we have in ptr1
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
pha ; Save digit value
|
|
|
|
lda ptr1
|
|
|
|
ldx ptr1+1
|
|
|
|
asl ptr1
|
|
|
|
rol ptr1+1 ; * 2
|
|
|
|
asl ptr1
|
|
|
|
rol ptr1+1 ; * 4, assume carry clear
|
|
|
|
adc ptr1
|
|
|
|
sta ptr1
|
|
|
|
txa
|
|
|
|
adc ptr1+1
|
|
|
|
sta ptr1+1 ; * 5
|
|
|
|
asl ptr1
|
|
|
|
rol ptr1+1 ; * 10, assume carry clear
|
|
|
|
pla
|
|
|
|
adc ptr1 ; Add digit value
|
|
|
|
sta ptr1
|
|
|
|
bcc @Loop
|
|
|
|
inc ptr1+1
|
|
|
|
bcs @Loop ; Branch always
|
2000-12-01 09:36:57 +00:00
|
|
|
|
|
|
|
; We're done converting
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
@L9: lda ptr1
|
|
|
|
ldx ptr1+1 ; Load result
|
|
|
|
rts
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2000-12-01 09:36:57 +00:00
|
|
|
|
2000-11-30 23:04:24 +00:00
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Put a character into the argument buffer and increment the buffer index
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
PutBuf: ldy BufIdx
|
|
|
|
inc BufIdx
|
|
|
|
sta Buf,y
|
|
|
|
rts
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
2000-12-01 09:36:57 +00:00
|
|
|
; Get a pointer to the current buffer end and push it onto the stack
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2000-12-01 09:36:57 +00:00
|
|
|
PushBufPtr:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #<Buf
|
|
|
|
ldx #>Buf
|
|
|
|
add BufIdx
|
|
|
|
bcc @L1
|
|
|
|
inx
|
|
|
|
@L1: jmp pushax
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Push OutData onto the software stack
|
|
|
|
|
|
|
|
PushOutData:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda OutData
|
|
|
|
ldx OutData+1
|
|
|
|
jmp pushax
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Output Width pad characters
|
|
|
|
;
|
|
|
|
|
|
|
|
PadLoop:
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr OutputPadChar
|
2000-11-30 23:04:24 +00:00
|
|
|
OutputPadding:
|
2013-05-09 11:56:54 +00:00
|
|
|
inc Width
|
|
|
|
bne PadLoop
|
|
|
|
inc Width+1
|
|
|
|
bne PadLoop
|
|
|
|
rts
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Output the argument itself: outfunc (d, str, arglen);
|
|
|
|
;
|
|
|
|
|
|
|
|
OutputArg:
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr PushOutData
|
|
|
|
lda Str
|
|
|
|
ldx Str+1
|
|
|
|
jsr pushax
|
|
|
|
lda ArgLen
|
|
|
|
ldx ArgLen+1
|
|
|
|
jsr pushax
|
|
|
|
jmp CallOutFunc
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2000-12-01 09:36:57 +00:00
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; ltoa: Wrapper for _ltoa that pushes all arguments
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
ltoa: sty Base ; Save base
|
|
|
|
jsr pusheax ; Push value
|
|
|
|
jsr PushBufPtr ; Push the buffer pointer...
|
|
|
|
lda Base ; Restore base
|
|
|
|
jmp _ltoa ; ultoa (l, s, base);
|
2000-12-01 09:36:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; ultoa: Wrapper for _ultoa that pushes all arguments
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
ultoa: sty Base ; Save base
|
|
|
|
jsr pusheax ; Push value
|
|
|
|
jsr PushBufPtr ; Push the buffer pointer...
|
|
|
|
lda Base ; Restore base
|
|
|
|
jmp _ultoa ; ultoa (l, s, base);
|
2000-12-01 09:36:57 +00:00
|
|
|
|
|
|
|
|
2000-11-30 23:04:24 +00:00
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
;
|
|
|
|
|
|
|
|
__printf:
|
|
|
|
|
|
|
|
; Save the register bank variables into the save area
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
pha ; Save low byte of ap
|
|
|
|
ldy #5
|
|
|
|
Save: lda regbank,y
|
|
|
|
sta RegSave,y
|
|
|
|
dey
|
|
|
|
bpl Save
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Get the parameters from the stack
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
pla ; Restore low byte of ap
|
|
|
|
sta ArgList ; Argument list pointer
|
|
|
|
stx ArgList+1
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr popax ; Format string
|
|
|
|
sta Format
|
|
|
|
stx Format+1
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr popax ; Output descriptor
|
|
|
|
sta OutData
|
|
|
|
stx OutData+1
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Initialize the output counter in the output descriptor to zero
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #0
|
|
|
|
tay
|
|
|
|
sta (OutData),y
|
|
|
|
iny
|
|
|
|
sta (OutData),y
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Get the output function from the output descriptor and remember it
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
iny
|
|
|
|
lda (OutData),y
|
|
|
|
sta CallOutFunc+1
|
|
|
|
iny
|
|
|
|
lda (OutData),y
|
|
|
|
sta CallOutFunc+2
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Start parsing the format string
|
|
|
|
|
|
|
|
MainLoop:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda Format ; Remember current format pointer
|
|
|
|
sta FSave
|
|
|
|
lda Format+1
|
|
|
|
sta FSave+1
|
|
|
|
|
|
|
|
ldy #0 ; Index
|
|
|
|
@L1: lda (Format),y ; Get next char
|
|
|
|
beq @L2 ; Jump on end of string
|
|
|
|
cmp #'%' ; Format spec?
|
|
|
|
beq @L2
|
|
|
|
iny ; Bump pointer
|
|
|
|
bne @L1
|
|
|
|
inc Format+1 ; Bump high byte of pointer
|
|
|
|
bne @L1 ; Branch always
|
2000-12-08 11:37:01 +00:00
|
|
|
|
|
|
|
; Found a '%' character or end of string. Update the Format pointer so it is
|
|
|
|
; current (points to this character).
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
@L2: tya ; Low byte of offset
|
|
|
|
add Format
|
|
|
|
sta Format
|
|
|
|
bcc @L3
|
|
|
|
inc Format+1
|
2000-12-08 11:37:01 +00:00
|
|
|
|
|
|
|
; Calculate, how many characters must be output. Beware: This number may
|
|
|
|
; be zero. A still contains the low byte of the pointer.
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
@L3: sub FSave
|
|
|
|
sta FCount
|
|
|
|
lda Format+1
|
|
|
|
sbc FSave+1
|
|
|
|
sta FCount+1
|
|
|
|
ora FCount ; Is the result zero?
|
|
|
|
beq @L4 ; Jump if yes
|
2000-12-08 11:37:01 +00:00
|
|
|
|
|
|
|
; Output the characters that we have until now. To make the call to out
|
|
|
|
; faster, build the stack frame by hand (don't use pushax)
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr decsp6 ; 3 args
|
|
|
|
ldy #5
|
|
|
|
lda OutData+1
|
|
|
|
sta (sp),y
|
|
|
|
dey
|
|
|
|
lda OutData
|
|
|
|
sta (sp),y
|
|
|
|
dey
|
|
|
|
lda FSave+1
|
|
|
|
sta (sp),y
|
|
|
|
dey
|
|
|
|
lda FSave
|
|
|
|
sta (sp),y
|
|
|
|
dey
|
|
|
|
lda FCount+1
|
|
|
|
sta (sp),y
|
|
|
|
dey
|
|
|
|
lda FCount
|
|
|
|
sta (sp),y
|
|
|
|
jsr CallOutFunc ; Call the output function
|
2000-12-08 11:37:01 +00:00
|
|
|
|
|
|
|
; We're back from out(), or we didn't call it. Check for end of string.
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
@L4: jsr GetFormatChar ; Get one char, zero in Y
|
|
|
|
tax ; End of format string reached?
|
|
|
|
bne NotDone ; End not reached
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; End of format string reached. Restore the zeropage registers and return.
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #5
|
|
|
|
Rest: lda RegSave,x
|
|
|
|
sta regbank,x
|
|
|
|
dex
|
|
|
|
bpl Rest
|
|
|
|
rts
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Still a valid format character. Check for '%' and a '%%' sequence. Output
|
|
|
|
; anything that is not a format specifier. On intro, Y is zero.
|
|
|
|
|
|
|
|
NotDone:
|
2013-05-09 11:56:54 +00:00
|
|
|
cmp #'%'
|
|
|
|
bne @L1
|
|
|
|
lda (Format),y ; Check for "%%"
|
|
|
|
cmp #'%'
|
|
|
|
bne FormatSpec ; Jump if really a format specifier
|
|
|
|
jsr IncFormatPtr ; Skip the second '%'
|
|
|
|
@L1: jsr Output1 ; Output the character...
|
|
|
|
jmp MainLoop ; ...and continue
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; We have a real format specifier
|
|
|
|
; Format is: %[flags][width][.precision][mod]type
|
|
|
|
; Y is zero on entry.
|
|
|
|
|
|
|
|
FormatSpec:
|
|
|
|
|
|
|
|
; Initialize the flags
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #0
|
|
|
|
ldx #FormatVarSize-1
|
|
|
|
@L1: sta FormatVars,x
|
|
|
|
dex
|
|
|
|
bpl @L1
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2000-12-01 09:36:57 +00:00
|
|
|
; Start with reading the flags if there are any. X is $FF which is used
|
|
|
|
; for "true"
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
ReadFlags:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda (Format),y ; Get next char...
|
|
|
|
cmp #'-'
|
|
|
|
bne @L1
|
|
|
|
stx LeftJust
|
|
|
|
beq @L4
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
@L1: cmp #'+'
|
|
|
|
bne @L2
|
|
|
|
stx AddSign
|
|
|
|
beq @L4
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
@L2: cmp #' '
|
|
|
|
bne @L3
|
|
|
|
stx AddBlank
|
|
|
|
beq @L4
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
@L3: cmp #'#'
|
|
|
|
bne ReadPadding
|
|
|
|
stx AltForm
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
@L4: jsr IncFormatPtr
|
|
|
|
jmp ReadFlags ; ...and start over
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Done with flags, read the pad char. Y is still zero if we come here.
|
|
|
|
|
|
|
|
ReadPadding:
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #' ' ; PadChar
|
|
|
|
cmp #'0'
|
|
|
|
bne @L1
|
|
|
|
tax ; PadChar is '0'
|
|
|
|
jsr IncFormatPtr
|
|
|
|
lda (Format),y ; Read current for later
|
|
|
|
@L1: stx PadChar
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Read the width. Even here, Y is still zero. A contains the current character
|
|
|
|
; from the format string
|
|
|
|
|
|
|
|
ReadWidth:
|
2013-05-09 11:56:54 +00:00
|
|
|
cmp #'*'
|
|
|
|
bne @L1
|
|
|
|
jsr IncFormatPtr
|
|
|
|
jsr GetIntArg ; Width is an additional argument
|
|
|
|
jmp @L2
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
@L1: jsr ReadInt ; Read integer from format string...
|
|
|
|
@L2: sta Width
|
|
|
|
stx Width+1 ; ...and remember in Width
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Read the precision. Even here, Y is still zero.
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
sty Prec ; Assume Precision is zero
|
|
|
|
sty Prec+1
|
|
|
|
lda (Format),y ; Load next format string char
|
|
|
|
cmp #'.' ; Precision given?
|
|
|
|
bne ReadMod ; Branch if no precision given
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
ReadPrec:
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr IncFormatPtr ; Skip the '.'
|
|
|
|
lda (Format),y
|
|
|
|
cmp #'*' ; Variable precision?
|
|
|
|
bne @L1
|
|
|
|
jsr IncFormatPtr ; Skip the '*'
|
|
|
|
jsr GetIntArg ; Get integer argument
|
|
|
|
jmp @L2
|
|
|
|
|
|
|
|
@L1: jsr ReadInt ; Read integer from format string
|
|
|
|
@L2: sta Prec
|
|
|
|
stx Prec+1
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Read the modifiers. Y is still zero.
|
|
|
|
|
|
|
|
ReadMod:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda (Format),y
|
|
|
|
cmp #'z' ; size_t - same as unsigned
|
|
|
|
beq @L2
|
|
|
|
cmp #'h' ; short - same as int
|
|
|
|
beq @L2
|
2004-12-11 18:33:49 +00:00
|
|
|
cmp #'t' ; ptrdiff_t - same as int
|
|
|
|
beq @L2
|
|
|
|
cmp #'j' ; intmax_t/uintmax_t - same as long
|
|
|
|
beq @L1
|
2013-05-09 11:56:54 +00:00
|
|
|
cmp #'L' ; long double
|
|
|
|
beq @L1
|
|
|
|
cmp #'l' ; long int
|
|
|
|
bne DoFormat
|
|
|
|
@L1: lda #$FF
|
|
|
|
sta IsLong
|
|
|
|
@L2: jsr IncFormatPtr
|
|
|
|
jmp ReadMod
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Initialize the argument buffer pointers. We use a static buffer (ArgBuf) to
|
|
|
|
; assemble strings. A zero page index (BufIdx) is used to keep the current
|
|
|
|
; write position. A pointer to the buffer (Str) is used to point to the the
|
|
|
|
; argument in case we will not use the buffer but a user supplied string.
|
|
|
|
; Y is zero when we come here.
|
|
|
|
|
|
|
|
DoFormat:
|
2013-05-09 11:56:54 +00:00
|
|
|
sty BufIdx ; Clear BufIdx
|
|
|
|
ldx #<Buf
|
|
|
|
stx Str
|
|
|
|
ldx #>Buf
|
|
|
|
stx Str+1
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Skip the current format character, then check it (current char in A)
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr IncFormatPtr
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Is it a character?
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
cmp #'c'
|
|
|
|
bne CheckInt
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; It is a character
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr GetIntArg ; Get the argument (promoted to int)
|
|
|
|
sta Buf ; Place it as zero terminated string...
|
|
|
|
lda #0
|
|
|
|
sta Buf+1 ; ...into the buffer
|
|
|
|
jmp HaveArg ; Done
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Is it an integer?
|
|
|
|
|
|
|
|
CheckInt:
|
2013-05-09 11:56:54 +00:00
|
|
|
cmp #'d'
|
|
|
|
beq @L1
|
|
|
|
cmp #'i'
|
|
|
|
bne CheckCount
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; It is an integer
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
@L1: ldx #0
|
|
|
|
lda AddBlank ; Add a blank for positives?
|
|
|
|
beq @L2 ; Jump if no
|
|
|
|
ldx #' '
|
|
|
|
@L2: lda AddSign ; Add a plus for positives (precedence)?
|
|
|
|
beq @L3
|
|
|
|
ldx #'+'
|
|
|
|
@L3: stx Leader
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Integer argument
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr GetSignedArg ; Get argument as a long
|
|
|
|
ldy sreg+1 ; Check sign
|
|
|
|
bmi @Int1
|
|
|
|
ldy Leader
|
|
|
|
beq @Int1
|
|
|
|
sty Buf
|
|
|
|
inc BufIdx
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
@Int1: ldy #10 ; Base
|
|
|
|
jsr ltoa ; Push arguments, call _ltoa
|
|
|
|
jmp HaveArg
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Is it a count pseudo format?
|
|
|
|
|
|
|
|
CheckCount:
|
2013-05-09 11:56:54 +00:00
|
|
|
cmp #'n'
|
|
|
|
bne CheckOctal
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; It is a count pseudo argument
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr GetIntArg
|
|
|
|
sta ptr1
|
|
|
|
stx ptr1+1 ; Get user supplied pointer
|
|
|
|
ldy #0
|
|
|
|
lda (OutData),y ; Low byte of OutData->ccount
|
|
|
|
sta (ptr1),y
|
|
|
|
iny
|
|
|
|
lda (OutData),y ; High byte of OutData->ccount
|
|
|
|
sta (ptr1),y
|
|
|
|
jmp MainLoop ; Done
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Check for an octal digit
|
|
|
|
|
|
|
|
CheckOctal:
|
2013-05-09 11:56:54 +00:00
|
|
|
cmp #'o'
|
|
|
|
bne CheckPointer
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Integer in octal representation
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr GetSignedArg ; Get argument as a long
|
|
|
|
ldy AltForm ; Alternative form?
|
|
|
|
beq @Oct1 ; Jump if no
|
|
|
|
pha ; Save low byte of value
|
|
|
|
stx tmp1
|
|
|
|
ora tmp1
|
|
|
|
ora sreg
|
|
|
|
ora sreg+1
|
|
|
|
ora Prec
|
|
|
|
ora Prec+1 ; Check if value or Prec != 0
|
|
|
|
beq @Oct1
|
|
|
|
lda #'0'
|
|
|
|
jsr PutBuf
|
|
|
|
pla ; Restore low byte
|
|
|
|
|
|
|
|
@Oct1: ldy #8 ; Load base
|
|
|
|
jsr ltoa ; Push arguments, call _ltoa
|
|
|
|
jmp HaveArg
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2002-03-17 16:50:35 +00:00
|
|
|
; Check for a pointer specifier (%p)
|
|
|
|
|
|
|
|
CheckPointer:
|
2013-05-09 11:56:54 +00:00
|
|
|
cmp #'p'
|
|
|
|
bne CheckString
|
2002-03-17 16:50:35 +00:00
|
|
|
|
|
|
|
; It's a pointer. Use %#x conversion
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #0
|
|
|
|
stx IsLong ; IsLong = 0;
|
|
|
|
inx
|
|
|
|
stx AltForm ; AltForm = 1;
|
|
|
|
lda #'x'
|
|
|
|
bne IsHex ; Branch always
|
2002-03-17 16:50:35 +00:00
|
|
|
|
2000-11-30 23:04:24 +00:00
|
|
|
; Check for a string specifier (%s)
|
|
|
|
|
|
|
|
CheckString:
|
2013-05-09 11:56:54 +00:00
|
|
|
cmp #'s'
|
|
|
|
bne CheckUnsigned
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; It's a string
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr GetIntArg ; Get 16bit argument
|
|
|
|
sta Str
|
|
|
|
stx Str+1
|
|
|
|
jmp HaveArg
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Check for an unsigned integer (%u)
|
|
|
|
|
|
|
|
CheckUnsigned:
|
2013-05-09 11:56:54 +00:00
|
|
|
cmp #'u'
|
|
|
|
bne CheckHex
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; It's an unsigned integer
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr GetUnsignedArg ; Get argument as unsigned long
|
|
|
|
ldy #10 ; Load base
|
|
|
|
jsr ultoa ; Push arguments, call _ultoa
|
|
|
|
jmp HaveArg
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Check for a hexadecimal integer (%x)
|
|
|
|
|
|
|
|
CheckHex:
|
2013-05-09 11:56:54 +00:00
|
|
|
cmp #'x'
|
|
|
|
beq IsHex
|
|
|
|
cmp #'X'
|
|
|
|
bne UnknownFormat
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Hexadecimal integer
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
IsHex: pha ; Save the format spec
|
|
|
|
lda AltForm
|
|
|
|
beq @L1
|
|
|
|
lda #'0'
|
|
|
|
jsr PutBuf
|
|
|
|
lda #'X'
|
|
|
|
jsr PutBuf
|
|
|
|
|
|
|
|
@L1: jsr GetUnsignedArg ; Get argument as an unsigned long
|
|
|
|
ldy #16 ; Load base
|
|
|
|
jsr ultoa ; Push arguments, call _ultoa
|
|
|
|
|
|
|
|
pla ; Get the format spec
|
|
|
|
cmp #'x' ; Lower case?
|
|
|
|
bne @L2
|
|
|
|
lda Str
|
|
|
|
ldx Str+1
|
|
|
|
jsr _strlower ; Make characters lower case
|
|
|
|
@L2: jmp HaveArg
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2002-03-17 16:50:35 +00:00
|
|
|
; Unknown format character, skip it
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
UnknownFormat:
|
2013-05-09 11:56:54 +00:00
|
|
|
jmp MainLoop
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; We have the argument, do argument string formatting
|
|
|
|
|
|
|
|
HaveArg:
|
|
|
|
|
|
|
|
; ArgLen = strlen (Str);
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
lda Str
|
|
|
|
ldx Str+1
|
|
|
|
jsr _strlen ; Get length of argument
|
|
|
|
sta ArgLen
|
|
|
|
stx ArgLen+1
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; if (Prec && Prec < ArgLen) ArgLen = Prec;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
lda Prec
|
|
|
|
ora Prec+1
|
|
|
|
beq @L1
|
|
|
|
ldx Prec
|
|
|
|
cpx ArgLen
|
|
|
|
lda Prec+1
|
|
|
|
tay
|
|
|
|
sbc ArgLen+1
|
|
|
|
bcs @L1
|
|
|
|
stx ArgLen
|
|
|
|
sty ArgLen+1
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; if (Width > ArgLen) {
|
2013-05-09 11:56:54 +00:00
|
|
|
; Width -= ArgLen; /* padcount */
|
2000-11-30 23:04:24 +00:00
|
|
|
; } else {
|
|
|
|
; Width = 0;
|
|
|
|
; }
|
|
|
|
; Since width is used as a counter below, calculate -(width+1)
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
@L1: sec
|
|
|
|
lda Width
|
|
|
|
sbc ArgLen
|
|
|
|
tax
|
|
|
|
lda Width+1
|
|
|
|
sbc ArgLen+1
|
|
|
|
bcs @L2
|
|
|
|
lda #0
|
|
|
|
tax
|
|
|
|
@L2: eor #$FF
|
|
|
|
sta Width+1
|
|
|
|
txa
|
|
|
|
eor #$FF
|
|
|
|
sta Width
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; /* Do padding on the left side if needed */
|
|
|
|
; if (!leftjust) {
|
|
|
|
; /* argument right justified */
|
|
|
|
; while (width) {
|
2013-05-09 11:56:54 +00:00
|
|
|
; fout (d, &padchar, 1);
|
|
|
|
; --width;
|
2000-11-30 23:04:24 +00:00
|
|
|
; }
|
|
|
|
; }
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
lda LeftJust
|
|
|
|
bne @L3
|
|
|
|
jsr OutputPadding
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Output the argument itself
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
@L3: jsr OutputArg
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; /* Output right padding bytes if needed */
|
|
|
|
; if (leftjust) {
|
|
|
|
; /* argument left justified */
|
|
|
|
; while (width) {
|
2013-05-09 11:56:54 +00:00
|
|
|
; fout (d, &padchar, 1);
|
|
|
|
; --width;
|
2000-11-30 23:04:24 +00:00
|
|
|
; }
|
|
|
|
; }
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
lda LeftJust
|
|
|
|
beq @L4
|
|
|
|
jsr OutputPadding
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Done, parse next chars from format string
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
@L4: jmp MainLoop
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Local data (all static)
|
|
|
|
|
|
|
|
.bss
|
|
|
|
|
|
|
|
; Save area for the zero page registers
|
2013-05-09 11:56:54 +00:00
|
|
|
RegSave: .res regbanksize
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; One character argument for OutFunc
|
2013-05-09 11:56:54 +00:00
|
|
|
CharArg: .byte 0
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Format variables
|
|
|
|
FormatVars:
|
2013-05-09 11:56:54 +00:00
|
|
|
LeftJust: .byte 0
|
|
|
|
AddSign: .byte 0
|
|
|
|
AddBlank: .byte 0
|
|
|
|
AltForm: .byte 0
|
|
|
|
PadChar: .byte 0
|
|
|
|
Width: .word 0
|
|
|
|
Prec: .word 0
|
|
|
|
IsLong: .byte 0
|
|
|
|
Leader: .byte 0
|
|
|
|
BufIdx: .byte 0 ; Argument string pointer
|
|
|
|
FormatVarSize = * - FormatVars
|
2000-11-30 23:04:24 +00:00
|
|
|
|
|
|
|
; Argument buffer and pointer
|
2013-05-09 11:56:54 +00:00
|
|
|
Buf: .res 20
|
|
|
|
Str: .word 0
|
|
|
|
ArgLen: .res 2
|
2000-11-30 23:04:24 +00:00
|
|
|
|
2002-07-16 09:40:45 +00:00
|
|
|
.data
|
|
|
|
|
|
|
|
; Stuff from OutData. Is used as a vector and must be aligned
|
2013-05-09 11:56:54 +00:00
|
|
|
CallOutFunc: jmp $0000
|
2002-07-16 09:40:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|