Files
totk-pc-recomp/runtime/nvn_procs.c
T
Valiantsin DzerakhandClaude Opus 5.5 97d3762ba1 Strip milestone and task labels from code comments and drop the dead gfx_stubs.c
Comments named the agent task (T1-T7, M2-M5, Task 2.x) that once owned each file or block, and pointed at local
notes the repository does not carry (backlog items, milestone plans, research notes). They now say what the code
does. gfx_stubs.c held weak stand-ins for functions that every build has defined for a long time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 13:12:50 +03:00

79 lines
2.9 KiB
C

// 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_<name> is the implementation of one proc: the weak definitions here stop with the proc's name, nvn_api.c and
// nvn_ext.c define the strong ones. The name list is runtime/nvn_procs.inc (tools/gen_nvn_procs.py).
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#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));
}