NEW
  AUTO 3,1
*--------------------------------------
Kernel.Init2	sei
 				ldx	#$FF
				txs
				
				sta IO.SETALTZP

				stx SaveSM
				stx SaveSX

				.DO K8M=1
				sta IO.CLRALTZP
				.FIN
				
				ldx #Kernel.ZP.S-1

.1				lda Kernel.ZP,x
				sta CHARGET,x
				dex
				bpl .1

				.DO K8M=0
				sta IO.CLRALTZP
				.FIN
				
				>LDYAI MSG.Init2
				jsr PrintFYA

				.DO K8M=0
				sta IO.SETALTZP
				.FIN
				
				>LDYA PAKME.GP
				>STYA ZPInBufPtr
				>LDYAI $0140
				jsr A2osX.Unpak

				.DO K8M=0
				sta IO.CLRALTZP
				.FIN
				
				>LDYA PAKME.MAIN
				>STYA ZPInBufPtr
				>LDYAI MAIN.ORG
				jsr A2osX.Unpak

				.DO K8M=0
				>LDYA PAKME.AUX
				>STYA ZPInBufPtr
				>LDYAI $9F00
				jsr A2osX.Unpak

				>LDYAI $9F00
				>STYA ZPPtr1
				>LDYAI Mem.XHiMem
				>STYA ZPPtr2

				lda #AUX.S
				eor #$ff
				tax
				lda /AUX.S
				eor #$ff
				pha

				ldy #0

				sta IO.SETWRITEAUX

.2				inx
				bne .3

				pla
				inc
				beq .4

				pha

.3				lda (ZPPtr1),y
				sta (ZPPtr2),y
				iny
				bne .2

				inc ZPPtr1+1
				inc ZPPtr2+1
				bra .2

.4				sta IO.CLRWRITEAUX

				.FIN

				.DO K8M=0
				
				sta IO.SETALTZP
				
				.FIN

				lda IO.RRAMWRAMBNK2
				lda IO.RRAMWRAMBNK2

				>LDYA PAKME.D2
				>STYA ZPInBufPtr
				>LDYAI $D000
				jsr A2osX.Unpak

				lda IO.RRAMWRAMBNK1
				lda IO.RRAMWRAMBNK1

				>LDYA PAKME.D1
				>STYA ZPInBufPtr
				>LDYAI $D000
				jsr A2osX.Unpak

				>LDYA PAKME.E0
				>STYA ZPInBufPtr
				>LDYAI $E000
				jsr A2osX.Unpak

				lda IO.RROMBNK1

				.DO K8M=0

				sta IO.CLRALTZP

				.FIN

				cli

				jsr KConfigLoad

				lda A2osX.FSID
				beq .5
				
				sta IO.SETALTZP
				sta A2osX.FSID
				sta IO.CLRALTZP

				>LDYAI MSG.FX
				jsr PrintFYA

.5				sta IO.SETALTZP
				lda A2osX.HZ
				sta IO.CLRALTZP
				pha
				>LDYAI MSG.HZ
				jsr PrintFYA

				>LDYAI MSG.Init2.OK
				jsr PrintFYA
*--------------------------------------
				>DEBUGOA
*--------------------------------------
Kernel.Init3	sta IO.SETALTZP
				lda IO.RRAMWRAMBNK1
				lda IO.RRAMWRAMBNK1

				lda #$ff				Make sure we are in Kernel Mode during setup
				sta IRQ.InKernel
				stz IRQ.InLib

				lda /K.VER
				sta A2osX.KVER
				lda #K.VER
				sta A2osX.KVER+1

				sta IO.SETALTCHAR
				sta IO.SETMIXED
				sta IO.CLRHIRES
				sta IO.SET80DISP
				sta IO.SET80STORE
				sta IO.CLRPAGE2

				lda IO.RDIOUDIS
				sta IO.SETIOUDIS
				sta IO.SETDHIRES
				bmi .10

				sta IO.CLRIOUDIS

.10				jsr MemMgrInit

				>LDYAI S.PS				Select Process 0 (Kernel)
				jsr K.GetMem0
				>STYA pPs
				stx PSs.hPS

				jsr Mem.SetOwner
				lda #1
				sta CORE.PSCount
				>LDYAI K.STACKTOP
				>STYA pStack
				sta pBase+1

				jsr SysCDEVInit
				bcs *

*				sta IO.CLRMIXED

				>PUSHWI DEV.CONSOLE
				>PUSHBI	O.RDWR
				>PUSHBI S.FI.T.TXT
				>PUSHWZ					Aux type
				>SYSCALL2 fopen
				bcs *

				ldy #S.PS.hStdIn
				sta (pPS),y
				iny						#S.PS.hStdOut
				sta (pPS),y
				iny						#S.PS.hStdErr
				sta (pPS),y
