]> git.sur5r.net Git - cc65/blob - libsrc/apple2/open.s
Renamed the defines in symdefs.h to something more meaningful. They were named
[cc65] / libsrc / apple2 / open.s
1 ;
2 ; Oliver Schmidt, 30.12.2004
3 ;
4 ; int open (const char* name, int flags, ...);
5 ;
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).
9
10         .export         _open, closedirect, freebuffer
11         .destructor     closeallfiles, 17
12
13         .import         pushname, popname
14         .import         iobuf_alloc, iobuf_free
15         .import         addysp, incsp4, incaxy, pushax, popax
16
17         .include        "zeropage.inc"
18         .include        "errno.inc"
19         .include        "fcntl.inc"
20         .include        "mli.inc"
21         .include        "filedes.inc"
22
23 _open:
24         ; Throw away all parameters except name
25         ; and flags occupying together 4 bytes
26         dey
27         dey
28         dey
29         dey
30         jsr     addysp
31
32         ; Start with first fdtab slot
33         ldy     #$00
34
35         ; Check for free fdtab slot
36 :       lda     fdtab + FD::REF_NUM,y
37         beq     found
38
39         ; Advance to next fdtab slot
40         .assert .sizeof(FD) = 4, error
41         iny
42         iny
43         iny
44         iny
45
46         ; Check for end of fdtab
47         cpy     #MAX_FDS * .sizeof(FD)
48         bcc     :-
49
50         ; Load errno code
51         lda     #EMFILE
52
53         ; Cleanup stack
54 errno:  jsr     incsp4          ; Preserves A
55
56         ; Set __errno
57         jmp     __directerrno
58
59         ; Save fdtab slot
60 found:  tya
61         pha
62
63         ; Alloc I/O buffer
64         lda     #<(fdtab + FD::BUFFER)
65         ldx     #>(fdtab + FD::BUFFER)
66         jsr     incaxy
67         jsr     pushax
68         lda     #$00
69         ldx     #>$0100
70         jsr     pushax          ; Preserves A
71         ldx     #>$0400
72         jsr     iobuf_alloc
73         tay                     ; Save errno code
74
75         ; Restore fdtab slot
76         pla
77         sta     tmp2            ; Save fdtab slot
78
79         ; Check for error
80         tya                     ; Restore errno code
81         bne     errno
82
83         ; Get and save flags
84         jsr     popax
85         sta     tmp3
86
87         ; Get and push name
88         jsr     popax
89         jsr     pushname
90         bne     oserr1
91
92         ; Set pushed name
93         lda     sp
94         ldx     sp+1
95         sta     mliparam + MLI::OPEN::PATHNAME
96         stx     mliparam + MLI::OPEN::PATHNAME+1
97
98         ; Check for create flag
99         lda     tmp3            ; Restore flags
100         and     #O_CREAT
101         beq     open
102
103         ; PATHNAME already set
104         .assert MLI::CREATE::PATHNAME = MLI::OPEN::PATHNAME, error
105
106         ; Set all other parameters from template
107         ldx     #(MLI::CREATE::CREATE_TIME+1) - (MLI::CREATE::PATHNAME+1) - 1
108 :       lda     CREATE,x
109         sta     mliparam + MLI::CREATE::ACCESS,x
110         dex
111         bpl     :-
112
113         ; Create file
114         lda     #CREATE_CALL
115         ldx     #CREATE_COUNT
116         jsr     callmli
117         bcc     open
118
119         ; Check for ordinary errors
120         cmp     #$47            ; "Duplicate filename"
121         bne     oserr2
122
123         ; Check for exclusive flag
124         lda     tmp3            ; Restore flags
125         and     #O_EXCL
126         beq     open
127
128         lda     #$47            ; "Duplicate filename"
129
130         ; Cleanup name
131 oserr2: jsr     popname         ; Preserves A
132
133 oserr1: ldy     tmp2            ; Restore fdtab slot
134
135         ; Cleanup I/O buffer
136         pha                     ; Save oserror code
137         jsr     freebuffer
138         pla                     ; Restore oserror code
139
140         ; Set __oserror
141         jmp     __mappederrno
142
143 open:   ldy     tmp2            ; Restore fdtab slot
144
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
149
150         ; Open file
151         lda     #OPEN_CALL
152         ldx     #OPEN_COUNT
153         jsr     callmli
154         bcs     oserr2
155
156         ; Get and save fd
157         ldx     mliparam + MLI::OPEN::REF_NUM
158         stx     tmp1            ; Save fd
159
160         ; Set flags and check for truncate flag
161         lda     tmp3            ; Restore flags
162         sta     fdtab + FD::FLAGS,y
163         and     #O_TRUNC
164         beq     done
165
166         ; Set fd and zero size
167         stx     mliparam + MLI::EOF::REF_NUM
168         ldx     #$02
169         lda     #$00
170 :       sta     mliparam + MLI::EOF::EOF,x
171         dex
172         bpl     :-
173
174         ; Set file size
175         lda     #SET_EOF_CALL
176         ldx     #EOF_COUNT
177         jsr     callmli
178         bcc     done
179
180         ; Cleanup file
181         pha                     ; Save oserror code
182         lda     tmp1            ; Restore fd
183         jsr     closedirect
184         pla                     ; Restore oserror code
185         bne     oserr2          ; Branch always
186
187         ; Store fd
188 done:   lda     tmp1            ; Restore fd
189         sta     fdtab + FD::REF_NUM,y
190
191         ; Convert fdtab slot to handle
192         .assert .sizeof(FD) = 4, error
193         tya
194         lsr
195         lsr
196
197         ; Cleanup name
198         jsr     popname         ; Preserves A
199
200         ; Return success
201         ldx     #$00
202         stx     __oserror
203         rts
204
205 freebuffer:
206         ; Free I/O buffer
207         lda     #$00
208         ldx     fdtab + FD::BUFFER+1,y
209         jmp     iobuf_free
210
211 closeallfiles:
212         ; All open files
213         lda     #$00
214
215 closedirect:
216         ; Set fd
217         sta     mliparam + MLI::CLOSE::REF_NUM
218
219         ; Call close
220         lda     #CLOSE_CALL
221         ldx     #CLOSE_COUNT
222         jmp     callmli
223
224         .rodata
225
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