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