PSP 上的 Wasm TinyGo

2024-12-21 19:10:56 编辑:抖狐科技 来源:摘自互联网

本文介绍了如何在psp上运行go语言程序的非传统方法:通过tinygo将go代码编译成webassembly (wasm),再利用wasm3运行时在psp上执行。原生编译go到psp几乎不可能,因为go的mipsle架构支持依赖操作系统。 作者选择wasm作为中间层,绕过了这个难题。

核心代码片段展示了一个简单的“Hello, world!”示例:

package main

import "unsafe"

//go:wasmimport debug println
func println(ptr unsafe.pointer, len int32)

//export start
func start() {
    main()
}

func main() {
    message := "hello, webassembly, from golang!"
    println(unsafe.pointer(unsafe.stringdata(message)), int32(len(message)))
}

登录后复制

该代码调用运行时提供的println函数。 运行时本身使用C语言和Wasm3库实现,println函数的C语言实现如下:

#define printf pspDebugScreenPrintf

static const void* host_debug_println(IM3Runtime runtime, IM3ImportContext ctx, uint64_t *stack, void *mem)
{
    uint32_t ptr = (uint32_t) stack[0];
    uint32_t length = (uint32_t) stack[1];

    uint8_t* bytes = (uint8_t*)mem + ptr;

    char buffer[256];
    if (length >= sizeof(buffer)) {
        length = sizeof(buffer)-1;
    }
    memcpy(buffer, bytes, length);
    buffer[length] = '';

    printf("%s
", buffer);

    return NULL;
}

登录后复制

该实现将Wasm传递的内存指针和长度转换为C字符串,并使用pspDebugScreenPrintf打印到PSP屏幕。

作者还指出,尝试使用Rust和Wasmi作为运行时在模拟器上成功,但在实际PSP硬件上失败了,怀疑是内核模式相关的问题。 最终,C语言和Wasm3的组合成功地在PSP上运行了Wasm代码。

PSP 上的 Wasm TinyGo

项目中面临的挑战包括:

  • Wasm3交叉编译: 需要使用PSP工具链编译Wasm3。作者为此创建了一个fork的仓库:wasm3-for-psp。
  • TinyGo构建选项: 找到正确的TinyGo构建命令(tinygo build -o hello.wasm -target=wasm -no-debug main.go)花费了大量时间。
  • 导出主函数: 由于目标并非WASI,需要显式导出start函数作为程序入口点。

作者认为,这种方法可以扩展到其他支持Wasm编译的语言,并鼓励其他开发者尝试。

以上就是PSP 上的 Wasm TinyGo的详细内容,更多请关注抖狐科技其它相关文章!

本站文章均为抖狐网站建设摘自权威资料,书籍,或网络原创文章,如有版权纠纷或者违规问题,请即刻联系我们删除,我们欢迎您分享,引用和转载,我们谢绝直接复制和抄袭!感谢...
我们猜你喜欢