Reset::
	di
	call TurnSoundOff
	xor a
	ld [hMapAnims], a
	call ClearPalettes
	xor a
	ld [rIF], a
	ld a, 1 ; VBlank int
	ld [rIE], a
	ei

	; disable joypad
	ld hl, wcfbe
	set 7, [hl]

	ld c, 32
	call DelayFrames

	jr Init_NoDI

_Start::
	cp $11
	ld a, $1
	jr z, .cgb
	xor a
.cgb
	ld [hCGB], a
	; fallthrough

Init::
	di
	; fallthrough

Init_NoDI:
	xor a
	ld [rIF], a
	ld [rIE], a
	ld [rRP], a
	ld [rSCX], a
	ld [rSCY], a
	ld [rSB], a
	ld [rSC], a
	ld [rWX], a
	ld [rWY], a
	ld [rTMA], a
	ld [rTAC], a
	ld [$d000], a

	ld a, (1 << rTAC_ON) | rTAC_65536_HZ ; Start timer at 262144Hz
	ld [rTAC], a

; Clear WRAM bank 0
	xor a
	ld hl, $c000
	ld bc, $d000 - $c000
.byteFill
	ld [hli], a
	dec c
	jr nz, .byteFill
	dec b
	jr nz, .byteFill

	ld sp, wStack

; Clear HRAM
	ld a, [hCGB]
	push af
	xor a
	ld hl, HRAM_START
	ld bc, HRAM_END - HRAM_START
	call ByteFill
	pop af
	ld [hCGB], a

	and a
	jr nz, .cgb
; In DMG, Nintendo logo still persists after booting, so we need to fade that out
	ld a, 7
	ld [hVBlank], a
	ld a, 1 << VBLANK
	ld [rIE], a
	ei
	ld hl, DMGFadeToWhiteTable
	callba DMGFade
	di
	xor a
	ld [hVBlank], a
	ld [rIF], a
	ld [rIE], a
.cgb	
	ld [rBGP], a
	ld [rOBP0], a
	ld [rOBP1], a

.wait
	ld a, [rLY]
	cp 145
	jr nz, .wait

	xor a
	ld [rLCDC], a

	call ClearWRAM
	call ClearVRAM
	call ClearSprites

	; load the current build number to WRAM, in a place that should not be modified, ever
	ld hl, wBuildNumberCheck
	ld a, BUILD_NUMBER >> 8
	ld [hli], a
	ld [hl], BUILD_NUMBER & $ff

	; quickly initialize and randomize the RNG state
	sbk BANK(sRNGState)
	ld hl, sRNGState
	ld b, 0
	ld a, [hli]
	or b
	ld a, [hli]
	or b
	ld a, [hli]
	or b
	or [hl]
	jr nz, .doNotInitializeRNG
	ld hl, sRNGState
	ld a, 251
	ld [hli], a
	ld a, 241
	ld [hli], a
	ld a, 41
	ld [hli], a
	ld [hl], 101
.doNotInitializeRNG
	ld hl, sRNGState
	ld de, wRNGState
	ld bc, 4
	push hl
	push de
	push bc
	rst CopyBytes
	call Random
	pop bc
	pop hl
	pop de
	rst CopyBytes
	scls

	callba LoadPushOAM

	xor a
	ld [hMapAnims], a
	ld [hSCX], a
	ld [hSCY], a
	ld [rJOYP], a

	ld a, $8 ; HBlank int enable
	ld [rSTAT], a

	ld a, $90
	ld [hWY], a
	ld [rWY], a

	ld a, 7
	ld [hWX], a
	ld [rWX], a

	ld a, %11100011
	; LCD on
	; Win tilemap 1
	; Win on
	; BG/Win tiledata 0
	; BG Tilemap 0
	; OBJ 8x8
	; OBJ on
	; BG on
	ld [rLCDC], a

	ld a, CONNECTION_NOT_ESTABLISHED
	ld [hSerialConnectionStatus], a

	ld a, vWindowMap / $100
	ld [hBGMapAddress + 1], a
	xor a ; vWindowMap % $100
	ld [hBGMapAddress], a

	callba StartClock

	xor a
	ld [MBC3LatchClock], a
	ld [MBC3SRamEnable], a

	ld a, [hCGB]
	and a
	call nz, DoubleSpeed

	xor a
	ld [rIF], a
	ld a, 1 << VBLANK | 1 << SERIAL ; VBlank, LCDStat, Timer, Serial interrupts
	ld [rIE], a
	call LoadLCDCode
	ei

	call DelayFrame

	call TurnSoundOff
	xor a
	ld [wMapMusic], a
	jpba GameInit

ClearVRAM::
; Wipe VRAM banks 0 and 1

	vbk BANK(vStandingFrameTiles)
	call .clear

	vbk BANK(vObjTiles)
.clear
	ld hl, vObjTiles
	ld bc, VRAM_End - vObjTiles
	xor a
	jp ByteFill

ClearWRAM::
; Wipe swappable WRAM banks (1-7)
	ld d, 7 ; count backwards from bank 7
.loop
	wbk d
	xor a
	ld hl, $d000
	ld bc, $1000
	call ByteFill
	dec d
	jr nz, .loop
	ret
