nolist

read "newrom1.asm"

.erames
call nomen
db 31,24,1,#ff
call setcff
db "E",#bf," ",#ff
ret

.alltag
call fprint
db "all t",#ab,"f",#b4,#ff
ret

.SErase
call clastf
call erames
call cshow
call getyn
pop hl
ret nz
call forone
call writedir
jp newfs

.MErase
call ctagged
call erames
call alltag
call getyn
ret nz
call fortagged
.newfs2
call writedir
jp dosort

.SMove
call clastf
call movmes
call cshow
ld e,#96
call getmuser
pop hl
ret z
call forone
jr newfs2

.MMove
call ctagged
call movmes
call alltag
call getmuser
ret z
call fortagged
jr newfs2

.movmes
call mestop
db "Move ",#ff
ret

.getmuser
call fprint
db #b8,"u",#a7,#ff
ld hl,#2901
ld ix,tuser
.entul
call entuser2
cp #f3
jr z,entul
cp #fc
ret z
ld a,b
or a
ret z
ld hl,tuser
call convuser
ld (destuser),a
ld de,movf
or d
ret

.movf
push hl
ld l,e
call faddr
ld a,(destuser)
ld (hl),a
.popret
pop hl
ret

.getatt
call fprint
db #b8,": ",#fe,"R",#b1,"  read/",#fe,"Write  "
db #fe,"D",#be," ",#fe,"System",#ff
.getatl
call #bb18
call upper
ld de,attf
cp #fc
scf
ret z
ld hl,#8009
cp "R"
ret z
ld h,#0
cp "W"
ret z
inc l
cp "D"
ret z
ld h,#80
cp "S"
jr nz,getatl
ret

.getyn
call cprint
db " - ",#d2,#ff
.getyn2
call getukey
ld de,eraf
cp "Y"
jr z,bb5a
ld a,"N"
.bb5a
jp #bb5a

.Filespec
call nomen
db "U",#a7,#ff
ld hl,unumb
call cprint2
call cprint
db 31,25,1,"F",#b4,"pec",#c0,#ff
.entfile
ld hl,#2401
call enter
cp #f2
jr nz,notf2
ld hl,#1001
call entuser
cp #f3
ld a,b
ld (userl),a
jr z,entfile
.notf2
ld a,(userl)
or a
ld a,1
jr z,setall
dec a
.setall
ld (fspec+1),a
ld hl,unumb
call convuser
ld (fspec+2),a
call convmask
ld a,(fspec)
inc a
ret z
jp newfs

.convuser
ld bc,#300
.numlp
ld a,(hl)
cp " "
jr z,finc
ld a,c
add a
add a
add c
add a
add (hl)
sub 48
ld c,a
inc hl
djnz numlp
.finc
ld a,c
ret

.Rename
call clastf
call mestop
db "R",#bd,#ff
call cshow
ld e,#98
call fprint
db #b8,":",#ff
ld hl,tempf
ld b,12
.cltfl
ld (hl)," "
inc l
djnz cltfl
ld (hl),#ff
ld hl,#1f01
ld ix,tempf
call enter3
cp #fc
jr z,popret2
ld a,b
or a
.popret2
jp z,popret
ld hl,tempf2+1
ld de,tempf
call convmask2
pop hl
push hl
ld l,(hl)
call faddr
ld a,(hl)
ld (tempf2),a
call find1
pop hl
jr nz,dorenit
ld hl,tempf2
call exists
.anykey
call cprint
db " - P",#ba,#ff
jp #bb18
.dorenit
ld de,renf
call forone
jp newfs2

.exists
push hl
call mestop
db "File ",#ff
pop hl
db #dd:ld l,#ff
call pfile
call cprint
db 31,24,1,"exists",#ff
ret

.find1
ld de,tempf2
ld hl,#100
.find2
ld a,(dire)
inc a
ld b,a
.loopff
push bc
ld b,11
call compare2
jr nz,notfile
pop bc
ret
.contfind
push bc
.notfile
ld bc,32
add hl,bc
pop bc
djnz loopff
ret

.renf
push hl
ld l,e
call faddr
inc hl
ld b,11
ld de,tempf2+1
ex de,hl
.renlp
ld a,(de)
and #80
or (hl)
ld (de),a
inc l
inc de
djnz renlp
pop hl
ret

.SUntag
ld de,untagf
jr STag+3

.STag
ld de,tagf
call clastf
call coffset
push hl
call forone
pop hl
ld de,(curadr)
call showf
call showt
pop hl
call cursor3+3
jp godown

.tagf
bit 6,a
ret nz
push hl
or #40
ld (de),a
call getfze
add hl,bc
bit 0,a
jr z,setkt
inc de
.setkt
ld (ftagged),de
ld (ktagged),hl
pop hl
ret

