From 59072deeed9cb2dd7679f4336b1a9c54cfecfad4 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Sun, 11 Jan 2026 08:27:54 +0100 Subject: [PATCH] mem: moved tiles to a dedicated ram bank. Added tile canary --- assets | 2 +- maps/l1.inc | 33 ++++++++++++++++++++++----------- src/config.s | 3 +++ src/defs.s | 5 +++-- src/mem.s | 40 ++++++++++++++++------------------------ src/wram.s | 17 ++++++++++++++--- 6 files changed, 59 insertions(+), 41 deletions(-) diff --git a/assets b/assets index 7d90515..efe93c1 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 7d9051571e7aa6295d9c02bfb59e4769d233ff37 +Subproject commit efe93c14d63cb5adabf47a848b19a25090584d1c diff --git a/maps/l1.inc b/maps/l1.inc index d1d94f3..56e9459 100644 --- a/maps/l1.inc +++ b/maps/l1.inc @@ -1,17 +1,28 @@ ; this map was generated by tmx2map.py .db 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 -.db 0x0, 0x1, 0x1, 0x1, 0x1, 0x0, 0x0, 0x0, 0x1, 0x1, 0x1, 0x1, 0x0, 0x0, 0x0, 0x1, 0x1, 0x0, 0x0, 0x0 -.db 0x0, 0x1, 0x1, 0x1, 0x1, 0x2, 0x1, 0x2, 0x1, 0x1, 0x1, 0x1, 0x2, 0x1, 0x2, 0x1, 0x1, 0x0, 0x0, 0x0 -.db 0x0, 0x1, 0x1, 0x1, 0x1, 0x0, 0x0, 0x0, 0x1, 0x1, 0x1, 0x1, 0x0, 0x0, 0x0, 0x1, 0x1, 0x0, 0x0, 0x0 -.db 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0 -.db 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0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ff --git a/src/config.s b/src/config.s index 0ec3b14..e4bfd3e 100644 --- a/src/config.s +++ b/src/config.s @@ -1,3 +1,6 @@ #define DEBUG_UI +; enables debug canary values in wram +#define DEBUG_CANARY + diff --git a/src/defs.s b/src/defs.s index 91c5aa1..775c954 100644 --- a/src/defs.s +++ b/src/defs.s @@ -7,6 +7,7 @@ .def int OAMDMAFN = 0xFF80 #define WRAM 0xC000 +#define WRAM1 0xD000 #define WRAMLEN 0xFFF #define ACT_MAX 32 @@ -27,8 +28,8 @@ #define UI_TILE_HEIGHT 4 ; player position offset to get center of tile -#define MAP_W 20 -#define MAP_H 14 +#define MAP_W 22 +#define MAP_H 22 #define MAP_TILES (MAP_W * MAP_H) #define RENDER_BUF_W 20 diff --git a/src/mem.s b/src/mem.s index 26ff252..c0b898b 100644 --- a/src/mem.s +++ b/src/mem.s @@ -1,8 +1,15 @@ mem_init: ; clear wram - ld a, 0 + ld d, 0 ld hl, WRAM ld bc, WRAMLEN + call memset + + ld d, 0 + ld hl, WRAM1 + ; only clear half of the ram here + ; because the stack is using this bank too + ld bc, WRAMLEN/2 call memset call map_globals_init @@ -30,15 +37,22 @@ mem_init: ; set initial game state ptr ld hl, new_game call game_set_state - + +#ifdef DEBUG_CANARY + ; canary values for large buffers ld a, 0xde + ld [tiles_canary], a ld [render_canary], a ld a, 0xad + ld [tiles_canary+1], a ld [render_canary+1], a ld a, 0xbe + ld [tiles_canary+2], a ld [render_canary+2], a ld a, 0xef + ld [tiles_canary+3], a ld [render_canary+3], a +#endif ret @@ -64,28 +78,6 @@ memcpy: ret - ; same as memcpy - ; but only performs a write - ; if the value is currently 0 -memcpyz: -@loop: - ; skip check - ld a, [hl] - cp a, 0 - jr nz, @skip REL - - ld a, [de] - ld [hl], a -@skip: - inc hl - inc de - dec bc - ld a, b - or a, c - jp nz, @loop - - ret - ; sets a memory location to a specific value ; inputs: ; hl: destination diff --git a/src/wram.s b/src/wram.s index 717fdad..110f822 100644 --- a/src/wram.s +++ b/src/wram.s @@ -87,9 +87,6 @@ tmp_x: .adv 1 ; current map ptr map: .adv 2 - ; ptr to current tile to be updated -tiles: .adv t_size * MAP_TILES -tiles_end: .adv 0 ; collision related data ; y/x positions @@ -111,4 +108,18 @@ minimap_buffer: .adv 9 combat: .adv combat_size render_buffer: .adv RENDER_BUF_TILES + +#ifdef DEBUG_CANARY render_canary: .adv 4 +#endif + +; tile memory +.org 0xC800 + + ; ptr to current tile to be updated +tiles: .adv t_size * MAP_TILES +tiles_end: .adv 0 + +#ifdef DEBUG_CANARY +tiles_canary: .adv 4 +#endif -- 2.30.2