NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF A2OSX.SYSTEM,TSYS
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
				.INB INC/IO.I
				.INB INC/MONITOR.I
				.INB INC/MLI.I
				.INB INC/MLI.E.I
				.INB INC/ZP.I
				.INB INC/GFX.I
*--------------------------------------
TmpPtr1			.EQ	$0					main & aux ZP
TmpPtr2			.EQ	$2					main & aux ZP
*--------------------------------------
A2osX.KMLOAD	.EQ $2000
A2osX.LOGOLOAD	.EQ $4000
A2osX.KMMAXLEN	.EQ $6000
A2osX.OpenBuff1	.EQ $8000				1024 Buffer for MLIOPEN01
A2osX.OpenBuff2	.EQ $8400				1024 Buffer for MLIOPEN02
A2osX.ReadBuff	.EQ	$8800				512 Buffer for MLIREAD01
Boot.Prefix		.EQ $8A00				MLI.MAXPATH
LOGO.Filename	.EQ $8A80				MLI.MAXPATH
KM.Filename		.EQ $8B00				MLI.MAXPATH
*--------------------------------------
* Kernel Stage 0
*--------------------------------------
SYSTEM.START	.EQ *
				.PH	$1000
A2osX.Init0		>LDYAI $2000
				>STYA TmpPtr1
				>LDYAI $1000
				>STYA TmpPtr2

				ldx #$10				reloc $2000-$2FFF to $1000-$1FFF
				ldy #0
.1				lda (TmpPtr1),y
				sta (TmpPtr2),y
				iny
				bne .1
				inc TmpPtr1+1
				inc TmpPtr2+1
				dex
				bne .1

				jmp A2osX.Init1
*--------------------------------------
A2osX.Init1		ldx	#$FF				init 6502 stack to highest
				txs
				bit RROMBNK1
				jsr HOME
				>LDYAI MSG.INIT0
				jsr PrintFYA

				>LDYAI MSG.SYSMEM
				jsr PrintFYA

				lda MACHID
				and #MACHID.M
				cmp #MACHID.M.128
				beq A2osX.Init1.128
				>LDYAI MSG.SYSMEM.KO
				jsr PrintFYA
				bra *
*--------------------------------------
A2osX.Init1.128	jsr MLI
				.DA #MLIGETPREFIX
				.DA MLIGETPREFIX01
				bcs *

				ldx Boot.Prefix

.1				lda Boot.Prefix,x
				sta A2osX.QC.B.BOOT,x
				sta A2osX.QC.B.ROOT,x
				dex
				bpl .1

				jsr A2osX.Screen80
				>LDYAI MSG.INIT0
				jsr PrintFYA

				lda #Boot.Prefix
				pha
				lda /Boot.Prefix
				pha
				>LDYAI MSG.BOOTPREFIX
				jsr PrintFYA

				>LDYAI MSG.SYSMEM
				jsr PrintFYA

				>LDYAI MSG.SYSMEM.OK
				jsr PrintFYA
				>LDYAI MSG.CPU
				jsr PrintFYA

				sed						Test 6502 BCD bug
				lda #$99
				clc
				adc #$01
				cld
				bpl .2					if minus, BUG!, we have 6502
				>LDYAI MSG.CPU.KO
				jsr PrintFYA
				bra *

.2				>LDYAI MSG.CPU.OK
				jsr PrintFYA

				stz LEVEL				Back To FILE LEVEL 0

				jsr MLI					Make sure all files are closed
				.DA #MLICLOSE
				.DA MLICLOSE01
				bcs *
*--------------------------------------
A2osX.RamDrv	>LDYAI MSG.RAMDRV
				jsr PrintFYA

				jsr DisableRamDRV
				bcc .2

				tax
				bne .1
				>LDYAI MSG.RAMDRV.KO0
				jsr PrintFYA
				bra A2osX.SetupQC
.1				>LDYAI MSG.RAMDRV.KO1
				jsr PrintFYA
				bra A2osX.SetupQC
.2				>LDYAI MSG.RAMDRV.OK
				jsr PrintFYA
*--------------------------------------
A2osX.EnumKM	>LDYAI MSG.KMENUM
				jsr PrintFYA

				jsr EnumKM

				bcs *
*--------------------------------------
A2osX.SetupQC	lda RRAMWRAMBNK2
				lda RRAMWRAMBNK2

				ldx #0
