From d9a10948d3f7d41fd40f6dffec4829571c296379 Mon Sep 17 00:00:00 2001 From: engineer124 <47598039+engineer124@users.noreply.github.com> Date: Tue, 9 Aug 2022 20:49:20 -0400 Subject: [PATCH] aisetnextbuf.c OK (code_80194790.c) (#982) * aisetnextbuf OK * format * Function headers * Add comment --- include/functions.h | 4 +- include/variables.h | 7 --- spec | 3 +- src/code/audio/aisetnextbuf.c | 107 +++++++++++++++++++++++++++++++++ src/code/audio/code_80194790.c | 9 --- tools/disasm/files.txt | 4 +- tools/disasm/functions.txt | 2 +- tools/sizes/code_functions.csv | 2 +- 8 files changed, 113 insertions(+), 25 deletions(-) create mode 100644 src/code/audio/aisetnextbuf.c delete mode 100644 src/code/audio/code_80194790.c diff --git a/include/functions.h b/include/functions.h index 38d7454fd7..f6064d291f 100644 --- a/include/functions.h +++ b/include/functions.h @@ -3149,9 +3149,7 @@ u32 Audio_NextRandom(void); void Audio_InitMesgQueues(void); void Audio_InvalDCache(void* buf, size_t size); void Audio_WritebackDCache(void* buf, size_t size); -// void func_80194790(void); -// void func_80194840(void); -// void func_801948B0(void); +s32 osAiSetNextBuffer(void* buf, u32 size); void AudioPlayback_NoteDisable(Note* note); void AudioPlayback_ProcessNotes(void); SoundFontSound* AudioPlayback_InstrumentGetSound(Instrument* instrument, s32 semitone); diff --git a/include/variables.h b/include/variables.h index f908fd43a6..a2731ff127 100644 --- a/include/variables.h +++ b/include/variables.h @@ -1582,13 +1582,6 @@ extern s32 gAudioContextInitalized; // extern UNK_TYPE1 D_801D5FF8; // extern UNK_TYPE4 D_801D5FFC; extern UNK_PTR D_801D6000; -// extern UNK_TYPE1 D_801D6010; -// extern UNK_TYPE1 D_801D6014; -// extern UNK_TYPE1 D_801D6028; -// extern UNK_TYPE1 D_801D6098; -// extern UNK_TYPE1 D_801D6100; -extern UNK_PTR D_801D6188; -extern UNK_PTR D_801D618C; // extern UNK_TYPE4 D_801D6190; // extern UNK_TYPE4 D_801D6194; // extern UNK_TYPE1 D_801D6200; diff --git a/spec b/spec index 41c41d6084..ecf0be8dca 100644 --- a/spec +++ b/spec @@ -614,8 +614,7 @@ beginseg include "build/src/code/audio/code_80192BE0.o" include "build/data/code/code_80192BE0.data.o" include "build/src/code/audio/audio_dcache.o" - include "build/src/code/audio/code_80194790.o" - include "build/data/code/code_80194790.data.o" + include "build/src/code/audio/aisetnextbuf.o" include "build/src/code/audio/audio_playback.o" include "build/src/code/audio/audio_effects.o" include "build/src/code/audio/audio_seqplayer.o" diff --git a/src/code/audio/aisetnextbuf.c b/src/code/audio/aisetnextbuf.c new file mode 100644 index 0000000000..b82f28e6f0 --- /dev/null +++ b/src/code/audio/aisetnextbuf.c @@ -0,0 +1,107 @@ +#include "global.h" + +//! Note that this is not the same as the original libultra +//! osAiSetNextBuffer, see comments in the function + +s32 osAiSetNextBuffer(void* buf, u32 size) { + static u8 D_801D6010 = false; + u32 bufAdjusted = (u32)buf; + s32 status; + + if (D_801D6010) { + bufAdjusted = (u32)buf - 0x2000; + } + if ((((u32)buf + size) & 0x1FFF) == 0) { + D_801D6010 = true; + } else { + D_801D6010 = false; + } + + // Originally a call to __osAiDeviceBusy + status = HW_REG(AI_STATUS_REG, s32); + if (status & AI_STATUS_AI_FULL) { + return -1; + } + + // OS_K0_TO_PHYSICAL replaces osVirtualToPhysical, this replacement + // assumes that only KSEG0 addresses are given + HW_REG(AI_DRAM_ADDR_REG, u32) = OS_K0_TO_PHYSICAL(bufAdjusted); + HW_REG(AI_LEN_REG, u32) = size; + return 0; +} + +//! Note that the remaining data and functions are unused and not part of the +//! original libultra osAiSetNextBuffer + +s16 D_801D6014[] = { + 19720, 18360, 17680, 16320, 14960, 13600, 12240, 10880, 9520, 0, +}; +s8 D_801D6028[7][16] = { + { 0, 1, 2, 2, 1, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3 }, { 0, 1, 2, 3, 3, 2, 1, 4, 5, 6, 7, 8, 7, 6, 5, 4 }, + { 0, 1, 2, 3, 4, 4, 3, 2, 1, 5, 6, 7, 8, 7, 6, 5 }, { 0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 6, 7, 8, 7, 6 }, + { 0, 1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1, 7, 8, 7 }, { 0, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 8 }, + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1 }, +}; +s16 D_801D6098[7][17] = { + { 0, 268, 560, 834, 1126, 1418, 1692, 1966, 2240, 2514, 2718, 2922, 3196, 3470, 3744, 4018, 4292 }, + { 0, 268, 560, 834, 1108, 1382, 1674, 1966, 2240, 2514, 2788, 2992, 3196, 3470, 3744, 4018, 4292 }, + { 0, 268, 560, 834, 1108, 1382, 1656, 1930, 2222, 2514, 2788, 3062, 3266, 3470, 3744, 4018, 4292 }, + { 0, 268, 560, 834, 1108, 1382, 1656, 1930, 2204, 2478, 2770, 3062, 3336, 3540, 3744, 4018, 4292 }, + { 0, 268, 560, 834, 1108, 1382, 1656, 1930, 2222, 2514, 2752, 3026, 3318, 3610, 3814, 4018, 4292 }, + { 0, 268, 560, 834, 1108, 1382, 1656, 1930, 2134, 2408, 2682, 2956, 3230, 3504, 3796, 4088, 4292 }, + { 0, 268, 560, 834, 1108, 1382, 1656, 1930, 2134, 2338, 2612, 2886, 3163, 3434, 3708, 4000, 4292 }, +}; +s16* D_801D6188 = D_801D6098[0]; +s8* D_801D618C = D_801D6028[0]; + +// Unused +void func_80194804(s32 index) { + D_801D6188 = D_801D6098[index]; + D_801D618C = D_801D6028[index]; +} + +// Only used below +s16 func_80194840(s32 arg0) { + s32 i; + s32 j = 0; + + for (i = 1; i < 16; i++) { + if (arg0 < D_801D6188[i]) { + break; + } + j++; + } + + if (i == 16) { + return 0; + } else { + j = D_801D618C[j]; + return D_801D6014[j]; + } +} + +// Unused +s32 func_801948B0(s32* arg0, s32* arg1) { + s32 temp_v0; + s32 phi_s0 = *arg0; + s32 phi_s1 = *arg1; + + while (true) { + temp_v0 = func_80194840(phi_s0); + if (temp_v0 == 0) { + return -1; + } + + if (phi_s1 < temp_v0) { + break; + } + + phi_s1 -= temp_v0; + phi_s0++; + } + + *arg0 = phi_s0; + *arg1 = phi_s1; + + return 0; +} diff --git a/src/code/audio/code_80194790.c b/src/code/audio/code_80194790.c deleted file mode 100644 index 9ba2436deb..0000000000 --- a/src/code/audio/code_80194790.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "global.h" - -#pragma GLOBAL_ASM("asm/non_matchings/code/code_80194790/func_80194790.s") - -#pragma GLOBAL_ASM("asm/non_matchings/code/code_80194790/func_80194804.s") - -#pragma GLOBAL_ASM("asm/non_matchings/code/code_80194790/func_80194840.s") - -#pragma GLOBAL_ASM("asm/non_matchings/code/code_80194790/func_801948B0.s") diff --git a/tools/disasm/files.txt b/tools/disasm/files.txt index 22762b51a9..ffa305cddd 100644 --- a/tools/disasm/files.txt +++ b/tools/disasm/files.txt @@ -484,7 +484,7 @@ 0x8018EB60 : "audio_load", 0x80192BE0 : "code_80192BE0", 0x80194710 : "audio_dcache", - 0x80194790 : "code_80194790", + 0x80194790 : "aisetnextbuf", 0x80194930 : "audio_playback", 0x80196A00 : "audio_effects", 0x801974D0 : "audio_seqplayer", @@ -587,7 +587,7 @@ 0x801D5FB0 : "audio_synthesis", 0x801D5FE0 : "audio_load", 0x801D5FF0 : "code_80192BE0", - 0x801D6010 : "code_80194790", + 0x801D6010 : "aisetnextbuf", 0x801D6190 : "audio_effects", 0x801D61A0 : "audio_seqplayer", 0x801D6200 : "audio_dramStack", diff --git a/tools/disasm/functions.txt b/tools/disasm/functions.txt index 3e7077fa48..722faab561 100644 --- a/tools/disasm/functions.txt +++ b/tools/disasm/functions.txt @@ -3722,7 +3722,7 @@ 0x801946E4:("Audio_InitMesgQueues",), 0x80194710:("Audio_InvalDCache",), 0x80194750:("Audio_WritebackDCache",), - 0x80194790:("func_80194790",), + 0x80194790:("osAiSetNextBuffer",), 0x80194804:("func_80194804",), 0x80194840:("func_80194840",), 0x801948B0:("func_801948B0",), diff --git a/tools/sizes/code_functions.csv b/tools/sizes/code_functions.csv index 1167eeb167..3ac8d0ff33 100644 --- a/tools/sizes/code_functions.csv +++ b/tools/sizes/code_functions.csv @@ -3238,7 +3238,7 @@ asm/non_matchings/code/code_80192BE0/Audio_NextRandom.s,Audio_NextRandom,0x80194 asm/non_matchings/code/code_80192BE0/Audio_InitMesgQueues.s,Audio_InitMesgQueues,0x801946E4,0xB asm/non_matchings/code/code_80194710/Audio_InvalDCache.s,Audio_InvalDCache,0x80194710,0x10 asm/non_matchings/code/code_80194710/Audio_WritebackDCache.s,Audio_WritebackDCache,0x80194750,0x10 -asm/non_matchings/code/code_80194710/func_80194790.s,func_80194790,0x80194790,0x1D +asm/non_matchings/code/code_80194710/osAiSetNextBuffer.s,osAiSetNextBuffer,0x80194790,0x1D asm/non_matchings/code/code_80194710/func_80194804.s,func_80194804,0x80194804,0xF asm/non_matchings/code/code_80194710/func_80194840.s,func_80194840,0x80194840,0x1C asm/non_matchings/code/code_80194710/func_801948B0.s,func_801948B0,0x801948B0,0x20