2 ; Oliver Schmidt, 30.12.2004
4 ; int open (const char* name, int flags, ...);
7 .export _open, closedirect, freebuffer
8 .export __filetype, __auxtype, __datetime
9 .constructor raisefilelevel
10 .destructor closeallfiles, 5
12 .import pushname, popname, __dos_type
13 .import iobuf_alloc, iobuf_free
14 .import addysp, incsp4, incaxy, pushax, popax
16 .include "zeropage.inc"
20 .include "filedes.inc"
34 ; Throw away all parameters except name
35 ; and flags occupying together 4 bytes
42 ; Start with first fdtab slot
45 ; Check for free fdtab slot
46 : lda fdtab + FD::REF_NUM,y
49 ; Advance to next fdtab slot
50 .assert .sizeof(FD) = 4, error
56 ; Check for end of fdtab
57 cpy #MAX_FDS * .sizeof(FD)
64 errno: jsr incsp4 ; Preserves A
74 lda #<(fdtab + FD::BUFFER)
75 ldx #>(fdtab + FD::BUFFER)
80 jsr pushax ; Preserves A
87 sta tmp2 ; Save fdtab slot
90 tya ; Restore errno code
105 sta mliparam + MLI::OPEN::PATHNAME
106 stx mliparam + MLI::OPEN::PATHNAME+1
108 ; Check for create flag
109 lda tmp3 ; Restore flags
113 ; PATHNAME already set
114 .assert MLI::CREATE::PATHNAME = MLI::OPEN::PATHNAME, error
116 ; Set all other parameters from template
117 ldx #(MLI::CREATE::CREATE_TIME+1) - (MLI::CREATE::PATHNAME+1) - 1
119 sta mliparam + MLI::CREATE::ACCESS,x
129 ; Check for ordinary errors
130 cmp #$47 ; "Duplicate filename"
133 ; Check for exclusive flag
134 lda tmp3 ; Restore flags
138 lda #$47 ; "Duplicate filename"
141 oserr2: jsr popname ; Preserves A
143 oserr1: ldy tmp2 ; Restore fdtab slot
146 pha ; Save oserror code
148 pla ; Restore oserror code
153 open: ldy tmp2 ; Restore fdtab slot
155 ; Set allocated I/O buffer
156 ldx fdtab + FD::BUFFER+1,y
157 sta mliparam + MLI::OPEN::IO_BUFFER ; A = 0
158 stx mliparam + MLI::OPEN::IO_BUFFER+1
167 ldx mliparam + MLI::OPEN::REF_NUM
170 ; Set flags and check for truncate flag
171 lda tmp3 ; Restore flags
172 sta fdtab + FD::FLAGS,y
176 ; Set fd and zero size
177 stx mliparam + MLI::EOF::REF_NUM
180 : sta mliparam + MLI::EOF::EOF,x
191 pha ; Save oserror code
192 lda tmp1 ; Restore fd
194 pla ; Restore oserror code
195 bne oserr2 ; Branch always
198 done: lda tmp1 ; Restore fd
199 sta fdtab + FD::REF_NUM,y
201 ; Convert fdtab slot to handle
202 .assert .sizeof(FD) = 4, error
208 jsr popname ; Preserves A
218 ldx fdtab + FD::BUFFER+1,y
223 sta mliparam + MLI::CLOSE::REF_NUM
231 ; All open files with current level (or higher)
235 ; Restore original file level
243 CREATE: .byte %11000011 ; ACCESS: Standard full access
245 .byte $06 ; FILE_TYPE: Standard binary file
247 .word $0000 ; AUX_TYPE: Load address N/A
248 .byte $01 ; STORAGE_TYPE: Standard seedling file
250 .word $0000 ; CREATE_DATE: Current date
251 .word $0000 ; CREATE_TIME: Current time