.1				lda A2osX.QC.B.Start,x
				sta $D100,x
				lda A2osX.QC.B.Start+$100,x
				sta $D200,x
				lda A2osX.QC.B.Start+$200,x
				sta $D300,x
				inx
				bne .1

				lda RROMBNK1
*--------------------------------------
A2osX.MLIQuit	>LDYAI MSG.INIT0OK
				jsr PrintFYA
				>DEBUGOA
				jsr MLI
				.DA #MLIQUIT
				.DA MLIQUIT01
				bra *
*--------------------------------------
A2osX.Screen80	lda #$8C				Reset 80 col screen ($0C:HOME)
				jsr $C300

				ldx Boot.Prefix

.1				lda Boot.Prefix,x
				sta LOGO.Filename,x
				dex
				bne .1
				ldx Boot.Prefix
				ldy #0
.2				inx
				iny
				lda A2osX.LOGO,y
				sta LOGO.Filename,x
				cpy A2osX.LOGO
				bne .2
				stx LOGO.Filename
				jsr MLI
				.DA #MLIOPEN
				.DA MLIOPEN00
				bcs .99

				lda MLIOPEN00+5
				sta MLIREAD00+1
				sta MLICLOSE00+1
				jsr MLI
				.DA #MLIREAD
				.DA MLIREAD00		
				bcs .98
				jsr .98
				bra A2osX.ScreenDLGR

.98				jsr MLI
				.DA #MLICLOSE
				.DA MLICLOSE00
.99				rts
*--------------------------------------
A2osX.ScreenDLGR
				lda #20
				sta WNDTOP
				sta CV
				jsr VTAB
				sta CLRHIRES
				sta SETMIXED
				sta CLRTEXT
				sta SET80STORE
				sta SETDHIRES

				lda #A2osX.LOGOLOAD+S.BM
				sta TmpPtr1
				lda /A2osX.LOGOLOAD+S.BM
				sta TmpPtr1+1
				ldx #0
.1				lda SCR.BASEL,x
				sta TmpPtr2
				lda SCR.BASEH,x
				sta TmpPtr2+1

				ldy #0
.2				lda (TmpPtr1),y
				pha
				and #$f
				phy
				tay
				lda PALETTE.AUX,y
				ply
				sta SETPAGE2
				sta (TmpPtr2),y
				pla
				lsr
				lsr
				lsr
				lsr
				sta CLRPAGE2
				sta (TmpPtr2),y

				iny
				cpy #40
				bne .2
				lda TmpPtr1
				clc
				adc #40
				sta TmpPtr1
				bcc .3
				inc TmpPtr1+1
.3				ldy #0

.4				lda (TmpPtr1),y
				pha
				and #$f
				phy
				tay
				lda PALETTE.AUX,y
				ply
				asl
				asl
				asl
				asl
				sta SETPAGE2
				ora (TmpPtr2),y
				sta (TmpPtr2),y
				pla
				and #$f0
				sta CLRPAGE2
				ora (TmpPtr2),y
				sta (TmpPtr2),y

				iny
				cpy #40
				bne .4
				lda TmpPtr1
				clc
				adc #40
				sta TmpPtr1
				bcc .7
				inc TmpPtr1+1
.7				inx
				cpx #20
				bne .1

				rts
*--------------------------------------
DisableRamDRV	php
				sei
				lda DEVPTRS3D2
				cmp DEVPTRS				S0D1=NOVEV				
				bne .1
				lda DEVPTRS3D2+1
				cmp DEVPTRS+1			S0D1=NODEV
				beq .9					S3D2=NODEV, nothing to do
.1				ldx DEVCNT

.2				lda DEVLST,x			LOOKING FOR $BF, $BB, $B7, $B3
				and #$F3
				cmp #$B3
				beq .3
				dex
				bpl .2
				plp
				lda #1					No device found, exit with error
				sec
				rts
.3				cpx DEVCNT
				beq .5

.4				lda DEVLST+1,x
				sta DEVLST,x
				inx
				cpx DEVCNT
				bne .4
.5				ldx DEVCNT
				stz DEVLST,x
				dec DEVCNT
				lda DEVPTRS
				sta DEVPTRS3D2
				lda DEVPTRS+1
				sta DEVPTRS3D2+1
				plp
				jsr MLI
				.DA #MLIONLINE
				.DA MLIONLINE01
				clc						Success!!
				rts
