client/tests/shadow_contact.c (12773B)
1 #include "recomp_types.h" 2 #include <assert.h> 3 4 void sub_36117150(void); 5 6 enum { 7 controller = 0x50000000, owner = 0x50001000, 8 shadow = 0x50004000, playfield = 0x50005000, 9 surface = 0x50006000, position = 0x50007000, engine = 0x50008000, 10 visual = 0x50002000, mesh = 0x50003000, world_matrix = 0x5000c000 11 }; 12 static unsigned timing_calls, geometry_calls, update_calls, segment_calls; 13 static unsigned line_calls, sample_calls; 14 static unsigned attractor_calls; 15 static int line_hit; 16 static float contact_normal_y; 17 static float first_endpoint[3], updated_direction[3]; 18 19 /* Exercise the controller with stubbed engine and terrain boundaries. */ 20 static void timing(void) { ++timing_calls; g_esp += 4; } 21 static void find_owner(void) { g_eax = owner; g_esp += 4; } 22 static void get_playfield(void) { assert(g_ecx == owner); g_eax = playfield; g_esp += 4; } 23 static void get_surface(void) { assert(g_ecx == playfield); g_eax = surface; g_esp += 4; } 24 static void get_position(void) { assert(g_ecx == owner); g_eax = position; g_esp += 4; } 25 static void cast_owner(void) { g_eax = owner; g_esp += 4; } 26 static void get_mesh(void) { assert(g_ecx == owner); g_eax = visual; g_esp += 4; } 27 static void get_world_matrix(void) { assert(g_ecx == mesh + 0x3c); g_eax = world_matrix; g_esp += 4; } 28 29 static void get_attractor(void) 30 { 31 assert(g_ecx == visual && MEM32(g_esp + 4) == 0); 32 ++attractor_calls; 33 uint32_t matrix = MEM32(g_esp + 8); 34 if (!MEM8(mesh + 0x288) && MEM32(mesh + 0x284) != 0xffffffff) { 35 *(float *)ADDR(matrix + 0x30) = 100000; 36 *(float *)ADDR(matrix + 0x34) = 100000; 37 *(float *)ADDR(matrix + 0x38) = 100000; 38 } else { 39 *(float *)ADDR(matrix + 0x34) = 2; 40 } 41 g_esp += 12; 42 } 43 44 static void geometry(void) 45 { 46 assert(g_ecx == surface); 47 ++geometry_calls; 48 uint32_t point = MEM32(g_esp + 8), normal = MEM32(g_esp + 12); 49 memcpy((void *)ADDR(point), (void *)ADDR(position), 12); 50 *(float *)ADDR(point + 4) = 0; 51 *(float *)ADDR(normal + 4) = 1; 52 g_eax = 1; 53 g_esp += 24; 54 } 55 56 static void line(void) 57 { 58 assert(g_ecx == surface); 59 if (!line_calls) 60 memcpy(first_endpoint, (void *)ADDR(MEM32(g_esp + 8)), sizeof(first_endpoint)); 61 ++line_calls; 62 uint32_t point = MEM32(g_esp + 12), normal = MEM32(g_esp + 16); 63 memcpy((void *)ADDR(point), (void *)ADDR(MEM32(g_esp + 4)), 12); 64 *(float *)ADDR(point + 4) = 0; 65 *(float *)ADDR(normal + 4) = contact_normal_y; 66 *(float *)ADDR(point) = 60 + line_calls; 67 g_eax = line_hit; 68 g_esp += 28; 69 } 70 71 static void sun(void) { g_eax = 0x5000d000; g_esp += 4; } 72 static void dimension(void) { g_eax = 128; g_esp += 4; } 73 static void sample(void) { ++sample_calls; fp_push_impl(g_st, &g_fp_top, 0); g_esp += 12; } 74 static void integer(void) { g_eax = (int32_t)fp_pop_impl(g_st, &g_fp_top); g_esp += 4; } 75 static void clamp(void) 76 { 77 float value = *(float *)ADDR(g_esp + 4); 78 if (value < *(float *)ADDR(g_esp + 8)) value = *(float *)ADDR(g_esp + 8); 79 if (value > *(float *)ADDR(g_esp + 12)) value = *(float *)ADDR(g_esp + 12); 80 fp_push_impl(g_st, &g_fp_top, value); 81 g_esp += 4; 82 } 83 static void subtract(void) 84 { 85 uint32_t rhs = MEM32(g_esp + 4); 86 for (unsigned i = 0; i < 12; i += 4) 87 *(float *)ADDR(g_ecx + i) -= *(float *)ADDR(rhs + i); 88 g_eax = g_ecx; 89 g_esp += 8; 90 } 91 92 static void cross(void) 93 { 94 float left[3], right[3]; 95 memcpy(left, (void *)ADDR(g_ecx), sizeof(left)); 96 memcpy(right, (void *)ADDR(MEM32(g_esp + 4)), sizeof(right)); 97 float result[3] = {left[1]*right[2] - left[2]*right[1], 98 left[2]*right[0] - left[0]*right[2], left[0]*right[1] - left[1]*right[0]}; 99 memcpy((void *)ADDR(g_ecx), result, sizeof(result)); 100 g_eax = g_ecx; 101 g_esp += 8; 102 } 103 104 static void update(void) 105 { 106 assert(g_ecx == shadow && geometry_calls >= 1); 107 memcpy(updated_direction, (void *)ADDR(MEM32(g_esp + 4)), sizeof(updated_direction)); 108 ++update_calls; 109 g_eax = 1; 110 g_esp += 20; 111 } 112 113 static void zero_segments(void) 114 { 115 assert(g_ecx == shadow && MEM32(g_esp + 4) == 0); 116 ++segment_calls; 117 MEM32(shadow + 0x1fc) = 0; 118 g_esp += 8; 119 } 120 121 recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } 122 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } 123 recomp_func_t recomp_lookup(uint32_t address) 124 { 125 switch (address) { 126 case 0x3615fd86: return cast_owner; 127 case 0x70000034: return get_mesh; 128 case 0x70000038: return get_attractor; 129 case 0x7000003c: return get_world_matrix; 130 case 0x360fc300: return timing; 131 case 0x360d3b40: return sun; 132 case 0x360887d0: return clamp; 133 case 0x3615fd8c: return integer; 134 case 0x70000020: return line; 135 case 0x70000024: return dimension; 136 case 0x70000028: return sample; 137 case 0x7000002c: return subtract; 138 case 0x70000030: return cross; 139 case 0x70000000: return find_owner; 140 case 0x70000004: return get_playfield; 141 case 0x70000008: return get_surface; 142 case 0x7000000c: return get_position; 143 case 0x70000010: return geometry; 144 case 0x70000014: return update; 145 case 0x7000001c: return zero_segments; 146 default: fprintf(stderr, "Unexpected shadow dependency: %08x from %08x\n", address, g_cur_func); abort(); 147 } 148 } 149 150 static void arrange_fixture(void) 151 { 152 memset((void *)ADDR(0x50000000), 0, 0x10000); 153 timing_calls = geometry_calls = update_calls = segment_calls = 0; 154 line_calls = sample_calls = 0; line_hit = 0; contact_normal_y = 0; 155 attractor_calls = 0; 156 MEM32(owner + 0xc8) = visual; 157 MEM32(visual + 0x28) = mesh; 158 MEM32(0x3616ffe8) = 0x70000034; 159 MEM32(0x3616f834) = 0x70000038; 160 MEM32(0x3616f3a0) = 0x7000003c; 161 MEM32(0x3616ffb0) = 0x7000000c; 162 *(float *)ADDR(world_matrix) = 1; 163 *(float *)ADDR(world_matrix + 0x14) = 1; 164 *(float *)ADDR(world_matrix + 0x28) = 1; 165 *(float *)ADDR(world_matrix + 0x3c) = 1; 166 *(float *)ADDR(world_matrix + 0x34) = 1; 167 MEM8(0x3630d684) = 1; 168 MEM32(0x3616ff70) = 0x70000000; 169 MEM32(0x3616ffac) = 0x70000004; 170 MEM32(0x361700c0) = 0x70000008; 171 MEM32(0x361700a8) = 0x7000000c; 172 MEM32(0x3616f830) = 0x70000014; 173 MEM32(0x3616f82c) = 0x7000001c; 174 MEM32(0x3616ff74) = 0x50009000; 175 MEM32(0x50009000) = engine; 176 MEM32(engine + 0x80) = 0x5000a000; 177 *(float *)ADDR(0x36170d68) = .1f; 178 *(float *)ADDR(controller + 0x28) = 2; 179 MEM32(controller + 0x2c) = shadow; 180 MEM32(owner + 4) = 50000; 181 MEM32(shadow + 0x1fc) = 6; 182 MEM32(surface) = 0x5000b000; 183 MEM32(0x5000b004) = 0x70000010; 184 MEM32(0x5000b010) = 0x70000020; 185 MEM32(0x3616fda4) = MEM32(0x3616fda8) = 0x70000024; 186 MEM32(0x3616fe70) = 0x70000028; 187 MEM32(0x36170168) = 0x7000002c; 188 MEM32(0x3617017c) = 0x70000030; 189 MEM32(controller + 0x30) = surface; 190 *(float *)ADDR(controller + 0x34) = .25f; 191 *(float *)ADDR(position) = 64; 192 *(float *)ADDR(position + 4) = 1; 193 *(float *)ADDR(position + 8) = 64; 194 *(float *)ADDR(0x5000d004) = 1; 195 *(float *)ADDR(0x36170380) = 1; 196 *(float *)ADDR(0x36170464) = .5f; 197 *(float *)ADDR(0x36170854) = 20; 198 *(float *)ADDR(0x36170ef0) = .0001f; 199 *(float *)ADDR(0x36170eac) = .0001f; 200 *(float *)ADDR(0x36170f04) = .2f; 201 g_fs_base = 0x5000e000; 202 MEM32(g_fs_base) = 0x12345678; 203 } 204 205 static void run_controller(void) 206 { 207 g_ecx = controller; 208 g_esp = 0x5000ff00; 209 g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; 210 g_fp_top = 0; 211 memset(g_st, 0, sizeof(g_st)); 212 memset(g_st_i64, 0, sizeof(g_st_i64)); 213 sub_36117150(); 214 assert(g_esp == 0x5000ff04); 215 assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); 216 assert(g_fp_top == 0 && MEM32(g_fs_base) == 0x12345678); 217 assert(MEM32(controller + 0x10) == 0xbf800000); 218 } 219 220 static void failed_contact_with_low_normals_skips_samples(void) 221 { 222 arrange_fixture(); 223 MEM8(controller + 0x39) = 1; 224 225 run_controller(); 226 227 assert(line_calls == 3 && sample_calls == 0); 228 assert(segment_calls == 1 && update_calls == 0); 229 assert(MEM8(controller + 0x39) == 0); 230 } 231 232 static void failed_contact_with_upward_normal_keeps_samples(void) 233 { 234 arrange_fixture(); 235 contact_normal_y = 1; 236 MEM8(controller + 0x39) = 1; 237 238 run_controller(); 239 240 assert(line_calls == 3 && sample_calls == 8); 241 assert(segment_calls == 1 && update_calls == 0); 242 assert(MEM8(controller + 0x39) == 0); 243 } 244 245 static void successful_contact_restores_conformance(void) 246 { 247 arrange_fixture(); 248 MEM8(controller + 0x39) = 1; 249 run_controller(); 250 line_hit = 1; 251 contact_normal_y = 1; 252 253 run_controller(); 254 255 assert(sample_calls == 8 && update_calls == 1); 256 assert(MEM8(controller + 0x39) == 0); 257 } 258 259 static void normal_threshold_and_unordered_cases(void) 260 { 261 float normals[] = {.49999997f, .5f, NAN}; 262 unsigned expected_samples[] = {0, 8, 0}; 263 for (unsigned i = 0; i < 3; ++i) { 264 arrange_fixture(); 265 contact_normal_y = normals[i]; 266 MEM8(controller + 0x39) = 1; 267 268 run_controller(); 269 270 assert(sample_calls == expected_samples[i]); 271 assert(MEM8(controller + 0x39) == 0 && segment_calls == 1); 272 } 273 } 274 275 static void light_direction_tracks_inputs(void) 276 { 277 const float inputs[][3] = {{0,1,0}, {1,0,0}, {0,0,0}, {.01f,0,0}, 278 {.01f,0,0}, {.01f,0,0}, {NAN,1,0}}; 279 const float expected[][3] = {{0,-1,0}, {-1,0,0}, {0,-1,0}, {0,-1,0}, 280 {-1,0,0}, {-.5f,0,0}, {0,-1,0}}; 281 for (unsigned i = 0; i < 7; ++i) { 282 arrange_fixture(); 283 line_hit = 1; contact_normal_y = 1; 284 memcpy((void *)ADDR(0x5000d000), inputs[i], 12); 285 *(float *)ADDR(0x36170ef0) = i >= 4 ? .00001f : .001f; 286 *(float *)ADDR(0x36170380) = i == 5 ? .5f : 1; 287 288 run_controller(); 289 290 assert(update_calls == 1 && line_calls == 2); 291 for (unsigned axis = 0; axis < 3; ++axis) 292 assert(updated_direction[axis] == expected[i][axis]); 293 } 294 } 295 296 static void repeated_light_keeps_current_position(void) 297 { 298 arrange_fixture(); 299 line_hit = 1; contact_normal_y = 1; 300 run_controller(); 301 assert(first_endpoint[0] == 64 && first_endpoint[1] == -17); 302 *(float *)ADDR(position) = 96; 303 line_calls = 0; 304 305 run_controller(); 306 307 assert(first_endpoint[0] == 96 && first_endpoint[1] == -17); 308 assert(update_calls == 2 && geometry_calls == 2 && line_calls == 2); 309 assert(updated_direction[0] == 0 && updated_direction[1] == -1); 310 assert(updated_direction[2] == 0); 311 } 312 313 static void inactive_attractor_defers_shadow_height(void) 314 { 315 arrange_fixture(); 316 MEM32(controller + 0x28) = 0; 317 MEM8(controller + 0x39) = 1; 318 319 run_controller(); 320 321 assert(MEM32(controller + 0x28) == 0); 322 assert(attractor_calls == 0 && geometry_calls == 0 && line_calls == 0); 323 assert(timing_calls == 1 && segment_calls == 1 && update_calls == 0); 324 assert(MEM8(controller + 0x39) == 0); 325 } 326 327 static void activated_attractor_initializes_shadow_height(void) 328 { 329 arrange_fixture(); 330 MEM32(controller + 0x28) = 0; 331 run_controller(); 332 MEM8(mesh + 0x288) = 1; 333 line_hit = 1; contact_normal_y = 1; 334 335 run_controller(); 336 337 assert(*(float *)ADDR(controller + 0x28) == 2); 338 assert(attractor_calls == 1 && geometry_calls == 1 && update_calls == 1); 339 assert(first_endpoint[1] == -17); 340 } 341 342 static void unindexed_attractor_initializes_shadow_height(void) 343 { 344 arrange_fixture(); 345 MEM32(controller + 0x28) = 0; 346 MEM32(mesh + 0x284) = 0xffffffff; 347 348 run_controller(); 349 350 assert(*(float *)ADDR(controller + 0x28) == 2); 351 assert(attractor_calls == 1 && geometry_calls == 1); 352 } 353 354 static void throttled_skeleton_defers_first_height_until_update(void) 355 { 356 arrange_fixture(); 357 MEM32(controller + 0x28) = 0; 358 MEM8(mesh + 0x288) = 1; 359 MEM32(mesh + 0x280) = 1; 360 361 run_controller(); 362 363 assert(MEM32(controller + 0x28) == 0 && attractor_calls == 0); 364 assert(geometry_calls == 0 && line_calls == 0); 365 MEM32(mesh + 0x280) = 0; 366 run_controller(); 367 assert(*(float *)ADDR(controller + 0x28) == 2 && attractor_calls == 1); 368 } 369 370 int main(void) 371 { 372 assert(guest_map("shadow fixture", 0x50000000, 0x10000)); 373 assert(guest_map("shadow imports and constants", 0x36160000, 0x20000)); 374 assert(guest_map("shadow preference", 0x36300000, 0x10000)); 375 failed_contact_with_low_normals_skips_samples(); 376 failed_contact_with_upward_normal_keeps_samples(); 377 successful_contact_restores_conformance(); 378 normal_threshold_and_unordered_cases(); 379 light_direction_tracks_inputs(); 380 repeated_light_keeps_current_position(); 381 inactive_attractor_defers_shadow_height(); 382 activated_attractor_initializes_shadow_height(); 383 unindexed_attractor_initializes_shadow_height(); 384 throttled_skeleton_defers_first_height_until_update(); 385 puts("shadow-contact: failed contacts, retained sampling, recovery and guest ABI passed"); 386 }