NEW
  AUTO 3,1
*--------------------------------------
BS.KW.BSAVE		bcc	LAE2D

				lda GP.FoundBITS+1
				and	#CMDBITS1.A+CMDBITS1.E+CMDBITS1.L
				cmp	#CMDBITS1.A+CMDBITS1.L

XAE00			bcc	LAE31

				lda GP.ParamA
				sta	GP.CRAUXTYPE
				sta	GP.FIAUXTYPE

				lda GP.ParamA+1
				sta	GP.CRAUXTYPE+1
				sta	GP.FIAUXTYPE+1

				lda #$06
				sta	GP.ParamT
				sta	GP.FITYPE

				lda GP.FoundBITS
				and	#CMDBITS0.T
				bne	LAE31

				jsr	BS.CreateFileOrDir
				bcs	LAE33

				jsr	BS.GetFileInfo
				bcs	LAE33

LAE2D			lda #$02
				bne LAE44

LAE31			lda #$06

LAE33			sec
				rts
*--------------------------------------
BS.KW.BRUN		jsr	BS.KW.BLOAD
				bcs	LAE33

				jsr	LAE3F
				clc
				rts

LAE3F			jmp	(GP.MLIRW.DATAPTR)
*--------------------------------------
BS.KW.BLOAD		lda #$01
LAE44			ldx	#$06
				jsr	BS.CheckFTypeXAccessA
				bcs	LAE33

				ldx	GP.ParamA
				ldy	GP.ParamA+1
				lda GP.FoundBITS+1
				bmi	LAE66

				ldx	GP.FIAUXTYPE
				ldy	GP.FIAUXTYPE+1
				lda GP.FoundBITS
				and	#CMDBITS0.T
				bne	LAEAB

				lda GP.FoundBITS+1

LAE66			stx	GP.MLIRW.DATAPTR
				sty	GP.MLIRW.DATAPTR+1
				bit	GP.FoundBITS+1
				bvs	LAE80

				pha
				lda GP.FITYPE
				cmp	#$06
				bne	LAE7F

				stx	GP.FIAUXTYPE
				sty	GP.FIAUXTYPE+1

LAE7F			pla
LAE80			ldx	GP.ParamL
				ldy	GP.ParamL+1
				and	#$30
				beq	LAEAF

				eor	#$30
				beq	LAEAB

				and	#$10
				beq	LAEC5

				lda GP.ParamE
				sec
				sbc	GP.ParamA
				tax
				lda GP.ParamE+1
				sbc	GP.ParamA+1
				tay
				inx
				bne	LAEA5

				iny

LAEA5			bcs	LAEC5

				lda #BS.E.RANGE
				.HS 2C					BIT ABS

LAEAB			lda #BS.E.INVOPT
				sec
				rts

LAEAF			jsr	BS.MLIGetEOF
				bcs	LAEC3

				ldx	GP.SBUFADR
				ldy	GP.SBUFADR+1
				lda GP.SBUFADR+2
				beq	LAEC5

				lda #BS.E.TOOLARGE
LAEC3			sec
				rts

LAEC5			stx	GP.MLIRW.COUNT
				sty	GP.MLIRW.COUNT+1

				lda GP.FoundBITS+1
				and	#CMDBITS1.B
				beq	LAEF7

				ldx	#$02

LAED4			lda GP.ParamB,x
				sta	GP.SBUFADR,x
				dex
				bpl	LAED4

LAEDD			jsr BS.MLISetMark
				ldx	GP.XCNUM
				bcc	LAEF7

				cmp	#$02
				bne	LAEC3

				cpx	#$0F
				bne	LAEC3

				jsr BS.MLISetEOF
				bcc	LAEDD
				rts

LAEF7			ldx	GP.XCNUM
				lda #MLI.C.READ
				cpx	#$0F
				bne	LAF24

				bit	GP.FoundBITS+1
				bvs	LAF22

				lda GP.FoundBITS+1
				and	#CMDBITS1.E+CMDBITS1.L
				beq	LAF22

				ldx	GP.MLIRW.COUNT
				ldy	GP.MLIRW.COUNT+1
				jsr BS.SetSBUFADRXY0
				jsr BS.MLISetEOF

LAF22			lda #MLI.C.WRITE

LAF24			jsr	GP.GOSYSTEM
				bcs	LAF38

				bit	GP.FoundBITS+1
				bvs	LAF35

				cpx	#$0F
				bne	LAF35

				jsr	BS.SetFileInfoNoDate

LAF35			jmp	BS.MLIClose

LAF38			rts
*--------------------------------------
BS.KW.STORE		bcc	LAF4E

				lda #$FD
				sta	GP.ParamT
				sta	GP.FITYPE
				lda #$C3
				sta	GP.FIACCESS
				jsr	BS.CreateFileOrDir
				bcs	LAF9C

LAF4E			jsr	LA411
				bcs	LAF9C

				ldx	#$FD
				lda #$02
				jsr	BS.CheckFTypeXAccessA
				bcs	LAF95

				jsr	BS.SetupRWBASHDR
				jsr	BS.WriteXYBytes
				bcs	LAF95

				lda XBBE9
				sta	GP.MLIRW.DATAPTR
				sta	GP.FIAUXTYPE
				lda XBBEA
				sta	GP.MLIRW.DATAPTR+1
				sta	GP.FIAUXTYPE+1
				ldy	XBBEC
				ldx	XBBEB
				jsr	BS.WriteXYBytes
				bcs	LAF95

				jsr BS.MLIGetMark

				jsr BS.MLISetEOF
				bcs	LAF95

				jsr	BS.SetFileInfoNoDate
				bcs	LAF95

