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