client/tests/terrain_normal.c (4671B)
1 #include "recomp_types.h" 2 #include <assert.h> 3 4 void sub_380385E0(void); 5 void sub_3B001170(void); 6 void sub_3B0010E0(void); 7 void sub_3B001110(void); 8 void sub_3B003730(void); 9 static unsigned diagonal, registrations, corner_calls; 10 11 static void corner_points(void) 12 { 13 const float corners[] = {0,0,0, 4,0,0, 4,4,4, 0,0,4}; 14 for (unsigned i = 0; i < 4; ++i) 15 memcpy((void *)ADDR(MEM32(g_esp + 12 + i * 4)), corners + i * 3, 12); 16 ++corner_calls; 17 g_eax = diagonal; 18 g_esp += 28; 19 } 20 static void size(void) { g_eax = 1; g_esp += 4; } 21 static void to_integer(void) { g_eax = (int32_t)fp_pop_impl(g_st, &g_fp_top); g_esp += 4; } 22 static void register_exit(void) { ++registrations; g_eax = 0; g_esp += 4; } 23 static void yield(void) {} 24 recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } 25 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } 26 recomp_func_t recomp_lookup(uint32_t address) 27 { 28 switch (address) { 29 case 0x38038370: return corner_points; 30 case 0x38039da0: case 0x38039db0: return size; 31 case 0x3804b688: return to_integer; 32 case 0x3804b54c: return register_exit; 33 case 0x3b001170: case 1: return sub_3B001170; 34 case 0x3b0010e0: return sub_3B0010E0; 35 case 0x3b001110: return sub_3B001110; 36 case 0x3b003730: return sub_3B003730; 37 default: abort(); 38 } 39 } 40 41 static void fixture(unsigned parity, float x, float z) 42 { 43 diagonal = parity; 44 registrations = corner_calls = 0; 45 recomp_yield_hook = NULL; 46 MEM32(0x3804e4c8) = 0x3b001170; 47 MEM32(0x3804e4d4) = 0x3b001110; 48 MEM32(0x3804e4ec) = 0x3b003730; 49 MEM32(0x3804e4f4) = 0x3b0010e0; 50 *(float *)ADDR(0x3b006048) = 1; 51 memset((void *)ADDR(0x3806ef70), 0, 0x40); 52 53 g_ecx = 0x50000000; g_esp = 0x5001ff00; 54 g_ebx = 111; g_esi = 222; g_edi = 333; g_ebp = 444; 55 g_fp_top = 2; g_st[0] = 17.25; g_st[1] = -31.5; 56 g_st_i64[0] = INT64_MAX; g_st_i64[1] = INT64_MIN; 57 MEM32(0x5000000c) = 0x50001000; 58 *(float *)ADDR(0x50009264) = 4; 59 MEM32(g_esp + 4) = 0x5000a000; 60 MEM32(g_esp + 8) = 0x5000a010; 61 *(float *)ADDR(0x5000a000) = x; 62 *(float *)ADDR(0x5000a004) = 51; 63 *(float *)ADDR(0x5000a008) = z; 64 memset((void *)ADDR(0x5000a010), 0xa5, 12); 65 } 66 67 static void check_abi(void) 68 { 69 assert(g_esp == 0x5001ff0c); 70 assert(g_ebx == 111 && g_esi == 222 && g_edi == 333 && g_ebp == 444); 71 assert(g_fp_top == 2 && g_st[0] == 17.25 && g_st[1] == -31.5); 72 assert(g_st_i64[0] == INT64_MAX && g_st_i64[1] == INT64_MIN); 73 } 74 75 static void triangle(unsigned parity, float x, float z, float height, float nx, float ny, float nz) 76 { 77 fixture(parity, x, z); 78 79 sub_380385E0(); 80 81 assert(fp_pop_impl(g_st, &g_fp_top) == height); 82 assert(fabs(*(float *)ADDR(0x5000a010) - nx) < 1e-6); 83 assert(fabs(*(float *)ADDR(0x5000a014) - ny) < 1e-6); 84 assert(fabs(*(float *)ADDR(0x5000a018) - nz) < 1e-6); 85 assert(registrations == 4 && corner_calls == 1); 86 check_abi(); 87 } 88 89 static void outside(float x, float z) 90 { 91 fixture(0, x, z); 92 93 sub_380385E0(); 94 95 assert(fp_pop_impl(g_st, &g_fp_top) == 0); 96 for (unsigned i = 0; i < 3; ++i) assert(MEM32(0x5000a010 + i * 4) == 0xa5a5a5a5); 97 assert(registrations == 0 && corner_calls == 0); 98 check_abi(); 99 } 100 101 static void alias_and_fallback(unsigned fallback) 102 { 103 fixture(1, 3, .5f); 104 MEM32(g_esp + 8) = 0x5000a000; 105 if (fallback == 1) recomp_yield_hook = yield; 106 if (fallback == 2) MEM32(0x3804e4c8) = 1; 107 108 sub_380385E0(); 109 110 /* The plane uses the overwritten input coordinates before normalization. */ 111 assert(fp_pop_impl(g_st, &g_fp_top) == -16); 112 assert(*(float *)ADDR(0x5000a000) == 0); 113 assert(fabs(*(float *)ADDR(0x5000a004) - .70710678f) < 1e-6); 114 assert(fabs(*(float *)ADDR(0x5000a008) + .70710678f) < 1e-6); 115 check_abi(); 116 } 117 118 int main(void) 119 { 120 assert(guest_map("terrain normal fixture", 0x50000000, 0x20000)); 121 assert(guest_map("terrain imports", 0x38040000, 0x10000)); 122 assert(guest_map("terrain globals", 0x38060000, 0x10000)); 123 assert(guest_map("vector constants", 0x3b000000, 0x10000)); 124 triangle(1, 3, .5f, .5f, 0, .70710678f, -.70710678f); 125 triangle(1, .5f, 3, .5f, -.70710678f, .70710678f, 0); 126 triangle(1, 2, 2, 2, -.70710678f, .70710678f, 0); 127 triangle(0, .5f, .5f, 0, 0, 1, 0); 128 triangle(0, 3, 3, 2, -.57735027f, .57735027f, -.57735027f); 129 triangle(0, 2, 2, 0, 0, 1, 0); 130 outside(-1, 0); outside(0, -1); outside(4, 0); outside(0, 4); 131 outside(NAN, 0); outside(0, NAN); outside(INFINITY, 0); 132 for (unsigned fallback = 0; fallback < 3; ++fallback) alias_and_fallback(fallback); 133 puts("terrain normals: triangles, edges, bounds, aliasing and fallback passed"); 134 }