diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/picow.c | 338 | ||||
| -rw-r--r-- | src/wamr_test.c | 21 |
2 files changed, 354 insertions, 5 deletions
diff --git a/src/picow.c b/src/picow.c index 8825261..51a3f8c 100644 --- a/src/picow.c +++ b/src/picow.c @@ -12,6 +12,8 @@ #include "sysroot.h" +#include "./wamr_test.c" + static void usage(char const *name) { // clang-format off printf("Usage: %s [-s <sysroot>] [-c <carts>] [-v[v[v]]]\n", name); @@ -156,16 +158,17 @@ static bool load_cart(struct pw_pxtexture *texture, struct pw_cart *cart, goto cleanup; } - Sint64 size = SDL_GetIOSize(rom); - if (size < 0) { + Sint64 ssize = SDL_GetIOSize(rom); + if (ssize < 0) { goto cleanup; } - if (size > 1024 * 1024) { + if (ssize > 1024 * 1024) { SDL_SetError("cart .png is too large"); goto cleanup; } uint8_t data[1024 * 1024]; - size_t n = SDL_ReadIO(rom, data, (size_t)size); + size_t size = (size_t)ssize; + size_t n = SDL_ReadIO(rom, data, size); if (n != size) { goto cleanup; } @@ -491,6 +494,113 @@ static void *wasm_realloc(void *ptr, unsigned int size) { static void wasm_free(void *ptr) { SDL_free(ptr); } +// TODO(nlordell): FOR TESTING!!! +static void test_wasm_host_mul(wasm_exec_env_t exec, uint64_t *args) { + (void)exec; + native_raw_return_type(int32_t, args); + native_raw_get_arg(int32_t, a, args); + native_raw_get_arg(int32_t, b, args); + int32_t r = a * b; + printf("-- %d * %d = %d\n", a, b, r); + native_raw_set_return(r); +} + +/* +WASM INFO: + IMPORT: + env.abort [func (i)->()] + env.setTempRet0 [func (i)->()] + env.getTempRet0 [func ()->(i)] + env.invoke_vii [func (iii)->()] + env._SDL_ClearError [func ()->()] + env._SDL_GetError [func ()->(i)] + env._SDL_GetModState [func ()->(i)] + env._SDL_GetMouseState [func (ii)->(i)] + env._SDL_GetTicks [func ()->(i)] + env._SDL_GetWindowSize [func (iii)->()] + env._SDL_Init [func (i)->(i)] + env._SDL_LockAudio [func ()->()] + env._SDL_LockSurface [func (i)->(i)] + env._SDL_PollEvent [func (i)->(i)] + env._SDL_SetVideoMode [func (iiii)->(i)] + env._SDL_UnlockAudio [func ()->()] + env._SDL_UnlockSurface [func (i)->()] + env.___buildEnvironment [func (i)->()] + env.___lock [func (i)->()] + env.___setErrNo [func (i)->()] + env.___syscall140 [func (ii)->(i)] + env.___syscall145 [func (ii)->(i)] + env.___syscall146 [func (ii)->(i)] + env.___syscall195 [func (ii)->(i)] + env.___syscall220 [func (ii)->(i)] + env.___syscall221 [func (ii)->(i)] + env.___syscall5 [func (ii)->(i)] + env.___syscall54 [func (ii)->(i)] + env.___syscall6 [func (ii)->(i)] + env.___unlock [func (i)->()] + env._abort [func ()->()] + env._emscripten_asm_const_i [func (i)->(i)] + env._emscripten_asm_const_ii [func (ii)->(i)] + env._emscripten_asm_const_iii [func (iii)->(i)] + env._emscripten_asm_const_iiiii [func (iiiii)->(i)] + env._emscripten_asm_const_iiiiiii [func (iiiiiii)->(i)] + env._emscripten_asm_const_iiiiiiiii [func (iiiiiiiii)->(i)] + env._emscripten_async_wget_data [func (iiii)->()] + env._emscripten_get_heap_size [func ()->(i)] + env._emscripten_memcpy_big [func (iii)->(i)] + env._emscripten_resize_heap [func (i)->(i)] + env._emscripten_run_script [func (i)->()] + env._emscripten_set_main_loop [func (iii)->()] + env._exit [func (i)->()] + env._gmtime [func (i)->(i)] + env._localtime [func (i)->(i)] + env._longjmp [func (ii)->()] + env._strftime [func (iiii)->(i)] + env._time [func (i)->(i)] + env.abortOnCannotGrowMemory [func (i)->(i)] + env.table [table] + env.memory [memory] + env.__table_base [global] + env.DYNAMICTOP_PTR [global] + global.NaN [global] + global.Infinity [global] + EXPORT: + ___emscripten_environ_constructor [func ()->()] + ___errno_location [func ()->(i)] + __get_daylight [func ()->(i)] + __get_environ [func ()->(i)] + __get_timezone [func ()->(i)] + __get_tzname [func ()->(i)] + _codo_mixer_callback [func (iii)->()] + _free [func (i)->()] + _main [func (ii)->(i)] + _malloc [func (i)->(i)] + _memcpy [func (iii)->(i)] + _memset [func (iii)->(i)] + _mix_0 [func (ii)->(i)] + _realloc [func (ii)->(i)] + _saveSetjmp [func (iiii)->(i)] + _sbrk [func (i)->(i)] + _setThrew [func (ii)->()] + _testSetjmp [func (iii)->(i)] + dynCall_ii [func (ii)->(i)] + dynCall_iidiiii [func (iiFiiii)->(i)] + dynCall_iii [func (iii)->(i)] + dynCall_iiii [func (iiii)->(i)] + dynCall_iiiii [func (iiiii)->(i)] + dynCall_iiiiii [func (iiiiii)->(i)] + dynCall_jiji [func (iiiii)->(i)] + dynCall_v [func (i)->()] + dynCall_vi [func (ii)->()] + dynCall_vii [func (iii)->()] + dynCall_viii [func (iiii)->()] + dynCall_viiiii [func (iiiiii)->()] + establishStackSpace [func (ii)->()] + stackAlloc [func (i)->(i)] + stackRestore [func (i)->()] + stackSave [func ()->(i)] +*/ + static SDL_AppResult scene_cart_selection(struct pw_appstate *state) { if (state->cartidx != state->cartidx2) { state->cartidx = state->cartidx2; @@ -521,6 +631,23 @@ static SDL_AppResult scene_game_init(struct pw_appstate *state) { return FAIL("failed to instantiate PICO-8 WebAssembly module"); } + wasm_function_inst_t function = + wasm_runtime_lookup_function(state->instance, "wamr_test_main"); + if (function == NULL) { + return FAIL("could not find function"); + } + + wasm_exec_env_t exec = wasm_runtime_create_exec_env(state->instance, 1024); + if (exec == NULL) { + return FAIL("failed to create execution environment"); + } + + uint32_t args[] = {0}; + if (!wasm_runtime_call_wasm(exec, function, 1, args)) { + return FAIL("error calling main"); + } + printf("=> %d\n", (int32_t)args[0]); + return SDL_APP_CONTINUE; } @@ -631,14 +758,214 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { return FAIL("failed to initialize the WebAssembly runtime"); } + struct NativeSymbol env[] = { + {"host_mul", test_wasm_host_mul, "(ii)i", NULL}, + }; + if (!wasm_runtime_register_natives_raw("foo", 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.len, err, sizeof err); + //wasm_runtime_load(pico8_wasm.data, pico8_wasm.len, err, sizeof err); + wasm_runtime_load(test_wasm, sizeof(test_wasm), err, sizeof err); if (state->module == NULL) { SDL_SetError("%s", err); return FAIL("failed to load the PICO-8 WebAssembly module"); } + struct NativeGlobal genv[] = { + {"host_magic_number", {WASM_I32, .of = {.i32 = -964079907}}}, + }; + if (!wasm_runtime_link_globals(state->module, "foo", genv, SDL_arraysize(genv))) { + SDL_SetError("unspecified error"); + return FAIL("failed to register host globals"); + } + + // TODO(nlordell): remove me + int32_t count; + count = wasm_runtime_get_import_count(state->module); + wasm_import_t import; + char kind_name[8] = {0}; + char extra_info[128] = {0}; + uint32_t param_count; + uint32_t j; + char *cursor; + wasm_valkind_t valkind; + for (int32_t i = 0; i < count; i++) { + wasm_runtime_get_import_type(state->module, i, &import); + switch (import.kind) { + case WASM_IMPORT_EXPORT_KIND_FUNC: + strlcpy(kind_name, "func", sizeof(kind_name)); + cursor = extra_info; + *(cursor++) = ' '; + *(cursor++) = '('; + param_count = wasm_func_type_get_param_count(import.u.func_type); + for (j = 0; j < param_count; j++) { + valkind = wasm_func_type_get_param_valkind(import.u.func_type, j); + switch (valkind) { + case WASM_I32: + *(cursor++) = 'i'; + break; + case WASM_I64: + *(cursor++) = 'I'; + break; + case WASM_F32: + *(cursor++) = 'f'; + break; + case WASM_F64: + *(cursor++) = 'F'; + break; + case WASM_V128: + *(cursor++) = 'v'; + break; + case WASM_EXTERNREF: + *(cursor++) = 'r'; + break; + case WASM_FUNCREF: + *(cursor++) = 'R'; + break; + } + } + *(cursor++) = ')'; + *(cursor++) = '-'; + *(cursor++) = '>'; + *(cursor++) = '('; + param_count = wasm_func_type_get_result_count(import.u.func_type); + for (j = 0; j < param_count; j++) { + valkind = wasm_func_type_get_result_valkind(import.u.func_type, j); + switch (valkind) { + case WASM_I32: + *(cursor++) = 'i'; + break; + case WASM_I64: + *(cursor++) = 'I'; + break; + case WASM_F32: + *(cursor++) = 'f'; + break; + case WASM_F64: + *(cursor++) = 'F'; + break; + case WASM_V128: + *(cursor++) = 'v'; + break; + case WASM_EXTERNREF: + *(cursor++) = 'r'; + break; + case WASM_FUNCREF: + *(cursor++) = 'R'; + break; + } + } + *(cursor++) = ')'; + *(cursor++) = 0; + break; + case WASM_IMPORT_EXPORT_KIND_TABLE: + strlcpy(kind_name, "table", sizeof(kind_name)); + extra_info[0] = 0; + break; + case WASM_IMPORT_EXPORT_KIND_MEMORY: + strlcpy(kind_name, "memory", sizeof(kind_name)); + extra_info[0] = 0; + break; + case WASM_IMPORT_EXPORT_KIND_GLOBAL: + strlcpy(kind_name, "global", sizeof(kind_name)); + extra_info[0] = 0; + break; + } + SDL_LogDebug(0, "IMPORT: %s.%s [%s%s]", import.module_name, import.name, kind_name, extra_info); + } + + count = wasm_runtime_get_export_count(state->module); + wasm_export_t export; + for (int32_t i = 0; i < count; i++) { + wasm_runtime_get_export_type(state->module, i, &export); + switch (export.kind) { + case WASM_IMPORT_EXPORT_KIND_FUNC: + strlcpy(kind_name, "func", sizeof(kind_name)); + cursor = extra_info; + *(cursor++) = ' '; + *(cursor++) = '('; + param_count = wasm_func_type_get_param_count(export.u.func_type); + for (j = 0; j < param_count; j++) { + valkind = wasm_func_type_get_param_valkind(export.u.func_type, j); + switch (valkind) { + case WASM_I32: + *(cursor++) = 'i'; + break; + case WASM_I64: + *(cursor++) = 'I'; + break; + case WASM_F32: + *(cursor++) = 'f'; + break; + case WASM_F64: + *(cursor++) = 'F'; + break; + case WASM_V128: + *(cursor++) = 'v'; + break; + case WASM_EXTERNREF: + *(cursor++) = 'r'; + break; + case WASM_FUNCREF: + *(cursor++) = 'R'; + break; + } + } + *(cursor++) = ')'; + *(cursor++) = '-'; + *(cursor++) = '>'; + *(cursor++) = '('; + param_count = wasm_func_type_get_result_count(export.u.func_type); + for (j = 0; j < param_count; j++) { + valkind = wasm_func_type_get_result_valkind(export.u.func_type, j); + switch (valkind) { + case WASM_I32: + *(cursor++) = 'i'; + break; + case WASM_I64: + *(cursor++) = 'I'; + break; + case WASM_F32: + *(cursor++) = 'f'; + break; + case WASM_F64: + *(cursor++) = 'F'; + break; + case WASM_V128: + *(cursor++) = 'v'; + break; + case WASM_EXTERNREF: + *(cursor++) = 'r'; + break; + case WASM_FUNCREF: + *(cursor++) = 'R'; + break; + } + } + *(cursor++) = ')'; + *(cursor++) = 0; + break; + case WASM_IMPORT_EXPORT_KIND_TABLE: + strlcpy(kind_name, "table", sizeof(kind_name)); + extra_info[0] = 0; + break; + case WASM_IMPORT_EXPORT_KIND_MEMORY: + strlcpy(kind_name, "memory", sizeof(kind_name)); + extra_info[0] = 0; + break; + case WASM_IMPORT_EXPORT_KIND_GLOBAL: + strlcpy(kind_name, "global", sizeof(kind_name)); + extra_info[0] = 0; + break; + } + SDL_LogDebug(0, "EXPORT: %s [%s%s]", export.name, kind_name, extra_info); + } + // TODO(nlordell): remove me + state->scene = PW_SCENE_CART_SELECTION; state->scene2 = PW_SCENE_CART_SELECTION; return SDL_APP_CONTINUE; @@ -692,6 +1019,7 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) { void SDL_AppQuit(void *appstate, SDL_AppResult result) { struct pw_appstate *state = appstate; + (void)result; if (state->module) { if (state->instance) { diff --git a/src/wamr_test.c b/src/wamr_test.c new file mode 100644 index 0000000..974cc31 --- /dev/null +++ b/src/wamr_test.c @@ -0,0 +1,21 @@ +static uint8_t test_wasm[281] = { + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0b, 0x02, 0x60, 0x02, 0x7f, 0x7f, 0x01, + 0x7f, 0x60, 0x00, 0x01, 0x7f, 0x02, 0x29, 0x02, 0x03, 0x66, 0x6f, 0x6f, 0x08, 0x68, 0x6f, 0x73, + 0x74, 0x5f, 0x6d, 0x75, 0x6c, 0x00, 0x00, 0x03, 0x66, 0x6f, 0x6f, 0x11, 0x68, 0x6f, 0x73, 0x74, + 0x5f, 0x6d, 0x61, 0x67, 0x69, 0x63, 0x5f, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x03, 0x7f, 0x01, + 0x03, 0x03, 0x02, 0x00, 0x01, 0x05, 0x03, 0x01, 0x00, 0x10, 0x06, 0x19, 0x03, 0x7f, 0x01, 0x41, + 0x80, 0x80, 0xc0, 0x00, 0x0b, 0x7f, 0x00, 0x41, 0x80, 0x80, 0xc0, 0x00, 0x0b, 0x7f, 0x00, 0x41, + 0x80, 0x80, 0xc0, 0x00, 0x0b, 0x07, 0x46, 0x05, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, + 0x00, 0x0d, 0x77, 0x61, 0x6d, 0x72, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x61, 0x64, 0x64, 0x00, + 0x01, 0x0e, 0x77, 0x61, 0x6d, 0x72, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x6d, 0x61, 0x69, 0x6e, + 0x00, 0x02, 0x0a, 0x5f, 0x5f, 0x64, 0x61, 0x74, 0x61, 0x5f, 0x65, 0x6e, 0x64, 0x03, 0x01, 0x0b, + 0x5f, 0x5f, 0x68, 0x65, 0x61, 0x70, 0x5f, 0x62, 0x61, 0x73, 0x65, 0x03, 0x02, 0x0a, 0x6a, 0x02, + 0x2d, 0x01, 0x01, 0x7f, 0x23, 0x01, 0x41, 0x10, 0x6b, 0x21, 0x02, 0x20, 0x02, 0x20, 0x00, 0x36, + 0x02, 0x00, 0x20, 0x02, 0x20, 0x01, 0x36, 0x02, 0x04, 0x20, 0x02, 0x20, 0x00, 0x36, 0x02, 0x08, + 0x20, 0x02, 0x20, 0x01, 0x36, 0x02, 0x0c, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0f, 0x0b, 0x3a, 0x01, + 0x03, 0x7f, 0x23, 0x01, 0x41, 0x10, 0x6b, 0x21, 0x00, 0x20, 0x00, 0x24, 0x01, 0x41, 0xb9, 0x0a, + 0x41, 0xb5, 0x81, 0x2c, 0x10, 0x00, 0x21, 0x01, 0x20, 0x00, 0x20, 0x01, 0x36, 0x02, 0x08, 0x20, + 0x01, 0x23, 0x00, 0x10, 0x01, 0x21, 0x02, 0x20, 0x00, 0x20, 0x02, 0x36, 0x02, 0x0c, 0x20, 0x00, + 0x41, 0x10, 0x6a, 0x24, 0x01, 0x20, 0x02, 0x0f, 0x0b, +}; + |
