|
| 1 | +/* |
| 2 | + * Wasm3 - high performance WebAssembly interpreter written in C. |
| 3 | + * Copyright © 2021 Volodymyr Shymanskyy, Steven Massey. |
| 4 | + * All rights reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <TFT_eSPI.h> |
| 8 | +#include <SPI.h> |
| 9 | +#include "NotoSansBold15.h" |
| 10 | + |
| 11 | +#include <wasm3.h> |
| 12 | +#include <m3_api_defs.h> |
| 13 | + |
| 14 | +#define NATIVE_STACK_SIZE (32*1024) |
| 15 | + |
| 16 | +#define BUTTON_UP 35 |
| 17 | +#define BUTTON_DOWN 0 |
| 18 | + |
| 19 | +#define DISPLAY_BRIGHTESS 1 // 0..255 |
| 20 | + |
| 21 | +TFT_eSPI tft = TFT_eSPI(135, 240); // Invoke custom library |
| 22 | + |
| 23 | +/* |
| 24 | + * Dino game by by Ben Smith (binji) |
| 25 | + * https://github.com/binji/raw-wasm/tree/master/dino |
| 26 | + * To build: |
| 27 | + * export PATH=/opt/wasp/build/src/tools:$PATH |
| 28 | + * wasp wat2wasm --enable-numeric-values -o dino.wasm dino.wat |
| 29 | + * xxd -iC dino.wasm > dino.wasm.h |
| 30 | + * |
| 31 | + * Note: In Arduino IDE, select Tools->Optimize->Faster (-O3) |
| 32 | + */ |
| 33 | +#include "dino.wasm.h" |
| 34 | + |
| 35 | +/* |
| 36 | + * Engine start, liftoff! |
| 37 | + */ |
| 38 | + |
| 39 | +#define FATAL(func, msg) { Serial.print("Fatal: " func " "); Serial.println(msg); while(1) { delay(100); } } |
| 40 | +#define TSTART() { tstart = micros(); } |
| 41 | +#define TFINISH(s) { tend = micros(); Serial.print(s " in "); Serial.print(tend-tstart); Serial.println(" us"); } |
| 42 | + |
| 43 | +// The Math.random() function returns a floating-point, |
| 44 | +// pseudo-random number in the range 0 to less than 1 |
| 45 | +m3ApiRawFunction(Math_random) |
| 46 | +{ |
| 47 | + m3ApiReturnType (float) |
| 48 | + |
| 49 | + float r = (float)random(INT_MAX)/INT_MAX; |
| 50 | + //Serial.print("Random: "); Serial.println(r); |
| 51 | + |
| 52 | + m3ApiReturn(r); |
| 53 | +} |
| 54 | + |
| 55 | +// Memcpy is generic, and much faster in native code |
| 56 | +m3ApiRawFunction(Dino_memcpy) |
| 57 | +{ |
| 58 | + m3ApiGetArgMem (uint8_t *, dst) |
| 59 | + m3ApiGetArgMem (uint8_t *, src) |
| 60 | + m3ApiGetArgMem (uint8_t *, dstend) |
| 61 | + |
| 62 | + do { |
| 63 | + *dst++ = *src++; |
| 64 | + } while (dst < dstend); |
| 65 | + |
| 66 | + m3ApiSuccess(); |
| 67 | +} |
| 68 | + |
| 69 | +IM3Environment env; |
| 70 | +IM3Runtime runtime; |
| 71 | +IM3Module module; |
| 72 | +IM3Function func_run; |
| 73 | +uint8_t* mem; |
| 74 | + |
| 75 | +void load_wasm() |
| 76 | +{ |
| 77 | + M3Result result = m3Err_none; |
| 78 | + |
| 79 | + if (!env) { |
| 80 | + env = m3_NewEnvironment (); |
| 81 | + if (!env) FATAL("NewEnvironment", "failed"); |
| 82 | + } |
| 83 | + |
| 84 | + m3_FreeRuntime(runtime); |
| 85 | + |
| 86 | + runtime = m3_NewRuntime (env, 1024, NULL); |
| 87 | + if (!runtime) FATAL("NewRuntime", "failed"); |
| 88 | + |
| 89 | + result = m3_ParseModule (env, &module, dino_wasm, sizeof(dino_wasm)); |
| 90 | + if (result) FATAL("ParseModule", result); |
| 91 | + |
| 92 | + result = m3_LoadModule (runtime, module); |
| 93 | + if (result) FATAL("LoadModule", result); |
| 94 | + |
| 95 | + m3_LinkRawFunction (module, "Math", "random", "f()", &Math_random); |
| 96 | + m3_LinkRawFunction (module, "Dino", "memcpy", "v(iii)", &Dino_memcpy); |
| 97 | + |
| 98 | + mem = m3_GetMemory (runtime, NULL, 0); |
| 99 | + if (!mem) FATAL("GetMemory", "failed"); |
| 100 | + |
| 101 | + result = m3_FindFunction (&func_run, runtime, "run"); |
| 102 | + if (result) FATAL("FindFunction", result); |
| 103 | +} |
| 104 | + |
| 105 | +void init_device() |
| 106 | +{ |
| 107 | + pinMode(BUTTON_UP, INPUT); |
| 108 | + pinMode(BUTTON_DOWN, INPUT); |
| 109 | + |
| 110 | + // Try to randomize seed |
| 111 | + randomSeed((analogRead(A5) << 16) + analogRead(A4)); |
| 112 | + Serial.print("Random: 0x"); Serial.println(random(INT_MAX), HEX); |
| 113 | + |
| 114 | + tft.init(); |
| 115 | + tft.setRotation(1); |
| 116 | + tft.setSwapBytes(true); |
| 117 | + tft.loadFont(NotoSansBold15); |
| 118 | + |
| 119 | + // Set backlight brightness |
| 120 | + ledcSetup(0, 5000, 8); |
| 121 | + ledcAttachPin(TFT_BL, 0); |
| 122 | + ledcWrite(0, DISPLAY_BRIGHTESS); |
| 123 | +} |
| 124 | + |
| 125 | +void display_info() |
| 126 | +{ |
| 127 | + tft.fillScreen(TFT_WHITE); |
| 128 | + tft.setTextColor(TFT_BLACK, TFT_WHITE); |
| 129 | + tft.setCursor(0, 5); |
| 130 | + tft.println(" Wasm3 v" M3_VERSION " (ESP32)"); |
| 131 | + tft.println(" Dino game"); |
| 132 | + tft.println(" by Ben Smith (binji)"); |
| 133 | +} |
| 134 | + |
| 135 | +void setup() |
| 136 | +{ |
| 137 | + Serial.begin(115200); |
| 138 | + |
| 139 | + Serial.println("\nWasm3 v" M3_VERSION " (" M3_ARCH "), build " __DATE__ " " __TIME__); |
| 140 | + |
| 141 | + uint32_t tend, tstart; |
| 142 | + TSTART(); |
| 143 | + init_device(); |
| 144 | + display_info(); |
| 145 | + TFINISH("Device init"); |
| 146 | + |
| 147 | + TSTART(); |
| 148 | + load_wasm(); |
| 149 | + TFINISH("Wasm3 init"); |
| 150 | + |
| 151 | + Serial.println("Running WebAssembly..."); |
| 152 | + |
| 153 | + xTaskCreate(&wasm_task, "wasm3", NATIVE_STACK_SIZE, NULL, 5, NULL); |
| 154 | +} |
| 155 | + |
| 156 | +void wasm_task(void*) |
| 157 | +{ |
| 158 | + M3Result result; |
| 159 | + uint64_t last_fps_print = 0; |
| 160 | + |
| 161 | + while (true) { |
| 162 | + const uint64_t framestart = micros(); |
| 163 | + |
| 164 | + // Process inputs |
| 165 | + uint32_t* input = (uint32_t*)(mem + 0x0000); |
| 166 | + *input = 0; |
| 167 | + if (LOW == digitalRead(BUTTON_UP)) { |
| 168 | + *input |= 0x1; |
| 169 | + } |
| 170 | + if (LOW == digitalRead(BUTTON_DOWN)) { |
| 171 | + *input |= 0x2; |
| 172 | + } |
| 173 | + |
| 174 | + // Render frame |
| 175 | + result = m3_CallV (func_run); |
| 176 | + if (result) break; |
| 177 | + |
| 178 | + // Output to display |
| 179 | + tft.pushImage(0, 135-10-75, 240, 75, (uint16_t*)(mem+0x5000)); |
| 180 | + |
| 181 | + const uint64_t frametime = micros() - framestart; |
| 182 | + const uint32_t target_frametime = 1000000/50; |
| 183 | + if (target_frametime > frametime) { |
| 184 | + delay((target_frametime - frametime)/1000); |
| 185 | + } |
| 186 | + if (framestart - last_fps_print > 1000000) { |
| 187 | + Serial.print("FPS: "); Serial.println((uint32_t)(1000000/frametime)); |
| 188 | + last_fps_print = framestart; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + if (result != m3Err_none) { |
| 193 | + M3ErrorInfo info; |
| 194 | + m3_GetErrorInfo (runtime, &info); |
| 195 | + Serial.print("Error: "); |
| 196 | + Serial.print(result); |
| 197 | + Serial.print(" ("); |
| 198 | + Serial.print(info.message); |
| 199 | + Serial.println(")"); |
| 200 | + if (info.file && strlen(info.file) && info.line) { |
| 201 | + Serial.print("At "); |
| 202 | + Serial.print(info.file); |
| 203 | + Serial.print(":"); |
| 204 | + Serial.println(info.line); |
| 205 | + } |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +void loop() |
| 210 | +{ |
| 211 | + delay(100); |
| 212 | +} |
0 commit comments