aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


client/tests/terrain_line.c (6828B)


      1 #include "recomp_types.h"
      2 #include <assert.h>
      3 
      4 void sub_3803B970(void);
      5 void sub_3803C130(void);
      6 void sub_3803C150(void);
      7 static uint32_t visited[5][2];
      8 static unsigned visits, hit_at;
      9 static float hit_fraction, child_fraction;
     10 
     11 static void width(void) { g_eax = 640; g_esp += 4; }
     12 static void height(void) { g_eax = 512; g_esp += 4; }
     13 static void to_integer(void)
     14 {
     15     int64_t value = fp_st0_to_i64(g_st[0], g_st_i64[0], 0x0c7f);
     16     fp_pop_impl(g_st, &g_fp_top);
     17     g_eax = (uint32_t)value;
     18     g_edx = (uint32_t)((uint64_t)value >> 32);
     19     g_esp += 4;
     20 }
     21 static void round_down(void)
     22 {
     23     fp_push_impl(g_st, &g_fp_top, floor(MEMD(g_esp + 4)));
     24     g_esp += 4;
     25 }
     26 static void subtract(void)
     27 {
     28     uint32_t from = MEM32(g_esp + 4);
     29     for (unsigned i = 0; i < 3; ++i)
     30         *(float *)ADDR(g_ecx + i * 4) = (float)((double)*(float *)ADDR(g_ecx + i * 4)
     31             - *(float *)ADDR(from + i * 4));
     32     g_eax = g_ecx;
     33     g_esp += 8;
     34 }
     35 static void tile(void)
     36 {
     37     assert(visits < 5);
     38     visited[visits][0] = MEM32(g_esp + 4);
     39     visited[visits][1] = MEM32(g_esp + 8);
     40     ++visits;
     41     g_eax = visits == hit_at;
     42     if (g_eax) {
     43         uint32_t output = MEM32(g_esp + 12), normal = MEM32(g_esp + 16);
     44         for (unsigned i = 0; i < 3; ++i) {
     45             double start = *(float *)ADDR(0x3806f028 + i * 4);
     46             *(float *)ADDR(output + i * 4) = (float)(start + hit_fraction *
     47                 ((double)*(float *)ADDR(0x3806f008 + i * 4) - start));
     48             *(float *)ADDR(normal + i * 4) = i == 1 ? 1 : 0;
     49         }
     50     }
     51     g_esp += 20;
     52 }
     53 static void child(void)
     54 {
     55     uint32_t output = MEM32(g_esp + 12), normal = MEM32(g_esp + 16);
     56     for (unsigned i = 0; i < 3; ++i) {
     57         double start = *(float *)ADDR(0x3806f028 + i * 4);
     58         *(float *)ADDR(output + i * 4) = (float)(start + child_fraction *
     59             ((double)*(float *)ADDR(0x3806f008 + i * 4) - start));
     60         *(float *)ADDR(normal + i * 4) = i == 1 ? -1 : 0;
     61     }
     62     g_eax = 1;
     63     g_esp += 28;
     64 }
     65 recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; }
     66 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); }
     67 recomp_func_t recomp_lookup(uint32_t address)
     68 {
     69     switch (address) {
     70     case 0x38039da0: return width;
     71     case 0x38039db0: return height;
     72     case 0x3804b688: return to_integer;
     73     case 0x500: return round_down;
     74     case 0x504: return subtract;
     75     case 0x508: return child;
     76     case 0x3803c220: return tile;
     77     case 0x3803c130: return sub_3803C130;
     78     case 0x3803c150: return sub_3803C150;
     79     default: fprintf(stderr, "Unexpected call %08x\n", address); abort();
     80     }
     81 }
     82 
     83 struct result {
     84     uint32_t abi[6], outputs[6], path[5][2];
     85     unsigned count;
     86     int top;
     87     double live_fpu[3];
     88     int64_t live_exact[3];
     89 };
     90 static void run(struct result *result, const float points[6], float spacing, int nested)
     91 {
     92     memset((void *)ADDR(0x50000000), 0, 0x20000);
     93     memset((void *)ADDR(0x38060000), 0, 0x10000);
     94     g_ecx = 0x50000000; g_esp = 0x5001ff00;
     95     g_ebp = 123; g_ebx = 234; g_esi = 345; g_edi = 456;
     96     g_eax = 567; g_edx = 678; g_fp_top = 3;
     97     g_flag_k = FK_NONE; g_flag_a = g_flag_b = g_flag_cf = 0;
     98     for (unsigned i = 0; i < 8; ++i) { g_st[i] = i + .25; g_st_i64[i] = INT64_MAX - i; }
     99     MEM32(g_ecx + 4) = 0x50001000;
    100     MEM32(0x5000100c) = 0x50002000;
    101     *(float *)ADDR(0x5000a264) = spacing;
    102     *(float *)ADDR(g_ecx + 16) = 640 * spacing;
    103     *(float *)ADDR(g_ecx + 20) = 512 * spacing;
    104     if (nested) {
    105         MEM32(g_ecx + 12) = 0x5000b000;
    106         MEM32(0x5000b000) = 0x5000b100;
    107         MEM32(0x5000b110) = 0x508;
    108     }
    109     memcpy((void *)ADDR(0x5000c000), points, 24);
    110     MEM32(g_esp + 4) = 0x5000c000;
    111     MEM32(g_esp + 8) = 0x5000c00c;
    112     MEM32(g_esp + 12) = 0x5000d000;
    113     MEM32(g_esp + 16) = 0x5000d00c;
    114     MEM32(g_esp + 20) = 1;
    115     MEM32(g_esp + 24) = 0;
    116     memset((void *)ADDR(0x5000d000), 0xa5, 24);
    117     visits = 0;
    118 
    119     sub_3803B970();
    120 
    121     memset(result, 0, sizeof(*result));
    122     uint32_t abi[] = {g_eax & 255, g_esp, g_ebp, g_ebx, g_esi, g_edi};
    123     memcpy(result->abi, abi, sizeof(abi));
    124     memcpy(result->outputs, (void *)ADDR(0x5000d000), 24);
    125     result->count = visits;
    126     memcpy(result->path, visited, visits * sizeof(*visited));
    127     result->top = g_fp_top;
    128     memcpy(result->live_fpu, g_st, sizeof(result->live_fpu));
    129     memcpy(result->live_exact, g_st_i64, sizeof(result->live_exact));
    130 }
    131 
    132 int main(void)
    133 {
    134     assert(guest_map("line fixtures", 0x50000000, 0x20000));
    135     assert(guest_map("line constants", 0x38040000, 0x10000));
    136     assert(guest_map("line globals", 0x38060000, 0x10000));
    137     MEM32(0x3804e0ac) = 0x500; MEM32(0x3804e4f4) = 0x504;
    138     *(float *)ADDR(0x3804e8e4) = 0;
    139     *(float *)ADDR(0x3804e8cc) = 1;
    140     *(float *)ADDR(0x3804eabc) = 100;
    141     *(float *)ADDR(0x3804f7a0) = -100;
    142     *(float *)ADDR(0x3804ea94) = .1f;
    143     /* Stationary, forward/reverse ties, clipping, tile/child hits, and float boundaries. */
    144     const float points[][6] = {
    145         {.5f,10,.5f, .5f,-5,.5f},
    146         {.5f,10,.5f, 2.5f,-5,2.5f},
    147         {2.5f,10,2.5f, .5f,-5,.5f},
    148         {-1,10,.5f, 2.5f,-5,.5f},
    149         {.5f,10,.5f, 2.5f,-5,2.5f},
    150         {.5f,10,.5f, 2.5f,-5,2.5f},
    151         {331.499969f,10,331.500031f,334.099976f,-5,334.100037f},
    152     };
    153     const unsigned counts[] = {1, 5, 5, 3, 2, 2, 5};
    154     const uint32_t paths[7][5][2] = {
    155         {{0,0}},
    156         {{0,0}, {0,1}, {1,1}, {1,2}, {2,2}},
    157         {{2,2}, {2,1}, {1,1}, {1,0}, {0,0}},
    158         {{0,0}, {1,0}, {2,0}},
    159         {{0,0}, {0,1}},
    160         {{0,0}, {0,1}},
    161         {{254,255}, {255,255}, {255,256}, {256,256}, {256,257}},
    162     };
    163     const float tile_hit[] = {1.5f, 2.5f, 1.5f, 0, 1, 0};
    164     const float child_hit[] = {1, 6.25f, 1, 0, -1, 0};
    165     const uint32_t no_hit[] = {0xbf800000, 0xbf800000, 0xbf800000,
    166         0xa5a5a5a5, 0xa5a5a5a5, 0xa5a5a5a5};
    167     for (unsigned i = 0; i < 7; ++i) {
    168         struct result result;
    169         hit_at = i == 4 || i == 5 ? 2 : 0;
    170         hit_fraction = .5f; child_fraction = .25f;
    171 
    172         run(&result, points[i], i == 6 ? 1.3f : 1, i == 5);
    173 
    174         assert(result.abi[0] == (i == 4 || i == 5));
    175         assert(result.count == counts[i]);
    176         assert(!memcmp(result.path, paths[i], counts[i] * sizeof(paths[i][0])));
    177         assert(!memcmp(result.outputs, i == 4 ? (const void *)tile_hit :
    178             i == 5 ? (const void *)child_hit : (const void *)no_hit, 24));
    179         assert(result.abi[1] == 0x5001ff1c);
    180         assert(result.abi[2] == 123 && result.abi[3] == 234);
    181         assert(result.abi[4] == 345 && result.abi[5] == 456);
    182         assert(result.top == 3);
    183         for (unsigned j = 0; j < 3; ++j) {
    184             assert(result.live_fpu[j] == j + .25);
    185             assert(result.live_exact[j] == INT64_MAX - j);
    186         }
    187     }
    188     puts("terrain traversal paths, clipping, hit selection and ABI passed");
    189     return 0;
    190 }