aboutsummaryrefslogtreecommitdiff
path: root/src/env.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/env.c')
-rw-r--r--src/env.c443
1 files changed, 421 insertions, 22 deletions
diff --git a/src/env.c b/src/env.c
index 6d5078a..4581195 100644
--- a/src/env.c
+++ b/src/env.c
@@ -1,30 +1,70 @@
#include <math.h>
#include <SDL3/SDL_error.h>
+#include <SDL3/SDL_log.h>
#include <SDL3/SDL_stdinc.h>
#include <SDL3/SDL_timer.h>
#include "env.h"
+#include "pico8.h"
+
+// PICO-8 allocates a fix 128 Mb heap unconditinoally; but for some platforms,
+// that may be too much. Specify a lower bound, in 64Kb pages, for the memory
+// that we allocate in our Web Assembly environment. This _technically_ does
+// not respect the module's minimum memory size.
+#define ENV_MIN_PAGES 512
+
+#define ENV_DYNAMICTOP_PTR 3199024
+#define ENV_DYNAMIC_BASE 8441936
+
+#define ENV_ERROR_MAX_SIZE 256
+
+#define ENV_KEYDOWN 0x300
+#define ENV_KEYUP 0x301
+#define ENV_MOUSEMOTION 0x400
+
+// PICO-8 engine is compiled with the Emscripten built-in SDL1.3 library, which
+// has a different pixel format encoding than SDL3 which is used on the host.
+// The pixel format used by the engine is:
+#define ENV_PIXELFORMAT_ABGR8888 0x86762004
struct w2c_env *pw_env_init(size_t memory) {
- if (memory < 24 || memory > 128) {
+ size_t pages = (memory << 20) / wasm2c_pico8_pagesize_env_memory;
+ if (pages < ENV_MIN_PAGES || pages > wasm2c_pico8_max_env_memory) {
SDL_InvalidParamError("memory");
return NULL;
}
- size_t pages = ((memory << 20) + wasm2c_pico8_pagesize_env_memory - 1) /
- wasm2c_pico8_pagesize_env_memory;
- size_t memory_size = pages * wasm2c_pico8_pagesize_env_memory;
- struct w2c_env *env = SDL_malloc(sizeof(*env) + memory_size);
+
+ struct w2c_env *env = SDL_calloc(1, sizeof(*env));
if (!env) {
return NULL;
}
- env->memory.data = (uint8_t *)env + sizeof(*env);
- env->memory.data_end = env->memory.data + memory_size;
- env->memory.size = memory_size;
- SDL_zerop(&env->context);
+ env->memory.data = SDL_calloc(pages, wasm2c_pico8_pagesize_env_memory);
+ if (!env->memory.data) {
+ goto error;
+ }
+ env->memory.size = pages * wasm2c_pico8_pagesize_env_memory;
+ env->memory.data_end = env->memory.data + env->memory.size;
+
+ env->table.data =
+ SDL_calloc(wasm2c_pico8_min_env_table, sizeof(*env->table.data));
+ if (!env->table.data) {
+ goto error;
+ }
+ env->table.size = wasm2c_pico8_min_env_table;
return env;
+
+error:
+ if (!env->memory.data) {
+ SDL_free(env->memory.data);
+ }
+ if (!env->table.data) {
+ SDL_free(env->table.data);
+ }
+ SDL_free(env);
+ return NULL;
}
void pw_env_free(struct w2c_env *env) {
@@ -33,6 +73,8 @@ void pw_env_free(struct w2c_env *env) {
return;
}
+ SDL_free(env->memory.data);
+ SDL_free(env->table.data);
SDL_free(env);
}
@@ -45,7 +87,16 @@ bool pw_env_link(struct w2c_env *env, struct pw_env_ctx context) {
}
env->context = context;
- env->main_loop = 0;
+ env->dynamictop = ENV_DYNAMICTOP_PTR;
+ env->tablebase = 0;
+ env->mainloop = 0;
+ env->error = 0;
+ env->tempret = 0;
+
+ SDL_memset(env->memory.data, 0, env->memory.size);
+ wasm_rt_memory_store32_unchecked(&env->memory, env->dynamictop,
+ ENV_DYNAMIC_BASE);
+
return true;
}
@@ -63,10 +114,43 @@ static uint32_t env_ticks(void) {
return (uint32_t)(SDL_GetTicks() & UINT32_MAX);
}
+static SDL_NORETURN void unimplemented(char const *name) {
+ SDL_LogCritical(0, "called unimplemented function: %s", name);
+ wasm_rt_trap(WASM_RT_TRAP_UNREACHABLE);
+}
+
+extern uint32_t *w2c_env_DYNAMICTOP_PTR(struct w2c_env *env) {
+ return &env->dynamictop;
+}
+
+extern uint32_t *w2c_env_0x5F_table_base(struct w2c_env *env) {
+ return &env->tablebase;
+}
+
struct wasm_rt_memory_t *w2c_env_memory(struct w2c_env *env) {
return &env->memory;
}
+extern wasm_rt_funcref_table_t *w2c_env_table(struct w2c_env *env) {
+ return &env->table;
+}
+
+void w2c_env_0x5FSDL_ClearError(struct w2c_env *env) {
+ (void)env;
+ SDL_ClearError();
+}
+
+uint32_t w2c_env_0x5FSDL_GetError(struct w2c_env *env) {
+ char err[ENV_ERROR_MAX_SIZE];
+ SDL_strlcpy(err, SDL_GetError(), ENV_ERROR_MAX_SIZE);
+ wasm_rt_memory_store(&env->memory, err, env->error, ENV_ERROR_MAX_SIZE);
+ return env->error;
+}
+
+uint32_t w2c_env_0x5FSDL_GetModState(struct w2c_env *env) {
+ return (uint32_t)pw_input_keymod(env->context.input);
+}
+
uint32_t w2c_env_0x5FSDL_GetMouseState(struct w2c_env *env, uint32_t x,
uint32_t y) {
struct pw_coordinates pos;
@@ -81,7 +165,31 @@ uint32_t w2c_env_0x5FSDL_GetMouseState(struct w2c_env *env, uint32_t x,
return (uint32_t)button;
}
-/* import: 'env' '_SDL_LockSurface' */
+uint32_t w2c_env_0x5FSDL_GetTicks(struct w2c_env *env) {
+ (void)env;
+ return env_ticks();
+}
+
+void w2c_env_0x5FSDL_GetWindowSize(struct w2c_env *env, uint32_t window,
+ uint32_t x, uint32_t y) {
+ (void)window;
+ wasm_rt_memory_store32(&env->memory, x, PW_FB_WIDTH);
+ wasm_rt_memory_store32(&env->memory, y, PW_FB_HEIGHT);
+}
+
+uint32_t w2c_env_0x5FSDL_Init(struct w2c_env *env, uint32_t flags) {
+ (void)flags;
+
+ env->error = w2c_pico8_0x5Fmalloc(env->context.instance, ENV_ERROR_MAX_SIZE);
+
+ return 0;
+}
+
+void w2c_env_0x5FSDL_LockAudio(struct w2c_env *env) {
+ (void)env;
+ // TODO(nlordell): I need to figure out audio...
+}
+
uint32_t w2c_env_0x5FSDL_LockSurface(struct w2c_env *env, uint32_t surface) {
if (!surface) {
SDL_InvalidParamError("surface");
@@ -92,10 +200,6 @@ uint32_t w2c_env_0x5FSDL_LockSurface(struct w2c_env *env, uint32_t surface) {
return 0;
}
-#define ENV_KEYDOWN 0x300
-#define ENV_KEYUP 0x301
-#define ENV_MOUSEMOTION 0x400
-
uint32_t w2c_env_0x5FSDL_PollEvent(struct w2c_env *env, uint32_t event) {
if (!event) {
return (uint32_t)pw_input_poll(env->context.input, NULL);
@@ -140,11 +244,6 @@ uint32_t w2c_env_0x5FSDL_PollEvent(struct w2c_env *env, uint32_t event) {
return (uint32_t)remaining;
}
-// PICO-8 engine is compiled with the Emscripten built-in SDL1.3 library, which
-// has a different pixel format encoding than SDL3 which is used on the host.
-// The pixel format used by the engine is:
-#define ENV_PIXELFORMAT_ABGR8888 0x86762004
-
uint32_t w2c_env_0x5FSDL_SetVideoMode(struct w2c_env *env, uint32_t w,
uint32_t h, uint32_t bpp,
uint32_t flags) {
@@ -210,6 +309,11 @@ uint32_t w2c_env_0x5FSDL_SetVideoMode(struct w2c_env *env, uint32_t w,
return result;
}
+void w2c_env_0x5FSDL_UnlockAudio(struct w2c_env *env) {
+ (void)env;
+ // TODO(nlordell): More audio things to figure out...
+}
+
void w2c_env_0x5FSDL_UnlockSurface(struct w2c_env *env, uint32_t surface) {
if (!surface) {
SDL_InvalidParamError("surface");
@@ -235,11 +339,239 @@ void w2c_env_0x5FSDL_UnlockSurface(struct w2c_env *env, uint32_t surface) {
pw_fb_unlock(env->context.fb);
}
-/* import: 'env' '_emscripten_set_main_loop' */
+void w2c_env_0x5F_0x5FbuildEnvironment(struct w2c_env *env, uint32_t environ) {
+ char const *vars[] = {"USER=picow", "LOGNAME=picow", "PATH=/",
+ "PWD=/", "HOME=/home/picow", "LANG=C.UTF-8",
+ "_=./picow"};
+ size_t count = SDL_arraysize(vars);
+
+ uint32_t array = w2c_pico8_0x5Fmalloc(env->context.instance,
+ (count + 1) * sizeof(uint32_t));
+ for (uint32_t i = 0; i < count; ++i) {
+ uint32_t sz = (uint32_t)SDL_strlen(vars[i]) + 1;
+ uint32_t addr = array + i * sizeof(uint32_t);
+ uint32_t str = w2c_pico8_0x5Fmalloc(env->context.instance, sz);
+ wasm_rt_memory_store32(&env->memory, addr, str);
+ wasm_rt_memory_store(&env->memory, vars[i], str, sz);
+ }
+
+ wasm_rt_memory_store32(&env->memory, array + count * sizeof(uint32_t), 0);
+ wasm_rt_memory_store32(&env->memory, environ, array);
+}
+
+void w2c_env_0x5F_0x5Flock(struct w2c_env *env, uint32_t ptr) {
+ (void)env;
+ (void)ptr;
+}
+
+void w2c_env_0x5F_0x5FsetErrNo(struct w2c_env *env, uint32_t value) {
+ uint32_t errno = w2c_pico8_0x5F_0x5Ferrno_location(env->context.instance);
+ wasm_rt_memory_store32(&env->memory, errno, value);
+}
+
+uint32_t w2c_env_0x5F_0x5Fsyscall140(struct w2c_env *env, uint32_t a,
+ uint32_t b) {
+ (void)env;
+ (void)a;
+ (void)b;
+ unimplemented("___syscall140");
+}
+
+uint32_t w2c_env_0x5F_0x5Fsyscall145(struct w2c_env *env, uint32_t a,
+ uint32_t b) {
+ (void)env;
+ (void)a;
+ (void)b;
+ unimplemented("___syscall145");
+}
+
+uint32_t w2c_env_0x5F_0x5Fsyscall146(struct w2c_env *env, uint32_t a,
+ uint32_t b) {
+ (void)env;
+ (void)a;
+ (void)b;
+ unimplemented("___syscall146");
+}
+
+uint32_t w2c_env_0x5F_0x5Fsyscall195(struct w2c_env *env, uint32_t a,
+ uint32_t b) {
+ (void)env;
+ (void)a;
+ (void)b;
+ unimplemented("___syscall195");
+}
+
+uint32_t w2c_env_0x5F_0x5Fsyscall220(struct w2c_env *env, uint32_t a,
+ uint32_t b) {
+ (void)env;
+ (void)a;
+ (void)b;
+ unimplemented("___syscall220");
+}
+
+uint32_t w2c_env_0x5F_0x5Fsyscall221(struct w2c_env *env, uint32_t a,
+ uint32_t b) {
+ (void)env;
+ (void)a;
+ (void)b;
+ unimplemented("___syscall221");
+}
+
+uint32_t w2c_env_0x5F_0x5Fsyscall5(struct w2c_env *env, uint32_t a,
+ uint32_t b) {
+ (void)env;
+ (void)a;
+ (void)b;
+ unimplemented("___syscall5");
+}
+
+uint32_t w2c_env_0x5F_0x5Fsyscall54(struct w2c_env *env, uint32_t a,
+ uint32_t b) {
+ (void)env;
+ (void)a;
+ (void)b;
+ unimplemented("___syscall54");
+};
+
+uint32_t w2c_env_0x5F_0x5Fsyscall6(struct w2c_env *env, uint32_t a,
+ uint32_t b) {
+ (void)env;
+ (void)a;
+ (void)b;
+ unimplemented("___syscall6");
+}
+
+void w2c_env_0x5F_0x5Funlock(struct w2c_env *env, uint32_t ptr) {
+ (void)env;
+ (void)ptr;
+}
+
+void w2c_env_0x5Fabort(struct w2c_env *env) {
+ (void)env;
+ wasm_rt_trap(WASM_RT_TRAP_UNREACHABLE);
+}
+
+uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env,
+ uint32_t code) {
+ (void)env;
+ switch (code) {
+ case 6:
+ // Web player control setup, no-op.
+ return 0;
+ case 16:
+ // On module load callback, no-op.
+ return 0;
+ case 27:
+ // Touch detection, no-op.
+ // TODO(nlordell): Not sure what this controls, maybe we should pass along
+ // whether or not we've detected touch controls?
+ return 0;
+ }
+
+ SDL_Log("asm const %d", code);
+ unimplemented("_emscripten_asm_const_i");
+}
+
+uint32_t w2c_env_0x5Femscripten_asm_const_ii(struct w2c_env *env, uint32_t a,
+ uint32_t b) {
+ (void)env;
+ (void)a;
+ (void)b;
+ unimplemented("_emscripten_asm_const_ii");
+}
+
+uint32_t w2c_env_0x5Femscripten_asm_const_iii(struct w2c_env *env, uint32_t a,
+ uint32_t b, uint32_t c) {
+ (void)env;
+ (void)a;
+ (void)b;
+ (void)c;
+ unimplemented("_emscripten_asm_const_iii");
+}
+
+uint32_t w2c_env_0x5Femscripten_asm_const_iiiii(struct w2c_env *env, uint32_t a,
+ uint32_t b, uint32_t c,
+ uint32_t d, uint32_t e) {
+ (void)env;
+ (void)a;
+ (void)b;
+ (void)c;
+ (void)d;
+ (void)e;
+ unimplemented("_emscripten_asm_const_iiiii");
+}
+
+uint32_t w2c_env_0x5Femscripten_asm_const_iiiiiii(struct w2c_env *env,
+ uint32_t a, uint32_t b,
+ uint32_t c, uint32_t d,
+ uint32_t e, uint32_t f,
+ uint32_t g) {
+ (void)env;
+ (void)a;
+ (void)b;
+ (void)c;
+ (void)d;
+ (void)e;
+ (void)f;
+ (void)g;
+ unimplemented("_emscripten_asm_const_iiiiiii");
+}
+
+uint32_t w2c_env_0x5Femscripten_asm_const_iiiiiiiii(
+ struct w2c_env *env, uint32_t a, uint32_t b, uint32_t c, uint32_t d,
+ uint32_t e, uint32_t f, uint32_t g, uint32_t h, uint32_t i) {
+ (void)env;
+ (void)a;
+ (void)b;
+ (void)c;
+ (void)d;
+ (void)e;
+ (void)f;
+ (void)g;
+ (void)h;
+ (void)i;
+ unimplemented("_emscripten_asm_const_iiiiiiiii");
+}
+
+void w2c_env_0x5Femscripten_async_wget_data(struct w2c_env *env, uint32_t a,
+ uint32_t b, uint32_t c,
+ uint32_t d) {
+ (void)env;
+ (void)a;
+ (void)b;
+ (void)c;
+ (void)d;
+ unimplemented("_emscripten_async_wget_data");
+}
+
+uint32_t w2c_env_0x5Femscripten_get_heap_size(struct w2c_env *env) {
+ return env->memory.size;
+}
+
+uint32_t w2c_env_0x5Femscripten_memcpy_big(struct w2c_env *env, uint32_t dst,
+ uint32_t src, uint32_t len) {
+ wasm_rt_memory_copy(&env->memory, dst, src, len);
+ return dst;
+}
+
+uint32_t w2c_env_0x5Femscripten_resize_heap(struct w2c_env *env,
+ uint32_t size) {
+ // Don't allow heap resizing.
+ (void)env;
+ (void)size;
+ return 0;
+}
+
+void w2c_env_0x5Femscripten_run_script(struct w2c_env *env, uint32_t src) {
+ (void)env;
+ (void)src;
+ unimplemented("_emscripten_run_script");
+}
+
void w2c_env_0x5Femscripten_set_main_loop(struct w2c_env *env, uint32_t func,
uint32_t fps,
uint32_t simulate_infinite_loop) {
- env->main_loop = func;
+ env->mainloop = func;
// Ignore the fps and simulate infinite loop parameters, they are always 0
// for PICO-8 anyway.
@@ -247,6 +579,73 @@ void w2c_env_0x5Femscripten_set_main_loop(struct w2c_env *env, uint32_t func,
(void)simulate_infinite_loop;
}
+void w2c_env_0x5Fexit(struct w2c_env *env, uint32_t code) {
+ (void)env;
+ (void)code;
+ unimplemented("_exit");
+}
+
+uint32_t w2c_env_0x5Fgmtime(struct w2c_env *env, uint32_t time) {
+ (void)env;
+ (void)time;
+ unimplemented("_gmtime");
+}
+
+uint32_t w2c_env_0x5Flocaltime(struct w2c_env *env, uint32_t time) {
+ (void)env;
+ (void)time;
+ unimplemented("_localtime");
+}
+
+void w2c_env_0x5Flongjmp(struct w2c_env *env, uint32_t jmpbuf, uint32_t value) {
+ (void)env;
+ (void)jmpbuf;
+ (void)value;
+ unimplemented("_longjmp");
+}
+
+uint32_t w2c_env_0x5Fstrftime(struct w2c_env *env, uint32_t a, uint32_t b,
+ uint32_t c, uint32_t d) {
+ (void)env;
+ (void)a;
+ (void)b;
+ (void)c;
+ (void)d;
+ unimplemented("_strftime");
+}
+
+uint32_t w2c_env_0x5Ftime(struct w2c_env *env, uint32_t time) {
+ (void)env;
+ (void)time;
+ unimplemented("_time");
+}
+
+void w2c_env_abort(struct w2c_env *env, uint32_t code) {
+ (void)code;
+ w2c_env_0x5Fabort(env);
+}
+
+uint32_t w2c_env_abortOnCannotGrowMemory(struct w2c_env *env, uint32_t size) {
+ (void)env;
+ (void)size;
+ unimplemented("abortOnCannotGrowMemory");
+}
+
+uint32_t w2c_env_getTempRet0(struct w2c_env *env) { return env->tempret; }
+
+void w2c_env_invoke_vii(struct w2c_env *env, uint32_t a, uint32_t b,
+ uint32_t c) {
+ (void)env;
+ (void)a;
+ (void)b;
+ (void)c;
+ unimplemented("invoke_vii");
+}
+
+void w2c_env_setTempRet0(struct w2c_env *env, uint32_t value) {
+ env->tempret = value;
+}
+
static struct w2c_global shared_global = {
.infinity = INFINITY,
.nan = NAN,