.9				plp
				lda #0					Not detected
				sec
				rts
*--------------------------------------
EnumKM.EPB		.EQ $0D
EnumKM.EL		.EQ $27
*--------------------------------------
EnumKM			ldx #0
				ldy Boot.Prefix

.1				inx
				lda SYS,x
				sta Boot.Prefix+1,y
				iny
				cpx SYS
				bne .1

				sty Boot.Prefix
				lda #Boot.Prefix
				pha
				lda /Boot.Prefix
				pha
				>LDYAI MSG.KMSYSDIR
				jsr PrintFYA
	
				jsr MLI
				.DA #MLIOPEN
				.DA MLIOPEN01
				bcc .10
				
				pha
				>LDYAI MSG.KMSYSDIR.KO
				jsr PrintFYA
				sec
				rts

.10				lda MLIOPEN01+5
				sta MLIREAD01+1
				sta MLICLOSE01+1

.2				jsr MLI
				.DA #MLIREAD
				.DA MLIREAD01
				bcs .98

				lda #EnumKM.EPB
				sta EnumKM.EC

				>LDYAI A2osX.ReadBuff+4
				>STYA EnumKM.BPTR

.3				>LDYA EnumKM.BPTR
				>STYA TmpPtr1

				lda (TmpPtr1)
				and #$F0				Empty ?
				beq .5					yes, skip

				and #$C0				anything out of type 1,2 or 3 ?
				bne .5					yes, skip

				lda (TmpPtr1)
				and #$0F
				cmp KM.PREFIX			at least enough chars for KM.*?
				bcc .5					no, skip

				ldy #$10				file type
				lda (TmpPtr1),y
				cmp #$06				'BIN'?
				bne .5					skip

				ldy KM.PREFIX

.4				lda (TmpPtr1),y			KM.* ?
				cmp KM.PREFIX,y
				bne .5

				dey
				bne .4

				jsr LoadKM

.5				lda EnumKM.BPTR
				clc	
				adc #EnumKM.EL
				sta EnumKM.BPTR
				bcc .6

				inc EnumKM.BPTR+1

.6				dec EnumKM.EC
				bne .3

				bra .2

.98				cmp #MLI.E.EOF
				beq .8

				pha
				jsr .8
				pla

				sec
				rts

.8				jsr MLI
				.DA #MLICLOSE
				.DA MLICLOSE01
				rts
*--------------------------------------
EnumKM.EC		.BS 1
EnumKM.BPTR		.BS 2
*--------------------------------------
LoadKM			ldx Boot.Prefix

.1				lda Boot.Prefix,x
				sta KM.Filename,x
				dex
				bne .1

				ldy #$1c
				lda (TmpPtr1),y
				sta LoadKM.VERSION
				iny
				lda (TmpPtr1),y
				sta LoadKM.MVERSION
				
				ldx Boot.Prefix
				lda (TmpPtr1)
				and #$0F
				tay
				clc
				adc Boot.Prefix
				sta KM.Filename
				tax

.2				bit LoadKM.VERSION
				bpl .6

				cpy #8
				bcs .3
				
				lda LoadKM.Bits,y
				bit LoadKM.VERSION
				beq .6
				bra .4

.3				lda LoadKM.Bits-8,y
				bit LoadKM.MVERSION
				beq .6
				
.4				lda (TmpPtr1),y
				eor #$20
				.HS 2C					BIT ABS

.6				lda (TmpPtr1),y
				sta KM.Filename,x
				dex
				dey
				bne .2

				lda #KM.Filename
				pha
				lda /KM.Filename
				pha
				>LDYAI MSG.KMLOAD
				jsr PrintFYA

				jsr MLI
				.DA #MLIOPEN
				.DA MLIOPEN02
				bcs .99

				lda MLIOPEN02+5
				sta MLIREAD02+1
				sta MLICLOSE02+1

				jsr MLI
				.DA #MLIREAD
				.DA MLIREAD02
				bcs .98

				jsr .9
				jsr A2osX.KMLOAD

.98				pha
				jsr .9
				pla

				sec
				rts

.9				jsr MLI
				.DA #MLICLOSE
				.DA MLICLOSE02
