我決定嘗試在 PSP 上使用 Golang,並探索了多種方法,包括移植 Clang、使用 TinyGo 以及使用 GopherJS 將 Golang 編譯為 JavaScript。然後,我在 PSP 自製程式 Discord 上遇到了一位名為 aethiopicuschan 的用戶,他正在使用 WebAssembly 嘗試實現相同的目標。他的範例在 PPSSPP 上有效,但在實際硬體上無效。
無論如何,由於即將到來的考試,我不得不暫停我的專案。但是,幾個月後,我發現一篇文章,aethiopicuschan 成功地將 Golang 編譯為 WASM,並使用 WASM 解釋器在 PSP 上運行它。
他的方法使用了一個名為 Wasm3 的 Wasm 解釋器來運行程式碼,但我知道我可以做得更好。我知道像 Wasm2C 這樣的項目,可以將 Wasm 二進位轉換為 C。
我很興奮,更深入地研究了這個主題,並發現了一篇有關將 WebAssembly 編譯為可移植 C 程式碼的文章。他們使用了一個名為 w2c2 的編譯器,我猜這是續集。
經過幾個小時的修改 CMake,我成功地使用 TinyGo 並針對 WASI 創建了一個工作範例。我還封裝了一個 raylib 函數 InitWindow(順便說一下,psp 有一個 raylib 連接埠),目的是將 raylib-go 綁定移植到這個 WASM-to-C 平台。此範例成功將 C InitWindow 函數綁定到編譯後的 WASM 程式碼。
如您所見,它看起來與任何其他 golang 程式碼
package main import "time" import rl "github.com/gen2brain/raylib-go/raylib" func main() { rl.InitWindow(480, 272, "Psp test") for { time.Sleep(time.Millisecond * 16) } }
但是在 rl 套件中,我們導入了一個 C 函數,我們也給它一個函數簽名。請記住。
package rl //go:wasmimport rl InitWindow func c_InitWindow(width int32, height int32, title string) // InitWindow - Initialize Window and OpenGL Graphics func InitWindow(width int32, height int32, title string) { c_InitWindow(width, height, title) }
讓我們一步一步分解這段程式碼。
#define __GLIBC_USE #include <raylib.h> #include <stdio.h> #include <time.h> #include <pspkernel.h> PSP_MODULE_INFO("WasiExample", 0, 1, 0); PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER); #include "pspdebug.h" #define printf pspDebugScreenPrintf </pspkernel.h></time.h></stdio.h></raylib.h>
首先我們導入 psp 庫,並設定樣板,我們也導入 raylib 連接埠
這裡我們導入編譯後的模組app.h
我們也導入了 w2c2 提供的 wasi「運行時」
// // the compiled wasm -> C code #include "app.h" // // wasi runtime #include "w2c2_base.h" #include "wasi.h" extern wasmMemory* e_memory; // the WASM memory. void trap(Trap trap) { fprintf(stderr, "TRAP: %s\n", trapDescription(trap)); abort(); } wasmMemory* wasiMemory(void* instance) { return app_memory((appInstance*)instance); } extern char** environ;
陷阱函數是樣板,
wasiMemory 函數是我為另一個輔助函數製作的輔助函數
char* getCStringFromMemory(void* memoryptr, U32 offset, U32 length) { wasmMemory* memory = wasiMemory(memoryptr); char* str = (char*)(memory->data + offset); char* result = (char*)malloc( length + 1); // Allocate space for the string + null terminator if (result == NULL) { fprintf(stderr, "Memory allocation failed\n"); return NULL; } // Copy the string from WASI memory to local memory for (U32 i = 0; i void rl__InitWindow( void* memoryptr, U32 width, U32 height, U32 offset, U32 length) { char* title = getCStringFromMemory(memoryptr, offset, length); InitWindow(width, height, title); bool ready = IsWindowReady(); if (ready) { // this will print to the psp screen. printf("Window was created"); } }'; // Null-terminate the string return result; }
這個函數所做的只是取得字串記憶體指標的記憶體指標(在 wasm 中,我們透過傳遞記憶體偏移量和字串的長度來處理字串),並使用偏移量和長度傳回一個 C 字串
這是我們從 golang 程式碼中呼叫的 C 函數,我必須自己定義它。
int main(int argc, char* argv[]) { pspDebugScreenInit(); appInstance i; appInstantiate(&i, NULL); if (!wasiInit(argc, argv, environ)) { fprintf(stderr, "failed to initialize WASI\n"); return 1; } app__start(&i); appFreeInstance(&i); return 0; }
我們正在讀取轉譯的 wasm 傳遞的參數,並將它們傳遞給 raylib。
這是更多的樣板文件,我們所做的就是運行 golang 程式碼的 main 函數,該函數導出為 app_start()
有用!
這是 psp 模擬器的螢幕截圖。
但它也適用於原始硬體。
有問題請在留言區留言!
以上是PSP(和其他語言)上的 Golang的詳細內容。更多資訊請關注PHP中文網其他相關文章!

掌握Go語言中的strings包可以提高文本處理能力和開發效率。 1)使用Contains函數檢查子字符串,2)用Index函數查找子字符串位置,3)Join函數高效拼接字符串切片,4)Replace函數替換子字符串。注意避免常見錯誤,如未檢查空字符串和大字符串操作性能問題。

