diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-28 22:44:04 +0100 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-28 22:44:04 +0100 |
| commit | 5db0d1702e9ba8b64d71967bbd651cb38b649cd7 (patch) | |
| tree | 4b1997a13ea649ac4022c01c6326abe749b68d31 | |
| parent | 756750924357cae5ea9df48af3f55e17a2fd90e2 (diff) | |
Implement Remaining Syscalls
| -rw-r--r-- | src/cart.c | 31 | ||||
| -rw-r--r-- | src/config.c | 1 | ||||
| -rw-r--r-- | src/env.c | 82 | ||||
| -rw-r--r-- | src/fb.c | 18 | ||||
| -rw-r--r-- | src/fb.h | 5 | ||||
| -rw-r--r-- | src/fs.c | 124 | ||||
| -rw-r--r-- | src/fs.h | 31 | ||||
| -rw-r--r-- | src/game.c | 1 | ||||
| -rw-r--r-- | src/input.c | 16 | ||||
| -rw-r--r-- | src/wasmrt.c | 5 | ||||
| -rw-r--r-- | src/wasmrt.h | 5 |
11 files changed, 273 insertions, 46 deletions
@@ -97,10 +97,10 @@ static bool blit_image(union pw_png_pixel const *image, SDL_Texture *texture) { return false; } + // Make sure that the surface dimensions match our expectations, and that + // the surface pixels buffer properly aligned. if (surface->w != PW_CART_PNG_WIDTH || surface->h != PW_CART_PNG_HEIGHT || - surface->pitch != PW_CART_PNG_WIDTH * 4 || - surface->format != SDL_PIXELFORMAT_RGBA32 || - (uintptr_t)surface->pixels % sizeof(uint32_t)) { + surface->format != SDL_PIXELFORMAT_RGBA32) { SDL_UnlockTexture(texture); SDL_SetError("invalid cart PNG surface"); return false; @@ -108,12 +108,16 @@ static bool blit_image(union pw_png_pixel const *image, SDL_Texture *texture) { // Copy over the raw image data to the surface, while clearing the // steganographic encoding of the cart data. - uint32_t *pixels = surface->pixels; - for (size_t i = 0; i < PW_CART_PNG_WIDTH * PW_CART_PNG_HEIGHT; i++) { - union pw_png_pixel px = image[i]; - px.raw &= 0xfcfcfcfc; - px.ch.a |= 0x03; - pixels[i] = px.raw; + for (size_t y = 0; y < PW_CART_PNG_HEIGHT; y++) { + union pw_png_pixel const *row = image + y * PW_CART_PNG_WIDTH; + uint8_t *pixels = (uint8_t *)surface->pixels + y * surface->pitch; + + for (size_t x = 0; x < PW_CART_PNG_WIDTH; x++) { + union pw_png_pixel px = row[x]; + px.raw &= 0xfcfcfcfc; + px.ch.a |= 0x03; + SDL_memcpy(pixels + x * sizeof(px), &px, sizeof(px)); + } } SDL_UnlockTexture(texture); @@ -294,12 +298,15 @@ struct pw_cart *pw_cart_load(struct pw_cart_file *file, // 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) { + for (size_t i = 0; i < storage->root.count; ++i) { struct pw_fs_entry *e = storage->root.entries[i]; if (!embedded_cart(e, NULL)) { continue; } + if (count >= PW_CART_EMBEDDED_MAX) { + SDL_LogWarn(0, "ignoring cart past embedding limit %s", e->path); + continue; + } ++count; } cart = SDL_calloc(1, sizeof(*cart) + count * sizeof(*cart->embedded)); @@ -308,7 +315,7 @@ struct pw_cart *pw_cart_load(struct pw_cart_file *file, } SDL_strlcpy(cart->embedded->name, file->name, PW_CART_NAME_MAX); - if (cart) { + if (png) { SDL_memcpy(cart->embedded->rom, png->rom, PW_CART_ROM_SIZE); } else if (!load_cart_png(file->path, NULL, cart->embedded->rom)) { goto error; diff --git a/src/config.c b/src/config.c index c16f525..944101b 100644 --- a/src/config.c +++ b/src/config.c @@ -5,6 +5,7 @@ // Windows), so detect whether or not it is there and avoid command line // parsing if it is not available. #if !defined(PW_NO_CLI) && (defined(__unix__) || defined(__APPLE__)) +#define _POSIX_C_SOURCE 200809L #include <unistd.h> #endif @@ -235,11 +235,6 @@ static inline SDL_NORETURN void env_abort() { wasm_rt_trap(WASM_RT_TRAP_UNREACHABLE); } -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 %" SDL_PRIu32, i, code); env_abort(); @@ -465,12 +460,16 @@ void w2c_env_0x5FSDL_UnlockSurface(struct w2c_env *env, uint32_t surface) { // to trap whitout properly unlocking it. uint32_t pixels = wasm_rt_memory_load32_unchecked(&env->memory, surface + 20); wasm_rt_memory_check(&env->memory, pixels, PW_FB_SIZE); - uint8_t *buffer = pw_fb_lock(env->context.fb); + size_t pitch; + uint8_t *buffer = pw_fb_lock(env->context.fb, &pitch); if (!buffer) { SDL_SetError("unable to lock framebuffer"); return; } - wasm_rt_memory_load_unchecked(&env->memory, pixels, buffer, PW_FB_SIZE); + for (size_t y = 0; y < PW_FB_HEIGHT; ++y) { + wasm_rt_memory_load_unchecked(&env->memory, pixels + y * PW_FB_STRIDE, + buffer + y * pitch, PW_FB_STRIDE); + } pw_fb_unlock(env->context.fb); } @@ -598,10 +597,42 @@ uint32_t w2c_env_0x5F_0x5Fsyscall146(struct w2c_env *env, uint32_t which, uint32_t w2c_env_0x5F_0x5Fsyscall195(struct w2c_env *env, uint32_t which, uint32_t ap) { - (void)env; (void)which; - (void)ap; - env_unimplemented("___syscall195"); + + char path[256]; + if (wasm_rt_memory_loada(&env->memory, env_varg(env, ap, 0), path, + sizeof(path)) >= sizeof(path)) { + return -PW_EINVAL; + } + uint32_t stat = env_varg(env, ap, 1); + + struct pw_fs_stat s; + if (!pw_fs_stat(env->context.cart->storage, path, &s)) { + return -pw_fs_errno(env->context.cart->storage); + } + + // Return mostly stubbed stat data, our file-system doesn't support most + // of the fields. If ever this causes issues with PICO-8 things (which I + // have not noticed for any cart), we can implement something. + + MEM_WRITE(32, env, stat, 0); // st_dev + MEM_WRITE(64, env, stat, 0); // st_ino + MEM_WRITE(32, env, stat, s.mode); + MEM_WRITE(32, env, stat, 0); // st_nlink + MEM_WRITE(32, env, stat, 0); // st_uid + MEM_WRITE(32, env, stat, 0); // st_gid + MEM_WRITE(32, env, stat, 0); // st_rdev + MEM_WRITE(64, env, stat, 0); // padding + MEM_WRITE(64, env, stat, s.size); + MEM_WRITE(32, env, stat, 0); // st_blksize + MEM_WRITE(32, env, stat, 0); // st_blocks + MEM_WRITE(64, env, stat, 0); // st_atime + MEM_WRITE(64, env, stat, 0); // st_mtime + MEM_WRITE(64, env, stat, 0); // st_ctime + MEM_WRITE(32, env, stat, 0); // unused[0] + MEM_WRITE(32, env, stat, 0); // unused[1] + + return 0; } uint32_t w2c_env_0x5F_0x5Fsyscall220(struct w2c_env *env, uint32_t which, @@ -609,15 +640,29 @@ uint32_t w2c_env_0x5F_0x5Fsyscall220(struct w2c_env *env, uint32_t which, (void)env; (void)which; (void)ap; - env_unimplemented("___syscall220"); + + // Our file-system does not support directories, so any file descriptor + // we get here is never a directory. + return -PW_ENOTDIR; } uint32_t w2c_env_0x5F_0x5Fsyscall221(struct w2c_env *env, uint32_t which, uint32_t ap) { - (void)env; (void)which; - (void)ap; - env_unimplemented("___syscall221"); + + int32_t fd = (int32_t)env_varg(env, ap, 0); + uint32_t op = env_varg(env, ap, 1); + // _Technically_ the vararg is defined for certain commands. However, + // fortunately, reading anywhere in Wasm memory is well-defined, with + // the caveat that the value can be gibberish. To simplify the function + // interface and our code, just read in unconditionally, even if the + // `pw_fs_cntl` will ignore its value when it is not passed in. + int32_t arg = (int32_t)env_varg(env, ap, 2); + + if (!pw_fs_cntl(env->context.cart->storage, fd, op, &arg)) { + return -pw_fs_errno(env->context.cart->storage); + } + return arg; } uint32_t w2c_env_0x5F_0x5Fsyscall5(struct w2c_env *env, uint32_t which, @@ -774,7 +819,9 @@ uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env, return (uint32_t)pw_input_key(env->context.input, NULL); case 38: { int32_t key; - pw_input_key(env->context.input, &key); + if (!pw_input_key(env->context.input, &key)) { + return -1; + } return (uint32_t)key; } case 39: { @@ -801,7 +848,10 @@ uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env, // do this as our SDL application handles them already; no-op. return 0; case 45: - return (uint32_t)pw_input_mouse(env->context.input, NULL); + // The touch PICO-8 button state, note this only ever considers left mouse + // button clicks, so mask appropriately. + return (uint32_t)(pw_input_mouse(env->context.input, NULL) & + PW_MOUSE_BUTTON_LEFT); case 46: case 48: // Machinery for creating directories, we have implicit directories in our @@ -114,24 +114,26 @@ bool pw_fb_coordinates(struct pw_framebuffer *fb, float wx, float wy, return true; } -uint8_t *pw_fb_lock(struct pw_framebuffer *fb) { +uint8_t *pw_fb_lock(struct pw_framebuffer *fb, size_t *pitch) { if (!fb) { SDL_InvalidParamError("fb"); return NULL; } - - void *buffer; - int pitch; - if (!SDL_LockTexture(fb->texture, NULL, &buffer, &pitch)) { + if (!pitch) { + SDL_InvalidParamError("pitch"); return NULL; } - if (pitch != PW_FB_WIDTH * PW_FB_BYTES_PER_PIXEL) { - SDL_UnlockTexture(fb->texture); - SDL_SetError("unexpected framebuffer pitch"); + void *buffer; + int ipitch; + if (!SDL_LockTexture(fb->texture, NULL, &buffer, &ipitch)) { return NULL; } + // This cast is necessarily safe, as it reprensents a memory offset + // from one row to the other, meaning it must fit in a `size_t`. + *pitch = (size_t)ipitch; + return buffer; } @@ -8,7 +8,8 @@ #define PW_FB_WIDTH 128 #define PW_FB_HEIGHT 128 #define PW_FB_BYTES_PER_PIXEL 4 -#define PW_FB_SIZE (PW_FB_WIDTH * PW_FB_HEIGHT * PW_FB_BYTES_PER_PIXEL) +#define PW_FB_STRIDE (PW_FB_WIDTH * PW_FB_BYTES_PER_PIXEL) +#define PW_FB_SIZE (PW_FB_STRIDE * PW_FB_HEIGHT) /** * A PICO-8 framebuffer. @@ -55,7 +56,7 @@ bool pw_fb_coordinates(struct pw_framebuffer *fb, float wx, float wy, /** * Locks the framebuffer for writing. */ -uint8_t *pw_fb_lock(struct pw_framebuffer *fb); +uint8_t *pw_fb_lock(struct pw_framebuffer *fb, size_t *pitch); /** * Unlocks the framebuffer for writing. @@ -184,8 +184,21 @@ struct pw_fs *pw_fs_init(char const *path) { pw_tar_free(tar); tar = NULL; + + fs->dirty = false; + } else if (path && SDL_GetPathInfo(path, NULL)) { + SDL_LogWarn(0, "corrupt cart storage: %s", SDL_GetError()); + + // In case of a corrupt cart storage, do not clobber it (in case it is + // a file intended for something else, and instead fallback to in-memory + // storage; just to be sure. + SDL_free(fs->path); + fs->path = NULL; + + fs->dirty = false; } else if (path) { - SDL_LogWarn(0, "could not open cart storage: %s", SDL_GetError()); + SDL_LogDebug(0, "initializing cart storage"); + fs->dirty = true; } else { SDL_LogDebug(0, "using in-memory cart storage"); } @@ -339,13 +352,18 @@ static int cmp_cart_path(void const *a, void const *b) { static struct pw_fs_entry *find_entry(struct pw_fs *fs, char const *path, bool absolute) { - SDL_CompareCallback compare = cmp_path; - if (!absolute && (pw_hasext(path, ".p8") || pw_hasext(path, ".p8.png"))) { - compare = cmp_cart_path; - } - struct pw_fs_entry **e = SDL_bsearch(path, fs->root.entries, fs->root.count, - sizeof(*fs->root.entries), compare); + sizeof(*fs->root.entries), cmp_path); + + // Under some conditions, PICO-8 will `LOAD("CART.P8")` by opening a file + // instead of using the embedded cart. To support this, for relative cart + // paths look in the `user_data/carts/` directory if we didn't find the + // file under root. + if (!e && !absolute && + (pw_hasext(path, ".p8") || pw_hasext(path, ".p8.png"))) { + e = SDL_bsearch(path, fs->root.entries, fs->root.count, + sizeof(*fs->root.entries), cmp_cart_path); + } if (!e) { return NULL; @@ -476,6 +494,52 @@ bool pw_fs_open(struct pw_fs *fs, char const *path, enum pw_fs_open_flag flags, return true; } +#define PW_S_IFCHR 0020000 +#define PW_S_IFREG 0100000 + +bool pw_fs_stat(struct pw_fs *fs, char const *path, struct pw_fs_stat *stat) { + char const *rawpath = path; + + if (!fs) { + return SDL_InvalidParamError("fs"); + } + if (!normalize_path(&path)) { + fs->err = PW_EINVAL; + return SDL_InvalidParamError("path"); + } + if (!stat) { + fs->err = PW_EINVAL; + return SDL_InvalidParamError("stat"); + } + + bool absolute = path != rawpath; + struct pw_fs_entry *e = find_entry(fs, path, absolute); + if (!e) { + fs->err = PW_ENOENT; + return SDL_SetError("file does not exist"); + } + + switch (e->node.type) { + case PW_FS_NODE_STATIC: + stat->mode = PW_S_IFCHR | 0444; + stat->size = 0; + break; + case PW_FS_NODE_BUFFER: + stat->mode = PW_S_IFREG | 0666; + stat->size = (uint64_t)SDL_min(e->node.w.size, UINT64_MAX); + break; + case PW_FS_NODE_OUTPUT: + stat->mode = PW_S_IFCHR | 0222; + stat->size = 0; + break; + default: + fs->err = PW_ENOENT; + return false; + } + + return true; +} + static struct pw_fs_stream *get_stream(struct pw_fs *fs, pw_fd fd) { if (!fs) { SDL_InvalidParamError("fs"); @@ -587,6 +651,52 @@ bool pw_fs_seek(struct pw_fs *fs, pw_fd fd, int64_t offset, return true; } +bool pw_fs_cntl(struct pw_fs *fs, pw_fd fd, enum pw_fs_cntl_op op, + int32_t *arg) { + struct pw_fs_stream *s = get_stream(fs, fd); + if (!s) { + return false; + } + if (!arg) { + fs->err = PW_EINVAL; + return false; + } + + switch (op) { + case PW_F_GETFL: { + enum pw_fs_open_flag f; + if (s->flags & PW_FS_STREAM_READ && s->flags & PW_FS_STREAM_WRITE) { + f = PW_O_RDWR; + } else if (s->flags & PW_FS_STREAM_READ) { + f = PW_O_RDONLY; + } else if (s->flags & PW_FS_STREAM_WRITE) { + f = PW_O_WRONLY; + } else { + fs->err = PW_EBADF; + return SDL_SetError("invalid file stream flags"); + } + if (s->flags & PW_FS_STREAM_APPEND) { + f |= PW_O_APPEND; + } + + *arg = (int32_t)f; + return true; + } + case PW_F_SETFL: + if (*arg & PW_O_APPEND) { + s->flags |= PW_FS_STREAM_APPEND; + } else { + s->flags &= ~PW_FS_STREAM_APPEND; + } + + *arg = 0; + return true; + default: + fs->err = PW_EINVAL; + return SDL_SetError("invalid file descriptor operation %d", op); + } +} + 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); @@ -88,6 +88,14 @@ enum pw_fs_open_flag { }; /** + * PICO-8 file stat. + */ +struct pw_fs_stat { + uint32_t mode; + uint64_t size; +}; + +/** * PICO-8 compatible seek position. */ enum pw_fs_seek_whence { @@ -97,6 +105,14 @@ enum pw_fs_seek_whence { }; /** + * PICO-8 file descriptor operation. + */ +enum pw_fs_cntl_op { + PW_F_GETFL = 3, + PW_F_SETFL = 4, +}; + +/** * PICO-8 file-system error number. */ enum pw_fs_errno { @@ -106,6 +122,7 @@ enum pw_fs_errno { PW_ENOMEM = 12, PW_EACCES = 13, PW_EEXIST = 17, + PW_ENOTDIR = 20, PW_EINVAL = 22, PW_EMFILE = 24, PW_ENOTTY = 25, @@ -149,6 +166,11 @@ bool pw_fs_open(struct pw_fs *fs, char const *path, enum pw_fs_open_flag flags, pw_fd *fd); /** + * Stats a file. + */ +bool pw_fs_stat(struct pw_fs *fs, char const *path, struct pw_fs_stat *stat); + +/** * Closes a file stream. */ bool pw_fs_close(struct pw_fs *fs, pw_fd fd); @@ -160,6 +182,15 @@ bool pw_fs_seek(struct pw_fs *fs, pw_fd fd, int64_t offset, enum pw_fs_seek_whence whence, int64_t *newpos); /** + * Modify a file descriptor. + * + * `arg` is used for both passing in an argument for an operation and + * returning a result for one. + */ +bool pw_fs_cntl(struct pw_fs *fs, pw_fd fd, enum pw_fs_cntl_op op, + int32_t *arg); + +/** * Return a readonly buffer of size `*sz` to the contents of the file. It * will be at most `len` in size. */ @@ -67,6 +67,7 @@ void pw_game_free(struct pw_game *game) { pw_input_free(game->input); pw_env_free(game->env); if (game->cart) { + pw_fs_sync(game->cart->storage); pw_cart_free(game->cart); } SDL_free(game); diff --git a/src/input.c b/src/input.c index 945c798..fcedb1f 100644 --- a/src/input.c +++ b/src/input.c @@ -66,6 +66,14 @@ static bool handle_connection(struct pw_input *input, static void set_button(enum pw_button *player, enum pw_button button, bool pressed) { + // Note that we map several different controls (axis and buttons) to the + // same PICO-8 button, meaning that they can compete for the 'set' state + // of these buttons. We opt to, for simplicity, to just take the value + // of the last control that set this state. This means that if you press + // both buttons for X, and release one of them, PICO-8 will think that + // the button was released (even while the second button was held). We + // are OK with this for now, and will consider changing this in case it + // ends up feeling unatural while playing. if (pressed) { *player |= button; } else { @@ -242,6 +250,14 @@ static bool handle_key(struct pw_input *input, SDL_KeyboardEvent *event) { } sc |= (uint32_t)event->mod << PW_SCANCODE_MOD_SHIFT; + // The scancode and key queue size was selected to not be an issue under + // regular use, PICO-8 seems to drain many of the queue items ever frame + // so I do not believe we will run into issues where the queue is full + // in practice (which would manifest as dropped keys and potentially + // keyboard state in PICO-8 drifting from the real keyboard state). Warn + // in case either queue is full, and if this proves to be an issue in + // practice consider either making the queue bigger or dynamically sized. + if (!enqueue_item(&input->keyboard.scancodes, sc)) { SDL_LogDebug(0, "keyboard event queue full, dropping scancode"); } diff --git a/src/wasmrt.c b/src/wasmrt.c index 2fda952..3074a75 100644 --- a/src/wasmrt.c +++ b/src/wasmrt.c @@ -126,7 +126,10 @@ uint32_t wasm_rt_memory_load32_unchecked(struct wasm_rt_memory_t const *mem, 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 - // end of our memory region, but it is OK for now. + // end of our memory region, and cause the bounds check to fail for + // a valid string that ends before the end of the buffer. This does + // not appear to be an issue for now, and can be implemented more + // more correctly in case we ever run into issues. wasm_rt_memory_check(mem, addr, sz); return wasm_rt_memory_loada_unchecked(mem, addr, buf, sz); } diff --git a/src/wasmrt.h b/src/wasmrt.h index b0dcddc..1bb8d95 100644 --- a/src/wasmrt.h +++ b/src/wasmrt.h @@ -27,6 +27,11 @@ * * Besides, no platforms with these extra features supported, you are probably * better off using PICO-8 directly anyway. + * + * Note that we explicitely disable stack depth checks. This _may_ cause stack + * overflows on systems with a small stack space. We should consider turning + * this on in case we ever observe this issue. That being said, if we do get + * into a situation where we observe a stack overflow, the game crashed anyway. */ #define WASM_RT_MEMCHECK_GUARD_PAGES 0 |
