source/page00/interrupt.asm
author brandonlw
Wed, 07 May 2014 08:10:58 -0400
changeset 13 b533bee0f778
parent 11 fea1578031ed
child 16 27ca02d0d8aa
permissions -rw-r--r--
Whoops, wouldn't boot when trashing A here.

interruptFromLinkActivity:
		res		3, (iy+12h)
		push	hl
		sub		a
		push	af
		ld		a, 8
		out		(3), a
		pop		af
		out		(3), a
		jp		page0_83D

interruptFromSecondHardwareTimer:
		ld		a, 4
		bit		0, (iy+16h)
		jp		z, intReturn_AtoPort3First
		ld		a, (iy+16h)
		xor		2
		ld		(iy+16h), a
		bit		1, (iy+16h)
		jp		nz, page0_170
		set		2, (iy+16h)
		in		a, (2)
		bit		0, a
page0_155:
		jr		nz, page0_15B
page0_157:
		res		2, (iy+16h)
page0_15B:
		ld		a, 76h ; 'v'
		out		(4), a
page0_15F:
		res		2, (iy+18h)
		res		0, (iy+16h)
page0_167:
		set		7, (iy+33h)
		ld		a, 9
		jp		intReturn_AtoPort3First
page0_170:
		set		2, (iy+18h)
		ld		a, 76h ; 'v'
		out		(4), a
		ld		a, 76h ; 'v'
		out		(4), a
		ld		a, 9
		push	af
		ld		a, 8
		out		(3), a
		pop		af
		out		(3), a
		jp		intReturnEnableSecondHardwareTimer

page0_189:
		bit		4, (iy+9)
		ret

page0_18E:
		bit		0, (iy+7)
		ret

page0_193:
		ld		a, (unk_9653)
		or		a
		ret

page0_198:
		bit		5, (iy+2)
		ret

page0_19D:
		bit		7, (iy+2)
		ret

page0_1A2:
		bit		6, (iy+2)
		ret

page0_1A7:
		ld		a, 3Ah ; ':'
		call	Page0Call_3F9F
		ret

DivHLBy10:
		ld		a, 0Ah

DivHLByA:
		push	bc
		ld		c, a
		sub		a
		ld		b, 10h
page0_1B4:
		add		hl, hl
		rla
		cp		c
		jr		c, page0_1BB
		sub		c
		inc		l
page0_1BB:
		djnz	page0_1B4
		pop		bc
		ret

page0_1BF:
		ld		hl, indicCounter
		dec		(hl)
		ret		nz
		call	Page0Call_3FA5
		ret

GetBaseVer:
		ld		a, 1
		ld		b, 13h
		ret

getRomPage:
		in		a, (6)
		ret

continue_start:
		ld		a, 1Fh
		jr		z, page0_1DA
		ld		a, 3
		out		(0Eh), a
		ld		a, 7Fh ; ''
page0_1DA:
		out		(6), a
		ld		a, 77h ; 'w'
		out		(4), a
#ifdef DEVIATE_FROM_ORIGINAL_FILE
;Jump to the 84+/SE/84+CSE boot code entry point instead.
		in		a, (2)
		bit		5, a
		jp		nz, 812Ch
#endif
		jp		80D5h

LoadCIndPaged:
		push	af
		in		a, (6)
		push	af
		in		a, (2)
		and		80h ; ''
		ld		a, b
		jr		nz, page0_1F0
		and		1Fh
page0_1F0:
		out		(6), a
		ld		c, (hl)
		pop		af
		out		(6), a
		pop		af
		ret

.module CheckForFlashTrickery
checkForFlashTrickery:
page0_1F8:
		push	bc
		push	hl
		ld		(unk_83E8), sp
		ld		a, (unk_83E8+1)
		and		0C0h
		cp		0C0h
		jr		z, page0_20A
page0_207:
		jp		start
page0_20A:
		ld		bc, 8
		ld		hl, (unk_83E8)
		add		hl, bc
		jr		c, page0_207
		call	NZIf83Plus
		in		a, (6)
		jr		nz, page0_226
		ld		b, 81h ; 'g

