From: Lukas Krickl Date: Sun, 10 Aug 2025 06:17:24 +0000 (+0200) Subject: vblank: Added way to overwrite vblank handler at runtime X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=9b25466a8bd05a8d283d0f4ade8838b7e412428b;p=gbrg%2F.git vblank: Added way to overwrite vblank handler at runtime This is done by having 4 bytes of self modifying code in wram. It offers enough space for a call and a reti instruction. Currently it is simply hard-set to the default handler, but if a custom handler is ever needed it can be put there. --- diff --git a/src/jmp.inc b/src/jmp.inc index 26d62f5..e5eafac 100644 --- a/src/jmp.inc +++ b/src/jmp.inc @@ -33,7 +33,7 @@ sys_farcall: ; vblank 0x40 ;============= vec_vblank: - call vblank + jp vblank_jp reti .fill 0, 0x48 - $ diff --git a/src/sys.s b/src/sys.s index d2cec96..8b89195 100644 --- a/src/sys.s +++ b/src/sys.s @@ -97,3 +97,22 @@ reset_unit_obj: xor a, a ld [unit_sprite], a ret + + ; writes default vblank jmp vector +setup_vblank_jp_smc: + ld hl, vblank_jp + + ; write instruction + ld a, 0xCD ; call + ld [hl+], a + + ; write address + ld a, vblank LO + ld [hl+], a + ld a, vblank HI + ld [hl+], a + + ; write reti + ld a, 0xD9 ; reti + ld [hl], a + ret diff --git a/src/video.s b/src/video.s index ddba5e5..5c0cc13 100644 --- a/src/video.s +++ b/src/video.s @@ -156,6 +156,9 @@ video_init: ld bc, 1024 ld d, UI_WINDOW_BACKGROUND call memset + + ; setup vblank jp + call setup_vblank_jp_smc ret diff --git a/src/wram.s b/src/wram.s index e3c7990..d761532 100644 --- a/src/wram.s +++ b/src/wram.s @@ -34,6 +34,13 @@ shadow_ie: .adv 1 ; status text buffer status_text: .adv 32 + ; self modifying code + ; the vblank interrupt jumps here + ; this should be enough memory for + ; a jp + address + ; and a reti instruction +vblank_jp: .adv 4 + ; ptr to demo inputs ; provides one byte of inputs per frame ; if all buttons are held (value is 0xFF) it indicates the end