NEW
  AUTO 3,1
*--------------------------------------
BS.ResetCSWKSW	pha
				phx

				ldx #3

.1				lda GP.VECTOUT,x
				sta	ZP.CSW,x
				dex
				bpl .1

				plx
				pla
				rts
*--------------------------------------
BS.SetState0	ldx	#$00
				jsr	BS.SetState

				lda ZP.KSW+1
				cmp	GP.VDOSI+1
				beq	BS.SetCSWKSW2VDOS

				lda ZP.CSW+1
				cmp	GP.VDOSO+1
				beq	BS.SetCSWKSW2VDOS

				bne	BS.SaveCSWKSW
*--------------------------------------
BS.IO.OUT		cmp	X9F65
				bne	L9A54

				stx	GP.PREGX
				tsx
				lda $103,x
				cmp	#$12				From AS rom: TRACE
				bne	L9A6E

				lda $104,x
				cmp	#$D8				jsr from $D810, then JSR MON.COUT
				bne	L9A6E

				ldx	#$04
				jsr	BS.SetState
				lda X9F65
				ldx	GP.PREGX
				jmp	BS.JMP.GP.VSYSO

L9A54			cmp	GP.CHRLAST
				sta	GP.CHRLAST
				bne	L9A74

				cmp	#$8D
				bne	L9A74

				lda ZP.CH
				bne	L9A69

				lda BS.CMDBUF
				cmp	#$3F

L9A69			lda #$8D
				bcs	L9A74

				rts

L9A6E			ldx	GP.PREGX
				lda X9F65

L9A74			jsr	BS.ResetCSWKSW
				jsr	MON.COUT
				cmp	#$8D
				bne	BS.SaveCSWKSW

				lda AS.TRCFLG
				cmp	#$A5
				beq	L9A8B

				sta	GP.DTRACE
				lda #$A5
				sta	AS.TRCFLG

L9A8B			lda #$8D

BS.SaveCSWKSW	pha
				phx

				ldx #3

.1				lda ZP.CSW,x
				sta	GP.VECTOUT,x
				dex
				bpl .1

				plx
				pla

BS.SetCSWKSW2VDOS
				pha
				phx

				ldx #3

.1				lda GP.VDOSO,x
				sta	ZP.CSW,x
				dex
				bpl .1

				plx
				pla
				rts
*--------------------------------------
BS.IO.IN		bit	GP.EXECACTV
				bpl	.1

				jsr	BS.SaveAXY
				jmp	BS.IO.IN.EXEC

.1				jsr	BS.ResetCSWKSW

				jsr	MON.KEYIN0
				cmp	#$8D
				beq .5

				cmp #$88
				beq .2

				cmp #$FF
				bne BS.SaveCSWKSW

.2			txa
			beq BS.IO.IN.BS

.3			lda #$88
			jsr MON.COUT

			lda #$A0
			jsr MON.COUT

			bra	BS.IO.IN.BS

.5				jsr	BS.SaveAXY
				sta	IO.LINEBUF,x

BS.CheckDOSCMD	jsr	BS.DOSCMD
				bit	GP.XCNUM
				bmi	L9AE8

				bcs	BS.ERROUT

				ldy	GP.PREGY
				ldx	#$00

BS.IO.IN.BS		lda #$88
				bra	BS.SaveCSWKSW

L9AE8			jsr	BS.RestoreAXY
				jmp	BS.SaveCSWKSW
*--------------------------------------
BS.NODEVERR		lda #$03

BS.ERROUT		sta	GP.ERRCODE
				sta	AS.ERRNUM
				lda GP.STATE
				cmp	#$01
				php
				lda #$0F
				sta	MLI.LEVEL

				stz	GP.IFILACTV
				stz	GP.OFILACTV
				stz	GP.PFXACTV
				stz	GP.MLICF.REFNUM		Close All

				jsr BS.MLIClose
				bcs	L9B27

				jsr	LA004
				bcs	L9B27

				stz	MLI.LEVEL
				stz	GP.MLICF.REFNUM
				lda #MLI.FLUSH
				jsr GP.GOSYSTEM

L9B27			plp
				ldx	#$04
				bit	AS.ERRFLG
				bcs	L9B30

				ldx	#$00

L9B30			php
				jsr	BS.SetState
				jsr	BS.SetCSWKSW2VDOS
				lda GP.ERRCODE
				plp
				bmi	L9B4D

				php
				jsr	GP.PRINTERR
				jsr	LB36B
				plp
				bcs	L9B53

				jsr	MON.CROUT
				jmp	AS.RESTART1

