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
16 .import addysp, popax, popptr1
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
40 jsr popptr1 ; Get name
42 lda filedesc ; is there a file already open?
45 lda tmp1 ; check open mode
46 and #(O_RDWR | O_CREAT)
47 cmp #O_RDONLY ; only O_RDONLY is valid
52 jsr _FindFile ; try to find the file
56 lda dirEntryBuf + OFF_DE_TR_SC ; tr&se for ReadByte (r1)
58 lda dirEntryBuf + OFF_DE_TR_SC + 1
60 lda #<diskBlkBuf ; buffer for ReadByte (r4)
64 ldx #0 ; offset for ReadByte (r5)
70 lda #FILEDES ; return fd
74 lda #EINVAL ; invalid parameters - invalid open mode
77 lda #EMFILE ; too many opened files (there can be only one)
78 jmp __directerrno ; set errno, clear oserror, return -1
80 jmp __mappederrno ; set platform error code, return -1
85 jsr __seterrno ; clear errors
92 ; a/x - number of bytes
94 ; popax - fd, must be == to the above one
95 ; return -1+__oserror or number of bytes read
101 sta ptr1+1 ; -(# of bytes to read)-1
104 stx ptr2+1 ; buffer ptr
106 cmp #FILEDES ; lo-byte == FILEDES
109 beq @fileok ; fd must be == FILEDES
113 jmp __directerrno ; Sets _errno, clears _oserror, returns -1
118 sta ptr3+1 ; put 0 into ptr3 (number of bytes read)
119 sta __oserror ; clear error flags
122 lda f_track ; restore stuff for ReadByte
136 bcc @L3 ; branch always
139 ldy #0 ; store the byte
141 inc ptr2 ; increment target address
145 @L1: inc ptr3 ; increment byte count
149 @L2: lda __oserror ; was there error ?
151 cmp #BFR_OVERFLOW ; EOF?
152 beq @done ; yes, we're done
153 jmp __mappederrno ; no, we're screwed
155 @L3: inc ptr1 ; decrement the count
161 lda r1L ; preserve data for ReadByte
174 lda ptr3 ; return byte count
181 .res 1 ; file open flag - 0 (no file opened) or 1
183 .res 1 ; values preserved for ReadByte