]> git.sur5r.net Git - cc65/blob - libsrc/conio/vcprintf.s
Fix comments and TELEMON uppercase
[cc65] / libsrc / conio / vcprintf.s
1 ;
2 ; int __fastcall__ vcprintf (const char* Format, va_list ap);
3 ;
4 ; Ullrich von Bassewitz, 2.12.2000
5 ;
6
7         .export         _vcprintf
8         .import         pushax, popax, popptr1
9         .import         __printf, _cputc
10         .importzp       sp, ptr1, ptr2, ptr3, tmp1
11
12         .macpack        generic
13
14
15 .data
16
17 ; ----------------------------------------------------------------------------
18 ;
19 ; Static data for the _vsprintf routine
20 ;
21
22 outdesc:                        ; Static outdesc structure
23         .word   0               ; ccount
24         .word   out             ; Output function pointer
25         .word   0               ; ptr
26         .word   0               ; uns
27
28 .code
29
30 ; ----------------------------------------------------------------------------
31 ; Callback routine used for the actual output.
32 ;
33 ; static void __cdecl__ out (struct outdesc* d, const char* buf, unsigned count)
34 ; /* Routine used for writing */
35 ; {
36 ;     /* Fast screen output */
37 ;     d->ccount += count;
38 ;     while (count) {
39 ;         cputc (*buf);
40 ;         ++buf;
41 ;         --count;
42 ;     }
43 ; }
44 ;
45 ; We're using ptr1 and tmp1, since we know that the cputc routine will not use
46 ; them (they're also used in cputs, so they must be safe).
47
48 out:    jsr     popax           ; count
49         sta     ptr2
50         eor     #$FF
51         sta     outdesc+6
52         txa
53         sta     ptr2+1
54         eor     #$FF
55         sta     outdesc+7
56
57         jsr     popptr1         ; buf
58
59         jsr     popax           ; d
60         sta     ptr3
61         stx     ptr3+1
62
63 ; Sum up the total count of characters
64
65         ldy     #0              ; ccount in struct outdesc
66         sty     tmp1            ; Initialize tmp1 while we have zero available
67         lda     (ptr3),y
68         add     ptr2
69         sta     (ptr3),y
70         iny
71         lda     (ptr3),y
72         adc     ptr2+1
73         sta     (ptr3),y
74
75 ; Loop outputting characters
76
77 @L1:    inc     outdesc+6
78         beq     @L4
79 @L2:    ldy     tmp1
80         lda     (ptr1),y
81         iny
82         bne     @L3
83         inc     ptr1+1
84 @L3:    sty     tmp1
85         jsr     _cputc
86         jmp     @L1
87
88 @L4:    inc     outdesc+7
89         bne     @L2
90         rts
91
92 ; ----------------------------------------------------------------------------
93 ; vcprintf - formatted console i/o
94 ;
95 ; int __fastcall__ vcprintf (const char* format, va_list ap)
96 ; {
97 ;     struct outdesc d;
98 ;
99 ;     /* Setup descriptor */
100 ;     d.fout = out;
101 ;
102 ;     /* Do formatting and output */
103 ;     _printf (&d, format, ap);
104 ;
105 ;     /* Return bytes written */
106 ;     return d.ccount;
107 ; }
108
109 _vcprintf:
110         sta     ptr1            ; Save ap
111         stx     ptr1+1
112
113 ; Setup the outdesc structure
114
115         lda     #0
116         sta     outdesc
117         sta     outdesc+1       ; Clear ccount
118
119 ; Get the format parameter and push it again
120
121         ldy     #1
122         lda     (sp),y
123         tax
124         dey
125         lda     (sp),y
126         jsr     pushax
127
128 ; Replace the passed format parameter on the stack by &d - this creates
129 ; exactly the stack frame _printf expects. Parameters will get dropped
130 ; by _printf.
131
132         ldy     #2              ; Low byte of d
133         lda     #<outdesc
134         sta     (sp),y
135         iny
136         lda     #>outdesc
137         sta     (sp),y
138
139 ; Restore ap and call _printf
140
141         lda     ptr1
142         ldx     ptr1+1
143         jsr     __printf
144
145 ; Return the number of bytes written.
146
147         lda     outdesc         ; ccount
148         ldx     outdesc+1
149         rts