aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-27 16:25:33 +0100
committerNicholas Rodrigues Lordello <[email protected]>2026-10-27 16:30:52 +0100
commita7bf69b22a9b7559e40031b9d1ad16385e029511 (patch)
tree68be1a83cb3a20827a5c6605ea5de122ae76d8b6
parent8dda4698251ac7c724005a158d9c11e90a44c7e9 (diff)
Refactor Embedded Cart Loading
-rw-r--r--src/cart.c144
-rw-r--r--src/cart.h17
-rw-r--r--src/env.c49
-rw-r--r--src/fs.c5
-rw-r--r--src/game.c2
5 files changed, 145 insertions, 72 deletions
diff --git a/src/cart.c b/src/cart.c
index e4dbfbb..e960ed2 100644
--- a/src/cart.c
+++ b/src/cart.c
@@ -1,4 +1,5 @@
#include <SDL3/SDL_filesystem.h>
+#include <SDL3/SDL_log.h>
#include "cart.h"
#include "path.h"
@@ -168,6 +169,37 @@ void pw_cart_png_free(struct pw_cart_png *png) {
SDL_free(png);
}
+static bool decode_cart_png(uint8_t const *data, size_t size,
+ SDL_Texture *texture,
+ uint8_t rom[PW_CART_ROM_SIZE]) {
+ bool result = false;
+ union pw_png_pixel *image = NULL;
+
+ image = pw_png_load(data, size, PW_CART_PNG_WIDTH, PW_CART_PNG_HEIGHT);
+ if (!image) {
+ goto cleanup;
+ }
+
+ if (texture && !blit_image(image, texture)) {
+ goto cleanup;
+ }
+
+ // Read steganographically encoded cart ROM.
+ for (size_t i = 0; i < PW_CART_ROM_SIZE; i++) {
+ union pw_png_pixel px = image[i];
+ px.raw &= 0x03030303;
+ rom[i] = (px.ch.a << 6) | (px.ch.r << 4) | (px.ch.g << 2) | px.ch.b;
+ }
+
+ result = true;
+
+cleanup:
+ if (image) {
+ SDL_free(image);
+ }
+ return result;
+}
+
static bool load_cart_png(char const *path, SDL_Texture *texture,
uint8_t rom[PW_CART_ROM_SIZE]) {
@@ -178,7 +210,6 @@ static bool load_cart_png(char const *path, SDL_Texture *texture,
bool result = false;
uint8_t *data = NULL;
- union pw_png_pixel *image = NULL;
int64_t ssize = SDL_GetIOSize(png);
if (ssize < 0) {
@@ -200,28 +231,9 @@ static bool load_cart_png(char const *path, SDL_Texture *texture,
goto cleanup;
}
- image = pw_png_load(data, size, PW_CART_PNG_WIDTH, PW_CART_PNG_HEIGHT);
- if (!image) {
- goto cleanup;
- }
-
- if (texture && !blit_image(image, texture)) {
- goto cleanup;
- }
-
- // Read steganographically encoded cart ROM.
- for (size_t i = 0; i < PW_CART_ROM_SIZE; i++) {
- union pw_png_pixel px = image[i];
- px.raw &= 0x03030303;
- rom[i] = (px.ch.a << 6) | (px.ch.r << 4) | (px.ch.g << 2) | px.ch.b;
- }
-
- result = true;
+ result = decode_cart_png(data, size, texture, rom);
cleanup:
- if (image) {
- SDL_free(image);
- }
if (data) {
SDL_free(data);
}
@@ -242,6 +254,21 @@ bool pw_cart_png_load(struct pw_cart_file *file, struct pw_cart_png *png) {
return load_cart_png(file->path, png->texture, png->rom);
}
+static struct pw_fs_node_buffer *embedded_cart(struct pw_fs_entry *e,
+ char const **filename) {
+ static char const cart_dir[] = "user_data/carts/";
+ if (e->node.type != PW_FS_NODE_BUFFER ||
+ SDL_strstr(e->path, cart_dir) != e->path ||
+ !pw_hasext(e->path, ".p8.png")) {
+ return NULL;
+ }
+
+ if (filename) {
+ *filename = e->path + sizeof(cart_dir) - 1;
+ }
+ return &e->node.w;
+}
+
struct pw_cart *pw_cart_load(struct pw_cart_file *file,
struct pw_cart_png *png) {
if (!file) {
@@ -249,36 +276,75 @@ struct pw_cart *pw_cart_load(struct pw_cart_file *file,
return NULL;
}
- struct pw_cart *cart = SDL_calloc(1, sizeof(*cart));
+ struct pw_fs *storage = NULL;
+ struct pw_cart *cart = NULL;
+
+ // We are _guaranteed_ that `len < PW_PATH_MAX` by construction, and that
+ // it has a `.p8.png` suffix. This allows us to easily add the `.p8.tar`
+ // suffix for the user-data mount.
+ char storage_path[PW_CART_PATH_MAX];
+ size_t len = SDL_strlcpy(storage_path, file->path, PW_CART_PATH_MAX);
+ SDL_memcpy(storage_path + len - 3, "tar", 4);
+ storage = pw_fs_init(storage_path);
+ if (!storage) {
+ goto error;
+ }
+
+ // While `LOAD("CART.P8")` works with the filesystem, `RELOAD("CART.P8")`
+ // only works for embedded carts. In order to support all cases, we read
+ // through our storage and add any valid
+ size_t count = 1;
+ for (size_t i = 0; i < storage->root.count && count < PW_CART_EMBEDDED_MAX;
+ ++i) {
+ struct pw_fs_entry *e = storage->root.entries[i];
+ if (!embedded_cart(e, NULL)) {
+ continue;
+ }
+ ++count;
+ }
+ cart = SDL_calloc(1, sizeof(*cart) + count * sizeof(*cart->embedded));
if (!cart) {
- return NULL;
+ goto error;
}
- SDL_strlcpy(cart->name, file->name, PW_CART_NAME_MAX);
+ SDL_strlcpy(cart->embedded->name, file->name, PW_CART_NAME_MAX);
if (cart) {
- SDL_memcpy(cart->rom, png->rom, PW_CART_ROM_SIZE);
- } else if (!load_cart_png(file->path, NULL, cart->rom)) {
+ SDL_memcpy(cart->embedded->rom, png->rom, PW_CART_ROM_SIZE);
+ } else if (!load_cart_png(file->path, NULL, cart->embedded->rom)) {
goto error;
}
- // We are _guaranteed_ that `len < PW_PATH_MAX` by construction, and that
- // it has a `.p8.png` suffix. This allows us to easily add the `.p8.tar`
- // suffix for the user-data mount.
- char datpath[PW_CART_PATH_MAX];
- size_t len = SDL_strlcpy(datpath, file->path, PW_CART_PATH_MAX);
- SDL_memcpy(datpath + len - 3, "tar", 4);
- cart->dat = pw_fs_init(datpath);
- if (!cart->dat) {
- goto error;
+ for (size_t i = 0, j = 1; i < storage->root.count && j < count; ++i) {
+ struct pw_fs_entry *e = storage->root.entries[i];
+
+ char const *fn;
+ struct pw_fs_node_buffer *b = embedded_cart(e, &fn);
+ if (!b) {
+ continue;
+ }
+
+ struct pw_cart_data *d = cart->embedded + j;
+ if (!pw_stripext(d->name, PW_CART_NAME_MAX, fn, ".png") ||
+ !decode_cart_png(b->data, b->size, NULL, d->rom)) {
+ SDL_LogWarn(0, "malformed embedded cart %s", e->path);
+ goto error;
+ }
+
+ SDL_LogDebug(0, "embedding cart %s", d->name);
+ ++j;
}
+ cart->count = count;
+ cart->storage = storage;
return cart;
error:
- if (cart->dat) {
- pw_fs_free(cart->dat);
+ if (storage) {
+ pw_fs_free(storage);
+ }
+ if (cart) {
+ SDL_free(cart);
}
- SDL_free(cart);
return NULL;
}
@@ -288,6 +354,6 @@ void pw_cart_free(struct pw_cart *cart) {
return;
}
- pw_fs_free(cart->dat);
+ pw_fs_free(cart->storage);
SDL_free(cart);
}
diff --git a/src/cart.h b/src/cart.h
index 47ef77f..b43c6a0 100644
--- a/src/cart.h
+++ b/src/cart.h
@@ -11,6 +11,9 @@
/** The maximum supported PICO-8 cart name size. */
#define PW_CART_NAME_MAX 64
+/** The maximum number of embedded PICO-8 carts. */
+#define PW_CART_EMBEDDED_MAX 32
+
/** The size of a PICO-8 ROM. */
#define PW_CART_ROM_SIZE 32768
@@ -46,12 +49,20 @@ struct pw_cart_png {
};
/**
- * A PICO-8 cart, its loaded rom and mounted user data file-system.
+ * PICO-8 cart data.
*/
-struct pw_cart {
+struct pw_cart_data {
char name[PW_CART_NAME_MAX];
uint8_t rom[PW_CART_ROM_SIZE];
- struct pw_fs *dat;
+};
+
+/**
+ * A PICO-8 cart, its loaded embedded ROMs and mounted storage.
+ */
+struct pw_cart {
+ size_t count;
+ struct pw_fs *storage;
+ struct pw_cart_data embedded[];
};
/**
diff --git a/src/env.c b/src/env.c
index f109c5f..1c246ec 100644
--- a/src/env.c
+++ b/src/env.c
@@ -513,9 +513,9 @@ uint32_t w2c_env_0x5F_0x5Fsyscall140(struct w2c_env *env, uint32_t which,
uint32_t whence = env_varg(env, ap, 4);
int64_t pos;
- if (!pw_fs_seek(env->context.cart->dat, fd, offset, whence, &pos)) {
+ if (!pw_fs_seek(env->context.cart->storage, fd, offset, whence, &pos)) {
SDL_LogDebug(0, "error seeking file: %s", SDL_GetError());
- return -pw_fs_errno(env->context.cart->dat);
+ return -pw_fs_errno(env->context.cart->storage);
}
wasm_rt_memory_store64(&env->memory, result, pos);
@@ -548,9 +548,9 @@ uint32_t w2c_env_0x5F_0x5Fsyscall145(struct w2c_env *env, uint32_t which,
uint32_t len = wasm_rt_memory_load32(&env->memory, iov + 8 * i + 4);
uint32_t size;
uint8_t const *contents =
- pw_fs_read(env->context.cart->dat, fd, len, &size);
+ pw_fs_read(env->context.cart->storage, fd, len, &size);
if (!contents) {
- return -pw_fs_errno(env->context.cart->dat);
+ return -pw_fs_errno(env->context.cart->storage);
}
wasm_rt_memory_store(&env->memory, buffer, contents, size);
n += size;
@@ -582,15 +582,15 @@ uint32_t w2c_env_0x5F_0x5Fsyscall146(struct w2c_env *env, uint32_t which,
for (uint32_t i = 0; i < iovcnt; i++) {
uint32_t buffer = wasm_rt_memory_load32(&env->memory, iov + 8 * i);
uint32_t len = wasm_rt_memory_load32(&env->memory, iov + 8 * i + 4);
- uint8_t *contents = pw_fs_write(env->context.cart->dat, fd, len);
+ uint8_t *contents = pw_fs_write(env->context.cart->storage, fd, len);
if (!contents) {
- return -pw_fs_errno(env->context.cart->dat);
+ return -pw_fs_errno(env->context.cart->storage);
}
wasm_rt_memory_load(&env->memory, buffer, contents, len);
}
- if (!pw_fs_flush(env->context.cart->dat, fd)) {
- return -pw_fs_errno(env->context.cart->dat);
+ if (!pw_fs_flush(env->context.cart->storage, fd)) {
+ return -pw_fs_errno(env->context.cart->storage);
}
return (uint32_t)n;
@@ -634,9 +634,9 @@ uint32_t w2c_env_0x5F_0x5Fsyscall5(struct w2c_env *env, uint32_t which,
SDL_Log("OPEN: %s", path);
int32_t fd;
- if (!pw_fs_open(env->context.cart->dat, path, flags, &fd)) {
- SDL_Log("ERR: %d", -pw_fs_errno(env->context.cart->dat));
- return -pw_fs_errno(env->context.cart->dat);
+ if (!pw_fs_open(env->context.cart->storage, path, flags, &fd)) {
+ SDL_Log("ERR: %d", -pw_fs_errno(env->context.cart->storage));
+ return -pw_fs_errno(env->context.cart->storage);
}
SDL_Log("FD: %d", fd);
@@ -662,8 +662,8 @@ uint32_t w2c_env_0x5F_0x5Fsyscall6(struct w2c_env *env, uint32_t which,
SDL_Log("CLOSE %d", fd);
- if (!pw_fs_close(env->context.cart->dat, fd)) {
- return -pw_fs_errno(env->context.cart->dat);
+ if (!pw_fs_close(env->context.cart->storage, fd)) {
+ return -pw_fs_errno(env->context.cart->storage);
}
return 0;
}
@@ -683,27 +683,26 @@ uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env,
uint32_t code) {
switch (code) {
case 0:
- // Whether or not there is embedded cart data. Since we _only_ run with
- // embedded cart data, this is always 1 (i.e. true).
+ // Whether or not there is embedded cart data. Since we _always_ run
+ // with embedded cart data, this is always 1 (i.e. true).
return 1;
case 1:
- // We only ever include a single embedded cart.
- return 1;
+ return env->context.cart->count;
case 2: {
- uint32_t pos = env->data.cartpos++;
- if (pos < PW_CART_ROM_SIZE) {
- return env->context.cart->rom[pos];
- } else {
+ uint32_t index = env->data.cartpos / PW_CART_ROM_SIZE;
+ uint32_t pos = env->data.cartpos++ % PW_CART_ROM_SIZE;
+ if (index >= env->context.cart->count) {
return 0;
}
+ return env->context.cart->embedded[index].rom[pos];
}
case 4:
- if (!env->data.cartname.index &&
- env->data.cartname.pos < PW_CART_NAME_MAX) {
- return env->context.cart->name[env->data.cartname.pos++];
- } else {
+ if (env->data.cartname.index >= env->context.cart->count ||
+ env->data.cartname.pos >= PW_CART_NAME_MAX) {
return 0;
}
+ return env->context.cart->embedded[env->data.cartname.index]
+ .rom[env->data.cartname.pos++];
case 5:
// Browser history push state, no-op.
return 0;
diff --git a/src/fs.c b/src/fs.c
index e4a2d4e..6553810 100644
--- a/src/fs.c
+++ b/src/fs.c
@@ -189,10 +189,6 @@ struct pw_fs *pw_fs_init(char const *path) {
SDL_LogDebug(0, "using in-memory cart storage");
}
- for (size_t i = 0; i < fs->root.count; ++i) {
- SDL_LogDebug(0, "mounted %s", fs->root.entries[i]->path);
- }
-
fs->dev.in.r.type = PW_FS_NODE_STATIC;
fs->dev.in.r.data = empty_bytes;
fs->dev.in.r.size = 0;
@@ -349,6 +345,7 @@ static struct pw_fs_entry *find_entry(struct pw_fs *fs, char const *path,
struct pw_fs_entry **e = SDL_bsearch(path, fs->root.entries, fs->root.count,
sizeof(*fs->root.entries), compare);
+
if (!e) {
return NULL;
}
diff --git a/src/game.c b/src/game.c
index c1b52da..f740500 100644
--- a/src/game.c
+++ b/src/game.c
@@ -203,7 +203,7 @@ mixdone:
if (game->env->data.state.fssync) {
game->env->data.state.fssync = false;
- if (!pw_fs_sync(game->cart->dat)) {
+ if (!pw_fs_sync(game->cart->storage)) {
SDL_LogWarn(0, "failed to persist user storage: %s", SDL_GetError());
}
}