]> git.sur5r.net Git - cc65/blob - libsrc/cbm/read.s
Made the CBM stdin consoles echo '\n' to the screen.
[cc65] / libsrc / cbm / read.s
1 ;
2 ; 2002-11-16, Ullrich von Bassewitz
3 ; 2013-12-18, Greg King
4 ;
5 ; int read (int fd, void* buf, unsigned count);
6 ;
7
8         .export         _read
9         .constructor    initstdin
10
11         .import         SETLFS, OPEN, CHKIN, BASIN, CLRCH, BSOUT, READST
12         .import         rwcommon
13         .import         popax
14         .importzp       ptr1, ptr2, ptr3, tmp1, tmp2, tmp3
15
16         .include        "cbm.inc"
17         .include        "errno.inc"
18         .include        "fcntl.inc"
19         .include        "filedes.inc"
20
21
22 ;--------------------------------------------------------------------------
23 ; initstdin: Open the stdin file descriptors for the keyboard
24
25 .segment        "INIT"
26
27 .proc   initstdin
28
29         lda     #LFN_READ
30         sta     fdtab+STDIN_FILENO
31         lda     #STDIN_FILENO + LFN_OFFS
32         ldx     #CBMDEV_KBD
33         stx     unittab+STDIN_FILENO
34         ldy     #$FF
35         jsr     SETLFS
36         jmp     OPEN            ; Will always succeed
37
38 .endproc
39
40 ;--------------------------------------------------------------------------
41 ; _read
42
43 .code
44
45 .proc   _read
46
47         jsr     rwcommon        ; Pop params, check handle
48         bcs     invalidfd       ; Invalid handle
49
50 ; Check if the LFN is valid and the file is open for writing
51
52         adc     #LFN_OFFS       ; Carry is already clear
53         tax
54         lda     fdtab-LFN_OFFS,x; Get flags for this handle
55         and     #LFN_READ       ; File open for writing?
56         beq     invalidfd
57
58 ; Check the EOF flag. If it is set, don't read anything
59
60         lda     fdtab-LFN_OFFS,x; Get flags for this handle
61         bmi     eof
62
63 ; Remember the device number.
64
65         ldy     unittab-LFN_OFFS,x
66         sty     unit
67
68 ; Valid lfn. Make it the input file
69
70         jsr     CHKIN
71         bcc     @L3             ; Branch if ok
72         jmp     __mappederrno   ; Store into __oserror, map to errno, return -1
73
74 ; Read the next byte
75
76 @L0:    jsr     BASIN
77         sta     tmp1            ; Save the input byte
78         ldx     unit
79         bne     @L0_1           ; Not keyboard/screen-editor
80         cmp     #$0D            ; Is it a Carriage Return?
81         bne     @L0_1
82         jsr     BSOUT           ; Yes, echo it (because editor didn't)
83
84 @L0_1:  jsr     READST          ; Read the IEEE status
85         sta     tmp3            ; Save it
86         and     #%10111111      ; Check anything but the EOI bit
87         bne     devnotpresent   ; Assume device not present
88
89 ; Store the byte just read
90
91         ldy     #0
92         lda     tmp1
93         sta     (ptr2),y
94         inc     ptr2
95         bne     @L1
96         inc     ptr2+1          ; *buf++ = A;
97
98 ; Increment the byte count
99
100 @L1:    inc     ptr3
101         bne     @L2
102         inc     ptr3+1
103
104 ; Get the status again and check the EOI bit
105
106 @L2:    lda     tmp3
107         and     #%01000000      ; Check for EOI
108         bne     @L4             ; Jump if end of file reached
109
110 ; Decrement the count
111
112 @L3:    inc     ptr1
113         bne     @L0
114         inc     ptr1+1
115         bne     @L0
116         beq     done            ; Branch always
117
118 ; Set the EOI flag and bail out
119
120 @L4:    ldx     tmp2            ; Get the handle
121         lda     #LFN_EOF
122         ora     fdtab,x
123         sta     fdtab,x
124
125 ; Read done, close the input channel
126
127 done:   jsr     CLRCH
128
129 ; Clear _oserror and return the number of chars read
130
131 eof:    lda     #0
132         sta     __oserror
133         lda     ptr3
134         ldx     ptr3+1
135         rts
136
137 ; Error entry: Device not present
138
139 devnotpresent:
140         lda     #ENODEV
141         jmp     __directerrno   ; Sets _errno, clears _oserror, returns -1
142
143 ; Error entry: The given file descriptor is not valid or not open
144
145 invalidfd:
146         lda     #EBADF
147         jmp     __directerrno   ; Sets _errno, clears _oserror, returns -1
148
149 .endproc
150
151
152 ;--------------------------------------------------------------------------
153
154 .bss
155
156 unit:   .res    1
157
158