.99				rts
*--------------------------------------
LoadKM.VERSION	.BS 1
LoadKM.MVERSION	.BS 1
LoadKM.Bits		.HS 8040201008040201
*--------------------------------------
				.INB USR/SRC/SHARED/X.PRINTF.S
*--------------------------------------
MSG.INIT0		.AZ "A2osX[Stage0]:Init\n"
MSG.SYSMEM		.AZ "Checking System Memory..."
MSG.SYSMEM.OK	.AZ "128k Detected.\n"
MSG.SYSMEM.KO	.AZ "A2osX Requires At Least 128k Memory.\n"

MSG.CPU			.AZ "Checking CPU..."
MSG.CPU.OK		.AZ "65C02 CPU (or higher) Detected.\n"
MSG.CPU.KO		.AZ "A2osX Requires At Least A 65C02 CPU.\n"

MSG.RAMDRV		.AZ "Checking ProDOS RamDrive..."
MSG.RAMDRV.OK	.AZ "Successfully Disabled.\n"
MSG.RAMDRV.KO0	.AZ "Not Detected.\n"
MSG.RAMDRV.KO1	.AZ "Uninstall Failed !!!\n"

MSG.BOOTPREFIX	.AZ "Boot Prefix:%S\n"

MSG.KMENUM		.AZ "Looking For Kernel Modules...\n"
MSG.KMSYSDIR	.AZ "SYS dir is %S\n"
MSG.KMSYSDIR.KO	.AZ "***Error : $%h\n"
MSG.KMLOAD		.AZ "***Loading KM : %S\n"
MSG.INIT0OK		.AZ "A2osX[Stage0]:Complete.\n"
*--------------------------------------
MLIGETPREFIX01	.DA #1
				.DA Boot.Prefix
MLIONLINE01		.DA #2
				.DA #0
				.DA MLIONLINE01BUF
MLIONLINE01BUF	.BS 256
*--------------------------------------
MLIOPEN00		.DA #3					For DIR listing
				.DA LOGO.Filename
				.DA A2osX.OpenBuff1		
				.BS 1
MLIREAD00		.DA #4
				.BS 1
				.DA	A2osX.LOGOLOAD
				.DA 2048
				.BS 2
MLICLOSE00		.DA #1
				.BS 1
*--------------------------------------
MLIOPEN01		.DA #3					For DIR listing
				.DA Boot.Prefix
				.DA A2osX.OpenBuff1		
				.BS 1
MLIREAD01		.DA #4
				.BS 1
				.DA A2osX.ReadBuff		Load address Dir Blocks
				.DA $200				Maxlen of Dir Blocks (512)
				.BS 2
MLICLOSE01		.DA #1
				.BS 1
*--------------------------------------
MLIOPEN02		.DA #3
				.DA KM.Filename
				.DA A2osX.OpenBuff2
				.BS 1
MLIREAD02		.DA #4
				.BS 1
				.DA A2osX.KMLOAD		Load address for KMs
				.DA A2osX.KMMAXLEN		Maxlen of KM
				.BS 2
MLICLOSE02		.DA #1
				.BS 1
*--------------------------------------
MLIQUIT01		.DA #4
				.DA #0
				.DA 0
				.DA #0
				.DA 0
*--------------------------------------
A2osX.LOGO		>PSTR "A2osX.logo"
SYS				>PSTR "sys/"
KM.PREFIX		>PSTR "KM."
*--------------------------------------
SCR.BASEL		.HS	00.80.00.80.00.80.00.80.28.A8.28.A8.28.A8.28.A8.50.D0.50.D0.50.D0.50.D0
SCR.BASEH		.HS	04.04.05.05.06.06.07.07.04.04.05.05.06.06.07.07.04.04.05.05.06.06.07.07
*--------------------------------------
PALETTE.AUX		.HS 00080109020A030B040C050D060E070F
*--------------------------------------
				.EP
*--------------------------------------
A2osX.QC.B.Start
A2osX.QC.B.BOOT	.EQ *+4 
A2osX.QC.B.ROOT .EQ *+4+32
				.INB USR/SRC/A2OSX.S.QC
				.LIST ON
SYSTEM.END		.EQ *
				.LIST OFF
*--------------------------------------
MAN
SAVE USR/SRC/A2OSX.S
ASM
