Skip to content

Commit d59fd00

Browse files
committed
Keep NFS elapsed game time consistent under unlimited library pacing
1 parent ccf1d14 commit d59fd00

8 files changed

Lines changed: 67 additions & 15 deletions

File tree

‎dm/games/010029B0118E8000/README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ NVN pacing rate and its display-link mode, before Metal initialization. An unset
121121
120. This is a target, not measured performance: actual gameplay speed,
122122
stability and frame rate at 120 still need the owner's testing.
123123

124+
With TonTon's `DM_FRAME_LIMIT=unlimited` library preference, this same exact-build
125+
adapter now writes one measured active-guest-clock duration to the timer, tolerance
126+
and both 2D/3D consumers, instead of assuming every rendered frame lasts 1/120 s.
127+
The verified busy-wait repair remains required. Eleven production timing cases,
128+
including 400 changing-rate steps at 120/60/40/20 Hz, pass under ASan/UBSan.
129+
The summed simulation time agrees with measured active time in the test; real
130+
device driving speed and suspension behavior remain pending owner testing.
131+
Explicit fixed targets retain their previous behavior with the original-game
132+
library preference. This adapter does not change the race-only drift mapping.
133+
124134
Candidate `20261029.9` passes a full minimum-16.1 host build and legacy resource
125135
audit. Its packaged fixup marker, five added libraries/reflection files,
126136
30/60/120 launcher choices and startup timing entry point were checked. Ten