LAF92			jsr	BS.MLIClose

LAF95			php
				pha
				jsr	LA480
				pla
				plp
LAF9C			rts
*--------------------------------------
BS.SetupRWBASHDR
				lda #BS.BAS.HDR
				sta	GP.MLIRW.DATAPTR
				lda /BS.BAS.HDR
				sta	GP.MLIRW.DATAPTR+1
				ldy	#$00
				ldx	#$05
				rts
*--------------------------------------
BS.KW.RESTORE	ldx	#$FD
				lda #$01
				jsr	BS.CheckFTypeXAccessA
				bcs	LAF9C

				jsr	BS.SetupRWBASHDR
				jsr	BS.ReadXYBytes
				bcs	LAF9C

				lda GP.FIAUXTYPE
				sta	XBBE9
				sta	GP.MLIRW.DATAPTR
				lda AS.HIMEM+1
				sec
				sbc	BS.BAS.HDR+4
				clc
				adc	GP.FIAUXTYPE+1
				sta	GP.MLIRW.DATAPTR+1
				sta	XBBEA
				cmp	AS.PGRMEND+1
				bcc	LAFF3

				beq	LAFF3

				lda #$00
				sbc	XBBE9
				tax
				lda AS.HIMEM+1
				sbc	XBBEA
				tay
				jsr	BS.ReadXYBytes
				bcs	LAF9C

				bra LAF92

LAFF3			lda #BS.E.TOOLARGE
				sec
				rts
*--------------------------------------
BS.ReadXYBytes	clc
				.HS B0					BCS
BS.WriteXYBytes	sec
				sty	GP.MLIRW.COUNT+1
				stx	GP.MLIRW.COUNT
				bcc BS.MLIRead

BS.MLIWrite		lda #MLI.C.WRITE
				.HS 2C					BIT ABS
BS.MLIRead		lda #MLI.C.READ
				.HS 2C					BIT ABS
BS.MLIClose		lda #MLI.C.CLOSE
				.HS 2C					BIT ABS
BS.MLIGetEOF	lda #MLI.C.GETEOF
				.HS 2C					BIT ABS
BS.MLISetEOF	lda #MLI.C.SETEOF
				.HS 2C					BIT ABS
BS.MLIGetMark	lda #MLI.C.GETMARK
				.HS 2C					BIT ABS
BS.MLISetMark	lda #MLI.C.SETMARK
				jmp	GP.GOSYSTEM
*--------------------------------------
BS.KW.PR		lda #$00
				tax
				beq	LB018
*--------------------------------------
BS.KW.IN		ldx #$02
				lda #$08

LB018			stx	BS.GetCharSep

				ora	GP.ParamINPR
				asl
				tax
				lda GP.FoundBITS
				and	#CMDBITS0.SLOT
				beq	LB035

				lda GP.FoundBITS+1
				bmi	LB04A

				lda GP.OUTVECT0,x
				sta	GP.ParamA
				lda GP.OUTVECT0+1,x
				sta	GP.ParamA+1

LB035			jsr	LB05C
				bcs	LB049

				ldy	BS.GetCharSep
				lda GP.ParamA
				sta	ZP.CSW,y
				lda GP.ParamA+1
				sta	ZP.CSW+1,y
LB049			rts

LB04A			jsr	LB05C
				bcs	LB05B

				lda GP.ParamA+1
				sta	GP.OUTVECT0+1,x
				lda GP.ParamA
				sta	GP.OUTVECT0,x
LB05B			rts
*--------------------------------------
LB05C			lda GP.ParamA
				sta	ZP.PCL
				lda GP.ParamA+1
				sta	ZP.PCH
				ldy	#$00
				cmp	#$C0
				bcc	LB081

				lda $CFFF
				sty	ZP.A1L
				lda (ZP.PCL),y
				cmp	#$FF
				beq	LB087

LB077			cmp	(ZP.PCL),y
				bne	LB087

				dec	ZP.A1L
				bne	LB077

LB07F			clc
				rts

LB081			lda (ZP.PCL),y
				cmp	#$D8
				beq	LB07F

LB087			lda #BS.E.NODEV
				sec
				rts
*--------------------------------------
BS.KW.BYE		jsr	BS.CloseAll
				jsr	LB36B
				jsr BS.RAMReset
*			lda #$65
				jsr	MLI
				.DA #MLI.C.QUIT
				.DA	GP.MLIRW
*--------------------------------------
BS.QUIT.IIgs	jsr BS.RAMReset
				jsr	MLI
				.DA #MLI.C.QUIT
				.DA .1

.1				.DA #4
				.DA #$EE
				.DA MLI.PATHBUF
				.DA #0
				.DA 0
*--------------------------------------
BS.RAMReset		bit GP.CISPARE1
				bpl .8
			
				php
				sei

				sta IO.SETWRITEAUX

				lda #$FF
				sta AUX.RAMSIG
				dec						$FE
				sta AUX.RAMSIG+1		Set $E000-$FEFF as BLOCK FREE

				sta IO.CLRWRITEAUX

				plp

.8				rts
*--------------------------------------
BS.KW.CAT		lda #$27
				.HS 2C					BIT ABS
BS.KW.CATALOG	lda #$4F

				sta	BS.CATWIDTH

				lda GP.FoundBITS
				and	#CMDBITS0.T+CMDBITS0.FN1
				lsr
				bne	LB0BA

				sta	GP.ParamT

LB0BA			bcs	LB0C1

				jsr	BS.GetFileInfo
				bcs	LB127

