Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion dm/games/0400486024DEC000/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The Legend of Zelda: Ocarina of Time (`0400486024DEC000`)

A Switch 2 game: update v131072, SDK 22.2.8 (`Ounce`). Nintendo EPD's engine (sead, agl, gsys, Havok, CRIWARE; project `Owl`, with Tears of the Kingdom's file layout) on NVN2. Modules `main`, `subsdk0` (NintendoSDK_movie) and `sdk`, no rewriter patches. Status: plays on the Mac through the title, the opening, a new file's first scene, Link's house and Kokiri Forest, with saves, at about 59 fps with the picture close to console video. Mac only so far.
A Switch 2 game: update v131072, SDK 22.2.8 (`Ounce`). Nintendo EPD's engine (sead, agl, gsys, Havok, CRIWARE; project `Owl`, with Tears of the Kingdom's file layout) on NVN2. Modules `main`, `subsdk0` (NintendoSDK_movie) and `sdk`. Update v131072 has an optional host render-scale ceiling. Status: plays on the Mac through the title, the opening, a new file's first scene, Link's house and Kokiri Forest, with saves, at about 59 fps with the picture close to console video. Mac only so far.

## What it needs

Expand All @@ -16,6 +16,14 @@ A Switch 2 game: update v131072, SDK 22.2.8 (`Ounce`). Nintendo EPD's engine (se
| `shader_groups`, `shader_note` | `game.tsv` | shaders grouped as `/Shader/`, `/Effect/`, `/Lib/`, `/UI/` |
| `pad_layout full` | `game.tsv` | a 3D field and both sticks |

## Render-scale ceiling

Update v131072 (`main` build `e0f3ccfaf50065ae4ea01eb5aeb13b1a`) accepts `DM_RENDER_SCALE=0.5` through `1.0`. The default `1.0` keeps the game's original scale. Lower values cap the scale the engine has selected, so dynamic resolution can still reduce it further. The output size and the game's upscaler follow their existing paths. For example, a host ceiling of `0.5` keeps a game-selected `0.4` and caps a game-selected `0.75` to `0.5`.

The rewriter checks the instruction at `main+0x31d3f48` before replacing its final scale store. The stub reads the existing host state page at `+0x10`, preserves the store's registers and condition flags, and advertises `RENDER_SCALE` so the launcher offers its existing Render scale control. Other builds receive no fixed-address hook. This is a deliberate game rendering option whose scale calculation lives in the game binary. It uses the existing shader libraries.

Validation on the M1 Pro exercised the original cap at 50% on the house-to-Kokiri Forest route. The isolated stub additionally preserves the source SIMD register, checked by ARM64 execution across caps, game-selected scales and condition flags. The test also checks the launcher setting against rewritten metadata. Sustained frame-rate gains, later areas and iOS remain unmeasured for this isolated change.

## Files