L9B4D			ldx	AS.REMSTK
				dex
				dex
				txs
				tax

L9B53			lda #$03
				jmp	AS.BITERRFLG
*--------------------------------------
L9B58			stz	AS.ERRNUM
				lda #$A3
				sta	X9F65
				jsr	AS.NORMAL
				jsr	BS.ResetCSWKSW
				jsr	BS.WriteTBUF
				jsr	BS.SetState0
				jsr	BS.RestoreAXY
				jmp	BS.IO.IN
*--------------------------------------
BS.IO.IN0		jsr	BS.SaveAXY
				bit	GP.PFXACTV
				bpl	L9B7E

				jmp	BS.IO.IN.CAT

L9B7E			bit	GP.IFILACTV
				bpl	L9B86
				jmp	BS.IO.IN.FILE

L9B86			bit	GP.EXECACTV
				bmi	BS.IO.IN.EXEC

				lda ZP.PROMPT
				cmp	#"]"
				beq	L9B58

				jsr	BS.ResetCSWKSW

L9B94			lda GP.PREGA
				ldy	GP.PREGY
				jsr	MON.KEYIN0
				cmp	#$88
				bne	L9BAC

				lda ZP.PROMPT
				cmp	#">"
				beq	L9BAA

				txa
				beq	L9B94

L9BAA			lda #$88
L9BAC			jmp	BS.SaveCSWKSW
*--------------------------------------
BS.IO.IN.EXEC	sta	(ZP.BASL),y
				lda ZP.PROMPT
				cmp	#">"
				beq	L9BF2

				lda IO.KBD
				bpl	L9BCD

				cmp	#$83
				bne	L9BCD

				jsr	LB36B
				lda GP.STATE
				bne	L9C01

				sta	IO.KBDSTROBE
				beq	L9C01

L9BCD			jsr	BS.Setup4readEFBytesAt200
				jsr	L9C6C
				bcs	L9BFA

				jsr	BS.SaveAXY
				bcc	L9BDE

L9BDA			dex
				lda IO.LINEBUF,x

L9BDE			ora	#$80
				sta	IO.LINEBUF,x
				txa
				bne	L9BDA

				jsr	BS.ResetCSWKSW
				jsr	BS.CheckDOSCMD
				bit	GP.XCNUM
				bpl	L9BCD

L9BF1			rts

L9BF2			jsr	BS.Setup4readEFBytesAt200
				jsr	BS.ReadOneByte
				bcc	L9BF1

L9BFA			jsr	LB2B5
				cmp	#$05
				bne	L9C13

L9C01			ldx	#$00
				ldy	ZP.CH
				bne	L9C0E

				lda #"]"
				sta	ZP.PROMPT
				lda #$88
				rts

L9C0E			lda (ZP.BASL),y
				jmp	(ZP.KSW)

L9C13			jmp	BS.ERROUT
*--------------------------------------
BS.IO.IN.FILE	lda ZP.PROMPT
				cmp	#"]"
				bne	L9C1F
				jmp	L9B58

L9C1F			lda GP.PREGA
				sta	(ZP.BASL),y
				lda IO.KBD
				bpl	L9C31

				cmp	#$83
				bne	L9C31

				bit	IO.KBDSTROBE
				rts

L9C31			lda ZP.PROMPT
				bit	GP.DIRFLG
				bmi	L9C95

				cmp	#">"
				beq	L9C42

				jsr	L9C67
				bcs	L9C13

				rts

L9C42			jsr	BS.ReadOneByte
				bcs	L9C13

				rts
*--------------------------------------
BS.ReadOneByte	lda GP.MLIRW.COUNT
				sta	GP.ParamL
				lda #$01
				sta	GP.MLIRW.COUNT

				jsr BS.MLIRead
				bcs	L9C66

				lda GP.ParamL
				sta	GP.MLIRW.COUNT
				ldx	GP.MLIRW.DATAPTR
				lda IO.LINEBUF,x

L9C66			rts
*--------------------------------------
L9C67			lda GP.PREGA
				sta	(ZP.BASL),y

L9C6C			jsr BS.MLIRead
				bcs	L9C66

				lda GP.MLIRW.TRANS
				beq	L9C8E

				clc
				adc	GP.MLIRW.DATAPTR
				tax
				lda IO.LINEBUF-1,x
				and	#$7F
				cmp	#$0D
				bne	L9C8E

				lda GP.FoundBITS+1
				and	#CMDBITS1.L
				bne	L9C8E

				dex

