From 26620e850e4e76acd69713b70dd2fb6289b99c73 Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Fri, 29 Oct 2021 15:44:43 +0100 Subject: [PATCH] Have the constants only in one place --- agb/crt0.s | 2 +- agb/src/sound/mixer/mixer.s | 25 ++++++++++++++++--------- agb/src/sound/mixer/sw_mixer.rs | 12 ++++++++++++ 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/agb/crt0.s b/agb/crt0.s index b2c327d0..9a20af8a 100644 --- a/agb/crt0.s +++ b/agb/crt0.s @@ -64,5 +64,5 @@ b .Initialise_mb b 1b .pool -.include "src/sound/mixer/mixer.s" .include "interrupt_handler.s" +.include "src/sound/mixer/mixer.s" diff --git a/agb/src/sound/mixer/mixer.s b/agb/src/sound/mixer/mixer.s index c6abf36e..4ea2f3f4 100644 --- a/agb/src/sound/mixer/mixer.s +++ b/agb/src/sound/mixer/mixer.s @@ -1,4 +1,8 @@ -.equ SOUND_BUFFER_SIZE, 176 +.section .iwram + .global agb_rs__buffer_size + .balign 4 +agb_rs__buffer_size: + .word agb_arm_func agb_rs__mixer_add @ Arguments @@ -19,8 +23,8 @@ agb_arm_func agb_rs__mixer_add modifications_fallback: orr r7, r7, r3, lsl #16 @ r7 now is the left channel followed by the right channel modifications. - mov r5, #0 @ current index we're reading from - mov r8, #SOUND_BUFFER_SIZE @ the number of steps left + mov r5, #0 @ current index we're reading from + ldr r8, agb_rs__buffer_size @ the number of steps left 1: @@ -62,8 +66,8 @@ same_modification: lsrs r7, r7, #1 bne 1b - mov r5, #0 @ current index we're reading from - mov r8, #SOUND_BUFFER_SIZE @ the number of steps left + mov r5, #0 @ current index we're reading from + ldr r8, agb_rs__buffer_size @ the number of steps left .macro mixer_add_loop_simple add r4, r0, r5, asr #8 @ calculate the address of the next read from the sound buffer @@ -132,7 +136,7 @@ agb_arm_func agb_rs__mixer_add_stereo str r4, [r1], #4 @ store the new value, and increment the pointer .endm - mov r8, #SOUND_BUFFER_SIZE + ldr r8, agb_rs__buffer_size 1: mixer_add_loop_simple_stereo mixer_add_loop_simple_stereo @@ -159,8 +163,10 @@ agb_arm_func agb_rs__mixer_collapse @ Arguments: @ r0 = target buffer (i8) @ r1 = input buffer (i16) of fixnums with 4 bits of precision (read in sets of i16 in an i32) + push {r4} - mov r2, #SOUND_BUFFER_SIZE @ loop counter + ldr r2, agb_rs__buffer_size @ loop counter + mov r4, r2 1: @ r12 = *r1; r1++ @@ -173,11 +179,12 @@ agb_arm_func agb_rs__mixer_collapse clamp_s8 r12 @ clamp the audio to 8 bit values clamp_s8 r3 - strb r3, [r0, #SOUND_BUFFER_SIZE] @ *(r0 + SOUND_BUFFER_SIZE) = r3 + strb r3, [r0, r4] @ *(r0 + r4 = SOUND_BUFFER_SIZE) = r3 strb r12, [r0], #1 @ *r0 = r12; r0++ subs r2, r2, #1 @ r2 -= 1 bne 1b @ loop if not 0 + pop {r4} bx lr -agb_arm_end agb_rs__mixer_collapse \ No newline at end of file +agb_arm_end agb_rs__mixer_collapse diff --git a/agb/src/sound/mixer/sw_mixer.rs b/agb/src/sound/mixer/sw_mixer.rs index 5b7e9406..aac9d0bc 100644 --- a/agb/src/sound/mixer/sw_mixer.rs +++ b/agb/src/sound/mixer/sw_mixer.rs @@ -94,6 +94,16 @@ impl Mixer { const SOUND_FREQUENCY: i32 = 10512; const SOUND_BUFFER_SIZE: usize = 176; +fn set_asm_buffer_size() { + extern "C" { + static mut agb_rs__buffer_size: usize; + } + + unsafe { + agb_rs__buffer_size = SOUND_BUFFER_SIZE; + } +} + #[repr(C, align(4))] struct SoundBuffer([i8; SOUND_BUFFER_SIZE * 2]); @@ -106,6 +116,8 @@ struct MixerBuffer { impl MixerBuffer { fn new() -> Self { + set_asm_buffer_size(); + MixerBuffer { buffer1: SoundBuffer([0; SOUND_BUFFER_SIZE * 2]), buffer2: SoundBuffer([0; SOUND_BUFFER_SIZE * 2]),