source/page00/system.asm
author brandonlw
Thu, 15 May 2014 05:38:01 -0400
changeset 40 ba7bd14b7d7b
parent 39 4651e87022c7
child 41 9ef17625274f
permissions -rw-r--r--
Minor cleanup

coorMon:
		res		g_style_active, (iy+sGrFlags)
		call	j_CursorOff
		call	j_COORDISP
Mon:	call	j_RunIndicOff
		call	j_CursorOn
		res		displayCoords, (iy+newDispF)
		bit		sendFinalPacketOnMon, (iy+linkFlags2)
		jr		z, Mon_NoPacketSend
		di
		res		sendFinalPacketOnMon, (iy+linkFlags2)
		ld		hl, Mon_ResetIndicOnly
		call	pushErrorHandleR
		set		indicOnly, (iy+indicFlags)
		ld		a, (mcxCurApp)
		cp		kError
		jr		nz, {+}
		ld		a, (errNo)
		call	j_SendSkipExitPacket
		jr		Mon_LinkEnd
+:		call	j_SendAck
Mon_LinkEnd:
		call	popErrorHandleR
Mon_ResetIndicOnly:
		res		indicOnly, (iy+indicFlags)
		ei
Mon_NoPacketSend:
		xor		a
		ld		(kbdKey), a
		ld		(keyExtend), a
		call	j_getkey
monForceKey:
		res		recallingPrgmName, (iy+rclFlag2)
		res		recallingListName, (iy+rclFlag3)
		ld		(kbdKey), a
		bit		allowCursor, (iy+plotFlags)
		jr		z, {+}
		res		curAble, (iy+curFlags)
		bit		curOn, (iy+curFlags)
		call	z, j_BLINKGCUR
		ld		a, (kbdKey)
		jr		{++}
+:		call	j_CursorOff
++:		call	j_RunIndicOn
		or		a
		jr		nz, {++}
		;ON was pressed; now what?
		bit		appWantIntrpt, (iy+appFlags)
		jr		nz, {+}
		res		onInterrupt, (iy+onFlags)
		jr		jpCoorMon
+:		;Return kEnter instead
		ld		a, kEnter
		jp		sendKPress
++:		cp		kVarx
		jr		nz, monNotVarKey
		ld		a, kTheta
		call	IsFunctionMode
		jr		nz, {+}
		ld		a, kCapT
		call	IsParametricMode
		jr		nz, {+}
		ld		a, kCapX
		call	IsFuncMode
		jr		nz, {+}
		ld		a, kRecn
		ld		(keyExtend), a
		ld		a, kExtendEcho
+:		ld		(kbdKey), a
monNotVarKey:
		res		appIgnoreKey, (iy+APIFlg)
		res		sendKeyStraightTocxMain, (iy+APIFlg)
		bit		rawKeyHookActive, (iy+hookflags2)
		jr		z, {+}
		call	j_call_rawkey_hook
		bit		appIgnoreKey, (iy+APIFlg)
		jr		nz, jpCoorMon
		bit		sendKeyStraightTocxMain, (iy+APIFlg)
		jr		z, {+}
		push	af
		jp		Mon_CallcxMain
+:		cp		kOff
		jr		z, {+}
		call	j_PULLDOWNCHK
		jr		c, jpCoorMon
		call	j_clrTR
		jr		c, jpCoorMon
		call	IsExternalApp
+:		jr		z, sendKPress
		cp		kDoug
		jr		z, sendKPress
		cp		kListflag
		jr		z, sendKPress
		jr		Mon_Continue
-:		pop		hl
		pop		bc
		djnz	{+}
jpCoorMon:
		jp		coorMon
SendProgToEditTocxMain:
		ld		b, 8
		ld		hl, progToEdit-1
+:		inc		hl
		ld		a, (hl)
		or		a
		jr		z, jpCoorMon
		push	bc
		push	hl
		ld		d, 0
		ld		e, a
		call	j_TokToKey
		call	cxMain
		res		onInterrupt, (iy+onFlags)
		jr		{-}
Mon_Continue:
		cp		EchoStart
		jp		c, Mon_HandleNonEchoKey
		cp		kExtendEcho
		jr		z, sendKPress
		cp		kExtendEcho2
		jr		z, sendKPress
		cp		kExtendEcho3
		jr		nz, _isOtherKey
		ld		a, (keyExtend)
		or		a
		jr		z, {+}
		cp		0Dh
		jr		c, _isValid3Key
		cp		8Ch
		jr		c, Mon_HandleOurselves
		cp		0A6h
		jr		nc, Mon_HandleOurselves
