Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions games/oot/runtime/oot_timestep.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ static void patch_oot_timestep(Module *m) {
0x7200011f,0x52a84e08,0x1e270101,0x9134e268,0xb82a1108,
0x1e220c21,0x1e201828
};
static const uint32_t water_context[] = {
0xb9404e68,0x1e380009,0xf00261db,0xd0026238,0xf946b37b,
0xf9455718,0x1b097d08,0xb90037e8,0x7100051f,0x5400f94b
};
/* Water-surface simulation (main+0x472e0): main+0x475f8 truncates the VFR
ratio with fcvtzs w9, s0 before mul w8, w8, w9 at main+0x4760c (w8 = base
substeps per 60 Hz frame, 6). Above 60 FPS, s0 < 1 truncates to 0 and the
loop at main+0x47618 skips the compute simulation (leaving frozen ripples
in the ping-pong heightfields). Scale in float with round-to-nearest-even
and clamp positive products to >= 1 so >60 FPS keeps ~360 substeps/s while
pause (s0 == 0) stays 0. s16 and w9 are dead here; s0 is preserved;
main+0x47614 immediately overwrites NZCV. */
uint32_t water[] = {
0x1e220110,0x1e200a10,0x1e200208,0x1e202208,
0x1a9fd7e9,0x6b09011f,0x1a89c108,0x14000000
};
/* stp x16,x17; stp q16,q17; adrp/add page; optional pin; otherwise
scvtf elapsed; multiply by 1/31250000; multiply original numerator;
record; restore; branch back. Literal pools stay in the RX text page. */
Expand Down Expand Up @@ -76,14 +92,19 @@ static void patch_oot_timestep(Module *m) {
}
uint64_t page = (m->file_size + 0x3fff) & ~0x3fffULL;
uint64_t stub = (m->text_end + 15) & ~15ULL;
uint64_t water_stub = stub + 384; /* after resolution veneers (+372) and before OOT_VR_CALL_STUB (+512) */
uint64_t rx_end = (m->text_end + 0x3fff) & ~0x3fffULL;
if (m->text_end != 0x3c461d0 || m->file_size < 0x4e94b28 ||
page + 0x4000 > m->size || stub + sizeof input + sizeof logic > rx_end ||
water_stub + sizeof water > rx_end ||
memcmp(m->base + 0x3b4eadc, input_context, sizeof input_context) ||
memcmp(m->base + 0x6e6c98, logic_context, sizeof logic_context))
memcmp(m->base + 0x6e6c98, logic_context, sizeof logic_context) ||
memcmp(m->base + 0x475f4, water_context, sizeof water_context))
runtime_fail("OOT time-step sites differ from verified update 131072");
for (uint64_t k = stub; k < stub + sizeof input + sizeof logic; k++)
if (m->base[k]) runtime_fail("OOT time-step text padding already in use");
for (uint64_t k = water_stub; k < water_stub + sizeof water; k++)
if (m->base[k]) runtime_fail("OOT water time-step text padding already in use");
for (uint64_t k = page; k < page + 40; k++)
if (m->base[k]) runtime_fail("OOT time-step data page already in use");
uint32_t *code[] = { input, logic };
Expand All @@ -100,6 +121,10 @@ static void patch_oot_timestep(Module *m) {
memcpy(m->base + sites[k], &jump, sizeof jump);
stub += lengths[k];
}
water[7] = oot_step_branch(water_stub + 28, 0x47610);
uint32_t water_jump = oot_step_branch(0x4760c, water_stub);
memcpy(m->base + water_stub, water, sizeof water);
memcpy(m->base + 0x4760c, &water_jump, sizeof water_jump);
oot_step_pin = (_Atomic uint32_t *)(void *)(m->base + page);
fprintf(stderr, "[oot] native VFR uses framework elapsed ticks (31250000 Hz); input and logic; state main+0x%llx\n", (unsigned long long)page);
fprintf(stderr, "[oot] native VFR uses framework elapsed ticks (31250000 Hz); input, logic and water substeps; state main+0x%llx\n", (unsigned long long)page);
}
15 changes: 14 additions & 1 deletion games/oot/tests/oot_timestep_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string.h>
extern unsigned char step_state[4096],step_check[64];
extern float call_input(void *,float,float),call_logic(void *,float,float);
extern int32_t call_water(int32_t,float);
/* The veneers cap a frame's duration at 1/30 s (oot_timestep.h): seconds = min(ticks * 3.2e-8, 1/30). */
static float capped(uint64_t ticks){float sec=(float)ticks*3.2e-8f,cap=1.0f/30.0f;return sec<cap?sec:cap;}
static void near(float a,float b){
Expand All @@ -30,5 +31,17 @@ int main(void){
pin=0;memcpy(step_state,&pin,4);memcpy(&before,step_state+24,8);memcpy(&count,step_state+16,8);
for(int k=0;k<20;k++)near(call_logic(fw,60,60),2); /* 50 ms frames, capped at 1/30 s */
double after;uint64_t n;memcpy(&after,step_state+24,8);memcpy(&n,step_state+16,8);assert(fabs(after-before-20.0/30.0)<1e-5);assert(n-count==20);
puts("native ARM64 time-step tests: 16.7/33.3/50/100 ms and a 2.3 s hitch (capped at 1/30 s), 30/60 selectors, startup fallback, live pin, accumulated time and register/flag preservation passed");return 0;
const struct { int32_t base; float ratio; int32_t want; } water_cases[] = {
{6, 0.0f, 0}, {6, -1.0f, 0}, {6, NAN, 0}, {0, 1.0f, 0},
{6, 0.01f, 1}, {6, 0.25f, 2}, {6, 0.5f, 3}, {6, 2.0f/3.0f, 4},
{6, 0.76f, 5}, {6, 0.99f, 6}, {6, 1.0f, 6}, {6, 1.5f, 9}, {6, 2.0f, 12}
};
for(unsigned k=0;k<sizeof water_cases/sizeof *water_cases;k++){
int32_t got=call_water(water_cases[k].base,water_cases[k].ratio);
assert(got==water_cases[k].want);
float s0_after; memcpy(&s0_after,step_check,4);
if(isnan(water_cases[k].ratio)) assert(isnan(s0_after));
else assert(s0_after==water_cases[k].ratio);
}
puts("native ARM64 time-step tests: 16.7/33.3/50/100 ms and a 2.3 s hitch (capped at 1/30 s), 30/60 selectors, startup fallback, live pin, accumulated time, water substeps and register/flag preservation passed");return 0;
}
16 changes: 16 additions & 0 deletions games/oot/tests/oot_timestep_native.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@
elif i==3:asm+=' add x17,x17,_step_state@PAGEOFF\n'
elif i==ret:asm+=' ret\n'
else:asm+=f' .long 0x{v:08x}\n'
wvals=[int(x,16)for x in re.findall(r'0x[0-9a-f]+',re.search(r'uint32_t water\[\] = \{(.*?)\};',h,re.S).group(1))]
asm+='''.globl _call_water
_call_water:
stp x29,x30,[sp,#-16]!
mov w8,w0
bl L_water
adrp x10,_step_check@PAGE
add x10,x10,_step_check@PAGEOFF
str s0,[x10]
mov w0,w8
ldp x29,x30,[sp],#16
ret
L_water:
'''
for i,v in enumerate(wvals):
asm+=' ret\n' if i==7 else f' .long 0x{v:08x}\n'
(D/'step-test.S').write_text(asm)
subprocess.run(['xcrun','clang','-Wall','-Wextra','-Werror',str(source/'oot_timestep_native.c'),str(D/'step-test.S'),'-o',str(D/'step-test')],check=True)
subprocess.run([str(D/'step-test')],check=True)