2000-05-28 13:40:48 +00:00
|
|
|
;
|
|
|
|
; Christian Groessler, May-2000
|
|
|
|
;
|
|
|
|
; fd indirection table & helper functions
|
|
|
|
;
|
|
|
|
|
|
|
|
.include "atari.inc"
|
2000-10-29 00:05:06 +00:00
|
|
|
.include "fd.inc"
|
2000-05-30 22:28:30 +00:00
|
|
|
.importzp tmp1,tmp2,tmp3,ptr4,sp
|
2000-10-29 00:05:06 +00:00
|
|
|
.import fd_table,fd_index
|
|
|
|
.import fdt_to_fdi
|
2000-05-28 13:40:48 +00:00
|
|
|
.export fdtoiocb
|
|
|
|
.export fdtoiocb_down
|
2000-05-28 21:53:09 +00:00
|
|
|
.export fddecusage
|
|
|
|
.export newfd
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
.code
|
|
|
|
|
|
|
|
; gets fd in ax, decrements usage counter
|
|
|
|
; return iocb index in X
|
|
|
|
; return N bit set for invalid fd
|
|
|
|
; return Z bit set if last user
|
|
|
|
; all registers destroyed
|
|
|
|
.proc fdtoiocb_down
|
|
|
|
|
|
|
|
cpx #0
|
|
|
|
bne inval
|
2000-05-30 22:28:30 +00:00
|
|
|
cmp #MAX_FD_INDEX
|
2000-05-28 13:40:48 +00:00
|
|
|
bcs inval
|
2000-05-30 22:28:30 +00:00
|
|
|
tax
|
|
|
|
lda fd_index,x ; get index
|
|
|
|
tay
|
|
|
|
lda #$ff
|
|
|
|
sta fd_index,x ; clear entry
|
|
|
|
tya
|
2000-05-28 13:40:48 +00:00
|
|
|
asl a ; create index into fd table
|
|
|
|
asl a
|
|
|
|
tax
|
|
|
|
lda #$ff
|
|
|
|
cmp fd_table+ft_iocb,x ; entry in use?
|
|
|
|
beq inval ; no, return error
|
|
|
|
lda fd_table+ft_usa,x ; get usage counter
|
|
|
|
beq ok_notlast ; 0?
|
|
|
|
sec
|
|
|
|
sbc #1 ; decr usage counter
|
|
|
|
sta fd_table+ft_usa,x
|
|
|
|
retiocb:php
|
|
|
|
txa
|
|
|
|
tay
|
|
|
|
lda fd_table+ft_iocb,x ; get iocb
|
|
|
|
tax
|
|
|
|
plp
|
|
|
|
bne cont
|
|
|
|
php
|
|
|
|
lda #$ff
|
|
|
|
sta fd_table+ft_iocb,y ; clear table entry
|
|
|
|
plp
|
|
|
|
cont: rts
|
|
|
|
|
|
|
|
ok_notlast:
|
|
|
|
lda #1 ; clears Z
|
|
|
|
jmp retiocb
|
|
|
|
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
inval: ldx #$ff ; sets N
|
|
|
|
rts
|
|
|
|
|
|
|
|
|
|
|
|
; gets fd in ax
|
|
|
|
; return iocb index in X
|
|
|
|
; return N bit set for invalid fd
|
|
|
|
; all registers destroyed
|
|
|
|
.proc fdtoiocb
|
|
|
|
|
|
|
|
cpx #0
|
|
|
|
bne inval
|
2000-05-30 22:28:30 +00:00
|
|
|
cmp #MAX_FD_INDEX
|
2000-05-28 13:40:48 +00:00
|
|
|
bcs inval
|
2000-05-30 22:28:30 +00:00
|
|
|
tax
|
|
|
|
lda fd_index,x
|
2000-05-28 13:40:48 +00:00
|
|
|
asl a ; create index into fd table
|
|
|
|
asl a
|
|
|
|
tax
|
|
|
|
lda #$ff
|
|
|
|
cmp fd_table+ft_iocb,x ; entry in use?
|
|
|
|
beq inval ; no, return error
|
|
|
|
lda fd_table+ft_usa,x ; get usage counter
|
|
|
|
beq inval ; 0? should not happen
|
|
|
|
lda fd_table+ft_iocb,x ; get iocb
|
|
|
|
rts
|
|
|
|
|
|
|
|
.endproc
|
2000-05-28 21:53:09 +00:00
|
|
|
|
|
|
|
; decrements usage counter for fd
|
|
|
|
; if 0 reached, it's marked as unused
|
|
|
|
; get fd index in tmp2
|
|
|
|
; Y register preserved
|
|
|
|
.proc fddecusage
|
|
|
|
|
|
|
|
lda tmp2 ; get fd
|
2000-05-30 22:28:30 +00:00
|
|
|
cmp #MAX_FD_INDEX
|
2000-05-28 21:53:09 +00:00
|
|
|
bcs ret ; invalid index, do nothing
|
2000-05-30 22:28:30 +00:00
|
|
|
tax
|
|
|
|
lda fd_index,x
|
|
|
|
pha
|
|
|
|
lda #$ff
|
|
|
|
sta fd_index,x
|
|
|
|
pla
|
2000-05-28 21:53:09 +00:00
|
|
|
asl a ; create index into fd table
|
|
|
|
asl a
|
|
|
|
tax
|
|
|
|
lda #$ff
|
|
|
|
cmp fd_table+ft_iocb,x ; entry in use?
|
|
|
|
beq ret ; no, do nothing
|
|
|
|
lda fd_table+ft_usa,x ; get usage counter
|
|
|
|
beq ret ; 0? should not happen
|
|
|
|
sec
|
|
|
|
sbc #1 ; decrement by one
|
|
|
|
sta fd_table+ft_usa,x
|
|
|
|
bne ret ; not 0
|
|
|
|
lda #$ff ; 0, table entry unused now
|
2000-05-28 21:54:19 +00:00
|
|
|
sta fd_table+ft_iocb,x ; clear table entry
|
2000-05-28 21:53:09 +00:00
|
|
|
ret: rts
|
|
|
|
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
; newfd
|
|
|
|
;
|
|
|
|
; called from open() function
|
|
|
|
; finds a fd to use for an open request
|
2000-05-30 22:28:30 +00:00
|
|
|
; checks whether it's a device or file (file: characters follow the ':')
|
2000-05-28 21:53:09 +00:00
|
|
|
; files always get an exclusive slot
|
|
|
|
; for devices it is checked whether the device is already open, and if yes,
|
|
|
|
; a link to this open device is returned
|
|
|
|
;
|
2000-05-30 22:28:30 +00:00
|
|
|
; Calling parameters:
|
|
|
|
; tmp3 - length of filename + 1
|
2000-05-28 21:53:09 +00:00
|
|
|
; AX - points to filename
|
|
|
|
; Y - iocb to use (if we need a new open)
|
|
|
|
; Return parameters:
|
2000-05-30 22:28:30 +00:00
|
|
|
; tmp2 - fd num ($ff and C=0 in case of error - no free slot)
|
2000-05-28 21:53:09 +00:00
|
|
|
; C - 0/1 for no open needed/open should be performed
|
|
|
|
; all registers preserved!
|
|
|
|
|
|
|
|
; local variables:
|
|
|
|
; AX - 0 (A-0,X-1)
|
|
|
|
; Y - 2
|
|
|
|
; ptr4 - 3,4 (backup)
|
|
|
|
; devnum - 5
|
|
|
|
|
2000-06-12 00:16:21 +00:00
|
|
|
.bss
|
2000-06-11 23:21:30 +00:00
|
|
|
loc_Y: .res 1
|
|
|
|
loc_ptr4_l: .res 1
|
|
|
|
loc_ptr4_h: .res 1
|
|
|
|
loc_tmp1: .res 1
|
|
|
|
loc_devnum: .res 1
|
|
|
|
loc_size: .res 1
|
|
|
|
|
|
|
|
.code
|
2000-05-28 21:53:09 +00:00
|
|
|
|
|
|
|
.proc newfd
|
|
|
|
|
|
|
|
pha
|
|
|
|
txa
|
|
|
|
pha
|
|
|
|
tya
|
|
|
|
pha
|
|
|
|
|
2000-06-11 23:21:30 +00:00
|
|
|
ldx #0
|
|
|
|
stx loc_devnum
|
2000-05-30 22:28:30 +00:00
|
|
|
lda tmp1
|
2000-06-11 23:21:30 +00:00
|
|
|
sta loc_tmp1
|
|
|
|
stx tmp1 ; init tmp1
|
|
|
|
stx tmp2 ; init tmp2
|
2000-05-28 21:53:09 +00:00
|
|
|
lda ptr4+1
|
2000-06-11 23:21:30 +00:00
|
|
|
sta loc_ptr4_h
|
2000-05-28 21:53:09 +00:00
|
|
|
lda ptr4
|
2000-06-11 23:21:30 +00:00
|
|
|
sta loc_ptr4_l
|
2000-05-28 21:53:09 +00:00
|
|
|
pla
|
2000-06-11 23:21:30 +00:00
|
|
|
sta loc_Y
|
2000-05-28 21:53:09 +00:00
|
|
|
pla
|
|
|
|
sta ptr4+1
|
|
|
|
pla
|
|
|
|
sta ptr4
|
|
|
|
|
|
|
|
; ptr4 points to filename
|
|
|
|
|
|
|
|
ldy #1
|
|
|
|
lda #':'
|
|
|
|
cmp (ptr4),y ; "X:"
|
|
|
|
beq colon1
|
|
|
|
iny
|
|
|
|
cmp (ptr4),y ; "Xn:"
|
|
|
|
beq colon2
|
|
|
|
|
|
|
|
; no colon there!? OK, then we use a fresh iocb....
|
2000-05-30 22:28:30 +00:00
|
|
|
; return error here? no, the subsequent open call should fail
|
2000-05-28 21:53:09 +00:00
|
|
|
|
2000-05-30 22:28:30 +00:00
|
|
|
do_open_nd: ; do open and don't remember device
|
|
|
|
lda #2
|
|
|
|
sta tmp1
|
|
|
|
do_open:lda tmp1
|
|
|
|
ora #1
|
|
|
|
sta tmp1 ; set flag to return 'open needed' : C = 1
|
|
|
|
ldx #ft_iocb
|
|
|
|
ldy #$ff
|
|
|
|
srchfree:
|
|
|
|
tya
|
|
|
|
cmp fd_table,x
|
|
|
|
beq freefnd ; found a free slot
|
|
|
|
txa
|
|
|
|
clc
|
|
|
|
adc #ft_entrylen
|
|
|
|
tax
|
|
|
|
cmp #(MAX_FD_VAL*4)+ft_iocb ; end of table reached?
|
|
|
|
bcc srchfree
|
|
|
|
|
|
|
|
; error: no free slot found
|
|
|
|
noslot: ldx #0
|
|
|
|
stx tmp1 ; return with C = 0
|
|
|
|
dex
|
|
|
|
stx tmp2 ; iocb: $ff marks error
|
|
|
|
jmp finish
|
|
|
|
|
|
|
|
; found a free slot
|
|
|
|
freefnd:txa
|
|
|
|
sec
|
|
|
|
sbc #ft_iocb ; normalize
|
|
|
|
tax
|
|
|
|
lsr a
|
|
|
|
lsr a
|
|
|
|
sta tmp2 ; return fd
|
|
|
|
lda #2
|
|
|
|
bit tmp1 ; remember device?
|
|
|
|
beq l1 ; yes
|
|
|
|
lda #0 ; no, put 0 in field
|
|
|
|
beq l2
|
2000-05-28 21:53:09 +00:00
|
|
|
|
2000-05-30 22:28:30 +00:00
|
|
|
l1: ldy #0
|
|
|
|
lda (sp),y ; get device
|
|
|
|
l2: sta fd_table+ft_dev,x ; set device
|
|
|
|
lda #1
|
|
|
|
sta fd_table+ft_usa,x ; set usage counter
|
2000-06-11 23:21:30 +00:00
|
|
|
lda loc_Y
|
2000-05-30 22:28:30 +00:00
|
|
|
sta fd_table+ft_iocb,x ; set iocb index
|
2000-06-11 23:21:30 +00:00
|
|
|
lda loc_devnum
|
2000-05-30 22:28:30 +00:00
|
|
|
and #7 ; only 3 bits
|
|
|
|
sta fd_table+ft_flag,x
|
|
|
|
lda tmp2
|
|
|
|
jsr fdt_to_fdi ; get new index
|
|
|
|
bcs noslot ; no one available
|
|
|
|
;cmp #$ff ; no one available
|
|
|
|
;beq noslot ;@@@ cleanup needed
|
|
|
|
sta tmp2 ; return index
|
|
|
|
jmp finish
|
|
|
|
|
|
|
|
; string in "Xn:xxx" format
|
2000-05-28 21:53:09 +00:00
|
|
|
colon2: dey
|
|
|
|
lda (ptr4),y ; get device number
|
|
|
|
sec
|
|
|
|
sbc #'0'
|
2000-05-30 22:28:30 +00:00
|
|
|
and #7
|
2000-06-11 23:21:30 +00:00
|
|
|
sta loc_devnum
|
2000-05-30 22:28:30 +00:00
|
|
|
sta tmp2 ; save it for speed later here also
|
|
|
|
lda #4 ; max. length if only device + number ("Xn:")
|
|
|
|
cmp tmp3
|
|
|
|
bcc do_open_nd ; string is longer -> contains filename
|
|
|
|
bcs check_dev ; handle device only string
|
|
|
|
|
|
|
|
; string in "X:xxx" format
|
|
|
|
colon1: lda #3 ; max. length if device only ("X:")
|
|
|
|
cmp tmp3
|
|
|
|
bcc do_open_nd ; string is longer -> contains filename
|
|
|
|
|
|
|
|
; get device and search it in fd table
|
|
|
|
check_dev:
|
|
|
|
ldy #0
|
|
|
|
lda (ptr4),y ; get device id
|
|
|
|
tay
|
|
|
|
ldx #(MAX_FD_VAL*4) - ft_entrylen
|
|
|
|
srchdev:lda #$ff
|
|
|
|
cmp fd_table+ft_iocb,x ; is entry valid?
|
|
|
|
beq srch2 ; no, skip this entry
|
|
|
|
tya
|
|
|
|
cmp fd_table+ft_dev,x
|
|
|
|
beq fnddev
|
|
|
|
srch2: txa
|
|
|
|
sec
|
|
|
|
sbc #ft_entrylen+1
|
|
|
|
tax
|
|
|
|
bpl srchdev
|
|
|
|
|
|
|
|
; not found, open new iocb
|
|
|
|
jmp do_open
|
|
|
|
|
|
|
|
; helper for branch out of range
|
|
|
|
noslot1:jmp noslot
|
|
|
|
|
|
|
|
; found device in table, check device number (e.g R0 - R3)
|
|
|
|
fnddev: lda fd_table+ft_flag,x
|
|
|
|
and #7
|
|
|
|
cmp tmp2 ; contains devnum
|
|
|
|
bne srch2 ; different device numbers
|
|
|
|
|
|
|
|
; found existing open iocb with same device
|
|
|
|
txa
|
|
|
|
lsr a
|
|
|
|
lsr a
|
|
|
|
sta tmp2
|
|
|
|
inc fd_table+ft_usa,x ; increment usage counter
|
|
|
|
jsr fdt_to_fdi ; get new index
|
|
|
|
bcs noslot1 ; no one available
|
|
|
|
sta tmp2 ; return index
|
|
|
|
|
|
|
|
; clean up and go home
|
|
|
|
finish: lda ptr4
|
|
|
|
pha
|
|
|
|
lda ptr4+1
|
|
|
|
pha
|
2000-06-11 23:21:30 +00:00
|
|
|
lda loc_Y
|
2000-05-30 22:28:30 +00:00
|
|
|
pha
|
|
|
|
lda tmp1
|
|
|
|
pha
|
2000-06-11 23:21:30 +00:00
|
|
|
lda loc_tmp1
|
2000-05-30 22:28:30 +00:00
|
|
|
sta tmp1
|
|
|
|
pla
|
|
|
|
lsr a ; set C as needed
|
|
|
|
|
2000-05-28 21:53:09 +00:00
|
|
|
pla
|
|
|
|
tay
|
2000-05-30 22:28:30 +00:00
|
|
|
pla
|
|
|
|
tax
|
|
|
|
pla
|
|
|
|
rts
|
2000-05-28 21:53:09 +00:00
|
|
|
|
|
|
|
.endproc
|
|
|
|
|