_isValid3Key:
		ld		a, kExtendEcho3
		jr		sendKPress
+:		ld		a, (kbdKey)
_isOtherKey:
		cp		0EFh
		jr		nz, {+}
		ld		a, 0A6h
		jr		setKeyExtend
+:		cp		0F9h
		jr		c, {+}
		sub		14h
		jr		setKeyExtend
+:		cp		0F3h
		jr		c, sendKPress
		sub		7Ch
setKeyExtend:
		ld		(keyExtend), a
		ld		a, kExtendEcho
sendKPress:
		cp		echoStart2
		jr		nz, {+}
		ld		a, kExtendEcho2
		jr		setKeyExtend
+:		cp		echoStart1+1
		jr		nz, {+}
		ld		a, 0FDh
		jr		setKeyExtend
sendKPress_NamedVar:
+:		cp		kDoug
		push	af
		jr		nz, {+}
		set		recallingPrgmName, (iy+rclFlag2)
		ld		a, kExec
		jr		Mon_CallcxMain
+:		pop		af
		cp		kListflag
		push	af
		jr		nz, Mon_CallcxMain
		set		recallingListName, (iy+rclFlag3)
		ld		a, kListName
Mon_CallcxMain:
		call	cxMain
		res		onInterrupt, (iy+onFlags)
		pop		af
		jp		z, SendProgToEditTocxMain
Mon_HandleOurselves:
		bit		enableQueue, (iy+rclFlag)
		jr		z, Mon_Restart
		ld		de, (rclQueue)
		ld		hl, (editCursor)
		bit		useRclQueueEnd, (iy+hookflags1)
		jr		z, {+}
		ld		hl, (rclQueueEnd)
		ex		de, hl
+:		call	CpHLDE
		jr		nc, Mon_DisableQueueRestart
		ld		a, (hl)
		call	isPtrHLA2ByteTok
		ld		d, 0
		ld		e, a
		jr		nz, {+}
		ld		d, a
		inc		hl
		ld		e, (hl)
+:		bit		useRclQueueEnd, (iy+hookflags1)
		jr		z, {+}
		inc		hl
		ld		(rclQueue), hl
+:		call	j_TokToKey
jpSendKPress:
		jr		sendKPress
Mon_DisableQueueRestart:
		res		enableQueue, (iy+rclFlag)
		ld		hl, flags+textFlags
		ld		a, (promptIns)
		res		textInsMode, (hl)
		or		(hl)
		ld		(hl), a
Mon_Restart:
		res		useRclQueueEnd, (iy+hookflags1)
		jp		Mon
Mon_HandleNonEchoKey:
		cp		kApp
		jr		c, jpSendKPress
		cp		echoStart1
		jr		c, Mon_SwitchToStandardContext
		cp		kEval
		jr		nc, Mon_SwitchToGraphContextThenPressKey
		bit		prgmEdit, (iy+prgmFlags)
		jr		nz, jpSendKPress
		cp		echoStart2
		jr		c, Mon_SwitchToGraphContextThenPressKey
		cp		kPen
		jr		z, Mon_SwitchToGraphContextThenPressKey
		cp		echoStart2
		jr		z, jpSendKPress
		ld		b, a
		ld		a, (mcxCurApp)
		cp		kGraph
		ld		a, b
		jr		nz, jpSendKPress
Mon_SwitchToGraphContextThenPressKey:
		ld		b, a
		ld		a, kGraph
		jr		{+}
		push	af
Mon_SwitchToStandardContext:
		ld		b, a
		cp		kNewApps
		jr		nz, {+}
		ld		a, (keyExtend)
		add		a, b
		cp		echoStart1+1
		jr		nc, JForceCmdNoChar
		ld		b, a
+:		ld		c, a
		ld		a, (mcxCurApp)
		cp		kPrgmInput
		jr		nz, {+}
		ld		a, b
		cp		appStart
		jr		z, JForceCmdNoChar
		ld		a, kPrgmInput
		jr		{++}
+:		cp		kError
		ld		a, c
		jr		nz, {++}
		cp		appStart
		jr		nz, Mon_Restart
