From 94fba81a90e82c8bbdafb9d9f41b9eb4e08fbee9 Mon Sep 17 00:00:00 2001 From: Philipp AUER Date: Thu, 2 Jan 2025 15:11:32 +0100 Subject: [PATCH] Initialize m4a and IRQ handler in a loaded section (#5912) Co-authored-by: sbird --- include/main.h | 1 - ld_script_test.ld | 22 +++++++++++++--------- src/crt0.s | 10 ++++++---- src/m4a.c | 4 ---- src/m4a_1.s | 10 ++++++---- src/main.c | 5 +---- 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/include/main.h b/include/main.h index eba04cbaa9..b64c9349a8 100644 --- a/include/main.h +++ b/include/main.h @@ -53,7 +53,6 @@ extern u16 gKeyRepeatContinueDelay; extern bool8 gSoftResetDisabled; extern IntrFunc gIntrTable[]; extern u8 gLinkVSyncDisabled; -extern u32 IntrMain_Buffer[]; extern s8 gPcmDmaCounter; void AgbMain(void); diff --git a/ld_script_test.ld b/ld_script_test.ld index 8972a97b00..d279d6b4f6 100644 --- a/ld_script_test.ld +++ b/ld_script_test.ld @@ -38,7 +38,7 @@ SECTIONS { __iwram_end = .; } > IWRAM - .iwram.sbss (NOLOAD) : + .iwram.bss (NOLOAD) : ALIGN(4) { src/*.o(.bss); @@ -55,13 +55,17 @@ SECTIONS { data/*.o(COMMON); test/*.o(COMMON); *libc.a:sbrkr.o(COMMON); - . = ALIGN(4); + } > IWRAM - /* .persistent starts at 0x3007F00 */ - /* WARNING: This is the end of the IRQ stack, if there's too - * much data it WILL be overwritten. */ - . = 0x7F00; - test/*.o(.persistent); + /* .persistent starts at 0x3007F00 */ + /* WARNING: This is the end of the IRQ stack, if there's too + * much data it WILL be overwritten. */ + + . = 0x03007F00; + .iwram.persistent (NOLOAD) : + ALIGN(4) + { + test/*.o(.persistent); } > IWRAM /* BEGIN ROM DATA */ @@ -79,7 +83,7 @@ SECTIONS { script_data : ALIGN(4) { - data/*.o(script_data); + data/*.o(script_data); } > ROM =0 lib_text : @@ -114,7 +118,7 @@ SECTIONS { } > ROM =0 .data.iwram : - ALIGN(4) + ALIGN(8) { __iwram_lma = .; . = . + (__iwram_end - __iwram_start); diff --git a/src/crt0.s b/src/crt0.s index af6ea0bc95..bd0eb9426c 100644 --- a/src/crt0.s +++ b/src/crt0.s @@ -14,15 +14,15 @@ Init:: mov r0, #PSR_SYS_MODE msr cpsr_cf, r0 ldr sp, sp_sys -@ Prepare for interrupt handling - ldr r1, =INTR_VECTOR - adr r0, IntrMain - str r0, [r1] @ Dispatch memory reset request to hardware mov r0, #255 @ RESET_ALL svc #1 << 16 @ Fill RAM areas with appropriate data bl InitializeWorkingMemory +@ Prepare for interrupt handling + ldr r1, =INTR_VECTOR + ldr r0, =IntrMain + str r0, [r1] @ Jump to AgbMain ldr r1, =AgbMain + 1 mov lr, pc @@ -37,6 +37,7 @@ sp_irq: .word IWRAM_END - 0x60 .pool .arm + .section .iwram.code .align 2, 0 IntrMain:: mov r3, #REG_BASE @@ -129,6 +130,7 @@ IntrMain_RetAddr: .pool + .text .align 2, 0 @ Don't pad with nop. @ Fills initialized IWRAM and EWRAM sections in RAM from LMA areas in ROM diff --git a/src/m4a.c b/src/m4a.c index e90e9b06f0..493577cc23 100644 --- a/src/m4a.c +++ b/src/m4a.c @@ -6,8 +6,6 @@ extern const u8 gCgb3Vol[]; #define BSS_CODE __attribute__((section(".bss.code"))) -BSS_CODE ALIGNED(4) char SoundMainRAM_Buffer[0x800] = {0}; - COMMON_DATA struct SoundInfo gSoundInfo = {0}; COMMON_DATA struct PokemonCrySong gPokemonCrySongs[MAX_POKEMON_CRIES] = {0}; COMMON_DATA struct MusicPlayerInfo gPokemonCryMusicPlayers[MAX_POKEMON_CRIES] = {0}; @@ -72,8 +70,6 @@ void m4aSoundInit(void) { s32 i; - CpuCopy32((void *)((s32)SoundMainRAM & ~1), SoundMainRAM_Buffer, sizeof(SoundMainRAM_Buffer)); - SoundInit(&gSoundInfo); MPlayExtender(gCgbChans); m4aSoundMode(SOUND_MODE_DA_BIT_8 diff --git a/src/m4a_1.s b/src/m4a_1.s index 20f9197a8d..4c0c8f7f89 100644 --- a/src/m4a_1.s +++ b/src/m4a_1.s @@ -52,11 +52,11 @@ SoundMain_3: cmp r3, 0 beq SoundMain_4 ldr r0, [r0, o_SoundInfo_musicPlayerHead] - bl _081DD25E + bl call_r3 ldr r0, [sp, 0x18] SoundMain_4: ldr r3, [r0, o_SoundInfo_CgbSound] - bl _081DD25E + bl call_r3 ldr r0, [sp, 0x18] ldr r3, [r0, o_SoundInfo_pcmSamplesPerVBlank] mov r8, r3 @@ -73,18 +73,19 @@ SoundMain_4: SoundMain_5: str r5, [sp, 0x8] ldr r6, lt_PCM_DMA_BUF_SIZE - ldr r3, lt_SoundMainRAM_Buffer + ldr r3, lt_SoundMainRAM bx r3 .align 2, 0 lt_SOUND_INFO_PTR: .word SOUND_INFO_PTR lt_ID_NUMBER: .word ID_NUMBER -lt_SoundMainRAM_Buffer: .word SoundMainRAM_Buffer + 1 +lt_SoundMainRAM: .word SoundMainRAM + 1 lt_REG_VCOUNT: .word REG_VCOUNT lt_o_SoundInfo_pcmBuffer: .word o_SoundInfo_pcmBuffer lt_PCM_DMA_BUF_SIZE: .word PCM_DMA_BUF_SIZE thumb_func_end SoundMain + .section .iwram.code thumb_func_start SoundMainRAM SoundMainRAM: ldrb r3, [r0, o_SoundInfo_reverb] @@ -708,6 +709,7 @@ _081DD594: .pool arm_func_end SoundMainRAM_Unk2 + .text thumb_func_start SoundMainBTM SoundMainBTM: mov r12, r4 diff --git a/src/main.c b/src/main.c index 29f02c20e4..39b4f7e7a7 100644 --- a/src/main.c +++ b/src/main.c @@ -69,7 +69,6 @@ COMMON_DATA u16 gKeyRepeatContinueDelay = 0; COMMON_DATA bool8 gSoftResetDisabled = 0; COMMON_DATA IntrFunc gIntrTable[INTR_COUNT] = {0}; COMMON_DATA u8 gLinkVSyncDisabled = 0; -COMMON_DATA u32 IntrMain_Buffer[0x200] = {0}; COMMON_DATA s8 gPcmDmaCounter = 0; COMMON_DATA void *gAgbMainLoop_sp = NULL; @@ -316,9 +315,7 @@ void InitIntrHandlers(void) for (i = 0; i < INTR_COUNT; i++) gIntrTable[i] = gIntrTableTemplate[i]; - DmaCopy32(3, IntrMain, IntrMain_Buffer, sizeof(IntrMain_Buffer)); - - INTR_VECTOR = IntrMain_Buffer; + INTR_VECTOR = IntrMain; SetVBlankCallback(NULL); SetHBlankCallback(NULL);