你應該關心Go語言中的strings包,因為它能簡化字符串操作,使代碼更清晰高效。 1)使用strings.Join高效拼接字符串;2)用strings.Fields按空白符分割字符串;3)通過strings.Index和strings.LastIndex查找子串位置;4)用strings.ReplaceAll進行字符串替換;5)利用strings.Builder進行高效字符串拼接;6)始終驗證輸入以避免意外結果。

thestringspackageingoisesential forefficientstringManipulation.1)itoffersSimpleyetpoperfulfunctionsFortaskSlikeCheckingSslingSubstringsStringStringsStringsandStringsN.2)ithandhishiCodeDewell,withFunctionsLikestrings.fieldsfieldsfieldsfordsforeflikester.fieldsfordsforwhitespace-fieldsforwhitespace-separatedvalues.3)3)

WhendecidingbetweenGo'sbytespackageandstringspackage,usebytes.Bufferforbinarydataandstrings.Builderforstringoperations.1)Usebytes.Bufferforworkingwithbyteslices,binarydata,appendingdifferentdatatypes,andwritingtoio.Writer.2)Usestrings.Builderforstrin

Go的strings包提供了多種字符串操作功能。 1)使用strings.Contains檢查子字符串。 2)用strings.Split將字符串分割成子字符串切片。 3)通過strings.Join合併字符串。 4)用strings.TrimSpace或strings.Trim去除字符串首尾的空白或指定字符。 5)用strings.ReplaceAll替換所有指定子字符串。 6)使用strings.HasPrefix或strings.HasSuffix檢查字符串的前綴或後綴。

使用Go語言的strings包可以提升代碼質量。 1)使用strings.Join()優雅地連接字符串數組,避免性能開銷。 2)結合strings.Split()和strings.Contains()處理文本,注意大小寫敏感問題。 3)避免濫用strings.Replace(),考慮使用正則表達式進行大量替換。 4)使用strings.Builder提高頻繁拼接字符串的性能。

Go的bytes包提供了多種實用的函數來處理字節切片。 1.bytes.Contains用於檢查字節切片是否包含特定序列。 2.bytes.Split用於將字節切片分割成smallerpieces。 3.bytes.Join用於將多個字節切片連接成一個。 4.bytes.TrimSpace用於去除字節切片的前後空白。 5.bytes.Equal用於比較兩個字節切片是否相等。 6.bytes.Index用於查找子切片在largerslice中的起始索引。

theEncoding/binarypackageingoisesenebecapeitProvidesAstandArdArdArdArdArdArdArdArdAndWriteBinaryData,確保Cross-cross-platformCompatibilitiational and handhandlingdifferentendenness.itoffersfunctionslikeread,寫下,寫,dearte,readuvarint,andwriteuvarint,andWriteuvarIntforPreciseControloverBinary


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

記事本++7.3.1
好用且免費的程式碼編輯器