forked from ytmytm/c64-lng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdio.s
executable file
·118 lines (91 loc) · 2.53 KB
/
stdio.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
.export _close
.export _write
.import __oserror
.importzp sp, ptr1, ptr2, ptr3, tmp2
.import popax
.import __errno
.import _stdout
.include "fcntl.inc"
.include "cbm.inc"
.include "../../include/jumptab.ca65.h"
EINVAL=1 ; error: invalid handle
MAX_FDS=5 ; max filedescriptors per task
.include "filedes.inc"
_close:
tax
jsr lkf_fclose
lda #0
tax
rts
;--------------------------------------------------------------------------
; rwcommon: Pop the parameters from stack, preprocess them and place them
; into zero page locations. Return carry set if the handle is invalid,
; return carry clear if it is ok. If the carry is clear, the handle is
; returned in A.
rwcommon:
eor #$FF
sta ptr1
txa
eor #$FF
sta ptr1+1 ; Remember -count-1
jsr popax ; Get buf
sta ptr2
stx ptr2+1
lda #$00
sta ptr3
sta ptr3+1 ; Clear ptr3
jsr popax ; Get the handle
; cpx #$01
; bcs invhandle
; cmp #MAX_FDS
; bcs invhandle
; sta tmp2
rts ; Return with carry clear
;invhandle:
; lda #EINVAL
; sta __errno
; lda #0
; sta __errno+1
; rts ; Return with carry set
;; size_t write(int fd,char *buf,size_t count)
_write:
jsr rwcommon
tax ; handle into X
;; do this in constructor
;; lda #LFN_WRITE
;; sta fdtab+STDOUT_FILENO
;; sta fdtab+STDERR_FILENO
;; lda #CBMDEV_SCREEN
;; sta unittab+STDOUT_FILENO
;; sta unittab+STDERR_FILENO
;; lda #FILENO_STDOUT
;; sta __filetab+STDOUT_FILENO
; Output the next character from the buffer
@L0: ldy #0
lda (ptr2),y
inc ptr2
bne @L1
inc ptr2+1 ; A = *buf++;
@L1:
;; inc $d020
;; ldx #stdout
jsr lkf_fputc
bcs error ; Bail out on errors
; Count characters written
inc ptr3
bne @L2
inc ptr3+1
; Decrement count
@L2: inc ptr1
bne @L0
inc ptr1+1
bne @L0
; Return the number of chars written
lda ptr3
ldx ptr3+1
rts
error:
;; sta __oserror
errout: lda #$FF
tax ; Return -1
rts