++:		call	newContext0
		ld		a, b
		jp		sendKPress_NamedVar

JForceCmdNoChar:
		xor		a
JForceCmd:
		ld		b, a
		ld		sp, (onSP)
		res		cmdExec, (iy+cmdFlags)
		call	SetFastSpeed
		ld		a, appStart
		call	newContext0
		ld		a, b
		cp		kExec
		jr		nz, {+}
		ld		a, kDoug
-:		jp		sendKPress
+:		cp		kListName
		jr		nz, {-}
		ld		a, kListflag
		jr		{-}

sysErrHandler:
		ld		sp, (onSP)
		ld		a, kError
		call	newContext
		jp		Mon

#ifndef DEVIATE_FROM_ORIGINAL_FILE
;We don't need this, I don't think.
		ret
#endif

newContext:
		ld		c, a
		sub		a
		ld		(kbdKey), a
		ld		b, a
		in		a, (6)
		push	af
		ld		a, c
		call	newContext0
		pop		af
sub_630:
		out		(6), a
		ret

newContext0:
		res		appRetKeyOff, (iy+APIFlg)
		ld		hl, mcxCurApp
		cp		(hl)
		jr		nz, newContext_Start
		cp		kGraph
		jr		nz, {+}
		;If you try to start the graph context again, just put it away
		push	bc
		call	j_GRPUTAWAY
		pop		bc
		ret
+:		;We're allowed to re-start the homescreen context again
		cp		appStart
		jr		z, newContext_Start
		ld		b, kApp
		ret
newContext_Start:
		push	bc
		push	af
		pop		af
		cp		kInputDone
		jr		nz, page0_65B
		ld		a, appStart
		push	af
		xor		a
		jr		page0_664
page0_65B:
		push	af
page0_65C:
		sub		appStart
		jr		nz, page0_664
		res		cmdExec, (iy+cmdFlags)
page0_664:
		call	Page0Call_3F51
		pop		af
		push	hl
		push	af
		res		curLock, (iy+curFlags)
		call	cxPutAway2
		pop		af
		push	af
		ld		b, a
		ld		a, (mcxCurApp)
		cp		kPrgmCr
		ld		a, b
		jr		z, page0_68E
		bit		prgmEdit, (iy+prgmFlags)
		jr		z, page0_68E
		cp		kFormat
		jr		z, page0_69E
		cp		kMode
		jr		z, page0_69E
		cp		kTblSet
		jr		z, page0_69E
page0_68E:
		call	Page0Call_3F57
		cp		kError
		jr		z, page0_69E
		call	cxPPutAway
		call	IsExternalApp
		call	z, SetFastSpeed
page0_69E:
		ld		a, (mcxCurApp)
		pop		bc
		push	af
		push	bc
		cp		kError
		jr		nz, page0_6B4
		call	j_POPCX
		call	sub_73D
		call	IsExternalApp
		call	z, SetFastSpeed
page0_6B4:
		ld		hl, mcxMain
		ld		de, mcxPrev
		ld		bc, 14
		ldir
		ld		a, (appFlagsAddr)
		ld		(de), a
		pop		af
		push	af
		cp		4Ah
		jr		nz, page0_6CE
		call	Page0Call_3F63
		jr		page0_707
page0_6CE:
		cp		kGraph
		call	z, Page0Call_3F63
		jr		z, page0_6DF
		set		6, (iy+1Dh)
		set		7, (iy+1Dh)
		jr		page0_707
page0_6DF:
		call	page0_17DB
		call	CheckSplitFlag
		jr		z, page0_707
		ld		a, (mcxCurApp)
		call	j_CompareContext
		jr		nz, page0_707
		ld		a, appStart
		ld		(mcxCurApp), a
		ld		hl, Page0Call_3F6F
		ld		a, (iy+12h)
		push	af
		ld		a, appstart
		call	sub_71D
		call	cxPutAway
		pop		af
		ld		(iy+12h), a
page0_707:
		pop		af
		pop		bc
		cp		46h ; 'F'
		jr		nz, page0_718
		push	af
		ld		a, b
		cp		kError
		jr		z, page0_717
		set		promptEdit, (iy+promptFlags)
page0_717:
		pop		af
page0_718:
		pop		hl
		ld		(mcxCurApp), a
		pop		af
