Add a text speed setting for the game's message windows on 1.1.1.

The patch set text_speed scales the typewriter speed of a message the game lets the player skip (fast, three times as
fast, its scripted waits as long) or runs the game's own skip on the first frame of each page (instant). Messages the
game marks unskippable, such as those that turn their page on a timer, keep the game's speed. The setting is live and
has a row on the Game tab in all four menu languages; with it at normal the set is off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 00:52:48 -05:00
parent e89cc4d430
commit 2bbdb25de7
12 changed files with 369 additions and 2 deletions

140
tests/text_speed_check.c Normal file
View File

@@ -0,0 +1,140 @@
// The text_speed setting (runtime/text_speed.c) and the patch set text_speed's lifted replacement words against a
// stand-in message window and typewriter: what each setting puts on the patch page and how it switches the set; the
// skip site's typewriter word and decide bit, for a message the window lets the player skip and one it does not; the
// speed site's scaled speed for the window's typewriter and for any other; and that both sites change nothing else.
// Built by tests/text_speed_check.py, which lifts the words into sites.c. With the argument kept-off the set starts
// off (NX2NAVI_TEXT_SPEED_PATCH=0 or a mod on its sites), and no setting may switch it on or touch the page.
#include <assert.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "text_speed.c"
void site_skip(Cpu *c); // main+0x774b74 without its adrp (x9 = the page) and its last tbz w9, #0
void site_speed(Cpu *c); // main+0xafe1d8 without its adrp (x22 = the page)
static const char *speed = "normal";
const char *cfg_value(const char *key) { return strcmp(key, "NX2NAVI_TEXT_SPEED") ? "" : speed; }
static int set_on = 1; // the patch set text_speed, on at the start as its default
int rt_patch_set(const char *name) { return strcmp(name, "text_speed") ? -1 : 7; }
int rt_patch_enabled(int set) { return set == 7 && set_on; }
void rt_patch_switch(int set, int on) { assert(set == 7); set_on = on != 0; }
static _Alignas(4096) uint8_t page[4096];
void *rt_patch_page(void) { return page; }
void rt_log(const char *fmt, ...) { (void)fmt; }
static uint8_t window[0x400]; // the message window: +0x34a 1 when its message may not be skipped
static _Alignas(16) uint8_t typewriter[0x80], other[0x80]; // typewriters: +0x4c the speed
static float page_float(int off) { float f; memcpy(&f, page + off, 4); return f; }
static uint64_t page_word(int off) { uint64_t v; memcpy(&v, page + off, 8); return v; }
static float lane(const Cpu *c, int v) { float f; memcpy(&f, &c->v[v], 4); return f; }
static void fill(Cpu *c, unsigned seed) {
uint8_t *p = (uint8_t *)c;
for (size_t i = 0; i < sizeof *c; i++) p[i] = (uint8_t)(seed * 131u + i * 7u);
c->nzcv = (uint64_t)(seed & 15) << 28;
}
// The skip site for the window and its typewriter: returns the decide bit the site's tbz tests, and checks that only
// x9, x10 and the page's typewriter word changed.
static int run_skip(int unskippable, int pressed, unsigned seed) {
Cpu c, before;
fill(&c, seed);
window[0x34a] = (uint8_t)unskippable;
c.x[19] = (uintptr_t)window;
c.x[8] = (uintptr_t)typewriter;
c.x[20] = (c.x[20] & ~1ull) | (uint64_t)pressed;
c.x[9] = (uintptr_t)page; // the site's adrp
before = c;
site_skip(&c);
assert(page_word(0x200) == (unskippable ? 0 : (uint64_t)(uintptr_t)typewriter));
for (int r = 0; r < 31; r++)
if (r != 9 && r != 10) assert(c.x[r] == before.x[r]);
assert(c.nzcv == before.nzcv && c.sp == before.sp);
assert(!memcmp(c.v, before.v, sizeof c.v));
return (int)(c.x[9] & 1);
}
// The speed site for a typewriter: returns the speed it loads into s1, and checks that only x27, s1, s2 and s8 changed.
static float run_speed(uint8_t *tw, float base, unsigned seed) {
Cpu c, before;
fill(&c, seed);
memcpy(tw + 0x4c, &base, 4);
c.x[19] = (uintptr_t)tw;
c.x[22] = (uintptr_t)page; // the site's adrp
before = c;
site_speed(&c);
for (int r = 0; r < 31; r++)
if (r != 27) assert(c.x[r] == before.x[r]);
assert(c.nzcv == before.nzcv && c.sp == before.sp);
for (int v = 0; v < 32; v++)
if (v != 1 && v != 2 && v != 8) assert(!memcmp(&c.v[v], &before.v[v], sizeof c.v[v]));
return lane(&c, 1);
}
static void kept_off(void) {
set_on = 0;
memset(page, 0xa5, sizeof page);
const char *all[] = {"normal", "fast", "instant", "normal"};
for (int i = 0; i < 4; i++) {
speed = all[i];
text_speed_poll();
assert(!set_on && !text_speed_available());
for (size_t k = 0; k < sizeof page; k++) assert(page[k] == 0xa5);
}
printf("text_speed_check: a set kept off stays off and the page untouched\n");
}
int main(int argc, char **argv) {
if (argc > 1 && !strcmp(argv[1], "kept-off")) {
kept_off();
return 0;
}
// normal: the set goes off, the page holds no factor and no skip
speed = "normal";
text_speed_poll();
assert(text_speed_available() && !set_on && page_float(0x208) == 0.0f && page[0x20c] == 0);
// An unknown value reads as normal.
speed = "turbo";
text_speed_poll();
assert(!set_on && page_float(0x208) == 0.0f && page[0x20c] == 0);
// fast: the set on, the factor less one on the page, no skip
speed = "fast";
text_speed_poll();
assert(set_on && page_float(0x208) == TEXT_SPEED_FAST_FACTOR - 1.0f && page[0x20c] == 0);
for (unsigned seed = 1; seed < 40; seed++) {
int pressed = seed & 1;
// a message the player may skip: the window's typewriter goes on the page and types faster; the decide bit
// is the player's own
assert(run_skip(0, pressed, seed) == pressed);
float base = 0.25f * (float)(seed % 9 + 1);
assert(fabsf(run_speed(typewriter, base, seed) - base * TEXT_SPEED_FAST_FACTOR) < 1e-5f);
// any other typewriter keeps its speed
assert(run_speed(other, base, seed) == base);
// a message the window does not let the player skip: no typewriter on the page, the game's speed
assert(run_skip(1, pressed, seed) == pressed);
assert(run_speed(typewriter, base, seed) == base);
}
// instant: the set on, no factor, the decide bit set for the skip test (whose +0x34a test is the game's own)
speed = "instant";
text_speed_poll();
assert(set_on && page_float(0x208) == 0.0f && page[0x20c] == 1);
for (unsigned seed = 1; seed < 40; seed++) {
assert(run_skip(0, seed & 1, seed) == 1);
assert(run_speed(typewriter, 1.5f, seed) == 1.5f);
assert(run_skip(1, seed & 1, seed) == 1);
assert(run_speed(typewriter, 1.5f, seed) == 1.5f);
}
// back to normal: the set off again (the sites run the original instructions), the page cleared
speed = "normal";
text_speed_poll();
assert(!set_on && page_float(0x208) == 0.0f && page[0x20c] == 0);
printf("text_speed_check: settings, page words and both sites as expected\n");
return 0;
}

