Skip to content

Commit 3e696c2

Browse files
committed
Merge PR #2400: Ocarina of Time scales its water substeps in float above 60 fps
2 parents dc7d107 + c13d32f commit 3e696c2

3 files changed

Lines changed: 57 additions & 3 deletions

File tree

‎games/oot/runtime/oot_timestep.h‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ static void patch_oot_timestep(Module *m) {
4242
0x7200011f,0x52a84e08,0x1e270101,0x9134e268,0xb82a1108,
4343
0x1e220c21,0x1e201828
4444
};
45+
static const uint32_t water_context[] = {
46+
0xb9404e68,0x1e380009,0xf00261db,0xd0026238,0xf946b37b,
47+
0xf9455718,0x1b097d08,0xb90037e8,0x7100051f,0x5400f94b
48+
};
49+
/* Water-surface simulation (main+0x472e0): main+0x475f8 truncates the VFR
50+
ratio with fcvtzs w9, s0 before mul w8, w8, w9 at main+0x4760c (w8 = base
51+
substeps per 60 Hz frame, 6). Above 60 FPS, s0 < 1 truncates to 0 and the
52+
loop at main+0x47618 skips the compute simulation (leaving frozen ripples
53+
in the ping-pong heightfields). Scale in float with round-to-nearest-even
54+
and clamp positive products to >= 1 so >60 FPS keeps ~360 substeps/s while
55+
pause (s0 == 0) stays 0. s16 and w9 are dead here; s0 is preserved;
56+
main+0x47614 immediately overwrites NZCV. */
57+
uint32_t water[] = {
58+
0x1e220110,0x1e200a10,0x1e200208,0x1e202208,
59+
0x1a9fd7e9,0x6b09011f,0x1a89c108,0x14000000
60+
};
4561
/* stp x16,x17; stp q16,q17; adrp/add page; optional pin; otherwise
4662
scvtf elapsed; multiply by 1/31250000; multiply original numerator;
4763
record; restore; branch back. Literal pools stay in the RX text page. */
@@ -76,14 +92,19 @@ static void patch_oot_timestep(Module *m) {
7692
}
7793
uint64_t page = (m->file_size + 0x3fff) & ~0x3fffULL;
7894
uint64_t stub = (m->text_end + 15) & ~15ULL;
95+
uint64_t water_stub = stub + 384; /* after resolution veneers (+372) and before OOT_VR_CALL_STUB (+512) */
7996
uint64_t rx_end = (m->text_end + 0x3fff) & ~0x3fffULL;
8097
if (m->text_end != 0x3c461d0 || m->file_size < 0x4e94b28 ||
8198
page + 0x4000 > m->size || stub + sizeof input + sizeof logic > rx_end ||
99+
water_stub + sizeof water > rx_end ||
82100
memcmp(m->base + 0x3b4eadc, input_context, sizeof input_context) ||
83-
memcmp(m->base + 0x6e6c98, logic_context, sizeof logic_context))
101+
memcmp(m->base + 0x6e6c98, logic_context, sizeof logic_context) ||
102+
memcmp(m->base + 0x475f4, water_context, sizeof water_context))
84103
runtime_fail("OOT time-step sites differ from verified update 131072");
85104
for (uint64_t k = stub; k < stub + sizeof input + sizeof logic; k++)
86105
if (m->base[k]) runtime_fail("OOT time-step text padding already in use");
106+
for (uint64_t k = water_stub; k < water_stub + sizeof water; k++)
107+
if (m->base[k]) runtime_fail("OOT water time-step text padding already in use");
87108
for (uint64_t k = page; k < page + 40; k++)
88109
if (m->base[k]) runtime_fail("OOT time-step data page already in use");
89110
uint32_t *code[] = { input, logic };
@@ -100,6 +121,10 @@ static void patch_oot_timestep(Module *m) {
100121
memcpy(m->base + sites[k], &jump, sizeof jump);
101122
stub += lengths[k];
102123
}
124+
water[7] = oot_step_branch(water_stub + 28, 0x47610);
125+
uint32_t water_jump = oot_step_branch(0x4760c, water_stub);
126+
memcpy(m->base + water_stub, water, sizeof water);
127+
memcpy(m->base + 0x4760c, &water_jump, sizeof water_jump);
103128
oot_step_pin = (_Atomic uint32_t *)(void *)(m->base + page);
104-
fprintf(stderr, "[oot] native VFR uses framework elapsed ticks (31250000 Hz); input and logic; state main+0x%llx\n", (unsigned long long)page);
129+
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);
105130
}

‎games/oot/tests/oot_timestep_native.c‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <string.h>
66
extern unsigned char step_state[4096],step_check[64];
77
extern float call_input(void *,float,float),call_logic(void *,float,float);
8+
extern int32_t call_water(int32_t,float);
89
/* The veneers cap a frame's duration at 1/30 s (oot_timestep.h): seconds = min(ticks * 3.2e-8, 1/30). */
910
static float capped(uint64_t ticks){float sec=(float)ticks*3.2e-8f,cap=1.0f/30.0f;return sec<cap?sec:cap;}
1011
static void near(float a,float b){
@@ -30,5 +31,17 @@ int main(void){
3031
pin=0;memcpy(step_state,&pin,4);memcpy(&before,step_state+24,8);memcpy(&count,step_state+16,8);
3132
for(int k=0;k<20;k++)near(call_logic(fw,60,60),2); /* 50 ms frames, capped at 1/30 s */
3233
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);
33-
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;
34+
const struct { int32_t base; float ratio; int32_t want; } water_cases[] = {
35+
{6, 0.0f, 0}, {6, -1.0f, 0}, {6, NAN, 0}, {0, 1.0f, 0},
36+
{6, 0.01f, 1}, {6, 0.25f, 2}, {6, 0.5f, 3}, {6, 2.0f/3.0f, 4},
37+
{6, 0.76f, 5}, {6, 0.99f, 6}, {6, 1.0f, 6}, {6, 1.5f, 9}, {6, 2.0f, 12}
38+
};
39+
for(unsigned k=0;k<sizeof water_cases/sizeof *water_cases;k++){
40+
int32_t got=call_water(water_cases[k].base,water_cases[k].ratio);
41+
assert(got==water_cases[k].want);
42+
float s0_after; memcpy(&s0_after,step_check,4);
43+
if(isnan(water_cases[k].ratio)) assert(isnan(s0_after));
44+
else assert(s0_after==water_cases[k].ratio);
45+
}
46+
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;
3447
}

‎games/oot/tests/oot_timestep_native.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@
4141
elif i==3:asm+=' add x17,x17,_step_state@PAGEOFF\n'
4242
elif i==ret:asm+=' ret\n'
4343
else:asm+=f' .long 0x{v:08x}\n'
44+
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))]
45+
asm+='''.globl _call_water
46+
_call_water:
47+
stp x29,x30,[sp,#-16]!
48+
mov w8,w0
49+
bl L_water
50+
adrp x10,_step_check@PAGE
51+
add x10,x10,_step_check@PAGEOFF
52+
str s0,[x10]
53+
mov w0,w8
54+
ldp x29,x30,[sp],#16
55+
ret
56+
L_water:
57+
'''
58+
for i,v in enumerate(wvals):
59+
asm+=' ret\n' if i==7 else f' .long 0x{v:08x}\n'
4460
(D/'step-test.S').write_text(asm)
4561
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)
4662
subprocess.run([str(D/'step-test')],check=True)

0 commit comments

Comments
 (0)