LB0C1			jsr	BS.OpenDir
				bcs	LB127

				jsr	MON.CROUT

				jsr	BS.Filename2LineBuf
				jsr	BS.LINEBUFOUT
				jsr	MON.CROUT

				jsr	BS.LINEBUFCLR80

				ldy	#$10
				ldx #BSX.GetMSGY.X
				jsr	BS.ToAUX

				jsr	BS.LINEBUFOUT
				jsr	MON.CROUT

*				lda BS.CAT.FileCnt
*				ora	BS.CAT.FileCnt+1

LB0E8			jsr	LB241
				bcs	LB113

				lda GP.ParamT
				beq	LB0F7

				cmp	DIRENTBUF.TYPE
				bne	LB0FD

LB0F7			ldx #BSX.MkCatLine.X
				jsr BS.ToAUX

				jsr	BS.LINEBUFOUT

LB0FD			lda IO.KBD
				cmp	#$83
				bne	LB10E

				lda GP.STATE
				bne	LB113

				bit	IO.KSTROBE
				bcs	LB113

LB10E			lda #$FF
				bne	LB0E8

LB113			jsr	BS.MLIClose
				bcs	LB127

				jsr	MON.CROUT

				jsr	BS.MkCatLineF
				bcs	LB127

				jsr	BS.LINEBUFOUT
				jsr	MON.CROUT

				clc
LB127			rts
*--------------------------------------
BS.Filename2LineBuf
				jsr	BS.LINEBUFCLR80
				ldx	#$01
				ldy	DIRENTBUF.STNL
				lda #"/"
				cpy	#$F0
				bcc	LB13A

				sta	IO.LINEBUF,x
				inx

LB13A			tya
				and	#$0F
				sta	IO.LINEBUF

				ldy	#$05

LB142			lda DIRENTBUF,y
				ora	#$80
				sta	IO.LINEBUF,x
				iny
				inx
				dec	IO.LINEBUF
				bne	LB142

				lda #$A2
				sta	IO.LINEBUF
				rts
*--------------------------------------
BS.MkCatLineF	jsr BS.SetSBUFADRCMDBUF

				lda MLI.DEVNUM
				sta	GP.MLIMRKEOFBUF.REFNUM
				lda #MLI.C.ONLINE
				jsr	GP.GOSYSTEM
				bcs	LB127

				lda BS.CMDBUF
				and	#$0F
				inc
				sta	BS.CMDBUF.LEN
				lda #"/"
				sta	BS.CMDBUF
				jsr	BS.GetFileInfo
				bcs	LB127

				ldx #BSX.MkCatLineF.X
				jsr BS.ToAUX

				clc
				rts
*--------------------------------------
BS.OpenDir		lda #$01
				ldx	#$0F
				ldy	GP.STTYPE
				cpy	#$0F
				bne	LB1C8

				stx	GP.FITYPE

LB1C8			jsr	BS.OpenTypeXAccessA
				bcs	LB203

BS.ReadDirEnt	lda #$59
				sta	GP.MLIRW.DATAPTR
				lda #$02
				sta	GP.MLIRW.DATAPTR+1

				lda #$2B
				sta	GP.MLIRW.COUNT
				stz	GP.MLIRW.COUNT+1

				jsr BS.MLIRead
				bcs	LB203

				lda $27D
				sta BS.CAT.EPB

				lda $27C
				sta BS.CAT.EL
				sta	GP.MLIRW.COUNT

				lda #$01
				sta	BS.CAT.EICB

				stz	GP.SBUFADR+1
				stz	GP.SBUFADR+2
LB203			rts
*--------------------------------------
BS.CheckFTypeXAccessA
				pha
				lda GP.FoundBITS
				and	#CMDBITS0.T
				beq	LB20F

				ldx	GP.ParamT

LB20F			pla

BS.OpenTypeXAccessA
				cpx	GP.FITYPE
				bne	LB239

				and	GP.FIACCESS
				beq	LB23D

				lda BS.BUFPAGE
				sta	GP.OSYSBUF+1
				lda #$0F
				sta	MLI.LEVEL
				lda #MLI.C.OPEN
				jsr	GP.GOSYSTEM
				bcs	LB238

				lda GP.OREFNUM
				sta	GP.MLIRW.REFNUM
				sta	GP.MLICF.REFNUM
				sta	GP.MLIMRKEOFBUF.REFNUM

LB238			rts

LB239			lda #BS.E.MISMATCH
				.HS 2C					BIT ABS

LB23D			lda #BS.E.LOCKED
				sec
				rts
*--------------------------------------
LB241			lda GP.SBUFADR+1
				and	#$FE
				sta	GP.SBUFADR+1
				ldy	BS.CAT.EICB
				lda #$00
				cpy	BS.CAT.EPB
				bcc	LB25D

				tay
				sty	BS.CAT.EICB
				inc GP.SBUFADR+1

LB25A			inc GP.SBUFADR+1

LB25D			dey
				clc
				bmi	LB268

				adc	BS.CAT.EL
				bcc	LB25D

				bcs	LB25A

LB268			adc	#$04
				sta	GP.SBUFADR
				jsr BS.MLISetMark
				bcs	LB28D

				jsr BS.MLIRead
				bcs	LB28D

				inc	BS.CAT.EICB
				lda DIRENTBUF
				and	#$F0
				beq	LB241

*				lda BS.CAT.FileCnt
*				bne .1

*				dec	BS.CAT.FileCnt+1

*.1				dec	BS.CAT.FileCnt

