]> git.sur5r.net Git - cc65/blob - libsrc/atari/read.s
put zeropage variables into EXTZP segment
[cc65] / libsrc / atari / read.s
1 ;
2 ; Christian Groessler, Jul-2005
3 ;
4 ; int __fastcall__ read(int fd,void *buf,int count)
5 ;
6
7         .include "atari.inc"
8         .import __rwsetup,__do_oserror,__inviocb,__oserror
9         .export _read
10
11 _read:  jsr     __rwsetup       ; do common setup for read and write
12         beq     done            ; if size 0, it's a no-op
13         cpx     #$FF            ; invalid iocb?
14         beq     _inviocb
15
16 .ifdef  LINEBUF
17         ; E: should be always at IOCB #0
18         ; fixme: what happens when user closes and reopens stdin?
19         cpx     #0              ; E: handler (line oriented keyboard input)?
20         beq     do_line
21 .endif
22
23         lda     #GETCHR         ; iocb command code
24         sta     ICCOM,x
25         jsr     CIOV            ; read it
26         bpl     done
27         cpy     #EOFERR         ; eof is treated specially
28         beq     done
29         jmp     __do_oserror    ; update errno
30
31 done:   lda     ICBLL,x         ; buf len lo
32         pha                     ; save
33         lda     ICBLH,x         ; get buf len hi
34         tax                     ; to X
35 okdone: lda     #0
36         sta     __oserror       ; clear system dependend error code
37         pla                     ; get buf len lo
38         rts
39
40 _inviocb:
41         jmp     __inviocb
42
43
44 .ifdef  LINEBUF
45
46 ; line oriented input
47
48 do_line:
49         lda     buflen          ; line buffer active?
50         bne     use_buf         ; yes, get data from there
51
52         ; save user buffer address & length
53         ; update IOCB to point to line buffer
54         lda     ICBLL,x
55         pha
56         lda     #LINEBUF
57         sta     ICBLL,x
58         ;--------
59         lda     ICBLH,x
60         pha
61         lda     #0
62         sta     ICBLH,x
63         ;--------
64         lda     ICBAL,x
65         pha
66         lda     #<linebuf
67         sta     ICBAL,x
68         ;--------
69         lda     ICBAH,x
70         pha
71         lda     #>linebuf
72         sta     ICBAH,x
73
74         lda     #GETREC
75         sta     ICCOM,x
76         jsr     CIOV            ; read input data
77         bpl     newbuf
78         cpy     #EOFERR         ; eof is treated specially
79         beq     newbuf
80         pla                     ; fix stack
81         pla
82         pla
83         pla
84         jmp     __do_oserror    ; update errno
85
86 newbuf:
87         lda     ICBLL,x         ; get # of bytes read
88         sta     buflen
89         lda     #0
90         sta     index           ; fresh buffer
91
92         ; restore user buffer address & length
93         pla
94         sta     ICBAH,x
95         ;--------
96         pla
97         sta     ICBAL,x
98         ;--------
99         pla
100         sta     ICBLH,x
101         ;--------
102         pla
103         sta     ICBLL,x
104
105         ; fall into use_buf
106         lda     buflen
107
108 ; return bytes from line buffer
109 ; use buflen and index to access buffer
110 ; update index
111 ; use dataptr as a temporary pointer
112
113 use_buf:
114         sec
115         sbc     index           ; size of unread data in the buffer
116         sta     cbs
117
118         lda     ICBLL,x         ; buf len lo
119         cmp     cbs             ; larger than buffer size?
120         beq     bl1
121         bcs     btsmall         ; yes, adjust length
122
123 bl1:    lda     ICBLH,x         ; get buf len hi
124         bne     btsmall         ; buffer too small: buffer contents < read size
125
126 ; copy ICBLL,x bytes
127
128 icbll_copy:
129
130         lda     ICBAL,x         ; buffer address
131         sta     dataptr
132         lda     ICBAH,x         ; buffer address
133         sta     dataptr+1
134         lda     ICBLL,x
135         sta     copylen
136         pha                     ; remember for return value
137         ldy     #0
138         ldx     index
139
140 copy:   lda     linebuf,x
141         sta     (dataptr),y
142         iny
143         inx
144         dec     copylen
145         bne     copy
146
147         pla                     ; length
148         pha                     ; save length to return at okdone
149
150         clc
151         adc     index
152         sta     index
153         cmp     buflen          ; buffer used up?
154         bcc     c1              ; not yet
155
156         lda     #0
157         sta     buflen          ; indicate empty line buffer
158
159 c1:     ldx     #0
160         jmp     okdone          ; return to caller
161
162 btsmall:
163         lda     cbs
164         sta     ICBLL,x
165         bpl     icbll_copy
166
167         .segment        "EXTZP" : zeropage
168
169 index:  .res    1               ; index into line buffer
170 buflen: .res    1               ; length of used part of buffer
171 cbs:    .res    1               ; current buffer size: buflen - index
172 dataptr:.res    2               ; temp pointer to user buffer
173 copylen:.res    1               ; temp counter
174
175         .bss
176
177 linebuf:.res    LINEBUF         ; the line buffer
178
179 .endif          ; .ifdef LINEBUF
180