#ifdef DEVIATE_FROM_ORIGINAL_FILE
;Add support for the TI-84 Plus.
		in		a, (2)
		bit		5, a
		in		a, (6)
		jr		z, _ignore84P
		and		3Fh
		cp		3Ch
		jr		z, page0_232
		cp		3Dh
		jr		z, page0_232
		jr		page0_230
_ignore84P:
#endif

		and		7Fh ; ''
		cp		7Ch ; '|'
		jr		z, page0_232
		cp		7Dh ; '}'
		jr		page0_230
page0_226:
		ld		b, 41h
		and		1Fh
		cp		1Ch
		jr		z, page0_232
		cp		1Dh
page0_230:
		jr		nz, page0_207
page0_232:
		in		a, (7)
		cp		b
		jr		nz, page0_207
		ld		hl, 0C000h
		ld		c, 0
page0_23C:
		ld		a, (hl)
		ld		b, a
		cpl
		ld		(hl), a
		ld		a, (hl)
		cpl
		cp		b
		jr		nz, page0_207
		ld		(hl), a
		ld		a, c
sub_247:
		or		a
		jr		nz, page0_251
		ld		hl, 8000h
		ld		c, 1
		jr		page0_23C
page0_251:
		pop		hl
		pop		bc
		ret
.endmodule

sub_254:
		ld		a, (unk_89F8)
		bit		2, a
		ret		z
		ld		hl, (unk_9C24)
		ld		a, h
		or		l
		jr		z, sub_265
		dec		hl
		ld		(unk_9C24), hl
sub_265:
		ld		a, (unk_89F8)
		bit		3, a
		ret		z
		ld		hl, apdSubTimer
		dec		(hl)
		ret		nz
		inc		hl
		dec		(hl)
		ret		nz
page0_273:
		call	TurnOffLCD
		call	Page0Call_3F7B
page0_279:
		res		3, (iy+8)
		set		4, (iy+8)
		jp		page0_8AC

HandleLinkKeyActivity:
		nop
		nop
		di
		push	af
		call	NZIf83Plus
		jr		nz, page0_2A1
		in		a, (9)
		and		0D9h ; '٧
		jr		z, page0_29A
		bit		6, a
		call	nz, sub_2A8
		jr		page0_2A5
page0_29A:
		ld		a, (unk_9C86)
		cp		0
		jr		nz, page0_2A5
page0_2A1:
		pop		af
		ei
		halt
		ret
page0_2A5:
		pop		af
		ei
		ret

sub_2A8:
		set		7, (iy+3Dh)
		ret

ApdSetup:
		ld		hl, apdTimer
		ld		(hl), 74h ; 't'
		ret

kdbScan:
		call	Page0Call_305
		jr		c, page0_2FA
		ld		hl, 8441h
		cp		(hl)
		ld		hl, 8443h
		jr		z, page0_2C6
		ld		(unk_8441), a
		ld		(hl), 5
page0_2C6:
		or		a
		jr		nz, page0_2CC
		dec		(hl)
		ret		nz
		inc		(hl)
page0_2CC:
		ld		hl, 8440h
		cp		(hl)
		jr		nz, page0_2E9
		or		a
		ret		z
		cp		0F3h ; '󧍊
		jr		nc, page0_2DF
		cp		38h ; '8'
		jr		z, page0_2DF
		cp		5
		ret		nc
page0_2DF:
		ld		hl, 8442h
		dec		(hl)
		ret		nz
		ld		(hl), 0Ah
		jp		sub_37F
page0_2E9:
		call	sub_37F
		or		a
		jr		z, page0_2F3
		set		4, (iy+0)
page0_2F3:
		ld		(hl), a
		ld		a, 32h ; '2'
		ld		(unk_8442), a
		ret
page0_2FA:
		ld		a, 0FFh
		ld		(unk_8441), a
		ld		a, 5
		ld		(unk_8443), a
		ret

