Skip to content

Commit 16cecc6

Browse files
fix(nfs): exchange race X/Y controls and reduce audio callback buffering
1 parent 42daf4d commit 16cecc6

7 files changed

Lines changed: 150 additions & 3 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,24 @@ the BC-to-ASTC path). The EA dialogs now show
127127
text, logos and borders. This verifies that particular data-loss repair,
128128
not the complete 3D picture or gameplay: the bounded A-input reproduction
129129
ends on the user-agreement page. No shader chain or game resources changed.
130+
131+
132+
### Driving-only X/Y and audio underruns (2026-11-01)
133+
134+
The owner confirmed playable graphics but intermittent audio and requested X/Y
135+
functions to exchange only during a race. `nfs_driving_input.h` checks the exact
136+
`799d1061182c1302e08e706b84ce76e5` main image and five instructions before reading
137+
its control groups: CAR enabled, MENU and DEBUG disabled. The host swaps only
138+
X/Y bits after input sources merge. The screen layout and base keyboard or
139+
controller mappings stay unchanged. Unreadable or unknown state keeps native
140+
buttons. The production HID path passes 411 checks including menu transitions,
141+
all face-button combinations and touch/keyboard, controller and script inputs.
142+
143+
The original Air log contains real AudioUnit underruns: some roughly 12-second
144+
intervals inserted 1.9–2.94 seconds of silence. This title now requests a 2.5 ms
145+
session buffer instead of 5 ms, to reduce callbacks consuming several small
146+
guest blocks at once. Actual hardware rate and granted buffer duration remain
147+
logged. Other games keep 5 ms. This is a measured-failure-driven candidate,
148+
not a claim that every source of audio starvation is fixed. Compare new
149+
underrun/silence counters on the owner's next normal play session. The queue
150+
ownership, sample conversion and render backend are unchanged.

‎dm/host/hle_hid.c‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <time.h>
1616
#include <math.h>
1717
#include "motion_fusion.h"
18+
#include "nfs_driving_input.h"
19+
#include <mach/mach.h>
1820

1921
enum { NPAD_NO1 = 0, NPAD_HANDHELD = 0x20 };
2022
enum { STYLE_FULLKEY = 1u << 0, STYLE_HANDHELD = 1u << 1, STYLE_JOYDUAL = 1u << 2, STYLE_JOYLEFT = 1u << 3, STYLE_JOYRIGHT = 1u << 4 };
@@ -288,6 +290,20 @@ void dm_pad_players(uint32_t mask) {
288290
note_merged();
289291
}
290292

