client/tests/critical_result.c (18392B)
1 #include "recomp_types.h" 2 #include <assert.h> 3 4 void sub_360AECB0(void); 5 void sub_3607A320(void); 6 void sub_3607A2E0(void); 7 void sub_3607A660(void); 8 void sub_360AB4C0(void); 9 void sub_360AB500(void); 10 void sub_360AB560(void); 11 void sub_360AB580(void); 12 void sub_360AB5A0(void); 13 void sub_36075DB0(void); 14 void sub_3607BE50(void); 15 void sub_360B0A50(void); 16 void sub_3607FAA0(void); 17 18 static unsigned reads, writes, feedbacks, generic_feedbacks, deaths, ammo_updates; 19 static unsigned missing_attacker, missing_target, target_local; 20 static uint32_t expected_result, death_reason; 21 static int32_t ammunition; 22 static unsigned info_available; 23 24 static void info_apply(void) 25 { 26 assert(g_ecx == 0x50009500 && MEM32(g_esp + 4) == 0x50002000); 27 if (!MEM8(0x50002128)) { 28 MEM32(0x50002300) = MEM32(g_ecx + 0x14); 29 writes++; 30 } 31 g_esp += 8; 32 } 33 34 static void character_control(void) 35 { 36 assert(g_ecx == 0x50002000); 37 g_eax = 0x50009600; 38 g_esp += 4; 39 } 40 41 static void info_publish(void) 42 { 43 assert(g_ecx == 0x50009600 && MEM32(g_esp + 4) == 0x50009500); 44 info_available++; 45 g_esp += 8; 46 } 47 48 static void actor_lookup(void) 49 { 50 uint32_t identity = MEM32(g_esp + 4); 51 assert(MEM32(identity) == 50000); 52 switch (MEM32(identity + 4)) { 53 case 1: g_eax = missing_attacker ? 0 : 0x50001000; break; 54 case 2: g_eax = missing_target ? 0 : 0x50002000; break; 55 default: abort(); 56 } 57 g_esp += 4; 58 } 59 60 static void current_opponent(void) 61 { 62 assert(g_ecx == 0x50003000); 63 g_eax = 0x50002000; 64 g_esp += 4; 65 } 66 67 static void is_local(void) 68 { 69 assert(g_ecx == 0x50001000 || g_ecx == 0x50002000); 70 g_eax = g_ecx == 0x50002000 && target_local; 71 g_esp += 4; 72 } 73 74 static void actor_cast(void) 75 { 76 assert(MEM32(g_esp + 4) == 0x50002000); 77 g_eax = MEM32(g_esp + 4); 78 g_esp += 4; 79 } 80 81 static void actor_getter(void) 82 { 83 if (MEM32(g_esp + 4) == 339) { 84 assert(g_ecx == 0x500010e4 && MEM32(g_esp + 8) == 0); 85 g_eax = 0; 86 } else { 87 assert(g_ecx == 0x500020e4); 88 assert(MEM32(g_esp + 4) == 27 && MEM32(g_esp + 8) == 2); 89 reads++; 90 g_eax = MEM32(0x50002300); 91 } 92 g_esp += 12; 93 } 94 95 static void actor_setter(void) 96 { 97 assert(g_ecx == 0x500020e4); 98 assert(MEM32(g_esp + 4) == 27); 99 assert(MEM32(g_esp + 8) <= MEM32(0x50002300) || MEM32(g_esp + 8) == 205); 100 writes++; 101 MEM32(0x50002300) = MEM32(g_esp + 8); 102 g_esp += 12; 103 } 104 105 static void weapon_setter(void) 106 { 107 assert(g_ecx == 0x50006000 && MEM32(g_esp + 4) == 26); 108 assert((int32_t)MEM32(g_esp + 8) == ammunition); 109 ammo_updates++; 110 MEM32(0x50006020) = MEM32(g_esp + 8); 111 g_esp += 12; 112 } 113 114 static void damage_type(void) 115 { 116 assert(g_ecx == 0x50005000); 117 g_eax = 90; 118 g_esp += 4; 119 } 120 121 static void target_state(void) 122 { 123 assert(g_ecx == 0x50001000); 124 g_eax = 0; 125 g_esp += 4; 126 } 127 128 static void feedback_interface(void) 129 { 130 g_eax = 0x50000900; 131 g_esp += 4; 132 } 133 134 static void feedback(void) 135 { 136 assert(g_ecx == 0x50000900); 137 assert(MEM32(g_esp + 4) == (target_local ? 30 : 32)); 138 assert(MEM32(g_esp + 8) == 0x50002000); 139 assert(MEM32(g_esp + 12) == 13 || MEM32(g_esp + 12) == 7 || MEM32(g_esp + 12) == 0); 140 assert(MEM32(g_esp + 16) == 0x50001000); 141 assert(MEM32(g_esp + 20) == 0 && MEM32(g_esp + 24) == 90); 142 assert(MEM32(g_esp + 28) == expected_result); 143 feedbacks++; 144 g_esp += 32; 145 } 146 147 static void generic_format(void) 148 { 149 assert(MEM32(g_esp + 8) == 0x361b4e14 && MEM32(g_esp + 12) == 13); 150 strcpy((char *)ADDR(MEM32(g_esp + 4)), "fixture damage text"); 151 g_esp += 4; 152 } 153 154 static void generic_feedback(void) 155 { 156 assert(g_ecx == 0x50000900 && MEM32(g_esp + 8) == 23); 157 assert(strcmp((char *)ADDR(MEM32(g_esp + 4)), "fixture damage text") == 0); 158 generic_feedbacks++; 159 g_esp += 12; 160 } 161 162 static void target_death(void) 163 { 164 assert(g_ecx == 0x50002000 && MEM32(g_esp + 4) == death_reason); 165 assert(MEM32(0x50002300) == 87 && writes == 1); 166 deaths++; 167 g_esp += 8; 168 } 169 170 recomp_func_t recomp_lookup(uint32_t address) 171 { 172 switch (address) { 173 case 0x3606da90: return actor_lookup; 174 case 0x3607a320: return sub_3607A320; 175 case 0x3607a2e0: return sub_3607A2E0; 176 case 0x3607a660: return sub_3607A660; 177 case 0x360ab5a0: return sub_360AB5A0; 178 case 0x360ab4c0: return sub_360AB4C0; 179 case 0x360ab500: return sub_360AB500; 180 case 0x360ab560: return sub_360AB560; 181 case 0x360ab580: return sub_360AB580; 182 case 0x360ab7b0: return damage_type; 183 case 0x36075db0: return sub_36075DB0; 184 case 0x3607be50: return sub_3607BE50; 185 case 0x3605b1a0: return info_apply; 186 case 0x36072000: return character_control; 187 case 0x360564a0: return info_publish; 188 case 0x36072310: return target_state; 189 case 0x3601c590: return feedback_interface; 190 case 0x3601d0b0: return feedback; 191 case 0x3601c8c0: return generic_feedback; 192 case 0x36072340: return target_death; 193 case 0x3615fd86: return actor_cast; 194 case 0x50000400: return actor_getter; 195 case 0x50000404: return actor_setter; 196 case 0x50000408: return weapon_setter; 197 case 0x5000040c: return current_opponent; 198 case 0x50000410: return is_local; 199 case 0x50000414: return generic_format; 200 default: fprintf(stderr, "unexpected critical-result call: %08x\n", address); abort(); 201 } 202 } 203 recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } 204 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } 205 206 static void setup(void) 207 { 208 reads = writes = feedbacks = generic_feedbacks = deaths = ammo_updates = 0; 209 memset((void *)ADDR(0x50000000), 0, 0x10000); 210 MEM32(0x50000004) = MEM32(0x5000002c) = 50000; 211 MEM32(0x50000008) = 1; 212 // Known-attacker delivery ignores this different payload target identity. 213 MEM32(0x50000030) = missing_attacker ? 2 : 3; 214 MEM32(0x50000020) = 6; 215 MEM32(0x50000024) = 13; 216 MEM32(0x50000028) = ammunition; 217 MEM32(0x50000034) = death_reason; 218 MEM32(0x50000038) = expected_result; 219 MEM32(0x50001300) = MEM32(0x50002300) = MEM32(0x50004300) = 100; 220 MEM32(0x5000116c) = 0x50003000; 221 MEM32(0x5000216c) = 0x50003800; 222 MEM32(0x50003000) = 0x50000200; 223 MEM32(0x50003004) = 0x50001000; 224 MEM32(0x50003024) = 0x50005000; 225 MEM32(0x50003804) = 0x50002000; 226 MEM32(0x50000250) = 0x5000040c; 227 MEM32(0x500020e4) = MEM32(0x500010e4) = 0x50000100; 228 MEM32(0x50000104) = 0x50000400; 229 MEM32(0x50000108) = 0x50000404; 230 MEM32(0x50002154) = 0x50007000; 231 MEM32(0x50007014) = 0x50002000; 232 MEM32(0x50005010) = 0x50008000; 233 MEM32(0x50008004) = 0x50008100; 234 MEM32(0x50005020) = 0x50006000; 235 MEM32(0x50006000) = 0x50000300; 236 MEM32(0x50000308) = 0x50000408; 237 MEM32(0x50006020) = 5; 238 MEM32(0x361700d4) = 0x50000410; 239 MEM32(0x3616f25c) = 0x50000414; 240 g_esp = 0x5000ff00; 241 g_ecx = 0x50000000; 242 } 243 244 static void queued_results(void) 245 { 246 const struct { uint32_t result, death, index, local; int32_t ammo; } cases[] = { 247 {4, 0, 0, 1, 3}, {5, 0, 0, 1, 3}, 248 {4, 7, 49, 0, 0}, {5, 7, 49, 0, -1} 249 }; 250 for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { 251 expected_result = cases[i].result; 252 death_reason = cases[i].death; 253 ammunition = cases[i].ammo; 254 target_local = cases[i].local; 255 missing_attacker = missing_target = 0; 256 setup(); 257 MEM32(0x50005014) = MEM32(0x50005018) = cases[i].index; 258 259 sub_360AECB0(); 260 261 assert(g_eax == 0 && g_esp == 0x5000ff04); 262 assert(MEM32(0x50002300) == 100 && reads == 0 && writes == 0); 263 assert(feedbacks == 0 && deaths == 0 && ammo_updates == 1); 264 assert((int32_t)MEM32(0x50006020) == ammunition); 265 assert(MEM32(0x50008100 + cases[i].index * 8) == 13); 266 assert(MEM32(0x50008104 + cases[i].index * 8) == expected_result); 267 assert(MEM32(0x50005014) == (cases[i].index == 49 ? 0 : 1)); 268 assert(MEM32(0x50003874) == death_reason); 269 270 g_esp = 0x5000ff00; 271 g_ecx = 0x50005000; 272 MEM32(g_esp + 4) = 0x50002000; 273 MEM32(g_esp + 8) = 0x50001000; 274 sub_360AB5A0(); 275 276 assert(g_esp == 0x5000ff0c); 277 assert(MEM32(0x50002300) == 87 && reads == 1 && writes == 1); 278 assert(MEM32(0x50001300) == 100 && MEM32(0x50004300) == 100); 279 assert(feedbacks == 1 && generic_feedbacks == 0 && ammo_updates == 1); 280 assert(deaths == (death_reason != 0)); 281 assert(MEM32(0x50005014) == MEM32(0x50005018)); 282 283 g_esp = 0x5000ff00; 284 g_ecx = 0x50005000; 285 sub_360AB5A0(); 286 287 assert(g_esp == 0x5000ff0c); 288 assert(MEM32(0x50002300) == 87 && reads == 1 && writes == 1); 289 assert(feedbacks == 1 && deaths == (death_reason != 0)); 290 } 291 } 292 293 static void missing_attacker_results(void) 294 { 295 const struct { uint32_t result, death, local, missing; } cases[] = { 296 {4, 0, 1, 0}, {5, 0, 1, 0}, {5, 7, 1, 0}, 297 {5, 0, 0, 0}, {5, 0, 1, 1} 298 }; 299 for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { 300 expected_result = cases[i].result; 301 death_reason = cases[i].death; 302 target_local = cases[i].local; 303 missing_attacker = 1; 304 missing_target = cases[i].missing; 305 ammunition = 3; 306 setup(); 307 308 sub_360AECB0(); 309 310 assert(g_eax == 0 && g_esp == 0x5000ff04); 311 unsigned applied = target_local && !missing_target; 312 assert(MEM32(0x50002300) == (applied ? 87 : 100)); 313 assert(reads == applied && writes == applied && generic_feedbacks == applied); 314 assert(feedbacks == 0 && deaths == (applied && death_reason != 0)); 315 assert(ammo_updates == 0 && MEM32(0x50006020) == 5); 316 assert(MEM32(0x50005014) == 0 && MEM32(0x50005018) == 0); 317 } 318 } 319 320 static void backlog_results(void) 321 { 322 for (unsigned index = 0; index <= 49; index += 49) { 323 expected_result = 4; 324 death_reason = 0; 325 ammunition = 3; 326 target_local = 1; 327 missing_attacker = missing_target = 0; 328 setup(); 329 MEM32(0x50003050) = 2; 330 MEM32(0x50003058) = 50000; 331 MEM32(0x5000305c) = 2; 332 MEM32(0x50005014) = MEM32(0x50005018) = index; 333 sub_360AECB0(); 334 assert(MEM32(0x50002300) == 100 && writes == 0); 335 336 g_esp = 0x5000ff00; 337 g_ecx = 0x50000000; 338 sub_360AECB0(); 339 340 assert(g_esp == 0x5000ff04); 341 assert(MEM32(0x50002300) == 74 && reads == 2 && writes == 2); 342 assert(feedbacks == 2 && ammo_updates == 2 && deaths == 0); 343 assert(MEM32(0x50005014) == MEM32(0x50005018)); 344 assert(MEM32(0x50003050) == 2 && MEM32(0x5000305c) == 2); 345 } 346 } 347 348 static void zero_result_backlog(void) 349 { 350 const struct { unsigned pending, zeros, health, read, write; } cases[] = { 351 {0, 1, 100, 0, 1}, {0, 2, 100, 2, 2}, 352 {1, 1, 87, 2, 2}, {1, 2, 87, 2, 3} 353 }; 354 for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { 355 expected_result = 4; 356 death_reason = 0; 357 ammunition = 3; 358 target_local = 1; 359 missing_attacker = missing_target = 0; 360 setup(); 361 MEM32(0x50003050) = 2; 362 MEM32(0x50003058) = 50000; 363 MEM32(0x5000305c) = 2; 364 if (cases[i].pending) sub_360AECB0(); 365 MEM32(0x50000024) = 0; 366 367 for (unsigned n = 0; n < cases[i].zeros; n++) { 368 g_esp = 0x5000ff00; 369 g_ecx = 0x50000000; 370 sub_360AECB0(); 371 } 372 373 assert(g_esp == 0x5000ff04); 374 assert(MEM32(0x50002300) == cases[i].health); 375 assert(MEM32(0x50005014) == cases[i].write); 376 assert(MEM32(0x50005018) == cases[i].read); 377 assert(reads == cases[i].read && writes == cases[i].read && feedbacks == cases[i].read); 378 assert(ammo_updates == cases[i].write && MEM32(0x50006020) == 3); 379 assert(MEM32(0x50003050) == 2 && MEM32(0x5000305c) == 2 && deaths == 0); 380 if (cases[i].read != cases[i].write) assert(MEM32(0x50008100 + cases[i].read * 8) == 0); 381 } 382 } 383 384 static void absolute_health_keeps_real_weapon_results(void) 385 { 386 const struct { unsigned index, late, snapshots, target, health; } cases[] = { 387 {0, 0, 1, 2, 205}, {49, 0, 2, 2, 205}, 388 {0, 1, 1, 2, 198}, {0, 0, 1, 3, 198}, {0, 0, 0, 2, 193}, 389 {49, 2, 1, 2, 198} 390 }; 391 for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { 392 expected_result = 4; 393 death_reason = 0; 394 ammunition = 3; 395 target_local = 1; 396 missing_attacker = missing_target = 0; 397 setup(); 398 MEM32(0x3806c110) = 0x50009200; 399 MEM32(0x50009204) = MEM32(0x50009208) = 0x50009220; 400 MEM32(0x50009224) = 0x50009240; 401 MEM32(0x50009240) = MEM32(0x50009248) = 0x50009220; 402 MEM32(0x5000924c) = 50000; 403 MEM32(0x50009250) = 1; 404 MEM32(0x50009254) = 0x50001000; 405 MEM32(0x50002300) = 200; 406 MEM32(0x50000024) = 7; 407 MEM32(0x50003050) = 2; 408 MEM32(0x50003058) = 50000; 409 MEM32(0x5000305c) = cases[i].target; 410 MEM32(0x50005014) = MEM32(0x50005018) = cases[i].index; 411 if (cases[i].late != 1) sub_360AECB0(); 412 413 // Deliver the same absolute HealthDamage update used by resource recovery. 414 MEM32(0x3616ff70) = 0x3606da90; 415 MEM32(0x50009004) = 50000; 416 MEM32(0x50009008) = 2; 417 MEM32(0x50009020) = 205; 418 MEM32(0x50009024) = 12; 419 uint8_t weapon[0x40], results[400]; 420 memcpy(weapon, (void *)ADDR(0x50005000), sizeof(weapon)); 421 memcpy(results, (void *)ADDR(0x50008100), sizeof(results)); 422 423 for (unsigned snapshot = 0; snapshot < cases[i].snapshots; ++snapshot) { 424 g_esp = 0x5000ff00; 425 g_ecx = 0x50009000; 426 sub_360B0A50(); 427 assert(MEM32(0x50002300) == 205); 428 assert(memcmp(weapon, (void *)ADDR(0x50005000), sizeof(weapon)) == 0); 429 assert(memcmp(results, (void *)ADDR(0x50008100), sizeof(results)) == 0); 430 } 431 if (cases[i].late) { 432 g_esp = 0x5000ff00; 433 g_ecx = 0x50000000; 434 sub_360AECB0(); 435 } 436 g_esp = 0x5000ff00; 437 MEM32(g_esp + 4) = 0; 438 MEM32(g_esp + 8) = 6; 439 g_ecx = 0x50003000; 440 sub_3607A2E0(); 441 442 assert(g_esp == 0x5000ff0c); 443 assert(MEM32(0x50002300) == cases[i].health); 444 unsigned hits = cases[i].late == 2 ? 2 : 1; 445 assert(reads == hits && writes == cases[i].snapshots + hits && feedbacks == hits && ammo_updates == hits); 446 assert(MEM32(0x50005014) == (cases[i].index + hits) % 50); 447 assert(MEM32(0x50005018) == MEM32(0x50005014)); 448 assert(MEM32(0x50003050) == 2 && MEM32(0x5000305c) == cases[i].target && deaths == 0); 449 450 // A subsequent real hit must still debit HP normally after the snapshot. 451 g_esp = 0x5000ff00; 452 g_ecx = 0x50000000; 453 sub_360AECB0(); 454 g_esp = 0x5000ff00; 455 MEM32(g_esp + 4) = 0; 456 MEM32(g_esp + 8) = 6; 457 g_ecx = 0x50003000; 458 sub_3607A2E0(); 459 assert(MEM32(0x50002300) == cases[i].health - 7); 460 assert(reads == hits + 1 && writes == cases[i].snapshots + hits + 1 && feedbacks == hits + 1 && ammo_updates == hits + 1); 461 } 462 } 463 464 static void inspection_keeps_real_weapon_results(void) 465 { 466 const struct { unsigned index, local, snapshots; } cases[] = { 467 {0, 0, 1}, {49, 0, 2}, {0, 1, 1} 468 }; 469 for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { 470 expected_result = 4; 471 death_reason = 0; 472 ammunition = 3; 473 target_local = cases[i].local; 474 missing_attacker = missing_target = info_available = 0; 475 setup(); 476 MEM32(0x3806c110) = 0x50009200; 477 MEM32(0x50009204) = MEM32(0x50009208) = 0x50009220; 478 MEM32(0x50009224) = 0x50009240; 479 MEM32(0x50009240) = MEM32(0x50009248) = 0x50009220; 480 MEM32(0x5000924c) = 50000; 481 MEM32(0x50009250) = 1; 482 MEM32(0x50009254) = 0x50001000; 483 MEM32(0x50002300) = 200; 484 MEM8(0x50002128) = cases[i].local; 485 MEM32(0x50000024) = 7; 486 MEM32(0x50003050) = 2; 487 MEM32(0x50003058) = 50000; 488 MEM32(0x5000305c) = 2; 489 MEM32(0x50005014) = MEM32(0x50005018) = cases[i].index; 490 sub_360AECB0(); 491 MEM32(0x3616ff70) = 0x3606da90; 492 MEM32(0x50009004) = 50000; 493 MEM32(0x50009008) = 2; 494 MEM32(0x50009020) = 0x50009500; 495 MEM32(0x50009514) = 193; 496 uint8_t weapon[0x40], results[400]; 497 memcpy(weapon, (void *)ADDR(0x50005000), sizeof(weapon)); 498 memcpy(results, (void *)ADDR(0x50008100), sizeof(results)); 499 500 for (unsigned snapshot = 0; snapshot < cases[i].snapshots; ++snapshot) { 501 g_esp = 0x5000ff00; 502 g_ecx = 0x50009000; 503 g_esi = 123; 504 g_edi = 456; 505 sub_3607FAA0(); 506 assert(g_esp == 0x5000ff04 && g_esi == 123 && g_edi == 456); 507 assert(MEM32(0x50002300) == (cases[i].local ? 200 : 193)); 508 assert(!memcmp(weapon, (void *)ADDR(0x50005000), sizeof(weapon))); 509 assert(!memcmp(results, (void *)ADDR(0x50008100), sizeof(results))); 510 } 511 g_esp = 0x5000ff00; 512 MEM32(g_esp + 4) = 0; 513 MEM32(g_esp + 8) = 6; 514 g_ecx = 0x50003000; 515 sub_3607A2E0(); 516 517 assert(MEM32(0x50002300) == 193 && deaths == 0); 518 assert(info_available == cases[i].snapshots && feedbacks == 1 && ammo_updates == 1); 519 assert(MEM32(0x50005014) == (cases[i].index + 1) % 50); 520 assert(MEM32(0x50005018) == MEM32(0x50005014)); 521 522 // An outcome received after inspection still applies its real damage. 523 g_esp = 0x5000ff00; 524 g_ecx = 0x50000000; 525 sub_360AECB0(); 526 g_esp = 0x5000ff00; 527 MEM32(g_esp + 4) = 0; 528 MEM32(g_esp + 8) = 6; 529 g_ecx = 0x50003000; 530 sub_3607A2E0(); 531 assert(MEM32(0x50002300) == 186 && feedbacks == 2 && ammo_updates == 2); 532 } 533 } 534 535 int main(void) 536 { 537 assert(guest_map("critical result fixture", 0x50000000, 0x10000)); 538 assert(guest_map("critical result imports", 0x36160000, 0x20000)); 539 assert(guest_map("actor directory", 0x38060000, 0x10000)); 540 queued_results(); 541 missing_attacker_results(); 542 backlog_results(); 543 zero_result_backlog(); 544 absolute_health_keeps_real_weapon_results(); 545 inspection_keeps_real_weapon_results(); 546 puts("native weapon results: impact, backlog drain, absolute HP reconciliation, feedback, death and ammo passed"); 547 }