*--------------------------------------
Kernel.Init3C	>LDYAI MSG.Init3
				>SYSCALL2 PutS
			
				jsr HW.Detect

				jsr CPU.Init

				jsr CPU.Init.Z80

				jsr DevMgrInit

				jsr IrqMgrInit
				bcs *

				jsr EvtMgrInit

				jsr TskMgrInit
				bcs *

				jsr PwdMgrInit

				>PUSHWI MSG.EXEC
				>PUSHWI SBININITD
				>PUSHBI 2
				>SYSCALL PrintF

				>PUSHWI SBININITD
				>PUSHBI 0				PS Flags

				>SYSCALL2 ExecV
				bcs Kernel.Init3.Err

				>LDYAI MSG.Init3.OK
				>SYSCALL2 PutS
				>DEBUGOA

				jmp CORE.Run
Kernel.Init3.Err
				pha
				>PUSHWI MSG.StartupErr
				pla
				>PUSHA
				>PUSHBI 1
				>SYSCALL2 printf
				bra *					No need to discard Expanded CMDLINE
*--------------------------------------
* INIT2 Subs
*--------------------------------------
KConfigLoad		>LDYAI MSG.KCREAD
				jsr PrintFYA

				jsr GP.MLIENTRY			Bypass ATALK
				.DA #MLI.GFINFOEX
				.DA pMLIGFIEX
				bcc .1

				eor #MLI.E.BADCALL
				beq .2					regular ProDOS

				ror A2osX.FSID			Bad file, but ProDOS.FX detected
				bra .9

.1				sec
				ror A2osX.FSID			File Present & ProDOS.FX detected

.2				jsr MLI
				.DA #MLI.OPEN
				.DA pMLIOPEN
				bcs .9

				lda pMLIOPEN+5
				sta pMLIREAD+1
				sta pMLICLOSE+1
				jsr MLI
				.DA #MLI.READ
				.DA pMLIREAD
				php
				pha
				jsr MLI
				.DA #MLI.CLOSE
				.DA pMLICLOSE
				pla
				plp
				bcs .9
				
				ldx #15
				
.3				lda A2osX.KCONFIG,x
				sta IO.SETALTZP
				sta A2osX.KCONFIG,x
				sta IO.CLRALTZP
				dex
				bpl .3

				>LDYAI MSG.KCREAD.OK
				bra .8

.9				>LDYAI MSG.KCREAD.KO

.8				jsr PrintFYA
				
				rts
*--------------------------------------
* INIT3 Subs
*--------------------------------------
MemMgrInit		>LDYAI Mem.MHiMem
				>STYA Mem.HiMem
				>STYA Mem.Free
				>LDYAI Mem.MLoMem
				>STYA Mem.LoMem
				stz Mem.LastSlot		Reserve Slot #0

				sta IO.SETWRITEAUX

				>LDYAI Mem.XLoMem
				>STYA Mem.LoMem

				stz Mem.LastSlot		Reserve Slot #0
				sta IO.CLRWRITEAUX

				ldx #K.FILE.MAX-1

.1				stz FILEs.hFD,x
				stz FILEs.hName,x
				stz FILEs.oCnt,x
				dex
				bpl .1

				ldx #K.PS.MAX*4-1

.3				stz PSs.PID,x
				dex
				bpl .3
				
				ldx #K.FD.MAX-1
				
.4				stz FDs.pLO,x
				stz FDs.pHI,x
				dex
				bpl .4

				ldx #K.EVT.MAX*S.EVT-1

.5				stz K.S.EVT,x
				dex
				bpl .5

				ldx #K.USR.MAX*2-1

.6				stz USRs.hSID,x
				dex
				bpl .6

				ldx #K.PFT.MAX*4-1

.7				stz PFTs.pLO,x
				dex
				bpl .7

				rts
*--------------------------------------
SysCDEVInit		>PUSHWI FD.NULL
				>PUSHWI FD.NULL.NAME
				>SYSCALL2 MKDev
				bcs .9

				>LDYAI Mem.XHiMem
				>STYA FD.TTY+S.FD.DEV.DCBPTR

				jsr SysCDEVInit.TTYPtrs

				>PUSHWI FD.TTY
				>PUSHWI FD.CONSOLE.NAME
				>SYSCALL2 MKDev
				bcs .9

				ldx A2osX.TTYDEVS

.1				phx
				inc FD.TTY+S.FD.DEV.DEVID
				inc FD.TTY.NAME+3
				jsr SysCDEVInit.TTYPtrs

				>PUSHWI FD.TTY
				>PUSHWI FD.TTY.NAME
				>SYSCALL2 MKDev
				plx
				bcs .9

				dex
				bne .1

*				clc
.9				rts
*--------------------------------------
SysCDEVInit.TTYPtrs
				lda FD.TTY+S.FD.DEV.DCBPTR
				sec
				sbc #80*24+S.DCB.TTY
				sta FD.TTY+S.FD.DEV.DCBPTR
				sta ZPPtr1
				tay

				lda FD.TTY+S.FD.DEV.DCBPTR+1
				sbc /80*24+S.DCB.TTY
				sta FD.TTY+S.FD.DEV.DCBPTR+1
				sta ZPPtr1+1

				sta IO.SETWRITEAUX

				>STYA Mem.HiMem
				>STYA Mem.Free

				ldy #S.DCB.TTY-1

				lda #0

