2005-02-14 09:59:54 +00:00
|
|
|
; vfprintf.s
|
2000-12-01 15:05:46 +00:00
|
|
|
;
|
2005-02-14 09:59:54 +00:00
|
|
|
; int fastcall vfprintf(FILE* f, const char* Format, va_list ap);
|
2000-12-01 15:05:46 +00:00
|
|
|
;
|
2005-02-14 09:59:54 +00:00
|
|
|
; 2005-02-08, Ullrich von Bassewitz
|
|
|
|
; 2005-02-11, Greg King
|
2000-12-01 15:05:46 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.export _vfprintf
|
|
|
|
.import push1, pushwysp, incsp6
|
|
|
|
.import _fwrite, __printf
|
|
|
|
.importzp sp, ptr1
|
2000-12-01 15:05:46 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.macpack generic
|
2000-12-01 15:05:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
.data
|
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Static data for the _vfprintf routine
|
|
|
|
;
|
2005-02-14 09:59:54 +00:00
|
|
|
outdesc: ; Static outdesc structure
|
2013-05-09 11:56:54 +00:00
|
|
|
ccount: .res 2
|
|
|
|
.word out ; Output function pointer
|
|
|
|
ptr: .res 2 ; Points to output file
|
|
|
|
.res 2 ; (Not used by this function)
|
2000-12-01 15:05:46 +00:00
|
|
|
|
|
|
|
.code
|
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Callback routine used for the actual output.
|
|
|
|
;
|
2005-02-14 10:28:54 +00:00
|
|
|
; Since we know, that this routine is always called with "our" outdesc, we
|
|
|
|
; can ignore the passed pointer d, and access the data directly. While this
|
|
|
|
; is not very clean, it gives better and shorter code.
|
|
|
|
;
|
2015-06-22 04:15:48 +00:00
|
|
|
; static void cdecl out (struct outdesc* d, const char* buf, unsigned count)
|
2000-12-01 15:05:46 +00:00
|
|
|
; /* Routine used for writing */
|
|
|
|
; {
|
2005-02-14 09:59:54 +00:00
|
|
|
; register size_t cnt;
|
2005-02-14 10:28:54 +00:00
|
|
|
;
|
2000-12-01 15:05:46 +00:00
|
|
|
; /* Write to the file */
|
2005-02-14 10:28:54 +00:00
|
|
|
; if ((cnt = fwrite(buf, 1, count, ptr)) == 0) {
|
|
|
|
; ccount = -1;
|
2000-12-01 15:05:46 +00:00
|
|
|
; } else {
|
2005-02-14 10:28:54 +00:00
|
|
|
; ccount += cnt;
|
2000-12-01 15:05:46 +00:00
|
|
|
; }
|
|
|
|
; }
|
|
|
|
|
|
|
|
; About to call
|
|
|
|
;
|
2005-02-14 10:28:54 +00:00
|
|
|
; fwrite (buf, 1, count, ptr);
|
2000-12-01 15:05:46 +00:00
|
|
|
;
|
2005-02-14 09:59:54 +00:00
|
|
|
out: ldy #5
|
2005-02-08 20:53:48 +00:00
|
|
|
jsr pushwysp ; Push buf
|
|
|
|
jsr push1 ; Push #1
|
|
|
|
ldy #7
|
|
|
|
jsr pushwysp ; Push count
|
2005-02-14 10:28:54 +00:00
|
|
|
lda ptr
|
2015-06-22 04:15:48 +00:00
|
|
|
ldx ptr+1
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr _fwrite
|
|
|
|
sta ptr1 ; Save function result
|
|
|
|
stx ptr1+1
|
2000-12-01 15:05:46 +00:00
|
|
|
|
2005-02-14 09:59:54 +00:00
|
|
|
; Check the return value.
|
2000-12-01 15:05:46 +00:00
|
|
|
|
2005-02-14 10:28:54 +00:00
|
|
|
ora ptr1+1
|
2013-05-09 11:56:54 +00:00
|
|
|
bne @Ok
|
2000-12-01 15:05:46 +00:00
|
|
|
|
2005-02-14 10:28:54 +00:00
|
|
|
; We had an error. Store -1 into ccount
|
2000-12-01 15:05:46 +00:00
|
|
|
|
2005-02-14 09:59:54 +00:00
|
|
|
.ifp02
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #<-1
|
2005-02-14 09:59:54 +00:00
|
|
|
.else
|
2013-05-09 11:56:54 +00:00
|
|
|
dec a
|
2005-02-14 09:59:54 +00:00
|
|
|
.endif
|
2005-02-14 10:28:54 +00:00
|
|
|
sta ccount
|
2005-02-08 20:53:48 +00:00
|
|
|
bne @Done ; Branch always
|
2000-12-01 15:05:46 +00:00
|
|
|
|
|
|
|
; Result was ok, count bytes written
|
|
|
|
|
2005-02-14 10:28:54 +00:00
|
|
|
@Ok: lda ptr1
|
|
|
|
add ccount
|
|
|
|
sta ccount
|
|
|
|
txa
|
|
|
|
adc ccount+1
|
|
|
|
@Done: sta ccount+1
|
2005-02-14 09:59:54 +00:00
|
|
|
jmp incsp6 ; Drop stackframe
|
2000-12-01 15:05:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
2000-12-01 21:43:07 +00:00
|
|
|
; vfprintf - formatted output
|
2000-12-01 15:05:46 +00:00
|
|
|
;
|
2005-02-14 09:59:54 +00:00
|
|
|
; int fastcall vfprintf(FILE* f, const char* format, va_list ap)
|
2000-12-01 15:05:46 +00:00
|
|
|
; {
|
2005-02-14 09:59:54 +00:00
|
|
|
; static struct outdesc d = {
|
|
|
|
; 0,
|
|
|
|
; out
|
|
|
|
; };
|
2000-12-01 15:05:46 +00:00
|
|
|
;
|
|
|
|
; /* Setup descriptor */
|
2005-02-14 09:59:54 +00:00
|
|
|
; d.ccount = 0;
|
2000-12-01 15:05:46 +00:00
|
|
|
; d.ptr = f;
|
|
|
|
;
|
|
|
|
; /* Do formatting and output */
|
|
|
|
; _printf (&d, format, ap);
|
|
|
|
;
|
|
|
|
; /* Return bytes written */
|
|
|
|
; return d.ccount;
|
|
|
|
; }
|
2005-02-14 09:59:54 +00:00
|
|
|
;
|
2000-12-01 15:05:46 +00:00
|
|
|
_vfprintf:
|
2013-05-09 11:56:54 +00:00
|
|
|
pha ; Save low byte of ap
|
2000-12-01 15:05:46 +00:00
|
|
|
|
|
|
|
; Setup the outdesc structure
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #0
|
|
|
|
sta ccount
|
|
|
|
sta ccount+1 ; Clear character-count
|
2000-12-01 15:05:46 +00:00
|
|
|
|
|
|
|
; Reorder the stack. Replace f on the stack by &d, so the stack frame is
|
|
|
|
; exactly as _printf expects it. Parameters will get dropped by _printf.
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy #2
|
|
|
|
lda (sp),y ; Low byte of f
|
|
|
|
sta ptr
|
|
|
|
lda #<outdesc
|
|
|
|
sta (sp),y
|
2005-02-14 09:59:54 +00:00
|
|
|
iny
|
2013-05-09 11:56:54 +00:00
|
|
|
lda (sp),y ; High byte of f
|
|
|
|
sta ptr+1
|
|
|
|
lda #>outdesc
|
|
|
|
sta (sp),y
|
2000-12-01 15:05:46 +00:00
|
|
|
|
|
|
|
; Restore low byte of ap and call _printf
|
|
|
|
|
2005-02-14 09:59:54 +00:00
|
|
|
pla
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr __printf
|
2000-12-01 15:05:46 +00:00
|
|
|
|
|
|
|
; Return the number of bytes written
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
lda ccount
|
|
|
|
ldx ccount+1
|
2005-02-14 09:59:54 +00:00
|
|
|
rts
|
2000-12-01 15:05:46 +00:00
|
|
|
|
|
|
|
|