client/tests/grid_walk.c (3937B)
1 #include "recomp_types.h" 2 #include <assert.h> 3 4 void sub_4200F990(void); 5 static uint32_t visited[32]; 6 static unsigned visits; 7 8 static void visit_cell(void) 9 { 10 assert(g_ecx == 0x50000100); 11 assert(visits < 32); 12 visited[visits++] = MEM32(g_esp + 4); 13 /* The visitor may clobber caller-saved registers and use the floating stack. */ 14 g_eax = 0x12345678; g_ecx = 0xabcdef; g_edx = 0x87654321; 15 fp_push_impl(g_st, &g_fp_top, 123.5); 16 fp_pop_impl(g_st, &g_fp_top); 17 g_esp += 8; 18 } 19 static void to_integer(void) 20 { 21 int64_t value = fp_st0_to_i64(g_st[0], g_st_i64[0], 0x0c7f); 22 fp_pop_impl(g_st, &g_fp_top); 23 g_eax = (uint32_t)value; 24 g_edx = (uint32_t)((uint64_t)value >> 32); 25 g_esp += 4; 26 } 27 static void round_down(void) 28 { 29 fp_push_impl(g_st, &g_fp_top, floor(MEMD(g_esp + 4))); 30 g_esp += 4; 31 } 32 recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } 33 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } 34 recomp_func_t recomp_lookup(uint32_t address) 35 { 36 switch (address) { 37 case 0x42016f3c: return to_integer; 38 case 0x504: return round_down; 39 case 0x508: return visit_cell; 40 default: fprintf(stderr, "Unexpected call %08x\n", address); abort(); 41 } 42 } 43 44 static void run(const float points[4], float spacing) 45 { 46 g_esp = 0x5001ff00; 47 g_ebp = 123; g_ebx = 234; g_esi = 345; g_edi = 456; 48 g_fp_top = 3; 49 for (unsigned i = 0; i < 8; ++i) { 50 g_st[i] = i + .25; 51 g_st_i64[i] = INT64_MAX - i; 52 } 53 *(float *)ADDR(0x50000200) = points[0]; 54 *(float *)ADDR(0x50000208) = points[1]; 55 *(float *)ADDR(0x50000210) = points[2]; 56 *(float *)ADDR(0x50000218) = points[3]; 57 MEM32(g_esp + 4) = 0x50000200; 58 MEM32(g_esp + 8) = 0x50000210; 59 MEM32(g_esp + 12) = 0x50000100; 60 MEM32(g_esp + 16) = 8; 61 MEM32(g_esp + 20) = 6; 62 *(float *)ADDR(g_esp + 24) = 8 * spacing; 63 *(float *)ADDR(g_esp + 28) = 6 * spacing; 64 visits = 0; 65 66 sub_4200F990(); 67 68 assert(g_esp == 0x5001ff04); 69 assert(g_ebp == 123 && g_ebx == 234 && g_esi == 345 && g_edi == 456); 70 assert(g_fp_top == 3); 71 for (unsigned i = 0; i < 3; ++i) { 72 assert(g_st[i] == i + .25); 73 assert(g_st_i64[i] == INT64_MAX - i); 74 } 75 } 76 77 int main(void) 78 { 79 assert(guest_map("grid fixtures", 0x50000000, 0x20000)); 80 assert(guest_map("grid constants", 0x42010000, 0x10000)); 81 MEM32(0x50000100) = 0x50000110; 82 MEM32(0x50000110) = 0x508; 83 MEM32(0x42018054) = 0x504; 84 *(float *)ADDR(0x420180f8) = 0; 85 *(float *)ADDR(0x42018430) = 100; 86 *(float *)ADDR(0x42018420) = -100; 87 *(double *)ADDR(0x42018428) = 1; 88 /* Stationary, axis aligned, ties, negative fractional starts, bounds, and 89 * a zero X direction on a cell boundary while moving backward in Z. */ 90 const float points[][4] = { 91 {.5f,.5f, .5f,.5f}, 92 {.5f,.5f, 3.5f,.5f}, 93 {.5f,.5f, .5f,3.5f}, 94 {.5f,.5f, 2.5f,2.5f}, 95 {2.5f,2.5f, .5f,.5f}, 96 {-.5f,.5f, 2.5f,.5f}, 97 {-1,.5f, 2.5f,.5f}, 98 {7.5f,4.5f, 9.5f,4.5f}, 99 {6.5f,5.5f, 6.5f,7.5f}, 100 {2,2, 2,0}, 101 {2,2, 0,2}, 102 {2,2, 0,0}, 103 }; 104 const unsigned counts[] = {1,4,4,5,5,3,0,1,1,3,3,5}; 105 const uint32_t paths[][5] = { 106 {0}, {0,1,2,3}, {0,8,16,24}, {0,8,9,17,18}, 107 {18,10,9,1,0}, {0,1,2}, {0}, {39}, {46}, 108 {18,10,2}, {18,17,16}, {18,10,9,1,0}, 109 }; 110 for (unsigned i = 0; i < sizeof(counts) / sizeof(*counts); ++i) { 111 run(points[i], 1); 112 113 assert(visits == counts[i]); 114 assert(memcmp(visited, paths[i], visits * sizeof(*visited)) == 0); 115 } 116 /* Rounding end Z to float would incorrectly visit cell 24 as well. */ 117 const float boundary[] = {.85f,.85f, .85f,5.1f}; 118 const uint32_t boundary_path[] = {0,8,16}; 119 120 run(boundary, 1.7f); 121 122 assert(visits == 3); 123 assert(memcmp(visited, boundary_path, sizeof(boundary_path)) == 0); 124 puts("grid walk PASS"); 125 return 0; 126 }