From b7d0c353c7ff265f4904fd710c96395078e1dbe9 Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Mon, 19 Oct 2026 09:58:25 +0200 Subject: Implement Many Host Functions Implemented many of the missing host functions, but some things are still unimplemented: 1. Audio, because I have _no_ idea how that works. 2. Syscalls, they look hairy and will look into them separately. 3. ASM constants, we need to go over the array in the the exported JS module and understand how each one maps to our host. The other functions are _mostly_ straightforward to implement, and I did so with some caveats (for example, only supporting the single format the PICO-8 uses for `strftime`) which is good enough for us. Next, I need to refactor how we load carts: in particular, all carts need to come from the `_cartdat` mechanism, so the "embedded" carts instead of loading them ad-hoc. This requires us to refactor how we do the multicart loading to pre-load the entire `.p8.dir` when a cart is selected (instead of just keeping their names and lazy-loading them in). --- src/env.c | 378 ++++++++++++++++++++++++++++++++++++++++++----------------- src/env.h | 15 ++- src/game.c | 6 +- src/input.c | 3 +- src/wasmrt.c | 19 ++- src/wasmrt.h | 17 ++- 6 files changed, 304 insertions(+), 134 deletions(-) diff --git a/src/env.c b/src/env.c index bb4f7f9..05cb0de 100644 --- a/src/env.c +++ b/src/env.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include "env.h" @@ -11,7 +12,9 @@ // PICO-8 allocates a fix 128 Mb heap unconditinoally; but for some platforms, // that may be too much. Specify a lower bound, in 64Kb pages, for the memory // that we allocate in our Web Assembly environment. This _technically_ does -// not respect the module's minimum memory size. +// not respect the module's minimum memory size. This lower bound corresponds +// to 32 Mb, and is large enough to write the data at the fixed memory offsets +// (such as the dynamic top value, and the time data). #define ENV_MIN_PAGES 512 #define ENV_DYNAMICTOP_PTR 3199024 @@ -28,6 +31,11 @@ // The pixel format used by the engine is: #define ENV_PIXELFORMAT_ABGR8888 0x86762004 +#define ENV_TM_CURRENT 3198880 +#define ENV_TM_TIMEZONE 3198928 +#define ENV_SECONDS_PER_DAY (1000 * 60 * 60 * 24) +#define ENV_NS_PER_YEAR 31556952000000000 + struct w2c_env *pw_env_init(size_t memory) { size_t bytes = memory << 20; size_t pages = bytes / wasm2c_pico8_pagesize_env_memory; @@ -84,21 +92,27 @@ bool pw_env_link(struct w2c_env *env, struct pw_env_ctx context) { if (!env) { return SDL_InvalidParamError("env"); } + if (env->memory.size < ENV_MIN_PAGES * wasm2c_pico8_pagesize_env_memory) { + return SDL_SetError("cannot write to fixed memory offsets"); + } if (!context.fb || !context.input || !context.carts || !context.instance) { return SDL_InvalidParamError("context"); } + SDL_memset(env->memory.data, 0, env->memory.size); + SDL_memset(env->table.data, 0, env->table.size * sizeof(*env->table.data)); env->context = context; - env->dynamictop = ENV_DYNAMICTOP_PTR; - env->tablebase = 0; - env->mainloop = 0; - env->error = 0; - env->tempret = 0; + SDL_zerop(&env->data); - SDL_memset(env->memory.data, 0, env->memory.size); - wasm_rt_memory_store32_unchecked(&env->memory, env->dynamictop, + // Prepare the memory at fixed offsets that come from the host: + + env->data.dynamictop = ENV_DYNAMICTOP_PTR; + wasm_rt_memory_store32_unchecked(&env->memory, env->data.dynamictop, ENV_DYNAMIC_BASE); + char tz[] = "GMT"; + wasm_rt_memory_store_unchecked(&env->memory, ENV_TM_TIMEZONE, tz, sizeof(tz)); + return true; } @@ -112,28 +126,131 @@ bool pw_env_link(struct w2c_env *env, struct pw_env_ctx context) { a += (n / 8); \ } while (0) +static uint32_t env_swap32(uint32_t *slot, uint32_t value) { + uint32_t result = *slot; + *slot = value; + return result; +} + +static uint32_t env_strndup(struct w2c_env *env, char const *s, uint32_t sz) { + if (!sz) { + size_t ssz = SDL_strlen(s) + 1; + if (ssz > UINT32_MAX) { + wasm_rt_trap(WASM_RT_TRAP_OOB); + } + sz = (uint32_t)ssz; + } + + uint32_t ptr = w2c_pico8_0x5Fmalloc(env->context.instance, sz); + wasm_rt_memory_store(&env->memory, ptr, s, sz); + return ptr; +} + static uint32_t env_ticks(void) { return (uint32_t)(SDL_GetTicks() & UINT32_MAX); } -static SDL_NORETURN void unimplemented(char const *name) { - SDL_LogCritical(0, "called unimplemented function: %s", name); +static uint32_t env_timetm(struct w2c_env *env, uint32_t time, + bool local_time) { + int64_t ns = SDL_SECONDS_TO_NS(time); + SDL_DateTime dt; + if (!SDL_TimeToDateTime(ns, &dt, local_time)) { + return 0; + } + + SDL_DateTime jan = { + .year = dt.year, + .month = 1, + .day = 1, + .hour = 0, + .minute = 0, + .second = 0, + .nanosecond = 0, + .day_of_week = 0, + .utc_offset = dt.utc_offset, + }; + if (!SDL_DateTimeToTime(&jan, &ns)) { + return 0; + } + int day_of_year = (time - SDL_NS_TO_SECONDS(ns)) / ENV_SECONDS_PER_DAY; + + bool is_dst; + if (local_time) { + SDL_DateTime jul; + if (!SDL_TimeToDateTime(ns, &jan, true) || + !SDL_TimeToDateTime(ns + ENV_NS_PER_YEAR / 2, &jul, true)) { + return 0; + } + is_dst = jan.utc_offset != jul.utc_offset && + dt.utc_offset == SDL_min(jan.utc_offset, jul.utc_offset); + } else { + is_dst = false; + } + + uint32_t tm = ENV_TM_CURRENT; + MEM_WRITE(32, env, tm, dt.second); + MEM_WRITE(32, env, tm, dt.minute); + MEM_WRITE(32, env, tm, dt.hour); + MEM_WRITE(32, env, tm, dt.day); + MEM_WRITE(32, env, tm, dt.month); + MEM_WRITE(32, env, tm, dt.year - 1900); + MEM_WRITE(32, env, tm, dt.day_of_week); + MEM_WRITE(32, env, tm, day_of_year); + MEM_WRITE(32, env, tm, (uint32_t)is_dst); + MEM_WRITE(32, env, tm, dt.utc_offset); + if (local_time) { + uint32_t tz = w2c_pico8_0x5F_get_timezone(env->context.instance); + wasm_rt_memory_store32(&env->memory, tz, dt.utc_offset); + uint32_t ds = w2c_pico8_0x5F_get_timezone(env->context.instance); + wasm_rt_memory_store32(&env->memory, ds, dt.utc_offset); + + // SDL does not provide an API for getting a timezone name, and we do not + // want to rely on POSIX for this. Just use a fake `LT` and `LST` for + // "local time" and "local daylight savings time" respectively. + uint32_t tn = w2c_pico8_0x5F_get_tzname(env->context.instance); + if (is_dst) { + char const name[] = "LST"; + wasm_rt_memory_store(&env->memory, tn, name, sizeof(name)); + } else { + char const name[] = "LT"; + wasm_rt_memory_store(&env->memory, tn, name, sizeof(name)); + }; + + MEM_WRITE(32, env, tm, tn); + } else { + MEM_WRITE(32, env, tm, ENV_TM_TIMEZONE); + } + + return ENV_TM_CURRENT; +} + +static inline SDL_NORETURN void env_abort() { wasm_rt_trap(WASM_RT_TRAP_UNREACHABLE); } -extern uint32_t *w2c_env_DYNAMICTOP_PTR(struct w2c_env *env) { - return &env->dynamictop; +static SDL_NORETURN void env_unimplemented(char const *name) { + SDL_LogCritical(0, "called unimplemented function: %s", name); + env_abort(); +} + +static SDL_NORETURN void env_badasm(char const *i, uint32_t code) { + SDL_LogCritical(0, "unknown ASM (%s) code %x", i, code); + env_abort(); +} + +uint32_t *w2c_env_DYNAMICTOP_PTR(struct w2c_env *env) { + return &env->data.dynamictop; } -extern uint32_t *w2c_env_0x5F_table_base(struct w2c_env *env) { - return &env->tablebase; +uint32_t *w2c_env_0x5F_table_base(struct w2c_env *env) { + return &env->data.tablebase; } struct wasm_rt_memory_t *w2c_env_memory(struct w2c_env *env) { return &env->memory; } -extern wasm_rt_funcref_table_t *w2c_env_table(struct w2c_env *env) { +struct wasm_rt_funcref_table_t *w2c_env_table(struct w2c_env *env) { return &env->table; } @@ -143,10 +260,10 @@ void w2c_env_0x5FSDL_ClearError(struct w2c_env *env) { } uint32_t w2c_env_0x5FSDL_GetError(struct w2c_env *env) { - char err[ENV_ERROR_MAX_SIZE]; + char err[ENV_ERROR_MAX_SIZE] = {0}; SDL_strlcpy(err, SDL_GetError(), ENV_ERROR_MAX_SIZE); - wasm_rt_memory_store(&env->memory, err, env->error, ENV_ERROR_MAX_SIZE); - return env->error; + wasm_rt_memory_store(&env->memory, env->data.errstr, err, ENV_ERROR_MAX_SIZE); + return env->data.errstr; } uint32_t w2c_env_0x5FSDL_GetModState(struct w2c_env *env) { @@ -182,14 +299,15 @@ void w2c_env_0x5FSDL_GetWindowSize(struct w2c_env *env, uint32_t window, uint32_t w2c_env_0x5FSDL_Init(struct w2c_env *env, uint32_t flags) { (void)flags; - env->error = w2c_pico8_0x5Fmalloc(env->context.instance, ENV_ERROR_MAX_SIZE); + env->data.errstr = + w2c_pico8_0x5Fmalloc(env->context.instance, ENV_ERROR_MAX_SIZE); return 0; } void w2c_env_0x5FSDL_LockAudio(struct w2c_env *env) { (void)env; - // TODO(nlordell): I need to figure out audio... + env_unimplemented("_SDL_LockAudio"); } uint32_t w2c_env_0x5FSDL_LockSurface(struct w2c_env *env, uint32_t surface) { @@ -313,7 +431,7 @@ uint32_t w2c_env_0x5FSDL_SetVideoMode(struct w2c_env *env, uint32_t w, void w2c_env_0x5FSDL_UnlockAudio(struct w2c_env *env) { (void)env; - // TODO(nlordell): More audio things to figure out... + env_unimplemented("_SDL_UnlockAudio"); } void w2c_env_0x5FSDL_UnlockSurface(struct w2c_env *env, uint32_t surface) { @@ -337,7 +455,7 @@ void w2c_env_0x5FSDL_UnlockSurface(struct w2c_env *env, uint32_t surface) { SDL_SetError("unable to lock framebuffer"); return; } - wasm_rt_memory_load_unchecked(&env->memory, buffer, pixels, PW_FB_SIZE); + wasm_rt_memory_load_unchecked(&env->memory, pixels, buffer, PW_FB_SIZE); pw_fb_unlock(env->context.fb); } @@ -350,11 +468,9 @@ void w2c_env_0x5F_0x5FbuildEnvironment(struct w2c_env *env, uint32_t environ) { uint32_t array = w2c_pico8_0x5Fmalloc(env->context.instance, (count + 1) * sizeof(uint32_t)); for (uint32_t i = 0; i < count; ++i) { - uint32_t sz = (uint32_t)SDL_strlen(vars[i]) + 1; uint32_t addr = array + i * sizeof(uint32_t); - uint32_t str = w2c_pico8_0x5Fmalloc(env->context.instance, sz); + uint32_t str = env_strndup(env, vars[i], 0); wasm_rt_memory_store32(&env->memory, addr, str); - wasm_rt_memory_store(&env->memory, vars[i], str, sz); } wasm_rt_memory_store32(&env->memory, array + count * sizeof(uint32_t), 0); @@ -376,7 +492,7 @@ uint32_t w2c_env_0x5F_0x5Fsyscall140(struct w2c_env *env, uint32_t a, (void)env; (void)a; (void)b; - unimplemented("___syscall140"); + env_unimplemented("___syscall140"); } uint32_t w2c_env_0x5F_0x5Fsyscall145(struct w2c_env *env, uint32_t a, @@ -384,7 +500,7 @@ uint32_t w2c_env_0x5F_0x5Fsyscall145(struct w2c_env *env, uint32_t a, (void)env; (void)a; (void)b; - unimplemented("___syscall145"); + env_unimplemented("___syscall145"); } uint32_t w2c_env_0x5F_0x5Fsyscall146(struct w2c_env *env, uint32_t a, @@ -392,7 +508,7 @@ uint32_t w2c_env_0x5F_0x5Fsyscall146(struct w2c_env *env, uint32_t a, (void)env; (void)a; (void)b; - unimplemented("___syscall146"); + env_unimplemented("___syscall146"); } uint32_t w2c_env_0x5F_0x5Fsyscall195(struct w2c_env *env, uint32_t a, @@ -400,7 +516,7 @@ uint32_t w2c_env_0x5F_0x5Fsyscall195(struct w2c_env *env, uint32_t a, (void)env; (void)a; (void)b; - unimplemented("___syscall195"); + env_unimplemented("___syscall195"); } uint32_t w2c_env_0x5F_0x5Fsyscall220(struct w2c_env *env, uint32_t a, @@ -408,7 +524,7 @@ uint32_t w2c_env_0x5F_0x5Fsyscall220(struct w2c_env *env, uint32_t a, (void)env; (void)a; (void)b; - unimplemented("___syscall220"); + env_unimplemented("___syscall220"); } uint32_t w2c_env_0x5F_0x5Fsyscall221(struct w2c_env *env, uint32_t a, @@ -416,7 +532,7 @@ uint32_t w2c_env_0x5F_0x5Fsyscall221(struct w2c_env *env, uint32_t a, (void)env; (void)a; (void)b; - unimplemented("___syscall221"); + env_unimplemented("___syscall221"); } uint32_t w2c_env_0x5F_0x5Fsyscall5(struct w2c_env *env, uint32_t a, @@ -424,7 +540,7 @@ uint32_t w2c_env_0x5F_0x5Fsyscall5(struct w2c_env *env, uint32_t a, (void)env; (void)a; (void)b; - unimplemented("___syscall5"); + env_unimplemented("___syscall5"); } uint32_t w2c_env_0x5F_0x5Fsyscall54(struct w2c_env *env, uint32_t a, @@ -432,7 +548,7 @@ uint32_t w2c_env_0x5F_0x5Fsyscall54(struct w2c_env *env, uint32_t a, (void)env; (void)a; (void)b; - unimplemented("___syscall54"); + env_unimplemented("___syscall54"); }; uint32_t w2c_env_0x5F_0x5Fsyscall6(struct w2c_env *env, uint32_t a, @@ -440,7 +556,7 @@ uint32_t w2c_env_0x5F_0x5Fsyscall6(struct w2c_env *env, uint32_t a, (void)env; (void)a; (void)b; - unimplemented("___syscall6"); + env_unimplemented("___syscall6"); } void w2c_env_0x5F_0x5Funlock(struct w2c_env *env, uint32_t ptr) { @@ -450,64 +566,74 @@ void w2c_env_0x5F_0x5Funlock(struct w2c_env *env, uint32_t ptr) { void w2c_env_0x5Fabort(struct w2c_env *env) { (void)env; - wasm_rt_trap(WASM_RT_TRAP_UNREACHABLE); + SDL_LogCritical(0, "aborting"); + env_abort(); } uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env, uint32_t code) { - (void)env; switch (code) { + case 1: + // Whether or not there is embedded cart data. Since we _only_ run with + // embedded cart data, this is always 1 (i.e. true). + return 1; case 6: - // Web player control setup, no-op. + // Command setup, no-op for us. return 0; + case 11: + return env_swap32(&env->data.command, 0); + case 12: + return env_swap32(&env->data.commandp, 0); case 16: // On module load callback, no-op. return 0; - case 27: - // Touch detection, no-op. - // TODO(nlordell): Not sure what this controls, maybe we should pass along - // whether or not we've detected touch controls? - return 0; + default: + env_badasm("i", code); } - - SDL_Log("asm const %d", code); - unimplemented("_emscripten_asm_const_i"); } -uint32_t w2c_env_0x5Femscripten_asm_const_ii(struct w2c_env *env, uint32_t a, - uint32_t b) { +uint32_t w2c_env_0x5Femscripten_asm_const_ii(struct w2c_env *env, uint32_t code, + uint32_t a) { (void)env; (void)a; - (void)b; - unimplemented("_emscripten_asm_const_ii"); + switch (code) { + default: + env_badasm("ii", code); + } } -uint32_t w2c_env_0x5Femscripten_asm_const_iii(struct w2c_env *env, uint32_t a, - uint32_t b, uint32_t c) { +uint32_t w2c_env_0x5Femscripten_asm_const_iii(struct w2c_env *env, + uint32_t code, uint32_t a, + uint32_t b) { (void)env; (void)a; (void)b; - (void)c; - unimplemented("_emscripten_asm_const_iii"); + switch (code) { + default: + env_badasm("iii", code); + } } -uint32_t w2c_env_0x5Femscripten_asm_const_iiiii(struct w2c_env *env, uint32_t a, +uint32_t w2c_env_0x5Femscripten_asm_const_iiiii(struct w2c_env *env, + uint32_t code, uint32_t a, uint32_t b, uint32_t c, - uint32_t d, uint32_t e) { + uint32_t d) { (void)env; (void)a; (void)b; (void)c; (void)d; - (void)e; - unimplemented("_emscripten_asm_const_iiiii"); + switch (code) { + default: + env_badasm("iiiii", code); + } } uint32_t w2c_env_0x5Femscripten_asm_const_iiiiiii(struct w2c_env *env, - uint32_t a, uint32_t b, - uint32_t c, uint32_t d, - uint32_t e, uint32_t f, - uint32_t g) { + uint32_t code, uint32_t a, + uint32_t b, uint32_t c, + uint32_t d, uint32_t e, + uint32_t f) { (void)env; (void)a; (void)b; @@ -515,13 +641,15 @@ uint32_t w2c_env_0x5Femscripten_asm_const_iiiiiii(struct w2c_env *env, (void)d; (void)e; (void)f; - (void)g; - unimplemented("_emscripten_asm_const_iiiiiii"); + switch (code) { + default: + env_badasm("iiiiiii", code); + } } uint32_t w2c_env_0x5Femscripten_asm_const_iiiiiiiii( - struct w2c_env *env, uint32_t a, uint32_t b, uint32_t c, uint32_t d, - uint32_t e, uint32_t f, uint32_t g, uint32_t h, uint32_t i) { + struct w2c_env *env, uint32_t code, uint32_t a, uint32_t b, uint32_t c, + uint32_t d, uint32_t e, uint32_t f, uint32_t g, uint32_t h) { (void)env; (void)a; (void)b; @@ -531,19 +659,22 @@ uint32_t w2c_env_0x5Femscripten_asm_const_iiiiiiiii( (void)f; (void)g; (void)h; - (void)i; - unimplemented("_emscripten_asm_const_iiiiiiiii"); + switch (code) { + default: + env_badasm("iiiiiiiii", code); + } } -void w2c_env_0x5Femscripten_async_wget_data(struct w2c_env *env, uint32_t a, - uint32_t b, uint32_t c, - uint32_t d) { - (void)env; - (void)a; - (void)b; - (void)c; - (void)d; - unimplemented("_emscripten_async_wget_data"); +void w2c_env_0x5Femscripten_async_wget_data(struct w2c_env *env, uint32_t url, + uint32_t userdata, uint32_t onload, + uint32_t onerror) { + (void)onload; + + char urlstr[256] = {0}; + wasm_rt_memory_load(&env->memory, url, urlstr, sizeof(url) - 1); + + SDL_LogDebug(0, "ignoring attempt to fetch %s", urlstr); + w2c_pico8_dynCall_vi(env->context.instance, onerror, userdata); } uint32_t w2c_env_0x5Femscripten_get_heap_size(struct w2c_env *env) { @@ -567,13 +698,13 @@ uint32_t w2c_env_0x5Femscripten_resize_heap(struct w2c_env *env, void w2c_env_0x5Femscripten_run_script(struct w2c_env *env, uint32_t src) { (void)env; (void)src; - unimplemented("_emscripten_run_script"); + SDL_LogDebug(0, "ignoring attempt to run script on host"); } void w2c_env_0x5Femscripten_set_main_loop(struct w2c_env *env, uint32_t func, uint32_t fps, uint32_t simulate_infinite_loop) { - env->mainloop = func; + env->data.mainloop = func; // Ignore the fps and simulate infinite loop parameters, they are always 0 // for PICO-8 anyway. @@ -582,58 +713,93 @@ void w2c_env_0x5Femscripten_set_main_loop(struct w2c_env *env, uint32_t func, } void w2c_env_0x5Fexit(struct w2c_env *env, uint32_t code) { - (void)env; - (void)code; - unimplemented("_exit"); + // Simulate an exit by signaling that an exit was requested and then trap, + // and handling this in the host. + SDL_LogDebug(0, "exiting with code: %d", code); + env->data.exit = true; + wasm_rt_trap(WASM_RT_TRAP_UNREACHABLE); } uint32_t w2c_env_0x5Fgmtime(struct w2c_env *env, uint32_t time) { - (void)env; - (void)time; - unimplemented("_gmtime"); + return env_timetm(env, time, false); } uint32_t w2c_env_0x5Flocaltime(struct w2c_env *env, uint32_t time) { - (void)env; - (void)time; - unimplemented("_localtime"); + return env_timetm(env, time, true); } void w2c_env_0x5Flongjmp(struct w2c_env *env, uint32_t jmpbuf, uint32_t value) { (void)env; (void)jmpbuf; (void)value; - unimplemented("_longjmp"); + env_unimplemented("_longjmp"); } -uint32_t w2c_env_0x5Fstrftime(struct w2c_env *env, uint32_t a, uint32_t b, - uint32_t c, uint32_t d) { - (void)env; - (void)a; - (void)b; - (void)c; - (void)d; - unimplemented("_strftime"); +uint32_t w2c_env_0x5Fstrftime(struct w2c_env *env, uint32_t buf, uint32_t sz, + uint32_t fmt, uint32_t tm) { + // AFAICT, PICO-8 only ever formats `struct tm`s with a single format string, + // so instead of relying on `strftime` (which has quite a few portability + // papercuts), just implement the bear minimum of what we need. + char buffer[64] = {0}; + + // Check that the format matches the single one we support, and it not, write + // the empty string and return a written length of 0. + wasm_rt_memory_load(&env->memory, fmt, buffer, sizeof(buffer) - 1); + if (SDL_strcmp(buffer, "%Y-%m-%d %H:%M:%S")) { + wasm_rt_memory_store8(&env->memory, buf, 0); + return 0; + } + + wasm_rt_memory_check(&env->memory, tm, 44); + SDL_DateTime dt = { + .second = wasm_rt_memory_load32_unchecked(&env->memory, tm + 0), + .minute = wasm_rt_memory_load32_unchecked(&env->memory, tm + 4), + .hour = wasm_rt_memory_load32_unchecked(&env->memory, tm + 8), + .day = wasm_rt_memory_load32_unchecked(&env->memory, tm + 12), + .month = wasm_rt_memory_load32_unchecked(&env->memory, tm + 16), + .year = wasm_rt_memory_load32_unchecked(&env->memory, tm + 20) + 1900, + }; + int n = + SDL_snprintf(buffer, sizeof(buffer), "%d-%02d-%02d %02d:%02d:%02d", + dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second); + if (n < 0 || (size_t)n >= sizeof(buffer)) { + wasm_rt_memory_store8(&env->memory, buf, 0); + return 0; + } + + if (sz) { + wasm_rt_memory_store(&env->memory, buf, buffer, sz - 1); + wasm_rt_memory_store8(&env->memory, buf + sz - 1, 0); + } + return n; } uint32_t w2c_env_0x5Ftime(struct w2c_env *env, uint32_t time) { - (void)env; - (void)time; - unimplemented("_time"); + SDL_Time ns; + if (!SDL_GetCurrentTime(&ns)) { + return (uint32_t)(-1); + } + + uint32_t s = SDL_NS_TO_SECONDS(ns); + if (time) { + wasm_rt_memory_store32(&env->memory, time, s); + } + return s; } void w2c_env_abort(struct w2c_env *env, uint32_t code) { - (void)code; - w2c_env_0x5Fabort(env); + (void)env; + SDL_LogCritical(0, "aborting with code 0x%08x", code); + env_abort(); } uint32_t w2c_env_abortOnCannotGrowMemory(struct w2c_env *env, uint32_t size) { (void)env; - (void)size; - unimplemented("abortOnCannotGrowMemory"); + SDL_LogCritical(0, "aborting unable to grow memory to size %u", size); + env_abort(); } -uint32_t w2c_env_getTempRet0(struct w2c_env *env) { return env->tempret; } +uint32_t w2c_env_getTempRet0(struct w2c_env *env) { return env->data.tempret; } void w2c_env_invoke_vii(struct w2c_env *env, uint32_t a, uint32_t b, uint32_t c) { @@ -641,11 +807,11 @@ void w2c_env_invoke_vii(struct w2c_env *env, uint32_t a, uint32_t b, (void)a; (void)b; (void)c; - unimplemented("invoke_vii"); + env_unimplemented("invoke_vii"); } void w2c_env_setTempRet0(struct w2c_env *env, uint32_t value) { - env->tempret = value; + env->data.tempret = value; } static struct w2c_global shared_global = { diff --git a/src/env.h b/src/env.h index c1a74c1..9c9aebd 100644 --- a/src/env.h +++ b/src/env.h @@ -30,11 +30,16 @@ struct w2c_env { struct wasm_rt_memory_t memory; struct wasm_rt_funcref_table_t table; struct pw_env_ctx context; - uint32_t dynamictop; - uint32_t tablebase; - uint32_t mainloop; - uint32_t error; - uint32_t tempret; + struct pw_env_data { + uint32_t dynamictop; + uint32_t tablebase; + uint32_t mainloop; + uint32_t errstr; + uint32_t tempret; + uint32_t command; + uint32_t commandp; + bool exit; + } data; }; /** diff --git a/src/game.c b/src/game.c index fe02d6f..4b1483b 100644 --- a/src/game.c +++ b/src/game.c @@ -99,7 +99,7 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *carts) { char const progname[] = "./picow"; uint32_t arg0 = w2c_pico8_stackAlloc(&game->instance, sizeof(progname)); - wasm_rt_memory_store(&game->env->memory, progname, arg0, sizeof(progname)); + wasm_rt_memory_store(&game->env->memory, arg0, progname, sizeof(progname)); uint32_t argv = w2c_pico8_stackAlloc(&game->instance, 2 * sizeof(arg0)); wasm_rt_memory_store32(&game->env->memory, argv, arg0); wasm_rt_memory_store32(&game->env->memory, argv + 4, 0); @@ -111,7 +111,7 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *carts) { if (status) { return SDL_SetError("main returned exit code"); } - if (!game->env->mainloop) { + if (!game->env->data.mainloop) { return SDL_SetError("cannot continue without main loop set"); } @@ -147,7 +147,7 @@ bool pw_game_iterate(struct pw_game *game) { wasm_rt_trap_str(handler.code)); } - w2c_pico8_dynCall_v(&game->instance, game->env->mainloop); + w2c_pico8_dynCall_v(&game->instance, game->env->data.mainloop); w2c_pico8_0x5Fmix_0(&game->instance, 42, 1337); wasm_rt_trap_unregister(); diff --git a/src/input.c b/src/input.c index 412cf88..9c03002 100644 --- a/src/input.c +++ b/src/input.c @@ -244,7 +244,8 @@ bool pw_input_event(struct pw_input *input, struct pw_players *players, static uint32_t convert_scancode(SDL_Scancode scancode) { // Some of the scancodes have slightly different numbering. switch (scancode) { - // TODO: remap scancodes that are different in case we run into any issues. + // TODO(nlordell): remap scancodes that are different in case we run into + // any issues. default: return scancode; } diff --git a/src/wasmrt.c b/src/wasmrt.c index a1daaf7..372112f 100644 --- a/src/wasmrt.c +++ b/src/wasmrt.c @@ -90,14 +90,14 @@ void wasm_rt_memory_check(struct wasm_rt_memory_t const *mem, uint32_t addr, // store and load bytes backwards, and compute offsets from the end of the // allocated memory segment. -void wasm_rt_memory_load(struct wasm_rt_memory_t const *mem, void *dst, - uint32_t addr, uint32_t len) { +void wasm_rt_memory_load(struct wasm_rt_memory_t const *mem, uint32_t addr, + void *dst, uint32_t len) { wasm_rt_memory_check(mem, addr, len); - wasm_rt_memory_load_unchecked(mem, dst, addr, len); + wasm_rt_memory_load_unchecked(mem, addr, dst, len); } void wasm_rt_memory_load_unchecked(struct wasm_rt_memory_t const *mem, - void *dst, uint32_t addr, uint32_t len) { + uint32_t addr, void *dst, uint32_t len) { #if SDL_BYTEORDER == SDL_LIL_ENDIAN SDL_memcpy(dst, mem->data + addr, len); #else @@ -125,15 +125,14 @@ uint32_t wasm_rt_memory_load32_unchecked(struct wasm_rt_memory_t const *mem, return value; } -void wasm_rt_memory_store(struct wasm_rt_memory_t *mem, void const *src, - uint32_t addr, uint32_t len) { +void wasm_rt_memory_store(struct wasm_rt_memory_t *mem, uint32_t addr, + void const *src, uint32_t len) { wasm_rt_memory_check(mem, addr, len); - wasm_rt_memory_store_unchecked(mem, src, addr, len); + wasm_rt_memory_store_unchecked(mem, addr, src, len); } -void wasm_rt_memory_store_unchecked(struct wasm_rt_memory_t *mem, - void const *src, uint32_t addr, - uint32_t len) { +void wasm_rt_memory_store_unchecked(struct wasm_rt_memory_t *mem, uint32_t addr, + void const *src, uint32_t len) { #if SDL_BYTEORDER == SDL_LIL_ENDIAN SDL_memcpy(mem->data + addr, src, len); #else diff --git a/src/wasmrt.h b/src/wasmrt.h index 62ac4d0..3800e94 100644 --- a/src/wasmrt.h +++ b/src/wasmrt.h @@ -40,7 +40,7 @@ #endif /** - * A Web Assembly trap, only the ones used by the PICO-8 `.wasm` export. + * A Web Assembly trap. */ typedef enum wasm_rt_trap_t { WASM_RT_TRAP_NONE, @@ -190,14 +190,14 @@ void wasm_rt_memory_check(struct wasm_rt_memory_t const *mem, uint32_t addr, /** * Loads bytes from memory. */ -void wasm_rt_memory_load(struct wasm_rt_memory_t const *mem, void *dst, - uint32_t addr, uint32_t len); +void wasm_rt_memory_load(struct wasm_rt_memory_t const *mem, uint32_t addr, + void *dst, uint32_t len); /** * Loads bytes from memory without bounds checking. */ void wasm_rt_memory_load_unchecked(struct wasm_rt_memory_t const *mem, - void *dst, uint32_t addr, uint32_t len); + uint32_t addr, void *dst, uint32_t len); /** * Loads a 32-bit word from memory. @@ -214,15 +214,14 @@ uint32_t wasm_rt_memory_load32_unchecked(struct wasm_rt_memory_t const *mem, /** * Stores bytes to memory. */ -void wasm_rt_memory_store(struct wasm_rt_memory_t *mem, void const *src, - uint32_t addr, uint32_t len); +void wasm_rt_memory_store(struct wasm_rt_memory_t *mem, uint32_t addr, + void const *src, uint32_t len); /** * Stores bytes to memory without bounds checking. */ -void wasm_rt_memory_store_unchecked(struct wasm_rt_memory_t *mem, - void const *src, uint32_t addr, - uint32_t len); +void wasm_rt_memory_store_unchecked(struct wasm_rt_memory_t *mem, uint32_t addr, + void const *src, uint32_t len); /** * Stores a 32-bit word to memory. -- cgit v1.2.3