LB28D			rts
*--------------------------------------
BS.KW.EXTERNAL	jmp	(GP.XTRNADDR)
*--------------------------------------
BS.KW.EXEC		jsr	BS.CheckFilename
				bcs	LB2C0

				bit	GP.EXECFILE
				bpl	LB2BC

				sta	GP.MLIMRKEOFBUF.REFNUM

				jsr BS.ZeroSBUFADR

				jsr BS.MLISetMark
				bcs	LB2B5

				lda GP.MLIMRKEOFBUF.REFNUM
				bne	LB333

LB2B5			pha
				jsr	LB36B
				pla
				sec
				rts

LB2BC			lda #BS.E.FBUSTY
				sec
				rts

LB2C0			bit	GP.EXECACTV
				bpl	LB2CA

				jsr	LB36B
				bcs	LB2D3

LB2CA			lda GP.FITYPE
				cmp	#$04
				beq	LB2D5

				lda #BS.E.MISMATCH
LB2D3			sec
				rts

LB2D5			jsr	BS.GetSysBuf
				bcs	LB2D3

				stz	GP.SBUFADR

				lda BS.BUFPAGE
				sta	GP.SBUFADR+1
				ldx	GP.OPENCNT
				beq	LB30E

				tay
				txa
				asl
				asl
				adc	BS.BUFPAGE
				pha

LB2F2			cmp	BS.BUFPAGEs-1,x
				beq	LB2FB

				dex
				bne	LB2F2

				brk

LB2FB			tya
				sta	BS.BUFPAGEs-1,x
				lda BS.REFNUMs,x
				sta	GP.MLIMRKEOFBUF.REFNUM
				lda #MLI.C.SETBUF
				jsr GP.GOSYSTEM
				bcc	LB30D

				brk

LB30D			pla

LB30E			sta	BS.BUFPAGE
				sta	GP.OSYSBUF+1

				stz	MLI.LEVEL

				lda #MLI.C.OPEN
				jsr	GP.GOSYSTEM
				bcc	LB327

				pha
				jsr	BS.FreeSysBuf
				pla
				sec
				rts

LB327			ldx	GP.OSYSBUF+1
				stx	BS.REFNUMs
				lda GP.OREFNUM
				sta	BS.REFNUM

LB333			sta	GP.MLIRW.REFNUM
				sta	GP.MLIMRKEOFBUF.REFNUM
				sta	GP.NEWLREF

				ldx	GP.FIAUXTYPE
				stx	GP.ParamL
				ldx	GP.FIAUXTYPE+1
				stx	GP.ParamL+1
				jsr	BS.AddOpenFile

				lda #$7F
				sta	GP.NLINEMASK
				lda #MLI.C.NEWLINE
				jsr	GP.GOSYSTEM
				lda GP.FoundBITS+1
				and	#CMDBITS1.F+CMDBITS1.R
				beq	LB364

				jsr	LB592
				bcc	LB364

				jmp	LB2B5

LB364			lda #$FF
				sta	GP.EXECACTV
				clc
				rts
*--------------------------------------
LB36B			lda GP.EXECACTV
				bpl	.8

				sta	GP.EXECFILE
				ldx	#$08
				lda BS.REFNUMs,x
				jsr	BS.CloseA

.8				rts
*--------------------------------------
BS.KW.VERIFY	bcs	LB3B7

				lda GP.FoundBITS
				and	#CMDBITS1.R
				bne	.8

				ldy	#0
				jsr	BS.GetMSGY
				jsr	MON.CROUT

.8				clc
				rts

*				lda #$00
*				beq	LB39F				BS.KW.FLUSH.1

*--------------------------------------
BS.KW.FLUSH		lda GP.FoundBITS
				and	#CMDBITS1.R
				beq	.1

				jsr	BS.CheckFilename
				bcs	.9

.1				sta	GP.MLICF.REFNUM
				lda #MLI.C.FLUSH
				jsr	GP.GOSYSTEM

.9				rts
*--------------------------------------
BS.KW.OPEN		php
				jsr	BS.CheckFilename
				bcs	LB3BB

				plp
				lda #BS.E.FBUSTY
				.HS 2C					BIT ABS
LB3B3			lda #BS.E.MISMATCH
				.HS 2C					BIT ABS
LB3B7			lda #BS.E.PATHNFND6
				sec
LB3B9			rts

LB3BB			plp
				ldx	#$00
				ldy	#$00
				lda GP.FoundBITS+1
				and	#CMDBITS1.L
				bne	LB3CD

				stx	GP.ParamL+1
				sty	GP.ParamL

LB3CD			lda GP.FoundBITS
				and	#CMDBITS0.T
				eor	#CMDBITS0.T
				beq	LB3DB

				lda #$04
				sta	GP.ParamT

LB3DB			bcc	LB3FE

				beq	LB3B7

				sta	GP.FITYPE
				lda #$C3
				sta	GP.FIACCESS

				ldx	GP.ParamL+1
				ldy	GP.ParamL
				stx	GP.CRAUXTYPE+1
				stx	GP.FIAUXTYPE+1
				sty	GP.CRAUXTYPE
				sty	GP.FIAUXTYPE
				jsr	BS.CreateFileOrDir
				bcs	LB3B9

LB3FE			lda GP.FITYPE
				cmp	GP.ParamT
				bne	LB3B3

				cmp	#$04
				bne	LB41D

				ldx	GP.FIAUXTYPE+1
				ldy	GP.FIAUXTYPE
				lda GP.FoundBITS+1
				and	#CMDBITS1.L
				bne	LB41D

				stx	GP.ParamL+1
				sty	GP.ParamL

