From 0ee68db2c6ba3199eb7a11e7a1d90025602bfde9 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Tue, 24 Dec 2024 14:10:23 +0100 Subject: [PATCH] Made vram access more secure when loading maps. A row is now guaranteed to wait for the *next* vblank before proceeding to draw. Additionall we are now disabling objects when drawing a new room. --- src/map.s | 24 +++++++++++++++++------- src/mem.s | 6 ++++++ src/video.s | 11 +++++++++++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/map.s b/src/map.s index 9de9f4d..874aab8 100644 --- a/src/map.s +++ b/src/map.s @@ -155,12 +155,15 @@ room_goto_player_pos: ; curr_room_exits: ptr to exits table ; a: direction EAST, WEST, NORTH, SOUTH, EXIT_SPECIAL room_goto: + ; TODO: save current tables back ; save a for later push af - - ; TODO: save current tables back call disableinterrutpts - call vblank_wait + + ; disable objects + ld a, LCDCF_ON | LCDCF_BGON | LCDF_OBJ_SIZE + ld [RLCD], a + ;call lcd_off @@ -189,8 +192,15 @@ room_goto: ; its player's turn again ld a, WHO_PLAYER ld [who], a - - ;call lcd_on + + ; restore palette + ld a, BGP + ld [RBGP], a + + ; clear soam + call shadow_oam_clear + + call lcd_on call enableinterrupts ret @@ -246,7 +256,7 @@ room_row_draw: ; wait for vlbank for every row if the screen is not off ld a, [RLCD] cp a, 0 - call nz, vblank_wait + call nz, next_vblank_wait ld b, ROOM_W @@ -288,7 +298,7 @@ room_row_draw: ; wait for vlbank for every row if the screen is not off ld a, [RLCD] cp a, 0 - call nz, vblank_wait + call nz, next_vblank_wait ; almost the same loop again ; for the second row diff --git a/src/mem.s b/src/mem.s index 7ca3754..fc66033 100644 --- a/src/mem.s +++ b/src/mem.s @@ -57,6 +57,12 @@ memset: jp nz, @loop ret +shadow_oam_clear: + ld bc, OBJSMAX * oamsize + ld hl, shadow_oam + ld d, 0 + jp memset + ; dma shadow oam to oam ; registers: diff --git a/src/video.s b/src/video.s index d80eb5d..a81f6bd 100644 --- a/src/video.s +++ b/src/video.s @@ -59,6 +59,17 @@ vblank_wait: jp c, vblank_wait ret +not_vblank_wait: + ld a, [RLY] + cp a, 1 + jp nz, not_vblank_wait + ret + +next_vblank_wait: + call not_vblank_wait + jp vblank_wait + + ; turns off the lcd lcd_off: ; *never* turn off LCD without waiting -- 2.30.2