diff --git a/core/include/nova64_api.h b/core/include/nova64_api.h new file mode 100644 index 0000000..1c84685 --- /dev/null +++ b/core/include/nova64_api.h @@ -0,0 +1,6 @@ +#ifndef NOVA64_API_H +#define NOVA64_API_H + +void register_nova64_imports(void); + +#endif // NOVA64_API_H diff --git a/core/src/nova64_api.c b/core/src/nova64_api.c new file mode 100644 index 0000000..95050b2 --- /dev/null +++ b/core/src/nova64_api.c @@ -0,0 +1,24 @@ +#include "FreeRTOS.h" +#include "task.h" +#include "wasm_export.h" +#include "projdefs.h" + +static void native_yield(wasm_exec_env_t exec_env, uint32_t timeout_ms) { + if (timeout_ms == 0) { + // Gibt die Rest-Quantenzeit ab, blockiert aber nicht künstlich + taskYIELD(); + } else { + // Blockiert den Task für x Millisekunden + vTaskDelay(pdMS_TO_TICKS(timeout_ms)); + } +} + +static NativeSymbol native_symbols[] = { + {"yield", (void *)native_yield, "(i)", NULL}, + // { "flush_framebuffer", (void*)native_flush_framebuffer, "()", NULL } +}; + +void register_nova64_imports(void) { + wasm_runtime_register_natives("nova64", native_symbols, + sizeof(native_symbols) / sizeof(NativeSymbol)); +} \ No newline at end of file diff --git a/core/src/wasm_runner.c b/core/src/wasm_runner.c index 38e4d99..0683460 100644 --- a/core/src/wasm_runner.c +++ b/core/src/wasm_runner.c @@ -1,6 +1,7 @@ #include "wasm_runner.h" #include "nova64_fs.h" #include "nova64_internal.h" +#include "nova64_api.h" #include "wasm_export.h" #include @@ -17,6 +18,7 @@ uint32_t stack_size = 8092, heap_size = 8092; bool wasm_runner_initialize(void) { wasm_runtime_init(); + register_nova64_imports(); nova64_log("WASM runtime initialized successfully.\n"); const char *module_file = "SYS:/init.wasm"; @@ -44,7 +46,33 @@ bool wasm_runner_initialize(void) { wasm_module_inst_t module_inst = wasm_runtime_instantiate( module, stack_size, heap_size, error_buf, sizeof(error_buf)); - wasm_exec_env_t exec_env = wasm_runtime_create_exec_env(module_inst, stack_size); - + wasm_function_inst_t start_func = + wasm_runtime_lookup_function(module_inst, "_start"); + + if (start_func == NULL) { + nova64_log("Failed to find '_start' function in WASM module: %s\n", + wasm_runtime_get_exception(module_inst)); + free(buffer); + return false; + } + + wasm_exec_env_t exec_env = + wasm_runtime_create_exec_env(module_inst, stack_size); + + uint32_t argv[2] = {5, 3}; // Example arguments for the "add" function + + if (wasm_runtime_call_wasm(exec_env, start_func, 2, argv)) { + nova64_log("WASM module executed successfully. Sum: %d\n", argv[0]); + } else { + nova64_log("Failed to execute WASM module: %s\n", + wasm_runtime_get_exception(module_inst)); + } + + wasm_runtime_destroy_exec_env(exec_env); + wasm_runtime_deinstantiate(module_inst); + wasm_runtime_unload(module); + free(buffer); + wasm_runtime_destroy(); + return true; } diff --git a/simulator/sd/slot0/init.wasm b/simulator/sd/slot0/init.wasm index 3ef499b..dd9f70d 100644 Binary files a/simulator/sd/slot0/init.wasm and b/simulator/sd/slot0/init.wasm differ diff --git a/software/build.bat b/software/build.bat index 9c4868b..19ae4d5 100644 --- a/software/build.bat +++ b/software/build.bat @@ -1 +1,4 @@ -tinygo build -o ../simulator/sd/slot0/init.wasm -target wasm-unknown init.go \ No newline at end of file +@REM tinygo build -o ../simulator/sd/slot0/init.wasm -target wasm-unknown init.go + +@REM zig build-exe init.zig -target wasm32-freestanding -fno-entry -O ReleaseSmall -femit-bin=../simulator/sd/slot0/init.wasm +zig build-exe init.zig -target wasm32-freestanding -O ReleaseSmall -femit-bin=../simulator/sd/slot0/init.wasm \ No newline at end of file diff --git a/software/init.go b/software/init.go index 39166ef..a1dbd3c 100644 --- a/software/init.go +++ b/software/init.go @@ -2,7 +2,9 @@ package main // Declare a main function, this is the entrypoint into our go module // That will be run. In our example, we won't need this -func main() {} +func main() { + _ = add(1, 2) +} // This exports an add function. // It takes in two 32-bit integer values diff --git a/software/init.zig b/software/init.zig new file mode 100644 index 0000000..4b81eca --- /dev/null +++ b/software/init.zig @@ -0,0 +1,12 @@ +extern "nova64" fn yield(timeout_ms: u32) void; + +pub fn main() void { + _ = add(1, 2); + while (true) { + yield(16); // Yield for 1 second + } +} + +export fn add(a: i32, b: i32) i32 { + return a + b; +}