client/tests/music.c (6604B)
1 #include "linux_music.h" 2 #include "linux_thread.h" 3 #include "guest_heap.h" 4 #include <assert.h> 5 #include <stdatomic.h> 6 #include <time.h> 7 8 void sub_3A004700(void); 9 void sub_3A004A10(void); 10 void sub_3A004AD0(void); 11 void sub_3A004C00(void); 12 static _Atomic unsigned references = 1, attached, entered, release_load; 13 static _Atomic int muted; 14 static _Atomic unsigned generation; 15 static unsigned signals, plays, stops, destroys, loads, volumes; 16 static uint32_t events[8], forces[8], last_volume; 17 18 /* The real mmr controller runs on a real guest-capable worker. Only pr and 19 the driver are substitutes, so the test can hold project loading open. */ 20 void linux_audio_retain_driver(uint32_t driver) { assert(driver == 11); ++references; } 21 void linux_audio_release_driver(uint32_t driver) { assert(driver == 11); assert(references); --references; } 22 uint32_t linux_audio_mute_music(uint32_t driver, int value) 23 { 24 assert(driver == 11); 25 if (value) { muted = 1; ++generation; } 26 return generation; 27 } 28 void linux_audio_enable_music(uint32_t driver, uint32_t value) 29 { 30 assert(driver == 11); 31 if (generation == value) muted = 0; 32 } 33 uint32_t guest_crt_errno_address(void) { return g_fs_base + 0x40; } 34 void recomp_eh_thread_exit(void) {} 35 recomp_func_t recomp_lookup_manual(uint32_t address) { (void)address; return NULL; } 36 recomp_func_t recomp_lookup_import(uint32_t address) { (void)address; abort(); } 37 void linux_thread_notify(uint32_t reason) 38 { 39 assert(reason == 2); 40 assert(MEM32(g_fs_base + 0x18) == g_fs_base); 41 assert(g_esp > MEM32(g_fs_base + 8) && g_esp < MEM32(g_fs_base + 4)); 42 ++attached; 43 } 44 45 static void wait_for(_Atomic unsigned *value, unsigned expected) 46 { 47 for (unsigned i = 0; atomic_load(value) != expected && i < 5000; ++i) { 48 struct timespec delay = {0, 1000000}; 49 nanosleep(&delay, NULL); 50 } 51 assert(atomic_load(value) == expected); 52 } 53 54 static void ensure_project(void) 55 { 56 assert(linux_music_worker && g_ecx != 0x50001000); 57 assert(MEM32(MEM32(g_ecx + 0x60) + 0xc) == 11); 58 if (!MEM32(0x3A00D2DC)) { 59 if (!loads) { 60 entered = 1; 61 wait_for(&release_load, 1); 62 } 63 ++loads; 64 MEM32(0x3A00D2DC) = guest_malloc(32); 65 MEM32(MEM32(0x3A00D2DC) + 0x18) = 0x50002000; 66 for (unsigned i = 0; i < 17; ++i) MEM32(g_ecx + 0xc4 + 4 * i) = 100 + i; 67 } 68 g_esp += 4; 69 } 70 71 static void find_layer(void) 72 { 73 assert(linux_music_worker); 74 uint32_t name = MEM32(g_esp + 4); 75 assert(name == 0x3A00D1B8 || name == 0x3A00D21C); 76 g_eax = name == 0x3A00D1B8 ? 17 : 23; 77 g_esp += 8; 78 } 79 80 static void signal_event(void) 81 { 82 assert(linux_music_worker && signals < 8); 83 events[signals] = MEM32(g_esp + 4); 84 forces[signals++] = MEM32(g_esp + 8); 85 g_esp += 12; 86 } 87 static void play(void) { assert(linux_music_worker); ++plays; g_esp += 12; } 88 static void stop(void) { assert(linux_music_worker); ++stops; g_esp += 12; } 89 static void destroy(void) { assert(linux_music_worker); ++destroys; g_esp += 4; } 90 static void volume(void) 91 { 92 assert(linux_music_worker); 93 ++volumes; 94 last_volume = MEM32(g_esp + 4); 95 g_esp += 8; 96 } 97 static void link_player(void) { MEM32(g_ecx + 0x1c) = MEM32(g_esp + 4); g_esp += 8; } 98 static void delete_player(void) { guest_free(MEM32(g_esp + 4)); g_esp += 4; } 99 100 recomp_func_t recomp_lookup(uint32_t address) 101 { 102 switch (address) { 103 case 0x3A004700: return sub_3A004700; 104 case 0x3A004770: return ensure_project; 105 case 0x3A004A10: return sub_3A004A10; 106 case 0x3A004AD0: return sub_3A004AD0; 107 case 0x3A004C00: return sub_3A004C00; 108 case 0x3A008D40: return link_player; 109 case 0x3A008F76: return delete_player; 110 case 1: return find_layer; 111 case 2: return signal_event; 112 case 3: return play; 113 case 4: return volume; 114 case 5: return stop; 115 case 6: return destroy; 116 } 117 fprintf(stderr, "Unexpected music test call %08x\n", address); 118 abort(); 119 } 120 121 int main(void) 122 { 123 assert(guest_map("music globals", 0x3A000000, 0x10000)); 124 assert(guest_map("music main thread", 0x50000000, 0x20000)); 125 g_fs_base = 0x50010000; 126 g_esp = 0x5000ff00; 127 MEM32(0x3A00A150) = 1; MEM32(0x3A00A154) = 2; MEM32(0x3A00A158) = 3; 128 MEM32(0x3A00A160) = 4; MEM32(0x3A00A164) = 5; MEM32(0x3A00A168) = 6; 129 uint32_t manager = 0x50001000, audio = 0x50001200; 130 MEM32(manager + 0x60) = audio; 131 MEM8(manager + 4) = MEM8(manager + 0x109) = MEM8(audio + 0x10) = 1; 132 MEM32(audio + 0xc) = 11; 133 MEM32(0x3A00D03C) = 0x3e800000; /* 0.25f */ 134 MEM8(0x3A00D040) = 1; 135 linux_music_volume(manager); 136 linux_music_start(manager); 137 wait_for(&entered, 1); 138 139 MEM32(manager + 0x94) = 5; 140 MEM8(manager + 0x109) = 0; 141 linux_music_zone(manager, 695, 42); 142 MEM32(manager + 0x94) = 0; 143 linux_music_zone(manager, 695, (uint32_t)-911); 144 linux_music_zone(manager, 695, (uint32_t)-911); 145 linux_music_event(manager, 65535); 146 linux_music_mute(manager, 1); 147 assert(muted); 148 linux_music_mute(manager, 0); 149 assert(muted); 150 linux_music_event(manager, 44); 151 MEM32(0x3A00D03C) = 0x3f400000; /* 0.75f */ 152 MEM8(0x3A00D040) = 1; 153 linux_music_volume(manager); 154 linux_music_shutdown(manager); 155 --references; /* The game closes its driver and frees its manager immediately. */ 156 memset((void *)ADDR(manager), 0xa5, 0x224); 157 158 assert(!release_load && muted && references > 0 && attached == 1); 159 release_load = 1; 160 wait_for(&references, 0); 161 assert(loads == 2 && signals == 5 && plays == 1 && stops == 2 && destroys == 2); 162 assert(events[0] == 17 && forces[0] == 1); 163 assert(events[1] == 105 && forces[1] == 0); 164 assert(events[2] == 23 && forces[2] == 0); 165 assert(events[3] == 65535 && forces[3] == 1); 166 assert(events[4] == 44 && forces[4] == 0); 167 assert(volumes && last_volume == 0x3f400000 && !MEM32(0x3A00D2DC)); 168 169 references = 1; 170 memset((void *)ADDR(manager), 0, 0x224); 171 MEM32(manager + 0x60) = audio; 172 MEM8(manager + 4) = MEM8(manager + 0x109) = MEM8(audio + 0x10) = 1; 173 MEM32(audio + 0xc) = 11; 174 175 linux_music_zone(manager, 695, (uint32_t)-911); 176 linux_music_shutdown(manager); 177 --references; 178 wait_for(&references, 0); 179 180 assert(loads == 3 && signals == 6 && plays == 2 && stops == 3 && destroys == 3); 181 assert(events[5] == 23 && forces[5] == 1 && last_volume == 0x3f400000); 182 puts("Music: blocked project load does not block zone, volume, mute or shutdown; snapshots and driver lifetime passed"); 183 puts("Music: startup, combat override, neutral zone, duplicate event, forced transition and mute/unmute preserved"); 184 return 0; 185 }