sub_71D:
		bit		appChangeHookActive, (iy+hookflags4)
		call	nz, j_call_appchange_hook
		push	af
		set		5, (iy+1)
		call	jpHL
		res		5, (iy+1)
		pop		bc
		ret

ldHLind_AWord:
		add		a, a
		ld		e, a
		ld		d, 0
		add		hl, de
		jp		ldHLind

cxPutAway:
		call	cxPutAway2
sub_73D:
		call	cxPPutAway
page0_740:
		res		promptEdit, (iy+promptFlags)
		ret

cxPutAway2:
		bit		nocxPutAway, (iy+contextFlags)
		ret		nz
		push	hl
		ld		hl, (mcxPPutAway)
page0_74E:
		call	sub_77F
		jr		page0_762

cxPPutAway:
		bit		nocxPutAway, (iy+contextFlags)
		ret		nz
		push	hl
		ld		hl, (mcxPutAway)
		call	sub_77F
		call	zero_basic_prog
page0_762:
		pop		hl
page0_763:
		jp		CancelAlpha

cxRedisp:
		ld		hl, (unk_8593)
		jr		sub_77F

cxSizeWind:
		bit		5, (iy+1)
		push	af
		ld		a, (menuActive)
		ld		b, a
page0_774:
		ld		hl, (mcxSizeWind)
sub_777:
		call	sub_77F
		pop		af
		ret

cxErrEP:
		ld		hl, (mcxErrorEP)
sub_77F:
		in		a, (6)
		push	af
		ld		a, (mcxPage)
		call	sub_630
		call	jpHL
		pop		af
		call	sub_630
		ret

cxMain:
		push	hl
		ld		hl, (mcxMain)
		push	af
		ld		a, (mcxPage)
		call	sub_630
		pop		af
		call	jpHL
		pop		hl
		ret

jpHL:
		jp		(hl)

page0_7A2:
		ld		a, (unk_85A7)
		call	sub_630
		ld		hl, (unk_85A1)
		jr		jpHL

monErrHand:
		ld		hl, monErrHand
		call	pushErrorHandleR
		ld		a, 52h ; 'R'
		call	newContext0
		jp		Mon

		push	hl
		ld		hl, 64h	; 'd'
		jr		page0_7C5
page0_7C1:
		push	hl
		ld		hl, 32h	; '2'
page0_7C5:
		call	EnoughMem
		jp		c, Page0Call_24F9
		pop		hl
AppInit:
		ld		de, mcxMain
		ld		bc, 12
		ldir
		ld		a, (hl)
		ld		(appFlagsAddr), a
		in		a, (6)
		ld		(mcxPage), a
		ret

IsExternalApp:
		ld		b, a
		ld		a, (mcxCurApp)
		cp		kExtApps
		ld		a, b
		ret

handleONKeyPress:
		;Switch to slow speed
		in		a, (2)
		and		80h
		jr		z, {+}
		in		a, (20h)
		ld		e, a
		xor		a
		out		(20h), a
+:		ld		b, 0
debounceRestart:
		ld		hl, 1016h
debounceLoop:
		in		a, (4)
		and		8
		cp		b
		ld		b, a
		jr		nz, debounceRestart
		dec		hl
		ld		a, l
		or		h
		jr		nz, debounceLoop
		cp		b
		jr		z, ONKeyBeingPressed
		;ON key is being released
		in		a, (7)
		ld		b, a
		in		a, (2)
		and		80h
		jr		z, {+}
		;Restore previous speed
		ld		a, e
		out		(20h), a
		ld		a, b
		cp		81h
		jr		isRAMUnhooked
+:		ld		a, b
		and		4Fh
		cp		41h
isRAMUnhooked:
		jp		nz, RAMUnhookedWaitForPowerOn
		bit		onRunning, (iy+onFlags)
		ret		nz
		;ON key being released and we're just off, so just return from the interrupt handler
		pop		hl
		sub		a
		push	af
		ld		a, 8
		out		(3), a
		pop		af
		out		(3), a
		ld		a, 11h
		jp		noSecondHardwareTimer
ONKeyBeingPressed:
		;Restore previous speed
		in		a, (2)
		and		80h
		jr		z, HandlePowerOnRequest
		ld		a, e
		out		(20h), a

