]> git.sur5r.net Git - cc65/blob - libsrc/atari/read.s
Fixed _textcolor definition.
[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         .segment        "EXTZP" : zeropage
49
50 index:  .res    1               ; index into line buffer
51 cbs:    .res    1               ; current buffer size: buflen - index
52 dataptr:.res    2               ; temp pointer to user buffer
53 copylen:.res    1               ; temp counter
54
55         .bss
56
57 buflen: .res    1               ; length of used part of buffer
58 linebuf:.res    LINEBUF         ; the line buffer
59
60         .code
61
62 do_line:
63         lda     buflen          ; line buffer active?
64         bne     use_buf         ; yes, get data from there
65
66         ; save user buffer address & length
67         ; update IOCB to point to line buffer
68         lda     ICBLL,x
69         pha
70         lda     #LINEBUF
71         sta     ICBLL,x
72         ;--------
73         lda     ICBLH,x
74         pha
75         lda     #0
76         sta     ICBLH,x
77         ;--------
78         lda     ICBAL,x
79         pha
80         lda     #<linebuf
81         sta     ICBAL,x
82         ;--------
83         lda     ICBAH,x
84         pha
85         lda     #>linebuf
86         sta     ICBAH,x
87
88         lda     #GETREC
89         sta     ICCOM,x
90         jsr     CIOV            ; read input data
91         bpl     newbuf
92         cpy     #EOFERR         ; eof is treated specially
93         beq     newbuf
94         pla                     ; fix stack
95         pla
96         pla
97         pla
98         jmp     __do_oserror    ; update errno
99
100 newbuf:
101         lda     ICBLL,x         ; get # of bytes read
102         sta     buflen
103         lda     #0
104         sta     index           ; fresh buffer
105
106         ; restore user buffer address & length
107         pla
108         sta     ICBAH,x
109         ;--------
110         pla
111         sta     ICBAL,x
112         ;--------
113         pla
114         sta     ICBLH,x
115         ;--------
116         pla
117         sta     ICBLL,x
118
119         ; fall into use_buf
120         lda     buflen
121
122 ; return bytes from line buffer
123 ; use buflen and index to access buffer
124 ; update index
125 ; use dataptr as a temporary pointer
126
127 use_buf:
128         sec
129         sbc     index           ; size of unread data in the buffer
130         sta     cbs
131
132         lda     ICBLL,x         ; buf len lo
133         cmp     cbs             ; larger than buffer size?
134         beq     bl1
135         bcs     btsmall         ; yes, adjust length
136
137 bl1:    lda     ICBLH,x         ; get buf len hi
138         bne     btsmall         ; buffer too small: buffer contents < read size
139
140 ; copy ICBLL,x bytes
141
142 icbll_copy:
143
144         lda     ICBAL,x         ; buffer address
145         sta     dataptr
146         lda     ICBAH,x         ; buffer address
147         sta     dataptr+1
148         lda     ICBLL,x
149         sta     copylen
150         pha                     ; remember for return value
151         ldy     #0
152         ldx     index
153
154 copy:   lda     linebuf,x
155         sta     (dataptr),y
156         iny
157         inx
158         dec     copylen
159         bne     copy
160
161         pla                     ; length
162         pha                     ; save length to return at okdone
163
164         clc
165         adc     index
166         sta     index
167         cmp     buflen          ; buffer used up?
168         bcc     c1              ; not yet
169
170         lda     #0
171         sta     buflen          ; indicate empty line buffer
172
173 c1:     ldx     #0
174         jmp     okdone          ; return to caller
175
176 btsmall:
177         lda     cbs
178         sta     ICBLL,x
179         bpl     icbll_copy
180
181 .endif          ; .ifdef LINEBUF
182