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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Build outputs (Xcode derived data, produced by the scripts under dm/ios)
/build/
.iosdd/
.iosdd_mac/
.iosdd_*/
Expand Down
1 change: 1 addition & 0 deletions dm/games/0100152000022000/game.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ program_id 0100152000022000
name Mario Kart 8 Deluxe
ipa MarioKart8Deluxe.ipa
app_category public.app-category.racing-games
pad_layout full
# the race records SetRenderEnableConditional as a course loads: evaluated at replay, as on the GPU (drawn anyway, the same race route at the same frames looked the same; evaluated is NVN's behaviour)
render_cond on

Expand Down
4 changes: 4 additions & 0 deletions dm/games/0100152000022000/ios.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Always expose the complete Switch controller surface, including L3/R3 and
# the D-pad, and keep touch controls usable beside a keyboard or controller.
DM_PAD_FORCE_FULL=1
DM_PAD_KEEP_TOUCH=1
5 changes: 4 additions & 1 deletion dm/games/0100F43008C44000/game.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ program_id 0100F43008C44000
name Pokémon Legends: Z-A
ipa PokMonLegendsZA.ipa
app_category public.app-category.games
pad_layout simple
pad_layout full
render_cond on

# ---- Versions ----
build v393216 2.0.2 main=b1f12fd919eae86ab8a978317677e64b sdk=df7a3960c74c3a4803f826c587153252
Expand All @@ -23,6 +24,8 @@ shader_versions v393216
modules main sdk
romfs_marker arc/data.trpfs
zip_store .bnk:.pck:.trpfd:.trpfs:.metallib
# Old Apple GPU builds are invalid without this profile's proven shader companions.
ios_companions ios_companions.json

# ---- Memory ----
# nninitStartup (main+0x1b2e650) initializes its StandardAllocator with virtual address memory over 12 GB of address space (StandardAllocator::Initialize(nullptr, 0x300000000), main+0x1b3e3ec),
Expand Down
11 changes: 11 additions & 0 deletions dm/games/0100F43008C44000/ios.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Production iOS defaults for Pokemon Legends: Z-A 2.0.2.
# Documents/env.txt has priority over every value below.
DM_PRESENT_INTERVAL=game
DM_ZA_SKILL_LAYOUT=1
DM_DISPLAY_HZ=120
DM_PRESENT_HZ=120
DM_ZA_FPS_MODE=dynamic60
# Always expose the complete Switch controller surface, including L3/R3 and
# the D-pad, and keep touch controls usable beside a keyboard or controller.
DM_PAD_FORCE_FULL=1
DM_PAD_KEEP_TOUCH=1
55 changes: 55 additions & 0 deletions dm/games/0100F43008C44000/ios_companions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"schema": 1,
"manifests": [
{
"directory": "layered-v1",
"kind": "layered-slices-v1",
"required_records": [
"57246f9a04af8529_b",
"d7d72bbbb0e32517_b",
"b29f08619f379a83_b",
"5b931243ad28aeeb_b"
]
},
{
"directory": "nonpoint-v1",
"required_records": [
"94a4c491a475b170_b",
"ce7eacf81c6e4ffc_b"
]
},
{
"directory": "sampler-limit-v1",
"kind": "sampler-limit-v1",
"limit": 16,
"required_records": [
"a6c06cda878a4fe0_b"
]
},
{
"directory": "guarded-index-v1",
"kind": "guarded-index-v1",
"original_directory": "nonpoint-v1",
"required_records": [
"94a4c491a475b170_b",
"ce7eacf81c6e4ffc_b"
]
},
{
"directory": "zero-image-v1",
"kind": "zero-image-clear-v1",
"required_records": [
"5b107ddfd06551fc_b"
]
}
],
"records": [
{
"directory": "atomic-v1",
"kind": "bounded-image-atomics-v1",
"required_records": [
"2bd5037003980716_b"
]
}
]
}
29 changes: 20 additions & 9 deletions dm/games/0100F43008C44000/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,23 @@
(0x1b3e3e4, '6200c0d2', '2200c0d2', 'StandardAllocator::Initialize(nullptr, size): address space mov x2,#0x300000000 → mov x2,#0x100000000 (12 GB → 4 GB)'),
],
}
# The same cut for builds without registered CUTS (dm/tools/nso_match.py), found by the fingerprint of the function that makes the call (132 bytes, the mov at +52) and its direct
# callees' (the allocator option getter it calls first, StandardAllocator::Initialize). The base game 1.0.0 (v0, main 7222e13e…, GitHub issue #266) has it at main+0x19f84b4
# (function main+0x19f8480: an exact nso_fnmap.py match of 2.0.2's main+0x1b3e3b0, the same fingerprint) and ships 2.0.2's sdk (df7a3960…), so the node area cut and the
# 5 GB layout hold for it unchanged
allocator = 'StandardAllocator::Initialize(nullptr, size): address space 12 GB → 4 GB'
MATCH_CUTS = {'main': [
{'name': allocator, 'size': 132, 'fingerprints': ['af8eeda3d34bbfbfee1896ee0469ada1804c957695f3c7f2b8a970a20f495879'],
'cuts': [(52, '6200c0d2', '2200c0d2', allocator + ': mov x2,#0x300000000 → mov x2,#0x100000000 (as 2.0.2 main+0x1b3e3e4)')]},
]}

