]> git.sur5r.net Git - cc65/blob - libsrc/conio/vcprintf.s
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / libsrc / conio / vcprintf.s
1 ;
2 ; int vcprintf (const char* Format, va_list ap);
3 ;
4 ; Ullrich von Bassewitz, 2.12.2000
5 ;
6
7         .export         _vcprintf
8         .import         pushax, popax
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 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     popax           ; buf
58         sta     ptr1
59         stx     ptr1+1
60
61         jsr     popax           ; d
62         sta     ptr3
63         stx     ptr3+1
64
65 ; Sum up the total count of characters
66
67         ldy     #0              ; ccount in struct outdesc
68         sty     tmp1            ; Initialize tmp1 while we have zero available
69         lda     (ptr3),y
70         add     ptr2
71         sta     (ptr3),y
72         iny
73         lda     (ptr3),y
74         adc     ptr2+1
75         sta     (ptr3),y
76
77 ; Loop outputting characters
78
79 @L1:    inc     outdesc+6
80         beq     @L4
81 @L2:    ldy     tmp1
82         lda     (ptr1),y
83         iny
84         bne     @L3
85         inc     ptr1+1
86 @L3:    sty     tmp1
87         jsr     _cputc
88         jmp     @L1
89
90 @L4:    inc     outdesc+7
91         bne     @L2
92         rts
93
94 ; ----------------------------------------------------------------------------
95 ; vcprintf - formatted console i/o
96 ;
97 ; int vcprintf (const char* format, va_list ap)
98 ; {
99 ;     struct outdesc d;
100 ;
101 ;     /* Setup descriptor */
102 ;     d.fout = out;
103 ;
104 ;     /* Do formatting and output */
105 ;     _printf (&d, format, ap);
106 ;
107 ;     /* Return bytes written */
108 ;     return d.ccount;
109 ; }
110 ;
111 ; It is intentional that this function does not have __fastcall__ calling
112 ; conventions - we need the space on the stack anyway, so there's nothing
113 ; gained by using __fastcall__.
114
115 _vcprintf:
116         sta     ptr1            ; Save ap
117         stx     ptr1+1
118
119 ; Setup the outdesc structure
120
121         lda     #0
122         sta     outdesc
123         sta     outdesc+1       ; Clear ccount
124
125 ; Get the format parameter and push it again
126
127         ldy     #1
128         lda     (sp),y
129         tax
130         dey
131         lda     (sp),y
132         jsr     pushax
133
134 ; Replace the passed format parameter on the stack by &d - this creates
135 ; exactly the stack frame _printf expects. Parameters will get dropped
136 ; by _printf.
137
138         ldy     #2              ; Low byte of d
139         lda     #<outdesc
140         sta     (sp),y
141         iny
142         lda     #>outdesc
143         sta     (sp),y
144
145 ; Restore ap and call _printf
146
147         lda     ptr1
148         ldx     ptr1+1
149         jsr     __printf
150
151 ; Return the number of bytes written.
152
153         lda     outdesc         ; ccount
154         ldx     outdesc+1
155         rts
156
157                       
158