L9C8E			lda #$8D
				ldy	GP.PREGY
				clc
				rts
*--------------------------------------
L9C95			cmp	#$BE
				beq	L9C8E

				lda GP.PREGA
				sta	(ZP.BASL),y
				lda #$4F
				sta	BS.CATWIDTH
				jsr BS.MLIGetMark
				bcs	L9D1F

				lda GP.SBUFADR
				ora	GP.SBUFADR+1
				bne	L9CDF

				lda #$02
				sta	GP.CATFLAG
				jsr	BS.ReadDirEnt
				bcs	L9D1F

				jsr BS.GetFTableIdx

				lda BS.CAT.EL
				sta	BS.OpenFiles+1,x
				lda BS.CAT.EPB
				sta	BS.OpenFiles+2,x
				jsr	BS.Filename2LineBuf

L9CD3			lda #$A2
				sta	IO.LINEBUF
				lda #$8D
				sta	IO.LINEBUF,x
				clc
				rts

L9CDF			lda GP.CATFLAG
				beq	L9D22

				bmi	L9CF9

				ldx	#$00
				dec	GP.CATFLAG
				beq	L9CD3

				jsr	BS.LINEBUFCLR80

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

L9CF5			ldx	#$50
				bne	L9CD3

L9CF9			cmp	#$FF
				bne	L9D1C

				dec	GP.CATFLAG

				stz	GP.MLIRW.COUNT
				stz	GP.MLIRW.COUNT+1

				jsr BS.MLIRead
				jsr	BS.MkCatLineF
				pha
				lda GP.MLIRW.REFNUM
				sta	GP.MLIMRKEOFBUF.REFNUM
				pla
				bcc	L9CF5

				bcs	L9D1F

L9D1C			lda #$05
				sec

L9D1F			jmp	BS.ERROUT

L9D22			lda #$FF
				sta	BS.CAT.EICB

				jsr BS.GetFTableIdx

				lda BS.OpenFiles+1,x
				sta	BS.CAT.EL
				lda BS.OpenFiles+2,x
				sta	BS.CAT.EPB
				lda GP.SBUFADR+1
				and	#$01
				tax
				lda GP.SBUFADR
				sec

L9D45			inc BS.CAT.EICB
				sbc	BS.CAT.EL
				bcs	L9D45

				dex
				beq	L9D45

				jsr	LB241
				bcc	L9D65

				cmp	#$02
				beq	L9D5D

				cmp	#$05
				bne	L9D1F

L9D5D			dec GP.CATFLAG
				ldx	#$00
				jmp	L9CD3

L9D65			ldx #BSX.MkCatLine
				jsr BS.ToAUX

				jmp	L9CF5
*--------------------------------------
BS.IO.IN.CAT	cmp	#$DD
				bne	L9D72

				jmp	L9B58

L9D72			lda GP.PREGA
				sta	(ZP.BASL),y

				stz	GP.PFXACTV
				ldx	BS.CMDBUF.LEN

L9D7F			lda BS.CMDBUF.LEN,x
				sta	IO.LINEBUF-1,x
				dex
				bne	L9D7F

				ldx	BS.CMDBUF.LEN
				lda #$8D
				rts
*--------------------------------------
BS.Setup4readEFBytesAt200
				lda BS.REFNUM
				sta	GP.MLIRW.REFNUM
				lda #$02
				sta	GP.MLIRW.DATAPTR+1
				lda #$EF
				sta	GP.MLIRW.COUNT

				stz	GP.MLIRW.COUNT+1
				stz	GP.MLIRW.DATAPTR
				rts
*--------------------------------------
BS.VSYSO		jsr	BS.SaveAXY
				cmp	#$84
				bne	.1

				jsr	BS.WriteTBUF
			stz	GP.INPTR
			stz	GP.IFILACTV
			stz	GP.OFILACTV
			stz	GP.PFXACTV
				jmp BS.SetState8RestAXY

.1				jsr BS.SetState4RestAXY

				jmp	BS.JMP.GP.VSYSO
*--------------------------------------
BS.IO.OUT.CAT	jsr	BS.SaveAXY
				ldx	GP.INPTR
				sta	IO.LINEBUF,x
				cmp	#$8D
				beq	L9DEB

				inc	GP.INPTR
				bne	L9DE7

				lda #$10

L9DE4			jmp	BS.ERROUT

L9DE7			ldx	GP.PREGX
				rts