82
tests/text_speed_check.py Normal file
View File

@@ -0,0 +1,82 @@
"""Build and run tests/text_speed_check.c: the text_speed setting (runtime/text_speed.c) and the replacement words of
the patch set text_speed (recomp/patches-v111.txt), lifted by recomp/lifter.py as the generator lifts them, against a
stand-in message window and typewriter. No game files: the lifted words go to the test's build folder.
Usage: text_speed_check.py
"""
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path[:0] = [str(ROOT), str(ROOT / 'recomp')]
from lifter import Lifter # noqa: E402
from patches import load # noqa: E402
from tools.check_content import GENERATED_MARKER # noqa: E402
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
MAIN = 0x7000000000
SKIP_SITE, SPEED_SITE = 0x774b74, 0xafe1d8
def compiler():
if os.environ.get('CC'):
return os.environ['CC']
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
return str(MSYS_BIN / 'clang.exe')
return shutil.which('clang') or 'clang'
def site_words(table, site, original):
"""The set's replacement words at a site, after checking the site has only them and the original word."""
alternatives = table.sites['main'][site]
assert len(alternatives) == 1, hex(site)
key, words, have = alternatives[0]
assert table.sets[key] == ('text_speed', 'NX2NAVI_TEXT_SPEED_PATCH', 1), table.sets[key]
assert have == original, (hex(site), hex(have))
return words
def lifted(name, site, words, page_reg, last_branch):
"""A C function running the words at the site: the adrp of the patch page is left to the caller, who puts the
page's address in that register, and a last conditional branch is left to the caller too, who reads its register."""
body = []
for j, word in enumerate(words):
if isinstance(word, tuple):
assert word == ('page', page_reg) and j == (1 if site == SPEED_SITE else 0), (hex(site), word)
continue
insn = Lifter(0, simd='c').lift(word, MAIN + site)
if j == len(words) - 1 and last_branch:
assert insn.kind != 'op' and word == 0x36000069, hex(word) # tbz w9, #0, the original's target
continue
assert insn.kind == 'op', (hex(site), hex(word), insn.kind)
body.append(' ' + insn.code)
return f'void {name}(Cpu *c) {{\n' + '\n'.join(body) + '\n}\n'
def main():
table = load(ROOT / 'recomp/patches-v111.txt', [('main', MAIN, 0x4ea0000)])
skip = site_words(table, SKIP_SITE, 0x36000074) # tbz w20, #0, #0x774b80
speed = site_words(table, SPEED_SITE, 0xbd404e61) # ldr s1, [x19, #0x4c]
work = ROOT / 'build' / 'test' / 'text_speed'
work.mkdir(parents=True, exist_ok=True)
sites = work / 'sites.c'
sites.write_text(f'// {GENERATED_MARKER}: the patch set text_speed lifted by tests/text_speed_check.py.\n'
'#include "cpu.h"\n#include "simd.h"\n\n'
+ lifted('site_skip', SKIP_SITE, skip, 9, True) + '\n'
+ lifted('site_speed', SPEED_SITE, speed, 22, False))
exe = work / ('text_speed_check.exe' if os.name == 'nt' else 'text_speed_check')
env = dict(os.environ)
if os.name == 'nt' and MSYS_BIN.exists():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
subprocess.check_call([compiler(), '-O1', '-Wall', '-Wno-unused-function', '-Wno-unused-variable',
'-fno-strict-aliasing', '-I', str(ROOT / 'runtime'),
str(ROOT / 'tests' / 'text_speed_check.c'), str(sites), '-o', str(exe), '-lm'], env=env)
rc = subprocess.run([str(exe)], env=env).returncode
return subprocess.run([str(exe), 'kept-off'], env=env).returncode or rc
if __name__ == '__main__':
sys.exit(main())