client/tests/tile_corners.c (7905B)
1 #include "recomp_types.h" 2 #include <assert.h> 3 4 void sub_38038370(void); 5 void sub_38039DA0(void); 6 void sub_38039DB0(void); 7 void sub_38039DC0(void); 8 void sub_38039E60(void); 9 void sub_38039EC0(void); 10 11 void sub_25040840(void); 12 void sub_25040C80(void); 13 void sub_25040CB0(void); 14 15 static unsigned patch_calls, raw_calls, height_calls; 16 static uint32_t patch_coordinates[2][2], raw_coordinates[2][2], height_coordinates[2][2]; 17 18 /* These three imports supply terrain data; all tilemap functions are real. */ 19 static void patch_handle(void) 20 { 21 assert(g_ecx == 0x5000a000 && patch_calls < 2); 22 patch_coordinates[patch_calls][0] = MEM32(g_esp + 4); 23 patch_coordinates[patch_calls++][1] = MEM32(g_esp + 8); 24 g_eax = 0x5000b000; 25 g_esp += 12; 26 } 27 28 static void raw_height(void) 29 { 30 assert(g_ecx == 0x5000a000 && raw_calls < 2); 31 assert(MEM32(g_esp + 4) == 0x5000b000); 32 raw_coordinates[raw_calls][0] = MEM32(g_esp + 8); 33 raw_coordinates[raw_calls][1] = MEM32(g_esp + 12); 34 g_eax = raw_calls++ == 0 ? 0x0102 : 0x0506; 35 g_esp += 16; 36 g_st[7] = 91.25; 37 g_st_i64[7] = INT64_MAX; 38 } 39 40 static void height(void) 41 { 42 assert(g_ecx == 0x5000a000 && height_calls < 2); 43 height_coordinates[height_calls][0] = MEM32(g_esp + 4); 44 height_coordinates[height_calls][1] = MEM32(g_esp + 8); 45 g_eax = height_calls++ == 0 ? 0x0304 : 0x0708; 46 if (!MEM8(g_ecx + 0x29)) g_eax >>= 8; 47 g_esp += 12; 48 g_st[7] = -42.5; 49 g_st_i64[7] = INT64_MIN; 50 } 51 52 recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } 53 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } 54 recomp_func_t recomp_lookup(uint32_t address) 55 { 56 switch (address) { 57 case 0x38039dc0: return sub_38039DC0; 58 case 0x38039da0: return sub_38039DA0; 59 case 0x38039db0: return sub_38039DB0; 60 case 0x38039e60: return sub_38039E60; 61 case 0x38039ec0: return sub_38039EC0; 62 case 0x25040840: return sub_25040840; 63 case 0x25040c80: return sub_25040C80; 64 case 0x25040cb0: return sub_25040CB0; 65 case 0x500: return patch_handle; 66 case 0x504: return raw_height; 67 case 0x508: return height; 68 default: abort(); 69 } 70 } 71 72 static void arrange_tilemap(void) 73 { 74 /* Place the tilemap, its backing data, outputs, and guest stack separately. */ 75 memset((void *)ADDR(0x50000000), 0, 0x20000); 76 patch_calls = raw_calls = height_calls = 0; 77 g_eax = 123; g_ecx = 0x50000000; g_edx = 234; 78 g_esp = 0x5001ff00; g_ebp = 345; g_ebx = 456; g_esi = 567; g_edi = 678; 79 g_flag_k = FK_NONE; g_flag_a = 0; g_flag_b = 0; g_flag_cf = 0; 80 g_fp_top = 3; 81 for (unsigned i = 0; i < 8; ++i) { g_st[i] = i + .25; g_st_i64[i] = INT64_MAX - i; } 82 MEM32(0x5000000c) = 0x50001000; 83 *(float *)ADDR(0x5000101c) = .5f; 84 MEM32(0x50001228) = 0x5000b000; 85 MEM32(0x50009258) = 0x5000a000; 86 MEM32(0x5000925c) = 512; 87 MEM32(0x50009260) = 1024; 88 *(float *)ADDR(0x50009264) = 3.25f; 89 MEM32(0x5000a03c) = 4; 90 MEM32(0x5000a040) = 15; 91 92 /* The guest thiscall receives two indices and four Vector3 references. */ 93 MEM32(g_esp) = 0x11223344; 94 MEM32(g_esp + 4) = 2; 95 MEM32(g_esp + 8) = 3; 96 MEM32(g_esp + 12) = 0x5000f000; 97 MEM32(g_esp + 16) = 0x5000f00c; 98 MEM32(g_esp + 20) = 0x5000f018; 99 MEM32(g_esp + 24) = 0x5000f024; 100 MEM32(g_esp + 28) = 0x55667788; 101 } 102 103 static void assert_preserved_state(void) 104 { 105 assert(g_esp == 0x5001ff1c); 106 assert(g_ebp == 345 && g_ebx == 456 && g_esi == 567 && g_edi == 678); 107 assert(MEM32(0x5001ff00) == 0x11223344); 108 assert(MEM32(0x5001ff1c) == 0x55667788); 109 assert(g_fp_top == 3); 110 assert(g_st[0] == .25 && g_st[1] == 1.25 && g_st[2] == 2.25); 111 assert(g_st_i64[0] == INT64_MAX && g_st_i64[1] == INT64_MAX - 1); 112 assert(g_st_i64[2] == INT64_MAX - 2); 113 assert(g_st[4] == 0 && g_st[5] == 0 && g_st[6] == 0 && g_st[7] == 0); 114 assert(g_st_i64[4] == 0 && g_st_i64[5] == 0 && g_st_i64[6] == 0 && g_st_i64[7] == 0); 115 } 116 117 static void ground_8bit(void) 118 { 119 arrange_tilemap(); 120 const float expected[] = {6.5f, .5f, 9.75f, 9.75f, 1.5f, 9.75f, 121 9.75f, 2.5f, 13.f, 6.5f, 3.5f, 13.f}; 122 123 sub_38038370(); 124 125 assert(memcmp((void *)ADDR(0x5000f000), expected, sizeof(expected)) == 0); 126 assert((g_eax & 255) == 0); 127 assert(patch_calls == 2 && raw_calls == 2 && height_calls == 2); 128 assert(patch_coordinates[0][0] == 0 && patch_coordinates[0][1] == 0); 129 assert(patch_coordinates[1][0] == 0 && patch_coordinates[1][1] == 0); 130 assert(raw_coordinates[0][0] == 2 && raw_coordinates[0][1] == 3); 131 assert(raw_coordinates[1][0] == 3 && raw_coordinates[1][1] == 4); 132 assert(height_coordinates[0][0] == 3 && height_coordinates[0][1] == 3); 133 assert(height_coordinates[1][0] == 2 && height_coordinates[1][1] == 4); 134 assert_preserved_state(); 135 } 136 137 static void ground_16bit(void) 138 { 139 arrange_tilemap(); 140 MEM8(0x5000a029) = 1; 141 const float expected[] = {6.5f, 129.f, 9.75f, 9.75f, 386.f, 9.75f, 142 9.75f, 643.f, 13.f, 6.5f, 900.f, 13.f}; 143 144 sub_38038370(); 145 146 assert(memcmp((void *)ADDR(0x5000f000), expected, sizeof(expected)) == 0); 147 assert((g_eax & 255) == 0); 148 assert(patch_calls == 2 && raw_calls == 2 && height_calls == 2); 149 assert_preserved_state(); 150 } 151 152 static void dungeon_heights(void) 153 { 154 arrange_tilemap(); 155 MEM32(0x50001018) = 1; 156 MEM8(0x5000b401) = 2; 157 MEM8(0x5000b402) = 4; 158 MEM8(0x5000b602) = 6; 159 MEM8(0x5000b601) = 8; 160 const float expected[] = {6.5f, 1.f, 9.75f, 9.75f, 2.f, 9.75f, 161 9.75f, 3.f, 13.f, 6.5f, 4.f, 13.f}; 162 163 sub_38038370(); 164 165 assert(memcmp((void *)ADDR(0x5000f000), expected, sizeof(expected)) == 0); 166 assert((g_eax & 255) == 1); 167 assert(patch_calls == 0 && raw_calls == 0 && height_calls == 0); 168 assert_preserved_state(); 169 } 170 171 static void aliased_corner_rounding(void) 172 { 173 arrange_tilemap(); 174 MEM8(0x5000a029) = 1; 175 MEM32(0x5000925c) = 16777219; 176 MEM32(0x50009260) = 16777219; 177 *(float *)ADDR(0x50009264) = 1.f; 178 MEM32(g_esp + 4) = 16777217; 179 MEM32(g_esp + 8) = 16777217; 180 MEM32(g_esp + 16) = 0x5000f000; 181 MEM32(g_esp + 20) = 0x5000f000; 182 MEM32(g_esp + 24) = 0x5000f000; 183 const float expected[] = {16777216.f, 900.f, 16777218.f}; 184 185 sub_38038370(); 186 187 assert(memcmp((void *)ADDR(0x5000f000), expected, sizeof(expected)) == 0); 188 assert((g_eax & 255) == 1); 189 assert_preserved_state(); 190 } 191 192 static void resident_patch_boundary(unsigned full) 193 { 194 arrange_tilemap(); 195 MEM32(0x3804e24c) = 0x25040c80; 196 MEM32(0x3804e248) = 0x25040cb0; 197 MEM32(0x3804e244) = 0x25040840; 198 MEM8(0x5000a029) = full; 199 MEM32(0x5000a024) = 12345; 200 MEM32(0x5000a038) = 16; 201 MEM32(0x5000a048) = 2; 202 MEM32(0x5000a050) = 0x5000b000; 203 for (unsigned i = 0; i < 4; ++i) 204 MEM32(0x5000b000 + i * 84 + 0x1c) = 0x50010000 + i * 0x1000; 205 MEM16(0x5001021c) = 0x0102; 206 MEM16(0x500111fe) = 0x0304; 207 MEM16(0x50013000) = 0x0506; 208 MEM16(0x5001201e) = 0x0708; 209 MEM32(g_esp + 4) = 15; 210 MEM32(g_esp + 8) = 15; 211 const float expected8[] = {48.75f,.5f,48.75f, 52,1.5f,48.75f, 212 52,2.5f,52, 48.75f,3.5f,52}; 213 const float expected16[] = {48.75f,129,48.75f, 52,386,48.75f, 214 52,643,52, 48.75f,900,52}; 215 216 sub_38038370(); 217 218 assert(!memcmp((void *)ADDR(0x5000f000), full ? expected16 : expected8, sizeof(expected8))); 219 for (unsigned i = 0; i < 4; ++i) 220 assert(MEM32(0x5000b000 + i * 84 + 0x18) == 12345); 221 assert((g_eax & 255) == 1); 222 assert(g_ecx == 0x5000f024); 223 assert_preserved_state(); 224 } 225 226 int main(void) 227 { 228 assert(guest_map("tilemap test", 0x50000000, 0x20000)); 229 assert(guest_map("tilemap imports", 0x38040000, 0x10000)); 230 MEM32(0x3804e24c) = 0x500; 231 MEM32(0x3804e248) = 0x504; 232 MEM32(0x3804e244) = 0x508; 233 ground_8bit(); 234 ground_16bit(); 235 dungeon_heights(); 236 aliased_corner_rounding(); 237 resident_patch_boundary(0); 238 resident_patch_boundary(1); 239 puts("tile corner contracts passed"); 240 }