2 ; Christian Groessler, May-2000
4 ; fd indirection table & helper functions
9 .importzp tmp1,tmp2,tmp3,ptr4,sp
10 .import fd_table,fd_index
20 ; gets fd in ax, decrements usage counter
21 ; return iocb index in X
22 ; return N bit set for invalid fd
23 ; return Z bit set if last user
24 ; all registers destroyed
32 lda fd_index,x ; get index
35 sta fd_index,x ; clear entry
37 asl a ; create index into fd table
41 cmp fd_table+ft_iocb,x ; entry in use?
42 beq inval ; no, return error
43 lda fd_table+ft_usa,x ; get usage counter
44 beq ok_notlast ; 0? (shouldn't happen)
46 sbc #1 ; decr usage counter
51 lda fd_table+ft_iocb,x ; get iocb
56 sta fd_table+ft_iocb,y ; clear table entry
57 lda fd_table+ft_flag,y
58 and #16 ; opened by app?
59 eor #16 ; return set Z if yes
66 .endproc ; fdtoiocb_down
68 inval: ldx #$ff ; sets N
72 ; clear iocb except for ICHID field
73 ; expects X to be index to IOCB (0,$10,$20,etc.)
74 ; all registers destroyed
78 inx ; don't clear ICHID
92 ; return ZF = 0/1 for not found/found
94 ; all registers destroyed
109 inx ; return ZF cleared
112 .endproc ; findfreeiocb
115 ; decrements usage counter for fd
116 ; if 0 reached, it's marked as unused
117 ; get fd index in tmp2
118 ; Y register preserved
123 bcs ret ; invalid index, do nothing
130 asl a ; create index into fd table
134 cmp fd_table+ft_iocb,x ; entry in use?
135 beq ret ; no, do nothing
136 lda fd_table+ft_usa,x ; get usage counter
137 beq ret ; 0? should not happen
139 sbc #1 ; decrement by one
140 sta fd_table+ft_usa,x
142 lda #$ff ; 0, table entry unused now
143 sta fd_table+ft_iocb,x ; clear table entry
146 .endproc ; fddecusage
151 ; called from open() function
152 ; finds a fd to use for an open request
153 ; checks whether it's a device or file (file: characters follow the ':')
154 ; files always get an exclusive slot
155 ; for devices it is checked whether the device is already open, and if yes,
156 ; a link to this open device is returned
158 ; Calling parameters:
159 ; tmp3 - length of filename + 1
160 ; AX - points to filename
161 ; Y - iocb to use (if we need a new open)
163 ; tmp2 - fd num ($ff and C=0 in case of error - no free slot)
164 ; C - 0/1 for no open needed/open should be performed
165 ; all registers preserved!
204 ; ptr4 points to filename
214 ; no colon there!? OK, then we use a fresh iocb....
215 ; return error here? no, the subsequent open call should fail
217 do_open_nd: ; do open and don't remember device
222 sta tmp1 ; set flag to return 'open needed' : C = 1
227 cmp fd_table,x ; check ft_iocb field for $ff
228 beq freefnd ; found a free slot
233 cmp #(MAX_FD_VAL*4)+ft_iocb ; end of table reached?
236 ; error: no free slot found
238 stx tmp1 ; return with C = 0
240 stx tmp2 ; iocb: $ff marks error
246 sbc #ft_iocb ; normalize
252 bit tmp1 ; remember device?
254 lda #0 ; no, put 0 in field
258 lda (sp),y ; get device
259 l2: sta fd_table+ft_dev,x ; set device
261 sta fd_table+ft_usa,x ; set usage counter
263 sta fd_table+ft_iocb,x ; set iocb index
265 and #7 ; device number is 3 bits
266 ora #16 ; indicated a fd actively opened by the app
267 sta fd_table+ft_flag,x
269 jsr fdt_to_fdi ; get new index
270 noslot1:bcs noslot ; no one available (noslot1: helper label for branch out of range)
271 ;cmp #$ff ; no one available
272 ;beq noslot ;@@@ cleanup needed
273 sta tmp2 ; return index
276 ; string in "Xn:xxx" format
278 lda (ptr4),y ; get device number
283 sta tmp2 ; save it for speed later here also
284 lda #4 ; max. length if only device + number ("Xn:")
286 bcc do_open_nd ; string is longer -> contains filename
287 bcs check_dev ; handle device only string
289 ; string in "X:xxx" format
290 colon1: lda #3 ; max. length if device only ("X:")
292 bcc do_open_nd ; string is longer -> contains filename
294 ; get device and search it in fd table
297 lda (ptr4),y ; get device id
299 ldx #(MAX_FD_VAL*4) - ft_entrylen
301 cmp fd_table+ft_iocb,x ; is entry valid?
302 beq srch2 ; no, skip this entry
304 cmp fd_table+ft_dev,x
312 ; not found, open new iocb
315 ; found device in table, check device number (e.g R0 - R3)
316 fnddev: lda fd_table+ft_flag,x
318 cmp tmp2 ; contains devnum
319 bne srch2 ; different device numbers
321 ; found existing open iocb with same device
326 inc fd_table+ft_usa,x ; increment usage counter
327 jsr fdt_to_fdi ; get new index
328 bcs noslot1 ; no one available
329 sta tmp2 ; return index
331 ; clean up and go home
343 lsr a ; set C as needed