sub_305:
Page0Call_305:
		sub		a
		call	page0_35A
		xor		0FFh
		ret		z
		ld		hl, 0
		ld		c, 0FEh	; '򧍊
		ld		d, 1
page0_313:
		ld		a, c
		bit		0, (iy+2Ch)
		jr		z, page0_32F
		cp		0FEh ; '򧍊
		jr		nz, page0_32F
		call	page0_35A
		cp		0F5h ; '񧍊
		ret		z
		cp		0F3h ; '󧍊
		ret		z
		cp		0FAh ; '򧍊
		ret		z
		cp		0FCh ; '𧍊
		ret		z
		jr		page0_332

page0_32F:
		call	page0_35A
page0_332:
		ld		e, 0
		ld		b, 8
page0_336:
		rla
		jr		c, page0_33B
		inc		e
		ld		l, b
page0_33B:
		djnz	page0_336
		ld		a, e
		or		a
		jr		z, page0_34A
		cp		2
		jr		nc, page0_358
		ld		a, h
		or		a
		jr		nz, page0_358
		ld		h, d
page0_34A:
		inc		d
		rlc	c
		jr		c, page0_313
		ld		a, h
		or		a
		ret		z
		dec		a
		rla
		rla
		rla
		add		a, l
		ret

page0_358:
		scf
		ret

page0_35A:
		push	af
		in		a, (2)
		and		80h ; ''
		jr		nz, page0_371
page0_361:
		pop		af
		out		(1), a
page0_364:
		nop
		nop
		nop
		nop
page0_368:
		in		a, (1)
		ld		b, a
		ld		a, 0FFh
		out		(1), a
sub_36F:
		ld		a, b
		ret

page0_371:
		in		a, (20h)
		and		1
		jr		z, page0_361
		pop		af
		out		(1), a
		nop
		nop
		nop
		jr		page0_364

sub_37F:
		ld		(kbdScanCode), a
		set		3, (iy+0)
		or		a
		ret		z
		ld		(kbdGetKy), a
		ret

GetCSC:
		ld		hl, 843Fh
		di
		ld		a, (hl)
		ld		(hl), 0
		res		3, (iy+0)
		ei
		ret

page0_399:
Page0Call_399:
		bit		6, (iy+12h)
		ret		nz
		jr		page0_3A8

CanAlphIns:
		res		4, (iy+5)
page0_3A4:
		res		6, (iy+12h)
page0_3A8:
		bit		7, (iy+12h)
		ret		nz
		res		4, (iy+12h)
		ret

		set		7, (iy+12h)
		jr		page0_3BC

page0_3B8:
Page0Call_3B8:
		set		6, (iy+12h)
page0_3BC:
		set		4, (iy+12h)
		res		5, (iy+12h)
		ret

coorMon:
		res		5, (iy+14h)
		call	Page0Call_3EFD
		call	Page0Call_3F03
Mon:
		call	Page0Call_3F09
		call	Page0Call_3F0F
		res		7, (iy+8)
		bit		5, (iy+1Fh)
		jr		z, page0_408
		di
		res		5, (iy+1Fh)
		ld		hl, 403h
		call	pushErrorHandleR
		set		2, (iy+12h)
		ld		a, (mcxCurApp)
		cp		52h ; 'R'
		jr		nz, page0_3FD
		ld		a, (errNo)
		call	Page0Call_3F15
		jr		page0_400

page0_3FD:
		call	Page0Call_3F1B
page0_400:
		call	popErrorHandleR
		res		2, (iy+12h)
		ei
page0_408:
		xor		a
		ld		(kbdKey), a
		ld		(keyExtend), a
		call	Page0Call_3F21
monForceKey:
		res		7, (iy+16h)
		res		1, (iy+1Dh)
		ld		(kbdKey), a
		bit		0, (iy+2)
		jr		z, page0_433
		res		2, (iy+0Ch)
		bit		3, (iy+0Ch)
		call	z, Page0Call_3F27
		ld		a, (kbdKey)
		jr		page0_436

page0_433:
		call	Page0Call_3EFD