293+
// The policy is applied after all input paths and edge latches merge. The
294+
// frontends retain their printed letters, hit regions and keyboard bindings.
295+
static int nfs_input_read(uintptr_t address, void *out, size_t size) {
296+
vm_size_t got=0;
297+
return address>=0x1000 && address<=UINTPTR_MAX-size &&
298+
vm_read_overwrite(mach_task_self(),address,size,(vm_address_t)out,&got)==KERN_SUCCESS && got==size;
299+
}
300+
static uint64_t race_buttons(uint64_t buttons) {
301+
if (!(buttons & (DM_BTN_X|DM_BTN_Y))) return buttons;
302+
for (int i=0;i<g_nmod;i++) if (!strcmp(g_mod[i].name,"main"))
303+
return dm_nfs_driving_buttons(buttons, dm_nfs_driving((uintptr_t)g_mod[i].base,g_mod[i].build_id,nfs_input_read));
304+
return buttons;
305+
}
306+
291307
static void fill(NpadState *s, uint32_t id, uint32_t style) {
292308
int receipt = receipt_enabled();
293309
uint64_t read_start = receipt ? now_ns() : 0;
@@ -319,7 +335,7 @@ static void fill(NpadState *s, uint32_t id, uint32_t style) {
319335
if (s->ry > STICK_THRESH) b |= 1u << 21;
320336
if (s->rx > STICK_THRESH) b |= 1u << 22;
321337
if (s->ry < -STICK_THRESH) b |= 1u << 23;
322-
s->buttons = b;
338+
s->buttons = pl ? b : race_buttons(b);
323339
s->attributes = ATTR_CONNECTED | (id == NPAD_HANDHELD ? ATTR_WIRED | ATTR_LEFT_CONN | ATTR_LEFT_WIRED | ATTR_RIGHT_CONN | ATTR_RIGHT_WIRED : ATTR_LEFT_CONN | ATTR_RIGHT_CONN);
324340
if (receipt) receipt_note(s, id, style, read_start);
325341
}

‎dm/host/nfs_driving_input.h‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// NFS Hot Pursuit Remastered 1.0.3: read-only exact-build control groups.
2+
// GameLogicState::IsControlGroupEnabled (main+1c4a30) reads the four bytes
3+
// at owner+141448: NONE, CAR, MENU, DEBUG (enum at main+d84e78).
4+
// Its registration (1c0c38..48) obtains the wrapper from globals+13ae0;
5+
// GOT df6918 points at globals ed95f0. The wrapper's +10 holds the owner.
6+
// Fail closed during boot, loading, menus, photo mode, and unreadable objects.
7+
#pragma once
8+
#include <stdint.h>
9+
#include <string.h>
10+
#include "dm_input.h"
11+
typedef int (*dm_nfs_read)(uintptr_t, void *, size_t);
12+
static inline int dm_nfs_driving(uintptr_t base, const char *build, dm_nfs_read read) {
13+
if (!base || base>UINTPTR_MAX-0xeed0d8 || !build || strcmp(build, "799d1061182c1302e08e706b84ce76e5")) return 0;
14+
static const struct { uintptr_t off; uint32_t word; } code[] = {
15+
{0x1c0ae0, 0xd00061b7}, {0x1c0ae4, 0xf9448ef7},
16+
{0x1c4ab4, 0x52828909}, {0x1c4ab8, 0x72a00289}, {0x1c4abc, 0x38696908}
17+
};
18+
for (unsigned i=0;i<sizeof code/sizeof *code;i++) {
19+
uint32_t word;
20+
if (!read(base+code[i].off,&word,sizeof word) || word!=code[i].word) return 0;
21+
}
22+
uintptr_t wrapper, owner; uint8_t groups[4];
23+
if (!read(base+0xeed0d0,&wrapper,sizeof wrapper) || wrapper<0x1000 || wrapper>UINTPTR_MAX-0x10 ||
24+
!read(wrapper+0x10,&owner,sizeof owner) || owner<0x1000 || owner>UINTPTR_MAX-0x14144c ||
25+
!read(owner+0x141448,groups,sizeof groups)) return 0;
26+
for (unsigned i=0;i<4;i++) if (groups[i]>1) return 0;
27+
return groups[1]==1 && groups[2]==0 && groups[3]==0;
28+
}
29+
static inline uint64_t dm_nfs_driving_buttons(uint64_t buttons, int driving) {
30+
if (!driving) return buttons;
31+
return (buttons & ~(uint64_t)(DM_BTN_X|DM_BTN_Y)) |
32+
((buttons & DM_BTN_X)<<1) | ((buttons & DM_BTN_Y)>>1);
33+
}

‎dm/host/tests/t_hid.c‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ static int hid_usleep(useconds_t usec);
1212
#undef usleep
1313
#include "../pad_slots.h"
1414

15+
module_t *g_mod; int g_nmod; // no guest module in the generic HID regressions
1516
static int checks, fails, g_mode = 1, g_signals;
1617
static void *g_signalled;
1718
static time_t rumble_second = 1;
@@ -359,6 +360,43 @@ static void t_motion_hid(void) {
359360
dm_pad_players(1); g_phone_mode = -1;
360361
}
361362

363+
static void t_nfs_race_buttons(void) {
364+
static module_t m;
365+
size_t size=0x1800000;
366+
uint8_t *base=calloc(1,size), *wrapper=calloc(1,0x20), *owner=calloc(1,0x14144c);
367+
CHECK(base && wrapper && owner,"allocate isolated NFS control state");
368+
if (!base || !wrapper || !owner) abort();
369+
strcpy(m.name,"main"); strcpy(m.build_id,"799d1061182c1302e08e706b84ce76e5");m.base=base;m.size=size;g_mod=&m;g_nmod=1;
370+
*(uint32_t *)(base+0x1c0ae0)=0xd00061b7; *(uint32_t *)(base+0x1c0ae4)=0xf9448ef7;
371+
*(uint32_t *)(base+0x1c4ab4)=0x52828909; *(uint32_t *)(base+0x1c4ab8)=0x72a00289; *(uint32_t *)(base+0x1c4abc)=0x38696908;
372+
*(uintptr_t *)(base+0xeed0d0)=(uintptr_t)wrapper;*(uintptr_t *)(wrapper+0x10)=(uintptr_t)owner;
373+
uint8_t *groups=owner+0x141448;
374+
g_mode=1;ids((uint32_t[]){0},1);dm_pad_players(1);
375+
// Touch/keyboard, controller and script all pass through the real fill().
376+
for (int source=0;source<3;source++) for (int state=0;state<6;state++) {
377+
memset(groups,0,4);groups[1]=state!=0;groups[2]=state==2;groups[3]=state==3;
378+
if(state==4)groups[1]=2;
379+
if(state==5)m.build_id[0]='0';
380+
int driving=state==1;
381+
for (unsigned bits=0;bits<16;bits++) {
382+
dm_input_set(source==0?bits:0,0,0,0,0); dm_pad_set(source==1?bits:0,0,0,0,0);script_set(source==2?bits:0,0,0,0,0);
383+
(void)buttons_of(0,STYLE_FULLKEY); // drain the existing edge latch
384+
uint64_t expected=bits;
385+
if(driving)expected=(bits&~12u)|((bits&4u)<<1)|((bits&8u)>>1);
386+
CHECK(buttons_of(0,STYLE_FULLKEY)==expected,"NFS driving-only X/Y; A/B and menus unchanged across input sources");
387+
}
388+
m.build_id[0]='7';
389+
}
390+
groups[1]=1; groups[2]=groups[3]=0;
391+
*(uintptr_t *)(wrapper+0x10)=1;CHECK(race_buttons(DM_BTN_X)==DM_BTN_X,"unreadable owner fails closed");
392+
*(uintptr_t *)(wrapper+0x10)=(uintptr_t)owner;
393+
*(uintptr_t *)(base+0xeed0d0)=0;CHECK(race_buttons(DM_BTN_X)==DM_BTN_X,"loading with no wrapper fails closed");
394+
*(uintptr_t *)(base+0xeed0d0)=(uintptr_t)wrapper;
395+
*(uint32_t *)(base+0x1c4ab4)^=1;CHECK(race_buttons(DM_BTN_X)==DM_BTN_X,"different instruction fails closed");
396+
g_nmod=0;g_mod=NULL;dm_input_set(0,0,0,0,0);dm_pad_set(0,0,0,0,0);script_set(0,0,0,0,0);
397+
free(base);free(wrapper);free(owner);
398+
}
399+
362400
int main(void) {
363401
CHECK(setenv("DM_RUMBLE_LOG", "1", 1) == 0, "enable rumble diagnostics");
364402
t_slots();
@@ -369,6 +407,7 @@ int main(void) {
369407
t_motion_threads();
370408
t_motion_live();
371409
t_motion_hid();
410+
t_nfs_race_buttons();
372411
printf("hid players: %d checks, %d failed\n", checks, fails);
373412
return fails != 0;
374413
}

‎dm/ios/Sources/DmAudioBuffer.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
#include <strings.h>
3+
// NFS queues a small number of FMOD blocks. At 44.1-kHz hardware the former
4+
// 5-ms request became 278/279 guest frames per callback. Ask for <=128 device
5+
// frames so a callback need not exhaust multiple small guest blocks at once.
6+
// The actual route may choose another duration; always log the accepted value.
7+
static inline double dm_audio_buffer_seconds(const char *title) {
8+
return title && !strcasecmp(title,"010029B0118E8000") ? 0.0025 : 0.005;
9+
}

‎dm/ios/Sources/front_ios.m‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "dm.h"
1212
#include "haptics_apple.h"
1313
#include "DmAudioRetry.h"
14+
#include "DmAudioBuffer.h"
1415
#include "DmDisplayRefresh.h"
1516
#include <stdatomic.h>
1617
static _Atomic int g_ios_screen_hz=60;
@@ -423,7 +424,13 @@ void front_ios_audio_session(void) {
423424
e = nil;
424425
if (![s setPreferredSampleRate:48000 error:&e] && report) LOG("[RED] audio session preferred sample rate failed: %s", e.localizedDescription.UTF8String);
425426
e = nil;
426-
if (![s setPreferredIOBufferDuration:0.005 error:&e] && report) LOG("[RED] audio session I/O buffer duration could not be set: %s", e.localizedDescription.UTF8String);
427+
#ifdef DM_MULTI_GAME
428+
const char *audio_title = getenv("DM_LIBRARY_GAME_ID");
429+
#else
430+
const char *audio_title = DM_GAME_ID;
431+
#endif
432+
double requested_buffer = dm_audio_buffer_seconds(audio_title);
433+
if (![s setPreferredIOBufferDuration:requested_buffer error:&e] && report) LOG("[RED] audio session I/O buffer duration could not be set: %s", e.localizedDescription.UTF8String);
427434
e = nil;
428435
g_session_off = ![s setActive:YES error:&e];
429436
if (g_session_off) {
@@ -439,7 +446,7 @@ void front_ios_audio_session(void) {
439446
}
440447
front_ios_audio_apply();
441448
if (report) {
442-
LOG("audio session: sample rate %.0f Hz, I/O buffer %.2f ms (asked for 5 ms)", s.sampleRate, s.IOBufferDuration * 1000);
449+
LOG("audio session: sample rate %.0f Hz, I/O buffer %.2f ms (asked for %.2f ms)", s.sampleRate, s.IOBufferDuration * 1000, requested_buffer * 1000);
443450
front_ios_audio_route_log(g_session_off ? "activation failed" : "activated");
444451
}
445452
g_audio_activation_logged = 1;

‎dm/ios/tests/test_audio_buffer.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from pathlib import Path
2+
import subprocess,tempfile,unittest
3+
ROOT=Path(__file__).resolve().parents[3]
4+
class AudioBuffer(unittest.TestCase):
5+
def test_title_specific_request(self):
6+
code="""
7+
#include <assert.h>
8+
#include "DmAudioBuffer.h"
9+
int main(void){
10+
assert(dm_audio_buffer_seconds(0)==0.005);
11+
assert(dm_audio_buffer_seconds("")==0.005);
12+
assert(dm_audio_buffer_seconds("01001F5010DFA000")==0.005);
13+
assert(dm_audio_buffer_seconds("010029b0118e8000")==0.0025);
14+
assert(dm_audio_buffer_seconds("010029B0118E8000")==0.0025);
15+
assert(dm_audio_buffer_seconds("010029B0118E8000bad")==0.005);
16+
return 0;}
17+
"""
18+
with tempfile.TemporaryDirectory() as td:
19+
p=Path(td)/'policy.c';p.write_text(code);out=Path(td)/'policy'
20+
subprocess.run(['xcrun','clang','-Wall','-Werror','-I',str(ROOT/'dm/ios/Sources'),str(p),'-o',str(out)],check=True)
21+
subprocess.run([str(out)],check=True)
22+
if __name__=='__main__':unittest.main()

0 commit comments

Comments
 (0)