client/tests/character_reference.c (5736B)
1 #include "recomp_types.h" 2 #include <assert.h> 3 4 void sub_2B074A20(void); 5 void sub_2B0735D0(void); 6 static unsigned renders, queries, publications, identities, allocations; 7 static uint32_t level, profession, title; 8 static int npc; 9 static char published[80]; 10 11 static void instance(void) { g_eax = 0x50002000; g_esp += 4; } 12 static void is_npc(void) { g_eax = npc; g_esp += 8; } 13 static void actor_skill(void) 14 { 15 assert(g_ecx == 0x50002000); 16 uint32_t identity = MEM32(g_esp + 4); 17 assert(MEM32(identity) == 50000 && MEM32(identity + 4) == 52); 18 switch (MEM32(g_esp + 8)) { 19 case 54: g_eax = level; break; 20 case 368: g_eax = profession; break; 21 case 37: g_eax = title; break; 22 default: abort(); 23 } 24 ++queries; 25 g_esp += 12; 26 } 27 static void shop_skill(void) { g_eax = 0; g_esp += 12; } 28 29 /* Keep formatting as a sink; exercise the real callback and stat selector. */ 30 static void render_description(void) 31 { 32 assert(g_ecx == 0x50000000 && MEM32(g_esp + 8) == 1); 33 uint32_t identity = MEM32(g_esp + 4), stack = g_esp, values[3]; 34 const uint32_t stats[] = {54, 368, 37}; 35 for (unsigned i = 0; i < 3; ++i) { 36 g_esp -= 12; 37 MEM32(g_esp + 4) = identity; 38 MEM32(g_esp + 8) = stats[i]; 39 g_ecx = 0x50000000; 40 sub_2B0735D0(); 41 assert(g_esp == stack); 42 values[i] = g_eax; 43 } 44 int length = snprintf((char *)ADDR(0x50000300), 80, "%u/%u/%u", values[0], values[1], values[2]); 45 MEM32(0x50000208) = length; 46 ++renders; 47 g_esp += 12; 48 } 49 static void allocate_text(void) 50 { 51 assert(MEM32(g_esp + 4) == MEM32(0x50000208) + 1); 52 ++allocations; 53 g_eax = 0x50000400; 54 g_esp += 4; 55 } 56 static void copy_text(void) 57 { 58 assert(g_ecx == 0x50000200 && MEM32(g_esp + 8) == 0); 59 strcpy((char *)ADDR(MEM32(g_esp + 4)), (char *)ADDR(0x50000300)); 60 g_esp += 12; 61 } 62 static void publish_text(void) 63 { 64 assert(g_ecx == 0x50002000); 65 assert(MEM32(g_esp + 4) == 0x20 && MEM32(g_esp + 8) == 0xb2); 66 assert(MEM32(g_esp + 16) == strlen((char *)ADDR(MEM32(g_esp + 12))) + 1); 67 snprintf(published, sizeof(published), "%s", (char *)ADDR(MEM32(g_esp + 12))); 68 ++publications; 69 g_esp += 20; 70 } 71 static void publish_identity(void) 72 { 73 assert(g_ecx == 0x50002000 && MEM32(g_esp + 4) == 0x50000100); 74 ++identities; 75 g_esp += 8; 76 } 77 static void release_text(void) { assert(MEM32(g_esp + 4) == 0x50000400); g_esp += 4; } 78 static void resize_text(void) 79 { 80 assert(g_ecx == 0x50000200 && MEM32(g_esp + 4) == 3 && MEM32(g_esp + 8) == 1); 81 g_eax = 1; 82 g_esp += 12; 83 } 84 85 recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } 86 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } 87 recomp_func_t recomp_lookup(uint32_t address) 88 { 89 switch (address) { 90 case 1: return instance; 91 case 2: return is_npc; 92 case 3: return actor_skill; 93 case 4: return shop_skill; 94 case 5: return copy_text; 95 case 6: return publish_text; 96 case 7: return publish_identity; 97 case 9: return resize_text; 98 case 0x2b06fc30: return render_description; 99 case 0x2b0ab212: return allocate_text; 100 case 0x2b0ab1ba: return release_text; 101 default: fprintf(stderr, "unexpected reference call: %08x\n", address); abort(); 102 } 103 } 104 105 static void arrange(uint8_t mode) 106 { 107 memset((void *)ADDR(0x50000000), 0, 0x10000); 108 MEM8(0x5000001c) = mode; 109 MEM32(0x50000000) = MEM32(0x50000020) = MEM32(0x50000100) = 50000; 110 MEM32(0x50000004) = MEM32(0x50000024) = MEM32(0x50000104) = 52; 111 MEM32(0x50000028) = 0x50000200; 112 MEM32(0x50000204) = 0x50000300; 113 g_esp = 0x5000ff00; 114 MEM32(g_esp + 4) = 0x50000100; 115 g_ecx = 0x50000000; 116 g_ebx = 111; g_esi = 222; g_edi = 333; g_ebp = 444; 117 renders = queries = publications = identities = allocations = 0; 118 published[0] = 0; 119 npc = 0; 120 } 121 122 static void assert_abi(void) 123 { 124 assert(g_esp == 0x5000ff08); 125 assert(g_ebx == 111 && g_esi == 222 && g_edi == 333 && g_ebp == 444); 126 } 127 128 static void test_player_description(uint8_t mode) 129 { 130 arrange(mode); 131 level = 150; profession = 12; title = 5; 132 133 sub_2B074A20(); 134 135 assert(!strcmp(published, "150/12/5")); 136 assert(renders == 1 && queries == 3 && publications == 1 && identities == 1 && allocations == 1); 137 assert(MEM8(0x5000001c) == mode); 138 assert(MEM32(0x50000000) == 0 && MEM32(0x50000004) == 0); 139 assert_abi(); 140 } 141 142 static void test_unrelated_reply(unsigned offset) 143 { 144 arrange(141); 145 MEM32(0x50000100 + offset) = 53; 146 147 sub_2B074A20(); 148 149 assert(renders == 0 && queries == 0 && publications == 0 && identities == 0 && allocations == 0); 150 assert(MEM8(0x5000001c) == 141 && MEM32(0x50000000) == 50000 && MEM32(0x50000004) == 52); 151 assert_abi(); 152 } 153 154 static void test_npc_description(void) 155 { 156 arrange(1); 157 npc = 1; 158 159 sub_2B074A20(); 160 161 assert(!strcmp(published, "npc")); 162 assert(renders == 0 && queries == 0 && publications == 1 && identities == 1 && allocations == 1); 163 assert(MEM8(0x5000001c) == 1); 164 assert_abi(); 165 } 166 167 int main(void) 168 { 169 assert(guest_map("reference imports and text", 0x2b000000, 0x100000)); 170 assert(guest_map("reference fixture", 0x50000000, 0x10000)); 171 MEM32(0x2b0b5d6c) = MEM32(0x2b0b53cc) = 1; 172 MEM32(0x2b0b5e94) = 2; 173 MEM32(0x2b0b5ee0) = 3; MEM32(0x2b0b5e74) = 4; 174 MEM32(0x2b0b5b84) = 5; MEM32(0x2b0b53c4) = 6; 175 MEM32(0x2b0b53b4) = 7; MEM32(0x2b0b5154) = 9; 176 strcpy((char *)ADDR(0x2b0e517c), "npc"); 177 test_player_description(0); 178 test_player_description(1); 179 test_player_description(141); 180 test_player_description(255); 181 test_unrelated_reply(0); 182 test_unrelated_reply(4); 183 test_npc_description(); 184 puts("character references: actor stats, mode isolation, reply identity, NPC and ABI passed"); 185 }