page0_436:
		call	Page0Call_3F2D
		or		a
		jr		nz, page0_44D
		bit		0, (iy+0Dh)
		jr		nz, page0_448

page0_442:
		res		4, (iy+9)
		jr		page0_4B2

page0_448:
		ld		a, 5
		jp		sendKPress

page0_44D:
		cp		0B4h ; 'ԧ
		jr		nz, page0_470
		ld		a, 0CCh	; '̧
		call	page0_198
		jr		nz, page0_46D
		ld		a, 0ADh	; 'ͧ
sub_45A:
		call	page0_1A2
		jr		nz, page0_46D
		ld		a, 0B1h	; 'ѧ
page0_461:
		call	IsFunctionMode
		jr		nz, page0_46D
		ld		a, 0E8h	; '觍
		ld		(keyExtend), a
		ld		a, 0FEh
page0_46D:
		ld		(kbdKey), a
page0_470:
		res		1, (iy+28h)
		res		2, (iy+28h)
		bit		5, (iy+34h)
		jr		z, page0_491
		call	Page0Call_3F33
		bit		1, (iy+28h)
		jr		nz, page0_4B2
		bit		2, (iy+28h)
		jr		z, page0_491
		push	af
		jp		page0_53E

page0_491:
		cp		3Fh ; '?'
		jr		z, page0_4A2
		call	Page0Call_3F39
		jr		c, page0_4B2
		call	Page0Call_3F3F
		jr		c, page0_4B2
		call	page0_7DE
page0_4A2:
		jr		z, sendKPress
		cp		28h ; '('
		jr		z, sendKPress
		cp		29h ; ')'
		jr		z, sendKPress
		jr		page0_4D0

page0_4AE:
		pop		hl
		pop		bc
		djnz	page0_4BA

page0_4B2:
		jp		coorMon

page0_4B5:
		ld		b, 8
		ld		hl, 84BEh
page0_4BA:
		inc		hl
		ld		a, (hl)
		or		a
		jr		z, page0_4B2
		push	bc
		push	hl
		ld		d, 0
		ld		e, a
		call	Page0Call_3F45
		call	cxMain
		res		4, (iy+9)
		jr		page0_4AE

page0_4D0:
		cp		7Fh ; ''
		jp		c, page0_595
		cp		0FEh ; '򧍊
		jr		z, sendKPress
		cp		0FCh ; '𧍊
		jr		z, sendKPress
		cp		0FBh ; '󧍊
		jr		nz, page0_4FA
		ld		a, (keyExtend)
		or		a
		jr		z, page0_4F7
		cp		0Dh
		jr		c, page0_4F3
		cp		8Ch ; '̧
		jr		c, page0_549
		cp		0A6h ; 'Ƨ
		jr		nc, page0_549

page0_4F3:
		ld		a, 0FBh	;
		jr		sendKPress

page0_4F7:
		ld		a, (kbdKey)
page0_4FA:
		cp		0EFh ; '留
		jr		nz, page0_502
		ld		a, 0A6h	; 'Ƨ
		jr		page0_510

page0_502:
		cp		0F9h ; '񧍊
		jr		c, page0_50A
		sub		14h
		jr		page0_510

page0_50A:
		cp		0F3h ; '󧍊
		jr		c, sendKPress
		sub		7Ch ; '|'
page0_510:
		ld		(keyExtend), a
		ld		a, 0FEh	; '򧍊
sendKPress:
		cp		69h ; 'i'
		jr		nz, page0_51D
		ld		a, 0FCh	; '𧍊
		jr		page0_510

page0_51D:
		cp		5Bh ; '['
		jr		nz, page0_525
		ld		a, 0FDh	; '񧍊
		jr		page0_510

page0_525:
		cp		28h ; '('
		push	af
		jr		nz, page0_532
		set		7, (iy+16h)
		ld		a, 0DAh	; 'ڧ
		jr		page0_53E

page0_532:
		pop		af
		cp		29h ; ')'
		push	af
		jr		nz, page0_53E
		set		1, (iy+1Dh)
		ld		a, 7Fh ; ''
