diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-22 19:33:34 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-22 19:33:34 +0200 |
| commit | 76b91a038eb302e5513352c6ea7f781d374aaa62 (patch) | |
| tree | b0f8ab9b0f5b733a7607ec9550a152adb2a2cea3 | |
| parent | 131675bb507d96bea1b0ad20e0022d53171fad2a (diff) | |
Introduce Virtual File-System
PICO-8 carts write to a file-system when running carts, even ones
that don't do anything. From what I can tell, it records cart
changes to an `IndexDB` file-system in the Emscripten bundle, so
things like `CSTORE(...)` persist for a given cart.
It uses the same system calls for also:
- Reading the `data.pod` file, which contains some data for boot
- Writing logs to `stdout`, which we mimic by producing `SDL_Log`s
Persistance will be handled later, by reading and writing to a
UStar file (since they are simple, and allow us to keep all
cart-specific save data in one file).
None of this is hooked up to the `env.c` Emscripten glue code port.
| -rw-r--r-- | src/cart.c | 5 | ||||
| -rw-r--r-- | src/debug.c | 4 | ||||
| -rw-r--r-- | src/env.c | 2 | ||||
| -rw-r--r-- | src/fs.c | 516 | ||||
| -rw-r--r-- | src/fs.h | 182 | ||||
| -rw-r--r-- | tools/generate-sysroot.c | 6 |
6 files changed, 707 insertions, 8 deletions
@@ -190,8 +190,9 @@ struct pw_cart_png *pw_cart_png_init(SDL_Renderer *renderer) { goto error; } - union pw_png_pixel *image = pw_png_load( - unknown_p8_png.data, unknown_p8_png.size, PW_PNG_WIDTH, PW_PNG_HEIGHT); + union pw_png_pixel *image = + pw_png_load(pw_sysroot_unknown_p8_png.data, + pw_sysroot_unknown_p8_png.size, PW_PNG_WIDTH, PW_PNG_HEIGHT); if (!image) { goto error; } diff --git a/src/debug.c b/src/debug.c index b87aa31..f232490 100644 --- a/src/debug.c +++ b/src/debug.c @@ -42,8 +42,8 @@ struct pw_debug *pw_debug_init(SDL_Renderer *renderer) { SDL_DestroyPalette(palette); palette = NULL; - image = - pw_png_load(font_png.data, font_png.size, PW_FONT_WIDTH, PW_FONT_HEIGHT); + image = pw_png_load(pw_sysroot_font_png.data, pw_sysroot_font_png.size, + PW_FONT_WIDTH, PW_FONT_HEIGHT); if (!image) { goto error; } @@ -588,7 +588,7 @@ uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env, case 1: return (uint32_t)env->context.cartdat->count; case 2: - // Gets a byte from the embedded ROM data, note that we need to convert + // Gets setta byte from the embedded ROM data, note that we need to convert // the byte position to a cart index and offset within that cart. { uint32_t i = env->data.cartpos / PW_ROM_SIZE; diff --git a/src/fs.c b/src/fs.c new file mode 100644 index 0000000..72acde9 --- /dev/null +++ b/src/fs.c @@ -0,0 +1,516 @@ +#include <SDL3/SDL_error.h> +#include <SDL3/SDL_log.h> + +#include "fs.h" +#include "sysroot.h" + +#define BUFFER_SIZE 1024 + +static uint8_t empty_bytes[] = {0}; + +static bool init_output(union pw_fs_node *node) { + node->w.type = PW_FS_NODE_OUTPUT; + node->w.data = SDL_malloc(BUFFER_SIZE); + if (!node->w.data) { + return false; + } + node->w.size = 0; + node->w.capacity = BUFFER_SIZE; + return true; +} + +struct pw_fs *pw_fs_init(char const *tar) { + struct pw_fs *fs = SDL_calloc(1, sizeof(*fs)); + if (!fs) { + return NULL; + } + + fs->tar = SDL_strdup(tar); + if (!fs->tar) { + goto error; + } + + fs->root.entries = SDL_malloc(32 * sizeof(*fs->root.entries)); + if (!fs->root.entries) { + goto error; + } + fs->root.capacity = 32; + fs->root.entries[0] = SDL_calloc(1, sizeof(**fs->root.entries)); + if (!*fs->root.entries) { + goto error; + } + fs->root.count++; + SDL_strlcpy(fs->root.entries[0]->path, "data.pod", PW_FS_PATH_MAX); + fs->root.entries[0]->node.r.type = PW_FS_NODE_STATIC; + fs->root.entries[0]->node.r.data = pw_sysroot_pico8_data_pod.data; + fs->root.entries[0]->node.r.size = pw_sysroot_pico8_data_pod.size; + + // TODO: read the tar and populate root... + + fs->dev.in.r.type = PW_FS_NODE_STATIC; + fs->dev.in.r.data = empty_bytes; + fs->dev.in.r.size = 0; + if (!init_output(&fs->dev.out) || !init_output(&fs->dev.err)) { + goto error; + } + fs->streams[0].node = &fs->dev.in; + fs->streams[0].flags = PW_FS_STREAM_READ; + fs->streams[1].node = &fs->dev.out; + fs->streams[1].flags = PW_FS_STREAM_WRITE | PW_FS_STREAM_APPEND; + fs->streams[2].node = &fs->dev.err; + fs->streams[2].flags = PW_FS_STREAM_WRITE | PW_FS_STREAM_APPEND; + + return fs; + +error: + if (fs->root.entries) { + for (size_t i = 0; i < fs->root.count; ++i) { + SDL_free(fs->root.entries[i]); + } + SDL_free(fs->root.entries); + } + if (fs->dev.out.w.data) { + SDL_free(fs->dev.out.w.data); + } + if (fs->dev.err.w.data) { + SDL_free(fs->dev.err.w.data); + } + if (fs->tar) { + SDL_free(fs->tar); + } + SDL_free(fs); + return NULL; +} + +void pw_fs_free(struct pw_fs *fs) { + if (!fs) { + SDL_InvalidParamError("fs"); + return; + } + + if (fs->root.entries) { + for (size_t i = 0; i < fs->root.count; ++i) { + SDL_free(fs->root.entries[i]); + } + SDL_free(fs->root.entries); + } + SDL_free(fs->dev.out.w.data); + SDL_free(fs->dev.err.w.data); + + SDL_free(fs->tar); + SDL_free(fs); +} + +bool pw_fs_sync(struct pw_fs *fs) { + if (!fs) { + return SDL_InvalidParamError("fs"); + } + + // TODO write entries to `fs->tar` + return true; +} + +static int cmp_path(void const *a, void const *b) { + char const *path = a; + struct pw_fs_entry const *const *entry = b; + return SDL_strcmp(path, (*entry)->path); +} + +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), cmp_path); + if (!e) { + return NULL; + } + return *e; +} + +static int cmp_entry(void const *a, void const *b) { + struct pw_fs_entry const *const *ea = a; + struct pw_fs_entry const *const *eb = b; + return SDL_strcmp((*ea)->path, (*eb)->path); +} + +static struct pw_fs_entry *add_entry(struct pw_fs *fs, char const *path) { + struct pw_fs_entry *e = SDL_calloc(1, sizeof(*e)); + if (!e) { + fs->err = PW_ENOMEM; + return NULL; + } + + size_t n = SDL_strlcpy(e->path, path, PW_FS_PATH_MAX); + if (n >= PW_FS_PATH_MAX) { + fs->err = PW_EINVAL; + SDL_InvalidParamError("path"); + goto error; + } + + e->node.w.type = PW_FS_NODE_BUFFER; + e->node.w.data = SDL_malloc(BUFFER_SIZE); + if (!e->node.w.data) { + fs->err = PW_ENOMEM; + goto error; + } + e->node.w.size = 0; + e->node.w.capacity = BUFFER_SIZE; + + while (fs->root.count >= fs->root.capacity) { + size_t capacity = SDL_max(fs->root.capacity, 16) * 2; + if (capacity < fs->root.capacity) { + fs->err = PW_ENOMEM; + SDL_OutOfMemory(); + goto error; + } + + void *entries = + SDL_realloc(fs->root.entries, capacity * sizeof(*fs->root.entries)); + if (!entries) { + fs->err = PW_ENOMEM; + goto error; + } + + fs->root.entries = entries; + fs->root.capacity = capacity; + } + + fs->root.entries[fs->root.count++] = e; + SDL_qsort(fs->root.entries, fs->root.count, sizeof(*fs->root.entries), + cmp_entry); + + fs->dirty = true; + return e; + +error: + if (e->node.w.data) { + SDL_free(e->node.w.data); + } + SDL_free(e); + return NULL; +} + +bool pw_fs_open(struct pw_fs *fs, char const *path, enum pw_fs_open_flag flags, + pw_fd *fd) { + if (!fs) { + return SDL_InvalidParamError("fs"); + } + if (!path) { + fs->err = PW_EINVAL; + return SDL_InvalidParamError("path"); + } + if (!fd) { + fs->err = PW_EINVAL; + return SDL_InvalidParamError("fd"); + } + + int32_t f = 0; + for (; f < PW_FS_STREAM_MAX && fs->streams[f].node; ++f) { + } + if (f >= PW_FS_STREAM_MAX) { + fs->err = PW_EMFILE; + return SDL_SetError("too many open files"); + } + + struct pw_fs_stream *s = &fs->streams[f]; + switch (flags & PW_O_ACCMODE) { + case PW_O_RDONLY: + if (flags & PW_O_TRUNC) { + fs->err = PW_EINVAL; + return SDL_InvalidParamError("flags"); + } + s->flags = PW_FS_STREAM_READ; + break; + case PW_O_WRONLY: + s->flags = PW_FS_STREAM_WRITE; + break; + case PW_O_RDWR: + s->flags = PW_FS_STREAM_READ | PW_FS_STREAM_WRITE; + break; + default: + fs->err = PW_EINVAL; + return SDL_InvalidParamError("flags"); + } + if (flags & PW_O_APPEND) { + s->flags |= PW_FS_STREAM_APPEND; + } + + struct pw_fs_entry *e = find_entry(fs, path); + if (e && (flags & PW_O_CREAT) && (flags & PW_O_EXCL)) { + fs->err = PW_EEXIST; + return SDL_SetError("file already exists"); + } + if (!e && !(flags & PW_O_CREAT)) { + fs->err = PW_ENOENT; + return SDL_SetError("file does not exist"); + } + if (e && e->node.type == PW_FS_NODE_STATIC && + ((s->flags & PW_FS_STREAM_WRITE) || (flags & PW_O_TRUNC))) { + fs->err = PW_EACCES; + return SDL_SetError("cannot modify read-only file"); + } + if (e && e->node.type == PW_FS_NODE_OUTPUT && + (s->flags & PW_FS_STREAM_READ)) { + fs->err = PW_EACCES; + return SDL_SetError("cannot read write-only file"); + } + + if (!e && !(e = add_entry(fs, path))) { + return false; + } + + s->node = &e->node; + s->pos = 0; + if (flags & PW_O_TRUNC) { + e->node.w.size = 0; + } + + *fd = f; + return true; +} + +struct pw_fs_stream *get_stream(struct pw_fs *fs, pw_fd fd) { + if (!fs) { + SDL_InvalidParamError("fs"); + return NULL; + } + if (fd < 0 || fd >= PW_FS_STREAM_MAX || !fs->streams[fd].node) { + fs->err = PW_EBADF; + SDL_InvalidParamError("fd"); + return NULL; + } + + return &fs->streams[fd]; +} + +bool pw_fs_close(struct pw_fs *fs, pw_fd fd) { + struct pw_fs_stream *s = get_stream(fs, fd); + if (!s) { + return false; + } + + bool result = true; + + // We have some special handing for output streams (i.e. stdout and + // stderr) where we write a final log message in case there are + // pending bytes, even without a newline. + if (s->node->type == PW_FS_NODE_OUTPUT && s->node->w.size) { + if (s->node->w.data[s->node->w.size - 1] != '\n') { + uint8_t *buffer = pw_fs_write(fs, fd, 1); + if (!buffer) { + result = false; + goto close; + } + *buffer = '\n'; + } + result = pw_fs_flush(fs, fd); + } + +close: + SDL_zerop(s); + return result; +} + +bool pw_fs_seek(struct pw_fs *fs, pw_fd fd, int64_t offset, + enum pw_fs_seek_whence whence) { + struct pw_fs_stream *s = get_stream(fs, fd); + if (!s) { + return false; + } + + struct pw_fs_node_static *r = &s->node->r; + + size_t pos; + switch (whence) { + case PW_SEEK_SET: + pos = 0; + break; + case PW_SEEK_CUR: + pos = s->pos; + break; + case PW_SEEK_END: + pos = r->size; + break; + default: + fs->err = PW_EINVAL; + return SDL_InvalidParamError("whence"); + } + + if (offset > 0) { + uint64_t uoffset = (uint64_t)offset; + if (uoffset > SIZE_MAX - pos) { + fs->err = PW_EINVAL; + return SDL_InvalidParamError("offset"); + } + pos += (size_t)uoffset; + } else if (offset < 0) { + uint64_t uoffset = -(uint64_t)offset; + if (uoffset > pos) { + fs->err = PW_EINVAL; + return SDL_InvalidParamError("offset"); + } + pos -= (size_t)uoffset; + } + + s->pos = pos; + return true; +} + +uint8_t const *pw_fs_read(struct pw_fs *fs, pw_fd fd, uint32_t len, + uint32_t *sz) { + struct pw_fs_stream *s = get_stream(fs, fd); + if (!s) { + return NULL; + } + if (!sz) { + SDL_InvalidParamError("sz"); + return NULL; + } + + if (!(s->flags & PW_FS_STREAM_READ)) { + fs->err = PW_EBADF; + SDL_SetError("cannot read from write-only file stream"); + return NULL; + } + + struct pw_fs_node_static *r; + switch (s->node->type) { + case PW_FS_NODE_STATIC: + case PW_FS_NODE_BUFFER: + r = &s->node->r; + break; + default: + fs->err = PW_EBADF; + SDL_SetError("cannot read from write-only file entry"); + return NULL; + } + + // In case we seeked past the end of the file, read nothing. + if (s->pos >= r->size) { + *sz = 0; + return r->data + r->size; + } + + size_t end = s->pos + (size_t)len; + if (end < s->pos) { + end = SIZE_MAX; + } + end = SDL_min(end, r->size); + + uint8_t const *buffer = r->data + s->pos; + *sz = end - s->pos; + + s->pos = end; + return buffer; +} + +uint8_t *pw_fs_write(struct pw_fs *fs, pw_fd fd, uint32_t len) { + struct pw_fs_stream *s = get_stream(fs, fd); + if (!s) { + return NULL; + } + + if (!(s->flags & PW_FS_STREAM_WRITE)) { + fs->err = PW_EBADF; + SDL_SetError("cannot write to read-only file stream"); + } + + struct pw_fs_node_buffer *w; + switch (s->node->type) { + case PW_FS_NODE_BUFFER: + case PW_FS_NODE_OUTPUT: + w = &s->node->w; + break; + default: + fs->err = PW_EBADF; + SDL_SetError("cannot write to read-only file entry"); + return NULL; + } + + // With the append flag, always move to the end of the file. This + // matters when there are multiple file descriptors writing to the + // same file. + if (s->flags & PW_FS_STREAM_APPEND) { + s->pos = w->size; + } + + // If we write 0 bytes, just return right away, as we do not want + // to resize the buffer in case we have seeked past EOF. + if (!len) { + return w->data + w->capacity; + } + + size_t end = s->pos + (size_t)len; + if (end < s->pos) { + fs->err = PW_EFBIG; + SDL_SetError("write past maximum file size"); + return NULL; + } + + // Check if we need to grow our buffer. + if (end > w->capacity) { + size_t capacity = SDL_max(w->capacity * 2, end); + uint8_t *data = SDL_realloc(w->data, capacity); + if (!data) { + fs->err = PW_EFBIG; + return NULL; + } + + w->data = data; + w->capacity = capacity; + } + + // POSIX requires holes to be filled with 0s (in case the file was + // seeked past the end). + if (s->pos > w->size) { + SDL_memset(w->data + w->size, 0, s->pos - w->size); + } + + uint8_t *buffer = w->data + s->pos; + w->size = SDL_max(w->size, end); + s->pos = end; + + fs->dirty = true; + return buffer; +} + +bool pw_fs_flush(struct pw_fs *fs, pw_fd fd) { + struct pw_fs_stream *s = get_stream(fs, fd); + if (!s) { + return false; + } + + struct pw_fs_node_buffer *b; + switch (s->node->type) { + case PW_FS_NODE_OUTPUT: + b = &s->node->w; + break; + default: + return true; + } + + size_t offset = 0; + for (size_t i = 0; i < b->size; ++i) { + if (b->data[i] != '\n') { + continue; + } + + b->data[i] = '\0'; + SDL_Log("PICO-8 output: %s", b->data + offset); + offset = i + 1; + } + + b->size -= offset; + if (offset && b->size) { + SDL_memmove(b->data, b->data + offset, b->size); + } + + return true; +} + +enum pw_fs_errno pw_fs_errno(struct pw_fs *fs) { + if (!fs) { + SDL_InvalidParamError("fs"); + return PW_EOK; + } + + return fs->err; +} diff --git a/src/fs.h b/src/fs.h new file mode 100644 index 0000000..a214c4a --- /dev/null +++ b/src/fs.h @@ -0,0 +1,182 @@ +#ifndef PW_FS_H +#define PW_FS_H + +#include <SDL3/SDL_stdinc.h> + +#define PW_FS_PATH_MAX 100 +#define PW_FS_STREAM_MAX 16 + +/** + * File-system node. + */ +union pw_fs_node { + enum pw_fs_node_type { + PW_FS_NODE_STATIC, + PW_FS_NODE_BUFFER, + PW_FS_NODE_OUTPUT, + } type; + + struct pw_fs_node_static { + enum pw_fs_node_type type; + uint8_t const *data; + size_t size; + } r; + struct pw_fs_node_buffer { + enum pw_fs_node_type type; + uint8_t *data; + size_t size; + size_t capacity; + } w; +}; + +/** + * A file node. + */ +struct pw_fs_entry { + char path[PW_FS_PATH_MAX]; + union pw_fs_node node; +}; + +/** + * Special file-system standard input/output devices. + */ +struct pw_fs_dev { + union pw_fs_node in; + union pw_fs_node out; + union pw_fs_node err; +}; + +/** + * Mounted file-system entries. + */ +struct pw_fs_mount { + struct pw_fs_entry **entries; + size_t count; + size_t capacity; +}; + +/** + * An open file stream. + */ +struct pw_fs_stream { + union pw_fs_node *node; + size_t pos; + enum pw_fs_stream_flag { + PW_FS_STREAM_READ = 1 << 0, + PW_FS_STREAM_WRITE = 1 << 1, + PW_FS_STREAM_APPEND = 1 << 2, + } flags; +}; + +typedef int32_t pw_fd; + +/** + * PICO-8 compatible open flags. + * + * These correspond to the `musl` open flags that Emscripten is built with, + * only the ones supported by this mock file-system. + */ +enum pw_fs_open_flag { + PW_O_RDONLY = 00, + PW_O_WRONLY = 01, + PW_O_RDWR = 02, + PW_O_ACCMODE = 03, + PW_O_CREAT = 0100, + PW_O_EXCL = 0200, + PW_O_TRUNC = 01000, + PW_O_APPEND = 02000, +}; + +/** + * PICO-8 compatible seek position. + */ +enum pw_fs_seek_whence { + PW_SEEK_SET = 0, + PW_SEEK_CUR = 1, + PW_SEEK_END = 2, +}; + +/** + * PICO-8 file-system error number. + */ +enum pw_fs_errno { + PW_EOK = 0, + PW_ENOENT = 2, + PW_EBADF = 9, + PW_ENOMEM = 12, + PW_EACCES = 13, + PW_EEXIST = 17, + PW_EINVAL = 22, + PW_EMFILE = 24, + PW_EFBIG = 27, +}; + +/** + * A simple file-system for storing PICO-8 cart data. + */ +struct pw_fs { + char *tar; + bool dirty; + struct pw_fs_mount root; + struct pw_fs_dev dev; + struct pw_fs_stream streams[PW_FS_STREAM_MAX]; + enum pw_fs_errno err; +}; + +/** + * Loads a file-system from disk. + * + * File-systems are just stored as `tar` files. + */ +struct pw_fs *pw_fs_init(char const *tar); + +/** + * Frees resources associated with the file-system. + */ +void pw_fs_free(struct pw_fs *fs); + +/** + * Synchronizes the file-system to its persistent storage. + */ +bool pw_fs_sync(struct pw_fs *fs); + +/** + * Opens a file stream. + */ +bool pw_fs_open(struct pw_fs *fs, char const *path, enum pw_fs_open_flag flags, + pw_fd *fd); + +/** + * Closes a file stream. + */ +bool pw_fs_close(struct pw_fs *fs, pw_fd fd); + +/** + * Seek. + */ +bool pw_fs_seek(struct pw_fs *fs, pw_fd fd, int64_t offset, + enum pw_fs_seek_whence whence); + +/** + * Return a readonly buffer of size `*sz` to the contents of the file. It + * will be at most `len` in size. + */ +uint8_t const *pw_fs_read(struct pw_fs *fs, pw_fd fd, uint32_t len, + uint32_t *sz); + +/** + * Return a writable buffer of size `len` to the contents of the file. + */ +uint8_t *pw_fs_write(struct pw_fs *fs, pw_fd fd, uint32_t len); + +/** + * Flushes a file stream. + */ +bool pw_fs_flush(struct pw_fs *fs, pw_fd fd); + +/** + * Returns the error code of the last failed operation. + */ +enum pw_fs_errno pw_fs_errno(struct pw_fs *fs); + +#endif diff --git a/tools/generate-sysroot.c b/tools/generate-sysroot.c index ff81b2c..1acf35f 100644 --- a/tools/generate-sysroot.c +++ b/tools/generate-sysroot.c @@ -121,9 +121,9 @@ int main(int argc, char *argv[argc + 1]) { char const *name; char const *file; } entries[] = { - {"pico8_data_pod", "pico8.data.pod"}, - {"unknown_p8_png", "unknown.p8.png"}, - {"font_png", "font.png"}, + {"pw_sysroot_pico8_data_pod", "pico8.data.pod"}, + {"pw_sysroot_unknown_p8_png", "unknown.p8.png"}, + {"pw_sysroot_font_png", "font.png"}, }; for (size_t i = 0; i < sizeof entries / sizeof entries[0]; i++) { char file[PW_PATH_MAX]; |