L9DEB			inx
				dex
				beq	BS.SetState4RestAXY

				jsr	BS.ResetCSWKSW
				jsr	BS.DOSCMD
				bcs	L9DE4

				jsr	BS.SaveCSWKSW

BS.SetState4RestAXY
				ldx	#$04
				.HS 2C					BIT ABS
BS.SetState8RestAXY
				ldx	#$08
				jsr BS.SetState
				jmp	BS.RestoreAXY

L9E02			sty	GP.PREGY
				ldy	ZP.PROMPT
				beq	L9E15

				dey
				beq	L9E15

				dey
				beq	L9E15

				ldy	GP.PREGY
				jmp	L9A74

L9E15			ldy	GP.TBUFPTR
				and	#$7F
				sta	(AS.HIMEM),y
				ldy	GP.PREGY
				inc	GP.TBUFPTR
				bne	L9E2F

				jsr	BS.SaveAXY
				jsr	BS.WriteTBUF256
				bcs	L9DE4

				jsr	BS.RestoreAXY

L9E2F			rts
*--------------------------------------
BS.IO.OUT.TRACE	pha
				stx	GP.PREGX
				tsx
				lda $104,x
				cmp	#$12
				bne	L9E46

				lda $105,x
				cmp	#$D8
				bne	L9E46

				pla
				bcs	L9EBA

L9E46			pla
				ldx	GP.PREGX
				bit	GP.OFILACTV
				bpl	L9E70

				cmp	#$DD
				bne	L9E5A

				cmp	ZP.PROMPT
				beq	L9E8A

L9E5A			cmp	#$8D
				bne	L9E02

				lda ZP.PROMPT
				cmp	#$FF
				lda #$8D
				bcc	L9E02

				jsr	BS.WriteTBUF
				sta	GP.OFILACTV
				lda #$8D
				bne	L9EA3

L9E70			pha
				lda GP.IFILACTV
				ora	GP.PFXACTV
				bpl	L9E81

				lda ZP.PROMPT
				ora	#$04
				cmp	#$84
				beq	L9EA6

L9E81			pla
				cmp	#$DD
				bne	L9EA3

				cmp	ZP.PROMPT
				bne	L9EA3

L9E8A			jsr	BS.SaveAXY
				lda GP.OPENCNT
				beq	L9EA0

				jsr	BS.WriteTBUF
				sta	GP.OFILACTV
				jsr	BS.ResetCSWKSW
				lda #BS.E.FSTILLOPEN
				jsr	GP.PRINTERR

L9EA0			jsr	BS.RestoreAXY
L9EA3			jmp	L9A74

L9EA6			pla
				cmp	#$8D
				bne	L9EAE

				cmp	GP.CHRLAST

L9EAE			sta	GP.CHRLAST
				bne	L9EB5

				sta	ZP.PROMPT

L9EB5			rts

L9EB6			inc AS.TXTPTR+1
				bne	L9EBE

L9EBA			inc AS.TXTPTR
				beq	L9EB6

L9EBE			ldx	#$FF
				stx	ZP.PROMPT
				ldx	AS.REMSTK
				txs
				bit	GP.DTRACE
				bmi	L9F3D

L9ECA			ldy	#$00
				lda (AS.TXTPTR),y
				bmi	L9EF5

				beq	L9EF2

L9ED2			dec GP.STRINGS
				bne	L9EF0

				lda AS.STRINGSTART+1
				sbc	AS.ARRAYEND+1
				cmp	#$03
				bcs	L9EE9

				jsr	BS.WriteTBUF
				jsr	BS.KW.FRE
				lda AS.STRINGSTART+1
				sbc	AS.ARRAYEND+1

L9EE9			sta	GP.STRINGS
				ldy	#$00
				lda (AS.TXTPTR),y

L9EF0			cmp	#$3A
L9EF2			jmp	AS.EXECSTMT

L9EF5			sta	ZP.PROMPT
				tay
				lda BS.AS.KW-$80,y
				bmi	L9EF2

				sta	X9F00+1

X9F00			bne	BS.AS.KW0			SELF MODIFIED
BS.AS.KW0
BS.AS.KW.IF
BS.AS.KW.PRINT
				stz	ZP.PROMPT
				stz	GP.CHRLAST
				lda #BS.VSYSO
				sta	GP.VSYSO
				lda /BS.VSYSO
				sta	GP.VSYSO+1
				bne	L9F32

BS.AS.KW.LIST	lda #$01
				.HS 2C					BIT ABS
BS.AS.KW.CALL	lda #$02
				sta	ZP.PROMPT
				bne	L9F32