page0_53E:
		call	cxMain
		res		4, (iy+9)
		pop		af
		jp		z, page0_4B5
page0_549:
		bit		7, (iy+0Eh)
		jr		z, page0_58E
		ld		de, (rclQueue)
page0_553:
		ld		hl, (editCursor)
		bit		2, (iy+33h)
		jr		z, page0_560
		ld		hl, (rclQueueEnd)
		ex		de, hl
page0_560:
		call	CpHLDE
		jr		nc, page0_580
		ld		a, (hl)
		call	page0_172F
		ld		d, 0
		ld		e, a
		jr		nz, page0_571
		ld		d, a
		inc		hl
		ld		e, (hl)
page0_571:
		bit		2, (iy+33h)
		jr		z, page0_57B
		inc		hl
		ld		(rclQueue), hl
page0_57B:
		call	Page0Call_3F45
page0_57E:
		jr		sendKPress

page0_580:
		res		7, (iy+0Eh)
		ld		hl, 89F5h
		ld		a, (promptIns)
		res		4, (hl)
		or		(hl)
		ld		(hl), a
page0_58E:
		res		2, (iy+33h)
		jp		Mon

page0_595:
		cp		27h ; '''
		jr		c, page0_57E
		cp		5Ah ; 'Z'
		jr		c, page0_5C2
		cp		75h ; 'u'
		jr		nc, page0_5BC
		bit		0, (iy+9)
		jr		nz, page0_57E
		cp		69h ; 'i'
		jr		c, page0_5BC
		cp		6Dh ; 'm'
		jr		z, page0_5BC
		cp		69h ; 'i'
		jr		z, page0_57E
		ld		b, a
		ld		a, (mcxCurApp)
		cp		44h ; 'D'
		ld		a, b
		jr		nz, page0_57E
page0_5BC:
		ld		b, a
		ld		a, 44h ; 'D'
		jr		page0_5D0

		push	af
page0_5C2:
		ld		b, a
		cp		59h ; 'Y'
		jr		nz, page0_5D0
		ld		a, (keyExtend)
		add		a, b
		cp		5Bh ; '['
		jr		nc, JForceCmdNoChar
		ld		b, a
page0_5D0:
		ld		c, a
		ld		a, (mcxCurApp)
		cp		50h ; 'P'
		jr		nz, page0_5E1
		ld		a, b
		cp		40h ; '@'
		jr		z, JForceCmdNoChar
		ld		a, 50h ; 'P'
		jr		page0_5EA

page0_5E1:
		cp		52h ; 'R'
		ld		a, c
		jr		nz, page0_5EA
		cp		40h ; '@'
		jr		nz, page0_58E
page0_5EA:
		call	newContext0
		ld		a, b
		jp		page0_525

JForceCmdNoChar:
		xor		a
JForceCmd:
		ld		b, a
		ld		sp, (onSP)
		res		6, (iy+0Ch)
		call	page0_BB1
		ld		a, 40h ; '@'
		call	newContext0
		ld		a, b
		cp		0DAh ; 'ڧ
		jr		nz, page0_60D
		ld		a, 28h ; '('
page0_60A:
		jp		sendKPress

page0_60D:
		cp		7Fh ; ''
		jr		nz, page0_60A
		ld		a, 29h ; ')'
		jr		page0_60A

sysErrHandler:
		ld		sp, (onSP)
		ld		a, 52h ; 'R'
		call	newContext
		jp		Mon

		ret

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		7, (iy+28h)
		ld		hl, 859Ah
		cp		(hl)
		jr		nz, page0_64E
		cp		44h ; 'D'
		jr		nz, page0_647
		push	bc
page0_642:
		call	Page0Call_3F4B
		pop		bc
		ret

page0_647:
		cp		40h ; '@'
		jr		z, page0_64E
		ld		b, 27h ; '''
		ret

page0_64E:
		push	bc
		push	af
		pop		af
page0_651:
		cp		3Fh ; '?'
		jr		nz, page0_65B
		ld		a, 40h ; '@'
		push	af
		xor		a
		jr		page0_664

