From 06cd13a17f5ddba4ead3adf05bf58088785ba8c5 Mon Sep 17 00:00:00 2001 From: Rickey Fehr Date: Sun, 21 Sep 2025 13:14:22 -0700 Subject: [PATCH] Centralize location of memory pools This commit centralizes the location of memory pools and adds an associated script (scripts/get_memory_map.sh) to read the memory map. --- include/card.h | 5 ++++- include/joker.h | 4 ++++ include/pool.h | 25 +++++++++++++++---------- include/sprite.h | 5 ++++- scripts/get_memory_map.sh | 36 ++++++++++++++++++++++++++++++++++++ source/card.c | 8 -------- source/joker.c | 9 --------- source/pool.c | 7 +++++++ source/sprite.c | 7 ------- 9 files changed, 70 insertions(+), 36 deletions(-) create mode 100755 scripts/get_memory_map.sh diff --git a/include/card.h b/include/card.h index 40f2dc4..63f4ea7 100644 --- a/include/card.h +++ b/include/card.h @@ -6,6 +6,9 @@ #include "sprite.h" +#define MAX_CARDS (NUM_SUITS * NUM_RANKS) +#define MAX_CARDS_ON_SCREEN 16 + #define CARD_TID 0 #define CARD_SPRITE_OFFSET 16 #define CARD_PB 0 @@ -69,4 +72,4 @@ void card_object_set_selected(CardObject* card_object, bool selected); bool card_object_is_selected(CardObject* card_object); Sprite* card_object_get_sprite(CardObject* card_object); -#endif // CARD_H \ No newline at end of file +#endif // CARD_H diff --git a/include/joker.h b/include/joker.h index 4e9b9c7..cf9a04a 100644 --- a/include/joker.h +++ b/include/joker.h @@ -8,6 +8,10 @@ #include "game.h" #include "graphic_utils.h" +// This won't be more than the number of jokers in your current deck +// plus the amount that can fit in the shop, 8 should be fine. For now... +#define MAX_ACTIVE_JOKERS 8 + #define JOKER_TID (MAX_HAND_SIZE + MAX_SELECTION_SIZE) * JOKER_SPRITE_OFFSET // Tile ID for the starting index in the tile memory #define JOKER_SPRITE_OFFSET 16 // Offset for the joker sprites #define JOKER_BASE_PB 4 // The starting palette index for the jokers diff --git a/include/pool.h b/include/pool.h index bb04a6a..05cfae1 100644 --- a/include/pool.h +++ b/include/pool.h @@ -22,8 +22,8 @@ int pool_bm_get_free_idx(PoolBitmap *bm); type * objects; \ u32 max_entries; \ } type##Pool; \ - type *pool_get_##type(type##Pool* pool); \ - void pool_free_##type(type##Pool *pool, type *obj); \ + type *pool_get_##type(); \ + void pool_free_##type(type *obj); \ void pool_init_##type(type##Pool *pool); #define POOL_DEFINE_TYPE(type, capacity) \ @@ -38,20 +38,25 @@ int pool_bm_get_free_idx(PoolBitmap *bm); .objects = type##_storage, \ .max_entries = capacity, \ }; \ - type * pool_get_##type(type##Pool *pool) \ + type * pool_get_##type() \ { \ - int free_offset = pool_bm_get_free_idx(&pool->bm); \ + int free_offset = pool_bm_get_free_idx(&type##_pool.bm); \ if(free_offset == -1) return NULL; \ - return &pool->objects[free_offset]; \ + return &type##_pool.objects[free_offset]; \ } \ - void pool_free_##type(type##Pool *pool, type *entry) \ + void pool_free_##type(type *entry) \ { \ if(entry == NULL) return; \ - int offset = entry - &pool->objects[0]; \ - pool_bm_clear_idx(&pool->bm, offset); \ + int offset = entry - &type##_pool.objects[0]; \ + pool_bm_clear_idx(&type##_pool.bm, offset); \ } -#define POOL_GET(type) pool_get_##type(&type##_pool) -#define POOL_FREE(type, obj) pool_free_##type(&type##_pool, obj) +#define POOL_GET(type) pool_get_##type() +#define POOL_FREE(type, obj) pool_free_##type(obj) + +#define POOL_ENTRY(name, capacity) \ +POOL_DECLARE_TYPE(name); +#include "pools.def" +#undef POOL_ENTRY #endif // POOL_H diff --git a/include/sprite.h b/include/sprite.h index b3f27b8..e8f9784 100644 --- a/include/sprite.h +++ b/include/sprite.h @@ -5,6 +5,9 @@ #include #define CARD_SPRITE_SIZE 32 +#define MAX_SPRITES 128 +#define MAX_SPRITE_OBJECTS 16 + typedef struct { @@ -63,4 +66,4 @@ Sprite* sprite_object_get_sprite(SpriteObject* sprite_object); void sprite_object_set_focus(SpriteObject* sprite_object, bool focus); bool sprite_object_is_focused(SpriteObject* sprite_object); -#endif // SPRITE_H \ No newline at end of file +#endif // SPRITE_H diff --git a/scripts/get_memory_map.sh b/scripts/get_memory_map.sh new file mode 100755 index 0000000..b786e68 --- /dev/null +++ b/scripts/get_memory_map.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash + +set -euo pipefail + +# Run this from the root directory of the project, not the scripts directory +POOL_DEF_FILE='./include/pools.def' +ELF_FILE='./build/balatro-gba.elf' +READELF='/opt/devkitpro/devkitARM/bin/arm-none-eabi-readelf' + +get_pool_names() { + grep POOL_ENTRY "$POOL_DEF_FILE" | sed -n 's@.*(\(.*\)).*@\1@p' | sed 's@,@@g' | cut -d ' ' -f 1 +} + +TOTAL_BYTES=0 + +echo "--------------------------------------------" +printf "%-16s| %-10s | %s\n" "Object" "location" "size" +echo "--------------------------------------------" + +for name in $(get_pool_names); do + output="$( \ + "$READELF" -sW "$ELF_FILE" | \ + grep "${name}_" | \ + grep OBJECT | \ + grep storage | sed -E 's@ +@ @g; s@^ @@' \ + )" + + location="$(cut -d ' ' -f 2 <<< $output)" + size="$(cut -d ' ' -f 3 <<< $output)" + TOTAL_BYTES=$(( TOTAL_BYTES + size )) + + printf "%-16s| 0x%s | %-8u\n" "$name" "$location" "$size" +done + +echo "--------------------------------------------" +echo Total bytes used: $TOTAL_BYTES diff --git a/source/card.c b/source/card.c index 7317d77..be01b93 100644 --- a/source/card.c +++ b/source/card.c @@ -11,14 +11,6 @@ #include "pool.h" -#define MAX_CARDS_PER_HAND (NUM_SUITS * NUM_RANKS) -#define MAX_CARDS_ON_SCREEN 16 - -POOL_DECLARE_TYPE(Card); -POOL_DEFINE_TYPE(Card, MAX_CARDS_PER_HAND); -POOL_DECLARE_TYPE(CardObject); -POOL_DEFINE_TYPE(CardObject, MAX_CARDS_ON_SCREEN); - // Card sprites lookup table. First index is the suit, second index is the rank. The value is the tile index. const static u16 card_sprite_lut[NUM_SUITS][NUM_RANKS] = { {0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192}, diff --git a/source/joker.c b/source/joker.c index 03bc838..e9447f1 100644 --- a/source/joker.c +++ b/source/joker.c @@ -29,15 +29,6 @@ static const unsigned short *joker_gfxPal[] = #undef DEF_JOKER_GFX }; -// This won't be more than the number of jokers in your current deck -// plus the amount that can fit in the shop, 8 should be fine. For now... -#define MAX_ACTIVE_JOKERS 8 - -POOL_DECLARE_TYPE(Joker) -POOL_DEFINE_TYPE(Joker, MAX_ACTIVE_JOKERS) -POOL_DECLARE_TYPE(JokerObject) -POOL_DEFINE_TYPE(JokerObject, MAX_ACTIVE_JOKERS) - const static u8 edition_price_lut[MAX_EDITIONS] = { 0, // BASE_EDITION diff --git a/source/pool.c b/source/pool.c index d8c76fa..e2f150e 100644 --- a/source/pool.c +++ b/source/pool.c @@ -34,3 +34,10 @@ int pool_bm_get_free_idx(PoolBitmap *bm) return -1; } + + +#define POOL_ENTRY(name, capacity) \ +POOL_DEFINE_TYPE(name, capacity); +#include "sprite.h" +#include "pools.def" +#undef POOL_ENTRY diff --git a/source/sprite.c b/source/sprite.c index 7d16447..617a079 100644 --- a/source/sprite.c +++ b/source/sprite.c @@ -9,19 +9,12 @@ #include #include -#define MAX_SPRITES 128 #define MAX_AFFINES 32 #define SPRITE_FOCUS_RAISE_PX 10 OBJ_ATTR obj_buffer[MAX_SPRITES]; OBJ_AFFINE *obj_aff_buffer = (OBJ_AFFINE*)obj_buffer; -POOL_DECLARE_TYPE(SpriteObject) -POOL_DEFINE_TYPE(SpriteObject, MAX_SPRITES) - -POOL_DECLARE_TYPE(Sprite) -POOL_DEFINE_TYPE(Sprite, MAX_SPRITES) - static bool free_affines[MAX_AFFINES] = {false}; // Sprite methods