|
| 1 | +/* |
| 2 | + * Wasm3 - high performance WebAssembly interpreter written in C. |
| 3 | + * Copyright © 2020 Volodymyr Shymanskyy, Steven Massey. |
| 4 | + * All rights reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <wasm3.h> |
| 8 | + |
| 9 | +// You can redefine the default LED pin here, if needed |
| 10 | +//#define LED_BUILTIN D7 |
| 11 | + |
| 12 | +#ifndef LED_BUILTIN |
| 13 | +#define LED_BUILTIN 13 |
| 14 | +#endif |
| 15 | + |
| 16 | +/* |
| 17 | + * WebAssembly app |
| 18 | + * |
| 19 | + * This is essentially a simple "Blink" sketch, compiled to WebAssembly |
| 20 | + * The file was generated from ./wasm_apps/cpp |
| 21 | + * You can also find examples for Rust, TinyGO |
| 22 | + */ |
| 23 | + |
| 24 | +unsigned char app_wasm[] = { |
| 25 | + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x11, 0x04, 0x60, |
| 26 | + 0x02, 0x7f, 0x7f, 0x00, 0x60, 0x00, 0x00, 0x60, 0x01, 0x7f, 0x00, 0x60, |
| 27 | + 0x00, 0x01, 0x7f, 0x02, 0x4e, 0x04, 0x07, 0x61, 0x72, 0x64, 0x75, 0x69, |
| 28 | + 0x6e, 0x6f, 0x09, 0x67, 0x65, 0x74, 0x50, 0x69, 0x6e, 0x4c, 0x45, 0x44, |
| 29 | + 0x00, 0x03, 0x07, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x07, 0x70, |
| 30 | + 0x69, 0x6e, 0x4d, 0x6f, 0x64, 0x65, 0x00, 0x00, 0x07, 0x61, 0x72, 0x64, |
| 31 | + 0x75, 0x69, 0x6e, 0x6f, 0x0c, 0x64, 0x69, 0x67, 0x69, 0x74, 0x61, 0x6c, |
| 32 | + 0x57, 0x72, 0x69, 0x74, 0x65, 0x00, 0x00, 0x07, 0x61, 0x72, 0x64, 0x75, |
| 33 | + 0x69, 0x6e, 0x6f, 0x05, 0x64, 0x65, 0x6c, 0x61, 0x79, 0x00, 0x02, 0x03, |
| 34 | + 0x02, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07, 0x13, 0x02, 0x06, |
| 35 | + 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x06, 0x5f, 0x73, 0x74, |
| 36 | + 0x61, 0x72, 0x74, 0x00, 0x04, 0x0a, 0x3a, 0x01, 0x38, 0x01, 0x01, 0x7f, |
| 37 | + 0x41, 0x80, 0x08, 0x10, 0x00, 0x22, 0x00, 0x36, 0x02, 0x00, 0x20, 0x00, |
| 38 | + 0x41, 0x01, 0x10, 0x01, 0x03, 0x40, 0x41, 0x80, 0x08, 0x28, 0x02, 0x00, |
| 39 | + 0x41, 0x01, 0x10, 0x02, 0x41, 0xe4, 0x00, 0x10, 0x03, 0x41, 0x80, 0x08, |
| 40 | + 0x28, 0x02, 0x00, 0x41, 0x00, 0x10, 0x02, 0x41, 0x84, 0x07, 0x10, 0x03, |
| 41 | + 0x0c, 0x00, 0x0b, 0x00, 0x0b |
| 42 | +}; |
| 43 | +unsigned int app_wasm_len = 197; |
| 44 | + |
| 45 | +/* |
| 46 | + * API bindings |
| 47 | + * |
| 48 | + * Note: each RawFunction should complete with one of these calls: |
| 49 | + * m3ApiReturn(val) - Returns a value |
| 50 | + * m3ApiSuccess() - Returns void (and no traps) |
| 51 | + * m3ApiTrap(trap) - Returns a trap |
| 52 | + */ |
| 53 | + |
| 54 | +#include <m3_api_defs.h> |
| 55 | +#include <m3_env.h> |
| 56 | + |
| 57 | +m3ApiRawFunction(m3_arduino_millis) |
| 58 | +{ |
| 59 | + m3ApiReturnType (uint32_t) |
| 60 | + |
| 61 | + m3ApiReturn(millis()); |
| 62 | +} |
| 63 | + |
| 64 | +m3ApiRawFunction(m3_arduino_delay) |
| 65 | +{ |
| 66 | + m3ApiGetArg (uint32_t, ms) |
| 67 | + |
| 68 | + // You can also trace API calls |
| 69 | + //Serial.print("api: delay "); Serial.println(ms); |
| 70 | + |
| 71 | + delay(ms); |
| 72 | + |
| 73 | + m3ApiSuccess(); |
| 74 | +} |
| 75 | + |
| 76 | +// This maps pin modes from arduino_wasm_api.h |
| 77 | +// to actual platform-specific values |
| 78 | +uint8_t mapPinMode(uint8_t mode) |
| 79 | +{ |
| 80 | + switch(mode) { |
| 81 | + case (0): return INPUT; |
| 82 | + case (1): return OUTPUT; |
| 83 | + case (2): return INPUT_PULLUP; |
| 84 | + } |
| 85 | + return INPUT; |
| 86 | +} |
| 87 | + |
| 88 | +m3ApiRawFunction(m3_arduino_pinMode) |
| 89 | +{ |
| 90 | + m3ApiGetArg (uint32_t, pin) |
| 91 | + m3ApiGetArg (uint32_t, mode) |
| 92 | + |
| 93 | +#if !defined(PARTICLE) |
| 94 | + typedef uint8_t PinMode; |
| 95 | +#endif |
| 96 | + pinMode(pin, (PinMode)mapPinMode(mode)); |
| 97 | + |
| 98 | + m3ApiSuccess(); |
| 99 | +} |
| 100 | + |
| 101 | +m3ApiRawFunction(m3_arduino_digitalWrite) |
| 102 | +{ |
| 103 | + m3ApiGetArg (uint32_t, pin) |
| 104 | + m3ApiGetArg (uint32_t, value) |
| 105 | + |
| 106 | + digitalWrite(pin, value); |
| 107 | + |
| 108 | + m3ApiSuccess(); |
| 109 | +} |
| 110 | + |
| 111 | +// This is a convenience function |
| 112 | +m3ApiRawFunction(m3_arduino_getPinLED) |
| 113 | +{ |
| 114 | + m3ApiReturnType (uint32_t) |
| 115 | + |
| 116 | + m3ApiReturn(LED_BUILTIN); |
| 117 | +} |
| 118 | + |
| 119 | +// Dummy, for TinyGO |
| 120 | +m3ApiRawFunction(m3_dummy) |
| 121 | +{ |
| 122 | + m3ApiSuccess(); |
| 123 | +} |
| 124 | + |
| 125 | +M3Result m3_LinkArduino (IM3Runtime runtime) |
| 126 | +{ |
| 127 | + IM3Module module = runtime->modules; |
| 128 | + const char* arduino = "arduino"; |
| 129 | + |
| 130 | + m3_LinkRawFunction (module, arduino, "millis", "i()", &m3_arduino_millis); |
| 131 | + m3_LinkRawFunction (module, arduino, "delay", "v(i)", &m3_arduino_delay); |
| 132 | + m3_LinkRawFunction (module, arduino, "pinMode", "v(ii)", &m3_arduino_pinMode); |
| 133 | + m3_LinkRawFunction (module, arduino, "digitalWrite", "v(ii)", &m3_arduino_digitalWrite); |
| 134 | + |
| 135 | + m3_LinkRawFunction (module, arduino, "getPinLED", "i()", &m3_arduino_getPinLED); |
| 136 | + |
| 137 | + m3_LinkRawFunction (module, "env", "io_get_stdout", "i()", &m3_dummy); |
| 138 | + |
| 139 | + return m3Err_none; |
| 140 | +} |
| 141 | + |
| 142 | +/* |
| 143 | + * Engine start, liftoff! |
| 144 | + */ |
| 145 | + |
| 146 | +#define FATAL(func, msg) { Serial.print("Fatal: " func " "); Serial.println(msg); return; } |
| 147 | + |
| 148 | +void wasm_task(void*) |
| 149 | +{ |
| 150 | + M3Result result = m3Err_none; |
| 151 | + |
| 152 | + IM3Environment env = m3_NewEnvironment (); |
| 153 | + if (!env) FATAL("m3_NewEnvironment", "failed"); |
| 154 | + |
| 155 | + IM3Runtime runtime = m3_NewRuntime (env, 1024, NULL); |
| 156 | + if (!runtime) FATAL("m3_NewRuntime", "failed"); |
| 157 | + |
| 158 | + IM3Module module; |
| 159 | + result = m3_ParseModule (env, &module, app_wasm, app_wasm_len-1); |
| 160 | + if (result) FATAL("m3_ParseModule", result); |
| 161 | + |
| 162 | + result = m3_LoadModule (runtime, module); |
| 163 | + if (result) FATAL("m3_LoadModule", result); |
| 164 | + |
| 165 | + result = m3_LinkArduino (runtime); |
| 166 | + if (result) FATAL("m3_LinkArduino", result); |
| 167 | + |
| 168 | + IM3Function f; |
| 169 | + result = m3_FindFunction (&f, runtime, "_start"); |
| 170 | + if (result) FATAL("m3_FindFunction", result); |
| 171 | + |
| 172 | + Serial.println("Running WebAssembly...\n"); |
| 173 | + |
| 174 | + const char* i_argv[1] = { NULL }; |
| 175 | + result = m3_CallWithArgs (f, 0, i_argv); |
| 176 | + |
| 177 | + if (result) FATAL("m3_CallWithArgs", result); |
| 178 | + |
| 179 | + // Should not arrive here |
| 180 | +} |
| 181 | + |
| 182 | +void setup() |
| 183 | +{ |
| 184 | + Serial.begin(115200); |
| 185 | + delay(100); |
| 186 | + |
| 187 | + Serial.println("\nWasm3 v" M3_VERSION ", build " __DATE__ " " __TIME__); |
| 188 | + |
| 189 | +#ifdef ESP32 |
| 190 | + // On ESP32, we can launch in a separate thread |
| 191 | + xTaskCreate(&wasm_task, "wasm3", 32768, NULL, 5, NULL); |
| 192 | +#else |
| 193 | + wasm_task(NULL); |
| 194 | +#endif |
| 195 | +} |
| 196 | + |
| 197 | +void loop() |
| 198 | +{ |
| 199 | + delay(100); |
| 200 | +} |
0 commit comments