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 errnoexit, oserrexit
15 .import iobuf_alloc, iobuf_free
16 .import addysp, incsp4, incaxy, pushax, popax
18 .include "zeropage.inc"
22 .include "filedes.inc"
25 ; Throw away all parameters except name
26 ; and flags occupying together 4 bytes
33 ; Start with first fdtab slot
36 ; Check for free fdtab slot
37 : lda fdtab + FD::REF_NUM,y
40 ; Advance to next fdtab slot
41 .assert .sizeof(FD) = 4, error
47 ; Check for end of fdtab
48 cpy #MAX_FDS * .sizeof(FD)
55 errno: jsr incsp4 ; Preserves A
65 lda #<(fdtab + FD::BUFFER)
66 ldx #>(fdtab + FD::BUFFER)
71 jsr pushax ; Preserves A
78 sta tmp2 ; Save fdtab slot
81 tya ; Restore errno code
96 sta mliparam + MLI::OPEN::PATHNAME
97 stx mliparam + MLI::OPEN::PATHNAME+1
99 ; Check for create flag
100 lda tmp3 ; Restore flags
104 ; PATHNAME already set
105 .assert MLI::CREATE::PATHNAME = MLI::OPEN::PATHNAME, error
107 ; Set all other parameters from template
108 ldx #(MLI::CREATE::CREATE_TIME+1) - (MLI::CREATE::PATHNAME+1) - 1
110 sta mliparam + MLI::CREATE::ACCESS,x
120 ; Check for ordinary errors
121 cmp #$47 ; "Duplicate filename"
124 ; Check for exclusive flag
125 lda tmp3 ; Restore flags
129 lda #$47 ; "Duplicate filename"
132 oserr2: jsr popname ; Preserves A
134 oserr1: ldy tmp2 ; Restore fdtab slot
137 pha ; Save oserror code
139 pla ; Restore oserror code
144 open: ldy tmp2 ; Restore fdtab slot
146 ; Set allocated I/O buffer
147 ldx fdtab + FD::BUFFER+1,y
148 sta mliparam + MLI::OPEN::IO_BUFFER ; A = 0
149 stx mliparam + MLI::OPEN::IO_BUFFER+1
158 ldx mliparam + MLI::OPEN::REF_NUM
161 ; Set flags and check for truncate flag
162 lda tmp3 ; Restore flags
163 sta fdtab + FD::FLAGS,y
167 ; Set fd and zero size
168 stx mliparam + MLI::EOF::REF_NUM
171 : sta mliparam + MLI::EOF::EOF,x
182 pha ; Save oserror code
183 lda tmp1 ; Restore fd
185 pla ; Restore oserror code
186 bne oserr2 ; Branch always
189 done: lda tmp1 ; Restore fd
190 sta fdtab + FD::REF_NUM,y
192 ; Convert fdtab slot to handle
193 .assert .sizeof(FD) = 4, error
199 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