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 __aligned_malloc, _free
16 .import addysp, incsp4, 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)
56 jsr incsp4 ; Preserves A
68 jsr pushax ; Preserves A
76 ; Get and check I/O buffer high byte
80 ; Set I/O buffer high byte (low byte remains zero)
81 sta fdtab + FD::BUFFER+1,y
83 sty tmp2 ; Save fdtab slot
97 sta mliparam + MLI::OPEN::PATHNAME
98 stx mliparam + MLI::OPEN::PATHNAME+1
100 ; Check for create flag
101 lda tmp3 ; Restore flags
105 ; PATHNAME already set
106 .assert MLI::CREATE::PATHNAME = MLI::OPEN::PATHNAME, error
108 ; Set all other parameters from template
109 ldx #(MLI::CREATE::CREATE_TIME+1) - (MLI::CREATE::PATHNAME+1) - 1
111 sta mliparam + MLI::CREATE::ACCESS,x
121 ; Check for ordinary errors
122 cmp #$47 ; "Duplicate filename"
125 ; Check for exclusive flag
126 lda tmp3 ; Restore flags
130 lda #$47 ; "Duplicate filename"
133 oserr2: jsr popname ; Preserves A
135 oserr1: ldy tmp2 ; Restore fdtab slot
138 pha ; Save oserror code
140 pla ; Restore oserror code
145 open: ldy tmp2 ; Restore fdtab slot
147 ; Set allocated I/O buffer
148 ldx fdtab + FD::BUFFER+1,y
149 sta mliparam + MLI::OPEN::IO_BUFFER ; A = 0
150 stx mliparam + MLI::OPEN::IO_BUFFER+1
159 ldx mliparam + MLI::OPEN::REF_NUM
162 ; Set flags and check for truncate flag
163 lda tmp3 ; Restore flags
164 sta fdtab + FD::FLAGS,y
168 ; Set fd and zero size
169 stx mliparam + MLI::EOF::REF_NUM
172 : sta mliparam + MLI::EOF::EOF,x
183 pha ; Save oserror code
184 lda tmp1 ; Restore fd
186 pla ; Restore oserror code
187 bne oserr2 ; Branch always
190 done: lda tmp1 ; Restore fd
191 sta fdtab + FD::REF_NUM,y
193 ; Convert fdtab slot to handle
194 .assert .sizeof(FD) = 4, error
200 jsr popname ; Preserves A
209 ldx fdtab + FD::BUFFER+1,y
218 sta mliparam + MLI::CLOSE::REF_NUM
227 CREATE: .byte %11000011 ; ACCESS: Standard full access
228 .byte $06 ; FILE_TYPE: Standard binary file
229 .word $0000 ; AUX_TYPE: Load address N/A
230 .byte $01 ; STORAGE_TYPE: Standard seedling file
231 .word $0000 ; CREATE_DATE: Current date
232 .word $0000 ; CREATE_TIME: Current time