One [NUP container](../../../docs/development.md#the-packing-script-in-detail) of 25.6 GB: `scripts/pack_nup.py` unpacks it to `extract/v131072/`. The RomFS keeps most files as zstd with dictionaries from `Pack/ZsDic.pack.zs`. The movies are AV1.
Expand Down Expand Up @@ -77,6 +85,7 @@ From an empty save: at about 45 s `A` (title), `A` (New Game), then the movie. F

## Found on this game

- [A dynamic-resolution ceiling set after the game chooses its scale](../../../docs/reference/rendering.md#a-dynamic-resolution-ceiling-set-after-the-game-chooses-its-scale): the optional v131072 host scale cap.
- [Raw global memory lookups that start from their last entry](../../../docs/reference/shaders.md#raw-global-memory-lookups-that-start-from-their-last-entry): the foliage, vertex and compute shaders read structures word by word through loaded addresses; about 2 ms less GPU a frame at the spawn point.
- [The GPU idles with the next command buffer already committed](../../../docs/reference/frame-rate.md#the-gpu-idles-with-the-next-command-buffer-already-committed): the 25-40 ms frames.
- [Pool tables and readback buffers made anew each frame](../../../docs/reference/memory.md#pool-tables-and-readback-buffers-made-anew-each-frame): about 1,500 residency changes per 300 frames.
Expand Down
45 changes: 45 additions & 0 deletions dm/games/0400486024DEC000/patches.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Ocarina v131072's build-checked dynamic-resolution ceiling.

The engine stores its final scale after dynamic and fixed-mode selection.
State +0x10 holds the existing host DM_RENDER_SCALE setting.
"""
from nso_asm import ADRP, B


def render_scale(ctx):
site = 0x31d3f48
ctx.site(site, '805e0ebd') # str s0,[x20,#0xe5c]
start = ctx.here()
ctx.emit(0xadbf7ffe) # stp q30,q31,[sp,#-32]!
ctx.emit(0xa9bf47f0) # stp x16,x17,[sp,#-16]!
ctx.emit(ADRP(16, ctx.here(), ctx.state))
for word in (
0xd53b4211, # mrs x17,nzcv: the original store leaves flags intact
0xbd40121e, # ldr s30,[x16,#0x10]: host scale ceiling
0x1e2023c8, # fcmp s30,#0
0x540000ed, # b.le original: an unset state keeps the game
0x1e2e101f, # fmov s31,#1
0x1e3f23c0, # fcmp s30,s31
0x5400008a, # b.ge original: 1.0 keeps the original bits
0x1e3e581f, # fmin s31,s0,s30: the game can still choose a lower scale
0xbd0e5e9f, # str s31,[x20,#0xe5c]: leave s0 as the original store did
0x14000002, # b restore
0xbd0e5e80, # original: str s0,[x20,#0xe5c]
0xd51b4211, # restore: msr nzcv,x17
0xa8c147f0, # ldp x16,x17,[sp],#16
0xacc17ffe, # ldp q30,q31,[sp],#32
):
ctx.emit(word)
ctx.emit(B(ctx.here(), site + 4))
ctx.put(site, [B(site, start)], 'dynamic resolution capped by the host render scale (1.0 = original)')
ctx.render_scale = 1


FRAME_STEP = {'e0f3ccfaf50065ae4ea01eb5aeb13b1a': render_scale}

BINDING = [
{'what': 'dynamic-resolution ceiling', 'now': 'build', 'goal': 'game', 'purpose': 'rendering',
'why': "the game's final scale store follows its dynamic and fixed-mode choices",
'blocked': 'the store and its role are verified for this main build only',
'checked': 'v131072 main E0F3CCFAF50065AE4EA01EB5AEB13B1A, original store and surrounding code, 2026-11-06'},
]
90 changes: 90 additions & 0 deletions dm/tools/tests/test_oot_render_scale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""Execute Ocarina's emitted ARM64 cap and check its launcher capability."""
import importlib.util
import math
from pathlib import Path
import struct
import sys
import tempfile
import types
import unittest
from unittest.mock import patch

REPO = Path(__file__).resolve().parents[3]
sys.path.insert(0, str(REPO / 'dm/tools'))
sys.path.insert(0, str(REPO / 'dm/games'))
import settings # noqa: E402

spec = importlib.util.spec_from_file_location('oot_scale', REPO / 'dm/games/0400486024DEC000/patches.py')
assert spec is not None and spec.loader is not None
profile = importlib.util.module_from_spec(spec)
spec.loader.exec_module(profile)
try:
unicorn = importlib.import_module('unicorn')
arm = importlib.import_module('unicorn.arm64_const')
except ImportError:
unicorn = arm = None


class RenderScale(unittest.TestCase):
def test_registered_build_and_launcher_capability(self):
self.assertEqual(list(profile.FRAME_STEP), ['e0f3ccfaf50065ae4ea01eb5aeb13b1a'])
with tempfile.TemporaryDirectory() as tmp, patch.object(settings.nacp, 'languages', return_value=['en-US']):
fixups = Path(tmp) / 'main.fixups.tsv'
for supported in (False, True):
fixups.write_text('# FPS_STATE_OFF\t0x10000\n' + ('# RENDER_SCALE\t1\n' if supported else ''))
for platform in ('ios', 'mac'):
with patch.dict(settings.os.environ, {'SETTINGS_PLATFORM': platform}):
rows = settings.build('0400486024DEC000', tmp, 'unused')['settings']
scale = [row for row in rows if row['env'] == 'DM_RENDER_SCALE']
self.assertEqual(bool(scale), supported)
self.assertNotIn('DM_FPS_TARGET', [row['env'] for row in rows])
if scale:
self.assertEqual((scale[0]['min'], scale[0]['max'], scale[0]['default']), (0.5, 1.0, '1.0'))

@unittest.skipIf(unicorn is None, 'optional ARM64 execution dependency: unicorn')
def test_actual_stub_caps_the_store_and_preserves_registers(self):
assert unicorn is not None and arm is not None
base, area, state, obj, stack, site = 0x4000000, 0x10000, 0x20000, 0x30000, 0x40000, 0x31d3f48
machine = unicorn.Uc(unicorn.UC_ARCH_ARM64, unicorn.UC_MODE_ARM)
machine.mem_map(base, 0x3200000)
cursor = area
checked, patched = [], []

def emit(word):
nonlocal cursor
machine.mem_write(base + cursor, struct.pack('<I', word))
cursor += 4

def put(offset, words, reason):
patched.append((offset, reason))
machine.mem_write(base + offset, struct.pack('<' + 'I' * len(words), *words))

context = types.SimpleNamespace(here=lambda: cursor, emit=emit, site=lambda offset, old: checked.append((offset, old)), put=put, state=state)
profile.render_scale(context)
self.assertEqual(checked, [(site, '805e0ebd')])
self.assertEqual([offset for offset, _ in patched], [site])
self.assertEqual(context.render_scale, 1)
self.assertLessEqual(cursor - area, 80)
for ceiling in (-1.0, 0.0, 0.5, 0.75, 1.0, 2.0, float('inf'), float('nan')):
machine.mem_write(base + state + 0x10, struct.pack('<f', ceiling))
for original in (0.25, 0.5, 0.625, 0.875, 1.0):
for flags in (0, 0x60000000, 0x90000000, 0xf0000000):
with self.subTest(ceiling=ceiling, original=original, flags=flags):
registers = {getattr(arm, f'UC_ARM64_REG_X{i}'): 0x12340000 + i for i in range(31)}
registers.update({getattr(arm, f'UC_ARM64_REG_Q{i}'): 0x123456789abcdef0fedcba9876543210 + i for i in range(32)})
registers.update({arm.UC_ARM64_REG_X20: base + obj, arm.UC_ARM64_REG_SP: base + stack, arm.UC_ARM64_REG_NZCV: flags})
original_bits = struct.unpack('<I', struct.pack('<f', original))[0]
registers[arm.UC_ARM64_REG_Q0] = (registers[arm.UC_ARM64_REG_Q0] & ~0xffffffff) | original_bits
for register, value in registers.items():
machine.reg_write(register, value)
machine.mem_write(base + obj + 0xe58, b'\xa5' * 12)
machine.emu_start(base + site, base + site + 4, count=64)
expected = original if not 0 < ceiling < 1 or math.isnan(ceiling) else min(original, ceiling)
stored = machine.mem_read(base + obj + 0xe58, 12)
self.assertEqual(bytes(stored), b'\xa5' * 4 + struct.pack('<f', expected) + b'\xa5' * 4)
for register, value in registers.items():
self.assertEqual(machine.reg_read(register), value, f'register {register}')


if __name__ == '__main__':
unittest.main()
12 changes: 12 additions & 0 deletions docs/agents/game-patches.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Every patch bound tighter than it could be: the open gaps, then the blocked, kep
| `build` → `game` | 60 fps frame step and render scale | frame rate | blocked: frame-step stubs have no function-matched form yet | judgement | 1 | [The Legend of Zelda: Tears of the Kingdom](../../dm/games/0100F2C0115B6000/README.md) | the game's own timing object |
| `build` → `game` | 60 fps frame step and render scale (ksys::VFR) | frame rate | blocked: frame-step stubs have no function-matched form yet | judgement | 1 | [The Legend of Zelda: Breath of the Wild](../../dm/games/01007EF00011E000/README.md) | the game's own frame code; the frame-step state page is general |
| `build` → `game` | 60 fps frame step: the KIDS frame task and frame-rate block | frame rate | blocked: frame-step stubs have no function-matched form yet | judgement | 1 | [Hyrule Warriors: Age of Calamity](../../dm/games/01002B00111A2000/README.md) | the game's own frame code; only the host's frame-step state page is shared. Fire Emblem: Three Houses and Taiko Risshiden V DX run on KTGL too, with other frame code |
| `build` → `game` | dynamic-resolution ceiling | rendering | blocked: the store and its role are verified for this main build only | checked: v131072 main E0F3CCFAF50065AE4EA01EB5AEB13B1A, original store and surrounding code, 2026-11-06 | 1 | [The Legend of Zelda: Ocarina of Time](../../dm/games/0400486024DEC000/README.md) | the game's final scale store follows its dynamic and fixed-mode choices |
| `build` → `game` | frame step floor 2 → 1 vsync, and its frame step | frame rate | blocked: frame-step stubs have no function-matched form yet | judgement | 1 | [Paper Mario: The Thousand-Year Door](../../dm/games/0100ECD018EBE000/README.md) | the game's own clock floor and step |
| `build` → `game` | screen shape: the sizes, the view, the layouts' projection and their edge widgets (DM_SCREEN_ASPECT) | rendering | blocked: the stubs have no function-matched form yet | judgement | 1 | [Animal Crossing: New Horizons](../../dm/games/01006F8002326000/README.md) | the output size is the host's, but where the game takes its sizes, its cameras and its layouts are this build's code |
| `build` → `game` | Hagi coroutine stack region 1 GB → 64 MB | memory | kept: 1.1.4 is the last update; a match over Hagi would serve no other build of this game | checked: the final build 1.1.4 (the only one the profile packs), 2026-11-04 | 1 | [Super Mario 3D All-Stars: Super Mario Sunshine](../../dm/games/010049900F546002/README.md) | a size inside Hagi's own code: nothing the host sees tells a 1 GB reservation it can shrink from one it cannot |
Expand Down Expand Up @@ -88,6 +89,7 @@ Every patch bound tighter than it could be: the open gaps, then the blocked, kep
| [Taiko Risshiden V DX](#taiko-risshiden-v-dx) | 1 | 0 | 0 | 2 | 0 | `game` |
| [The Legend of Zelda: Breath of the Wild](#the-legend-of-zelda-breath-of-the-wild) | 7 | 0 | 43 | 0 | 0 | `build` → `game` ×2 |
| [The Legend of Zelda: Link's Awakening](#the-legend-of-zelda-links-awakening) | 1 | 0 | 0 | 0 | 0 | `build` → `game` |
| [The Legend of Zelda: Ocarina of Time](#the-legend-of-zelda-ocarina-of-time) | 0 | 0 | 1 | 0 | 0 | `build` → `game` |
| [The Legend of Zelda: Tears of the Kingdom](#the-legend-of-zelda-tears-of-the-kingdom) | 0 | 0 | 9 | 0 | 0 | `build` → `game` |
| [Xenoblade Chronicles 3](#xenoblade-chronicles-3) | 6 | 0 | 0 | 0 | 0 | `build` → `game` |

Expand Down Expand Up @@ -589,6 +591,16 @@ Every patch bound tighter than it could be: the open gaps, then the blocked, kep
- **1.1.0 `main`** `909e904a…` ([patches.py:12](../../dm/games/01006BB00C6F0000/patches.py#L12))
- cut `+0x10cd070`: physics completion task release (calls its own destructor): ldr x8,[x0] → ret (no longer destroyed; the step function rebuilds it in place every step)

### The Legend of Zelda: Ocarina of Time

[The Legend of Zelda: Ocarina of Time](../../dm/games/0400486024DEC000/README.md), `0400486024DEC000`, [patches.py](../../dm/games/0400486024DEC000/patches.py).

- **`build` → `game`**, rendering, blocked: the store and its role are verified for this main build only, checked: v131072 main E0F3CCFAF50065AE4EA01EB5AEB13B1A, original store and surrounding code, 2026-11-06: dynamic-resolution ceiling. The game's final scale store follows its dynamic and fixed-mode choices

- **v131072 `main`** `e0f3ccfa…` ([patches.py:38](../../dm/games/0400486024DEC000/patches.py#L38))
- frame step `+0x31d3f48`: dynamic resolution capped by the host render scale (1.0 = original)
- render scale stub (the launcher offers DM_RENDER_SCALE)

### The Legend of Zelda: Tears of the Kingdom

[The Legend of Zelda: Tears of the Kingdom](../../dm/games/0100F2C0115B6000/README.md), `0100F2C0115B6000`, [patches.py](../../dm/games/0100F2C0115B6000/patches.py).
Expand Down
1 change: 1 addition & 0 deletions docs/agents/symptoms.md
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
- read-only depth/stencil attachments are written back after every pass, and the periodic unwritten-store savings counter remains zero with the default configuration. → [Unwritten attachment stores without lookahead](../reference/rendering.md#unwritten-attachment-stores-without-lookahead)
- portrait gameplay overlaps the touch controls, touch coordinates land outside the game picture, or a TV's picture renders at 4K again after a layout pass. → [iOS layouts: portrait, a half-folded Duo, iPad windows](../reference/rendering.md#ios-layouts-portrait-a-half-folded-duo-ipad-windows)
- with *Picture shape* at 1.43:1 (`DM_SCREEN_ASPECT`) the game renders 1920x1344, but on an iPhone Duo in Fit (the default) the picture comes out smaller than the 16:9 one, with black bars on all four sides; it fills the screen only in Fill. On the Mac a click lands off the point under the pointer (it was mapped on a 16:9 rectangle of the window). → [The picture laid out in the shape the game presents](../reference/rendering.md#the-picture-laid-out-in-the-shape-the-game-presents)
- a game has dynamic resolution but the host's Render scale setting cannot lower its ceiling. → [A dynamic-resolution ceiling set after the game chooses its scale](../reference/rendering.md#a-dynamic-resolution-ceiling-set-after-the-game-chooses-its-scale)

## Shaders

Expand Down
2 changes: 1 addition & 1 deletion docs/env_switches.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ DM_QOS_EDGES switch `=e1,e2`: the Switch priority edges of `DM_QOS_MAP` (default
DM_QOS_MAP switch `=1` maps guest thread priority to QoS class: ≤ 44 user-interactive, 45–59 user-initiated, the rest utility (off by default: every guest thread runs user-interactive)
DM_REC diagnostic `=<file>`: writes player 1's physical input (keyboard, mouse touches, controllers; never `DM_KEYS` / `DM_CTL`) to the file as `DM_KEYS` steps in presented frames, for replaying a run (`DM_KEYS=@<file>`)
DM_REGIONS diagnostic `=<file>` writes the table of the SDK's address regions every 2 s (alias offset, size, where the request returns to in the game, bytes mapped, the part opened, the SDK offset the request came through; `hle_svc.c` "Address regions"); the NSP script's address region check sets it
DM_RENDER_SCALE player `0.5` to `1.0`: fixes the game's 3D views at that fraction of their size and turns its dynamic resolution off, for builds with a render scale stub; `1.0` (the default) keeps it
DM_RENDER_SCALE player `0.5` to `1.0` for builds with a render-scale stub. Ocarina v131072 caps its dynamic-resolution scale; other supported builds fix their 3D view scale and disable dynamic resolution. `1.0` (the default) keeps the game's scale
DM_RESOURCE_AOC path The add-on content folder the iOS build copies into the app's `aoc/` (empty for none); written by `gen_resources.py`, read by `stage_resources.sh`
DM_RESOURCE_BUILD switch Package version (for example `v327680`), written to `Gen/resources.env` by `gen_resources.py`; with `DM_COPY_ROMFS=NO` the copy phase puts it in the `build=` field of the app's `romfs.id`
DM_RESOURCE_CHAIN_H path Path of `dm/gfx/nvn/dmsh_chain.h`, written to `Gen/resources.env` by `build_ipa.sh`; the Xcode copy phase (`stage_resources.sh`) stops when the prepared shaders are not of its chain version
Expand Down
Loading
Loading