page0_65B:
		push	af
page0_65C:
		sub		40h ; '@'
		jr		nz, page0_664
		res		6, (iy+0Ch)
page0_664:
		call	Page0Call_3F51
		pop		af
		push	hl
		push	af
		res		4, (iy+0Ch)
		call	cxPutAway2
		pop		af
		push	af
		ld		b, a
		ld		a, (mcxCurApp)
		cp		47h ; 'G'
		ld		a, b
		jr		z, page0_68E
		bit		0, (iy+9)
		jr		z, page0_68E
		cp		57h ; 'W'
		jr		z, page0_69E
		cp		45h ; 'E'
		jr		z, page0_69E
		cp		4Bh ; 'K'
		jr		z, page0_69E
page0_68E:
		call	Page0Call_3F57
		cp		52h ; 'R'
		jr		z, page0_69E
		call	cxPPutAway
		call	page0_7DE
		call	z, page0_BB1
page0_69E:
		ld		a, (mcxCurApp)
		pop		bc
		push	af
		push	bc
		cp		52h ; 'R'
		jr		nz, page0_6B4
		call	Page0Call_3F5D
		call	sub_73D
		call	page0_7DE
		call	z, page0_BB1
page0_6B4:
		ld		hl, 858Dh
		ld		de, 859Bh
		ld		bc, 0Eh
		ldir
		ld		a, (appFlagsAddr)
		ld		(de), a
		pop		af
		push	af
		cp		4Ah ; 'J'
		jr		nz, page0_6CE
		call	Page0Call_3F63
		jr		page0_707

page0_6CE:
		cp		44h ; 'D'
		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	Page0Call_3F69
		jr		nz, page0_707
		ld		a, 40h ; '@'
		ld		(mcxCurApp), a
		ld		hl, Page0Call_3F6F
		ld		a, (iy+12h)
		push	af
		ld		a, 40h ; '@'
		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		52h ; 'R'
		jr		z, page0_717
		set		0, (iy+11h)
page0_717:
		pop		af
page0_718:
		pop		hl
		ld		(mcxCurApp), a
		pop		af
sub_71D:
		bit		2, (iy+36h)
		call	nz, Page0Call_3F75
		push	af
		set		5, (iy+1)
		call	sub_7A1
		res		5, (iy+1)
		pop		bc
		ret

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

cxPutAway:
		call	cxPutAway2
sub_73D:
		call	cxPPutAway
page0_740:
		res		0, (iy+11h)
		ret

cxPutAway2:
		bit		5, (iy+25h)
		ret		nz
		push	hl
		ld		hl, (mcxPPutAway)
page0_74E:
		call	sub_77F
		jr		page0_762

cxPPutAway:
		bit		5, (iy+25h)
		ret		nz
		push	hl
		ld		hl, (mcxPutAway)
		call	sub_77F
		call	zero_basic_prog
page0_762:
		pop		hl
page0_763:
		jp		page0_3A4

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	sub_7A1
		pop		af
		call	sub_630
		ret

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

sub_7A1:
		jp		(hl)

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

monErrHand:
		ld		hl, 7ADh
		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, 858Dh
		ld		bc, 0Ch
		ldir
		ld		a, (hl)
		ld		(appFlagsAddr), a
		in		a, (6)
		ld		(mcxPage), a
		ret

page0_7DE:
		ld		b, a
		ld		a, (mcxCurApp)
		cp		58h ; 'X'
		ld		a, b
		ret

handleONKeyPress:
		in		a, (2)
		and		80h ; ''
		jr		z, page0_7F2
		in		a, (20h)
		ld		e, a
		xor		a
		out		(20h), a
page0_7F2:
		ld		b, 0
page0_7F4:
		ld		hl, 1016h
