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 "../inc/geossym.inc"
16 .include "../inc/const.inc"
19 .importzp ptr1, ptr2, ptr3, tmp1
22 .import _FindFile, _ReadByte
24 .export _open, _close, _read
27 ;--------------------------------------------------------------------------
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)
38 jsr addysp ; fix stack, throw away unused parameters
40 ; Parameters ok. Pop the flags and save them into tmp3
49 lda filedesc ; is there a file already open?
52 lda tmp1 ; check open mode
53 and #(O_RDWR | O_CREAT)
54 cmp #O_RDONLY ; only O_RDONLY is valid
59 jsr _FindFile ; try to find the file
63 lda dirEntryBuf + OFF_DE_TR_SC ; tr&se for ReadByte (r1)
65 lda dirEntryBuf + OFF_DE_TR_SC + 1
67 lda #<diskBlkBuf ; buffer for ReadByte (r4)
71 ldx #0 ; offset for ReadByte (r5)
74 lda #FILEDES ; return fd
93 ; a/x - number of bytes
95 ; popax - fd, must be == to the above one
96 ; return -1+__oserror or number of bytes read
102 sta ptr1+1 ; -(# of bytes to read)-1
105 stx ptr2+1 ; buffer ptr
110 bne @notopen ; fd must be == FILEDES
114 sta ptr3+1 ; put 0 into ptr3 (number of bytes read)
116 lda f_track ; restore stuff for ReadByte
130 bcc @L3 ; branch always
133 ldy #0 ; store the byte
135 inc ptr2 ; increment target address
139 @L1: inc ptr3 ; increment byte count
143 @L2: lda __oserror ; was there error ?
145 cmp #BFR_OVERFLOW ; EOF?
149 @L3: inc ptr1 ; decrement the count
155 lda r1L ; preserve data for ReadByte
168 lda ptr3 ; return byte count
173 lda #61 ; File not open
181 filedesc: .res 1 ; file open flag - 0 (no file opened) or 1
182 f_track: .res 1 ; values preserved for ReadByte