aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-18 01:02:12 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-10-18 01:02:12 +0200
commitc2336f885c2b26ac613124e06225291ee59a7c15 (patch)
treef0fe830af28423ec77f538e42efe00e79ae48949 /src
parent7d29faead63abf66a9ef35606a732ca3493c6fbe (diff)
Stubbed ALL Imports
Now the cart fails to boot (we trap on unimplemented stubs), but a lot of small functionv have been setup. Now the marathon begins for trying to understand what each of the stubs do...
Diffstat (limited to 'src')
-rw-r--r--src/config.c2
-rw-r--r--src/env.c443
-rw-r--r--src/env.h7
-rw-r--r--src/game.c46
-rw-r--r--src/wasmrt.c14
-rw-r--r--src/wasmrt.h6
6 files changed, 474 insertions, 44 deletions
diff --git a/src/config.c b/src/config.c
index 346e649..d2bd649 100644
--- a/src/config.c
+++ b/src/config.c
@@ -205,7 +205,7 @@ struct pw_config *pw_config_load(int argc, char **argv) {
goto cleanup;
}
config->mode = PW_SCALE_MODE_PIXEL_PERFECT;
- config->memory = 32;
+ config->memory = 128;
config->logp = SDL_GetLogPriority(0);
// Read default values from the `config.ini` file before parsing command line
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,
diff --git a/src/env.h b/src/env.h
index 54d159f..c1a74c1 100644
--- a/src/env.h
+++ b/src/env.h
@@ -28,8 +28,13 @@ struct pw_env_ctx {
*/
struct w2c_env {
struct wasm_rt_memory_t memory;
+ struct wasm_rt_funcref_table_t table;
struct pw_env_ctx context;
- uint32_t main_loop;
+ uint32_t dynamictop;
+ uint32_t tablebase;
+ uint32_t mainloop;
+ uint32_t error;
+ uint32_t tempret;
};
/**
diff --git a/src/game.c b/src/game.c
index 84b17f7..35d98b9 100644
--- a/src/game.c
+++ b/src/game.c
@@ -82,29 +82,37 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *carts) {
});
wasm2c_pico8_instantiate(&game->instance, game->env, NULL);
- uint32_t argv;
- {
- char const value[] = "./pico8";
- uint32_t arg0 = w2c_pico8_stackAlloc(&game->instance, sizeof(value));
- if (!arg0) {
- return SDL_OutOfMemory();
- }
-
- wasm_rt_memory_store(&game->env->memory, value, arg0, sizeof(value));
- argv = w2c_pico8_stackAlloc(&game->instance, 2 * sizeof(arg0));
- if (!argv) {
- return SDL_OutOfMemory();
- }
+ struct wasm_rt_trap_handler_t handler;
+ if (!wasm_rt_trap_register(&handler)) {
+ return false;
+ }
- wasm_rt_memory_store32(&game->env->memory, argv, arg0);
- wasm_rt_memory_store32(&game->env->memory, argv + 4, 0);
+ if (wasm_rt_trapped(&handler)) {
+ return SDL_SetError("PICO-8 startup execution trapped: %s",
+ wasm_rt_trap_str(handler.code));
}
- if (w2c_pico8_0x5Fmain(&game->instance, 1, argv)) {
+ w2c_pico8_0x5F_0x5Femscripten_environ_constructor(&game->instance);
+
+ char const progname[] = "./picow";
+ uint32_t arg0 = w2c_pico8_stackAlloc(&game->instance, sizeof(progname));
+ wasm_rt_memory_store(&game->env->memory, progname, arg0, sizeof(progname));
+ uint32_t argv = w2c_pico8_stackAlloc(&game->instance, 2 * sizeof(arg0));
+ wasm_rt_memory_store32(&game->env->memory, argv, arg0);
+ wasm_rt_memory_store32(&game->env->memory, argv + 4, 0);
+
+ uint32_t status = w2c_pico8_0x5Fmain(&game->instance, 1, argv);
+
+ wasm_rt_trap_unregister();
+
+ if (status) {
return SDL_SetError("main returned exit code");
}
+ if (!game->env->mainloop) {
+ return SDL_SetError("cannot continue without main loop set");
+ }
- return game->env->main_loop;
+ return true;
}
bool pw_game_event(struct pw_game *game, struct pw_players *players,
@@ -132,11 +140,11 @@ bool pw_game_iterate(struct pw_game *game) {
}
if (wasm_rt_trapped(&handler)) {
- return SDL_SetError("PICO-8 execution trapped: %s",
+ return SDL_SetError("PICO-8 iteration execution trapped: %s",
wasm_rt_trap_str(handler.code));
}
- w2c_pico8_dynCall_v(&game->instance, game->env->main_loop);
+ w2c_pico8_dynCall_v(&game->instance, game->env->mainloop);
w2c_pico8_0x5Fmix_0(&game->instance, 42, 1337);
wasm_rt_trap_unregister();
diff --git a/src/wasmrt.c b/src/wasmrt.c
index 831b0ca..fce7ac8 100644
--- a/src/wasmrt.c
+++ b/src/wasmrt.c
@@ -22,6 +22,7 @@ void wasm_rt_trap(enum wasm_rt_trap_t code) {
wasm_rt_trap_str(code));
abort();
}
+ wasm_rt_trap_handler = NULL;
if (!code) {
SDL_LogWarn(0, "attempted to trap without a code");
@@ -174,7 +175,7 @@ void wasm_rt_memory_store16_unchecked(struct wasm_rt_memory_t *mem,
void wasm_rt_memory_store32(struct wasm_rt_memory_t *mem, uint32_t addr,
uint32_t value) {
wasm_rt_memory_check(mem, addr, sizeof(uint32_t));
- wasm_rt_memory_store16_unchecked(mem, addr, value);
+ wasm_rt_memory_store32_unchecked(mem, addr, value);
}
void wasm_rt_memory_store32_unchecked(struct wasm_rt_memory_t *mem,
@@ -195,3 +196,14 @@ void wasm_rt_memory_set(struct wasm_rt_memory_t *mem, uint32_t addr,
SDL_memset(mem->data_end - addr - len, value, len);
#endif
}
+
+void wasm_rt_memory_copy(struct wasm_rt_memory_t *mem, uint32_t dst,
+ uint32_t src, uint32_t len) {
+ wasm_rt_memory_check(mem, dst, len);
+ wasm_rt_memory_check(mem, src, len);
+#if SDL_BYTEORDER == SDL_LIL_ENDIAN
+ SDL_memmove(mem->data + dst, mem->data + src, len);
+#else
+ SDL_memmove(mem->data_end - dst - len, mem->data_end - src - len, len);
+#endif
+}
diff --git a/src/wasmrt.h b/src/wasmrt.h
index aaf2624..62ac4d0 100644
--- a/src/wasmrt.h
+++ b/src/wasmrt.h
@@ -266,4 +266,10 @@ void wasm_rt_memory_store32_unchecked(struct wasm_rt_memory_t *mem,
void wasm_rt_memory_set(struct wasm_rt_memory_t *mem, uint32_t addr,
uint32_t len, uint8_t value);
+/**
+ * Copies memory from one location to another, allowing overlap.
+ */
+void wasm_rt_memory_copy(struct wasm_rt_memory_t *mem, uint32_t dst,
+ uint32_t src, uint32_t len);
+
#endif