From e88f46cb288c55376f57538481179577e9023380 Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Tue, 26 Jul 2022 22:11:36 -0700 Subject: [PATCH] Link snd_service.c and hw_memory.c --- Makefile | 2 + asm/musyx/hardware.s | 8 +-- include/musyx/dsp_import.h | 2 + include/musyx/musyx.h | 5 ++ include/musyx/musyx_priv.h | 11 +++- include/types.h | 4 ++ obj_files.mk | 4 +- src/musyx/dsp_import.c | 2 +- src/musyx/hw_memory.c | 11 ++++ src/musyx/snd3d.c | 2 +- src/musyx/snd_init.c | 1 - src/musyx/snd_service.c | 116 +++++++++++++++++++++++++++++++++++++ 12 files changed, 158 insertions(+), 10 deletions(-) create mode 100644 src/musyx/hw_memory.c create mode 100644 src/musyx/snd_service.c diff --git a/Makefile b/Makefile index 9203cbfe..9bdd7d7e 100644 --- a/Makefile +++ b/Makefile @@ -103,6 +103,7 @@ ifeq ($(VERBOSE),0) LDFLAGS := $(MAPGEN) -fp fmadd -nodefaults -w off endif CFLAGS_1.2 = -proc gekko -nodefaults -Cpp_exceptions off -RTTI off -fp fmadd -str reuse,pool,readonly -O4,p -maxerrors 1 -use_lmw_stmw on -enum int -inline auto -MMD $(INCLUDES) +CFLAGS_MUSYX = -proc gekko -nodefaults -Cpp_exceptions off -RTTI off -fp fmadd -str reuse,pool,readonly -O4,p -maxerrors 1 -enum int -inline auto -MMD $(INCLUDES) CFLAGS = $(CFLAGS_1.2) -gccinc ifeq ($(VERBOSE),0) @@ -118,6 +119,7 @@ $(BUILD_DIR)/src/Dolphin/os/OSAudioSystem.o: MWCC_VERSION := 1.2.5 $(BUILD_DIR)/src/Dolphin/os/OSAudioSystem.o: CFLAGS := $(CFLAGS_1.2) $(BUILD_DIR)/src/Dolphin/dsp/dsp.o: MWCC_VERSION := 1.2.5 $(BUILD_DIR)/src/Dolphin/dsp/dsp.o: CFLAGS := $(CFLAGS_1.2) +$(BUILD_DIR)/src/musyx/snd_service.o: CFLAGS := $(CFLAGS_MUSYX) #------------------------------------------------------------------------------- # Recipes diff --git a/asm/musyx/hardware.s b/asm/musyx/hardware.s index 293e4dba..fa12cd4b 100644 --- a/asm/musyx/hardware.s +++ b/asm/musyx/hardware.s @@ -61,8 +61,8 @@ lbl_805A8BE0: .balign 4 .section .rodata -.global lbl_803D8BD0 -lbl_803D8BD0: +.global itdOffTab +itdOffTab: # ROM: 0x3D5BD0 .4byte 0 .4byte 0 @@ -917,9 +917,9 @@ lbl_803B39F8: /* 803B39F8 003B0958 80 1F 00 F0 */ lwz r0, 0xf0(r31) /* 803B39FC 003B095C 54 00 00 01 */ rlwinm. r0, r0, 0, 0, 0 /* 803B3A00 003B0960 41 82 00 30 */ beq lbl_803B3A30 -/* 803B3A04 003B0964 3C 60 80 3E */ lis r3, lbl_803D8BD0@ha +/* 803B3A04 003B0964 3C 60 80 3E */ lis r3, itdOffTab@ha /* 803B3A08 003B0968 57 C0 8D FC */ rlwinm r0, r30, 0x11, 0x17, 0x1e -/* 803B3A0C 003B096C 38 83 8B D0 */ addi r4, r3, lbl_803D8BD0@l +/* 803B3A0C 003B096C 38 83 8B D0 */ addi r4, r3, itdOffTab@l /* 803B3A10 003B0970 7C 64 02 2E */ lhzx r3, r4, r0 /* 803B3A14 003B0974 7C 04 02 2E */ lhzx r0, r4, r0 /* 803B3A18 003B0978 B0 7F 00 D0 */ sth r3, 0xd0(r31) diff --git a/include/musyx/dsp_import.h b/include/musyx/dsp_import.h index 6f0741b1..f40828c4 100644 --- a/include/musyx/dsp_import.h +++ b/include/musyx/dsp_import.h @@ -1,6 +1,8 @@ #ifndef MUSYX_DSP_IMPORT_H #define MUSYX_DSP_IMPORT_H +#include "types.h" + #ifdef __cplusplus extern "C" { #endif diff --git a/include/musyx/musyx.h b/include/musyx/musyx.h index b757aa5e..23fe3574 100644 --- a/include/musyx/musyx.h +++ b/include/musyx/musyx.h @@ -15,6 +15,11 @@ typedef struct _SynthInfo { u8 studios; } SynthInfo; +typedef struct _SND_HOOKS { + void *(*malloc)(u32 len); + void (*free)(void *addr); +} SND_HOOKS; + #ifdef __cplusplus } #endif diff --git a/include/musyx/musyx_priv.h b/include/musyx/musyx_priv.h index 85966893..da90fa85 100644 --- a/include/musyx/musyx_priv.h +++ b/include/musyx/musyx_priv.h @@ -1,11 +1,12 @@ #ifndef MUSYX_PRIV_H #define MUSYX_PRIV_H -#include "types.h" +#include "musyx/musyx.h" #ifdef __cplusplus extern "C" { #endif +typedef s32 (*SND_COMPARE)(u16*, u8*); void dataInit(u32, s32); /* extern */ void dataInitStack(); /* extern */ @@ -19,7 +20,15 @@ void hwExit(); void dataExit(); void s3dExit(); void synthExit(); +u32 synthGetTicksPerSecond(u32 seconds); +u16 sndRand(void); +s16 sndSin(u32 __x); +u8* sndBSearch(u16 *key,u8 *subTab,s32 mainTab,s32 len,SND_COMPARE cmp); +void sndConvertMs(u32* time); +void sndConvertTicks(u32* out, u32 seconds); +u32 sndConvert2Ms(u32 time); +extern SND_HOOKS salHooks; #ifdef __cplusplus } #endif diff --git a/include/types.h b/include/types.h index f7d9b2bd..baf17c65 100644 --- a/include/types.h +++ b/include/types.h @@ -62,6 +62,10 @@ typedef void* unkptr; #endif #endif +#ifndef ATTRIBUTE_ALIGN +#define ATTRIBUTE_ALIGN(num) __attribute__ ((aligned (num))) +#endif + // where should these go? void srand(int); int rand(); diff --git a/obj_files.mk b/obj_files.mk index 01082994..4fb3d2bf 100644 --- a/obj_files.mk +++ b/obj_files.mk @@ -800,12 +800,12 @@ MUSYX_FILES :=\ $(BUILD_DIR)/src/musyx/snd_init.o\ $(BUILD_DIR)/asm/musyx/snd_math.o\ $(BUILD_DIR)/asm/musyx/snd_midictrl.o\ - $(BUILD_DIR)/asm/musyx/snd_service.o\ + $(BUILD_DIR)/src/musyx/snd_service.o\ $(BUILD_DIR)/asm/musyx/hardware.o\ $(BUILD_DIR)/asm/musyx/hw_aramdma.o\ $(BUILD_DIR)/src/musyx/dsp_import.o\ $(BUILD_DIR)/asm/musyx/hw_dolphin.o\ - $(BUILD_DIR)/asm/musyx/hw_memory.o\ + $(BUILD_DIR)/src/musyx/hw_memory.o\ $(BUILD_DIR)/asm/musyx/creverb_fx.o\ $(BUILD_DIR)/asm/musyx/creverb.o\ $(BUILD_DIR)/asm/musyx/reverb_fx.o\ diff --git a/src/musyx/dsp_import.c b/src/musyx/dsp_import.c index e833aeae..1f623723 100644 --- a/src/musyx/dsp_import.c +++ b/src/musyx/dsp_import.c @@ -4,7 +4,7 @@ extern "C" { #endif -char dspSlave[0x19E0] = { +char dspSlave[0x19E0] ATTRIBUTE_ALIGN(32) = { 0x00, 0x00, 0x00, 0x00, 0x02, 0x9F, 0x0C, 0x10, 0x02, 0x9F, 0x0C, 0x1F, 0x02, 0x9F, 0x0C, 0x3B, 0x02, 0x9F, 0x0C, 0x4A, 0x02, 0x9F, 0x0C, 0x50, 0x02, 0x9F, 0x0C, 0x82, 0x02, 0x9F, 0x0C, 0x88, 0x13, 0x02, 0x13, 0x03, diff --git a/src/musyx/hw_memory.c b/src/musyx/hw_memory.c new file mode 100644 index 00000000..9e9c57df --- /dev/null +++ b/src/musyx/hw_memory.c @@ -0,0 +1,11 @@ +#include "musyx/musyx_priv.h" + + +void* salMalloc(u32 len) { + return salHooks.malloc(len); +} + +void salFree(void* addr) { + salHooks.free(addr); +} + diff --git a/src/musyx/snd3d.c b/src/musyx/snd3d.c index f6e18675..0ec3a0b2 100644 --- a/src/musyx/snd3d.c +++ b/src/musyx/snd3d.c @@ -1,4 +1,4 @@ -#include "musyx/musyx.h" +#include "musyx/musyx_priv.h" #ifdef __cplusplus extern "C" { diff --git a/src/musyx/snd_init.c b/src/musyx/snd_init.c index f7c2e71e..13a280f2 100644 --- a/src/musyx/snd_init.c +++ b/src/musyx/snd_init.c @@ -1,4 +1,3 @@ -#include "musyx/musyx.h" #include "musyx/musyx_priv.h" #ifdef __cplusplus diff --git a/src/musyx/snd_service.c b/src/musyx/snd_service.c new file mode 100644 index 00000000..ef774c16 --- /dev/null +++ b/src/musyx/snd_service.c @@ -0,0 +1,116 @@ +#include "musyx/musyx_priv.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +s16 sndSintab[1024] = { + 0, 6, 12, 18, 25, 31, 37, 43, 50, 56, 62, 69, 75, 81, 87, 94, 100, 106, 113, 119, 125, 131, + 138, 144, 150, 157, 163, 169, 175, 182, 188, 194, 200, 207, 213, 219, 226, 232, 238, 244, 251, 257, 263, 269, + 276, 282, 288, 295, 301, 307, 313, 320, 326, 332, 338, 345, 351, 357, 363, 370, 376, 382, 388, 395, 401, 407, + 413, 420, 426, 432, 438, 445, 451, 457, 463, 470, 476, 482, 488, 495, 501, 507, 513, 520, 526, 532, 538, 545, + 551, 557, 563, 569, 576, 582, 588, 594, 601, 607, 613, 619, 625, 632, 638, 644, 650, 656, 663, 669, 675, 681, + 687, 694, 700, 706, 712, 718, 725, 731, 737, 743, 749, 755, 762, 768, 774, 780, 786, 792, 799, 805, 811, 817, + 823, 829, 836, 842, 848, 854, 860, 866, 872, 879, 885, 891, 897, 903, 909, 915, 921, 928, 934, 940, 946, 952, + 958, 964, 970, 976, 983, 989, 995, 1001, 1007, 1013, 1019, 1025, 1031, 1037, 1043, 1050, 1056, 1062, 1068, 1074, 1080, 1086, + 1092, 1098, 1104, 1110, 1116, 1122, 1128, 1134, 1140, 1146, 1152, 1158, 1164, 1170, 1176, 1182, 1189, 1195, 1201, 1207, 1213, 1219, + 1225, 1231, 1237, 1243, 1248, 1254, 1260, 1266, 1272, 1278, 1284, 1290, 1296, 1302, 1308, 1314, 1320, 1326, 1332, 1338, 1344, 1350, + 1356, 1362, 1368, 1373, 1379, 1385, 1391, 1397, 1403, 1409, 1415, 1421, 1427, 1433, 1438, 1444, 1450, 1456, 1462, 1468, 1474, 1479, + 1485, 1491, 1497, 1503, 1509, 1515, 1520, 1526, 1532, 1538, 1544, 1550, 1555, 1561, 1567, 1573, 1579, 1584, 1590, 1596, 1602, 1608, + 1613, 1619, 1625, 1631, 1636, 1642, 1648, 1654, 1659, 1665, 1671, 1677, 1682, 1688, 1694, 1699, 1705, 1711, 1717, 1722, 1728, 1734, + 1739, 1745, 1751, 1756, 1762, 1768, 1773, 1779, 1785, 1790, 1796, 1802, 1807, 1813, 1819, 1824, 1830, 1835, 1841, 1847, 1852, 1858, + 1864, 1869, 1875, 1880, 1886, 1891, 1897, 1903, 1908, 1914, 1919, 1925, 1930, 1936, 1941, 1947, 1952, 1958, 1964, 1969, 1975, 1980, + 1986, 1991, 1997, 2002, 2007, 2013, 2018, 2024, 2029, 2035, 2040, 2046, 2051, 2057, 2062, 2067, 2073, 2078, 2084, 2089, 2094, 2100, + 2105, 2111, 2116, 2121, 2127, 2132, 2138, 2143, 2148, 2154, 2159, 2164, 2170, 2175, 2180, 2186, 2191, 2196, 2201, 2207, 2212, 2217, + 2223, 2228, 2233, 2238, 2244, 2249, 2254, 2259, 2265, 2270, 2275, 2280, 2286, 2291, 2296, 2301, 2306, 2312, 2317, 2322, 2327, 2332, + 2337, 2343, 2348, 2353, 2358, 2363, 2368, 2373, 2379, 2384, 2389, 2394, 2399, 2404, 2409, 2414, 2419, 2424, 2429, 2434, 2439, 2445, + 2450, 2455, 2460, 2465, 2470, 2475, 2480, 2485, 2490, 2495, 2500, 2505, 2510, 2515, 2519, 2524, 2529, 2534, 2539, 2544, 2549, 2554, + 2559, 2564, 2569, 2574, 2578, 2583, 2588, 2593, 2598, 2603, 2608, 2613, 2617, 2622, 2627, 2632, 2637, 2641, 2646, 2651, 2656, 2661, + 2665, 2670, 2675, 2680, 2684, 2689, 2694, 2699, 2703, 2708, 2713, 2717, 2722, 2727, 2732, 2736, 2741, 2746, 2750, 2755, 2760, 2764, + 2769, 2773, 2778, 2783, 2787, 2792, 2796, 2801, 2806, 2810, 2815, 2819, 2824, 2828, 2833, 2837, 2842, 2847, 2851, 2856, 2860, 2865, + 2869, 2874, 2878, 2882, 2887, 2891, 2896, 2900, 2905, 2909, 2914, 2918, 2922, 2927, 2931, 2936, 2940, 2944, 2949, 2953, 2957, 2962, + 2966, 2970, 2975, 2979, 2983, 2988, 2992, 2996, 3000, 3005, 3009, 3013, 3018, 3022, 3026, 3030, 3034, 3039, 3043, 3047, 3051, 3055, + 3060, 3064, 3068, 3072, 3076, 3080, 3085, 3089, 3093, 3097, 3101, 3105, 3109, 3113, 3117, 3121, 3126, 3130, 3134, 3138, 3142, 3146, + 3150, 3154, 3158, 3162, 3166, 3170, 3174, 3178, 3182, 3186, 3190, 3193, 3197, 3201, 3205, 3209, 3213, 3217, 3221, 3225, 3229, 3232, + 3236, 3240, 3244, 3248, 3252, 3255, 3259, 3263, 3267, 3271, 3274, 3278, 3282, 3286, 3289, 3293, 3297, 3301, 3304, 3308, 3312, 3315, + 3319, 3323, 3326, 3330, 3334, 3337, 3341, 3345, 3348, 3352, 3356, 3359, 3363, 3366, 3370, 3373, 3377, 3381, 3384, 3388, 3391, 3395, + 3398, 3402, 3405, 3409, 3412, 3416, 3419, 3423, 3426, 3429, 3433, 3436, 3440, 3443, 3447, 3450, 3453, 3457, 3460, 3463, 3467, 3470, + 3473, 3477, 3480, 3483, 3487, 3490, 3493, 3497, 3500, 3503, 3506, 3510, 3513, 3516, 3519, 3522, 3526, 3529, 3532, 3535, 3538, 3541, + 3545, 3548, 3551, 3554, 3557, 3560, 3563, 3566, 3570, 3573, 3576, 3579, 3582, 3585, 3588, 3591, 3594, 3597, 3600, 3603, 3606, 3609, + 3612, 3615, 3618, 3621, 3624, 3627, 3629, 3632, 3635, 3638, 3641, 3644, 3647, 3650, 3652, 3655, 3658, 3661, 3664, 3667, 3669, 3672, + 3675, 3678, 3680, 3683, 3686, 3689, 3691, 3694, 3697, 3700, 3702, 3705, 3708, 3710, 3713, 3716, 3718, 3721, 3723, 3726, 3729, 3731, + 3734, 3736, 3739, 3742, 3744, 3747, 3749, 3752, 3754, 3757, 3759, 3762, 3764, 3767, 3769, 3772, 3774, 3776, 3779, 3781, 3784, 3786, + 3789, 3791, 3793, 3796, 3798, 3800, 3803, 3805, 3807, 3810, 3812, 3814, 3816, 3819, 3821, 3823, 3826, 3828, 3830, 3832, 3834, 3837, + 3839, 3841, 3843, 3845, 3848, 3850, 3852, 3854, 3856, 3858, 3860, 3862, 3864, 3867, 3869, 3871, 3873, 3875, 3877, 3879, 3881, 3883, + 3885, 3887, 3889, 3891, 3893, 3895, 3897, 3899, 3900, 3902, 3904, 3906, 3908, 3910, 3912, 3914, 3915, 3917, 3919, 3921, 3923, 3925, + 3926, 3928, 3930, 3932, 3933, 3935, 3937, 3939, 3940, 3942, 3944, 3945, 3947, 3949, 3950, 3952, 3954, 3955, 3957, 3959, 3960, 3962, + 3963, 3965, 3967, 3968, 3970, 3971, 3973, 3974, 3976, 3977, 3979, 3980, 3982, 3983, 3985, 3986, 3988, 3989, 3990, 3992, 3993, 3995, + 3996, 3997, 3999, 4000, 4001, 4003, 4004, 4005, 4007, 4008, 4009, 4011, 4012, 4013, 4014, 4016, 4017, 4018, 4019, 4020, 4022, 4023, + 4024, 4025, 4026, 4027, 4029, 4030, 4031, 4032, 4033, 4034, 4035, 4036, 4037, 4038, 4039, 4040, 4041, 4042, 4043, 4044, 4045, 4046, + 4047, 4048, 4049, 4050, 4051, 4052, 4053, 4054, 4055, 4056, 4057, 4057, 4058, 4059, 4060, 4061, 4062, 4062, 4063, 4064, 4065, 4065, + 4066, 4067, 4068, 4068, 4069, 4070, 4071, 4071, 4072, 4073, 4073, 4074, 4075, 4075, 4076, 4076, 4077, 4078, 4078, 4079, 4079, 4080, + 4080, 4081, 4081, 4082, 4082, 4083, 4083, 4084, 4084, 4085, 4085, 4086, 4086, 4087, 4087, 4087, 4088, 4088, 4089, 4089, 4089, 4090, + 4090, 4090, 4091, 4091, 4091, 4091, 4092, 4092, 4092, 4092, 4093, 4093, 4093, 4093, 4094, 4094, 4094, 4094, 4094, 4094, 4095, 4095, + 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, +}; + +#define SINTAB_ELEMENT_COUNT (sizeof(sndSintab) / sizeof(short) - 1) + +u32 last_rnd = 1; + +u16 sndRand(void) { + last_rnd *= 2822053219; + return last_rnd >> 6; +} + +s16 sndSin(u32 __x) { + const u16 x = __x & 0xFFF; + if (x < 1024) { + return (int)sndSintab[x]; + } + if (x < 2048) { + return (int)sndSintab[SINTAB_ELEMENT_COUNT - (x & SINTAB_ELEMENT_COUNT)]; + } + if (x < 3072) { + return (int)-sndSintab[x & SINTAB_ELEMENT_COUNT]; + } + return -sndSintab[SINTAB_ELEMENT_COUNT - (x & SINTAB_ELEMENT_COUNT)]; +} + +u8* sndBSearch(u16 *key,u8 *subTab,s32 mainTab,s32 len,SND_COMPARE cmp) { + s32 left; + s32 right; + s32 mid; + s32 found; + u8* test; + + if (mainTab != 0) { + left = 1; + right = mainTab; + do { + mid = ((left + right) >> 1); + test = subTab + (len * (mid - 1)); + found = (*cmp)(key, test); + if (found == 0) { return test; } + + if (found < 0) { + right = mid - 1; + } else + { + left = mid + 1; + } + } while (left <= right); + } + return NULL; +} + + +void sndConvertMs(u32* time) { *time = *time * 256; } + +void sndConvertTicks(u32* out, u32 seconds) { *out = (((*out << 16) / synthGetTicksPerSecond(seconds)) * 1000) / 32; } + +u32 sndConvert2Ms(u32 time) { return time / 256; } + +#ifdef __cplusplus +} +#endif // __cplusplus