.module	HandlePowerOnRequest
HandlePowerOnRequest:
		ld		a, 76h
		out		(4), a
		in		a, (2)
		and		80h
		jr		z, {+}
		in		a, (7)
		cp		81h
		jr		_isRAMUnhooked
+:		in		a, (7)
		and		4Fh
		cp		41h
_isRAMUnhooked:
		jp		nz, HookRAMAndPowerOn
		bit		shift2nd, (iy+shiftFlags)
		jp		z, HandleONPressed
		;2nd is being pressed, so we might be turning off
		bit		enableKeyEcho, (iy+hookflags1)
		jr		nz, {+}
		bit		appRetKeyOff, (iy+APIFlg)
		jr		z, PowerOff
+:		set		kOffPressed, (iy+APIFlg)
		ret
.endmodule

.module	PowerOff
PowerOff:
		ld		a, 8
		push	af
		ld		a, 8
		out		(3), a
		pop		af
		out		(3), a
		call	TurnOffLCD
		bit		resetOnPowerOn, (iy+selfTestFlag)
		jr		z, {+}
		;If above flag is set and APD is disabled, skip RAM checksum generation (forcing a reset)
		bit		apdAble, (iy+apdFlags)
		jr		z, _skipRAMChecksum
+:		call	SetFastSpeed
		call	PreviousContext_CloseAnyOpenMenu
		call	j_hideCursor
		set		monAbandon, (iy+monFlags)
		ld		hl, lcdFlag
		set		powerLcdForGC, (hl)
		call	forcecmd
		ld		hl, lcdFlag
		res		powerLcdForGC, (hl)
		res		monAbandon, (iy+monFlags)
		bit		alt_Off, (iy+hookflags1)
		call	nz, j_ExecuteOFFSCRPT
_DoShutDown:
		call	j_SetupDBusMem
		ld		a, 8
		out		(3), a
		call	TurnOffLCD
		ld		hl, flags+onFlags
		bit		clrScrnOnPowerOff, (hl)
		res		clrScrnOnPowerOff, (hl)
		call	nz, j_ClrScrn
		call	calculateRAMChecksum
		ld		(RAMChecksum), hl
_skipRAMChecksum:
		res		bPLinkOn, (iy+hookflags1)
		res		enableKeyEcho, (iy+hookflags1)
		ld		a, 36h
		out		(4), a
		ld		a, 11h
		out		(3), a
		res		shift2nd, (iy+shiftFlags)
		res		onRunning, (iy+onFlags)
		ex		af, af'
		exx
		ei
-:		halt
		jr		{-}
.endmodule

HandleONPressed:
		res		checkNoBatteriesNextTime, (iy+batteryFlags2)
		call	ApdSetup
		bit		onRunning, (iy+onFlags)
		jr		nz, page0_94D
page0_8F1:
		set		onRunning, (iy+onFlags)
		ld		hl, flags+apdFlags
		bit		4, (hl)
		res		4, (hl)
		jr		nz, page0_92C
		res		indicInUse, (iy+indicFlags)
		bit		resetOnPowerOn, (iy+selfTestFlag)
		jr		z, page0_90D
		bit		2, (hl)
		jp		z, resetRam
page0_90D:
		ld		sp, (onSP)
		call	j_resetStacks
		xor		a
		ld		(MenuCurrent), a
		call	refreshSystemRAM
		call	sub_ADA
		call	Page0Call_3E97
		bit		cmdExec, (iy+cmdFlags)
		call	nz, Page0Call_3E9D
		ei
		jp		Mon

page0_92C:
		bit		alt_On, (iy+hookflags1)
		call	nz, j_ExecuteONSCRPT
		ld		a, 8
		push	af
		ld		a, 8
		out		(3), a
		pop		af
		out		(3), a
		call	j_RstrOScreen
		pop		de
		pop		de
		pop		de
		set		3, (hl)
		call	sub_ADA
		ex		af, af'
		exx
		ei
		reti

page0_94D:
		set		onInterrupt, (iy+onFlags)
		ret

HookRAMAndPowerOn:
		ld		a, 76h ; 'v'
		out		(4), a
		ld		a, 0C0h	; ''
		out		(0), a
		in		a, (2)
		and		80h ; ''
		ld		a, 41h ; 'A'
		jr		z, page0_96B
		ld		a, 1
		out		(20h), a
		xor		a
		out		(0Fh), a
		ld		a, 81h ; 'g
