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