// NVN proc table. Every name the game's proc loader asks for gets a synthetic host // address through hostfn_register before hostfn_seal; nvnBootstrapLoader and nvnDeviceGetProcAddress hand them out. // nvnp_ is the implementation of one proc: the weak definitions here stop with the proc's name, nvn_api.c and // nvn_ext.c (T3b onward) define the strong ones. The name list is runtime/nvn_procs.inc (tools/gen_nvn_procs.py). #include #include #include #include #include "gfx.h" #include "host.h" // The strong nvnp_nvnDeviceGetProcAddress below replaces the stand-in generated for the same row. #define nvnp_nvnDeviceGetProcAddress nvnp_stub_nvnDeviceGetProcAddress #define NVN_PROC(n) void nvnp_##n(Cpu *c); #include "nvn_procs.inc" #undef NVN_PROC #define NVN_PROC(n) \ __attribute__((weak)) void nvnp_##n(Cpu *c) { \ (void)c; \ rt_fatal("NVN " #n " is not implemented"); \ } #include "nvn_procs.inc" #undef NVN_PROC #undef nvnp_nvnDeviceGetProcAddress void nvnp_nvnDeviceGetProcAddress(Cpu *c); typedef struct { const char *name; uint64_t addr; } Proc; #define NVN_PROC(n) +1 enum { PROC_COUNT = 0 #include "nvn_procs.inc" }; #undef NVN_PROC static Proc procs[PROC_COUNT]; static int nprocs; static int by_name(const void *a, const void *b) { return strcmp(((const Proc *)a)->name, ((const Proc *)b)->name); } void nvn_procs_register(void) { if (nprocs) return; #define NVN_PROC(n) procs[nprocs++] = (Proc){#n, hostfn_register("nvn:" #n, nvnp_##n)}; #include "nvn_procs.inc" #undef NVN_PROC qsort(procs, (size_t)nprocs, sizeof *procs, by_name); } uint64_t nvn_proc_address(const char *name) { if (!name) return 0; const Proc *p = bsearch(&(Proc){name, 0}, procs, (size_t)nprocs, sizeof *procs, by_name); if (p) return p->addr; // Unknown names get NULL, as on hardware; each distinct one is logged once. static SRWLOCK lock = SRWLOCK_INIT; static char seen[32][96]; static int nseen; AcquireSRWLockExclusive(&lock); int fresh = 1; for (int i = 0; i < nseen; i++) if (!strncmp(seen[i], name, sizeof seen[i] - 1)) fresh = 0; if (fresh && nseen < 32) { strncpy(seen[nseen], name, sizeof seen[0] - 1); nseen++; } else { fresh = 0; } ReleaseSRWLockExclusive(&lock); if (fresh) rt_log("nvn: no such proc \"%s\", returning NULL", name); return 0; } // PFNNVNGENERICFUNC nvnDeviceGetProcAddress(const NVNdevice *device, const char *name) void nvnp_nvnDeviceGetProcAddress(Cpu *c) { const char *name = arg_p(c, 1); ret_x(c, nvn_proc_address(name)); }