From 897ad1d0ea1e021467fdd8a4bed19b05a4debc6d Mon Sep 17 00:00:00 2001 From: chrg6 Date: Sat, 7 Nov 2026 05:30:15 -0500 Subject: [PATCH] Step Ocarina's game by the display interval, not one 60 Hz frame per present, so it keeps its speed above 60 fps. The time manager **[main+0x4c7e890] holds the frame's base step in 60 Hz frames (+0x1c0), from which each time category's {frames, 1/frames, seconds, 1/seconds} is made (main+0x2c84c). The game leaves it at 1.0 whatever the frame rate, so at 144 fps it ran 2.4 times too fast. Each present now writes the 5-frame average of the display interval there (1/1000 to 1/15 s); the game never writes it itself. Scripted input follows that game clock. NX2NAVI_OCARINA_STEP=0 leaves the step alone; NX2NAVI_TRACE=perf reports game seconds against wall seconds. NX2NAVI_VI_HZ and NX2NAVI_VI_VRR set the refresh rate and VRR support vi reports (60 and off as before), for diagnosis. Co-Authored-By: Claude Opus 5.5 --- runtime/cfg.c | 2 +- runtime/frame_rate.c | 65 ++++++++++++++++++++++++++++++++++++++++++-- runtime/ov_gfx.c | 15 ++++++++-- 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index 108b44fe..21c123bc 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -30,7 +30,7 @@ static int save_ok = 1; // the last cfg_save_to wrote (o // Every name the runtime reads (and the patch sets in generated/), without the prefix. A file key outside this list is // logged as unknown. A new variable goes here too, or its ini key logs a harmless "unknown key" line. static const char *const known[] = { - "CORES", "TM_DISPATCH", "NVN2_CS_TABLE", "SKIP_SHADERS", "MOVIE_DEBAND", "MOVIE_SKIP", "MOVIE_OVERLAY", "MOVIES", "NPLN", "NVN2_PROGRAMS", "NVN_TRAP", "SHADER_DUMP", "SHOT_DIR", "SHOT_EVERY", // Switch 2 port: movie skip (romfs.c), NVN2 proc traps (nvn_procs.c) + "CORES", "TM_DISPATCH", "NVN2_CS_TABLE", "SKIP_SHADERS", "MOVIE_DEBAND", "MOVIE_SKIP", "MOVIE_OVERLAY", "MOVIES", "NPLN", "NVN2_PROGRAMS", "NVN_TRAP", "OCARINA_STEP", "VI_HZ", "VI_VRR", "SHADER_DUMP", "SHOT_DIR", "SHOT_EVERY", // Switch 2 port: movie skip (romfs.c), NVN2 proc traps (nvn_procs.c) "AB", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_OFF", "AUDIO_PREFILL_MS", "BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_LSTICK_DOWN", "BIND_LSTICK_LEFT", "BIND_LSTICK_RIGHT", "BIND_LSTICK_UP", "BIND_MINUS", diff --git a/runtime/frame_rate.c b/runtime/frame_rate.c index 92cfe7ec..312d6dff 100644 --- a/runtime/frame_rate.c +++ b/runtime/frame_rate.c @@ -232,6 +232,50 @@ static int player_pos(float xyz[3]) { return 0; } +// Ocarina 1.1.1's time manager, **[main+0x4c7e890]: +0x1c0 the frame's base step in 60 Hz frames, +0x10 the game +// speed (main+0x2c84c makes each time category's {frames, 1/frames, seconds, 1/seconds} from them and the category's +// factor). Read by the speed lines of a free-running build: the game seconds a frame stepped, or -1. +static double ocarina_step_s(void) { + uint64_t slot = read_ptr(MOD_MAIN_BASE + 0x4c7e890), mgr = slot ? read_ptr(slot) : 0; + float base = 0, speed = 0; + if (!mgr || !read_guest(mgr + 0x1c0, &base, 4) || !read_guest(mgr + 0x10, &speed, 4) || !isfinite(base) || !isfinite(speed)) + return -1; + return base * speed / 60.0; +} + +// Free-running builds (Ocarina): the time manager's base step follows the display interval (5-frame average, 1/1000 to +// 1/15 s, in 60 Hz frames), written at each present. The game left it at one 60 Hz frame whatever the frame rate, so at +// 144 fps it ran 2.4 times too fast. NX2NAVI_OCARINA_STEP=0 leaves it. Counts how often the game itself had changed +// the value since the last write (diagnosis). +static uint64_t ostep_writes, ostep_changed; +static float ostep_last = -1; + +static void ocarina_step_write(double dt) { + static int on = -1; + if (on < 0) { + const char *e = cfg_get("NX2NAVI_OCARINA_STEP"); + on = !(e && !strcmp(e, "0")); + } + if (!on || dt <= 0) return; + uint64_t slot = read_ptr(MOD_MAIN_BASE + 0x4c7e890), mgr = slot ? read_ptr(slot) : 0; + if (!mgr || !plat_mem_readable(mgr + 0x1c0, 4)) return; + _Atomic uint32_t *word = (_Atomic uint32_t *)(uintptr_t)(mgr + 0x1c0); + uint32_t now_bits = atomic_load_explicit(word, memory_order_relaxed); + float now_v; + memcpy(&now_v, &now_bits, 4); + if (ostep_last >= 0 && now_v != ostep_last) ostep_changed++; + if (dt < 1.0 / 1000) dt = 1.0 / 1000; + if (dt > STEP_MAX_S) dt = STEP_MAX_S; + float v = (float)(dt * 60.0); + uint32_t bits; + memcpy(&bits, &v, 4); + atomic_store_explicit(word, bits, memory_order_relaxed); + ostep_last = v; + if (++ostep_writes % 1200 == 0) + rt_log("frame step: Ocarina base step %.3f frames (%.2f ms); the game changed it %llu times in the last 1200 presents", + v, dt * 1000, (unsigned long long)ostep_changed), ostep_changed = 0; +} + static void speed_report(uint64_t now, double step, int interval) { if (!win_t0) { win_t0 = now; @@ -256,7 +300,7 @@ static void speed_report(uint64_t now, double step, int interval) { memcpy(last_pos, p, sizeof p); last_pos_ns = now; } - if (step_word) + if (step_word || win_game > 0) rt_log("fps: %llu frames in %.3f s (%.1f fps), %.3f s game stepped, speed %.3f; interval 1/2/3+ %llu/%llu/%llu%s", (unsigned long long)win_frames, wall, (double)win_frames / wall, win_game, win_game / wall, (unsigned long long)win_hist[1], (unsigned long long)win_hist[2], (unsigned long long)win_hist[3], pos); @@ -311,12 +355,27 @@ void frame_step_present(uint64_t display_ns, int interval) { step_store((float)step); } } + if (frame_free_running() && last_ns && dt > 0 && dt < STEP_RESET_S) { + static double os[STEP_SAMPLES]; + static unsigned on_, ox; + os[ox++ % STEP_SAMPLES] = dt; + if (on_ < STEP_SAMPLES) on_++; + double sum = 0; + for (unsigned i = 0; i < on_; i++) sum += os[i]; + ocarina_step_write(sum / on_); + } last_ns = display_ns; // The game clock of untimed script lines: what the game stepped this frame, the patch page's step with the // real-time step (kept through a reset), whole vsyncs without it. - double game = step_word ? step_now : (interval > 0 ? interval : 1) / 60.0; + double game = step_word ? step_now : ostep_last > 0 ? ostep_last / 60.0 : (interval > 0 ? interval : 1) / 60.0; atomic_store_explicit(&game_us, atomic_load_explicit(&game_us, memory_order_relaxed) + (uint64_t)(game * 1e6 + 0.5), memory_order_relaxed); world_clock(dt, step); - if (rt_trace & RT_TRACE_PERF) speed_report(display_ns, step, interval); + if (rt_trace & RT_TRACE_PERF) { + if (frame_free_running()) { // the game's own step, as its time manager holds it + double s = ocarina_step_s(); + step = s > 0 ? s : 0; + } + speed_report(display_ns, step, interval); + } } diff --git a/runtime/ov_gfx.c b/runtime/ov_gfx.c index 7c6d78d3..9807a15a 100644 --- a/runtime/ov_gfx.c +++ b/runtime/ov_gfx.c @@ -4,6 +4,9 @@ // nv::* record the game's allocator callbacks and graphics work memory (guest addresses) for the NVN layer // nn::vi::* display, layer and native window identities; the vsync event is bound here // nvnBootstrapLoader the end of --boot (rt_boot_event "nvn"); otherwise the synthetic address of an NVN proc (nvn_procs.c) +#include "cfg.h" +#include +#include #include #include "gfx.h" @@ -143,14 +146,20 @@ static void ov_vi_paper_white(Cpu *c) { static void ov_vi_refresh_rate(Cpu *c) { uint32_t *rate = arg_p(c, 0); uint8_t *variable = arg_p(c, 1); - if (rate) *rate = 60; - if (variable) *variable = 0; + const char *hz = cfg_get("NX2NAVI_VI_HZ"), *vrr = cfg_get("NX2NAVI_VI_VRR"); + if (rate) *rate = hz && *hz ? (uint32_t)atoi(hz) : 60; + if (variable) *variable = vrr && !strcmp(vrr, "1"); + static int logged; + if (!logged++) rt_log("vi: QueryDisplayCurrentRefreshRate -> %u Hz, variable %d", rate ? *rate : 0, variable ? *variable : -1); ret_x(c, 0); } // Result QueryDisplayVrrSupported(bool *supported, const Display *) static void ov_vi_vrr_supported(Cpu *c) { uint8_t *out = arg_p(c, 0); - if (out) *out = 0; + const char *vrr = cfg_get("NX2NAVI_VI_VRR"); + if (out) *out = vrr && !strcmp(vrr, "1"); + static int logged; + if (!logged++) rt_log("vi: QueryDisplayVrrSupported -> %d", out ? *out : -1); ret_x(c, 0); } HOST_OVERRIDE("_ZN2nn2vi21QueryDisplayHdrStatusEPbPNS0_15HdrOutputFormatEPtPKNS0_7DisplayE", ov_vi_hdr_status);