Triforce v0.2.0

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ruckus7382sfgsgdf
2026-11-10 19:32:48 -06:00
commit 34b3683441
1730 changed files with 554853 additions and 0 deletions

63
nx2/rt/eden_shim.c Normal file
View File

@@ -0,0 +1,63 @@
/* The recompiled game's side of the emulator ABI (mkw_host.h): a64-kit's OS hooks routed to the host table.
a64-kit's runtime (rt/a64_rt.c) leaves a64_svc to the OS layer and makes a64_on_trap, a64_call_miss and
a64_cntpct_host weak; this file is the "OS layer" when the game runs inside Eglantine, and adds a64_poll for the
loop-header polls (nx2/poll.py). Built into mkw_game.dll with the lifted code (nx2/build.py --dll). */
#include "a64.h"
#include "mkw_host.h"
#include <stdio.h>
#include <stdlib.h>
extern const uint32_t a64_fn_count;
extern const uint64_t mkw_image_base;
extern const char mkw_main_build_id[];
#define SAME(f) _Static_assert(offsetof(GuestContext, f) == offsetof(MkwGuestContext, f), "MkwGuestContext." #f)
SAME(x); SAME(pc); SAME(n); SAME(v); SAME(halted); SAME(vreg); SAME(tpidr_el0); SAME(tpidrro_el0); SAME(fpcr);
SAME(fpsr); SAME(excl_addr); SAME(excl_hi); SAME(excl_size); SAME(excl_valid); SAME(thread); SAME(poll);
_Static_assert(sizeof(GuestContext) == sizeof(MkwGuestContext), "MkwGuestContext size");
static MkwHost host;
static int attached;
void a64_svc(Ctx *c, uint32_t imm) { host.svc(host.user, c, imm); }
void a64_poll(Ctx *c) {
__atomic_store_n(&c->poll, 0, __ATOMIC_RELAXED);
host.poll(host.user, c);
}
void a64_on_trap(Ctx *c, uint64_t pc, uint32_t insn, const char *why) {
if (attached) host.trap(host.user, c, pc, insn, why);
fprintf(stderr, "[mkw] %s at %#llx (insn %08x)\n", why, (unsigned long long)pc, insn);
abort();
}
int a64_call_miss(Ctx *c, uint64_t target) { return attached && host.call_miss(host.user, c, target); }
uint64_t a64_cntpct_host(void) { return attached ? host.cntpct(host.user) : 0; }
MKW_EXPORT int mkw_recomp_attach(const MkwHost *h, MkwGameInfo *info) {
if (info) {
info->version = MKW_HOST_VERSION;
info->ctx_size = (uint32_t)sizeof(GuestContext);
info->mem_base = (uint64_t)(uintptr_t)A64_MEM;
info->image_base = mkw_image_base;
info->functions = a64_fn_count;
#if defined(A64_POLLING)
info->polls = 1;
#else
info->polls = 0;
#endif
info->main_build_id = mkw_main_build_id;
}
if (!h || h->version != MKW_HOST_VERSION || h->ctx_size != sizeof(GuestContext))
return 1;
if (!h->svc || !h->poll || !h->trap || !h->call_miss || !h->cntpct)
return 2;
host = *h;
attached = 1;
return 0;
}
MKW_EXPORT MkwFn mkw_recomp_lookup(uint64_t guest_addr) { return (MkwFn)a64_lookup(guest_addr); }

82
nx2/rt/mkw_host.h Normal file
View File

@@ -0,0 +1,82 @@
/* The C ABI between the recompiled game (mkw_game.dll: lifted code + a64-kit's runtime + eden_shim.c) and the
emulator that hosts it (Eglantine's ArmRecomp backend, eglantine/src/core/arm/recomp/).
The DLL knows nothing about Eden: every way out of lifted code (SVC, interrupt poll, trap, an indirect call to an
address with no lifted function, the counter) goes through the MkwHost table the backend hands to
mkw_recomp_attach. The backend knows nothing about the lifted code beyond this header: it looks functions up by
guest address and calls them with a context.
Guest memory is the 64-bit window at A64_HOST_BASE (nx2/rtgen.py): the backend must have placed the process's
fastmem arena there (attach checks it). */
#ifndef MKW_HOST_H
#define MKW_HOST_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define MKW_HOST_VERSION 1
/* The lifted code's context (a64-kit rt/a64.h GuestContext with nx2/rtgen.py's poll flag), as the host sees it.
eden_shim.c checks every offset against the real one at compile time. */
typedef struct MkwGuestContext {
uint64_t x[32]; /* x[31] is SP */
uint64_t pc; /* written where a thread stops (traps); otherwise the thread's start address */
uint8_t n, z, c, v;
int32_t halted;
uint64_t vreg[32][2];
uint64_t tpidr_el0;
uint64_t tpidrro_el0;
uint64_t fpcr, fpsr;
uint64_t excl_addr, excl_lo, excl_hi;
uint32_t excl_size;
int32_t excl_valid;
void *thread; /* the host's per-thread object */
uint32_t poll; /* interrupt request: the next loop header calls MkwHost::poll */
} MkwGuestContext;
struct GuestContext;
typedef void (*MkwFn)(struct GuestContext *c);
typedef struct MkwHost {
uint32_t version; /* MKW_HOST_VERSION */
uint32_t ctx_size; /* sizeof(GuestContext) as the host sees it */
void *user;
/* SVC #imm: the context holds the arguments; returns when the thread runs again, results in the context */
void (*svc)(void *user, struct GuestContext *c, uint32_t imm);
/* c->poll was set (an interrupt): returns when the thread runs again */
void (*poll)(void *user, struct GuestContext *c);
/* the thread cannot continue (UDF, BRK, a branch to a non-function): does not return */
void (*trap)(void *user, struct GuestContext *c, uint64_t pc, uint32_t insn, const char *why);
/* BLR / BR / a thread start at an address with no lifted function: run it some other way (the JIT) until it
returns to c->x[30] (calls) or forever (jumps); returns 1 if it did */
int (*call_miss)(void *user, struct GuestContext *c, uint64_t target);
/* CNTPCT_EL0 */
uint64_t (*cntpct)(void *user);
} MkwHost;
#if defined(_WIN32)
#define MKW_EXPORT __declspec(dllexport)
#else
#define MKW_EXPORT __attribute__((visibility("default")))
#endif
/* exported by mkw_game.dll */
typedef struct MkwGameInfo {
uint32_t version; /* MKW_HOST_VERSION */
uint32_t ctx_size; /* sizeof(GuestContext) in the DLL */
uint64_t mem_base; /* A64_HOST_BASE: where the guest window must be */
uint64_t image_base; /* the guest address the image was lifted at (rtld's load address) */
uint32_t functions; /* lifted entries */
uint32_t polls; /* 1 if built with A64_POLLING */
const char *main_build_id; /* hex, the main module's NSO build id */
} MkwGameInfo;
typedef int (*PFN_mkw_recomp_attach)(const MkwHost *host, MkwGameInfo *info); /* 0: ok */
typedef MkwFn (*PFN_mkw_recomp_lookup)(uint64_t guest_addr); /* NULL: no lifted function */
#ifdef __cplusplus
}
#endif
#endif