page0_7F7:
		in		a, (4)
		and		8
		cp		b
		ld		b, a
		jr		nz, page0_7F4
		dec		hl
		ld		a, l
		or		h
		jr		nz, page0_7F7
		cp		b
		jr		z, page0_834
		in		a, (7)
		ld		b, a
		in		a, (2)
		and		80h ; ''
		jr		z, page0_818
		ld		a, e
		out		(20h), a
		ld		a, b
		cp		81h ; 'g
		jr		page0_81D
page0_818:
		ld		a, b
		and		4Fh ; 'O'
		cp		41h ; 'A'
page0_81D:
		jp		nz, page0_A9B
		bit		3, (iy+9)
		ret		nz
		pop		hl
		sub		a
		push	af
		ld		a, 8
		out		(3), a
		pop		af
		out		(3), a
		ld		a, 11h
		jp		noSecondHardwareTimer

page0_834:
		in		a, (2)
		and		80h ; ''
		jr		z, page0_83D
		ld		a, e
		out		(20h), a
page0_83D:
		ld		a, 76h ; 'v'
		out		(4), a
		in		a, (2)
		and		80h ; ''
		jr		z, page0_84D
		in		a, (7)
		cp		81h ; 'g
		jr		page0_853
page0_84D:
		in		a, (7)
		and		4Fh ; 'O'
		cp		41h ; 'A'
page0_853:
		jp		nz, page0_952
		bit		3, (iy+12h)
		jp		z, page0_8E4
		bit		5, (iy+33h)
		jr		nz, page0_869
		bit		7, (iy+28h)
		jr		z, PowerOff

page0_869:
		set		3, (iy+28h)
		ret

PowerOff:
		ld		a, 8
		push	af
		ld		a, 8
		out		(3), a
		pop		af
		out		(3), a
		call	TurnOffLCD
		bit		2, (iy+24h)
		jr		z, page0_887
		bit		2, (iy+8)
		jr		z, page0_8C6
page0_887:
		call	page0_BB1
		call	page0_B76
		call	Page0Call_3E79
		set		4, (iy+1)
		ld		hl, 9D8Dh
		set		0, (hl)
		call	forcecmd
		ld		hl, 9D8Dh
		res		0, (hl)
		res		4, (iy+1)
		bit		1, (iy+33h)
		call	nz, Page0Call_3E7F
page0_8AC:
		call	Page0Call_3E85
		ld		a, 8
		out		(3), a
		call	TurnOffLCD
		ld		hl, 89F9h
		bit		2, (hl)
		res		2, (hl)
		call	nz, Page0Call_3E8B
		call	calculateRAMChecksum
		ld		(unk_85BE), hl
page0_8C6:
		res		4, (iy+33h)
		res		5, (iy+33h)
		ld		a, 36h ; '6'
		out		(4), a
		ld		a, 11h
		out		(3), a
		res		3, (iy+12h)
		res		3, (iy+9)
		ex		af, af'
		exx
		ei

page0_8E1:
		halt
		jr		page0_8E1

page0_8E4:
		res		7, (iy+0Fh)
		call	ApdSetup
		bit		3, (iy+9)
		jr		nz, page0_94D
page0_8F1:
		set		3, (iy+9)
		ld		hl, 89F8h
		bit		4, (hl)
		res		4, (hl)
		jr		nz, page0_92C
		res		1, (iy+12h)
		bit		2, (iy+24h)
		jr		z, page0_90D
		bit		2, (hl)
		jp		z, badRAMChecksum_doRAMReset
page0_90D:
		ld		sp, (onSP)
		call	Page0Call_3E91
		xor		a
		ld		(MenuCurrent), a
		call	refreshSystemRAM
		call	sub_ADA
		call	Page0Call_3E97
		bit		6, (iy+0Ch)
		call	nz, Page0Call_3E9D
		ei
		jp		Mon

page0_92C:
		bit		0, (iy+33h)
		call	nz, Page0Call_3EA3
		ld		a, 8
		push	af
		ld		a, 8
		out		(3), a
		pop		af
		out		(3), a
		call	Page0Call_3EA9
		pop		de
		pop		de
		pop		de
		set		3, (hl)
		call	sub_ADA
		ex		af, af'
		exx
		ei
		reti

