Skip to content

Rewriter: build-specific patches move into the profiles (#63) - #77

Closed
v0lumehi wants to merge 2 commits into
seedds:mainfrom
v0lumehi:patch-split
Closed

v0lumehi wants to merge 2 commits into
seedds:mainfrom
v0lumehi:patch-split

Conversation

@v0lumehi

@v0lumehi v0lumehi commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Implements the split proposed in #63. The change is mechanical, so it can be reviewed by checking it rather than by reading every line.

Layout

  • dm/tools/nso_asm.py: the instruction encoders (B, ADRP, LDRX, …), shared by the rewriter and the patches.
  • dm/games/<id>/patches.py: one game's CUTS, OBS and, for builds with a frame-step state page, FRAME_STEP with apply(p). The stubs go through p.emit, p.here, p.fs_site, p.fs_put and p.fs_state_off, and a render-scale stub sets p.render_scale = 1. 16 profiles have one; a game without patches has none.
  • dm/games/sdk_patches.py: the cuts of SDK builds (15 builds). These can't live in one game's file: nine of them are shared between games, and one between four (Odyssey, Raging Loop, DQ XI S, Luigi's Mansion 3). Each entry names the games whose build rows list it.
  • dm/tools/patch_nso.py (1,711 → 1,103 lines): it loads every file (load_patches; a build in two files is an error), applies what belongs to the build it rewrites, and keeps the state page and its fs_site / fs_put. build_src.sha256 now lists every source file, and the NSP script's rewrite fingerprint covers the new files, so editing a patches.py triggers a rewrite.
  • Docs: dm/games/README.md describes the format. Every place in AGENTS.md, docs/development.md and docs/agents that named patch_nso.py for a specific cut, gate or frame-step stub now names the file it lives in: the game's patches.py, or sdk_patches.py for SDK builds (33 places, second commit). References to the general rewriter (spin waits, jump tables, pieces, half_count) still name patch_nso.py.

What stays the same

Lines moved verbatim, comments included. There are only two rewrites:

  • if FS_STATE_OFF and BUILD_ID == '<id>': → def apply(p):, plus one line binding the helpers.
  • RENDER_SCALE = 1 → p.render_scale = 1.

Checks:

  • CUTS (30 builds), OBS (2) and FRAME_STEP (4) are equal to the old file's tables.
  • I replayed each of the four frame-step blocks, old and new, against a recording context. Tears of the Kingdom, Breath of the Wild, Age of Calamity and Paper Mario each give the same calls: sites, emitted words, puts and render scale. That covers the three games whose files I don't have.
  • Breath of the Wild (main, sdk, subsdk0) and Hades (main, sdk) rewrite byte-identically: all 30 output files match, except build_src.sha256.

Note

This conflicts with #76 in Breath of the Wild's frame-step block, since #76 adds stubs there. Whichever lands second, I'll rebase it: #76's stubs would move into dm/games/01007EF00011E000/patches.py, and its FS_KEYS would become a field of p.

…ofiles (GitHub issue seedds#63)

patch_nso.py held the general rewriter and every game's build-specific patches in one file, so work on
different games kept editing the same lines. Now:
- dm/tools/nso_asm.py: the instruction encoders (B, ADRP, LDRX, ...), shared by the rewriter and the patches.
- dm/games/<id>/patches.py: a game's CUTS, OBS and, for builds with a frame-step state page, FRAME_STEP with
  apply(p), which adds the stubs through p.emit / p.here / p.fs_site / p.fs_put / p.fs_state_off and sets
  p.render_scale. 16 profiles have one; a game without patches has none.
- dm/games/sdk_patches.py: the cuts of SDK builds (15), which several games can ship (nine of them are shared,
  one by four games); each entry names the games whose build rows list it.
- patch_nso.py (1,711 → 1,103 lines) loads every file (load_patches; a build in two files stops it), applies
  what belongs to the build it rewrites and keeps the state page and its fs_site / fs_put. build_src.sha256
  lists every source, and the NSP script's rewrite fingerprint covers the new files.

Lines moved verbatim, comments included; the only rewrites are "if FS_STATE_OFF and BUILD_ID == '<id>':" →
"def apply(p):" with one line binding the helpers, and "RENDER_SCALE = 1" → "p.render_scale = 1". Checked:
CUTS (30 builds), OBS (2) and FRAME_STEP (4) equal to the old file's tables; each of the four frame-step blocks
(Tears of the Kingdom, Breath of the Wild, Age of Calamity, Paper Mario) replayed old and new against a
recording context gives the same calls (sites, emitted words, puts, render scale); Breath of the Wild (main,
sdk, subsdk0) and Hades (main, sdk) rewrite byte-identically (all 30 output files but build_src.sha256).
…file it now lives in (the game's patches.py, or sdk_patches.py for SDK builds)
@v0lumehi

Copy link
Copy Markdown
Contributor Author

Superseded by f4f0a85, which implements #63 along the same lines (with FRAME_STEP as {build ID: stub(ctx)}, sdk_patches.py in dm/tools and its own tests). Thanks — closing.

@v0lumehi v0lumehi closed this Oct 23, 2026
@v0lumehi
v0lumehi deleted the patch-split branch October 23, 2026 15:53
@seedds

seedds commented Oct 23, 2026

Copy link
Copy Markdown
Owner

Thanks for putting this together, and for the careful checks. Replaying the four frame-step blocks against a recording context was a good way to cover the games you don't have files for. As you said when closing, f4f0a85 landed the same split, so this stays closed. It differs from the PR in a few places (full write-up on #63: #63 (comment)):

  • Frame-step stubs are FRAME_STEP = {build ID: stub(ctx)}, functions that get ctx.here / ctx.emit / ctx.site / ctx.put / ctx.state, instead of apply(p).
  • sdk_patches.py lives in dm/tools/ beside the rewriter, not in dm/games/.
  • The NSP script's rewrite key counts only the patch files that name one of the game's builds, so one game's patch edit doesn't make other games rewrite again.
  • dm/tools/tests/test_patches.py checks that no build ID is claimed twice, that each entry has the form the rewriter expects, and that a game's file names only its own profile's builds.

Thanks also for rebasing #76 onto it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants