2 ; Low level file I/O routines, ONLY for module loading OR sth similar
4 ; Maciej 'YTM/Elysium' Witkowiak <ytm@elysium.pl>
7 ; only ONE opened file at a time, only O_RDONLY flag
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);
13 FILEDES = 3 ; first free to use file descriptor
15 .importzp ptr1, ptr2, ptr3, tmp1
18 .import _FindFile, _ReadByte
19 .export _open, _close, _read
21 .include "geossym.inc"
27 cpy #4 ; correct # of arguments (bytes)?
28 beq @parmok ; parameter count ok
29 tya ; parm count < 4 shouldn't be needed to be...
30 sec ; ...checked (it generates a c compiler warning)
33 jsr addysp ; fix stack, throw away unused parameters
35 ; Parameters ok. Pop the flags and save them into tmp3
44 lda filedesc ; is there a file already open?
47 lda tmp1 ; check open mode
48 and #(O_RDWR | O_CREAT)
49 cmp #O_RDONLY ; only O_RDONLY is valid
54 jsr _FindFile ; try to find the file
58 lda dirEntryBuf + OFF_DE_TR_SC ; tr&se for ReadByte (r1)
60 lda dirEntryBuf + OFF_DE_TR_SC + 1
62 lda #<diskBlkBuf ; buffer for ReadByte (r4)
66 ldx #0 ; offset for ReadByte (r5)
72 lda #FILEDES ; return fd
76 lda #EINVAL ; invalid parameters - invalid open mode
79 lda #EMFILE ; too many opened files (there can be only one)
80 jmp __directerrno ; set errno, clear oserror, return -1
82 jmp __mappederrno ; set platform error code, return -1
87 jsr __seterrno ; clear errors
94 ; a/x - number of bytes
96 ; popax - fd, must be == to the above one
97 ; return -1+__oserror or number of bytes read
103 sta ptr1+1 ; -(# of bytes to read)-1
106 stx ptr2+1 ; buffer ptr
108 cmp #FILEDES ; lo-byte == FILEDES
111 beq @fileok ; fd must be == FILEDES
115 jmp __directerrno ; Sets _errno, clears _oserror, returns -1
120 sta ptr3+1 ; put 0 into ptr3 (number of bytes read)
121 sta __oserror ; clear error flags
124 lda f_track ; restore stuff for ReadByte
138 bcc @L3 ; branch always
141 ldy #0 ; store the byte
143 inc ptr2 ; increment target address
147 @L1: inc ptr3 ; increment byte count
151 @L2: lda __oserror ; was there error ?
153 cmp #BFR_OVERFLOW ; EOF?
154 beq @done ; yes, we're done
155 jmp __mappederrno ; no, we're screwed
157 @L3: inc ptr1 ; decrement the count
163 lda r1L ; preserve data for ReadByte
176 lda ptr3 ; return byte count
183 .res 1 ; file open flag - 0 (no file opened) or 1
185 .res 1 ; values preserved for ReadByte