Implement nova64 API with native yield function and registration

This commit is contained in:
Lukas Höppner
2026-08-10 12:43:13 +02:00
parent 1cfcd5a169
commit 27c3aaf88f
7 changed files with 79 additions and 4 deletions
+6
View File
@@ -0,0 +1,6 @@
#ifndef NOVA64_API_H
#define NOVA64_API_H
void register_nova64_imports(void);
#endif // NOVA64_API_H
+24
View File
@@ -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));
}
+29 -1
View File
@@ -1,6 +1,7 @@
#include "wasm_runner.h" #include "wasm_runner.h"
#include "nova64_fs.h" #include "nova64_fs.h"
#include "nova64_internal.h" #include "nova64_internal.h"
#include "nova64_api.h"
#include "wasm_export.h" #include "wasm_export.h"
#include <stdlib.h> #include <stdlib.h>
@@ -17,6 +18,7 @@ uint32_t stack_size = 8092, heap_size = 8092;
bool wasm_runner_initialize(void) { bool wasm_runner_initialize(void) {
wasm_runtime_init(); wasm_runtime_init();
register_nova64_imports();
nova64_log("WASM runtime initialized successfully.\n"); nova64_log("WASM runtime initialized successfully.\n");
const char *module_file = "SYS:/init.wasm"; 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( wasm_module_inst_t module_inst = wasm_runtime_instantiate(
module, stack_size, heap_size, error_buf, sizeof(error_buf)); 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; return true;
} }
Binary file not shown.
+4 -1
View File
@@ -1 +1,4 @@
tinygo build -o ../simulator/sd/slot0/init.wasm -target wasm-unknown init.go @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
+3 -1
View File
@@ -2,7 +2,9 @@ package main
// Declare a main function, this is the entrypoint into our go module // Declare a main function, this is the entrypoint into our go module
// That will be run. In our example, we won't need this // That will be run. In our example, we won't need this
func main() {} func main() {
_ = add(1, 2)
}
// This exports an add function. // This exports an add function.
// It takes in two 32-bit integer values // It takes in two 32-bit integer values
+12
View File
@@ -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;
}