]> git.sur5r.net Git - cc65/blob - libsrc/common/vfprintf.s
New loadable mouse drivers
[cc65] / libsrc / common / vfprintf.s
1 ;
2 ; int vfprintf (FILE* f, const char* Format, va_list ap);
3 ;
4 ; Ullrich von Bassewitz, 1.12.2000
5 ;
6
7         .export         _vfprintf
8         .import         pushax, popax, push1
9         .import         _fwrite, __printf
10         .importzp       sp, ptr1, ptr2
11
12         .macpack        generic
13
14
15 .data
16
17 ; ----------------------------------------------------------------------------
18 ;
19 ; Static data for the _vfprintf 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 ;     /* Write to the file */
37 ;     if (fwrite (buf, count, 1, (FILE*) d->ptr) == -1) {
38 ;         d->ccount = -1;
39 ;     } else {
40 ;         d->ccount += count;
41 ;     }
42 ; }
43
44 out:
45
46 ; About to call
47 ;
48 ;       fwrite (buf, count, 1, (FILE*) d->ptr);
49 ;
50 ; Since Buf and Count are already in place, we will just push the last
51 ; two parameters. The fwrite function will remove Buf and Count on exit.
52
53         jsr     push1
54         ldy     #7              ; Offset of D+1 on stack
55         lda     (sp),y
56         sta     ptr1+1
57         dey                     ; Offset of D on stack (6)
58         lda     (sp),y
59         sta     ptr1
60         dey                     ; Offset of ptr+1 in struct outdesc (5)
61         lda     (ptr1),y
62         tax
63         dey
64         lda     (ptr1),y        ; Load D->ptr
65         jsr     _fwrite
66         sta     ptr2            ; Save function result
67         stx     ptr2+1
68
69 ; Pop the last parameter from stack and store it in ptr1. This means that
70 ; the stack is clean now.
71
72         jsr     popax
73         sta     ptr1
74         stx     ptr1+1
75
76 ; Load the offset of ccount in struct outdesc
77
78         ldy     #$00
79
80 ; Check the return code. Checking the hig byte against $FF is ok here.
81
82         lda     ptr2+1
83         cmp     #$FF
84         bne     @Ok
85
86 ; We had an error. Store -1 into d->ccount
87
88         sta     (ptr1),y
89         iny
90         sta     (ptr1),y
91         rts
92
93 ; Result was ok, count bytes written
94
95 @Ok:    lda     (ptr1),y
96         add     ptr1
97         sta     (ptr1),y
98         iny
99         lda     (ptr1),y
100         adc     ptr1+1
101         sta     (ptr1),y
102         rts
103
104
105 ; ----------------------------------------------------------------------------
106 ; vfprintf - formatted output
107 ;
108 ; int vfprintf (FILE* f, const char* format, va_list ap)
109 ; {
110 ;     struct outdesc d;
111 ;
112 ;     /* Setup descriptor */
113 ;     d.fout = out;
114 ;     d.ptr  = f;
115 ;
116 ;     /* Do formatting and output */
117 ;     _printf (&d, format, ap);
118 ;
119 ;     /* Return bytes written */
120 ;     return d.ccount;
121 ; }
122
123 _vfprintf:
124         pha                     ; Save low byte of ap
125
126 ; Setup the outdesc structure
127
128         lda     #0
129         sta     outdesc
130         sta     outdesc+1       ; Clear ccount
131
132 ; Reorder the stack. Replace f on the stack by &d, so the stack frame is
133 ; exactly as _printf expects it. Parameters will get dropped by _printf.
134
135         ldy     #2              ;
136         lda     (sp),y          ; Low byte of f
137         sta     outdesc+4       ;
138         lda     #<outdesc
139         sta     (sp),y
140         iny
141         lda     (sp),y          ; High byte of f
142         sta     outdesc+5
143         lda     #>outdesc
144         sta     (sp),y
145
146 ; Restore low byte of ap and call _printf
147
148         pla
149         jsr     __printf
150
151 ; Return the number of bytes written
152
153         lda     outdesc
154         ldx     outdesc+1
155         rts
156
157