LB41D			jsr	BS.GetSysBuf
				bcs	LB3B9

				lda BS.BUFPAGE
				sta	GP.OSYSBUF+1

				lda #$07
				sta	MLI.LEVEL

				lda #MLI.C.OPEN
				jsr	GP.GOSYSTEM
				bcc	LB43B

				pha
				jsr	BS.FreeSysBuf
				pla
				sec
				rts

LB43B			lda GP.FITYPE
				cmp	#$0F
				beq	LB443

				clc

LB443			lda #$00
				ror
				sta	GP.DIRFLG
				ldx	GP.OPENCNT
				lda BS.BUFPAGE
				sta	BS.BUFPAGEs,x
				lda GP.OREFNUM
				sta	BS.REFNUMs+1,x
				inc	GP.OPENCNT
*--------------------------------------
BS.AddOpenFile	asl
				asl
				asl
				asl
				asl
				tax
				lda MLI.PATHBUF
				ora	GP.DIRFLG
				sta	BS.OpenFiles,x
				and	#$7F
				tay
				cmp	#$1E
				bcc	LB473

				lda #$1D

LB473			sta	ZP.PCL

				lda GP.ParamL
				sta	BS.OpenFiles+1,x
				lda GP.ParamL+1
				sta	BS.OpenFiles+2,x

LB481			inx
				lda MLI.PATHBUF,y
				sta	BS.OpenFiles+2,x
				dey
				dec	ZP.PCL
				bne	LB481

				clc
				rts
*--------------------------------------
BS.CheckFilename
				lda GP.FoundBITS
				and	#CMDBITS0.FN1
				bne	LB49A

				lda #BS.E.SYNTAX
				sec
				rts

LB49A			ldx	GP.OPENCNT
				beq	LB4B8

				stx	GP.EXECFILE

LB4A2			stx	ZP.PCH
				lda BS.REFNUMs,x
				jsr	BS.FindOpenFile
				bne	LB4B3

				ldx	ZP.PCH

LB4AE			lda BS.REFNUMs,x
LB4B1			clc
				rts

LB4B3			ldx	ZP.PCH
				dex
				bne	LB4A2

LB4B8			lda GP.EXECACTV
				bpl	LB4CE

				lda BS.REFNUM
				jsr	BS.FindOpenFile
				bne	LB4CE

				lda #$FF
				sta	GP.EXECFILE
				ldx	#$08
				bne	LB4AE

LB4CE			lda #BS.E.FNOTOPEN
				sec
				rts
*--------------------------------------
BS.FindOpenFile	asl
				asl
				asl
				asl
				asl
				tax
				lda BS.OpenFiles,x
				sta	GP.DIRFLG
				and	#$7F
				cmp	MLI.PATHBUF
				bne	LB508

				tay
				cmp	#$1E
				bcc	LB4EC

				lda #$1D

LB4EC			sta	ZP.PCL

				lda BS.OpenFiles+1,x
				sta	BS.RecordSize
				lda BS.OpenFiles+2,x
				sta	BS.RecordSize+1

LB4FA			inx
				lda MLI.PATHBUF,y
				cmp	BS.OpenFiles+2,x
				bne	LB508

				dey
				dec	ZP.PCL
				bne	LB4FA

LB508			rts
*--------------------------------------
BS.KW.CLOSE		lda GP.FoundBITS
				and	#CMDBITS0.FN1
				beq	BS.CloseAll

				jsr	BS.CheckFilename
				bcs	LB4B1

BS.CloseA		sta	GP.MLICF.REFNUM
				lda BS.BUFPAGEs-1,x
				sta	BS.BUFPAGE
				bit	GP.EXECFILE
				bmi	LB53F

				ldy	GP.OPENCNT
				pha
				lda BS.BUFPAGEs-1,y
				sta	BS.BUFPAGEs-1,x
				pla
				sta	BS.BUFPAGEs-1,y
				lda BS.REFNUMs,x
				pha
				lda BS.REFNUMs,y
				sta	BS.REFNUMs,x
				pla
				sta	BS.REFNUMs,y

LB53F			stz	MLI.LEVEL

				jsr	BS.MLIClose
				bcs	LB572

				jsr	BS.FreeSysBuf
				bit	GP.EXECFILE
				bpl	LB55E

				stz	GP.EXECACTV
				stz	GP.EXECFILE

				rts

LB55E			dec GP.OPENCNT
				rts
*--------------------------------------
BS.CloseAll		ldx	GP.OPENCNT
				beq	LB573

				stx	GP.EXECFILE
				lda BS.REFNUMs,x
				jsr	BS.CloseA
				bcc	BS.CloseAll

LB572			rts

LB573			stz	GP.MLICF.REFNUM

				lda #$07
				sta	MLI.LEVEL
				jmp	BS.MLIClose
*--------------------------------------
BS.KW.POSITION	jsr	BS.CheckFilename
				bcs	LB5F0

				sta	GP.MLIRW.REFNUM
				sta	GP.NEWLREF
				bit	GP.DIRFLG
				bmi	LB5F0
*--------------------------------------
LB592			lda GP.FoundBITS+1
				and	#CMDBITS1.F+CMDBITS1.R
				beq	LB5ED

				cmp	#$03
				beq	LB5ED

				and	#$01
				beq	LB5AD

				lda GP.ParamR
				sta	GP.ParamF
				lda GP.ParamR+1
				sta	GP.ParamF+1

LB5AD			lda #$EF
				sta	GP.MLIRW.COUNT
				stz	GP.MLIRW.COUNT+1

				stz	GP.MLIRW.DATAPTR
				lda #$02
				sta	GP.MLIRW.DATAPTR+1

				lda #$7F
				sta	GP.NLINEMASK
				lda #MLI.C.NEWLINE
				jsr	GP.GOSYSTEM
				bcs	LB5F0

