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
42 ; Advance to next fdtab slot
49 .error "Assertion failed"
52 ; Check for end of fdtab
53 cpy #MAX_FDS * .sizeof(FD)
61 jsr incsp4 ; Preserves A
73 jsr pushax ; Preserves A
81 ; Get and check I/O buffer high byte
85 ; Set I/O buffer high byte (low byte remains zero)
86 sta fdtab + FD::BUFFER+1,y
88 sty tmp2 ; Save fdtab slot
102 sta mliparam + MLI::OPEN::PATHNAME
103 stx mliparam + MLI::OPEN::PATHNAME+1
105 ; Check for create flag
106 lda tmp3 ; Restore flags
110 .if MLI::CREATE::PATHNAME = MLI::OPEN::PATHNAME
112 ; PATHNAME already set
115 .error "Assertion failed"
118 ; Set all other parameters from template
119 ldx #(MLI::CREATE::CREATE_TIME+1) - (MLI::CREATE::PATHNAME+1) - 1
121 sta mliparam + MLI::CREATE::ACCESS,x
131 ; Check for ordinary errors
132 cmp #$47 ; "Duplicate filename"
135 ; Check for exclusive flag
136 lda tmp3 ; Restore flags
140 lda #$47 ; "Duplicate filename"
143 oserr2: jsr popname ; Preserves A
145 oserr1: ldy tmp2 ; Restore fdtab slot
148 pha ; Save oserror code
150 pla ; Restore oserror code
155 open: ldy tmp2 ; Restore fdtab slot
157 ; Set allocated I/O buffer
158 ldx fdtab + FD::BUFFER+1,y
159 sta mliparam + MLI::OPEN::IO_BUFFER ; A = 0
160 stx mliparam + MLI::OPEN::IO_BUFFER+1
169 ldx mliparam + MLI::OPEN::REF_NUM
172 ; Set flags and check for truncate flag
173 lda tmp3 ; Restore flags
174 sta fdtab + FD::FLAGS,y
178 ; Set fd and zero size
179 stx mliparam + MLI::EOF::REF_NUM
182 : sta mliparam + MLI::EOF::EOF,x
193 pha ; Save oserror code
194 lda tmp1 ; Restore fd
196 pla ; Restore oserror code
197 bne oserr2 ; Branch always
200 done: lda tmp1 ; Restore fd
201 sta fdtab + FD::REF_NUM,y
205 ; Convert fdtab slot to handle
211 .error "Assertion failed"
215 jsr popname ; Preserves A
224 ldx fdtab + FD::BUFFER+1,y
233 sta mliparam + MLI::CLOSE::REF_NUM
242 CREATE: .byte %11000011 ; ACCESS: Standard full access
243 .byte $06 ; FILE_TYPE: Standard binary file
244 .word $0000 ; AUX_TYPE: Load address N/A
245 .byte $01 ; STORAGE_TYPE: Standard seedling file
246 .word $0000 ; CREATE_DATE: Current date
247 .word $0000 ; CREATE_TIME: Current time