.1				sta (ZPPtr1),y
				dey
				bne .1

				lda FD.TTY+S.FD.DEV.DEVID
				sta (ZPPtr1)

				sta IO.CLRWRITEAUX

				rts
*--------------------------------------
* http://www.1000bit.it/support/manuali/apple/technotes/misc/tn.misc.02.html
* https://mirrors.apple2.org.za/apple.cabi.net/FAQs.and.INFO/A2.TECH.NOTES.ETC/A2.CLASSIC.TNTS/a2misc007(1).htm
*--------------------------------------
HW.Detect		>PUSHWI MSG.HW
				>PUSHBI 0
				>SYSCALL2 printf

				bit IO.RROMBNK1

				stz A2osX.HWT
				stz A2osX.HWSubT

				sec
				jsr $FE1F				ROM.IDROUTINE		IIgs ?
				bcs .2

				lda #A2osX.HWT.IIgs
				sta A2osX.HWT

				cpy #2
				bne .1

				iny

.1				sty A2osX.HWSubT
				bra .8

.2				lda $FBC0				ROM.ZIDBYTE
				beq .4					//c

				ldx #A2osX.HWT.IIe
				stx A2osX.HWT
				cmp #$EA
				beq .8					//e OLD ROMs

				inc A2osX.HWSubT		//e Enh ROMs
				lda $FBDD
				cmp #2
				bne .8

				inc A2osX.HWSubT		//e LC card
				bra .8

.4				lda #A2osX.HWT.IIc
				sta A2osX.HWT
				lda $FBBF
				sta A2osX.HWSubT

.8				bit IO.RRAMWRAMBNK1
				bit IO.RRAMWRAMBNK1

				>PUSHWI MSG.HW.TypeSubT
				>PUSHB A2osX.HWT
				>PUSHB A2osX.HWSubT
				>PUSHBI 2
				>SYSCALL2 printf
				rts
*--------------------------------------
CPU.Init		>PUSHWI MSG.CPU
				>PUSHBI 0
				>SYSCALL2 printf
				jsr Detect6502			A = CPU type
				sta A2osX.CPUTYPE
				and #$F
				asl
				tax
				ldy MSG.CPUTYPE,x
				lda MSG.CPUTYPE+1,x
				>SYSCALL2 puts

				>PUSHWI MSG.CPU.SPEED
				>PUSHB A2osX.HZ
				>PUSHBI 1
				>SYSCALL2 printf

				stz A2osX.CPUSPEED+1

				ldy #0

				php
				sei

				lda A2osX.HWT
				cmp #A2osX.HWT.IIc
				beq CPU.Init2c
*--------------------------------------
* VBL = 20.000us 50hz, 16.666 60hz
*--------------------------------------
				ldx A2osX.HZ

.1				bit IO.VBL				//e : Wait VBL high....
				bpl .1

.2				lda IO.VBL				//e : wait VBL low
				bmi .2

.3				pha						(3)

				lda CPU.Hz2L-5,x		(5) 50Hz = 36, 60Hz = 29

.4				dec						(2)
				bne .4					(3)

				iny						(2)	38*5+10 = 200us
				cpy #100				(3)
				bne .5					(3)

				inc A2osX.CPUSPEED+1
				ldy #0

.5				pla

				eor IO.VBL
				bpl .3

				lda IO.VBL
				bmi .3

				bra CPU.InitOK
*--------------------------------------
CPU.Init2c		lda IO.RDIOUDIS
				pha
				sta IO.CLRIOUDIS

				lda IO.IIC.RDVBLMSK
				pha						Save VBLIRQ enable
				sta IO.IIC.ENVBL

				lda IO.IIC.CLRVBLIRQ

.1				bit IO.VBL				Wait VBL high....
				bpl .1

				bit IO.IIC.CLRVBLIRQ	clear //c IRQ

.3				ldx A2osX.HZ

.4				pha						(3)
				pla						(4)
				pha						(3)
				pla						(4)

				dex
				bne .4

				iny
				cpy #100
				bne .5

				inc A2osX.CPUSPEED+1
				ldy #0

.5				bit IO.VBL				Wait VBL high....
				bpl .3

				bit IO.IIC.CLRVBLIRQ	clear //c IRQ

				pla						Disable VBLIRQ ?
				bpl .8

				sta IO.IIC.DISVBL

.8				pla
				bpl CPU.InitOk

				sta IO.SETIOUDIS
*--------------------------------------
CPU.InitOk		plp

				sty A2osX.CPUSPEED

				>PUSHWI MSG.CPU.SPEEDOK
				lda A2osX.CPUSPEED+1
				>PUSHA					push CPU speed HI
				lda A2osX.CPUSPEED
				>PUSHA					push CPU speed LO
				>PUSHBI 2
				>SYSCALL2 printf
				rts
*--------------------------------------
CPU.Hz2L		.DA #36,#29
*--------------------------------------
CPU.Init.Z80	>PUSHWI MSG.Z80
				>PUSHBI 0
				>SYSCALL2 printf
				jsr DetectZ80
				bcs .9

				sta A2osX.Z80SLOT
				tax
				lda #A2osX.S.Z80
				sta A2osX.S-1,x

				>PUSHWI MSG.Z80.OK
				lda A2osX.Z80SLOT
				>PUSHA
				>PUSHBI 1
				>SYSCALL2 printf
				rts

