vblank: Added way to overwrite vblank handler at runtime
authorLukas Krickl <lukas@krickl.dev>
Sun, 10 Aug 2025 06:17:24 +0000 (08:17 +0200)
committerLukas Krickl <lukas@krickl.dev>
Sun, 10 Aug 2025 06:17:24 +0000 (08:17 +0200)
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.

src/jmp.inc
src/sys.s
src/video.s
src/wram.s

index 26d62f5d34ac26598b130dda8a941d7ff185ce38..e5eafaca1444a6075137b75703e6e8178dc53005 100644 (file)
@@ -33,7 +33,7 @@ sys_farcall:
 ; vblank 0x40
 ;=============
 vec_vblank:
-  call vblank  
+  jp vblank_jp  
   reti
 
 .fill 0, 0x48 - $
index d2cec9676214a26370afeff975f906eab220ab44..8b89195c75acc919d5dda5c73ab9f984387c065f 100644 (file)
--- 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
index ddba5e53befa4b4f28104dcec4f1792cd4c6d912..5c0cc1355ed9152879ae6da001666076611cc050 100644 (file)
@@ -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
index e3c79902738ab3b399a5452432845ebbe16d5aea..d76153257012e7b4e7ee18e83470f6d226b44edc 100644 (file)
@@ -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