aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/picow.c122
1 files changed, 117 insertions, 5 deletions
diff --git a/src/picow.c b/src/picow.c
index bf5a051..0e9eb97 100644
--- a/src/picow.c
+++ b/src/picow.c
@@ -61,6 +61,10 @@ struct pw_appstate {
ssize_t cartidx2;
wasm_module_t module;
wasm_module_inst_t instance;
+ wasm_exec_env_t exec;
+ wasm_function_inst_t dyncall;
+ wasm_function_inst_t mix;
+ int32_t mainfn;
enum pw_scene scene;
enum pw_scene scene2;
};
@@ -395,15 +399,106 @@ static SDL_AppResult scene_game_init(struct pw_appstate *state) {
state->instance =
wasm_runtime_instantiate(state->module, 1024, 0, err, sizeof err);
- if (state->instance == NULL) {
+ if (!state->instance) {
SDL_SetError("%s", err);
return FAIL("failed to instantiate PICO-8 WebAssembly module");
}
- return FAIL("not yet implemented");
+ wasm_function_inst_t ctor = wasm_runtime_lookup_function(
+ state->instance, "___emscripten_environ_constructor");
+ wasm_function_inst_t start =
+ wasm_runtime_lookup_function(state->instance, "_main");
+ wasm_function_inst_t stackalloc =
+ wasm_runtime_lookup_function(state->instance, "stackAlloc");
+ wasm_function_inst_t dyncall =
+ wasm_runtime_lookup_function(state->instance, "dynCall_v");
+ wasm_function_inst_t mix =
+ wasm_runtime_lookup_function(state->instance, "_mix_0");
+ if (!ctor || !start || !stackalloc || !dyncall || !mix) {
+ return FAIL("could not required exports");
+ }
+
+ wasm_exec_env_t exec = wasm_runtime_create_exec_env(state->instance, 1024);
+ if (exec == NULL) {
+ return FAIL("failed to create execution environment");
+ }
+ wasm_runtime_set_user_data(exec, state);
+
+ if (!wasm_runtime_call_wasm(exec, ctor, 0, NULL)) {
+ return FAIL("error calling main");
+ }
+
+ uint32_t arg0;
+ {
+ char const value[] = "./pico8";
+ uint32_t args[] = {(uint32_t)sizeof(value)};
+ if (!wasm_runtime_call_wasm(exec, stackalloc, 1, args)) {
+ return FAIL("error allocating argument 0");
+ }
+
+ arg0 = args[0];
+ char *target = wasm_runtime_addr_app_to_native(state->instance, arg0);
+ if (!target) {
+ return FAIL("invalid argument 0 address");
+ }
+
+ SDL_memcpy(target, value, sizeof(value));
+ }
+
+ uint32_t argv;
+ {
+ uint32_t const value[] = {arg0, 0};
+ uint32_t args[] = {(uint32_t)sizeof(value)};
+ if (!wasm_runtime_call_wasm(exec, stackalloc, 1, args)) {
+ return FAIL("error allocating arguments array");
+ }
+
+ argv = args[0];
+ char *target = wasm_runtime_addr_app_to_native(state->instance, argv);
+ if (!target) {
+ return FAIL("invalid arguments array address");
+ }
+
+ SDL_memcpy(target, value, sizeof(value));
+ }
+
+ state->mainfn = 0;
+ {
+ uint32_t args[] = {1, argv};
+ if (!wasm_runtime_call_wasm(exec, start, 2, args)) {
+ return FAIL("error calling main");
+ }
+ if (args[0]) {
+ return FAIL("main returned exit code");
+ }
+ }
+ if (!state->mainfn) {
+ return FAIL("main loop function not set");
+ }
+
+ state->exec = exec;
+ state->dyncall = dyncall;
+ state->mix = mix;
+ return SDL_APP_CONTINUE;
}
static SDL_AppResult scene_game(struct pw_appstate *state) {
+ {
+ uint32_t args[] = {state->mainfn};
+ if (!wasm_runtime_call_wasm(state->exec, state->dyncall, 1, args)) {
+ return FAIL("error calling main loop function");
+ }
+ }
+ {
+ uint32_t args[] = {42, 1337};
+ if (!wasm_runtime_call_wasm(state->exec, state->mix, 2, args)) {
+ return FAIL("error calling audio mixer");
+ }
+ if (!args[0]) {
+ return FAIL("audio mixing failed %d %d", args[0], args[1]);
+ }
+ }
+
void *fb;
int pitch;
if (!SDL_LockTexture(state->framebuffer, NULL, &fb, &pitch)) {
@@ -419,6 +514,21 @@ static SDL_AppResult scene_game(struct pw_appstate *state) {
return SDL_APP_CONTINUE;
}
+static void emrt_set_main_loop(wasm_exec_env_t exec, uint64_t *args) {
+ native_raw_get_arg(int32_t, func, args);
+ native_raw_get_arg(int32_t, fps, args);
+ native_raw_get_arg(int32_t, simulate_infinite_loop, args);
+
+ struct pw_appstate *state = wasm_runtime_get_user_data(exec);
+ SDL_LogDebug(0, "setting main loop function: %d", func);
+ state->mainfn = func;
+
+ // Ignore the fps and simulate infinite loop parameters, they are always 0
+ // for PICO-8 anyway.
+ (void)fps;
+ (void)simulate_infinite_loop;
+}
+
SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
struct pw_appstate *state = SDL_calloc(1, sizeof *state);
*appstate = state;
@@ -493,15 +603,17 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
return FAIL("failed to initialize the WebAssembly runtime");
}
- struct NativeSymbol env[] = {};
+ struct NativeSymbol env[] = {
+ {"_emscripten_set_main_loop", emrt_set_main_loop, "(iii)", NULL},
+ };
if (!wasm_runtime_register_natives_raw("env", env, SDL_arraysize(env))) {
SDL_SetError("unspecified error");
return FAIL("failed to register host functions");
}
char err[1024];
- state->module =
- wasm_runtime_load(pico8_wasm.data, pico8_wasm.size, err, sizeof err);
+ state->module = wasm_runtime_load(stub_pico8_wasm.data, stub_pico8_wasm.size,
+ err, sizeof err);
if (state->module == NULL) {
SDL_SetError("%s", err);
return FAIL("failed to load the PICO-8 WebAssembly module");