aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md3
-rw-r--r--src/env.c27
-rw-r--r--src/env.h5
-rw-r--r--src/game.c26
-rw-r--r--src/wasmrt.c20
-rw-r--r--src/wasmrt.h6
6 files changed, 72 insertions, 15 deletions
diff --git a/README.md b/README.md
index 04d5ef8..ca1d2c2 100644
--- a/README.md
+++ b/README.md
@@ -4,14 +4,13 @@ PICO-w provides a minimal launcher for the officially exported PICO-8 Wasm runti
## TODOs
-- [ ] Codereview
- [ ] Add clipboard support
- [ ] Improve audio error handling
- [x] Add `.p8.tar` support
- [x] Add `wget` shim for `.nfo` requests
- [ ] Implement clean cart shutdown (needs sample cart)
- Not sure this is possible, see: `shutdown.p8.png` cart
-- [ ] The Mythical Shutdown Menu Item
+- [x] The Mythical Shutdown Menu Item
- [ ] Add sysroot.tar recipe
- [ ] Building from pre-bundled sysroot
- [ ] The Legendary PS Vita Port
diff --git a/src/env.c b/src/env.c
index e64b3fb..fd67e98 100644
--- a/src/env.c
+++ b/src/env.c
@@ -116,6 +116,33 @@ bool pw_env_link(struct w2c_env *env, struct pw_env_ctx context) {
return true;
}
+bool pw_env_shutdown(struct w2c_env *env) {
+ if (!env) {
+ return SDL_InvalidParamError("env");
+ }
+
+ if (env->data.state.shutdown) {
+ return true;
+ }
+
+ // HERE BE DRAGONS... Taking a look at the Web Assembly module exported
+ // by PICO-8, it calls `env_emscripten_asm_iiiiiiiii(10, ...)` with the
+ // `request_shutdown` flag read from global memory offset `2220668`. For
+ // whatever reason, the `SHUTDOWN` menu flag is setting this location in
+ // memory to a non-zero value, but without calling the ASM const. In order
+ // to correctly support shutting down, we need to not only read the flag
+ // from the ASM const, but also this memory offset. This is tied to the
+ // PICO-8 version, so whenever it gets updated, just:
+ //
+ // grep -B2 'asm_const_iiiiiiiii' build/system/pico8.c
+ //
+ // And check the address that is being loaded for the last parameter to the
+ // state update ASM const call.
+
+ uint32_t value;
+ return wasm_rt_memory_load32_checked(&env->memory, 2220668, &value) && value;
+}
+
/**
* Convenience macro for writing to wasm memory and moving the wasm pointer
* forward for that amount.
diff --git a/src/env.h b/src/env.h
index 2b36773..bfe96ba 100644
--- a/src/env.h
+++ b/src/env.h
@@ -90,4 +90,9 @@ void pw_env_free(struct w2c_env *env);
*/
bool pw_env_link(struct w2c_env *env, struct pw_env_ctx context);
+/**
+ * Returns whether or not a shutdown is requested.
+ */
+bool pw_env_shutdown(struct w2c_env *env);
+
#endif
diff --git a/src/game.c b/src/game.c
index 41b53b0..ed5732b 100644
--- a/src/game.c
+++ b/src/game.c
@@ -115,14 +115,20 @@ bool pw_game_start(struct pw_game *game, struct pw_players *players,
w2c_pico8_0x5F_0x5Femscripten_environ_constructor(&game->instance);
- char const progname[] = "./picow";
- uint32_t arg0 = w2c_pico8_stackAlloc(&game->instance, 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);
+ char const *args[] = {"./picow", "-run"};
+ uint32_t argv = w2c_pico8_stackAlloc(
+ &game->instance, (SDL_arraysize(args) + 1) * sizeof(uint32_t));
+ wasm_rt_memory_store32(&game->env->memory,
+ argv + SDL_arraysize(args) * sizeof(uint32_t), 0);
+ for (size_t i = 0; i < SDL_arraysize(args); ++i) {
+ uint32_t sz = (uint32_t)SDL_strlen(args[i]) + 1;
+ uint32_t arg = w2c_pico8_stackAlloc(&game->instance, sz);
+ wasm_rt_memory_store(&game->env->memory, arg, args[i], sz);
+ wasm_rt_memory_store32(&game->env->memory, argv + i * sizeof(arg), arg);
+ }
- uint32_t status = w2c_pico8_0x5Fmain(&game->instance, 1, argv);
+ uint32_t status =
+ w2c_pico8_0x5Fmain(&game->instance, SDL_arraysize(args), argv);
wasm_rt_trap_unregister();
@@ -166,7 +172,7 @@ bool pw_game_iterate(struct pw_game *game) {
// In case we trapped for an `_exit` call, treat it as a shutdown
// request instead of a trap.
- if (game->env->data.state.shutdown) {
+ if (pw_env_shutdown(game->env)) {
goto shutdown;
}
return SDL_SetError("PICO-8 iteration execution trapped: %s",
@@ -192,7 +198,7 @@ mixdone:
wasm_rt_trap_unregister();
- if (game->env->data.state.shutdown) {
+ if (pw_env_shutdown(game->env)) {
goto shutdown;
}
@@ -216,7 +222,7 @@ shutdown:
SDL_LogWarn(0, "failed to reset pointer lock: %s", SDL_GetError());
}
- // TODO(nlordell): cleanly transition back to cart selection menu.
+ SDL_SetError("TODO(nlordell): implement clean shutdown");
return false;
}
diff --git a/src/wasmrt.c b/src/wasmrt.c
index 3074a75..2283d0b 100644
--- a/src/wasmrt.c
+++ b/src/wasmrt.c
@@ -72,12 +72,17 @@ void wasm_rt_trap_unregister(void) {
wasm_rt_trap_handler = NULL;
}
-void wasm_rt_memory_check(struct wasm_rt_memory_t const *mem, uint32_t addr,
- uint32_t len) {
+static inline bool memory_oob(struct wasm_rt_memory_t const *mem, uint32_t addr,
+ uint32_t len) {
// While memory address 0 is _technically_ allowed in Wasm, we reject it in
// our memory bounds checking, as nothing should ask us to write to it.
uint32_t end = addr + len;
- if (len && UNLIKELY(!addr || end < addr || end > mem->size)) {
+ return len && (!addr || end < addr || end > mem->size);
+}
+
+void wasm_rt_memory_check(struct wasm_rt_memory_t const *mem, uint32_t addr,
+ uint32_t len) {
+ if (UNLIKELY(memory_oob(mem, addr, len))) {
wasm_rt_trap(WASM_RT_TRAP_OOB);
}
}
@@ -123,6 +128,15 @@ uint32_t wasm_rt_memory_load32_unchecked(struct wasm_rt_memory_t const *mem,
return value;
}
+bool wasm_rt_memory_load32_checked(struct wasm_rt_memory_t const *mem,
+ uint32_t addr, uint32_t *value) {
+ if (UNLIKELY(memory_oob(mem, addr, sizeof(*value)))) {
+ return false;
+ }
+ *value = wasm_rt_memory_load32_unchecked(mem, addr);
+ return true;
+}
+
size_t wasm_rt_memory_loada(struct wasm_rt_memory_t const *mem, uint32_t addr,
char *buf, size_t sz) {
// This is overly pessimistic in case we try to load a string at the
diff --git a/src/wasmrt.h b/src/wasmrt.h
index 1bb8d95..e56d17e 100644
--- a/src/wasmrt.h
+++ b/src/wasmrt.h
@@ -208,6 +208,12 @@ uint32_t wasm_rt_memory_load32_unchecked(struct wasm_rt_memory_t const *mem,
uint32_t addr);
/**
+ * Loads a 32-bit word from memory, returning `false` if out of bounds.
+ */
+bool wasm_rt_memory_load32_checked(struct wasm_rt_memory_t const *mem,
+ uint32_t addr, uint32_t *value);
+
+/**
* Loads a NUL-terminated string from memory.
*
* It copies at most `sz - 1` bytes and ensures that `buf` always ends with