page0_96B:
		out		(7), a
		xor		a
		push	af
		ld		a, 8
		out		(3), a
		pop		af
		out		(3), a
		ld		iy, 89F0h
		call	NZIf83Plus
		call	z, Page0Call_3EAF
		ld		hl, (RAMChecksum)
		ld		de, 0A55Ah
		call	CpHLDE
		jp		z, doFunkyTurnOnFromA55AChecksum
		call	NZIf83Plus
		jr		z, page0_9A0
		ld		b, 0
		B_CALL(_getHardwareVersion)
		bit		0, b
		ld		a, 1Fh
		jr		z, page0_9A2
		ld		a, 1Ch
		jr		page0_9A2

page0_9A0:
		ld		a, 17h
page0_9A2:
		ld		(contrast), a
		call	calculateRAMChecksum
		ld		de, (RAMChecksum)
		call	CpHLDE
		jr		nz, resetRam
		dec		de
		ld		(RAMChecksum), de
		call	Page0Call_3EB5
		call	PreviousContext_CloseAnyOpenMenu
		ld		a, 0Bh
		out		(3), a
		res		apdWarmStart, (iy+apdFlags)
		call	forcecmd
		jp		page0_8F1

resetRam:
		di
		ld		hl, 8000h
		ld		de, 8001h
		ld		bc, 7FFFh
		ld		(hl), 0
		ldir
		ld		hl, 109h
		ld		(localLanguage), hl
;#ifdef DEVIATE_FROM_ORIGINAL_FILE
;		call	NZIfColorModel
;		jr		z, {+}
;		set		0, (iy+asm_Flag3)
;+:
;#endif
page0_9DE:
		ld		a, 0C0h	; ''
		out		(0), a
		ld		sp, 0FFF7h
		call	Page0Call_3EBB
		ld		hl, monErrHand
		call	pushErrorHandleR
		ld		(onSP), sp
		call	Page0Call_3EC1
		jp		Page0Call_3EC7

page0_9F8:
		di
		ld		a, 0C0h	; ''
		out		(0), a
		ld		sp, 0FFF7h
		ld		hl, 8000h
		ld		de, 8001h
		ld		bc, 7FFFh
		ld		(hl), 0
		ldir
		ld		hl, 109h
		ld		(localLanguage), hl
		call	Page0Call_3EBB
		ld		hl, monErrHand
		call	pushErrorHandleR
		ld		(onSP), sp
		call	Page0Call_3EC1
		jp		Page0Call_3ECD

page0_A26:
		call	GetCSC
		set		2, (iy+9)
		call	j_getkey
		call	j_ClrScrn
		res		2, (iy+9)
		jp		monForceKey

calculateRAMChecksum:
		push	ix
		ld		ix, 9D95h
		ld		bc, 60DBh
		ld		hl, 11h
		ld		d, h
page0_A47:
		ld		e, (ix+0)
		inc		ix
		add		hl, de
		dec		bc
		ld		a, c
		or		b
		jr		nz, page0_A47
		ld		e, d
		ex		de, hl
		sbc		hl, de
		pop		ix
		ret

forcecmd:
		call	CanAlphIns
		res		textInverse, (iy+textFlags)
		call	j_Disp
		res		cmdExec, (iy+cmdFlags)
		ld		a, 40h ; '@'
		jp		newContext0

bootCalc:
		call	SetSlowSpeed
		ld		a, 77h ; 'w'
		out		(4), a
		ld		b, 0
		ld		sp, 0FDFAh
page0_A78:
		djnz	$
		ld		ix, 1
		add		ix, sp
		ld		sp, ix
		jr		nc, page0_A78
		ld		sp, 0FFC5h
		in		a, (2)
		and		80h ; ''
		jr		z, page0_A91
		ld		a, 1
		out		(20h), a
page0_A91:
		ld		a, 77h ; 'w'
		out		(4), a
		im	1
		ld		a, 0C0h	; ''
		out		(0), a

RAMUnhookedWaitForPowerOn:
		ld		a, 37h ; '7'
		out		(4), a
		sub		a
		push	af
		ld		a, 8
		out		(3), a
		pop		af
		out		(3), a
		ld		a, 11h
		out		(3), a
		ei
		halt

lcd_busy:
		push	af
