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
47 sbc #1 ; decr usage counter
52 lda fd_table+ft_iocb,x ; get iocb
58 sta fd_table+ft_iocb,y ; clear table entry
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
91 ; return iocb index in A
92 ; return N bit set for invalid fd
93 ; all registers destroyed
102 asl a ; create index into fd table
106 cmp fd_table+ft_iocb,x ; entry in use?
107 beq inval ; no, return error
108 lda fd_table+ft_usa,x ; get usage counter
109 beq inval ; 0? should not happen
110 lda fd_table+ft_iocb,x ; get iocb
116 ; no entry parameters
117 ; return ZF = 0/1 for not found/found
118 ; index in X if found
119 ; all registers destroyed
134 inx ; return ZF cleared
137 .endproc ; findfreeiocb
139 ; decrements usage counter for fd
140 ; if 0 reached, it's marked as unused
141 ; get fd index in tmp2
142 ; Y register preserved
147 bcs ret ; invalid index, do nothing
154 asl a ; create index into fd table
158 cmp fd_table+ft_iocb,x ; entry in use?
159 beq ret ; no, do nothing
160 lda fd_table+ft_usa,x ; get usage counter
161 beq ret ; 0? should not happen
163 sbc #1 ; decrement by one
164 sta fd_table+ft_usa,x
166 lda #$ff ; 0, table entry unused now
167 sta fd_table+ft_iocb,x ; clear table entry
170 .endproc ; fddecusage
174 ; called from open() function
175 ; finds a fd to use for an open request
176 ; checks whether it's a device or file (file: characters follow the ':')
177 ; files always get an exclusive slot
178 ; for devices it is checked whether the device is already open, and if yes,
179 ; a link to this open device is returned
181 ; Calling parameters:
182 ; tmp3 - length of filename + 1
183 ; AX - points to filename
184 ; Y - iocb to use (if we need a new open)
186 ; tmp2 - fd num ($ff and C=0 in case of error - no free slot)
187 ; C - 0/1 for no open needed/open should be performed
188 ; all registers preserved!
193 ; ptr4 - 3,4 (backup)
231 ; ptr4 points to filename
241 ; no colon there!? OK, then we use a fresh iocb....
242 ; return error here? no, the subsequent open call should fail
244 do_open_nd: ; do open and don't remember device
249 sta tmp1 ; set flag to return 'open needed' : C = 1
255 beq freefnd ; found a free slot
260 cmp #(MAX_FD_VAL*4)+ft_iocb ; end of table reached?
263 ; error: no free slot found
265 stx tmp1 ; return with C = 0
267 stx tmp2 ; iocb: $ff marks error
273 sbc #ft_iocb ; normalize
279 bit tmp1 ; remember device?
281 lda #0 ; no, put 0 in field
285 lda (sp),y ; get device
286 l2: sta fd_table+ft_dev,x ; set device
288 sta fd_table+ft_usa,x ; set usage counter
290 sta fd_table+ft_iocb,x ; set iocb index
293 sta fd_table+ft_flag,x
295 jsr fdt_to_fdi ; get new index
296 bcs noslot ; no one available
297 ;cmp #$ff ; no one available
298 ;beq noslot ;@@@ cleanup needed
299 sta tmp2 ; return index
302 ; string in "Xn:xxx" format
304 lda (ptr4),y ; get device number
309 sta tmp2 ; save it for speed later here also
310 lda #4 ; max. length if only device + number ("Xn:")
312 bcc do_open_nd ; string is longer -> contains filename
313 bcs check_dev ; handle device only string
315 ; string in "X:xxx" format
316 colon1: lda #3 ; max. length if device only ("X:")
318 bcc do_open_nd ; string is longer -> contains filename
320 ; get device and search it in fd table
323 lda (ptr4),y ; get device id
325 ldx #(MAX_FD_VAL*4) - ft_entrylen
327 cmp fd_table+ft_iocb,x ; is entry valid?
328 beq srch2 ; no, skip this entry
330 cmp fd_table+ft_dev,x
338 ; not found, open new iocb
341 ; helper for branch out of range
344 ; found device in table, check device number (e.g R0 - R3)
345 fnddev: lda fd_table+ft_flag,x
347 cmp tmp2 ; contains devnum
348 bne srch2 ; different device numbers
350 ; found existing open iocb with same device
355 inc fd_table+ft_usa,x ; increment usage counter
356 jsr fdt_to_fdi ; get new index
357 bcs noslot1 ; no one available
358 sta tmp2 ; return index
360 ; clean up and go home
372 lsr a ; set C as needed