Skip to content

Commit 76afb08

Browse files
committed
Optimize memory, update comments. Press start to reload the game!
1 parent 17f6ccc commit 76afb08

File tree

4 files changed

+243
-218
lines changed

4 files changed

+243
-218
lines changed

examples/Wasm_PyBadge_Dino/Wasm_PyBadge_Dino.ino

Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* Engine start, liftoff!
2525
*/
2626

27-
#define FATAL(func, msg) { Serial.print("Fatal: " func " "); Serial.println(msg); return; }
27+
#define FATAL(func, msg) { Serial.print("Fatal: " func " "); Serial.println(msg); while(1) { delay(100); } }
2828
#define TSTART() { tstart = micros(); }
2929
#define TFINISH(s) { tend = micros(); Serial.print(s " in "); Serial.print(tend-tstart); Serial.println(" us"); }
3030

@@ -65,82 +65,107 @@ M3Result LinkImports (IM3Runtime runtime)
6565

6666
Adafruit_Arcada arcada;
6767

68-
void setup()
68+
IM3Environment env;
69+
IM3Runtime runtime;
70+
IM3Module module;
71+
IM3Function func_run;
72+
uint8_t* mem;
73+
74+
void load_wasm()
6975
{
70-
Serial.begin(115200);
76+
M3Result result = m3Err_none;
77+
78+
if (!env) {
79+
env = m3_NewEnvironment ();
80+
if (!env) FATAL("NewEnvironment", "failed");
81+
}
7182

72-
// Wait for serial port to connect
73-
// Needed for native USB port only
74-
//while(!Serial) {}
83+
m3_FreeRuntime(runtime);
7584

76-
uint32_t tend, tstart;
77-
TSTART();
85+
runtime = m3_NewRuntime (env, 1024, NULL);
86+
if (!runtime) FATAL("NewRuntime", "failed");
7887

88+
result = m3_ParseModule (env, &module, dino_wasm, sizeof(dino_wasm));
89+
if (result) FATAL("ParseModule", result);
90+
91+
result = m3_LoadModule (runtime, module);
92+
if (result) FATAL("LoadModule", result);
93+
94+
result = LinkImports (runtime);
95+
if (result) FATAL("LinkImports", result);
96+
97+
mem = m3_GetMemory (runtime, NULL, 0);
98+
if (!mem) FATAL("GetMemory", "failed");
99+
100+
result = m3_FindFunction (&func_run, runtime, "run");
101+
if (result) FATAL("FindFunction", result);
102+
}
103+
104+
void init_random()
105+
{
79106
// Try to randomize seed
80107
randomSeed((analogRead(A5) << 16) + analogRead(A4));
81108
Serial.print("Random: 0x"); Serial.println(random(INT_MAX), HEX);
109+
}
82110

83-
// Start TFT and fill black
111+
void init_arcada()
112+
{
84113
if (!arcada.arcadaBegin()) {
85-
Serial.print("Failed to begin");
86-
while (1);
114+
FATAL("arcadaBegin", "failed");
87115
}
88116
arcada.displayBegin();
89117
arcada.setBacklight(128);
118+
}
90119

91-
TFINISH("Arcada init");
92-
120+
void display_info()
121+
{
93122
arcada.display->fillScreen(ARCADA_WHITE);
94123
arcada.display->setTextColor(ARCADA_BLACK);
95124
arcada.display->setTextWrap(true);
96125
arcada.display->setCursor(0, 5);
97126
arcada.display->println(" Wasm3 v" M3_VERSION " (" M3_ARCH ")");
98127
arcada.display->println();
99128
arcada.display->println(" Dino game");
100-
arcada.display->println(" by Ben Smith (binji)");
101-
102-
TSTART();
103-
104-
M3Result result = m3Err_none;
105-
106-
IM3Environment env = m3_NewEnvironment ();
107-
if (!env) FATAL("NewEnvironment", "failed");
108-
109-
IM3Runtime runtime = m3_NewRuntime (env, 1024, NULL);
110-
if (!runtime) FATAL("NewRuntime", "failed");
111-
112-
IM3Module module;
113-
result = m3_ParseModule (env, &module, dino_wasm, sizeof(dino_wasm));
114-
if (result) FATAL("ParseModule", result);
115-
116-
result = m3_LoadModule (runtime, module);
117-
if (result) FATAL("LoadModule", result);
129+
arcada.display->println(" by Ben Smith (binji)");
130+
}
118131

119-
result = LinkImports (runtime);
120-
if (result) FATAL("LinkImports", result);
132+
void setup()
133+
{
134+
Serial.begin(115200);
121135

122-
TFINISH("Wasm3 init+parse");
136+
// Wait for serial port to connect
137+
// Needed for native USB port only
138+
//while(!Serial) {}
123139

140+
uint32_t tend, tstart;
124141
TSTART();
142+
init_random();
143+
init_arcada();
144+
display_info();
145+
TFINISH("Arcada init");
125146

126-
IM3Function f;
127-
result = m3_FindFunction (&f, runtime, "run");
128-
if (result) FATAL("FindFunction", result);
129-
130-
TFINISH("Compile run()");
147+
TSTART();
148+
load_wasm();
149+
TFINISH("Wasm3 init");
131150

132151
Serial.println("Running WebAssembly...");
133152

153+
M3Result result;
134154
const char* i_argv[1] = { NULL };
135-
uint8_t* mem = m3_GetMemory(runtime, NULL, 0);
136155

137156
while (true) {
138157
const uint32_t framestart = millis();
139158

140159
// Process inputs
141160
uint32_t pressed_buttons = arcada.readButtons();
142161
if (pressed_buttons & ARCADA_BUTTONMASK_START) {
143-
NVIC_SystemReset();
162+
//NVIC_SystemReset();
163+
164+
// Restart Dino game
165+
display_info();
166+
load_wasm();
167+
delay(100);
168+
144169
} else if (pressed_buttons & ARCADA_BUTTONMASK_A && pressed_buttons & ARCADA_BUTTONMASK_B) {
145170
*(uint32_t*)(mem) = 3;
146171
} else if (pressed_buttons & ARCADA_BUTTONMASK_B) { // Down
@@ -152,7 +177,7 @@ void setup()
152177
}
153178

154179
// Render frame
155-
result = m3_CallWithArgs (f, 0, i_argv);
180+
result = m3_CallWithArgs (func_run, 0, i_argv);
156181
if (result) break;
157182
arcada.display->drawRGBBitmap(0, 40, (uint16_t*)(mem+0x5000), 160, 75);
158183

-7 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)