aorecomp

Anarchy Online classic in the browser

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


commit 8805d275cd05c854bdbf128dbe110a97757b7406
parent 767140e4f556bb72f1c4bce86fd7862537768d8f
Author: anon <anon@example.com>
Date:   Sat, 17 Oct 2026 02:53:05 +0000

Replace terrain cell traversal emulation with scalar C

Diffstat:
Msrc/Makefile.linux | 7+++++++
Msrc/Makefile.wasm | 4++++
Msrc/game/llr.dll/lifted-0023.c | 259++++++++++++++++++++-----------------------------------------------------------
Asrc/tests/terrain_line.c | 190+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 265 insertions(+), 195 deletions(-)

diff --git a/src/Makefile.linux b/src/Makefile.linux @@ -222,6 +222,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/fpu-stack ./build/linux/water-ring ./build/linux/tile-corners + ./build/linux/terrain-line ./build/linux/network-queue ./build/linux/vertex-upload ./build/linux/vertex-upload-paged @@ -463,3 +464,9 @@ build/linux/archive-string build/linux/archive-string-paged: tests/archive_strin $(CC) $(CPPFLAGS) $(CFLAGS) $(if $(filter %-paged,$@),-U__linux__) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ test: build/linux/archive-string build/linux/archive-string-paged + +build/linux/terrain-line: tests/terrain_line.c game/llr.dll/lifted-0023.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h + mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ + +test: build/linux/terrain-line diff --git a/src/Makefile.wasm b/src/Makefile.wasm @@ -62,3 +62,7 @@ build/wasm/web/mixer-test.html: tests/browser_mixer.html build/wasm/web/mixer-te build/wasm/web/mixer-test.js: tests/audio.c runtime/linux_audio.c runtime/browser_audio.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/linux_path.c runtime/browser_audio.h runtime/browser_audio.js runtime/linux_audio.h runtime/guest_heap.h runtime/guest_memory.h runtime/recomp_types.h runtime/linux_path.h Makefile.wasm @mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) -I../local/wasm/deps/include -sUSE_SDL=2 $(filter %.c,$^) ../local/wasm/deps/lib/libsndfile.a ../local/wasm/deps/lib/libmp3lame.a -sUSE_MPG123=1 -sWASMFS --js-library runtime/browser_audio.js -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=4 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -sALLOW_MEMORY_GROWTH -Wl,--wrap=audio_output_open -o $@ + +build/wasm/terrain-line.js: tests/terrain_line.c game/llr.dll/lifted-0023.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@ diff --git a/src/game/llr.dll/lifted-0023.c b/src/game/llr.dll/lifted-0023.c @@ -2602,173 +2602,50 @@ L_3803BD38: RECOMP_FLAGS_IN(); MEM8(ebp + 0xB) = (uint8_t)(LO8(eax)); /* 0x3803BD54: mov byte ptr [ebp + 0xb], al */ L_3803BD57: - fp_push(*(float*)&MEM32(0x3806F028)); /* 0x3803BD57: fld dword ptr [0x3806f028] */ - _st[0] /= (double)*(float*)ADDR(ebp + (-0x4)); /* 0x3803BD5D: fdiv dword ptr [ebp - 4] */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + 0x18) = _v; } /* 0x3803BD60: fstp dword ptr [ebp + 0x18] */ - fp_push(*(float*)&MEM32(0x3806F030)); /* 0x3803BD63: fld dword ptr [0x3806f030] */ - _st[0] /= (double)*(float*)ADDR(ebp + (-0x4)); /* 0x3803BD69: fdiv dword ptr [ebp - 4] */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + 0x1C) = _v; } /* 0x3803BD6C: fstp dword ptr [ebp + 0x1c] */ - fp_push(*(float*)&MEM32(0x3806F008)); /* 0x3803BD6F: fld dword ptr [0x3806f008] */ - _st[0] /= (double)*(float*)ADDR(ebp + (-0x4)); /* 0x3803BD75: fdiv dword ptr [ebp - 4] */ - fp_push(*(float*)&MEM32(0x3806F010)); /* 0x3803BD78: fld dword ptr [0x3806f010] */ - _st[0] /= (double)*(float*)ADDR(ebp + (-0x4)); /* 0x3803BD7E: fdiv dword ptr [ebp - 4] */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + 0xC) = _v; } /* 0x3803BD81: fstp dword ptr [ebp + 0xc] */ - fp_push(*(float*)&MEM32(ebp + 0x18)); /* 0x3803BD84: fld dword ptr [ebp + 0x18] */ - RECOMP_ICALL(0x3804B688u); /* 0x3803BD87: call 0x3804b688 */ /* not lifted */ - RECOMP_FLAGS_IN(); - fp_push(*(float*)&MEM32(ebp + 0x1C)); /* 0x3803BD8C: fld dword ptr [ebp + 0x1c] */ - edi = eax; /* 0x3803BD8F: mov edi, eax */ - RECOMP_ICALL(0x3804B688u); /* 0x3803BD91: call 0x3804b688 */ /* not lifted */ - RECOMP_FLAGS_IN(); - fp_push(_st[0]); /* 0x3803BD96: fld st(0) */ - esi = eax; /* 0x3803BD98: mov esi, eax */ - RECOMP_ICALL(0x3804B688u); /* 0x3803BD9A: call 0x3804b688 */ /* not lifted */ - RECOMP_FLAGS_IN(); - fp_push(*(float*)&MEM32(ebp + 0xC)); /* 0x3803BD9F: fld dword ptr [ebp + 0xc] */ - ebx = eax; /* 0x3803BDA2: mov ebx, eax */ - RECOMP_ICALL(0x3804B688u); /* 0x3803BDA4: call 0x3804b688 */ /* not lifted */ - RECOMP_FLAGS_IN(); - _st[0] -= (double)*(float*)ADDR(ebp + 0x18); /* 0x3803BDA9: fsub dword ptr [ebp + 0x18] */ - fp_push(*(float*)&MEM32(ebp + 0xC)); /* 0x3803BDAC: fld dword ptr [ebp + 0xc] */ - MEM32(ebp + (-0x10)) = eax; /* 0x3803BDAF: mov dword ptr [ebp - 0x10], eax */ - _st[0] -= (double)*(float*)ADDR(ebp + 0x1C); /* 0x3803BDB2: fsub dword ptr [ebp + 0x1c] */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + 0xC) = _v; } /* 0x3803BDB5: fstp dword ptr [ebp + 0xc] */ - _fpu_cmp = FPU_CMP(_st[0], (double)*(float*)ADDR(0x3804E8E4)); /* 0x3803BDB8: fcom dword ptr [0x3804e8e4] */ - eax = (eax & 0xFFFF0000u) | ((_fpu_cmp >= 0x10000) ? (uint32_t)(_fpu_cmp & 0x4700) : _fpu_cmp == 2 ? 0x4500u : _fpu_cmp < 0 ? 0x0100u : _fpu_cmp == 0 ? 0x4000u : 0x0000u); /* 0x3803BDBE: fnstsw ax */ - /* test HI8(eax), 1 */ /* 0x3803BDC0: test ah, 1 */ - _flag_a = (uint32_t)(HI8(eax)) << 24; _flag_b = (uint32_t)(1) << 24; - _cf = 0; - _flag_k = FK_NARROW(FK_TEST, 24); - if (TEST_NZ(_flag_a, _flag_b)) goto L_3803BDF6; /* 0x3803BDC3: jne 0x3803bdf6 */ -L_3803BDC5: - fp_push(*(float*)&MEM32(0x3804EABC)); /* 0x3803BDC5: fld dword ptr [0x3804eabc] */ - _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - esp = esp - 8; /* 0x3803BDCB: sub esp, 8 */ - _flag_k = FK_CMP; - _st[0] /= _st[1]; /* 0x3803BDCE: fdiv st(1) */ - MEM32(ebp + (-0x8)) = 1; /* 0x3803BDD0: mov dword ptr [ebp - 8], 1 */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + (-0x4)) = _v; } /* 0x3803BDD7: fstp dword ptr [ebp - 4] */ - { _st[0] = _st[0]; g_st_i64[0] = g_st_i64[0]; fp_pop(); } /* 0x3803BDDA: fstp st(0) */ - fp_push(*(float*)&MEM32(ebp + 0x18)); /* 0x3803BDDC: fld dword ptr [ebp + 0x18] */ - { double _v = fp_pop(); *(double*)ADDR(esp) = _v; } /* 0x3803BDDF: fstp qword ptr [esp] */ - RECOMP_ICALL(MEM32(0x3804E0AC)); /* 0x3803BDE2: call dword ptr [0x3804e0ac] */ - RECOMP_FLAGS_IN(); - _st[0] += (double)*(float*)ADDR(0x3804E8CC); /* 0x3803BDE8: fadd dword ptr [0x3804e8cc] */ - _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); - _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); - esp = esp + 8; /* 0x3803BDEE: add esp, 8 */ - _flag_k = FK_ADD; - _st[0] -= (double)*(float*)ADDR(ebp + 0x18); /* 0x3803BDF1: fsub dword ptr [ebp + 0x18] */ - goto L_3803BE1F; /* 0x3803BDF4: jmp 0x3803be1f */ -L_3803BDF6: - fp_push(*(float*)&MEM32(0x3804F7A0)); /* 0x3803BDF6: fld dword ptr [0x3804f7a0] */ - _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - esp = esp - 8; /* 0x3803BDFC: sub esp, 8 */ - _flag_k = FK_CMP; - _st[0] /= _st[1]; /* 0x3803BDFF: fdiv st(1) */ - MEM32(ebp + (-0x8)) = 0xFFFFFFFFu; /* 0x3803BE01: mov dword ptr [ebp - 8], 0xffffffff */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + (-0x4)) = _v; } /* 0x3803BE08: fstp dword ptr [ebp - 4] */ - { _st[0] = _st[0]; g_st_i64[0] = g_st_i64[0]; fp_pop(); } /* 0x3803BE0B: fstp st(0) */ - fp_push(*(float*)&MEM32(ebp + 0x18)); /* 0x3803BE0D: fld dword ptr [ebp + 0x18] */ - { double _v = fp_pop(); *(double*)ADDR(esp) = _v; } /* 0x3803BE10: fstp qword ptr [esp] */ - RECOMP_ICALL(MEM32(0x3804E0AC)); /* 0x3803BE13: call dword ptr [0x3804e0ac] */ - RECOMP_FLAGS_IN(); - _st[0] = (double)*(float*)ADDR(ebp + 0x18) - _st[0]; /* 0x3803BE19: fsubr dword ptr [ebp + 0x18] */ - _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); - _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); - esp = esp + 8; /* 0x3803BE1C: add esp, 8 */ - _flag_k = FK_ADD; -L_3803BE1F: - _st[0] *= (double)*(float*)ADDR(ebp + (-0x4)); /* 0x3803BE1F: fmul dword ptr [ebp - 4] */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + 0x18) = _v; } /* 0x3803BE22: fstp dword ptr [ebp + 0x18] */ - fp_push(*(float*)&MEM32(ebp + 0xC)); /* 0x3803BE25: fld dword ptr [ebp + 0xc] */ - _fpu_cmp = FPU_CMP(_st[0], (double)*(float*)ADDR(0x3804E8E4)); /* 0x3803BE28: fcomp dword ptr [0x3804e8e4] */ - fp_pop(); - eax = (eax & 0xFFFF0000u) | ((_fpu_cmp >= 0x10000) ? (uint32_t)(_fpu_cmp & 0x4700) : _fpu_cmp == 2 ? 0x4500u : _fpu_cmp < 0 ? 0x0100u : _fpu_cmp == 0 ? 0x4000u : 0x0000u); /* 0x3803BE2E: fnstsw ax */ - /* test HI8(eax), 1 */ /* 0x3803BE30: test ah, 1 */ - _flag_a = (uint32_t)(HI8(eax)) << 24; _flag_b = (uint32_t)(1) << 24; - _cf = 0; - _flag_k = FK_NARROW(FK_TEST, 24); - if (TEST_NZ(_flag_a, _flag_b)) goto L_3803BE65; /* 0x3803BE33: jne 0x3803be65 */ -L_3803BE35: - fp_push(*(float*)&MEM32(0x3804EABC)); /* 0x3803BE35: fld dword ptr [0x3804eabc] */ - _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - esp = esp - 8; /* 0x3803BE3B: sub esp, 8 */ - _flag_k = FK_CMP; - _st[0] /= (double)*(float*)ADDR(ebp + 0xC); /* 0x3803BE3E: fdiv dword ptr [ebp + 0xc] */ - MEM32(ebp + (-0xC)) = 1; /* 0x3803BE41: mov dword ptr [ebp - 0xc], 1 */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + 0xC) = _v; } /* 0x3803BE48: fstp dword ptr [ebp + 0xc] */ - fp_push(*(float*)&MEM32(ebp + 0x1C)); /* 0x3803BE4B: fld dword ptr [ebp + 0x1c] */ - { double _v = fp_pop(); *(double*)ADDR(esp) = _v; } /* 0x3803BE4E: fstp qword ptr [esp] */ - RECOMP_ICALL(MEM32(0x3804E0AC)); /* 0x3803BE51: call dword ptr [0x3804e0ac] */ - RECOMP_FLAGS_IN(); - _st[0] += (double)*(float*)ADDR(0x3804E8CC); /* 0x3803BE57: fadd dword ptr [0x3804e8cc] */ - _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); - _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); - esp = esp + 8; /* 0x3803BE5D: add esp, 8 */ - _flag_k = FK_ADD; - _st[0] -= (double)*(float*)ADDR(ebp + 0x1C); /* 0x3803BE60: fsub dword ptr [ebp + 0x1c] */ - goto L_3803BE92; /* 0x3803BE63: jmp 0x3803be92 */ -L_3803BE65: - fp_push(*(float*)&MEM32(0x3804F7A0)); /* 0x3803BE65: fld dword ptr [0x3804f7a0] */ - MEM32(ebp + (-0xC)) = 0xFFFFFFFFu; /* 0x3803BE6B: mov dword ptr [ebp - 0xc], 0xffffffff */ - _st[0] /= (double)*(float*)ADDR(ebp + 0xC); /* 0x3803BE72: fdiv dword ptr [ebp + 0xc] */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + 0xC) = _v; } /* 0x3803BE75: fstp dword ptr [ebp + 0xc] */ - fp_push(*(float*)&MEM32(ebp + 0x1C)); /* 0x3803BE78: fld dword ptr [ebp + 0x1c] */ - { double _v = fp_pop(); *(double*)ADDR(ebp + (-0x24)) = _v; } /* 0x3803BE7B: fstp qword ptr [ebp - 0x24] */ - eax = MEM32(ebp + (-0x20)); /* 0x3803BE7E: mov eax, dword ptr [ebp - 0x20] */ - ecx = MEM32(ebp + (-0x24)); /* 0x3803BE81: mov ecx, dword ptr [ebp - 0x24] */ - PUSH32(esp, eax); /* 0x3803BE84: push eax */ - PUSH32(esp, ecx); /* 0x3803BE85: push ecx */ - RECOMP_ICALL(MEM32(0x3804E0AC)); /* 0x3803BE86: call dword ptr [0x3804e0ac] */ - RECOMP_FLAGS_IN(); - _st[0] = *(double*)ADDR(ebp + (-0x24)) - _st[0]; /* 0x3803BE8C: fsubr qword ptr [ebp - 0x24] */ - _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); - _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); - esp = esp + 8; /* 0x3803BE8F: add esp, 8 */ - _flag_k = FK_ADD; -L_3803BE92: - eax = MEM32(ebp + (-0x10)); /* 0x3803BE92: mov eax, dword ptr [ebp - 0x10] */ - _st[0] *= (double)*(float*)ADDR(ebp + 0xC); /* 0x3803BE95: fmul dword ptr [ebp + 0xc] */ - _flag_a = (uint32_t)(eax); _flag_b = (uint32_t)(esi); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - eax = eax - esi; /* 0x3803BE98: sub eax, esi */ - _flag_k = FK_CMP; - edx = ((int32_t)eax < 0) ? 0xFFFFFFFFu : 0; /* 0x3803BE9A: cdq */ - ecx = eax; /* 0x3803BE9B: mov ecx, eax */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + 0x1C) = _v; } /* 0x3803BE9D: fstp dword ptr [ebp + 0x1c] */ - _flag_a = (uint32_t)((ecx ^ edx)); _flag_b = (uint32_t)((ecx ^ edx)); - ecx = ecx ^ edx; /* 0x3803BEA0: xor ecx, edx */ - _cf = 0; - _flag_k = FK_TEST; - eax = ebx; /* 0x3803BEA2: mov eax, ebx */ - ebx = MEM32(ebp + 0x10); /* 0x3803BEA4: mov ebx, dword ptr [ebp + 0x10] */ - _flag_a = (uint32_t)(eax); _flag_b = (uint32_t)(edi); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - eax = eax - edi; /* 0x3803BEA7: sub eax, edi */ - _flag_k = FK_CMP; - _flag_a = (uint32_t)(ecx); _flag_b = (uint32_t)(edx); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - ecx = ecx - edx; /* 0x3803BEA9: sub ecx, edx */ - _flag_k = FK_CMP; - edx = ((int32_t)eax < 0) ? 0xFFFFFFFFu : 0; /* 0x3803BEAB: cdq */ - _flag_a = (uint32_t)((eax ^ edx)); _flag_b = (uint32_t)((eax ^ edx)); - eax = eax ^ edx; /* 0x3803BEAC: xor eax, edx */ - _cf = 0; - _flag_k = FK_TEST; - _flag_a = (uint32_t)(eax); _flag_b = (uint32_t)(edx); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - eax = eax - edx; /* 0x3803BEAE: sub eax, edx */ - _flag_k = FK_CMP; - _flag_a = (uint32_t)(ecx); _flag_b = (uint32_t)(eax); - _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); - ecx = ecx + eax; /* 0x3803BEB0: add ecx, eax */ - _flag_k = FK_ADD; - MEM32(ebp + (-0x10)) = ecx; /* 0x3803BEB2: mov dword ptr [ebp - 0x10], ecx */ - if (/* add result */ recomp_cond(_flag_k, _flag_a, _flag_b, CC_S)) goto L_3803BFD9; /* 0x3803BEB5: js 0x3803bfd9 */ + { + /* Keep the original float spills: the far X coordinate stays double. */ + double spacing = *(float *)ADDR(ebp - 4); + float start_x = (float)((double)*(float *)ADDR(0x3806F028) / spacing); + float start_z = (float)((double)*(float *)ADDR(0x3806F030) / spacing); + double end_x = (double)*(float *)ADDR(0x3806F008) / spacing; + float end_z = (float)((double)*(float *)ADDR(0x3806F010) / spacing); + edi = (uint32_t)fp_st0_to_i64(start_x, 0, 0x0c7f); + esi = (uint32_t)fp_st0_to_i64(start_z, 0, 0x0c7f); + uint32_t end_ix = (uint32_t)fp_st0_to_i64(end_x, 0, 0x0c7f); + uint32_t end_iz = (uint32_t)fp_st0_to_i64(end_z, 0, 0x0c7f); + + /* Distance to the first cell boundary and each following boundary. */ + double dx = end_x - start_x; + float dz = (float)((double)end_z - start_z); + int negative_x = !(dx >= 0.0), negative_z = !(dz >= 0.0); + float interval_x = (float)((negative_x ? -100.0 : 100.0) / dx); + float interval_z = (float)((negative_z ? -100.0 : 100.0) / dz); + double first_x = negative_x ? (double)start_x - floor(start_x) + : (floor(start_x) + 1.0) - start_x; + double first_z = negative_z ? (double)start_z - floor(start_z) + : (floor(start_z) + 1.0) - start_z; + *(float *)ADDR(ebp - 4) = interval_x; + *(float *)ADDR(ebp + 12) = interval_z; + *(float *)ADDR(ebp + 24) = (float)(first_x * interval_x); + *(float *)ADDR(ebp + 28) = (float)(first_z * interval_z); + MEM32(ebp - 8) = negative_x ? UINT32_MAX : 1; + MEM32(ebp - 12) = negative_z ? UINT32_MAX : 1; + memset(_st + 6, 0, 2 * sizeof(*_st)); + memset(g_st_i64 + 6, 0, 2 * sizeof(*g_st_i64)); + + /* Use wrapping guest arithmetic for the inclusive Manhattan cell count. */ + uint32_t distance_x = end_ix - edi, distance_z = end_iz - esi; + edx = (int32_t)distance_x < 0 ? UINT32_MAX : 0; + eax = (distance_x ^ edx) - edx; + uint32_t sign_z = (int32_t)distance_z < 0 ? UINT32_MAX : 0; + ecx = (distance_z ^ sign_z) - sign_z; + _flag_a = ecx; _flag_b = eax; _flag_k = FK_ADD; + _cf = ecx + eax < ecx; + ecx += eax; + MEM32(ebp - 16) = ecx; + ebx = MEM32(ebp + 16); + if ((int32_t)ecx < 0) goto L_3803BFD9; + } L_3803BEBB: /* test edi, edi */ /* 0x3803BEBB: test edi, edi */ _flag_a = (uint32_t)(edi); _flag_b = (uint32_t)(edi); @@ -2871,34 +2748,26 @@ L_3803BEF4: L_3803BFA1: ecx = MEM32(ebp + (-0x10)); /* 0x3803BFA1: mov ecx, dword ptr [ebp - 0x10] */ L_3803BFA4: - fp_push(*(float*)&MEM32(ebp + 0x18)); /* 0x3803BFA4: fld dword ptr [ebp + 0x18] */ - _fpu_cmp = FPU_CMP(_st[0], (double)*(float*)ADDR(ebp + 0x1C)); /* 0x3803BFA7: fcomp dword ptr [ebp + 0x1c] */ - fp_pop(); - eax = (eax & 0xFFFF0000u) | ((_fpu_cmp >= 0x10000) ? (uint32_t)(_fpu_cmp & 0x4700) : _fpu_cmp == 2 ? 0x4500u : _fpu_cmp < 0 ? 0x0100u : _fpu_cmp == 0 ? 0x4000u : 0x0000u); /* 0x3803BFAA: fnstsw ax */ - /* test HI8(eax), 1 */ /* 0x3803BFAC: test ah, 1 */ - _flag_a = (uint32_t)(HI8(eax)) << 24; _flag_b = (uint32_t)(1) << 24; - _cf = 0; - _flag_k = FK_NARROW(FK_TEST, 24); - if (TEST_Z(_flag_a, _flag_b)) goto L_3803BFC1; /* 0x3803BFAF: je 0x3803bfc1 */ -L_3803BFB1: - fp_push(*(float*)&MEM32(ebp + 0x18)); /* 0x3803BFB1: fld dword ptr [ebp + 0x18] */ - eax = MEM32(ebp + (-0x8)); /* 0x3803BFB4: mov eax, dword ptr [ebp - 8] */ - _st[0] += (double)*(float*)ADDR(ebp + (-0x4)); /* 0x3803BFB7: fadd dword ptr [ebp - 4] */ - _flag_a = (uint32_t)(edi); _flag_b = (uint32_t)(eax); - _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); - edi = edi + eax; /* 0x3803BFBA: add edi, eax */ - _flag_k = FK_ADD; - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + 0x18) = _v; } /* 0x3803BFBC: fstp dword ptr [ebp + 0x18] */ - goto L_3803BFCF; /* 0x3803BFBF: jmp 0x3803bfcf */ -L_3803BFC1: - fp_push(*(float*)&MEM32(ebp + 0x1C)); /* 0x3803BFC1: fld dword ptr [ebp + 0x1c] */ - eax = MEM32(ebp + (-0xC)); /* 0x3803BFC4: mov eax, dword ptr [ebp - 0xc] */ - _st[0] += (double)*(float*)ADDR(ebp + 0xC); /* 0x3803BFC7: fadd dword ptr [ebp + 0xc] */ - _flag_a = (uint32_t)(esi); _flag_b = (uint32_t)(eax); - _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); - esi = esi + eax; /* 0x3803BFCA: add esi, eax */ - _flag_k = FK_ADD; - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + 0x1C) = _v; } /* 0x3803BFCC: fstp dword ptr [ebp + 0x1c] */ + { + /* Equal boundary distances step Z; unordered comparisons step X. */ + float next_x = *(float *)ADDR(ebp + 24); + float next_z = *(float *)ADDR(ebp + 28); + if (!(next_x >= next_z)) { + eax = MEM32(ebp - 8); + _flag_a = edi; _flag_b = eax; + _cf = edi + eax < edi; + edi += eax; + *(float *)ADDR(ebp + 24) = (float)((double)next_x + *(float *)ADDR(ebp - 4)); + } else { + eax = MEM32(ebp - 12); + _flag_a = esi; _flag_b = eax; + _cf = esi + eax < esi; + esi += eax; + *(float *)ADDR(ebp + 28) = (float)((double)next_z + *(float *)ADDR(ebp + 12)); + } + _flag_k = FK_ADD; + _st[7] = 0; g_st_i64[7] = 0; + } L_3803BFCF: _flag_a = (uint32_t)(ecx); _flag_b = (uint32_t)(1); ecx = ecx - 1; /* 0x3803BFCF: dec ecx */ diff --git a/src/tests/terrain_line.c b/src/tests/terrain_line.c @@ -0,0 +1,190 @@ +#include "recomp_types.h" +#include <assert.h> + +void sub_3803B970(void); +void sub_3803C130(void); +void sub_3803C150(void); +static uint32_t visited[5][2]; +static unsigned visits, hit_at; +static float hit_fraction, child_fraction; + +static void width(void) { g_eax = 640; g_esp += 4; } +static void height(void) { g_eax = 512; g_esp += 4; } +static void to_integer(void) +{ + int64_t value = fp_st0_to_i64(g_st[0], g_st_i64[0], 0x0c7f); + fp_pop_impl(g_st, &g_fp_top); + g_eax = (uint32_t)value; + g_edx = (uint32_t)((uint64_t)value >> 32); + g_esp += 4; +} +static void round_down(void) +{ + fp_push_impl(g_st, &g_fp_top, floor(MEMD(g_esp + 4))); + g_esp += 4; +} +static void subtract(void) +{ + uint32_t from = MEM32(g_esp + 4); + for (unsigned i = 0; i < 3; ++i) + *(float *)ADDR(g_ecx + i * 4) = (float)((double)*(float *)ADDR(g_ecx + i * 4) + - *(float *)ADDR(from + i * 4)); + g_eax = g_ecx; + g_esp += 8; +} +static void tile(void) +{ + assert(visits < 5); + visited[visits][0] = MEM32(g_esp + 4); + visited[visits][1] = MEM32(g_esp + 8); + ++visits; + g_eax = visits == hit_at; + if (g_eax) { + uint32_t output = MEM32(g_esp + 12), normal = MEM32(g_esp + 16); + for (unsigned i = 0; i < 3; ++i) { + double start = *(float *)ADDR(0x3806f028 + i * 4); + *(float *)ADDR(output + i * 4) = (float)(start + hit_fraction * + ((double)*(float *)ADDR(0x3806f008 + i * 4) - start)); + *(float *)ADDR(normal + i * 4) = i == 1 ? 1 : 0; + } + } + g_esp += 20; +} +static void child(void) +{ + uint32_t output = MEM32(g_esp + 12), normal = MEM32(g_esp + 16); + for (unsigned i = 0; i < 3; ++i) { + double start = *(float *)ADDR(0x3806f028 + i * 4); + *(float *)ADDR(output + i * 4) = (float)(start + child_fraction * + ((double)*(float *)ADDR(0x3806f008 + i * 4) - start)); + *(float *)ADDR(normal + i * 4) = i == 1 ? -1 : 0; + } + g_eax = 1; + g_esp += 28; +} +recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } +recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } +recomp_func_t recomp_lookup(uint32_t address) +{ + switch (address) { + case 0x38039da0: return width; + case 0x38039db0: return height; + case 0x3804b688: return to_integer; + case 0x500: return round_down; + case 0x504: return subtract; + case 0x508: return child; + case 0x3803c220: return tile; + case 0x3803c130: return sub_3803C130; + case 0x3803c150: return sub_3803C150; + default: fprintf(stderr, "Unexpected call %08x\n", address); abort(); + } +} + +struct result { + uint32_t abi[6], outputs[6], path[5][2]; + unsigned count; + int top; + double live_fpu[3]; + int64_t live_exact[3]; +}; +static void run(struct result *result, const float points[6], float spacing, int nested) +{ + memset((void *)ADDR(0x50000000), 0, 0x20000); + memset((void *)ADDR(0x38060000), 0, 0x10000); + g_ecx = 0x50000000; g_esp = 0x5001ff00; + g_ebp = 123; g_ebx = 234; g_esi = 345; g_edi = 456; + g_eax = 567; g_edx = 678; g_fp_top = 3; + g_flag_k = FK_NONE; g_flag_a = g_flag_b = g_flag_cf = 0; + for (unsigned i = 0; i < 8; ++i) { g_st[i] = i + .25; g_st_i64[i] = INT64_MAX - i; } + MEM32(g_ecx + 4) = 0x50001000; + MEM32(0x5000100c) = 0x50002000; + *(float *)ADDR(0x5000a264) = spacing; + *(float *)ADDR(g_ecx + 16) = 640 * spacing; + *(float *)ADDR(g_ecx + 20) = 512 * spacing; + if (nested) { + MEM32(g_ecx + 12) = 0x5000b000; + MEM32(0x5000b000) = 0x5000b100; + MEM32(0x5000b110) = 0x508; + } + memcpy((void *)ADDR(0x5000c000), points, 24); + MEM32(g_esp + 4) = 0x5000c000; + MEM32(g_esp + 8) = 0x5000c00c; + MEM32(g_esp + 12) = 0x5000d000; + MEM32(g_esp + 16) = 0x5000d00c; + MEM32(g_esp + 20) = 1; + MEM32(g_esp + 24) = 0; + memset((void *)ADDR(0x5000d000), 0xa5, 24); + visits = 0; + + sub_3803B970(); + + memset(result, 0, sizeof(*result)); + uint32_t abi[] = {g_eax & 255, g_esp, g_ebp, g_ebx, g_esi, g_edi}; + memcpy(result->abi, abi, sizeof(abi)); + memcpy(result->outputs, (void *)ADDR(0x5000d000), 24); + result->count = visits; + memcpy(result->path, visited, visits * sizeof(*visited)); + result->top = g_fp_top; + memcpy(result->live_fpu, g_st, sizeof(result->live_fpu)); + memcpy(result->live_exact, g_st_i64, sizeof(result->live_exact)); +} + +int main(void) +{ + assert(guest_map("line fixtures", 0x50000000, 0x20000)); + assert(guest_map("line constants", 0x38040000, 0x10000)); + assert(guest_map("line globals", 0x38060000, 0x10000)); + MEM32(0x3804e0ac) = 0x500; MEM32(0x3804e4f4) = 0x504; + *(float *)ADDR(0x3804e8e4) = 0; + *(float *)ADDR(0x3804e8cc) = 1; + *(float *)ADDR(0x3804eabc) = 100; + *(float *)ADDR(0x3804f7a0) = -100; + *(float *)ADDR(0x3804ea94) = .1f; + /* Stationary, forward/reverse ties, clipping, tile/child hits, and float boundaries. */ + const float points[][6] = { + {.5f,10,.5f, .5f,-5,.5f}, + {.5f,10,.5f, 2.5f,-5,2.5f}, + {2.5f,10,2.5f, .5f,-5,.5f}, + {-1,10,.5f, 2.5f,-5,.5f}, + {.5f,10,.5f, 2.5f,-5,2.5f}, + {.5f,10,.5f, 2.5f,-5,2.5f}, + {331.499969f,10,331.500031f,334.099976f,-5,334.100037f}, + }; + const unsigned counts[] = {1, 5, 5, 3, 2, 2, 5}; + const uint32_t paths[7][5][2] = { + {{0,0}}, + {{0,0}, {0,1}, {1,1}, {1,2}, {2,2}}, + {{2,2}, {2,1}, {1,1}, {1,0}, {0,0}}, + {{0,0}, {1,0}, {2,0}}, + {{0,0}, {0,1}}, + {{0,0}, {0,1}}, + {{254,255}, {255,255}, {255,256}, {256,256}, {256,257}}, + }; + const float tile_hit[] = {1.5f, 2.5f, 1.5f, 0, 1, 0}; + const float child_hit[] = {1, 6.25f, 1, 0, -1, 0}; + const uint32_t no_hit[] = {0xbf800000, 0xbf800000, 0xbf800000, + 0xa5a5a5a5, 0xa5a5a5a5, 0xa5a5a5a5}; + for (unsigned i = 0; i < 7; ++i) { + struct result result; + hit_at = i == 4 || i == 5 ? 2 : 0; + hit_fraction = .5f; child_fraction = .25f; + + run(&result, points[i], i == 6 ? 1.3f : 1, i == 5); + + assert(result.abi[0] == (i == 4 || i == 5)); + assert(result.count == counts[i]); + assert(!memcmp(result.path, paths[i], counts[i] * sizeof(paths[i][0]))); + assert(!memcmp(result.outputs, i == 4 ? (const void *)tile_hit : + i == 5 ? (const void *)child_hit : (const void *)no_hit, 24)); + assert(result.abi[1] == 0x5001ff1c); + assert(result.abi[2] == 123 && result.abi[3] == 234); + assert(result.abi[4] == 345 && result.abi[5] == 456); + assert(result.top == 3); + for (unsigned j = 0; j < 3; ++j) { + assert(result.live_fpu[j] == j + .25); + assert(result.live_exact[j] == INT64_MAX - j); + } + } + puts("terrain traversal paths, clipping, hit selection and ABI passed"); + return 0; +}