page0_94D:
		set		4, (iy+9)
		ret

page0_952:
		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, (unk_85BE)
		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, (unk_85BE)
		call	CpHLDE
		jr		nz, badRAMChecksum_doRAMReset
		dec		de
		ld		(unk_85BE), de
		call	Page0Call_3EB5
		call	page0_B76
		ld		a, 0Bh
		out		(3), a
		res		4, (iy+8)
		call	forcecmd
		jp		page0_8F1

resetRam:
badRAMChecksum_doRAMReset:
		di
		ld		hl, 8000h
		ld		de, 8001h
		ld		bc, 7FFFh
		ld		(hl), 0
		ldir
		ld		hl, 109h
		ld		(localLanguage), hl
page0_9DE:
		ld		a, 0C0h	; ''
		out		(0), a
		ld		sp, 0FFF7h
		call	Page0Call_3EBB
		ld		hl, 7ADh
		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, 7ADh
		call	pushErrorHandleR
		ld		(onSP), sp
		call	Page0Call_3EC1
		jp		Page0Call_3ECD

page0_A26:
		call	GetCSC
		set		2, (iy+9)
		call	Page0Call_3F21
		call	Page0Call_3E8B
		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		3, (iy+5)
		call	Page0Call_3ED3
		res		6, (iy+0Ch)
		ld		a, 40h ; '@'
		jp		newContext0

bootCalc:
		call	page0_B8D
		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

page0_A9B:
		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	Page0Call_3EEB
		call	Chk_Batt_Low_2
		ret		nz
		call	refreshSystemRAM
		in		a, (2)
		bit		0, a
		jp		z, page0_8C6

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, batteriesGood
		res		5, (iy+18h)
batteriesGood:
		ld		a, 76h ; 'v'
		out		(4), a
		ld		a, 0Ah
		call	slowDelayA
		ld		a, 76h ; 'v'
		out		(4), a
		bit		5, (iy+18h)
		ret
refreshSystemRAM:
		ld		bc, 2710h
		ld		hl, 8000h
page0_B30:
		ld		a, (hl)
		ld		(hl), a
		dec		bc
		ld		a, b
		or		c
		jr		nz, page0_B30
		ret

doFunkyTurnOnFromA55AChecksum:
		ld		hl, 0
		ld		(unk_85BE), hl
		ld		sp, (onSP)
		ld		iy, 89F0h
		call	Page0Call_3EE5
		call	page0_B76
		call	forcecmd
		res		4, (iy+8)
		bit		6, (iy+0Ch)
		call	nz, Page0Call_3E9D
		ld		a, 0Bh
		out		(3), a
		call	Page0Call_3EEB
		set		3, (iy+9)
		call	Chk_Batt_Low_2
		call	z, sub_AEF
		bit		0, (iy+33h)
		call	nz, Page0Call_3EA3
		ei
		jp		Mon

page0_B76:
		call	page0_B86
		jr		z, page0_B82
		xor		a
		ld		(MenuCurrent), a
		call	Page0Call_3EF1

page0_B82:
		call	Page0Call_3EF7
		ret

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

page0_B8D:
		call	NZIf83Plus
		ret		nz
		jr		page0_B9C

page0_B93:
forceSlowSpeed:
		call	NZIf83Plus
		ret		nz
		in		a, (20h)
		ld		(oldSESpeed), a
page0_B9C:
		push	af
		xor		a
		out		(20h), a
		pop		af
		ret

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

SetExSpeed:
		or		a
page0_BAF:
		jr		z, page0_B8D


page0_BB1:
		push	af
		call	NZIf83Plus
		jr		nz, page0_BBB
		ld		a, 1
		out		(20h), a
page0_BBB:
		pop		af
		ret

page0_BBD:
		pop		af
		push	hl
		ld		hl, 9CAEh
		dec		(hl)
		pop		hl
		call	NZIf83Plus
		jr		nz, page0_BCD
		and		1
		out		(20h), a
page0_BCD:
		pop		af
		pop		af
		pop		af
		ret