.eraf
ld a,(doit)
or a
ret nz
push hl
ld l,e
call faddr
push hl
pop ix
bit 7,(ix+9)		;Is file read-only?
jr z,notro
ld a,(de)
and 1
jr z,notro
pop hl
push hl
push de
push ix
push hl
call nomen
db 31,20,1,"is r",#b1,". E",#bf,#ff
pop hl
ld de,#c781
call showf3
call getyn
pop hl
pop de
jr z,notro
ld a,1
ld (doit),a
pop hl
ret

.notro
ld (hl),#e5
pop hl
ld a,(de)

.untagf
bit 6,a
ret z
push hl
and #bf
ld (de),a
call getfze
or a
sbc hl,bc
bit 0,a
jr z,setkt
dec de
jr setkt

.getfze
push af
ld l,e
call faddr
push hl
pop ix
call getfsz
ld c,a
ld b,0
ld de,(ftagged)
ld hl,(ktagged)
pop af
ret

.forone
ld (fordo+1),de
.forsin
ld a,(hl)
call findsame
ld e,(hl)		;File offset
ld d,dalloch
xor a
ld (doit),a
ld a,(de)
call fordo		;E=entry #, D=dalloch
.foronel
inc hl
call checkhl2
ret c
ld e,(hl)
ld d,dalloch
ld a,(de)
bit 0,a
ret nz
call fordo
jr foronel

.forall
ld (fordo+1),de
ld hl,sorted
.foralp
call checkhl
ret c
push hl
call forsin
pop hl
inc hl
jr foralp

.fortagged
ld (fordo+1),de
ld hl,sorted
.foratlp
call checkhl
ret c
push hl
ld e,(hl)
ld d,dalloch
ld a,(de)
and #41
cp #41		;Tagged and first entry
call z,forsin
pop hl
inc hl
jr foratlp

.Backup
ret

.Configure
call mestop
db "C",#cf,": ",#fe,"D",#ad,#ce,"  ",#fe,"F",#ae," ",#ce,#ff
.notsetf
call #bb18
call upper
cp #fc
ret z
cp "D"
jp z,confdrvs
cp "F"
jr nz,notsetf
call nomen2
ld de,#c374
ld bc,#1aff
call tline
call hline2
call mline
ld a,2
call mhline
call mline
ld a,2
call mhline
call mline
call hline2
call bline
ld de,#c645
call fprint
db "Ok",#ff
ld a,3
ld (currf),a
.dshfl
call showfen
.dcurslp2
call fdcurs
call #bb18
push af
call fdcurs
pop af
ld b,3
call updown
jr z,dcurslp2
ld c,2
or a
jr z,setelf
ld c,4
dec a
jr z,setelf
ld c,8
.setelf
ld hl,RAM_DPBC
ld a,(hl)
xor c
ld (hl),a
dec l:dec l
ld (hl),a
jr dshfl

.fdcurs
ld bc,#1a01
ld a,(currf)
or a
jr z,isy0
cp 3
jr nz,noty3
ld a,8
jr isy0
.noty3
ld c,a
add a
add c
dec a
ld c,2
.isy0
push bc
call times80
pop bc
ld de,#c3c5
add hl,de
jp cursdisp

.showfen
ld c,#ff
ld de,#c3c5
call fprint
db "ELECTRO F",#ae,"s",#ff
ld a,(RAM_DPBC)
ld h,a
and 2
call endis
ld de,#c465
call fprint
db #c2,"10",-5," ",#ff
bit 2,h
call iendis
ld e,#b5
call fprint
db #c5,"F",#ae,"s",-4," ",#ff
bit 2,h
call endis
ld de,#c555
call fprint
db #c2,"1",-6," ",#ff
bit 3,h
call iendis
ld e,#a5
call fprint
db #d0,-6," ",#ff
bit 3,h
jr endis

.iendis
jr z,endis+2
xor a
.endis
jr z,diabd
call fprint
db #c0,"Enabled ",#ff
ret
.diabd
call fprint
db #c0,"Disabled",#ff
ret

.confdrvs
call nomen2
ld de,#c2d4
ld bc,#15ff
call tline
ld a,4
call mhline
call mline
ld a,4
call mhline
call mline
call hline2
call bline
ld de,#c645
call fprint
db "Ok",#ff
ld a,6
ld (currf),a
.sdrvslp
call shwdrvs
.dcurslp
call ddcurs
call #bb18
push af
call ddcurs
pop af
ld b,6
call updown
jr z,dcurslp

inc a
ld b,a
sub 4
ld e,0
jr c,isddv0
inc a
ld b,a
inc e
.isddv0
ld a,e
call chkdrv0
djnz ns4080
xor 2
bit 3,a
jr z,setvddb
and #0d
jr setvddb
.ns4080
djnz nssides
ld h,a
and #05
ld a,h
jr nz,isdsdd
xor #04
jr setvddb
.isdsdd
xor #01		;Now single sided
and #0b
jr setvddb
.nssides
xor 8
bit 1,a
jr z,setvddb
and #07
.setvddb
and #f
ld c,a
add a:add a:add a:add a
ld b,a
ld hl,RAM_DDB
ld a,(hl)
dec e
jr z,isdrv1
and #f
or b
.setddbb
ld (hl),a
dec l:dec l
ld (hl),a
jr sdrvslp
.isdrv1
and #f0
or c
jr setddbb