LB5CB			lda GP.ParamF
				ora	GP.ParamF+1
				clc
				beq	LB5F0

				jsr BS.MLIRead
				bcs	LB5F0

				lda GP.ParamF
				sbc	#$00
				sta	GP.ParamF
				lda GP.ParamF+1
				sbc	#$00
				sta	GP.ParamF+1
				bcs	LB5CB

LB5ED			lda #BS.E.INVOPT
				sec
LB5F0			rts
*--------------------------------------
LB5F1			lda BS.RecordSize
				sta	BS.TMPBUF4
				lda BS.RecordSize+1
				sta	BS.TMPBUF4+1

				stz	BS.TMPBUF4+2
				stz	BS.TMPBUF4+3

				jsr BS.ZeroSBUFADR

LB60E			lsr GP.ParamR+1
				ror GP.ParamR
				ldx	#$00
				bcc	LB62F

				clc

LB619			lda BS.TMPBUF4,x
				adc	GP.SBUFADR,x
				sta	GP.SBUFADR,x
				inx
				txa
				eor	#$03
				bne	LB619

				bcs	LB642

				ldx	BS.TMPBUF4+3
				bne	LB642

LB62F			rol BS.TMPBUF4,x
				inx
				txa
				eor	#$04
				bne	LB62F

				lda GP.ParamR
				ora	GP.ParamR+1
				bne	LB60E

				clc
				rts

LB642			lda #BS.E.RANGE
				sec
				rts
*--------------------------------------
BS.KW.READ		jsr	BS.CheckFilename
				bcs	LB69B

				sta	GP.MLIRW.REFNUM
				sta	GP.MLIMRKEOFBUF.REFNUM
				sta	GP.NEWLREF
				bit	GP.DIRFLG
				bmi	LB69C

				jsr	BS.CheckBFR
				bcs	LB69B

				ldx	#$7F
				ldy	#$EF
				lda GP.FoundBITS+1
				and	#CMDBITS1.L
				beq	LB67C

				ldy	GP.ParamL
				ldx	GP.ParamL+1
				bne	LB6CB

				cpy	#$EF
				bcs	LB6CB

				lda #$22
				sta	IO.LINEBUF

				lda #$01

LB67C			sta	GP.MLIRW.DATAPTR
				sty	GP.MLIRW.COUNT
				stx	GP.NLINEMASK
				lda #$02
				sta	GP.MLIRW.DATAPTR+1

				stz	GP.MLIRW.COUNT+1

				lda #MLI.C.NEWLINE
				jsr	GP.GOSYSTEM
				bcs	LB69B

LB696			lda #$FF
				sta	GP.IFILACTV
LB69B			rts

LB69C			lda #$59
				sta	GP.MLIRW.DATAPTR
				lda #$02
				sta	GP.MLIRW.DATAPTR+1
				lda #$01
				sta	GP.CATFLAG
				lda GP.FoundBITS+1
				and	#CMDBITS1.R
				clc
				beq	LB696

				jsr BS.ZeroSBUFADR

				jsr BS.MLISetMark
				bcc LB696

LB6CA			rts

LB6CB			lda #BS.E.RANGE
				tax
				sec
				rts
*--------------------------------------
BS.CheckBFR		lda GP.FoundBITS+1
				and	#CMDBITS1.B+CMDBITS1.F+CMDBITS1.R
				beq	LB719

				and	#CMDBITS1.R
				beq	LB6E5

				jsr	LB5F1
				bcs	LB6CB

LB6E0			jsr	LB712
				bcs	LB71A

LB6E5			lda GP.FoundBITS+1
				and	#CMDBITS1.F
				beq	LB6F1

				jsr	LB5AD
				bcs	LB71A

LB6F1			lda GP.FoundBITS+1
				and	#CMDBITS1.B
				beq	LB719

				jsr BS.MLIGetMark
				bcs	LB71A

				ldx	#$00
				ldy	#$02

LB703			lda GP.ParamB,x
				adc	GP.SBUFADR,x
				sta	GP.SBUFADR,x
				inx
				dey
				bpl	LB703

				bcs	LB6CB

LB712			jsr BS.MLISetMark
				bcs	LB71A

LB719			clc
LB71A			ldx	#$00
				rts
*--------------------------------------
BS.KW.WRITE		jsr	BS.CheckFilename
				bcs	LB732

				sta	GP.MLIRW.REFNUM
				sta	GP.MLIMRKEOFBUF.REFNUM
				sta	GP.NEWLREF
				bit	GP.DIRFLG
				bpl	LB734

				lda #BS.E.LOCKED

LB732			sec
				rts

LB734			stz	GP.MLIRW.DATAPTR
				lda #$02
				sta	GP.MLIRW.DATAPTR+1
				jsr	BS.CheckBFR
				bcc	LB757

				cmp	#$02
				bne	LB732

				cpx	#$02
				beq	LB732

LB74B			jsr BS.MLISetEOF
				bcs	LB732

				jsr	LB6E0
				bcs	LB732

LB757			lda AS.HIMEM
				sta	GP.MLIRW.DATAPTR
				lda AS.HIMEM+1
				sta	GP.MLIRW.DATAPTR+1
				lda #$FF
				sta	GP.OFILACTV
				clc
				rts
*--------------------------------------
BS.KW.APPEND	php
				jsr	BS.CheckFilename
				bcc	LB77A

				plp
				jsr	BS.KW.OPEN
				bcs	LB788

				lda GP.OREFNUM
				bne	LB77B

				brk

