From d8f49bb74b49087b8ac2d32cdf60f0322643e276 Mon Sep 17 00:00:00 2001 From: Ariel A <24759293+aarant@users.noreply.github.com> Date: Mon, 13 May 2024 21:55:08 -0400 Subject: [PATCH] feat: Linkerscript now tracks RAM/ROM usage Based on https://github.com/pret/pokeemerald/pull/1952 --- Makefile | 4 ++-- include/gba/defines.h | 4 +++- ld_script.ld | 46 ++++++++++++++++++++++--------------------- ld_script_modern.ld | 31 +++++++++++++++-------------- 4 files changed, 45 insertions(+), 40 deletions(-) diff --git a/Makefile b/Makefile index c8a72d8f6..63ac6e043 100644 --- a/Makefile +++ b/Makefile @@ -329,7 +329,7 @@ $(OBJ_DIR)/sym_common.ld: sym_common.txt $(C_OBJS) $(wildcard common_syms/*.txt) $(RAMSCRGEN) COMMON $< ENGLISH -c $(C_BUILDDIR),common_syms > $@ $(OBJ_DIR)/sym_ewram.ld: sym_ewram.txt - $(RAMSCRGEN) ewram_data $< ENGLISH > $@ + $(RAMSCRGEN) .sbss $< ENGLISH > $@ ifeq ($(MODERN),0) LD_SCRIPT := ld_script.ld @@ -340,7 +340,7 @@ LD_SCRIPT_DEPS := endif $(ELF): $(LD_SCRIPT) $(LD_SCRIPT_DEPS) $(OBJS) - @cd $(OBJ_DIR) && $(LD) $(LDFLAGS) -T ../../$< -o ../../$@ $(OBJS_REL) $(LIB) + @cd $(OBJ_DIR) && $(LD) $(LDFLAGS) -T ../../$< --print-memory-usage -o ../../$@ $(OBJS_REL) $(LIB) | cat $(FIX) $@ -t"$(TITLE)" -c$(GAME_CODE) -m$(MAKER_CODE) -r$(GAME_REVISION) --silent $(ROM): $(ELF) diff --git a/include/gba/defines.h b/include/gba/defines.h index a5cf7d8ca..0886e9709 100644 --- a/include/gba/defines.h +++ b/include/gba/defines.h @@ -11,7 +11,7 @@ #define EWRAM_DATA __attribute__((section("__DATA,ewram_data"))) #else #define IWRAM_DATA __attribute__((section("iwram_data"))) -#define EWRAM_DATA __attribute__((section("ewram_data"))) +#define EWRAM_DATA __attribute__((section(".sbss"))) #endif #if MODERN @@ -75,6 +75,8 @@ #define TILE_SIZE_4BPP 32 #define TILE_SIZE_8BPP 64 +#define BG_TILE_ADDR_4BPP(n) (void *)(BG_VRAM + (TILE_SIZE_4BPP * (n))) + #define TILE_OFFSET_4BPP(n) ((n) * TILE_SIZE_4BPP) #define TILE_OFFSET_8BPP(n) ((n) * TILE_SIZE_8BPP) diff --git a/ld_script.ld b/ld_script.ld index 3e2c330f5..564213e8d 100644 --- a/ld_script.ld +++ b/ld_script.ld @@ -1,10 +1,16 @@ gNumMusicPlayers = 4; gMaxLines = 0; +MEMORY +{ + EWRAM (rwx) : ORIGIN = 0x2000000, LENGTH = 256K + IWRAM (rwx) : ORIGIN = 0x3000000, LENGTH = 32K + ROM (rx) : ORIGIN = 0x8000000, LENGTH = 32M +} + SECTIONS { - . = 0x2000000; - ewram (NOLOAD) : + .ewram.sbss 0x2000000 (NOLOAD) : ALIGN(4) { gHeap = .; @@ -12,17 +18,14 @@ SECTIONS { . = 0x1C000; INCLUDE "sym_ewram.ld" - src/*.o(ewram_data); + src/*.o(.sbss); *libc.a:impure.o(.data); *libc.a:locale.o(.data); *libc.a:mallocr.o(.data); - . = 0x40000; - } - - . = 0x3000000; + } > EWRAM - iwram (NOLOAD) : + iwram 0x3000000 (NOLOAD) : ALIGN(4) { /* .bss starts at 0x3000000 */ @@ -38,10 +41,9 @@ SECTIONS { *libc.a:sbrkr.o(COMMON); end = .; + } > IWRAM - . = 0x8000; - } - + /* BEGIN ROM DATA */ . = 0x8000000; .text : @@ -308,7 +310,7 @@ SECTIONS { src/minigame_countdown.o(.text); src/berry_fix_program.o(.text); src/debug.o(.text); - } =0 + } > ROM =0 script_data : ALIGN(4) @@ -320,7 +322,7 @@ SECTIONS { data/battle_scripts_2.o(script_data); data/battle_ai_scripts.o(script_data); data/mystery_event_script_cmd_table.o(script_data); - } =0 + } > ROM =0 lib_text : ALIGN(4) @@ -389,7 +391,7 @@ SECTIONS { *libc.a:libcfunc.o(.text); *libc.a:lseekr.o(.text); *libc.a:readr.o(.text); - } =0 + } > ROM =0 .rodata : SUBALIGN(4) @@ -610,7 +612,7 @@ SECTIONS { data/mystery_event_msg.o(.rodata); src/m4a_tables.o(.rodata); data/sound_data.o(.rodata); - } =0 + } > ROM =0 song_data : ALIGN(4) @@ -962,7 +964,7 @@ SECTIONS { sound/songs/midi/mus_trainer_tower.o(.rodata); sound/songs/midi/mus_slow_pallet.o(.rodata); sound/songs/midi/mus_teachy_tv_menu.o(.rodata); - } + } > ROM =0 lib_rodata : SUBALIGN(4) @@ -1015,7 +1017,7 @@ SECTIONS { *libc.a:readr.o(.rodata); . = ALIGN(4); - } =0 + } > ROM =0 multiboot_data : ALIGN(4) @@ -1023,14 +1025,13 @@ SECTIONS { data/multiboot_ereader.o(.rodata); data/multiboot_berry_glitch_fix.o(.rodata); data/multiboot_pokemon_colosseum.o(.rodata); - } =0 + } > ROM =0 - . = 0x08D00000; - gfx_data : + gfx_data 0x08D00000 : ALIGN(4) { src/graphics.o(.rodata); - } =0 + } > ROM =0 extra : ALIGN(4) @@ -1038,7 +1039,8 @@ SECTIONS { src/*.o(.text); src/*.o(.rodata); data/*.o(.rodata); - } = 0 + } > ROM =0 + __rom_end = .; diff --git a/ld_script_modern.ld b/ld_script_modern.ld index fba2f80e9..fb11dafbb 100644 --- a/ld_script_modern.ld +++ b/ld_script_modern.ld @@ -1,24 +1,26 @@ gNumMusicPlayers = 4; gMaxLines = 0; +MEMORY +{ + EWRAM (rwx) : ORIGIN = 0x2000000, LENGTH = 256K + IWRAM (rwx) : ORIGIN = 0x3000000, LENGTH = 32K + ROM (rx) : ORIGIN = 0x8000000, LENGTH = 32M +} + SECTIONS { - . = 0x2000000; - ewram (NOLOAD) : + .ewram.sbss 0x2000000 (NOLOAD) : ALIGN(4) { gHeap = .; . = 0x1C000; - *(ewram_data); - - . = 0x40000; - } - - . = 0x3000000; + *(.sbss); + } > EWRAM - iwram (NOLOAD) : + iwram 0x3000000 (NOLOAD) : ALIGN(4) { /* .bss starts at 0x3000000 */ @@ -30,8 +32,7 @@ SECTIONS { *(COMMON); end = .; __end__ = .; - . = 0x8000; - } + } > IWRAM . = 0x8000000; @@ -43,25 +44,25 @@ SECTIONS { src/crt0.o(.text); src/main.o(.text); *(.text*); - } =0 + } > ROM =0 script_data : ALIGN(4) { *(script_data); - } =0 + } > ROM =0 .data : ALIGN(4) { *(.data*); - } =0 + } > ROM =0 .rodata : ALIGN(4) { *(.rodata*); - } =0 + } > ROM =0 __rom_end = .;