page0_AAF:
		in		a, (2)
		and		2
		jr		z, page0_AAF
		pop		af
		ret

page0_AB7:
TurnOffLCD:
		ld		a, 2
page0_AB9:
		push	af
loop:
		in		a, (2)
		and		2
		jr		z, loop
		pop		af
		out		(10h), a
		ret

page0_AC4:
smallDelay:
		push	af
		inc		hl
		dec		hl
		pop		af
		ret

page0_AC9:
slowDelay:
		ld		a, 0FFh

slowDelayA:
		push	af
		call	forceSlowSpeed
		pop		af
delayLoop:
		call	smallDelay
		dec		a
		jr		nz, delayLoop
		call	restorePreviousSpeed
		ret

sub_ADA:
		ld		a, 0Bh
		out		(3), a
		call	j_LCD_DRIVERON
		call	Chk_Batt_Low_2
		ret		nz
		call	refreshSystemRAM
		in		a, (2)
		bit		0, a
		jp		z, PowerOff._skipRAMChecksum

sub_AEF:
		call	Page0Call_3ED9
		call	forcecmd
		jp		Page0Call_3EDF

Chk_Batt_Low_2:
		call	refreshSystemRAM
Chk_Batt_Low:
		ld		a, 76h ; 'v'
		out		(4), a
		ld		a, 0F6h	; '򧍊
		out		(4), a
		ld		b, 5
longerDelay:
		call	slowDelay
		djnz	longerDelay
		set		5, (iy+18h)
		in		a, (2)
		bit		0, a
		jr		nz, {+}
		res		5, (iy+18h)
+:		ld		a, 76h ; 'v'
		out		(4), a
		ld		a, 0Ah
		call	slowDelayA
		ld		a, 76h ; 'v'
		out		(4), a
		bit		5, (iy+18h)
		ret

.module	RefreshSystemRAM
refreshSystemRAM:
		ld		bc, 2710h
		ld		hl, ramStart
-:		ld		a, (hl)
		ld		(hl), a
		dec		bc
		ld		a, b
		or		c
		jr		nz, {-}
		ret
.endmodule

doFunkyTurnOnFromA55AChecksum:
		ld		hl, 0
		ld		(RAMChecksum), hl
		ld		sp, (onSP)
		ld		iy, flags
		call	Page0Call_3EE5
		call	PreviousContext_CloseAnyOpenMenu
		call	forcecmd
		res		apdWarmStart, (iy+apdFlags)
		bit		cmdExec, (iy+cmdFlags)
		call	nz, Page0Call_3E9D
		ld		a, 0Bh
		out		(3), a
		call	j_LCD_DRIVERON
		set		onRunning, (iy+onFlags)
		call	Chk_Batt_Low_2
		call	z, sub_AEF
		bit		alt_On, (iy+hookflags1)
		call	nz, j_ExecuteONSCRPT
		ei
		jp		Mon

PreviousContext_CloseAnyOpenMenu:
		call	IsInMenu
		jr		z, PreviousContext
		xor		a
		ld		(MenuCurrent), a
		call	j_rstrpartial
PreviousContext:
		call	j_PrevContext
		ret

IsInMenu:
		ld		b, a
		ld		a, (MenuCurrent)
		or		a
		ld		a, b
		ret

SetSlowSpeed:
		call	NZIf83Plus
		ret		nz
		jr		{+}
forceSlowSpeed:
		call	NZIf83Plus
		ret		nz
		in		a, (20h)
		ld		(oldSESpeed), a
+:		push	af
		xor		a
		out		(20h), a
		pop		af
		ret

restorePreviousSpeed:
		call	NZIf83Plus
		ret		nz
		ld		a, (oldSESpeed)
		and		1
		out		(20h), a
		ret

SetExSpeed:
		or		a
SetSpeed_NZForFast:
		jr		z, SetSlowSpeed
SetFastSpeed:
		push	af
		call	NZIf83Plus
		jr		nz, {+}
		ld		a, 1
		out		(20h), a
+:		pop		af
		ret

page0_BBD:
		pop		af
		push	hl
		ld		hl, SEspeed
		dec		(hl)
		pop		hl
		call	NZIf83Plus
		jr		nz, {+}
		and		1
		out		(20h), a
+:		pop		af
		pop		af
		pop		af
		ret