LB77A			plp

LB77B			sta	GP.MLIRW.REFNUM
				sta	GP.MLIMRKEOFBUF.REFNUM
				bit	GP.DIRFLG
				bpl	LB78A

				lda #BS.E.LOCKED
				sec

LB788			rts

LB78A			ldx	GP.ParamL
				ldy	GP.ParamL+1
				lda GP.FoundBITS+1
				and	#CMDBITS1.L
				bne	LB79D

				ldx	GP.FIAUXTYPE
				ldy	GP.FIAUXTYPE+1

LB79D			phx
				jsr BS.GetFTableIdx
				pla
				sta	BS.OpenFiles+1,x
				sta	BS.RecordSize
				tya
				sta	BS.OpenFiles+2,x
				sta	BS.RecordSize+1
				jsr	BS.MLIGetEOF
				bcs	LB788

				lda BS.RecordSize+1
				bne	LB7C8

				lda BS.RecordSize
				cmp	#$02
				bcc	LB7CD

LB7C8			jsr	BS.FileLenDivRS
				bcs	LB788

LB7CD			jmp	LB74B
*--------------------------------------
BS.FileLenDivRS	ldx	#$03

LB7D2			lda GP.SBUFADR-1,x
				sta	BS.TMPBUF4-1,x
				dex
				bne	LB7D2

				stz	BS.TMPBUF4+3
				stz	XBC0E

				ldy	#24

LB7E3			jsr	BS.TMPBUF3x2
				rol BS.TMPBUF4+3
				rol XBC0E
				sec
				lda BS.TMPBUF4+3
				sbc	BS.RecordSize
				tax
				lda XBC0E
				sbc	BS.RecordSize+1
				bcc	LB805

				stx	BS.TMPBUF4+3
				sta	XBC0E
				inc	BS.TMPBUF4

LB805			dey
				bne	LB7E3

				lda BS.TMPBUF4+3
				ora	XBC0E
				clc
				beq	LB839

				lda BS.RecordSize
				sec
				sbc	BS.TMPBUF4+3
				tax
				lda BS.RecordSize+1
				sbc	XBC0E
				tay
				txa
				clc
				adc	GP.SBUFADR
				sta	GP.SBUFADR
				tya
				adc	GP.SBUFADR+1
				sta	GP.SBUFADR+1
				bcc .1

				inc GP.SBUFADR+2

.1				lda #BS.E.RANGE

LB839			rts
*--------------------------------------
BS.GetFileInfo	ldx #$0A
				lda #MLI.C.GETFILEINFO
				bne	LB858

BS.SetFileInfoNoDate
				ldx #7

.1				stz	GP.FIMDATE,x
				dex
				bpl .1

BS.SetFileInfo	ldx #$07
				lda #MLI.C.SETFILEINFO

LB858			stx	GP.SSGINFO
				jmp	GP.GOSYSTEM

BS.JMP.GP.VSYSO	jmp	(GP.VSYSO)
BS.JMP.GP.VSYSI	jmp	(GP.VSYSI)
*--------------------------------------
BS.KW.MTR		jsr	BS.SetState0
				jsr	MON.CLREOL
				jmp	MON.ENTERMON
*--------------------------------------
BS.KW.MEM		jsr	MON.CROUT

				ldx #9

.1				lda AS.PGRMSTART,x
				sta $260,x
				dex
				bpl .1

				lda AS.HIMEM
				sta $260+10
				lda AS.HIMEM+1
				sta $260+11

				lda #0
				sta XBC10

.2				ldx #BSX.Mem.X
				jsr BS.ToAUX

				jsr	BS.LINEBUFOUT
				inc XBC10
				lda XBC10
				cmp #6
				bne .2

				jsr	MON.CROUT

				clc
				rts
*--------------------------------------
BS.KW.ONLINE	jsr	MON.CROUT

				jsr BS.SetSBUFADRCMDBUF

				lda MLI.DEVNUM
				pha

				lda #$10

.1				sta	GP.MLIMRKEOFBUF.REFNUM

				lda #MLI.C.ONLINE
				jsr	GP.GOSYSTEM
				bcs .7

				lda BS.CMDBUF
				and #$0F
				inc
				sta BS.CMDBUF.LEN
				lda #'/'
				sta BS.CMDBUF

				jsr BS.GetFileInfo
				bcs .7

				ldx #BSX.Online.X
				jsr BS.ToAUX

				jsr	BS.LINEBUFOUT

.7				lda GP.MLIMRKEOFBUF.REFNUM
				eor #$80
				bmi .1

				clc
				adc #$10
				bpl .1

				jsr	MON.CROUT

				pla
				sta MLI.DEVNUM

BS.KW.NOMON		clc
				rts
*--------------------------------------
* FORMAT VOLNAME,Sx,Dy
* ,A<HEADCOUNT>,E<TRACKCOUNT>,F<STEPPING>
* ,L<CATBLOCK>,B<BLOCK>
*
* d   : 1,2 heads
* sss : 4,3,2,1 steppings
* => T : dev type, 0 = BLK, 255 = D2
* => V : d0000sss
*--------------------------------------
BS.KW.FORMAT	ldx #BSX.FMT.CheckP.X
				jsr BS.ToAUX
				bcs .99
				
				bit GP.ParamT
				bmi .5
