Implement nova64 API with native yield function and registration
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
#ifndef NOVA64_API_H
|
||||
#define NOVA64_API_H
|
||||
|
||||
void register_nova64_imports(void);
|
||||
|
||||
#endif // NOVA64_API_H
|
||||
@@ -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));
|
||||
}
|
||||
+30
-2
@@ -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 <stdlib.h>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Binary file not shown.
+4
-1
@@ -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
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user