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 .include "geossym.inc"
20 .importzp ptr1, ptr2, ptr3, tmp1
23 .import _FindFile, _ReadByte
25 .export _open, _close, _read
28 ;--------------------------------------------------------------------------
33 cpy #4 ; correct # of arguments (bytes)?
34 beq @parmok ; parameter count ok
35 tya ; parm count < 4 shouldn't be needed to be...
36 sec ; ...checked (it generates a c compiler warning)
39 jsr addysp ; fix stack, throw away unused parameters
41 ; Parameters ok. Pop the flags and save them into tmp3
50 lda filedesc ; is there a file already open?
53 lda tmp1 ; check open mode
54 and #(O_RDWR | O_CREAT)
55 cmp #O_RDONLY ; only O_RDONLY is valid
60 jsr _FindFile ; try to find the file
64 lda dirEntryBuf + OFF_DE_TR_SC ; tr&se for ReadByte (r1)
66 lda dirEntryBuf + OFF_DE_TR_SC + 1
68 lda #<diskBlkBuf ; buffer for ReadByte (r4)
72 ldx #0 ; offset for ReadByte (r5)
78 lda #FILEDES ; return fd
82 lda #EINVAL ; invalid parameters - invalid open mode
85 lda #EMFILE ; too many opened files (there can be only one)
86 jmp __directerrno ; set errno, clear oserror, return -1
88 jmp __mappederrno ; set platform error code, return -1
93 jsr __seterrno ; clear errors
100 ; a/x - number of bytes
102 ; popax - fd, must be == to the above one
103 ; return -1+__oserror or number of bytes read
109 sta ptr1+1 ; -(# of bytes to read)-1
112 stx ptr2+1 ; buffer ptr
114 cmp #FILEDES ; lo-byte == FILEDES
117 beq @fileok ; fd must be == FILEDES
121 jmp __directerrno ; Sets _errno, clears _oserror, returns -1
126 sta ptr3+1 ; put 0 into ptr3 (number of bytes read)
127 sta __oserror ; clear error flags
130 lda f_track ; restore stuff for ReadByte
144 bcc @L3 ; branch always
147 ldy #0 ; store the byte
149 inc ptr2 ; increment target address
153 @L1: inc ptr3 ; increment byte count
157 @L2: lda __oserror ; was there error ?
159 cmp #BFR_OVERFLOW ; EOF?
160 beq @done ; yes, we're done
161 jmp __mappederrno ; no, we're screwed
163 @L3: inc ptr1 ; decrement the count
169 lda r1L ; preserve data for ReadByte
182 lda ptr3 ; return byte count
187 filedesc: .res 1 ; file open flag - 0 (no file opened) or 1
188 f_track: .res 1 ; values preserved for ReadByte