*--------------------------------------
* Format BLK dev
*--------------------------------------
				lda #10
				sta ZP.BUFPTR+1			fake Buffer for status & format
				stz ZP.BUFPTR

				jsr .2					A = cmd = 0 = status
				bcs .99
				
				lda GP.FoundBITS+1
				and #CMDBITS1.B
				bne .1

				stx GP.ParamB
				sty GP.ParamB+1
				stz GP.ParamB+2

.1				lda #3					cmd = 3 = format

				jsr .2

				bra BS.KW.FORMAT.CAT	ignore if not supported by HW

.99				lda #BS.E.IO
				rts

.2				sta ZP.CMD

				lda BS.WBlock.P.NUM		unitnum : DSSS0000
				sta ZP.UNITNUM

				lsr
				lsr
				lsr
				tax						000DSSS0
				
				bit IO.RRAMWRAMBNK1
				bit IO.RRAMWRAMBNK1
				
				jsr .3
				
				bit IO.RROMBNK1
				rts

.3				jmp (MLI.DEVPTRS,x)
*--------------------------------------
* Format Disk II
*--------------------------------------
.5				stz GP.ParamB+1			Total Block Count
				
				ldx #3

				bit GP.ParamV
				bpl .6
				
				inx						2 heads....
				
.6				lda GP.ParamE
				
.7				asl
				rol GP.ParamB+1
				dex
				bne .7
				
				sta GP.ParamB

				ldx #BSX.FMT.D2.X
				jsr BS.ToAUX
				bcs .99
*--------------------------------------
BS.KW.FORMAT.CAT
				lda #2					512 bytes buffers
				jsr BS.GETBUFR
				bcs .99
				
				sta BS.WBlock.P.PTR+1

				ldx GP.ParamV
				bne .10
				
				lda #0					BB.ProDOS
				ldx #BSX.FMT.GetCatBlk.X
				jsr BS.ToAUX

				jsr BS.WBlock
				bcs .9

				ldx #BSX.FMT.GetCatBlk.X
				lda #1					BB.SOS
				jsr BS.ToAUX

				jsr BS.WBlock
				bcs .9

				bra .11

.10				ldx #BSX.FMT.GetCatBlk.X
				lda #2					BB.FX 1/2
				jsr BS.ToAUX

				jsr BS.WBlock
				bcs .9

				ldx #BSX.FMT.GetCatBlk.X
				lda #3					BB.FX 2/2
				jsr BS.ToAUX

				jsr BS.WBlock
				bcs .9

.11				ldx #BSX.FMT.GetCatBlk.X
				lda #4					Ist CAT block
				jsr BS.ToAUX

				jsr BS.WBlock
				bcs .9

.1				ldx #BSX.FMT.GetCatBlk.X
				lda #5					Next CAT block
				jsr BS.ToAUX
				bcs .2

				jsr BS.WBlock
				bcs .9
				
				bra .1

.2				ldx #BSX.FMT.GetCatBlk.X
				lda #6					BM blocks
				jsr BS.ToAUX
				bcs .3

				jsr BS.WBlock
				bcs .9

				bra .2

.3


				clc

.9				php
				jsr BS.FREEBUFR
				lda #BS.E.IO
				plp
.99				rts
*--------------------------------------
BS.WBlock		jsr MLI
				.DA #MLI.C.WRITEBLOCK
				.DA BS.WBlock.P
				bcs .9

				inc BS.WBlock.P.BLK
				rts

.9				lda #BS.E.IO
				
				rts
*--------------------------------------
BS.WBlock.P		.DA #3
BS.WBlock.P.NUM	.BS 1
BS.WBlock.P.PTR	.BS 2
BS.WBlock.P.BLK	.BS 2
*--------------------------------------
BS.Expand2.P	.DA #1
				.DA MLI.PATHBUF
*--------------------------------------
BS.Expand2PATHBUF
				stz MLI.PATHBUF			reset target path

				lda BS.CMDBUF
				cmp #'/'				new path is absolute ?
				beq .1

				jsr MLI					no, get current path as a base
				.DA #MLI.C.GETPREFIX
				.DA BS.Expand2.P

.1				ldx #BSX.Expand.X
*--------------------------------------
BS.ToAUX		stx .1+1

				sei

				tsx
				sta IO.SETALTZP
				stx $100
				ldx $101
				txs

				bit IO.RRAMWRAMBNK1
				bit IO.RRAMWRAMBNK1

				cli

.1				ldx #$FF				SELF MODIFIED
				jsr BSX.JMPX

				stx .3+1
				php
				plx
				stx .2+1

				sei

				tsx
				stx $101
				ldx $100
				txs
				sta IO.CLRALTZP

				bit IO.RROMBNK1

				cli

.2				ldx #$FF				SELF MODIFIED
				phx
.3				ldx #$FF				SELF MODIFIED
				plp
				rts
*--------------------------------------
BS.ToAUXSaveX	.BS 1
*--------------------------------------
BS.GetFTableIdx	lda GP.MLIRW.REFNUM
				lsr
				ror
				ror
				ror
				tax
				rts
*--------------------------------------
BS.SetSBUFADRCMDBUF
				ldx	#BS.CMDBUF
				ldy	/BS.CMDBUF
BS.SetSBUFADRXY0
				stz	GP.SBUFADR+2

BS.SetSBUFADRXY	stx	GP.SBUFADR
				sty	GP.SBUFADR+1
				rts
*--------------------------------------
BS.ZeroSBUFADR	lda #0
				sta	GP.SBUFADR
				sta	GP.SBUFADR+1
				sta	GP.SBUFADR+2
				rts
*--------------------------------------
MAN
SAVE usr/src/basic.fx/basic.s.c
LOAD usr/src/basic.fx/basic.s
ASM
