2 ; Oliver Schmidt, 30.12.2004
4 ; int open (const char* name, int flags, ...);
6 ; Be sure to keep the value priority of closeallfiles lower than that of
7 ; closeallstreams (which is the high level C file I/O counterpart and must be
8 ; called before closeallfiles).
10 .export _open, closedirect, freebuffer
11 .destructor closeallfiles, 17
13 .import pushname, popname
14 .import iobuf_alloc, iobuf_free
15 .import addysp, incsp4, incaxy, pushax, popax
17 .include "zeropage.inc"
21 .include "filedes.inc"
24 ; Throw away all parameters except name
25 ; and flags occupying together 4 bytes
32 ; Start with first fdtab slot
35 ; Check for free fdtab slot
36 : lda fdtab + FD::REF_NUM,y
39 ; Advance to next fdtab slot
40 .assert .sizeof(FD) = 4, error
46 ; Check for end of fdtab
47 cpy #MAX_FDS * .sizeof(FD)
54 errno: jsr incsp4 ; Preserves A
64 lda #<(fdtab + FD::BUFFER)
65 ldx #>(fdtab + FD::BUFFER)
70 jsr pushax ; Preserves A
77 sta tmp2 ; Save fdtab slot
80 tya ; Restore errno code
95 sta mliparam + MLI::OPEN::PATHNAME
96 stx mliparam + MLI::OPEN::PATHNAME+1
98 ; Check for create flag
99 lda tmp3 ; Restore flags
103 ; PATHNAME already set
104 .assert MLI::CREATE::PATHNAME = MLI::OPEN::PATHNAME, error
106 ; Set all other parameters from template
107 ldx #(MLI::CREATE::CREATE_TIME+1) - (MLI::CREATE::PATHNAME+1) - 1
109 sta mliparam + MLI::CREATE::ACCESS,x
119 ; Check for ordinary errors
120 cmp #$47 ; "Duplicate filename"
123 ; Check for exclusive flag
124 lda tmp3 ; Restore flags
128 lda #$47 ; "Duplicate filename"
131 oserr2: jsr popname ; Preserves A
133 oserr1: ldy tmp2 ; Restore fdtab slot
136 pha ; Save oserror code
138 pla ; Restore oserror code
143 open: ldy tmp2 ; Restore fdtab slot
145 ; Set allocated I/O buffer
146 ldx fdtab + FD::BUFFER+1,y
147 sta mliparam + MLI::OPEN::IO_BUFFER ; A = 0
148 stx mliparam + MLI::OPEN::IO_BUFFER+1
157 ldx mliparam + MLI::OPEN::REF_NUM
160 ; Set flags and check for truncate flag
161 lda tmp3 ; Restore flags
162 sta fdtab + FD::FLAGS,y
166 ; Set fd and zero size
167 stx mliparam + MLI::EOF::REF_NUM
170 : sta mliparam + MLI::EOF::EOF,x
181 pha ; Save oserror code
182 lda tmp1 ; Restore fd
184 pla ; Restore oserror code
185 bne oserr2 ; Branch always
188 done: lda tmp1 ; Restore fd
189 sta fdtab + FD::REF_NUM,y
191 ; Convert fdtab slot to handle
192 .assert .sizeof(FD) = 4, error
198 jsr popname ; Preserves A
208 ldx fdtab + FD::BUFFER+1,y
217 sta mliparam + MLI::CLOSE::REF_NUM
226 CREATE: .byte %11000011 ; ACCESS: Standard full access
227 .byte $06 ; FILE_TYPE: Standard binary file
228 .word $0000 ; AUX_TYPE: Load address N/A
229 .byte $01 ; STORAGE_TYPE: Standard seedling file
230 .word $0000 ; CREATE_DATE: Current date
231 .word $0000 ; CREATE_TIME: Current time