.9				>LDYAI MSG.Z80.KO
				>SYSCALL2 puts
				rts
*--------------------------------------
* Out:
*  A = $60, 6502
*  A = $61, 65C02
*  A = $62, 65R02
*  A = $63, 65816
*--------------------------------------
Detect6502		ldy #0					Test 6502 BCD bug
				sed
				lda #$99
				clc
				adc #$01
				cld
				bmi .9					BUG!, we have 6502

				ldy #3					test 65C816 TYX Instruction
				ldx #0
				.OP 65816
				tyx						TYX: if 65C816, x becomes non-zero
				.OP 65C02
				bne .9					worked! 65C816

				dey						y=2 -> R65C02
				ldx $EA					save zp $EA
				stz $EA					store 0 in $EA
*				.OP 65R02
				.HS F7EA				For compatibility with A2osX.ASM
*				smb 7,$EA				if not Rockwell, assemble as nop nop
*				.OP	65C02
				asl $EA					cs if Rockwell
				stx $EA					restore zp $EA
				bcs .9
				dey						y=1 -> 65C02
.9				tya
				ora #$60
				rts
*--------------------------------------
* Out:
*  A = Z80 Slot
*--------------------------------------
DetectZ80		ldx #Z80.300.L

.1				lda Z80.300-1,x
				sta $300-1,x			0F300H for Z80
				dex
				bne .1

				ldx #Z80.1000.L

.2				lda $1000-1,x
				sta Z80.SAVE-1,x
				lda Z80.1000-1,x
				sta $1000-1,x			00000H for Z80
				dex
				bne .2

				stz ZPPtr1
				lda #$C1

				sta ZPPtr1+1

.3				sta (ZPPtr1)			to Z80.....
				lda $100B				.....back
				bmi .8

				inc ZPPtr1+1
				lda ZPPtr1+1
				cmp #$C8
				beq .9					CS

				clc
				adc #$20
				sta $302
				bra .3

.8				lda ZPPtr1+1
				and #$0F
				clc

.9				php
				pha
				
				ldx #Z80.1000.L

.90				lda Z80.SAVE-1,x
				sta $1000-1,x
				dex
				bne .90
				
				pla
				plp
				rts
*--------------------------------------
DevMgrInit		>LDYAI MSG.DEV
				>SYSCALL2 PutS

				jsr DevMgr.VXD

				lda GP.DEVCNT
				sta .1+1

.1				ldx #$ff				SELF MODIFIED
				lda GP.DEVLST,x
				and #$F0
				asl
				php						Save Drive 1/2 in C
				ror						DSSS0000
				lsr
				lsr
				lsr

				tax						000DSSS0
				lsr						0000DSSS
				and #$7
				sta FD.BDEV+S.FD.DEV.BUSID

				ora #$30
				sta FD.BDEV.NAME+1		SnDy

				lda GP.DEVPTRS,x
				sta FD.BDEV+S.FD.DEV.BUSPTR
				lda GP.DEVPTRS+1,x
				sta FD.BDEV+S.FD.DEV.BUSPTR+1

				sta ZPPtr1+1
				stz ZPPtr1				Cn00

				plp
				lda #0
				adc #1					Make DevID 1 or 2
				sta FD.BDEV+S.FD.DEV.DEVID
				ora #$30
				sta FD.BDEV.NAME+3		SxDn
				lda ZPPtr1+1

				cmp #$FF				/RAM is always $FF00
				bne .2

				ldx #2
				jsr DevMgr.AddBDev
				bra .8					keep slot free

.2				cmp #$D0				Disk II Driver is $D000
				bne .3

				lda #280
				sta FD.BDEV+S.FD.DEV.SIZE
				lda /280
				sta FD.BDEV+S.FD.DEV.SIZE+1
				
				ldx #0
				jsr DevMgr.AddBDev
				lda #A2osX.S.D2
				bra .7					Go mark slot as USED

.3				bcs .53					> $D0, Some SP remapped DRV

				cmp #$C0
				bcs .4					$C0 .. $CF, in slot ROM

				ldx #4
				jsr DevMgr.AddBDev		Outside ROM/LC space, User loaded BLK dev
				bra .8					keep slot free

.4				ldx #BDEV.SIG-BDEV.OFS-1

.5				ldy BDEV.OFS,x		Check if there is a smartport firmware
				lda (ZPPtr1),y
				cmp BDEV.SIG,x
				bne .8					not a Disk Controller...

				dex
				bpl .5

				ldy #7
				lda (ZPPtr1),y
				bne .6					$Cn07=00 : SmartPort

				ldy #$ff
				lda (ZPPtr1),y
				clc
				adc #3					Compute smartport entry point
				sta FD.BDEV+S.FD.DEV.BUSPTR
				
				ldx #10
				jsr DevMgr.AddBDev
				lda #A2osX.S.SP
				bra .7

