7
7
#include " Adafruit_Arcada.h"
8
8
9
9
#include < wasm3.h>
10
- #include < m3_env.h>
11
10
#include < m3_api_defs.h>
12
11
13
12
/*
35
34
m3ApiRawFunction (Math_random)
36
35
{
37
36
m3ApiReturnType (float )
38
-
37
+
39
38
float r = (float )random (INT_MAX)/INT_MAX;
40
39
// Serial.print("Random: "); Serial.println(r);
41
40
@@ -49,22 +48,13 @@ m3ApiRawFunction(Dino_memcpy)
49
48
m3ApiGetArgMem (uint8_t *, src)
50
49
m3ApiGetArgMem (uint8_t *, dstend)
51
50
52
- unsigned len = dstend-dst;
53
- memcpy (dst, src, len ? len : 1 );
51
+ do {
52
+ *dst++ = *src++;
53
+ } while (dst < dstend);
54
54
55
55
m3ApiSuccess ();
56
56
}
57
57
58
- M3Result LinkImports (IM3Runtime runtime)
59
- {
60
- IM3Module module = runtime->modules ;
61
-
62
- m3_LinkRawFunction (module , " Math" , " random" , " f()" , &Math_random);
63
- m3_LinkRawFunction (module , " Dino" , " memcpy" , " v(iii)" , &Dino_memcpy);
64
-
65
- return m3Err_none;
66
- }
67
-
68
58
Adafruit_Arcada arcada;
69
59
70
60
IM3Environment env;
@@ -93,25 +83,22 @@ void load_wasm()
93
83
result = m3_LoadModule (runtime, module );
94
84
if (result) FATAL (" LoadModule" , result);
95
85
96
- result = LinkImports (runtime );
97
- if (result) FATAL ( " LinkImports " , result );
86
+ m3_LinkRawFunction ( module , " Math " , " random " , " f() " , &Math_random );
87
+ m3_LinkRawFunction ( module , " Dino " , " memcpy " , " v(iii) " , &Dino_memcpy );
98
88
99
89
mem = m3_GetMemory (runtime, NULL , 0 );
100
90
if (!mem) FATAL (" GetMemory" , " failed" );
101
-
91
+
102
92
result = m3_FindFunction (&func_run, runtime, " run" );
103
93
if (result) FATAL (" FindFunction" , result);
104
94
}
105
95
106
- void init_random ()
96
+ void init_device ()
107
97
{
108
98
// Try to randomize seed
109
99
randomSeed ((analogRead (A5) << 16 ) + analogRead (A4));
110
100
Serial.print (" Random: 0x" ); Serial.println (random (INT_MAX), HEX);
111
- }
112
101
113
- void init_arcada ()
114
- {
115
102
if (!arcada.arcadaBegin ()) {
116
103
FATAL (" arcadaBegin" , " failed" );
117
104
}
@@ -127,7 +114,7 @@ void display_info()
127
114
arcada.display ->setCursor (0 , 5 );
128
115
arcada.display ->println (" Wasm3 v" M3_VERSION " (" M3_ARCH " @" + String (F_CPU/1000000 ) + " MHz)\n " );
129
116
arcada.display ->println (" Dino game" );
130
- arcada.display ->println (" by Ben Smith (binji)" );
117
+ arcada.display ->println (" by Ben Smith (binji)" );
131
118
}
132
119
133
120
void setup ()
@@ -136,14 +123,15 @@ void setup()
136
123
137
124
// Wait for serial port to connect
138
125
// Needed for native USB port only
139
- // while(!Serial) {}
126
+ while (!Serial) {}
127
+
128
+ Serial.println (" \n Wasm3 v" M3_VERSION " (" M3_ARCH " ), build " __DATE__ " " __TIME__);
140
129
141
130
uint32_t tend, tstart;
142
131
TSTART ();
143
- init_random ();
144
- init_arcada ();
132
+ init_device ();
145
133
display_info ();
146
- TFINISH (" Arcada init" );
134
+ TFINISH (" Device init" );
147
135
148
136
TSTART ();
149
137
load_wasm ();
@@ -152,24 +140,22 @@ void setup()
152
140
Serial.println (" Running WebAssembly..." );
153
141
154
142
M3Result result;
155
- const char * i_argv[ 1 ] = { NULL } ;
143
+ uint64_t last_fps_print = 0 ;
156
144
157
145
while (true ) {
158
- const uint32_t framestart = millis ();
146
+ const uint64_t framestart = micros ();
159
147
160
148
// Process inputs
161
149
uint32_t pressed_buttons = arcada.readButtons ();
162
150
if (arcada.justPressedButtons () & ARCADA_BUTTONMASK_START) {
163
151
// NVIC_SystemReset();
164
152
165
153
// Restart Dino game
166
- init_random ();
167
154
display_info ();
168
155
load_wasm ();
169
156
}
170
157
171
158
uint32_t * input = (uint32_t *)(mem + 0x0000 );
172
-
173
159
*input = 0 ;
174
160
if (pressed_buttons & ARCADA_BUTTONMASK_A) { // Up
175
161
*input |= 0x1 ;
@@ -179,20 +165,23 @@ void setup()
179
165
}
180
166
181
167
// Render frame
182
- result = m3_CallWithArgs (func_run, 0 , i_argv );
168
+ result = m3_CallV (func_run);
183
169
if (result) break ;
184
170
185
171
// Output to display
186
172
arcada.display ->drawRGBBitmap (0 , 40 , (uint16_t *)(mem+0x5000 ), 160 , 75 );
187
173
188
- const uint32_t frametime = millis () - framestart;
189
- // Serial.print("FPS: "); Serial.println(1000/frametime);
174
+ const uint64_t frametime = micros () - framestart;
190
175
191
176
// Limit to 50..70 fps, depending on CPU/overclock setting (120..200MHz)
192
- // const int target_frametime = 1000 /map(F_CPU/1000000, 120, 200, 50, 70);
193
- const uint32_t target_frametime = 1000 /50 ;
177
+ // const int target_frametime = 1000000 /map(F_CPU/1000000, 120, 200, 50, 70);
178
+ const uint32_t target_frametime = 1000000 /50 ;
194
179
if (target_frametime > frametime) {
195
- delay (target_frametime - frametime);
180
+ delay ((target_frametime - frametime)/1000 );
181
+ }
182
+ if (framestart - last_fps_print > 1000000 ) {
183
+ Serial.print (" FPS: " ); Serial.println ((uint32_t )(1000000 /frametime));
184
+ last_fps_print = framestart;
196
185
}
197
186
}
198
187
0 commit comments