-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
154 lines (126 loc) · 3.53 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdio.h>
#include <string.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspiofilemgr.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include "wasm3.h"
#include "m3_api_wasi.h"
#define printf pspDebugScreenPrintf
PSP_MODULE_INFO("Wasm3Example", 0, 1, 0);
PSP_HEAP_SIZE_KB(1024);
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;
}
static void waitForUser()
{
SceCtrlData pad;
sceCtrlSetSamplingMode(PSP_CTRL_MODE_DIGITAL);
printf("\nPress START to exit.\n");
while (1) {
sceCtrlPeekBufferPositive(&pad, 1);
if (pad.Buttons & PSP_CTRL_START) {
break;
}
sceDisplayWaitVblankStart();
}
sceKernelExitGame();
}
int main(int argc, char *argv[])
{
pspDebugScreenInit();
SceUID fd = sceIoOpen("hello.wasm", PSP_O_RDONLY, 0777);
if (fd < 0) {
printf("Failed to open hello.wasm\n");
waitForUser();
return -1;
}
SceOff size = sceIoLseek(fd, 0, PSP_SEEK_END);
sceIoLseek(fd, 0, PSP_SEEK_SET);
uint8_t* wasmBytes = (uint8_t*)malloc(size);
if (!wasmBytes) {
printf("Failed to allocate memory for WASM\n");
sceIoClose(fd);
waitForUser();
return -1;
}
if (sceIoRead(fd, wasmBytes, size) != size) {
printf("Failed to read WASM file\n");
free(wasmBytes);
sceIoClose(fd);
waitForUser();
return -1;
}
sceIoClose(fd);
IM3Environment env = m3_NewEnvironment();
if (!env) {
printf("m3_NewEnvironment failed\n");
free(wasmBytes);
waitForUser();
return -1;
}
IM3Runtime runtime = m3_NewRuntime(env, 64*1024, NULL);
if (!runtime) {
printf("m3_NewRuntime failed\n");
m3_FreeEnvironment(env);
free(wasmBytes);
waitForUser();
return -1;
}
IM3Module module;
M3Result result = m3_ParseModule(env, &module, wasmBytes, (uint32_t)size);
free(wasmBytes);
if (result != m3Err_none) {
printf("m3_ParseModule: %s\n", result);
m3_FreeRuntime(runtime);
m3_FreeEnvironment(env);
waitForUser();
return -1;
}
result = m3_LoadModule(runtime, module);
if (result != m3Err_none) {
printf("m3_LoadModule: %s\n", result);
m3_FreeRuntime(runtime);
m3_FreeEnvironment(env);
waitForUser();
return -1;
}
// "v(ii)" = void(i32,i32)
result = m3_LinkRawFunction(module, "debug", "println", "v(ii)", &host_debug_println);
if (result != m3Err_none) {
printf("m3_LinkRawFunction: %s\n", result);
m3_FreeRuntime(runtime);
m3_FreeEnvironment(env);
waitForUser();
return -1;
}
IM3Function f;
result = m3_FindFunction(&f, runtime, "start");
if (result != m3Err_none) {
printf("m3_FindFunction(start): %s\n", result);
m3_FreeRuntime(runtime);
m3_FreeEnvironment(env);
waitForUser();
return -1;
}
result = m3_CallV(f);
if (result != m3Err_none) {
printf("m3_Call(start): %s\n", result);
}
m3_FreeRuntime(runtime);
m3_FreeEnvironment(env);
waitForUser();
return 0;
}