.updown
ld hl,currf
cp #fc
jr z,endsud
cp #f0
jr nz,notdscu
ld a,(hl)
or a
ret z
dec (hl)
xor a
ret
.notdscu
cp #f1
jr nz,notdscd
ld a,(hl)
cp b
ret z
inc (hl)
.dcursss
xor a
ret
.notdscd
cp 13
jr nz,dcursss
ld a,(hl)
cp b
ret nz
.endsud
pop hl
jp clmain

.ddcurs
ld a,(currf)
cp 3:jr c,lessy3
inc a:inc a
.lessy3
cp 8:jr nz,lessy6
inc a
.lessy6
call times80
ld bc,#c375
add hl,bc
ld bc,#1501
jp cursdisp

.shwdrvs
ld de,#c325
xor a
call shwdrve
ld de,#c4b5
ld a,1
.shwdrve
ld c,0
push de
push af
call chkdrv0
ld h,a
call fprint
db -6," D",#ad,#ff
pop af
add 65
call fastch
call fprint
db ":",-7," ",#ff
pop de
call dnlne
push de
ld c,#ff
bit 1,h
ld a,"4"
jr z,trk40t
ld a,"8"
.trk40t
call fastch
call fprint
db "0 tracks per side",#ff
pop de
call dnlne
push de
bit 0,h
call sdoub
call fprint
db "le sided ",#ff
bit 0,h
jr nz,isdoub
call fprint
db "(Side ",#ff
ld a,"0"
bit 2,h
jr z,issd0
inc a
.issd0
call fastch
ld a,")"
call fastch
jr isndoub
.isdoub
call fprint
db -8," ",#ff
.isndoub
pop de
call dnlne
bit 3,h
call sdoub
call fprint
db "le-stepping",#ff
ret

.sdoub
jr z,sings
call fprint
db "Doub",#ff
ret
.sings
call fprint
db "Sing",#ff
ret

.notz
or #ff
ret

.break
call #bb09
jr nc,notz
cp #fc
ret nz
call #bb8a
call #bb06
push af
call #bb8d
pop af
cp #fc
ret nz
call nomen
db "Operation aborted",#ff
call anykey
xor a
ret

