From 59c4a95e444f0ee5f050bd9cc0ff52ed6f17f009 Mon Sep 17 00:00:00 2001 From: cecilarmitais Date: Mon, 17 Aug 2026 15:48:24 -0700 Subject: [PATCH] Decomp the volume and pan fade track events Decompile from asm: DseTrackEvent_VolumeFade 0x020723C0 DseTrackEvent_PanFade 0x02072668 Both were deferred near-matches. Each reads a signed byte as the fade target and a little-endian pair as its duration in ticks, writes the target, and then either snaps current to it when the duration is zero, zeroes the duration when there is nothing to travel, or divides the distance by the duration to get the per-tick delta. The two differ only in which struct dse_fade they address -- volume at 0x2c, pan at 0x3c -- both of which already exist in dse.h. What closed them was declaration order, and in the opposite direction to the obvious one. The target loads ptr_next_byte[2] first, then [0] and [1]; writing the target expression first, so that its load comes first in the source, leaves the loads in ascending address order at score 620. Declaring ticks first and the target second emits them in the target's order and scores 0. So for this scheduler the later-declared expression's loads are issued first, and the fix is to write the declarations in the reverse of the order the asm reads them. Seven other spellings of the same two statements were tried -- explicit locals for each byte in the target's load order, s8/s16/s32 intermediates, an inline cast, and a pointer-cast subscript -- and every one of them stayed at 620. Only the declaration swap moves it. The lib/DSE headers extract_function.py generates do not include dse.h, so both were given it; every other header in that directory already does. Three deferred DSE functions remain, and this commit does not close them. DseTrackEvent_TuningFade is the same family with a bend fade plus the SetTuning tail; hoisting container out of channel before the flag test takes it from 935 to 760, which is progress and not a match. DseTrackEvent_SetupKeyBendLfo is unchanged in substance: its instructions have matched for some time and only register assignment differs. All 120 orderings of its five byte locals were tried this time, along with eleven structural variants, and the best is 55 rather than the 65 it sat at; the earlier note suggested exhaustive permutation as the untried move, and it has now been tried and does not close it. No comments are added to any pmd-sky file. Authored by Claude (Opus 5) under human direction. Confirmed by a matching build: build/pmdsky.us/pmdsky.us.nds: OK. Co-Authored-By: Claude Opus 5 --- lib/DSE/asm/include/main_020723C0.inc | 2 -- lib/DSE/asm/include/main_02072668.inc | 2 -- lib/DSE/asm/main_020723C0.s | 32 --------------------------- lib/DSE/asm/main_02072668.s | 32 --------------------------- lib/DSE/include/main_020723C0.h | 8 +++++++ lib/DSE/include/main_02072668.h | 8 +++++++ lib/DSE/src/main_020723C0.c | 22 ++++++++++++++++++ lib/DSE/src/main_02072668.c | 22 ++++++++++++++++++ main.lsf | 4 ++-- 9 files changed, 62 insertions(+), 70 deletions(-) delete mode 100644 lib/DSE/asm/include/main_020723C0.inc delete mode 100644 lib/DSE/asm/include/main_02072668.inc delete mode 100644 lib/DSE/asm/main_020723C0.s delete mode 100644 lib/DSE/asm/main_02072668.s create mode 100644 lib/DSE/include/main_020723C0.h create mode 100644 lib/DSE/include/main_02072668.h create mode 100644 lib/DSE/src/main_020723C0.c create mode 100644 lib/DSE/src/main_02072668.c diff --git a/lib/DSE/asm/include/main_020723C0.inc b/lib/DSE/asm/include/main_020723C0.inc deleted file mode 100644 index e6797521..00000000 --- a/lib/DSE/asm/include/main_020723C0.inc +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once -.public _s32_div_f diff --git a/lib/DSE/asm/include/main_02072668.inc b/lib/DSE/asm/include/main_02072668.inc deleted file mode 100644 index e6797521..00000000 --- a/lib/DSE/asm/include/main_02072668.inc +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once -.public _s32_div_f diff --git a/lib/DSE/asm/main_020723C0.s b/lib/DSE/asm/main_020723C0.s deleted file mode 100644 index 3ce64b19..00000000 --- a/lib/DSE/asm/main_020723C0.s +++ /dev/null @@ -1,32 +0,0 @@ - .include "asm/macros.inc" - .include "include/main_020723C0.inc" - - .text - - arm_func_start DseTrackEvent_VolumeFade -DseTrackEvent_VolumeFade: ; 0x020723C0 - stmdb sp!, {r4, r5, r6, lr} - mov r5, r0 - ldrsb r1, [r5, #2] - ldrb r2, [r5] - ldrb r0, [r5, #1] - mov r4, r3 - mov r1, r1, lsl #0x10 - add r0, r2, r0, lsl #8 - mov r0, r0, lsl #0x10 - str r1, [r4, #0x34] - movs r6, r0, lsr #0x10 - streq r1, [r4, #0x2c] - beq _02072410 - ldr r0, [r4, #0x2c] - subs r0, r1, r0 - moveq r6, #0 - beq _02072410 - mov r1, r6 - bl _s32_div_f - str r0, [r4, #0x30] -_02072410: - strh r6, [r4, #0x38] - add r0, r5, #3 - ldmia sp!, {r4, r5, r6, pc} - arm_func_end DseTrackEvent_VolumeFade diff --git a/lib/DSE/asm/main_02072668.s b/lib/DSE/asm/main_02072668.s deleted file mode 100644 index 11dbf561..00000000 --- a/lib/DSE/asm/main_02072668.s +++ /dev/null @@ -1,32 +0,0 @@ - .include "asm/macros.inc" - .include "include/main_02072668.inc" - - .text - - arm_func_start DseTrackEvent_PanFade -DseTrackEvent_PanFade: ; 0x02072668 - stmdb sp!, {r4, r5, r6, lr} - mov r5, r0 - ldrsb r1, [r5, #2] - ldrb r2, [r5] - ldrb r0, [r5, #1] - mov r4, r3 - mov r1, r1, lsl #0x10 - add r0, r2, r0, lsl #8 - mov r0, r0, lsl #0x10 - str r1, [r4, #0x44] - movs r6, r0, lsr #0x10 - streq r1, [r4, #0x3c] - beq _020726B8 - ldr r0, [r4, #0x3c] - subs r0, r1, r0 - moveq r6, #0 - beq _020726B8 - mov r1, r6 - bl _s32_div_f - str r0, [r4, #0x40] -_020726B8: - strh r6, [r4, #0x48] - add r0, r5, #3 - ldmia sp!, {r4, r5, r6, pc} - arm_func_end DseTrackEvent_PanFade diff --git a/lib/DSE/include/main_020723C0.h b/lib/DSE/include/main_020723C0.h new file mode 100644 index 00000000..4d76871a --- /dev/null +++ b/lib/DSE/include/main_020723C0.h @@ -0,0 +1,8 @@ +#ifndef PMDSKY_MAIN_020723C0_H +#define PMDSKY_MAIN_020723C0_H + +#include "dse.h" + +u8* DseTrackEvent_VolumeFade(u8 *ptr_next_byte, struct dse_sequence *sequence, struct dse_track *track, struct dse_channel *channel); + +#endif diff --git a/lib/DSE/include/main_02072668.h b/lib/DSE/include/main_02072668.h new file mode 100644 index 00000000..291c4839 --- /dev/null +++ b/lib/DSE/include/main_02072668.h @@ -0,0 +1,8 @@ +#ifndef PMDSKY_MAIN_02072668_H +#define PMDSKY_MAIN_02072668_H + +#include "dse.h" + +u8* DseTrackEvent_PanFade(u8 *ptr_next_byte, struct dse_sequence *sequence, struct dse_track *track, struct dse_channel *channel); + +#endif diff --git a/lib/DSE/src/main_020723C0.c b/lib/DSE/src/main_020723C0.c new file mode 100644 index 00000000..a4478bb6 --- /dev/null +++ b/lib/DSE/src/main_020723C0.c @@ -0,0 +1,22 @@ +#include "main_020723C0.h" + +u8* DseTrackEvent_VolumeFade(u8 *ptr_next_byte, struct dse_sequence *sequence, struct dse_track *track, struct dse_channel *channel) +{ + u16 ticks = ptr_next_byte[0] + (ptr_next_byte[1] << 8); + s32 target = *(s8 *)&ptr_next_byte[2] << 16; + + channel->volume.target = target; + if (ticks == 0) { + channel->volume.current = target; + } else { + s32 diff = target - channel->volume.current; + + if (diff == 0) { + ticks = 0; + } else { + channel->volume.delta = diff / ticks; + } + } + channel->volume.ticks_remaining = ticks; + return ptr_next_byte + 3; +} diff --git a/lib/DSE/src/main_02072668.c b/lib/DSE/src/main_02072668.c new file mode 100644 index 00000000..9f7f2cf1 --- /dev/null +++ b/lib/DSE/src/main_02072668.c @@ -0,0 +1,22 @@ +#include "main_02072668.h" + +u8* DseTrackEvent_PanFade(u8 *ptr_next_byte, struct dse_sequence *sequence, struct dse_track *track, struct dse_channel *channel) +{ + u16 ticks = ptr_next_byte[0] + (ptr_next_byte[1] << 8); + s32 target = *(s8 *)&ptr_next_byte[2] << 16; + + channel->pan.target = target; + if (ticks == 0) { + channel->pan.current = target; + } else { + s32 diff = target - channel->pan.current; + + if (diff == 0) { + ticks = 0; + } else { + channel->pan.delta = diff / ticks; + } + } + channel->pan.ticks_remaining = ticks; + return ptr_next_byte + 3; +} diff --git a/main.lsf b/main.lsf index df23a2ef..01ea5a6c 100644 --- a/main.lsf +++ b/main.lsf @@ -387,13 +387,13 @@ Static main Object lib/DSE/src/main_0207222C.o Object lib/DSE/src/main_0207227C.o Object lib/DSE/src/main_02072310.o - Object lib/DSE/asm/main_020723C0.o + Object lib/DSE/src/main_020723C0.o Object lib/DSE/src/main_0207241C.o Object lib/DSE/src/main_020724A8.o Object lib/DSE/src/main_02072504.o Object lib/DSE/src/main_02072554.o Object lib/DSE/src/main_020725D4.o - Object lib/DSE/asm/main_02072668.o + Object lib/DSE/src/main_02072668.o Object lib/DSE/src/main_020726C4.o Object lib/DSE/src/main_02072720.o Object lib/DSE/src/main_02072770.o