Skip to content

Instantly share code, notes, and snippets.

@ISSOtm
Created September 7, 2020 02:15
Show Gist options
  • Save ISSOtm/61089eb7979b4afa677692957b7f31ab to your computer and use it in GitHub Desktop.
Save ISSOtm/61089eb7979b4afa677692957b7f31ab to your computer and use it in GitHub Desktop.
Most of Secret Project's init code
Reset::
di ; Disable interrupts while we set up
; Kill sound
xor a
ldh [rNR52], a
; Wait for VBlank and turn LCD off
.waitVBlank
ldh a, [rLY]
cp SCRN_Y
jr c, .waitVBlank
xor a
ldh [rLCDC], a
; Goal now: set up the minimum required to turn the LCD on again
; A big chunk of it is to make sure the VBlank handler doesn't crash
ld sp, wStackBottom
ld a, BANK(OAMDMA)
; No need to write bank number to HRAM, interrupts aren't active
ld [rROMB0], a
ld hl, OAMDMA
lb bc, OAMDMA.end - OAMDMA, LOW(hOAMDMA)
.copyOAMDMA
ld a, [hli]
ldh [c], a
inc c
dec b
jr nz, .copyOAMDMA
; Reset variables necessary for the VBlank handler to function correctly
; But only those for now
ld a, $80
ldh [rBCPS], a
ldh [rOCPS], a
xor a
ldh [hVBlankFlag], a
ldh [hOAMHigh], a
ldh [hCanSoftReset], a
ldh [hVBlankCopyNbTiles], a
dec a ; ld a, $FF
ldh [hHeldKeys], a
; Clear palettes by making them fully white
lb bc, 8 * 4 * 2, LOW(rBCPD)
.writePalettes
ldh [c], a
ldh [rOCPD], a
dec b
jr nz, .writePalettes
; ld a, $FF
ldh [rLYC], a
ldh [hScanlineFXBuffer2], a
ld a, LOW(hScanlineFXBuffer2)
ldh [hWhichScanlineBuffer], a
; Load the correct ROM bank for later
; Important to do it before enabling interrupts
ld a, BANK(Intro)
ldh [hCurROMBank], a
ld [rROMB0], a
; Select wanted interrupts here
; You can also enable them later if you want
ld a, IEF_VBLANK | IEF_LCDC
ldh [rIE], a
xor a
ei ; Only takes effect after the following instruction
ldh [rIF], a ; Clears "accumulated" interrupts
; Init shadow regs
; xor a
ldh [hSCY], a
ldh [hSCX], a
ldh [hWY], a
ldh [hWX], a
ld a, STATF_LYC
ldh [rSTAT], a
ld a, LCDCF_ON | LCDCF_BGON
ldh [hLCDC], a
; And turn the LCD on!
ldh [rLCDC], a
; Clear OAM, so it doesn't display garbage
; This will get committed to hardware OAM after the end of the first
; frame, but the hardware doesn't display it, so that's fine.
ld hl, wShadowOAM
ld c, NB_SPRITES * 4
xor a
rst MemsetSmall
ld a, h ; ld a, HIGH(wShadowOAM)
ldh [hOAMHigh], a
; `Intro`'s bank has already been loaded earlier
ld a, [wSkipIntro]
and a
call z, Intro
; Init "global" vars now
xor a
ld [wNbMenus], a
; ... SNIP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment