Use LCD STAT interrupt more efficiently (#2)

This commit is contained in:
xCrystal 2024-03-19 19:49:36 +01:00
parent 5f0cdcaa85
commit 0d8fb09612
14 changed files with 41 additions and 16 deletions

View File

@ -36,7 +36,8 @@ DEF LCD_STAT EQU 1
DEF TIMER EQU 2
DEF SERIAL EQU 3
DEF JOYPAD EQU 4
DEF IE_DEFAULT EQU (1 << SERIAL) | (1 << TIMER) | (1 << LCD_STAT) | (1 << VBLANK)
DEF IE_DEFAULT EQU (1 << SERIAL) | (1 << LCD_STAT) | (1 << VBLANK)
DEF IE_DEFAULT_WO_LCD_STAT EQU (1 << SERIAL) | (1 << VBLANK)
; OAM attribute flags
DEF OAM_TILE_BANK EQU 3

View File

@ -104,7 +104,7 @@ endc
xor a
ldh [rIF], a
ld a, (1 << JOYPAD) | (1 << SERIAL) | (1 << TIMER) | (1 << VBLANK)
ld a, (1 << JOYPAD) | (1 << SERIAL) | (1 << VBLANK)
ldh [rIE], a
call Link_CopyRandomNumbers
@ -280,7 +280,7 @@ endc
xor a
ldh [rIF], a
ld a, (1 << JOYPAD) | (1 << SERIAL) | (1 << TIMER) | (1 << VBLANK)
ld a, (1 << JOYPAD) | (1 << SERIAL) | (1 << VBLANK)
ldh [rIE], a
ld de, MUSIC_NONE
call PlayMusic

View File

@ -1,6 +1,7 @@
ClearedLevelScreen:
xor a
ldh [hMapAnims], a
ldh [hLCDStatIntRequired], a
ldh [hSCY], a
ld a, -$4
ldh [hSCX], a

View File

@ -14,6 +14,7 @@ GameMenu:
GameMenu_KeepMusic:
xor a
ldh [hMapAnims], a
ldh [hLCDStatIntRequired], a
call ClearTilemap
call LoadFrame
call LoadStandardFont

View File

@ -4,6 +4,7 @@ LevelSelectionMenu::
ldh [hMapAnims], a
ldh [hSCY], a
ldh [hSCX], a
ldh [hLCDStatIntRequired], a
ld a, 1 << DONT_CLEAR_SHADOW_OAM_IN_SPRITE_ANIMS_F
ld [wStateFlags], a

View File

@ -14,6 +14,7 @@ MainMenu:
.loop
xor a
ldh [hMapAnims], a
ldh [hLCDStatIntRequired], a
call ClearTilemap
call LoadFrame
call LoadStandardFont

View File

@ -13,6 +13,8 @@ IntroSequence:
; fallthrough
StartTitleScreen:
ld a, TRUE
ldh [hLCDStatIntRequired], a
ldh a, [rSVBK]
push af
ld a, BANK(wLYOverrides)

View File

@ -8,6 +8,9 @@ Credits::
.okay
ld [wJumptableIndex], a
ld a, TRUE
ldh [hLCDStatIntRequired], a
ldh a, [rSVBK]
push af
ld a, BANK(wGBCPalettes)

View File

@ -1,4 +1,6 @@
CrystalIntro:
ld a, TRUE
ldh [hLCDStatIntRequired], a
ldh a, [rSVBK]
push af
ld a, BANK(wGBCPalettes)

View File

@ -3,6 +3,8 @@ SECTION "Events", ROMX
OverworldLoop::
xor a ; MAPSTATUS_START
ld [wMapStatus], a
ld a, TRUE
ld [hLCDStatIntRequired], a
.loop
ld a, [wMapStatus]
ld hl, .Jumptable

View File

@ -151,7 +151,8 @@ Init::
xor a
ldh [rIF], a
ld a, IE_DEFAULT
ldh [hLCDStatIntRequired], a
ld a, IE_DEFAULT_WO_LCD_STAT
ldh [rIE], a
ei

View File

@ -156,7 +156,7 @@ Serial_ExchangeByte::
.not_player_1_or_timed_out
ldh a, [rIE]
and (1 << SERIAL) | (1 << TIMER) | (1 << LCD_STAT) | (1 << VBLANK)
and IE_DEFAULT
cp 1 << SERIAL
jr nz, .loop
ld a, [wLinkByteTimeout]
@ -180,7 +180,7 @@ Serial_ExchangeByte::
xor a
ldh [hSerialReceivedNewData], a
ldh a, [rIE]
and (1 << SERIAL) | (1 << TIMER) | (1 << LCD_STAT) | (1 << VBLANK)
and IE_DEFAULT
sub 1 << SERIAL
jr nz, .non_serial_interrupts_enabled
@ -212,7 +212,7 @@ Serial_ExchangeByte::
.timed_out
ldh a, [rIE]
and (1 << SERIAL) | (1 << TIMER) | (1 << LCD_STAT) | (1 << VBLANK)
and IE_DEFAULT
cp 1 << SERIAL
ld a, SERIAL_NO_DATA_BYTE
ret z

View File

@ -35,12 +35,12 @@ VBlank::
reti
.VBlanks:
dw VBlank0
dw VBlank1
dw VBlank2
dw VBlank3
dw VBlank4
dw VBlank5
dw VBlank0 ; normal operation
dw VBlank1 ; battle transition, battle anims (double speed mode)
dw VBlank2 ; link
dw VBlank3 ; battle anims (regular speed mode)
dw VBlank4 ; printer
dw VBlank5 ; credits
dw VBlank6
dw VBlank0 ; just in case
@ -118,6 +118,7 @@ VBlank0::
ldh [hVBlankOccurred], a
; if hWindowHUDLY is active, enable interrupts so the LCD interrupt can trigger
; while non-vblank-sensitive operations are executed.
ldh a, [hWindowHUDLY]
and a
jr z, .next2
@ -163,10 +164,9 @@ VBlank0::
ldh a, [hROMBankBackup]
rst Bankswitch
; if hWindowHUDLY is not active, we're done
ldh a, [hWindowHUDLY]
and a
ret z
jr z, .no_window_hud
; interrupts must be enabled in the cycle that rLY becomes [hWindowHUDLY] to prevent flickering
; wait until [hWindowHUDLY] - [rLY] is NOT between 0 and 2 before disabling interrupts
@ -178,9 +178,17 @@ VBlank0::
cp 2 + 1
jr c, .wait_loop
; restore normal interrupts: enable ints besides joypad and let vblank finish
.no_window_hud
; if hWindowHUDLY is active, only LCD_STAT is enabled.
; if hLCDStatIntRequired changeed during last frame, interrupts to request have changed.
; so, restore normal interrupts: enable ints besides joypad (and maybe lcd stat) and let vblank finish.
di
ld a, [hLCDStatIntRequired]
and a
ld a, IE_DEFAULT
jr nz, .enable_ints
ld a, IE_DEFAULT_WO_LCD_STAT
.enable_ints
ldh [rIE], a
ret

View File

@ -13,6 +13,8 @@ hVBlankOccurred:: db
hROMBank:: db
hVBlank:: db
hLCDStatIntRequired:: db
hMapEntryMethod:: db
hJoypadReleased:: db