.53				ldx #12
				jsr DevMgr.AddBDev		Remapped SP devices....
				bra .8					don't mark slot as busy

.6				ldy #$FC				Size in Blk
				lda (ZPPtr1),y
				sta FD.BDEV+S.FD.DEV.SIZE
				iny
				lda (ZPPtr1),y
				sta FD.BDEV+S.FD.DEV.SIZE+1
				
				lda FD.BDEV+S.FD.DEV.BUSID	00000SSS
				eor FD.BDEV+S.FD.DEV.BUSPTR+1 Cn
				cmp #$C0
				beq .70
				
				ldx #8
				jsr DevMgr.AddBDev		Remapped BLK devices....
				bra .8					don't mark slot as busy

.70				ldx #6
				jsr DevMgr.AddBDev
				lda #A2osX.S.BLK

.7				ldx FD.BDEV+S.FD.DEV.BUSID
				sta A2osX.S-1,x

.8				dec .1+1
				bmi .80

				jmp .1

.80				clc
				rts
*--------------------------------------
DevMgr.AddBDev	stx FD.BDEV+S.FD.DEV.TYPE

				lda DRV.BDEVs,x
				sta FD.BDEV+S.FD.DEV.DRVPTR
				lda DRV.BDEVs+1,x
				sta FD.BDEV+S.FD.DEV.DRVPTR+1

				>PUSHWI MSG.BDEV
				>PUSHWI FD.BDEV.NAME
				>PUSHW FD.BDEV+S.FD.DEV.BUSPTR
				>PUSHW MSG.BDEVs,x
				>PUSHBI 6
				>SYSCALL2 printf

				>PUSHWI FD.BDEV
				>PUSHWI FD.BDEV.NAME
				>SYSCALL2 MKDev

				rts
*--------------------------------------
DevMgr.VXD		>LDYAI VSD.PATCH

				jsr .10
				bcc .8

				>LDYAI VED.PATCH
				jsr .10
				bcs .9

.8				ldy #4
				lda (ZPPtr1),y
				sec
				sbc #1
				sta .81+1

				iny
				lda (ZPPtr1),y
				sbc #0
				sta .81+2

				php
				sei

				bit IO.RRAMWRAMBNK2
				bit IO.RRAMWRAMBNK2
				sta IO.CLRALTZP
.81				ldx $FFFF				SELF MODIFIED : Slot0n
				sta IO.SETALTZP
				bit IO.RRAMWRAMBNK1
				bit IO.RRAMWRAMBNK1

				lda #A2osX.S.VXD
				sta A2osX.S-1,x

				plp

.9				rts
*--------------------------------------
.10				>STYA ZPPtr1

				ldy #9					10 bytes

.11				lda $BF41,y
				cmp (ZPPtr1),y
				bne .19

				dey
				bpl .11

				clc
				rts

.19				sec
				rts
*--------------------------------------
VSD.PATCH		bit IO.RRAMWRAMBNK2
				jsr $D002
				bit IO.RRAMWRAMBNK1
				rts
*--------------------------------------
VED.PATCH		bit IO.RRAMWRAMBNK2
				jsr $D401
				bit IO.RRAMWRAMBNK1
				rts
*--------------------------------------
* Setup AuxLC $FFFE->Kernel IRQ Handler
* if irq not handled, jmp (S.IrqMgrOldFFFE)
*--------------------------------------
IrqMgrInit		>LDYAI MSG.IRQ
				>SYSCALL2 puts

				php
				sei
				>LDYA $FFFE
				>STYA IRQ.JMP+1
				>LDYAI IRQ.Handler
				>STYA $FFFE

				>LDYAI GP.IRQ
				>STYA K.MLI.PARAMS+2

				>MLICALL MLI.ALLOCIRQ
				bcs .9

				lda K.MLI.PARAMS+1
				sta IRQ.INTNUM
*--------------------------------------
				lda A2osX.HWT
				cmp #A2osX.HWT.IIgs
				bne .1

				.OP 65816

				clc
				xce		  				go to native mode
				rep #$30				long M,X

				pea $000C				Vertical blanking interrupt handler
				pea $0000
				pea $0000
				>TKCALL GetVector
*			bcs *
				pla
				sta IRQ.GSVect+2

				pla
				sta IRQ.GSVect

				pea $000C				Vertical blanking interrupt handler
				pea $0000
				pea CORE.IRQGS
				>TKCALL SetVector
*			bcs *

				pea $0002				Enable vertical blanking interrupts
				>TKCALL IntSource
*			bcs *

				sep #$30				short M,X
				sec
				xce		  				back to emulation mode

				.OP 65C02

				jsr IrqMgr.HZ2Tick

				>LDYAI CORE.IRQGS
				jsr IrqMgr.SetV

				>LDYAI CORE.IRQGSOff
				>STYA CORE.Quit+3

				>LDYAI MSG.IRQGS
				ldx #"G"
				sec
				bra IrqMgrInit.8

.9				plp
				sec
				rts
*--------------------------------------
.1				cmp #A2osX.HWT.IIc
				beq .2

				jsr IrqMgr.TClk
				bcs .3

				>LDYAI CORE.IRQTK
				jsr IrqMgr.SetV

				>LDYAI CORE.IRQTKOff
				>STYA CORE.Quit+3

				>LDYAI MSG.IRQTK
				ldx #"C"
				sec
				bra IrqMgrInit.8

.2				sta IO.CLRPAGE2
				lda #$60
				sta $5fa				//c EXTINT COM1:Dont buffer but get IRQ
				sta $5fb				//c EXTINT COM2:Dont buffer but get IRQ

				stz $5FA				//c TYPHED no keyboard buffering

				stz $4FF				//c ROM FF: no ACIABUF
				stz $4FC				//c ROM 0X: no ACIABUF

.3				jsr IrqMgr.Mouse
				bcs .7

				>LDYAI CORE.IRQM
				jsr IrqMgr.SetV

				>LDYAI CORE.IRQMOff
				>STYA CORE.Quit+3

				>LDYAI MSG.IRQM
				ldx #"V"
				sec
				bra IrqMgrInit.8

.7				jsr IrqMgr.HZ2Tick

				ldx #"P"
				>LDYAI MSG.IRQP
				clc

IrqMgrInit.8	ror A2osX.IRQMode
				stx IRQ.Mode
				>SYSCALL2 puts

				plp
				clc
				rts
*--------------------------------------
IrqMgr.SetV		>STYA IRQ.JSR+1
				>STYA GP.IRQ.JSR+1
				rts
*--------------------------------------
IrqMgr.TClk		stz ZPPtr1				Try finding a TClock Card...
				lda #$C1
				sta ZPPtr1+1

.1				ldx #TClock.SIG-TClock.OFS-1

.2				ldy TClock.OFS,x
				lda (ZPPtr1),y
				cmp TClock.SIG,x
				bne .3

				dex
				bpl .2

				bra .4

.3				inc ZPPtr1+1			no match, try next slot....
				lda ZPPtr1+1
				cmp #$C8
				bne .1

*				sec
				rts

.4				lda ZPPtr1+1			get $Cn
				and #$f
				tax						get $0n
				asl
				asl
				asl
				asl
				tay						get $n0
				sty IRQ.n0
				lda #A2osX.S.CLK
				sta A2osX.S-1,x

				lda #$40+$20			Enable interrupt + 64Hz
				sta $c080,y

				lda #$40
				sta $478,x
				sta $7f8,x

				lda #64
				sta CORE.TickPerSec
				sta CORE.TickSec

				lda #11
				sta CORE.TickPer10t
				sta CORE.Tick10t
				dec A2osX.IRQMode

				clc
				rts
*--------------------------------------
IrqMgr.Mouse	stz ZPPtr1				Try finding a Mouse Card...
				lda #$C1
				sta ZPPtr1+1

.1				ldx #Mouse.SIG-Mouse.OFS-1

.2				ldy Mouse.OFS,x
				lda (ZPPtr1),y
				cmp Mouse.SIG,x
				bne .3

				dex
				bpl .2

				bra .4

.3				inc ZPPtr1+1			no match, try next slot....
				lda ZPPtr1+1
				cmp #$C8
				bne .1

*				sec
				rts

.4				lda ZPPtr1+1

				sta .11+2
				sta CORE.IRQMOff.1+2
				sta CORE.IRQM+2
				sta CORE.IRQM.2+2
				and #$0f
				sta IRQ.0n
				asl
				asl
				asl
				asl

				sta IRQ.n0

				bit IO.RROMBNK2			Renable ROM to allow MOUSE II firmware works properly
				ldy #INITMOUSE
				jsr .10

				ldy #SETMOUSE
				lda (ZPPtr1),y
				sta CORE.IRQMOff.1+1
				lda #MOUSEMODx.INTVBL+MOUSEMODx.ACTIVE
				jsr .10

				bit IO.RRAMWRAMBNK1		Back To LC Ram
				bit IO.RRAMWRAMBNK1
				ldy #SERVEMOUSE
				lda (ZPPtr1),y
				sta CORE.IRQM+1

				ldy #READMOUSE
				lda (ZPPtr1),y
				sta CORE.IRQM.2+1

				jsr IrqMgr.HZ2Tick
				dec A2osX.IRQMode

				clc
				rts

.10				pha
				lda (ZPPtr1),y
				sta .11+1
				pla
				ldx .11+2				Cn
				ldy IRQ.n0

.11				jmp $ffff				SELF MODIFIED
*--------------------------------------
IrqMgr.HZ2Tick	lda A2osX.HZ
				sta CORE.TickPer10t
				sta CORE.Tick10t
				asl
				asl
				clc
				adc A2osX.HZ

				asl

				sta CORE.TickPerSec
				sta CORE.TickSec

				rts
*--------------------------------------
EvtMgrInit		>LDYAI MSG.EVT
				>SYSCALL2 puts

				stz CORE.EvtCount

				lda /K.S.EVT
				sta pEvent+1
				clc
				rts