BS.AS.KW.LET	tya
				bra	L9ED2

BS.AS.KW.TRACE	sty	GP.DTRACE
				bne	L9F2E

BS.AS.KW.NOTRACE
				sta	GP.DTRACE
				dey

L9F2E			lda #$4A
				sta	AS.TRCFLG

L9F32			tya
				jmp	AS.EXECSTMT
*--------------------------------------
BS.AS.KW.RESUME	stz	AS.ERRNUM
				tya
				bne	L9EF0

L9F3D			jsr	BS.ResetCSWKSW
				lda #$A3
				jsr	MON.COUT
				ldx	AS.CURLINE
				lda AS.CURLINE+1
				jsr	AS.INTPRINTAX
				lda #$A0
				jsr	MON.COUT
				jsr	BS.SaveCSWKSW
				sec
				jmp	L9ECA
BS.AS.KW.FLASH
				lda #$E3
				.HS 2C					BIT ABS
BS.AS.KW.NORMAL
BS.AS.KW.INVERSE
				lda #$A3

L9F5E			sta	X9F65
				tya
				bne	L9EF0

X9F65			.DA #$A3
*--------------------------------------
BS.SaveAXY		sta	GP.PREGA
				stx	GP.PREGX
				sty	GP.PREGY
				rts
*--------------------------------------
BS.RestoreAXY	lda GP.PREGA
				ldx	GP.PREGX
				ldy	GP.PREGY
				rts
*--------------------------------------
BS.SetState		stx	GP.STATE
				ldy	#$00

.1				lda BS.SYSIOVECTS,x
				sta	GP.VSYSO,y
				inx
				iny
				cpy	#$04
				bne	.1

				rts
*--------------------------------------
BS.PRINTERR		ldx #BSX.GetErrA.X
				.HS 2C					BIT ABS
BS.GetMsgY		ldx #BSX.GetMsgY.X
				jsr	BS.ToAUX

				ldx	GP.INPTR
				stx	BS.CATWIDTH
				jsr	MON.CROUT
				lda #$87
				jsr	MON.COUT

BS.LINEBUFOUT	ldx	#$00

.1				lda IO.LINEBUF+1,x
				jsr	MON.COUT
				inx
				cpx	BS.CATWIDTH
				bcc	.1

				jmp	MON.CROUT
*--------------------------------------
BS.WriteTBUF256	lda #$00
				ldy	#$01
				bne	BS.WriteTBUFAY

BS.WriteTBUF	jsr	LA004
				bcc	LA020

				tay
				pla
				pla
				tya
				jmp	BS.ERROUT
*--------------------------------------
LA004			ldy	#$00
				lda GP.TBUFPTR
				clc
				beq	LA020

BS.WriteTBUFAY	sta GP.MLIRW.COUNT
				sty	GP.MLIRW.COUNT+1

				jsr BS.MLIWrite

				stz	GP.TBUFPTR

LA020			rts
*--------------------------------------
BS.FRE0			ldx #BSX.Fre0.X
				.HS 2C					BIT ABS
BS.KW.FRE
BS.FRE1			ldx #BSX.Fre0.X

				ldy #13

.1				lda AS.PGRMSTART,y
				sta ASX.PGRMSTART,y
				dey
				bpl .1

				jsr BS.ToAUX
				bcs .9

				pha
				phy
				ldy #13

.2				lda ASX.PGRMSTART,y
				sta AS.PGRMSTART,y
				dey
				bpl .2

				ply
				pla

.9				rts
*--------------------------------------
* DUPLICATE CODE in BASIC.S.XM.txt
*--------------------------------------
LA160			clc
				lda XBBDC
				sta	ZP.A2L
				stx	ZP.A2H
				eor	AS.ARRAYEND
				bne	LA170

				cpx	AS.ARRAYEND+1
				beq	LA198

LA170			ldy	#$02
				lda (ZP.A2L),y
				adc	ZP.A2L
				sta	XBBDC
				iny
				lda (ZP.A2L),y
				adc	ZP.A2H
				tax
				ldy	#$00
				lda (ZP.A2L),y
				iny
				eor	(ZP.A2L),y
				bpl	LA160

				ldy	#$04
				lda (ZP.A2L),y
				asl
				adc	#$05
				adc	ZP.A2L
				sta	ZP.A2L
				bcc	LA198

				inc	ZP.A2H

				clc
LA198			rts
*--------------------------------------
MAN
SAVE usr/src/basic.fx/basic.s.a
LOAD usr/src/basic.fx/basic.s
ASM
