aboutsummaryrefslogtreecommitdiff
path: root/src/env.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/env.c')
-rw-r--r--src/env.c82
1 files changed, 66 insertions, 16 deletions
diff --git a/src/env.c b/src/env.c
index bac97e0..f60d43f 100644
--- a/src/env.c
+++ b/src/env.c
@@ -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