*--------------------------------------
TskMgrInit		>LDYAI MSG.TSK
				>SYSCALL2 puts

				>LDYAI K.ENV.SIZE		get a buffer for ENV
				>SYSCALL NewStkObj		Buffer in AUX mem

				bcs .99

				>STYA ZPPtr1

				txa

				ldy	#S.PS.hENV
				sta (pPS),y

				sta IO.SETWRITEAUX
				lda #0
				sta (ZPPtr1)			make sure blank!!

				sta IO.CLRWRITEAUX

				>PUSHWZ
				>PUSHWZ
				>SYSCALL OpenSession
				bcs .99

				sta USRs.hSID

				ldy #S.PS.hStdIn
				lda (pPS),y
				sta USRs.hFile

				lda #1
				ldy #S.PS.hSession
				sta (pPS),y

				>LDYAI $2E0				Get ROOT from QC
				>SYSCALL2 strdup
.99				bcs .9

				txa
				ldy #S.PS.hCWD
				sta (pPS),y

				>PUSHWI I.ENV.BOOT
				>PUSHWI $2C0			Get BOOT from QC
				>SYSCALL2 SetEnv
				bcs .9

				>PUSHWI I.ENV.ROOT
				>PUSHWI $2E0			Get ROOT from QC
				>SYSCALL2 SetEnv
				bcs .9

				>PUSHWI I.ENV.PATH
				>SYSCALL2 PutEnv
				bcs .9

				>PUSHWI I.ENV.LIB
				>SYSCALL2 PutEnv
				bcs .9

				>PUSHWI I.ENV.DRV
				>SYSCALL2 PutEnv
.9				rts
*--------------------------------------
PwdMgrInit		>LDYAI MSG.PWD
				>SYSCALL2 puts

				>LDYAI PWD.ETCPASSWD
				jsr .1
				bcs .9

				stx PWD.hUsrDB
				>STYA PWD.UsrDBSize

				>LDYAI PWD.ETCGROUP
				jsr .1
				bcs .9

				stx PWD.hGrpDB
				>STYA PWD.GrpDBSize

*				clc
.9				rts

.1				>PUSHYA
				>PUSHBI	O.RDONLY
				>PUSHBI S.FI.T.TXT
				>PUSHWZ					Auxtype=0000
				>SYSCALL2 LoadStkObj
				rts
*--------------------------------------
* Modified CHARGET/CHARGOT for ROM CALL
* CHRGET         .EQ $B1 - C8
*--------------------------------------
Kernel.ZP		.PH $B1
CHARGET			inc TXTPTR
				bne CHARGOT
				inc TXTPTR+1
CHARGOT			lda $ffff
				beq ZP.IsDigit.9
ZP.IsDigit		cmp #'0'
				bcc ZP.IsDigit.9
				cmp #'9'+1
				rts
ZP.IsDigit.9	sec
				rts
				.EP
Kernel.ZP.S		.EQ *-Kernel.ZP
*--------------------------------------
Mouse.OFS		.HS 05070B0CFB
Mouse.SIG		.HS 38180120D6
*--------------------------------------
TClock.OFS		.HS 00020406
TClock.SIG		.HS	08285870
*--------------------------------------
BDEV.OFS		.HS 010305
BDEV.SIG		.HS 200003
*--------------------------------------
MSG.Init2		.CZ "A2osX[Stage2]:Init\rRelocating Kernel...\r"
MSG.KCREAD		.CZ "Reading Kernel Config File..."
MSG.KCREAD.OK	.CZ "OK\r"
MSG.KCREAD.KO	.CZ "\rError While Reading Kernel Config File, Using Default.\r"
MSG.HZ			.CZ "Kernel SYS Timer Set For %d0 hz Machine.\r"
MSG.FX			.CZ "!!!Running under ProDOS.FX!!! Enabling lowercase mode.\r"
MSG.Init2.OK	.CZ "A2osX[Stage2]:Complete."
*--------------------------------------
MSG.Init3		.CZ "A2osX[Stage3]:Init *** Press Ctrl-R for entering ROOT Mode ***"
MSG.DEV			.AZ "Device Manager..."
MSG.BDEV		.CZ "  Adding Device: /dev/%s, Addr:%H, Type: %s\r\n"
MSG.BDEVs		.DA MSG.BDEV.D2
				.DA MSG.BDEV.RAM
				.DA MSG.BDEV.USR
				.DA MSG.BDEV.BLK
				.DA MSG.BDEV.RBLK
				.DA MSG.BDEV.SP
				.DA MSG.BDEV.RSP
MSG.BDEV.D2		.AZ "Disk ]["
MSG.BDEV.RAM	.AZ "/RAM Disk"
MSG.BDEV.USR	.AZ "User Block"
MSG.BDEV.BLK	.AZ "Generic Block"
MSG.BDEV.RBLK	.AZ "Generic Block (Remapped)"
MSG.BDEV.SP		.AZ "SmartPort"
MSG.BDEV.RSP	.AZ "SmartPort (Remapped)"
DRV.BDEVs		.DA DRV.BLK				D2
				.DA DRV.BLK				RAM
				.DA DRV.BLK				USR
				.DA DRV.BLK
				.DA DRV.BLK				RBLK
				.DA DRV.SP
				.DA DRV.BLK				RSP
