]> git.sur5r.net Git - cc65/blob - libsrc/geos/devel/fio_module.s
Minor comment adjustment.
[cc65] / libsrc / geos / devel / fio_module.s
1 ;
2 ; Low level file I/O routines, ONLY for module loading OR sth similar
3 ;
4 ; Maciej 'YTM/Elysium' Witkowiak <ytm@elysium.pl>
5 ; 25.12.2002
6 ;
7 ; only ONE opened file at a time, only O_RDONLY flag
8
9 ; int open (const char* name, int flags, ...);  /* May take a mode argument */
10 ; int __fastcall__ close (int fd);
11 ; int __fastcall__ read (int fd, void* buf, unsigned count);
12
13 FILEDES = 3     ; first free to use file descriptor
14
15         .include        "../inc/geossym.inc"
16         .include        "../inc/const.inc"
17         .include        "fcntl.inc"
18
19         .importzp       ptr1, ptr2, ptr3, tmp1
20         .import         addysp, popax
21         .import         __oserror
22         .import         _FindFile, _ReadByte
23
24         .export         _open, _close, _read
25
26
27 ;--------------------------------------------------------------------------
28 ; _open
29
30 _open:
31
32         cpy     #4              ; correct # of arguments (bytes)?
33         beq     @parmok         ; parameter count ok
34         tya                     ; parm count < 4 shouldn't be needed to be...
35         sec                     ; ...checked (it generates a c compiler warning)
36         sbc     #4
37         tay
38         jsr     addysp          ; fix stack, throw away unused parameters
39
40 ; Parameters ok. Pop the flags and save them into tmp3
41
42 @parmok:
43         jsr     popax           ; Get flags
44         sta     tmp1
45         jsr     popax           ; Get name
46         sta     ptr1
47         stx     ptr1+1
48
49         lda     filedesc        ; is there a file already open?
50         bne     @alreadyopen
51
52         lda     tmp1            ; check open mode
53         and     #(O_RDWR | O_CREAT)
54         cmp     #O_RDONLY       ; only O_RDONLY is valid
55         bne     @badmode
56
57         lda     ptr1
58         ldx     ptr1+1
59         jsr     _FindFile       ; try to find the file
60         tax
61         bne     @error
62
63         lda     dirEntryBuf + OFF_DE_TR_SC              ; tr&se for ReadByte (r1)
64         sta     f_track
65         lda     dirEntryBuf + OFF_DE_TR_SC + 1
66         sta     f_sector
67         lda     #<diskBlkBuf                            ; buffer for ReadByte (r4)
68         sta     f_buffer
69         lda     #>diskBlkBuf
70         sta     f_buffer+1
71         ldx     #0                                      ; offset for ReadByte (r5)
72         stx     f_offset
73         stx     f_offset+1
74         lda     #FILEDES        ; return fd
75         sta     filedesc
76         rts
77 @badmode:
78 @alreadyopen:
79         lda     #70             ; no channel
80         sta     __oserror
81 @error:
82         lda     #$ff
83         tax
84         rts
85
86 _close:
87         lda #0                  ; clear fd
88         sta filedesc
89         tax
90         rts
91
92 _read:
93     ; a/x - number of bytes
94     ; popax - buffer ptr
95     ; popax - fd, must be == to the above one
96     ; return -1+__oserror or number of bytes read
97
98         eor     #$ff
99         sta     ptr1
100         txa
101         eor     #$ff
102         sta     ptr1+1          ; -(# of bytes to read)-1
103         jsr     popax
104         sta     ptr2
105         stx     ptr2+1          ; buffer ptr
106         jsr     popax
107         cmp     #FILEDES
108         bne     @notopen
109         txa
110         bne     @notopen        ; fd must be == FILEDES
111
112         lda     #0
113         sta     ptr3
114         sta     ptr3+1          ; put 0 into ptr3 (number of bytes read)
115
116         lda     f_track         ; restore stuff for ReadByte
117         ldx     f_sector
118         sta     r1L
119         stx     r1H
120         lda     f_buffer
121         ldx     f_buffer+1
122         sta     r4L
123         stx     r4H
124         lda     f_offset
125         ldx     f_offset+1
126         sta     r5L
127         stx     r5H
128
129         clc
130         bcc     @L3             ; branch always
131
132 @L0:    jsr     _ReadByte
133         ldy     #0              ; store the byte
134         sta     (ptr2),y
135         inc     ptr2            ; increment target address
136         bne     @L1
137         inc     ptr2+1
138
139 @L1:    inc     ptr3            ; increment byte count
140         bne     @L2
141         inc     ptr3+1
142
143 @L2:    lda     __oserror       ; was there error ?
144         beq     @L3
145         cmp     #BFR_OVERFLOW   ; EOF?
146         bne     @error
147         beq     @done
148
149 @L3:    inc     ptr1            ; decrement the count
150         bne     @L0
151         inc     ptr1+1
152         bne     @L0
153
154 @done:
155         lda     r1L             ; preserve data for ReadByte
156         ldx     r1H
157         sta     f_track
158         stx     f_sector
159         lda     r4L
160         ldx     r4H
161         sta     f_buffer
162         stx     f_buffer+1
163         lda     r5L
164         ldx     r5H
165         sta     f_offset
166         stx     f_offset+1
167         
168         lda     ptr3            ; return byte count
169         ldx     ptr3+1
170         rts
171
172 @notopen:
173         lda     #61             ; File not open
174 @error:
175         sta     __oserror
176         lda     #$ff
177         tax
178         rts
179
180 .bss
181 filedesc:       .res 1          ; file open flag - 0 (no file opened) or 1
182 f_track:        .res 1          ; values preserved for ReadByte
183 f_sector:       .res 1
184 f_offset:       .res 2
185 f_buffer:       .res 2