2 ; Christian Groessler, May-2000
4 ; fd indirection table & helper functions
9 .importzp tmp1,tmp2,tmp3,ptr4,sp
10 .import fd_table,fd_index
21 ; gets fd in ax, decrements usage counter
22 ; return iocb index in X
23 ; return N bit set for invalid fd
24 ; return Z bit set if last user
25 ; all registers destroyed
33 lda fd_index,x ; get index
36 sta fd_index,x ; clear entry
38 asl a ; create index into fd table
42 cmp fd_table+ft_iocb,x ; entry in use?
43 beq inval ; no, return error
44 lda fd_table+ft_usa,x ; get usage counter
45 beq ok_notlast ; 0? (shouldn't happen)
47 sbc #1 ; decr usage counter
52 lda fd_table+ft_iocb,x ; get iocb
57 sta fd_table+ft_iocb,y ; clear table entry
58 lda fd_table+ft_flag,y
59 and #16 ; opened by app?
60 eor #16 ; return set Z if yes
67 .endproc ; fdtoiocb_down
69 inval: ldx #$ff ; sets N
73 ; clear iocb except for ICHID field
74 ; expects X to be index to IOCB (0,$10,$20,etc.)
75 ; all registers destroyed
79 inx ; don't clear ICHID
92 ; return iocb index in A, fd_table index in X
93 ; return N bit set for invalid fd
94 ; all registers destroyed
103 asl a ; create index into fd table
107 cmp fd_table+ft_iocb,x ; entry in use?
108 beq inval ; no, return error
109 lda fd_table+ft_usa,x ; get usage counter
110 beq inval ; 0? should not happen
111 lda fd_table+ft_iocb,x ; get iocb
117 ; no entry parameters
118 ; return ZF = 0/1 for not found/found
119 ; index in X if found
120 ; all registers destroyed
135 inx ; return ZF cleared
138 .endproc ; findfreeiocb
140 ; decrements usage counter for fd
141 ; if 0 reached, it's marked as unused
142 ; get fd index in tmp2
143 ; Y register preserved
148 bcs ret ; invalid index, do nothing
155 asl a ; create index into fd table
159 cmp fd_table+ft_iocb,x ; entry in use?
160 beq ret ; no, do nothing
161 lda fd_table+ft_usa,x ; get usage counter
162 beq ret ; 0? should not happen
164 sbc #1 ; decrement by one
165 sta fd_table+ft_usa,x
167 lda #$ff ; 0, table entry unused now
168 sta fd_table+ft_iocb,x ; clear table entry
171 .endproc ; fddecusage
175 ; called from open() function
176 ; finds a fd to use for an open request
177 ; checks whether it's a device or file (file: characters follow the ':')
178 ; files always get an exclusive slot
179 ; for devices it is checked whether the device is already open, and if yes,
180 ; a link to this open device is returned
182 ; Calling parameters:
183 ; tmp3 - length of filename + 1
184 ; AX - points to filename
185 ; Y - iocb to use (if we need a new open)
187 ; tmp2 - fd num ($ff and C=0 in case of error - no free slot)
188 ; C - 0/1 for no open needed/open should be performed
189 ; all registers preserved!
228 ; ptr4 points to filename
238 ; no colon there!? OK, then we use a fresh iocb....
239 ; return error here? no, the subsequent open call should fail
241 do_open_nd: ; do open and don't remember device
246 sta tmp1 ; set flag to return 'open needed' : C = 1
251 cmp fd_table,x ; check ft_iocb field for $ff
252 beq freefnd ; found a free slot
257 cmp #(MAX_FD_VAL*4)+ft_iocb ; end of table reached?
260 ; error: no free slot found
262 stx tmp1 ; return with C = 0
264 stx tmp2 ; iocb: $ff marks error
270 sbc #ft_iocb ; normalize
276 bit tmp1 ; remember device?
278 lda #0 ; no, put 0 in field
282 lda (sp),y ; get device
283 l2: sta fd_table+ft_dev,x ; set device
285 sta fd_table+ft_usa,x ; set usage counter
287 sta fd_table+ft_iocb,x ; set iocb index
289 and #7 ; device number is 3 bits
290 ora #16 ; indicated a fd actively opened by the app
291 sta fd_table+ft_flag,x
293 jsr fdt_to_fdi ; get new index
294 bcs noslot ; no one available
295 ;cmp #$ff ; no one available
296 ;beq noslot ;@@@ cleanup needed
297 sta tmp2 ; return index
300 ; string in "Xn:xxx" format
302 lda (ptr4),y ; get device number
307 sta tmp2 ; save it for speed later here also
308 lda #4 ; max. length if only device + number ("Xn:")
310 bcc do_open_nd ; string is longer -> contains filename
311 bcs check_dev ; handle device only string
313 ; string in "X:xxx" format
314 colon1: lda #3 ; max. length if device only ("X:")
316 bcc do_open_nd ; string is longer -> contains filename
318 ; get device and search it in fd table
321 lda (ptr4),y ; get device id
323 ldx #(MAX_FD_VAL*4) - ft_entrylen
325 cmp fd_table+ft_iocb,x ; is entry valid?
326 beq srch2 ; no, skip this entry
328 cmp fd_table+ft_dev,x
336 ; not found, open new iocb
339 ; helper for branch out of range
342 ; found device in table, check device number (e.g R0 - R3)
343 fnddev: lda fd_table+ft_flag,x
345 cmp tmp2 ; contains devnum
346 bne srch2 ; different device numbers
348 ; found existing open iocb with same device
353 inc fd_table+ft_usa,x ; increment usage counter
354 jsr fdt_to_fdi ; get new index
355 bcs noslot1 ; no one available
356 sta tmp2 ; return index
358 ; clean up and go home
370 lsr a ; set C as needed