*--------------------------------------
MSG.IRQ			.AZ "Interrupt Manager..."
MSG.IRQGS		.AZ "  Apple IIgs VBL IRQ Activated."
MSG.IRQM		.AZ "  Mouse Card VBL IRQ Activated."
MSG.IRQTK		.AZ "  Clock Card IRQ Activated."
MSG.IRQP		.AZ "  Polling Mode."
MSG.EVT			.AZ "Event Manager..."
MSG.TSK			.AZ "Task Manager..."
MSG.PWD			.AZ "Password Manager..."
MSG.EXEC		.AZ "Executing %s..."
MSG.StartupErr	.CZ "Failed : [$%h]\r\n"
MSG.Init3.OK	.CZ "OK\r\nA2osX[Stage3]:Complete."
*--------------------------------------
MSG.HW			.AZ "Checking Hardware..."
MSG.HW.TypeSubT	.CZ "Type=%h,SubT=%h\r\n"
MSG.CPU			.AZ "Checking CPU..."
MSG.CPUTYPE		.DA MSG.6502
				.DA MSG.65C02
				.DA MSG.65R02
				.DA MSG.65816
MSG.6502		.AZ "6502."
MSG.65C02		.AZ "65C02."
MSG.65R02		.AZ "65R02."
MSG.65816		.AZ "65816."
MSG.CPU.SPEED	.AZ "Checking CPU Speed (%d0Hz)..."
MSG.CPU.SPEEDOK	.CZ "%d.%02d Mhz.\r\n"
MSG.Z80			.AZ "Checking Z80 CPU..."
MSG.Z80.OK		.CZ "Detected In Slot %d.\r\n"
MSG.Z80.KO		.AZ "Not Detected."
*--------------------------------------
DEV.CONSOLE		.AZ "/dev/console"
RAMx			.AZ "/RAMx"
I.ENV.BOOT		.AZ "BOOT"
I.ENV.ROOT		.AZ "ROOT"
I.ENV.PATH		.AZ "PATH=${BOOT}sbin/:${BOOT}bin/"
I.ENV.LIB		.AZ "LIB=${BOOT}lib/"
I.ENV.DRV		.AZ "DRV=${BOOT}drv/"
SBININITD		.AZ "${ROOT}sbin/initd"
				.DA #0					end of ARGV for ExecV
*--------------------------------------
pMLIGFIEX		.DA #2
				.DA FNKConfig
				.BS S.FIEX
*--------------------------------------
pMLIOPEN		.DA #3
				.DA FNKConfig
				.DA $B000
				.BS 1
FNKConfig		.PS "A2osX.kconfig"
*--------------------------------------
pMLIREAD		.DA #4
				.BS 1
				.DA A2osX.KCONFIG
				.DA 16
				.BS 2
*--------------------------------------
pMLICLOSE		.DA #1
				.BS 1
*--------------------------------------
FD.NULL			.DA #S.FD.T.CDEV
				.DA #0					HANDLER
				.DA #$F					BUSID
				.DA #$F					DEVID
				.DA 0					BUSPTR
				.DA DRV.NULL
				.DA 0					DCBPTR
				.DA 0					BUFPTR
				.BS 4
*--------------------------------------
FD.TTY			.DA #S.FD.T.CDEV
				.DA #0					HANDLER
				.DA #$F					BUSID
				.DA #0					DEVID
				.DA 0					BUSPTR
				.DA DRV.TERM
				.BS 2					DCBPTR
				.BS 2					BUFPTR
				.BS 4
*--------------------------------------
FD.BDEV			.DA #S.FD.T.BDEV
				.DA #0					HANDLER
				.DA #$F					BUSID
				.DA #0					DEVID
				.BS 2					BUSPTR
				.BS 2					DRVPTR
				.DA 0					DCBPTR
				.DA 0					BUFPTR
				.DA 0					SIZE
				.DA 0
*--------------------------------------
FD.NULL.NAME	.AZ "null"
FD.CONSOLE.NAME	.AZ "console"
FD.TTY.NAME		.AZ "tty0"
FD.BDEV.NAME	.AZ "s1d1"
*--------------------------------------
* Z80 Reserved
*--------------------------------------
*Z80CODE		.EQ $300				0F300h
*Z80STACK		.EQ $3CF				0F3CFh
*--------------------------------------
Z80.300			.HS	3200E1						LD (0E100H),A		Back to 6502

				.HS	C300F3						JP 0F300h
Z80.300.L		.EQ *-Z80.300
*--------------------------------------
Z80.1000		.HS	31CFF3				START:	LD SP,0F3CFh		Init Stack
				.HS	3EFF						LD A,0FFH			Set Flag
				.HS	320B00						LD (FLAG),A
				.HS	C300F3						JP 0F300h
				.HS 00					FLAG:	.DB 0
Z80.1000.L		.EQ *-Z80.1000
*--------------------------------------
Z80.SAVE		.EQ *
*--------------------------------------
MAN
SAVE usr/src/sys/kernel.s.init
LOAD usr/src/sys/kernel.s
ASM