.Format
ld hl,#bafe
set 0,(hl)
call nomen
db "D",#ad,#cb,"f",#ae,#c0,#ff
call getcdrive2
push af
call nomen
db "Select f",#ae," for D",#ad,#ff
pop af
push af
add 65
call #bb5a
ld a,":"
call #bb5a
pop af
ld e,a
call sdrv
ld e,a
ld ix,formbuff
ld bc,#1600		;Number of formats (22)
ld hl,forms+3
.checkf
ld a,(hl)
inc hl
inc hl
and e
jr z,noput
ld (ix+0),l
ld (ix+1),h
inc c
inc ix
inc ix
.noput
call skip0
inc hl
inc hl
inc hl
djnz checkf
ld a,c
ld (numfr),a
cp 16
jr c,less16
ld a,16
.less16
ld (scrls),a
push af
ld hl,#505
ld de,#1414
call #bb66
ld c,#ff
ld de,#c694
ld b,#90
call bline
pop af
.prflp
call mhline
inc hl
ld (basea),hl
call tline
xor a
ld (basef),a
ld (currf),a
ld c,a
ld a,(scrls)
ld b,a
ld de,(basea)
.shflp
call showfr
djnz shflp
.waitfc
call fcursor
call #bb18
push af
call fcursor
pop af
ld hl,currf
cp #f0
jr nz,notufc
ld a,(hl)
or a
jr z,waitfc
ld a,(basef)
cp (hl)
jr nz,noscruf
dec a
ld (basef),a
ld c,a
dec (hl)
call cprint
db 31,1,1,11,10,#ff
ld de,#c195
jr showfrc
.noscruf
dec (hl)
jr waitfc
.notufc
cp #f1
jr nz,notdfc
ld a,(numfr)
ld b,a
ld a,(hl)
inc a
cp b
jr z,waitfc
ld (hl),a
ld a,(basef)
ld b,a
ld a,(hl)
sub b
cp 16
jr nz,waitfc
ld a,b
inc a
ld (basef),a
call cprint
db 31,1,17,10,#ff
ld de,#c645
ld a,(currf)
ld c,a
.showfrc
call showfr
jr waitfc
.notdfc
cp #fc
jp z,clmain
cp 13
jr nz,waitfc
call clmain
call nomen
db "V",#af," while f",#b0,#ff
call getyn
jr z,isyes
xor a
.isyes
ld (verform),a
call unlog
ld a,(currf)
ld c,a
call getffm
.reform
push hl
call insert
db #b7,"f",#ae,#b8,#ff
pop hl
push hl
call fprint2
call fprint
db " f",#ae,#ff
ld a,(formdrv)
call indrive
jp z,escapef
ld hl,vbuff
ld de,vbuff+1
ld bc,#1ff
ld (hl),#e5
ldir
call nomen
db "F",#ae,#bb,-4," ",#ff
pop hl
push hl
call setform
ld a,(formdrv)
ld c,e
ld e,a
ld a,#f
call #ca63
ld (hl),#ff
inc hl
ld (hl),b		;Number of sectors per track
inc hl
inc hl
ld (hl),c		;Gap length #3
.formloop
push bc
push de
ld a,d
ld de,#c793
call dec3a
pop de
pop bc
call break
jp z,escapef
push bc
push de
ld hl,formbuff
push bc
push hl
ld c,d
.setfbuff
ld a,(#baff)
and 1
jr z,nomod
srl d
jr c,nomod
xor a
.nomod
ld (hl),d
inc hl
ld (hl),a
inc hl
inc hl
inc hl
djnz nomod
pop hl
ld d,c
call #c652	;Format track
pop bc
ld a,(verform)
or a
jr z,nokv
ld hl,#1901
call #bb75
ld hl,vbuff
call vtrack
jr z,okv
call cprint
db 7,"- E",#b9,#ff
jr nokv
.okv
call cprint
db #c1,#ff
.nokv
pop de
pop bc
ld a,d
dec d
or a
jr nz,formloop
call nomen
db "F",#ae,#d3,#ff
call getyn
pop hl
jp z,reform
push hl
.escapef
pop hl
call relog
jp clmain

.vtrack			;E=drive, D=track, B=sectors per track
ld a,(formbuff+2)
ld c,a
.vloop2
call vsect
ret nz
djnz vloop2
ret

.vsect		;Verifies sector C
push bc
push hl
call vsec
pop hl
pop bc
inc c
ld a,(#be4e)
and #3f
cp #08
ret

.vsec
call #c976
ld a,#51
ld b,#11
jp #c66d

.setform	;Set format buffer to format at HL
		;Returns E=gap #3, B=# of sectors, D=last track
		;(obaff) = old DDB, (#baff)=setting for format
		;format setup at formbuff
dec hl
ld b,(hl)	;Number of sectors per track
dec hl
ld c,(hl)	;Format specifier
dec hl
dec hl
dec hl
push bc
res 7,b
ld a,b		;B=8, 9 or 10
add (hl)
ld c,a		;Last sector + 1
ld de,#552	;9 sectors per track, interleave, gap #3
bit 0,b
jr nz,use9
ld e,#10	;10 sectors per track, gap #3
bit 1,b
jr nz,use9
ld de,#150	;8 sectors per track
.use9
ld a,(hl)
ld hl,formbuff
.use9l
inc hl
inc hl
ld (hl),a
inc hl
ld (hl),2
inc hl
add d		;Add interleave
cp c
jr c,noaddi
sub d
sub d
inc a
.noaddi
djnz use9l
pop bc
ld a,#ff
.checddb
inc a
srl c
jr nc,checddb
ld c,a
add a:add a:add a:add a
or c
ld c,a
ld a,(#baff)
ld (obaff),a
and #cc
or c
ld (#baff),a		;Bit 0 = 1 for double sided
ld d,40
bit 7,b
res 7,b
jr nz,use40
dec d
.use40
and 3
ret z
ld d,79
cp 3
ret nz
ld d,159
ret

.insert
call mestop
db "Insert ",#ff
jp fprint

.indrive
push af
call fprint
db " in d",#ad,#ff
pop af
add 65
call fastch
call fprint
db ": then p",#ba,#ff
call #bb18
cp #fc
ret

.clmain
call mainw
db 12,24,#ff
ret

.fcursor
ld a,(basef)
ld b,a
ld a,(currf)
sub b
call times80
ld bc,(basea)
add hl,bc
ld bc,#1001
jp cursdisp

.showfr
push bc
push de
call getffm
ld c,#ff
inc de
call fprint2
pop de
pop bc
inc c
.dnlne
push hl
ld hl,#50
add hl,de
ex de,hl
pop hl
ret

.getffm
ld hl,formbuff
ld b,0
add hl,bc
add hl,bc
ld a,(hl)
inc hl
ld h,(hl)
ld l,a
ret

.Logdisc
call nomen
db "Log Which D",#ad,": ",#ff
call getdrive
call loga
ld a,e
ld (fspec),a
.getdir
call dirinf
call rsects
call setbuff
call getsort
.getfdesc		;Display current format
ld a,(fspec)
ld e,a
.getfdesce
ld a,#2
call #ca63
ld d,(hl)		;BSH, block shift
inc hl
inc hl
inc hl
ld b,(hl)		;DSM, number of blocks - 1
ld a,#d
call #ca63
ld a,(hl)		;OFF, reserved tracks
inc hl
inc hl
ld c,(hl)		;first sector number
ld hl,forms
cp 1
jr nz,notms8
ld hl,ms800f
.notms8
push de
call sdrv
add a:add a:add a:add a:ld e,a
call findf
pop de
jr nc,issame
.noformf
ld hl,unknown
.issame
ld (formdesc),hl
ret

.unlogd
ld a,(formdrv)
ld hl,savedpb+#1d
jr unlog2

.unlog
ld hl,savedpb
ld a,(fspec)
.unlog2
ld e,a
push hl
xor a
call #ca63
pop de
ld bc,#16
ldir
ld hl,formdesc
ld bc,5
ldir
ld hl,#bafe
ldi
ldi
ret

.relogd
ld a,(formdrv)
ld hl,savedpb+#1d
call relog2
jp dirinf2

.relog
ld a,(fspec)
ld hl,savedpb

.relog2
ld e,a
push de
xor a
push hl
call #ca63
ex de,hl
pop hl
ld bc,#16
ldir
ld de,formdesc
ld bc,5
ldir
ld de,#bafe
res 0,(hl)
ldi
ldi
pop de
jp getfdesce

.loga
ld e,a
ld d,0
call getform
ld (sect1),a
inc d
inc d
call getform
ld (sect2),a
inc hl
ld b,(hl)
cp #71
jr nz,nms800
ld a,(sect1)
cp #c1
jr nz,nms800
dec hl		;#f
dec hl		;#e
dec hl		;#d
ld (hl),1
.nms800
ld a,(sect2)
add b
ld (sect3),a
ret

.sdrv
ld a,e
call chkdrv2
and 3
inc a
ld e,a
xor a
scf
.setdv
adc a
dec e
jr nz,setdv
ret

.findf			;Carry set if not found
ld a,(hl)		;Check sector number
or a
scf
ret z
inc hl
cp c
ld a,(hl)		;Check block shift
inc hl
push hl
jr nz,nosame
cp d
jr nz,nosame
ld a,(hl)
inc hl
cp b
jr nz,nosame
ld a,(hl)
inc hl
inc hl
and e
jr z,nosame
inc sp:inc sp
ret
.nosame
pop hl
inc hl:inc hl
call skip0
jr findf

.skip0
ld a,(hl)
inc hl
inc a
ret z
jr skip0

.dirinf
ld a,(fspec)
ld e,a
.dirinf2
push de
ld a,#d
call #ca63
ld a,(hl)
ld (resert),a
ld a,#2
call #ca63
ld c,(hl)	;BSH in C
inc hl
ld a,(hl)
ld (blmsk),a
inc hl
ld a,(hl)
ld (exmsk),a
inc hl
ld e,(hl)	;LSB of DSM
inc hl
ld d,(hl)	;MSB of DSM
inc hl
ld (dsm),de
ld a,(hl)	;DRM
ld (dire),a
inc hl
inc hl
srl a:inc a	;32
and #e0
rra:rra:rra
ld (dirsc),a
ex de,hl	;HL=DSM-1
inc hl
ld a,(de)	;AL0
ld de,0
ld b,8
.allop
add a
sbc hl,de
djnz allop
ld a,c		;HL=number of blocks
sub 3
jr z,nobshft
.bshft
add hl,hl
dec a
jr nz,bshft
.nobshft
ld (totalk),hl
pop de
.getdir2
ld a,(resert)
ld d,a
ld a,(dirsc)
ld b,a
ld a,(sect2)
ld c,a
ld hl,#100
ret

.writedir
ld a,(fspec)
ld e,a
call getdir2
.wsects
ld ix,#c64e
jr rsects+4

.rsects		;B=number of sects, D=drive, E=track, HL=addr, C=sect
ld ix,#c666
ld (rwcal+1),ix
.nxttrk
push bc
push hl
call rwcal
pop hl
pop bc
inc h
inc h
inc c
ld a,(sect3)
cp c
jr nz,notnxt
ld a,(sect2)
ld c,a
inc d
.notnxt
djnz nxttrk
ret

.getsort

ld hl,dalloc		;First clears the directory allocation map
ld a,(dire)
ld b,a
xor a
ld (hl),a
.gsl1
inc l
ld (hl),a
djnz gsl1
ld a,#80
.gsl2
inc l
jr z,lastde
ld (hl),a
jr gsl2

.lastde
ld hl,0
ld (ktagged),hl
ld (ftagged),hl

.dosort
push iy			;Then sorts the directory using a table at sortedx
ld iy,sortedx
ld hl,sorted
.clrsort
ld (hl),h
inc l
jr nz,clrsort
ld h,l
ld (lastf2),hl
.sort2
ld a,(dire)
inc a
ld b,a
ld c,0
.sort3
call checkent
jr nz,checkff
inc c
djnz sort3
pop iy
jp newfs

.checkff
ex de,hl
db #dd:ld l,c
.skipff
inc c
djnz other

db #dd:ld a,l
ld (iy+0),a
ld (lastf2),iy
inc iy
ld l,a
ld h,sortedh
xor a
ld (hl),a
jr sort2

.other
call checkent
jr z,skipff

push bc			;if entry at HL < entry at DE
call compare
pop bc
jr c,checkff		;if (DE) < (HL)
jr skipff

.compare
ld b,12
.compare2
push de
push hl
ex de,hl
ld a,(de)
cp (hl)
jr nz,lless
.compl1
inc l
inc e
ld a,(hl)
and #7f
ld c,a
ld a,(de)
and #7f
cp c
jr nz,lless
djnz compl1
.lless
pop hl
pop de
ret

.SortFiles
ld hl,issort
ld a,(hl)
cpl
ld (hl),a
jp dosort

.newfs
ld hl,0
ld (entries),hl		;Number of used directory entries
ld (files),hl
ld (kused),hl
ld de,dalloc		;Start of directory map
ld ix,#100		;First directory entry

.gmasklp
push ix
ld a,(de)		;bit 5=1 means not matching
cp #80			;1 means checked, first entry
jp z,noentry		;3 means checked, not first entry
			;bit 6 = 1 if tagged.
ld a,(ix+0)
cp #e5
jr nz,incentry
cp (ix+1)
jr z,nofile

ld hl,(files)
ld a,(exmsk)
jr chkmsk

.incentry
ld hl,(entries)
inc hl
ld (entries),hl

call getfsz

ld hl,(kused)
add l
ld l,a
ld a,h
adc 0
ld h,a
ld (kused),hl

ld hl,(files)
inc hl
ld a,c

.chkmsk
cpl
and (ix+12)
ld c,2
jr nz,noincc

ld (files),hl
dec c

.noincc
ld hl,fspec+1
ld a,(hl)		;Check user number ?
inc hl
or a
jr z,ychk
ld a,(ix+0)
cp #e5
jr z,nomatch
jr chky
.ychk
ld a,(ix+0)
cp (hl)
jr nz,nomatch
.chky
ld b,11
inc ix
inc hl
ld a,(hl)
cp "?"
jr z,nocheck
ld a,(ix+0)
and #7f
cp (hl)
jr nz,nomatch
.nocheck
djnz chky+2
jr setix
.nofile
ld a,#82	;Un-used directory entry
jr setix2
.nomatch
set 5,c
.setix
ld a,(de)
and #40		;Preserve tagged flag
or c
.setix2
ld (de),a
.noentry
pop ix
ld bc,#20
add ix,bc
inc e
jp nz,gmasklp

.sort
xor a
ld (firstf),a
ld h,a:ld l,a
ld (lastf),hl
ld (curpos),hl
ld (maxcp),hl

ld hl,sortedx
ld de,sorted

ld a,(issort)
or a
jr z,sort4

ld hl,dalloc
.sort5
ld a,(hl)
and #bf
dec a
jr nz,nxfile4
ld a,l
ld (de),a
ld (lastf),de
inc de
.nxfile4
inc l
ret z
jr sort5

.sort4
call checkhl2
ret c
push hl
ld l,(hl)
ld h,dalloch
ld a,(hl)
and #bf
dec a
jr nz,nxfile3
ld a,l
ld (de),a
ld (lastf),de
inc de
.nxfile3
pop hl
inc hl
jr sort4

.checkent	;Check entry C
ld l,c
ld h,sortedh
ld a,(hl)
or a
ret z
call faddr
ld a,(hl)
cp #e5
ret nz
inc l
ld a,(hl)
dec l
cp #e5		;Unused directory entry?
ret nz
.nowsort
ld l,c
ld h,sortedh
xor a
ld (hl),a
ret

.getfsz		;Calculate size of entry at IX in 1K blocks
		;Value returned in A

ld a,(blmsk)
ld b,a
cpl
ld c,a
ld a,(ix+15)
add b
and c			;Number of #80 blocks
rra:rra:rra
and #1f
ld b,a			;Number of 1K blocks

ld a,(exmsk)
ld c,a
and (ix+12)		;Number of 16K blocks
add a:add a
add a:add a		;Number of 1K blocks
add b
ret

.faddr
ld h,0
add hl,hl	;*2
add hl,hl	;*4
add hl,hl	;*8
add hl,hl	;*16
add hl,hl	;*32
inc h
ret

.getform	;E=drive, D=track
		;Returns A=format
push de
call #c763
call #c56c
ld a,#f
call #ca63
ld a,(hl)
pop de
ret


.DVerify
call nomen
db "D",#ad,#cb,"v",#af,#c0,#ff
call getdrive
ld (formdrv),a
push af
call insert
db #b7,"v",#af,#ff
pop af
call indrive
ret z
call unlog
ld a,(formdrv)
call loga
call getfdesce
call mestop
db "D",#b5,"is ",#ff
ld hl,(formdesc)
ld c,#ff
call fprint2
call fprint
db " f",#ae," - V",#af,#bb,#ff
ld (numadr),de
call mainw
db 12,31,2,2,"E",#b9,"s",#c0,26,11,#37,4,23,#ff
ld hl,(formdesc)
call setform
xor a
ld (verform),a
ld a,(formdrv)
ld e,a
ld a,(formbuff+2)
ld c,a
ld a,d
ld d,0
.verloop
push af
push bc
push de
push de
push bc
ld a,d
ld de,(numadr)
call dec3a
pop bc
pop de
xor a
ld (verror),a
.vloop3
call break
jp z,escape3
push bc
push de
ld hl,vbuff
ld a,2
ld (#be66),a
call #c666	;Read sector
ld a,16
ld (#be66),a
jr c,sectok
ld a,1
ld (verror),a
ld (verform),a
.sectok
pop de
pop bc
jr nc,verrbad
inc c
djnz vloop3
.verrbad
ld de,(numadr)
inc de:inc de:inc de:inc de
ld a,(verror)
or a
ld c,#ff
jr z,okv2
call fprint
db "- E",#b9,#ff
pop hl
push hl
ld l,h
ld h,0
call #cb3a
call cprint
db 7,"  ",#ff
jr nokv2
.okv2
call fprint
db #c1,#ff
.nokv2
pop de
pop bc
pop af
inc d
cp d
jr nc,verloop
ld a,(verform)
or a
jr z,noerrs
call nomen
db "V",#af," Failed",#ff
jr doany
.noerrs
call nomen
db "No e",#b9,"s found",#ff
.doany
call anykey
jr noesc
.escape3
pop de
pop bc
pop af
.noesc
call clmain
jp relog

.MTag
call clastf
ld de,tagf
call forall
pop hl
jp keyloop2

.MUntag
call ctagged
ld de,untagf
jr MTag+6

.backsp
call #bb75	;Locate
push ix
pop hl
call cprint2
dec hl
dec hl
.backspl
ld a,(hl)
cp " "
jr nz,notsp
dec hl
djnz backspl
.notsp
inc hl
ret

.getkey2
call setxy
call getukey
cp #fc
jp z,ret0
ld c,a
ret

.entuser
ld ix,unumb
.entuser2
ld (xypos),hl
ld b,3
call backsp
.prlp2
call getkey2
ld de,prlp2
cp 13
ret z
cp #f3
ret z
push de
cp #80
ret nc
cp 16
jp z,clrfl
cp 127
jp z,delete
ld a,c
cp "0"
ret c
cp "9"+1
ret nc
ld a,b
cp 3
ret z
cp 2
jr nz,tch+1
ld a,(ix+0)
cp "2"
jr z,ok1
jr tch
.ok1		;if first digit is "2"
ld a,(ix+1)
cp "5"
jr z,ok2
jr tch
.ok2		;And second digit is "5"
ld a,c
cp "6"
.tch
ret nc
jp sch

.enter			;Call with H=x offset, L=y offset
			;Carry not set if escape pressed
ld ix,fspec2
.enter2
db #3e
.enter3
xor a
ld (alloww),a
ld (xypos),hl
ld b,12
call backsp
.prlp
call getkey2
ld de,illegal
.chnc
ld a,(de)
inc de
or a
jr z,osch
cp c
jr nz,chnc
jr prlp
.osch
ld de,prlp
ld a,c
cp 13
ret z
cp #f2
ret z
push de
cp #80
ret nc
ld a,(alloww)
or a
ld a,c
jr nz,awild
cp "*"
ret z
cp "?"
ret z
.awild
ld d,h
ld e,l
cp 16
jr z,clrfl
cp 127
jr z,delete
ld a,b
cp 12
ret z
ld a,c
cp "."
jr z,dot
ld a,b
or a
jr z,fstch
dec hl
ld a,(hl)
inc hl
cp "*"
ret z
call isdot
jr nc,isdot2
ld a,b
cp 8
ret nc
jr fstch
.isdot2
sub b
neg
cp 3
ret nc
.fstch
ld a,c
cp 33
ret c
jr sch

.clrfl
ld a,b
or a
ret z
ld c,32
dec hl
ld (hl),c
dec b
ld a,b
call shpc
jr clrfl

.delete
ld a,b
or a
ret z
ld c,32
dec hl
ld (hl),c
dec b
ld a,b
jr pch

.dot
call isdot
ret nc
.sch
ld (hl),c
inc hl
ld a,b
inc b
.pch
.shpc
call setxy+1
ld a,c
jp #bb5a

.setxy
ld a,b
push hl
ld hl,(xypos)
add h
ld h,a
call #bb75
pop hl
ret

.convmask
ld hl,fspec+3
ld de,fspec2

.convmask2	;DE=source, HL=dest
ld bc,#80c
call bit
ld b,3
ld a,(de)
inc de
cp "."
jr nz,fillsp+1
.bit
ld a,c
or a
ret z
ld a,(de)
inc de
dec c
cp "*"
jr z,fillq
cp "."
jr z,fillsp
call upper
ld (hl),a
inc hl
djnz bit
ret
.fillsp
dec de
ld a," "
jr fillq+2
.fillq
ld a,"?"
ld (hl),a
inc hl
djnz fillq+2
ret

.illegal
db "%()_=|[];:<>,/\"
.moreil
db "%%",0

.isdot
ld a,b
or a
ret z
push bc
.idlp
dec de
ld a,(de)
cp "."
ld a,b
jr z,pbc
djnz idlp
scf
.pbc
pop bc
ret

.initcb
ld hl,(crsect)
inc h:inc h
ld (ddir),hl
ld bc,#2000
add hl,bc
jr setbuff2

.setbuff
ld (crsect),hl
.setbuff2
ld a,#80
ld (bufstbk),a
ld (bufendbk),a
xor a
ld l,a
ld (bufstrt),hl
ld (bufend),hl
dec h
ld e,h
ld d,a
ld hl,(buftot+1)
sbc hl,de
ld (buffree+1),hl
ld (buffree),a
ld de,#c728
call deck
.showfree
ld de,#c6d8
ld hl,(buffree+1)
jp deck

.initfs
db #ff,1,0,"???????????"

db "*.*         ",#ff

db "   ",#ff

db "   ",#ff

dw 0

db #c3,0,0
db #c3

.ramlist2
db #c0,#c4,#c5,#c6,#c7,#cc,#cd,#ce,#cf,#d4,#d5,#d6,#d7
db #dc,#dd,#de,#df,#e4,#e5,#e6,#e7,#ec,#ed,#ee,#ef
db #f4,#f5,#f6,#f7,#fc,#fd,#fe,#ff,0

.unknown
db "UNKNOWN",#ff

.g10 equ 14
.g9 equ 82
.g8 equ 80


;Use MS800 format block if OFF=1

.ms800f
db #71,#04,#c7,#fc,0,"MS800",#ff	;MS800 format

.forms					;first sect, BSH, LSB of DSM
					;form enable, sects

db #91,#04,#c7,#fc,10,#c6,"80",#ff	;1. PARADOS 80
db #81,#03,#cc,#ff,#8a,#c6,"41",#ff	;2. PARADOS 41
db #a1,#04,#c7,#fa,10,#c6,"40D",#ff	;3. PARADOS 40D
db #01,#04,#67,#f8,9,#c2,"1",#ff	;4. ROMDOS D1
db #21,#04,#67,#f8,9,#c2,"2",#ff	;5. ROMDOS D2
db #11,#04,#8f,#f8,10,#c2,"10",#ff	;6. ROMDOS D10
db #31,#04,#8f,#f8,10,#c2,"20",#ff	;7. ROMDOS D20
db #51,#04,#c7,#fa,10,#c2,"40",#ff	;8. ROMDOS D40
db #71,#04,#c7,#fc,10,"S-DOS",#ff	;9. S-DOS (ROMDOS D80)
db #c1,#03,#b3,#ff,9,#c4,#c7,#ff	;10. DATA (SS 40)
db #c1,#04,#b3,#2a,9,#c4,#c8,#ff	;11. DATA (DS 40)
db #c1,#04,#b3,#4c,9,#c4,#c9,#ff	;12. DATA (SS 80)
db #c1,#05,#b3,#f8,9,#c4,#ca,#ff	;13. DATA (DS 80)
db #41,#03,#aa,#ff,9,#c3,#c7,#ff	;14. SYSTEM (SS 40)
db #41,#04,#ae,#2a,9,#c3,#c8,#ff	;15. SYSTEM (DS 40)
db #41,#04,#ae,#4c,9,#c3,#c9,#ff	;16. SYSTEM (SS 80)
db #41,#05,#b0,#f8,9,#c3,#ca,#ff	;17. SYSTEM (DS 80)
db #01,#03,#9b,#ff,8,#c5,#c7,#ff	;18. IBM (SS 40)
db #01,#04,#9d,#2a,8,#c5,#c8,#ff	;19. IBM (DS 40)
db #01,#04,#9d,#4c,8,#c5,#c9,#ff	;20. IBM (SS 80)
db #01,#05,#9e,#f8,8,#c5,#ca,#ff	;21. IBM (DS 80)
db #10,#03,#cc,#ff,#8a,#d0,#ff		;22. ULTRAFORM
;db #01,#ff,#ff,#ff,#c6,#ff		;23. PCW format ???????????
db 0

.endall

.len2 equ $-setdrv

org routines+len2

.total equ $-#4000
