aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-29 11:31:08 +0100
committerNicholas Rodrigues Lordello <[email protected]>2026-10-29 11:31:08 +0100
commitf9950fce6f7b7a3743920f0d787fb1857aa05769 (patch)
tree96ee578fcbc11f30026f36cd19215cebf0afb081
parent668594a0bedec468e976a38bbf9eac871e1353de (diff)
Implement Cart Shutdown
-rw-r--r--README.md13
-rw-r--r--src/env.c122
-rw-r--r--src/env.h11
-rw-r--r--src/game.c107
-rw-r--r--src/game.h2
-rw-r--r--src/input.c7
-rw-r--r--src/picow.c7
7 files changed, 147 insertions, 122 deletions
diff --git a/README.md b/README.md
index 9eeefa5..c61aed3 100644
--- a/README.md
+++ b/README.md
@@ -1,16 +1,3 @@
# PICO-w
PICO-w provides a minimal launcher for the officially exported PICO-8 Wasm runtime. This enables support on other platforms (such as homebrew) by running the official PICO-8 as a Wasm program instead of emulating it.
-
-## TODOs
-
-- [x] 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
-- [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 de2bf9d..0113665 100644
--- a/src/env.c
+++ b/src/env.c
@@ -116,11 +116,51 @@ bool pw_env_link(struct w2c_env *env, struct pw_env_ctx context) {
return true;
}
-bool pw_env_shutdown(struct w2c_env *env) {
+bool pw_env_start(struct w2c_env *env) {
if (!env) {
return SDL_InvalidParamError("env");
}
+ struct wasm_rt_trap_handler_t handler;
+ if (!wasm_rt_trap_register(&handler)) {
+ return false;
+ }
+
+ if (wasm_rt_trapped(&handler)) {
+ return SDL_SetError("PICO-8 startup execution trapped: %s",
+ wasm_rt_trap_str(handler.code));
+ }
+
+ w2c_pico8_0x5F_0x5Femscripten_environ_constructor(env->context.instance);
+
+ char const *args[] = {"./picow", "-run"};
+ uint32_t argv = w2c_pico8_stackAlloc(
+ env->context.instance, (SDL_arraysize(args) + 1) * sizeof(uint32_t));
+ wasm_rt_memory_store32(&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(env->context.instance, sz);
+ wasm_rt_memory_store(&env->memory, arg, args[i], sz);
+ wasm_rt_memory_store32(&env->memory, argv + i * sizeof(arg), arg);
+ }
+
+ uint32_t status =
+ w2c_pico8_0x5Fmain(env->context.instance, SDL_arraysize(args), argv);
+
+ wasm_rt_trap_unregister();
+
+ if (status) {
+ return SDL_SetError("main returned exit code");
+ }
+ if (!env->data.mainloop) {
+ return SDL_SetError("cannot continue without main loop set");
+ }
+
+ return true;
+}
+
+static bool env_shutdown(struct w2c_env *env) {
if (env->data.state.shutdown) {
return true;
}
@@ -143,6 +183,86 @@ bool pw_env_shutdown(struct w2c_env *env) {
return wasm_rt_memory_load32_checked(&env->memory, 2220668, &value) && value;
}
+bool pw_env_iterate(struct w2c_env *env, bool *shutdown) {
+ if (!env || env_shutdown(env)) {
+ return SDL_InvalidParamError("env");
+ }
+ if (!shutdown) {
+ return SDL_InvalidParamError("shutdown");
+ }
+
+ struct wasm_rt_trap_handler_t handler;
+ if (!wasm_rt_trap_register(&handler)) {
+ return false;
+ }
+
+ if (wasm_rt_trapped(&handler)) {
+ // Make sure to reset any leftover invocation frames in case of
+ // a trap, as they point to invalid stack addresses.
+ env->data.frame = NULL;
+
+ // In case we trapped for an `_exit` call, treat it as a shutdown
+ // request instead of a trap.
+ if (env_shutdown(env)) {
+ *shutdown = true;
+ return true;
+ }
+
+ return SDL_SetError("PICO-8 iteration execution trapped: %s",
+ wasm_rt_trap_str(handler.code));
+ }
+
+ w2c_pico8_dynCall_v(env->context.instance, env->data.mainloop);
+
+ if (pw_audio_ready(env->context.audio)) {
+ uint32_t len;
+ int16_t *data = pw_audio_lock(env->context.audio, &len);
+ if (!data) {
+ SDL_LogWarn(0, "unable to lock audio device: %s", SDL_GetError());
+ goto mixdone;
+ }
+ if (!len) {
+ goto mixdone;
+ }
+
+ uint32_t samples = w2c_pico8_0x5Fmix_0(env->context.instance, len, len);
+ wasm_rt_memory_load16v(&env->memory, samples, (uint16_t *)data, len);
+ if (!pw_audio_unlock(env->context.audio)) {
+ SDL_LogWarn(0, "unable to unlock audio device: %s", SDL_GetError());
+ goto mixdone;
+ }
+ }
+mixdone:
+
+ wasm_rt_trap_unregister();
+
+ *shutdown = env_shutdown(env);
+
+ bool pointerlock;
+ bool fssync;
+ if (*shutdown) {
+ // Always reset pointer lock and sync storage on shutdown.
+ pointerlock = false;
+ fssync = true;
+ } else {
+ pointerlock = env->data.state.pointerlock;
+ env->data.state.pointerlock = false;
+ fssync = env->data.state.fssync;
+ env->data.state.fssync = false;
+ }
+ if (!pw_input_set_pointer_lock(env->context.input, pointerlock)) {
+ SDL_LogWarn(0, "failed to set pointer lock: %s", SDL_GetError());
+ }
+ if (fssync && !pw_fs_sync(env->context.cart->storage)) {
+ SDL_LogWarn(0, "failed to persist user storage: %s", SDL_GetError());
+ }
+ if (*shutdown && !pw_audio_stop(env->context.audio)) {
+ SDL_LogWarn(0, "failed to stop audio: %s", SDL_GetError());
+ }
+
+ return true;
+}
+
/**
* 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 a25fec0..3acd3f2 100644
--- a/src/env.h
+++ b/src/env.h
@@ -92,8 +92,15 @@ 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.
+ * Starts the Wasm module.
*/
-bool pw_env_shutdown(struct w2c_env *env);
+bool pw_env_start(struct w2c_env *env);
+
+/**
+ * Progresses the Wasm module by a frame.
+ *
+ * `shutdown` is set to true when the cart exits.
+ */
+bool pw_env_iterate(struct w2c_env *env, bool *shutdown);
#endif
diff --git a/src/game.c b/src/game.c
index ed5732b..312c362 100644
--- a/src/game.c
+++ b/src/game.c
@@ -1,7 +1,6 @@
#include <SDL3/SDL_log.h>
#include "game.h"
-#include "pico8.h"
struct pw_game *pw_game_init(SDL_Renderer *renderer, enum pw_scale_mode mode,
size_t memory) {
@@ -103,43 +102,7 @@ bool pw_game_start(struct pw_game *game, struct pw_players *players,
}
wasm2c_pico8_instantiate(&game->instance, game->env, NULL);
- struct wasm_rt_trap_handler_t handler;
- if (!wasm_rt_trap_register(&handler)) {
- return false;
- }
-
- if (wasm_rt_trapped(&handler)) {
- return SDL_SetError("PICO-8 startup execution trapped: %s",
- wasm_rt_trap_str(handler.code));
- }
-
- w2c_pico8_0x5F_0x5Femscripten_environ_constructor(&game->instance);
-
- 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, SDL_arraysize(args), argv);
-
- wasm_rt_trap_unregister();
-
- if (status) {
- return SDL_SetError("main returned exit code");
- }
- if (!game->env->data.mainloop) {
- return SDL_SetError("cannot continue without main loop set");
- }
-
- return true;
+ return pw_env_start(game->env);
}
bool pw_game_event(struct pw_game *game, struct pw_players *players,
@@ -151,7 +114,7 @@ bool pw_game_event(struct pw_game *game, struct pw_players *players,
return pw_input_event(game->input, players, event);
}
-bool pw_game_iterate(struct pw_game *game) {
+bool pw_game_iterate(struct pw_game *game, bool *shutdown) {
if (!game) {
SDL_InvalidParamError("game");
return false;
@@ -159,71 +122,7 @@ bool pw_game_iterate(struct pw_game *game) {
pw_fb_iterate(game->fb);
pw_input_iterate(game->input, game->fb);
-
- struct wasm_rt_trap_handler_t handler;
- if (!wasm_rt_trap_register(&handler)) {
- return false;
- }
-
- if (wasm_rt_trapped(&handler)) {
- // Make sure to reset any leftover invocation frames in case of
- // a trap, as they point to invalid stack addresses.
- game->env->data.frame = NULL;
-
- // In case we trapped for an `_exit` call, treat it as a shutdown
- // request instead of a trap.
- if (pw_env_shutdown(game->env)) {
- goto shutdown;
- }
- return SDL_SetError("PICO-8 iteration execution trapped: %s",
- wasm_rt_trap_str(handler.code));
- }
-
- w2c_pico8_dynCall_v(&game->instance, game->env->data.mainloop);
-
- // TODO(nlordell): we need to improve error handling here, right now
- // it just silently swallows errors.
- if (pw_audio_ready(game->audio)) {
- uint32_t len;
- int16_t *data = pw_audio_lock(game->audio, &len);
- if (!data || !len) {
- goto mixdone;
- }
-
- uint32_t samples = w2c_pico8_0x5Fmix_0(&game->instance, len, len);
- wasm_rt_memory_load16v(&game->env->memory, samples, (uint16_t *)data, len);
- pw_audio_unlock(game->audio);
- }
-mixdone:
-
- wasm_rt_trap_unregister();
-
- if (pw_env_shutdown(game->env)) {
- goto shutdown;
- }
-
- if (!pw_input_set_pointer_lock(game->input,
- game->env->data.state.pointerlock)) {
- SDL_LogWarn(0, "failed to lock pointer: %s", SDL_GetError());
- game->env->data.state.pointerlock = pw_input_get_pointer_lock(game->input);
- }
-
- if (game->env->data.state.fssync) {
- game->env->data.state.fssync = false;
- if (!pw_fs_sync(game->cart->storage)) {
- SDL_LogWarn(0, "failed to persist user storage: %s", SDL_GetError());
- }
- }
-
- return true;
-
-shutdown:
- if (!pw_input_set_pointer_lock(game->input, false)) {
- SDL_LogWarn(0, "failed to reset pointer lock: %s", SDL_GetError());
- }
-
- SDL_SetError("TODO(nlordell): implement clean shutdown");
- return false;
+ return pw_env_iterate(game->env, shutdown);
}
bool pw_game_render(struct pw_game *game) { return pw_fb_render(game->fb); }
diff --git a/src/game.h b/src/game.h
index 6f2c98b..a6ce202 100644
--- a/src/game.h
+++ b/src/game.h
@@ -51,7 +51,7 @@ bool pw_game_event(struct pw_game *game, struct pw_players *players,
/**
* Iterate a new frame for the game state.
*/
-bool pw_game_iterate(struct pw_game *game);
+bool pw_game_iterate(struct pw_game *game, bool *shutdown);
/**
* Renders the game state.
diff --git a/src/input.c b/src/input.c
index 5e0ee27..eec088e 100644
--- a/src/input.c
+++ b/src/input.c
@@ -36,6 +36,10 @@ void pw_input_free(struct pw_input *input) {
SDL_free(input);
}
+static void clear_queue(struct pw_keyboard_queue *queue) {
+ queue->start = queue->end = 0;
+}
+
bool pw_input_reset(struct pw_input *input, struct pw_players *players) {
if (!input) {
return SDL_InvalidParamError("input");
@@ -48,6 +52,9 @@ bool pw_input_reset(struct pw_input *input, struct pw_players *players) {
input->nplayers = pw_players_status(players, connected);
SDL_zeroa(input->players);
+ clear_queue(&input->keyboard.scancodes);
+ clear_queue(&input->keyboard.scancodes);
+
return true;
}
diff --git a/src/picow.c b/src/picow.c
index 97e9539..b6f655e 100644
--- a/src/picow.c
+++ b/src/picow.c
@@ -172,9 +172,14 @@ SDL_AppResult SDL_AppIterate(void *appstate) {
}
if (state->scene == PW_SCENE_GAME) {
- if (!pw_game_iterate(state->game)) {
+ bool shutdown;
+ if (!pw_game_iterate(state->game, &shutdown)) {
return failf("failed to update PICO-8 game state");
}
+
+ if (shutdown) {
+ state->scene = PW_SCENE_CART_SELECTION;
+ }
}
bool ok = true;