# No timing behavior changes by default. Both callbacks return without touching
# guest registers unless their diagnostics / experiment is explicitly enabled.
# hle_za_timing.c checks the same build ID again at runtime.
OBS = {
'b1f12fd919eae86ab8a978317677e64b': [
# Clock channel 0 returns {total_ns, delta_ns}. The next instructions convert
# delta_ns / 1e9 to float and hand it to engine updates (main+0x1b29314/334).
(0x1b292dc, 26, 'fd400400', 'Trinity base-clock delta: orig ldr d0,[x0,#8]; observe game seconds versus monotonic time'),
# After the configured interval and native adaptive candidate were selected,
# immediately after bl main+0x113488. The original mov forwards w20 to the
# interval setter. An opt-in mutable callback can reproduce Static60 without
# changing an instruction in memory; off preserves w20 and w22 exactly.
(0x1b296f0, 27, '2a1403e1', 'Trinity interval: orig mov w1,w20; optional saved-register experiment with timing diagnostics'),
# Generic layout visibility, filtered by the exact WazaLayoutView vtable in
# the host. Diagnostics only: no input remapping or register changes.
(0x425c70, 28, 'f9405408', 'Layout visibility: orig ldr x8,[x0,#0xa8]; observe the actual battle move layout, not ZL alone'),
(0x2b7bfbc, 29, 'a9be7bfd', 'Waza layout destructor: orig stp x29,x30,[sp,#-0x20]!; expire the observed skill context'),
],
}
1 change: 1 addition & 0 deletions dm/games/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Each game the port knows has a folder `dm/games/<id>/`, named after the base gam
- **`bf_want.tsv`**.
- **`README.md`**: the game's notes: what its profile needs and why, what was found on it, how to reach a scene on the Mac, what is still open. Listed in [Games](#games).
- **`patches.py`** (optional): the rewriter's patches of the game's own builds (`main`), keyed by NSO build ID: `CUTS` (byte cuts, each original asserted), `OBS` (observation gates) and `FRAME_STEP` (frame-step stubs). `dm/tools/patch_nso.py` loads every profile's file and applies those of a module's build, wherever it comes from; a build claimed twice stops the rewrite. A provisional profile (in `DM_GAMES_DIR`) may have one too, which the rewriter and the NSP script's rewrite step take as well. Cuts of Nintendo's `sdk` modules, which several games ship, are in `dm/tools/sdk_patches.py`. Opt-in `MATCH_CUTS` can locate verified cuts in unregistered builds of the selected game by function fingerprints (a patch of several functions applies whole or not at all); see [Function-matched cuts](../../docs/reference/rewriter.md#function-matched-cuts-for-unregistered-builds) for enrollment, profile precedence and limits. A game without patches has no file, and a derived profile uses its repository profile's rules. `dm/tools/tests/test_patches.py` checks that a file names only its own profile's builds.
- **`ios.env`** (optional): production iOS-only `KEY=VALUE` defaults bundled as `dm/env.txt`. An explicit `DM_ENV_FILE=<file>` replaces it, `DM_ENV_FILE=` disables it, and the player's `Documents/env.txt` overrides either source at runtime. It is not read by the Mac host or other platforms.

The chain version is not in the profile: one version serves every game (`dm/gfx/nvn/dmsh_chain.h`, written by `dmsh_chain.py --header`). Two more tables live in the profile folder but are not committed (git ignores them). A full `dmsh_build.py` run makes both from its Mac outputs, so each checkout makes its own, and stamps its libraries with the digest of the profile's inputs (`b-<V>/inputs.tsv`, `dmsh_chain.py --inputs`). The host takes a table only when its header names its library's chain version and that digest.
- **`c1_full.tsv`**: written by `c1_guard.py`. Without it every shader's c1 gets its own 64 KiB.
Expand Down
2 changes: 1 addition & 1 deletion dm/gfx/nvn/dmsh_chain.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Generated file (dm/gfx/tools/dmsh_chain.py --header): the B shader library translation chain version this source tree expects (default options), one for every game;
// the host loads only <library dir>/b-<V>/. Run dmsh_chain.py --header after changing the chain's tools and commit this file with the change
#define DMSH_CHAIN "b34b226edf5a"
#define DMSH_CHAIN "0cb0dfeacee3"
71 changes: 71 additions & 0 deletions dm/gfx/nvn/nvn_argument_encoding.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once
// Tier 1 argument buffers have a GPU-private layout. Populate their public
// encoder instead of assuming an eight-byte pointer/resource-ID representation.
#import <Metal/Metal.h>
#include "nvn_refl.h"
#include <stdio.h>
#include <string.h>

static int nvn_argument_encode(id<MTLArgumentEncoder> encoder, id<MTLBuffer> destination,
NSUInteger offset, const nvn_refl *reflection, const uint64_t *slots,
size_t count, char *error, size_t error_size) {
if (!encoder || !destination || !reflection || !slots || reflection->nb < 0 || reflection->nb > NVN_REFL_MAXB ||
offset > destination.length || encoder.encodedLength > destination.length - offset ||
(encoder.alignment && offset % encoder.alignment)) {
snprintf(error, error_size, "invalid encoder, destination, alignment or reflection"); return 0;
}
@try {
[encoder setArgumentBuffer:destination offset:offset];
for (int i = 0; i < reflection->nb; i++) {
const nvn_rbind *binding = &reflection->b[i];
unsigned n = binding->n ? binding->n : 1;
if (binding->aid >= count || n > count - binding->aid) {
snprintf(error, error_size, "argument index %u length %u exceeds slot capacity %zu", binding->aid, n, count); return 0;
}
for (unsigned element = 0; element < n; element++) {
NSUInteger index = binding->aid + element;
uint64_t token = slots[index];
switch (binding->kind) {
case NVN_RB_CBUF: case NVN_RB_SBUFP: case NVN_RB_SBUFU: case NVN_RB_UBOP:
case NVN_RB_SUPPORT: case NVN_RB_C1: case NVN_RB_DRV0: case NVN_RB_DRV2:
case NVN_RB_IMGA: case NVN_RB_GEOMETRY: {
NSUInteger buffer_offset = 0;
id<MTLBuffer> buffer = legacy_buffer_at(token, &buffer_offset);
if (token && !buffer) { snprintf(error, error_size, "unregistered buffer token at argument %lu", (unsigned long)index); return 0; }
[encoder setBuffer:buffer offset:buffer_offset atIndex:index];
break;
}
case NVN_RB_TEX: case NVN_RB_TEXU: case NVN_RB_IMG: case NVN_RB_TBUF:
case NVN_RB_TEXD: case NVN_RB_TEXA:
[encoder setTexture:(__bridge id<MTLTexture>)(void *)(uintptr_t)token atIndex:index]; break;
case NVN_RB_SAMP: case NVN_RB_SAMPT: case NVN_RB_SAMPU: case NVN_RB_SAMPI:
case NVN_RB_SAMPD: case NVN_RB_SAMPA:
[encoder setSamplerState:(__bridge id<MTLSamplerState>)(void *)(uintptr_t)token atIndex:index]; break;
default:
snprintf(error, error_size, "unsupported binding kind %u at argument %lu", binding->kind, (unsigned long)index); return 0;
}
}
}
} @catch (NSException *exception) {
snprintf(error, error_size, "%s: %s", exception.name.UTF8String, exception.reason.UTF8String); return 0;
}
return 1;
}

// Use argumentIndex rather than fixed byte offsets when reading the compiled
// pointer access on a device whose argument layout is private.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
static void nvn_argument_private_access(nvn_refl *reflection, MTLArgument *argument) {
if (!argument || argument.type != MTLArgumentTypeBuffer || argument.index != (NSUInteger)reflection->abi ||
argument.bufferDataType != MTLDataTypeStruct) return;
for (MTLStructMember *member in argument.bufferStructType.members) {
if (member.dataType != MTLDataTypePointer) continue;
MTLPointerType *pointer = member.pointerType;
if (!pointer || pointer.elementIsArgumentBuffer ||
(pointer.access != MTLArgumentAccessReadWrite && pointer.access != MTLArgumentAccessWriteOnly)) continue;
for (int i = 0; i < reflection->nb; i++) if (reflection->b[i].kind == NVN_RB_SBUFP &&
reflection->b[i].aid == member.argumentIndex) reflection->b[i].rw = 1;
}
}
#pragma clang diagnostic pop
143 changes: 143 additions & 0 deletions dm/gfx/nvn/nvn_cpu_decode_cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Exact, bounded cache of CPU-decoded BC pixels. The caller still uploads every
// dirty candidate: only CPU decoding is reused, so image writes and lost-upload
// retries retain the ordinary upload behavior. Serialized by the upload owner.
#pragma once
#include "nvn_tex.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>

#ifndef NVN_CPU_CACHE_LOG
#define NVN_CPU_CACHE_LOG(...) do { fprintf(stderr, "[cpu-decode-cache] " __VA_ARGS__); fputc('\n', stderr); } while (0)
#endif
#ifndef NVN_CPU_CACHE_BUDGET
#define NVN_CPU_CACHE_BUDGET (32u * 1024u * 1024u)
#endif
#ifndef NVN_CPU_CACHE_SOURCE_MAX
#define NVN_CPU_CACHE_SOURCE_MAX (8u * 1024u * 1024u)
#endif
#ifndef NVN_CPU_CACHE_DECODED_MAX
#define NVN_CPU_CACHE_DECODED_MAX (16u * 1024u * 1024u)
#endif
enum { CPU_CACHE_SLOTS = 32 };
typedef struct {
uint64_t serial, offset, source_bytes, decoded_bytes;
const void *mem, *pool;
nvn_texdesc desc;
} nvn_cpu_decode_key;
typedef struct {
nvn_cpu_decode_key key;
uint8_t *source, *decoded;
uint64_t used;
int valid;
} nvn_cpu_decode_entry;
typedef struct {
nvn_cpu_decode_entry *entry;
const uint8_t *source;
uint8_t *decoded;
int hit;
} nvn_cpu_decode_ticket;
static struct {
nvn_cpu_decode_entry entries[CPU_CACHE_SLOTS];
uint64_t bytes, clock, hits, misses, evictions, uncached, compare_ns, copy_ns;
uint64_t polled_frame, reported_frame;
int initialized, on;
} g_cpu_decode_cache;

static uint64_t nvn_cpu_cache_ns(void) {
struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t);
return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec;
}
static void nvn_cpu_decode_drop(nvn_cpu_decode_entry *e) {
if (!e->source && !e->decoded) return;
g_cpu_decode_cache.bytes -= e->key.source_bytes + e->key.decoded_bytes;
free(e->source); free(e->decoded); memset(e, 0, sizeof *e);
}
static void nvn_cpu_decode_clear(void) {
for (int i = 0; i < CPU_CACHE_SLOTS; i++) nvn_cpu_decode_drop(&g_cpu_decode_cache.entries[i]);
}
static int nvn_cpu_decode_on(uint64_t frame) {
if (!g_cpu_decode_cache.initialized || g_cpu_decode_cache.polled_frame != frame) {
g_cpu_decode_cache.initialized = 1; g_cpu_decode_cache.polled_frame = frame;
const char *e = getenv("DM_NVN_CPU_DECODE_CACHE"); int on = e && !strcmp(e, "1");
if (on != g_cpu_decode_cache.on) {
nvn_cpu_decode_clear(); g_cpu_decode_cache.on = on;
NVN_CPU_CACHE_LOG("[CPU-DECODE-CACHE] %s frame=%llu budget=%u source_cap=%u decoded_cap=%u; exact bytes, every upload retained",
on ? "enabled" : "disabled", (unsigned long long)frame,
NVN_CPU_CACHE_BUDGET, NVN_CPU_CACHE_SOURCE_MAX, NVN_CPU_CACHE_DECODED_MAX);
}
}
return g_cpu_decode_cache.on;
}
static int nvn_cpu_decode_same_key(const nvn_cpu_decode_key *a, const nvn_cpu_decode_key *b) {
return a->serial == b->serial && a->offset == b->offset && a->mem == b->mem && a->pool == b->pool
&& a->source_bytes == b->source_bytes && a->decoded_bytes == b->decoded_bytes
&& !memcmp(&a->desc, &b->desc, sizeof a->desc);
}
static nvn_cpu_decode_entry *nvn_cpu_decode_oldest(nvn_cpu_decode_entry *except) {
nvn_cpu_decode_entry *old = NULL;
for (int i = 0; i < CPU_CACHE_SLOTS; i++) {
nvn_cpu_decode_entry *e = &g_cpu_decode_cache.entries[i];
if (e != except && e->source && (!old || e->used < old->used)) old = e;
}
return old;
}
// Called only on a CPU-BC reupload, after its normal protect + generation stamp.
// The owned source is used for a miss's decode. A subsequent guest write cannot
// make the cached baseline disagree with the bytes actually decoded.
static nvn_cpu_decode_ticket nvn_cpu_decode_begin(uint64_t frame, const nvn_cpu_decode_key *key) {
nvn_cpu_decode_ticket out = {0};
if (!nvn_cpu_decode_on(frame)) return out;
if (!key->serial || !key->mem || !key->source_bytes || !key->decoded_bytes
|| key->source_bytes > NVN_CPU_CACHE_SOURCE_MAX || key->decoded_bytes > NVN_CPU_CACHE_DECODED_MAX
|| key->source_bytes + key->decoded_bytes > NVN_CPU_CACHE_BUDGET) {
g_cpu_decode_cache.uncached++; return out;
}
nvn_cpu_decode_entry *e = NULL;
for (int i = 0; i < CPU_CACHE_SLOTS; i++)
if (g_cpu_decode_cache.entries[i].source && g_cpu_decode_cache.entries[i].key.serial == key->serial) { e = &g_cpu_decode_cache.entries[i]; break; }
if (e && e->valid && nvn_cpu_decode_same_key(&e->key, key)) {
uint64_t start = nvn_cpu_cache_ns();
int same = !memcmp(e->source, key->mem, (size_t)key->source_bytes);
g_cpu_decode_cache.compare_ns += nvn_cpu_cache_ns() - start;
if (same) {
e->used = ++g_cpu_decode_cache.clock; g_cpu_decode_cache.hits++;
return (nvn_cpu_decode_ticket){e, e->source, e->decoded, 1};
}
}
g_cpu_decode_cache.misses++;
if (!e) for (int i = 0; i < CPU_CACHE_SLOTS; i++)
if (!g_cpu_decode_cache.entries[i].source) { e = &g_cpu_decode_cache.entries[i]; break; }
if (!e) { e = nvn_cpu_decode_oldest(NULL); g_cpu_decode_cache.evictions++; }
if (!e) return out;
nvn_cpu_decode_drop(e);
uint64_t need = key->source_bytes + key->decoded_bytes;
while (g_cpu_decode_cache.bytes + need > NVN_CPU_CACHE_BUDGET) {
nvn_cpu_decode_entry *old = nvn_cpu_decode_oldest(e);
if (!old) return out;
nvn_cpu_decode_drop(old); g_cpu_decode_cache.evictions++;
}
uint8_t *source = malloc((size_t)key->source_bytes);
uint8_t *decoded = calloc(1, (size_t)key->decoded_bytes);
if (!source || !decoded) { free(source); free(decoded); g_cpu_decode_cache.uncached++; return out; }
uint64_t start = nvn_cpu_cache_ns(); memcpy(source, key->mem, (size_t)key->source_bytes);
g_cpu_decode_cache.copy_ns += nvn_cpu_cache_ns() - start;
e->key = *key; e->source = source; e->decoded = decoded; e->used = ++g_cpu_decode_cache.clock;
g_cpu_decode_cache.bytes += need;
return (nvn_cpu_decode_ticket){e, source, decoded, 0};
}
static void nvn_cpu_decode_finish(nvn_cpu_decode_ticket *t, int decoded_ok) {
if (!t->entry || t->hit) return;
if (decoded_ok) t->entry->valid = 1; else nvn_cpu_decode_drop(t->entry);
}
static void nvn_cpu_decode_report(uint64_t frame) {
if (!g_cpu_decode_cache.on || frame / 300 == g_cpu_decode_cache.reported_frame) return;
g_cpu_decode_cache.reported_frame = frame / 300;
NVN_CPU_CACHE_LOG("[CPU-DECODE-CACHE] frame=%llu hits=%llu misses=%llu evictions=%llu uncached=%llu retained_bytes=%llu compare_ms=%.3f source_copy_ms=%.3f",
(unsigned long long)frame, (unsigned long long)g_cpu_decode_cache.hits,
(unsigned long long)g_cpu_decode_cache.misses, (unsigned long long)g_cpu_decode_cache.evictions,
(unsigned long long)g_cpu_decode_cache.uncached, (unsigned long long)g_cpu_decode_cache.bytes,
g_cpu_decode_cache.compare_ns / 1e6, g_cpu_decode_cache.copy_ns / 1e6);
}
Loading