2005-02-14 09:21:54 +00:00
|
|
|
;
|
|
|
|
; int fastcall vcscanf(const char* format, va_list ap);
|
|
|
|
;
|
|
|
|
; 2005-01-02, Greg King
|
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.export _vcscanf
|
2005-02-14 09:21:54 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.import _cgetc, _cputc
|
|
|
|
.import popax, pushax, swapstk
|
2005-02-14 09:21:54 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.include "../common/_scanf.inc"
|
2005-02-14 09:21:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
; static bool pushed;
|
|
|
|
; static char back;
|
|
|
|
;
|
|
|
|
.bss
|
2013-05-09 11:56:54 +00:00
|
|
|
pushed: .res 1
|
|
|
|
back: .res 1
|
2005-02-14 09:21:54 +00:00
|
|
|
|
|
|
|
.code
|
|
|
|
; /* Call-back functions:
|
|
|
|
; ** (Note: These prototypes must NOT be declared with fastcall! They don't
|
|
|
|
; ** use (getfunc)'s and (ungetfunc)'s last parameter. Leaving it out of these
|
|
|
|
; ** prototypes makes more efficient code.)
|
|
|
|
; */
|
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; /* Read a character from the console, and return it to an internal function */
|
|
|
|
; static int get(void) {
|
|
|
|
; static char C;
|
|
|
|
;
|
|
|
|
; if (pushed) {
|
|
|
|
; pushed = false;
|
|
|
|
; return (int)back;
|
|
|
|
; }
|
|
|
|
; cputc(C = cgetc()); /* echo a typed character */
|
|
|
|
; return (int)C;
|
|
|
|
; }
|
|
|
|
;
|
2013-05-09 11:56:54 +00:00
|
|
|
get: ldx pushed
|
|
|
|
beq L1
|
2005-02-14 09:21:54 +00:00
|
|
|
|
|
|
|
; Return the old, pushed-back character (instead of getting a new one).
|
|
|
|
;
|
2013-05-09 11:56:54 +00:00
|
|
|
dex ; ldx #>0
|
|
|
|
stx pushed
|
|
|
|
lda back
|
2005-02-14 09:21:54 +00:00
|
|
|
rts
|
|
|
|
|
|
|
|
; Directly read the keyboard.
|
|
|
|
;
|
2013-05-09 11:56:54 +00:00
|
|
|
L1: jsr _cgetc
|
2005-02-14 09:21:54 +00:00
|
|
|
|
|
|
|
; Echo the character to the screen.
|
|
|
|
;
|
|
|
|
pha
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr _cputc
|
2005-02-14 09:21:54 +00:00
|
|
|
pla
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #>0
|
2005-02-14 09:21:54 +00:00
|
|
|
rts
|
|
|
|
|
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; static int unget(int c) {
|
|
|
|
; pushed = true;
|
|
|
|
; return back = c;
|
|
|
|
; }
|
|
|
|
;
|
2013-05-09 11:56:54 +00:00
|
|
|
unget: ldx #1
|
|
|
|
stx pushed
|
|
|
|
jsr popax ; get the first argument
|
|
|
|
sta back
|
2005-02-14 09:21:54 +00:00
|
|
|
rts
|
|
|
|
|
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; int fastcall vcscanf(const char* format, va_list ap) {
|
|
|
|
; /* Initiate the data structure.
|
|
|
|
; ** Don't initiate the member that these conio functions don't use.
|
|
|
|
; */
|
|
|
|
; static const struct scanfdata d = {
|
|
|
|
; ( getfunc) get,
|
|
|
|
; (ungetfunc)unget
|
|
|
|
; };
|
|
|
|
;
|
|
|
|
; /* conio is very interactive. So, don't use any pushed-back character.
|
|
|
|
; ** Start fresh, each time that this function is called.
|
|
|
|
; */
|
|
|
|
; pushed = false;
|
|
|
|
;
|
|
|
|
; /* Call the internal function, and return the result. */
|
|
|
|
; return _scanf(&d, format, ap);
|
|
|
|
; }
|
|
|
|
;
|
|
|
|
; Beware: Because ap is a fastcall parameter, we must not destroy .XA.
|
|
|
|
;
|
2013-05-09 11:56:54 +00:00
|
|
|
.proc _vcscanf
|
2005-02-14 09:21:54 +00:00
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Static, constant scanfdata structure for the _vcscanf routine.
|
|
|
|
;
|
|
|
|
.rodata
|
2013-05-09 11:56:54 +00:00
|
|
|
d: .addr get ; SCANFDATA::GET
|
|
|
|
.addr unget ; SCANFDATA::UNGET
|
|
|
|
; .addr 0 ; SCANFDATA::DATA (not used)
|
2005-02-14 09:21:54 +00:00
|
|
|
|
|
|
|
.code
|
2013-05-09 11:56:54 +00:00
|
|
|
pha ; Save low byte of ap
|
2005-02-14 09:21:54 +00:00
|
|
|
txa
|
2013-05-09 11:56:54 +00:00
|
|
|
pha ; Save high byte of ap
|
|
|
|
ldx #0
|
|
|
|
stx pushed
|
2005-02-14 09:21:54 +00:00
|
|
|
|
|
|
|
; Put &d on the stack in front of the format pointer.
|
|
|
|
|
|
|
|
lda #<d
|
|
|
|
ldx #>d
|
|
|
|
jsr swapstk ; Swap .XA with top-of-stack
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr pushax ; Put format pointer back on stack
|
2005-02-14 09:21:54 +00:00
|
|
|
|
|
|
|
; Restore ap, and jump to _scanf which will clean up the stack.
|
|
|
|
|
|
|
|
pla
|
|
|
|
tax
|
|
|
|
pla
|
2013-05-09 11:56:54 +00:00
|
|
|
jmp __scanf
|
2005-02-14 09:21:54 +00:00
|
|
|
.endproc
|
|
|
|
|