本文介绍了如何在psp上运行go语言程序的非传统方法:通过tinygo将go代码编译成webassembly (wasm),再利用wasm3运行时在psp上执行。原生编译go到psp几乎不可能,因为go的mipsle架构支持依赖操作系统。 作者选择wasm作为中间层,绕过了这个难题。
核心代码片段展示了一个简单的“Hello, world!”示例:
<code class="go">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)))
}</code>该代码调用运行时提供的println函数。 运行时本身使用C语言和Wasm3库实现,println函数的C语言实现如下:
<code class="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] = '\0';
printf("%s\n", buffer);
return NULL;
}</code>该实现将Wasm传递的内存指针和长度转换为C字符串,并使用pspDebugScreenPrintf打印到PSP屏幕。
作者还指出,尝试使用Rust和Wasmi作为运行时在模拟器上成功,但在实际PSP硬件上失败了,怀疑是内核模式相关的问题。 最终,C语言和Wasm3的组合成功地在PSP上运行了Wasm代码。

项目中面临的挑战包括:
tinygo build -o hello.wasm -target=wasm -no-debug main.go)花费了大量时间。start函数作为程序入口点。作者认为,这种方法可以扩展到其他支持Wasm编译的语言,并鼓励其他开发者尝试。
以上就是PSP 上的 Wasm TinyGo的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号