‎dm/host/hle_fpslock.c‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ static int rd(uint64_t a, void *out, size_t n) { vm_size_t got = 0; return a >=
6161
// Writing just its 60-fps flag or changing display Hz leaves the engine cap.
6262
// Match FPSLocker's exact-build configuration; never apply these addresses to
6363
// another build, or without the rewriter's asserted busy-wait repair.
64-
static int nfs_init, nfs_target, nfs_logged; static uint8_t *nfs_base;
64+
static int nfs_init, nfs_target, nfs_logged, nfs_unlimited; static uint8_t *nfs_base;
65+
static uint64_t nfs_last_guest_ns;
6566
void dm_nfs_timing_init(void) {
6667
int *initialized = &nfs_init;
6768
if (*initialized) return;
@@ -79,10 +80,10 @@ void dm_nfs_timing_init(void) {
7980
LOG("[RED] DM_NFS_FPS must be 0, 30, 60 or 120; using original 30"); target = 30;
8081
}
8182
if (!target) target = 30;
82-
nfs_base=base; nfs_target=target;
83+
nfs_base=base; nfs_target=target; nfs_unlimited=dm_frame_limit_unlimited();
8384
// Configure before the game initializes Metal/the display link. A 120-Hz
8485
// screen request alone leaves NVN's independent default 60-Hz pacer active.
85-
char hz[16]; snprintf(hz,sizeof hz,"%d",target);
86+
char hz[16]; snprintf(hz,sizeof hz,"%d",nfs_unlimited?0:target);
8687
setenv("DM_PRESENT_HZ",hz,1); setenv("DM_PRESENT_INTERVAL","display",1); setenv("DM_PRESENT_LINK","2",1);
8788
}
8889
static void nfs_fps_tick(void) {
@@ -94,15 +95,29 @@ static void nfs_fps_tick(void) {
9495
!rd((uintptr_t)base + 0xeffc60, &step, 8) || !step) return;
9596
const uint64_t addresses[] = {(uintptr_t)base + 0xf0043c, timer + 0x30, timer + 0x28, step + 0x30, step + 0x50};
9697
uint32_t old[5], want[5] = {target >= 60, 19200000u / target, 1920000u / target, 0, 0};
97-
float seconds = 1.0f / target; memcpy(&want[3], &seconds, 4); want[4] = want[3];
98+
float seconds = 1.0f / target;
99+
uint64_t now=0;
100+
if(nfs_unlimited) {
101+
now=guest_ns();
102+
uint64_t elapsed=nfs_last_guest_ns && now>nfs_last_guest_ns ? now-nfs_last_guest_ns : 1000000000ull/(unsigned)target;
103+
// The guest clock excludes host freeze/suspend. Both timing consumers
104+
// receive the same real duration, independent of frame/display rate.
105+
// A representable Switch tick interval is required before any write.
106+
__uint128_t ticks=(__uint128_t)elapsed*19200000u/1000000000u;
107+
if(!ticks || ticks>UINT32_MAX) return;
108+
want[0]=1;want[1]=(uint32_t)ticks;want[2]=(uint32_t)(ticks/10);
109+
seconds=(float)((double)elapsed/1e9);
110+
}
111+
memcpy(&want[3], &seconds, 4); want[4] = want[3];
98112
// Validate every destination before changing any field. Null/uninitialized
99113
// managers occur during loading; do not dereference or partially update them.
100114
for (int i = 0; i < 5; i++) if (!rd(addresses[i], &old[i], 4)) return;
101115
if (old[0] > 1) return;
116+
if(nfs_unlimited) nfs_last_guest_ns=now;
102117
for (int i = 0; i < 5; i++) if (old[i] != want[i]) {
103118
uint32_t *p = (uint32_t *)(uintptr_t)addresses[i]; dm_guest_write_prepare(p, 4); __atomic_store_n(p, want[i], __ATOMIC_RELAXED);
104119
}
105-
if (!nfs_logged) { nfs_logged=1; LOG("main: NFS timer, tolerance and 2D/3D frame targets = %d fps (DM_NFS_FPS; actual performance unverified)", target); }
120+
if (!nfs_logged) { nfs_logged=1; LOG("main: NFS timing %s, nominal %d fps; 2D/3D steps agree (actual performance unverified)", nfs_unlimited?"unlimited, real elapsed clock":"fixed target", target); }
106121
}
107122

108123
// NieR 1.0.3 exposes its preset minimum but ships fixed mode-specific fallbacks.

‎dm/host/tests/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ $(B)/t_audio_spsc_tsan: t_audio_spsc.c ../hle_audio.c ../hle_bigstack.c ../hle.h
196196
run: $(B)/t_fpslock
197197
$(B)/t_fpslock: t_fpslock.c ../hle_fpslock.c ../hle_witcher_timing.inc ../frame_limit.h ../hle.h ../dm.h $(GAME_H) | $(B)
198198
$(CLANG) -O1 -g -Wno-unused-function $(GINC) -o $@ t_fpslock.c
199-
$(B)/t_nfs_fpslock: t_nfs_fpslock.c ../hle_fpslock.c ../hle.h ../dm.h $(GAME_H) | $(B)
199+
$(B)/t_nfs_fpslock: t_nfs_fpslock.c ../hle_fpslock.c ../hle_witcher_timing.inc ../frame_limit.h ../hle.h ../dm.h $(GAME_H) | $(B)
200200
$(CLANG) -O1 -g -Wall -Werror -Wno-unused-function $(GINC) -o $@ t_nfs_fpslock.c
201201
.PHONY: nfs-fpslock
202202
nfs-fpslock: $(B)/t_nfs_fpslock

‎dm/host/tests/t_fpslock.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
static module_t test_modules[1];
1010
module_t *g_mod = test_modules;
1111
int g_nmod;
12+
static uint64_t test_guest_time=1000000000ull;
13+
uint64_t guest_ns(void) { return test_guest_time; }
1214
static int writes;
1315
void dm_guest_write_prepare(void *p, size_t n) { (void)p; assert(n == 4 || n == 8); writes++; }
1416

‎dm/host/tests/t_nfs_fpslock.c‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <sys/wait.h>
77
#include <unistd.h>
88
static module_t modules[1]; module_t *g_mod = modules; int g_nmod;
9+
static uint64_t test_guest_time=1000000000ull;
10+
uint64_t guest_ns(void) { return test_guest_time; }
911
static int writes;
1012
void dm_guest_write_prepare(void *p, size_t n) { (void)p; assert(n == 4); writes++; }
1113
static void check(int mode) {
@@ -17,13 +19,27 @@ static void check(int mode) {
1719
*(uintptr_t *)(base+0xed96a8)=(uintptr_t)timer;*(uintptr_t *)(base+0xeffc60)=(uintptr_t)step;
1820
int target=mode==1?60:mode==2||mode==3||mode==9?30:120;
1921
setenv("DM_NFS_FPS",mode==1?"60":mode==2?"30":mode==3?"0":mode==9?"invalid":"120",1);
22+
if(mode==10)setenv("DM_FRAME_LIMIT","unlimited",1);
2023
if(mode==4)modules[0].build_id[0]='0';
2124
if(mode==6)*(uintptr_t *)(base+0xed96a8)=0;
2225
if(mode==7)*(uintptr_t *)(base+0xeffc60)=1;
2326
if(mode==8)*(uint32_t *)(base+0xf0043c)=7;
2427
dm_fps_lock_tick();
25-
if(mode!=4&&mode!=5){assert(atoi(getenv("DM_PRESENT_HZ"))==target);assert(!strcmp(getenv("DM_PRESENT_INTERVAL"),"display"));assert(!strcmp(getenv("DM_PRESENT_LINK"),"2"));}
26-
if(mode>=4&&mode<=8){assert(!writes); if(mode==6){*(uintptr_t *)(base+0xed96a8)=(uintptr_t)timer;dm_fps_lock_tick();assert(writes==5);}}
28+
if(mode!=4&&mode!=5){assert(atoi(getenv("DM_PRESENT_HZ"))==(mode==10?0:target));assert(!strcmp(getenv("DM_PRESENT_INTERVAL"),"display"));assert(!strcmp(getenv("DM_PRESENT_LINK"),"2"));}
29+
if(mode==10) {
30+
const uint64_t gaps[]={8333333,16666667,25000000,50000000};
31+
double summed=0,wall=0;
32+
for(int round=0;round<100;round++)for(unsigned i=0;i<4;i++) {
33+
test_guest_time+=gaps[i];wall+=(double)gaps[i]/1e9;dm_fps_lock_tick();
34+
float dt=*(float *)(step+0x30);summed+=dt;
35+
assert(fabs(dt-(double)gaps[i]/1e9)<1e-8);
36+
assert(dt==*(float *)(step+0x50));
37+
assert(*(uint32_t *)(timer+0x30)==(__uint128_t)gaps[i]*19200000u/1000000000u);
38+
assert(*(uint32_t *)(timer+0x28)==*(uint32_t *)(timer+0x30)/10);
39+
}
40+
assert(fabs(summed-wall)<1e-5);assert(*(uint32_t *)(base+0xf0043c)==1);
41+
}
42+
else if(mode>=4&&mode<=8){assert(!writes); if(mode==6){*(uintptr_t *)(base+0xed96a8)=(uintptr_t)timer;dm_fps_lock_tick();assert(writes==5);}}
2743
else {
2844
assert(*(uint32_t *)(base+0xf0043c)==(unsigned)(target>=60));
2945
assert(*(uint32_t *)(timer+0x30)==19200000u/target);
@@ -35,4 +51,4 @@ static void check(int mode) {
3551
}
3652
munmap(base,size);
3753
}
38-
int main(void){for(int i=0;i<10;i++){pid_t p=fork();assert(p>=0);if(!p){check(i);_exit(0);}int status;assert(waitpid(p,&status,0)==p);assert(WIFEXITED(status)&&!WEXITSTATUS(status));}puts("NFS FPS: 10 timing-group/build/instruction/null/unreadable/initialization cases passed");}
54+
int main(void){for(int i=0;i<11;i++){pid_t p=fork();assert(p>=0);if(!p){check(i);_exit(0);}int status;assert(waitpid(p,&status,0)==p);assert(WIFEXITED(status)&&!WEXITSTATUS(status));}puts("NFS FPS: 11 timing-group/elapsed-clock/build/instruction/null/unreadable/initialization cases passed");}

‎docs/agents/game-patches.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,8 @@ Every patch bound tighter than it could be: the open gaps, then the blocked, kep
404404
- host [hle_fpslock.c:44](../../dm/host/hle_fpslock.c#L44): engine frame limit set to fps_target: Criterion frame ticks / 10
405405
- host [hle_fpslock.c:45](../../dm/host/hle_fpslock.c#L45): engine frame limit set to fps_target: Criterion 2D game step
406406
- host [hle_fpslock.c:46](../../dm/host/hle_fpslock.c#L46): engine frame limit set to fps_target: Criterion 3D game step
407-
- host [hle_fpslock.c:71](../../dm/host/hle_fpslock.c#L71): build check
408-
- host [hle_fpslock.c:255](../../dm/host/hle_fpslock.c#L255): build check
407+
- host [hle_fpslock.c:72](../../dm/host/hle_fpslock.c#L72): build check
408+
- host [hle_fpslock.c:270](../../dm/host/hle_fpslock.c#L270): build check
409409
- host [nfs_driving_input.h:13](../../dm/host/nfs_driving_input.h#L13): build check
410410

411411
### NieR:Automata The End of YoRHa Edition (010056B015FE8000)
@@ -425,7 +425,7 @@ Every patch bound tighter than it could be: the open gaps, then the blocked, kep
425425
- cut `+0x9bf4d4`: dynamic resolution: optimal frame cost 32.01 -> 16.005 ms at 60 fps (second load)
426426
- cut `+0x9bf4f8`: dynamic resolution: good frame cost 30 -> 15 ms at 60 fps
427427
- launcher capabilities: DYNAMIC_RESOLUTION
428-
- host [hle_fpslock.c:118](../../dm/host/hle_fpslock.c#L118): frame interval and dynamic-resolution floor
428+
- host [hle_fpslock.c:133](../../dm/host/hle_fpslock.c#L133): frame interval and dynamic-resolution floor
429429

430430
### NieR:Automata The End of YoRHa Edition (0100B8E016F76000)
431431

@@ -444,7 +444,7 @@ Every patch bound tighter than it could be: the open gaps, then the blocked, kep
444444
- cut `+0x9c08e8`: dynamic resolution lower bound follows the selected preset
445445
- cut `+0x9c08f4`: dynamic resolution lower bound follows the selected preset
446446
- launcher capabilities: DYNAMIC_RESOLUTION
447-
- host [hle_fpslock.c:119](../../dm/host/hle_fpslock.c#L119): frame interval and dynamic-resolution floor
447+
- host [hle_fpslock.c:134](../../dm/host/hle_fpslock.c#L134): frame interval and dynamic-resolution floor
448448

449449
### Paper Mario: The Thousand-Year Door
450450

‎docs/fork-status.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ Start with the [build and installation guide](ios16-ios17.md).
1111

1212
## Upstream integration and resource compatibility
1313

14-
Upstream `cde88f62068e3d879b083b1a86a7fd78610c7b7b` was fetched and fully merged
14+
Upstream `372a8d8a0404bd0d0bdc9928a674b0d9c0104b5e` was fetched and fully merged
1515
on 2026-11-01, including the previous full integration through `8ea27784`.
16-
The added upstream fix bounds the Need for Speed Bundle 2 resource reader.
16+
The merged upstream changes include the bounded Need for Speed Bundle 2 reader
17+
and independent mmap/mprotect failure fixtures; the fork retains its alias
18+
exposure checks.
1719
This is a merge, not a rebase or a claim that merely fetching updated the fork.
1820
Before each further change batch, fetch and merge the latest upstream in a
1921
clean isolated checkout, resolve conflicts and test both renderer targets.

‎docs/ios16-ios17.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,10 @@ Witcher 3 build `b151a224a429f9a73abb05c5a3de9468` separately clears its engine
130130
`LimitFPS` under this policy, preserving its dynamic-resolution budgets and
131131
simulation/movie timing. Other builds are not written using these addresses.
132132
Actual uncapped performance and the remaining image fixes require device tests.
133+
134+
Need for Speed build `799d1061182c1302e08e706b84ce76e5` uses measured active
135+
frame durations under the unlimited policy, updating both 2D and 3D timing
136+
together. Its exact-build busy-wait repair is still required. Variable-rate
137+
clock tests pass; real-device performance and driving-speed validation remain
138+
pending. Other engines with fixed-step adapters retain their safety limits
139+
until their own elapsed-time integration is verified.

0 commit comments

Comments
 (0)