77#include " Adafruit_Arcada.h"
88
99#include < wasm3.h>
10- #include < m3_env.h>
1110#include < m3_api_defs.h>
1211
1312/*
3534m3ApiRawFunction (Math_random)
3635{
3736 m3ApiReturnType (float )
38-
37+
3938 float r = (float )random (INT_MAX)/INT_MAX;
4039 // Serial.print("Random: "); Serial.println(r);
4140
@@ -49,22 +48,13 @@ m3ApiRawFunction(Dino_memcpy)
4948 m3ApiGetArgMem (uint8_t *, src)
5049 m3ApiGetArgMem (uint8_t *, dstend)
5150
52- unsigned len = dstend-dst;
53- memcpy (dst, src, len ? len : 1 );
51+ do {
52+ *dst++ = *src++;
53+ } while (dst < dstend);
5454
5555 m3ApiSuccess ();
5656}
5757
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-
6858Adafruit_Arcada arcada;
6959
7060IM3Environment env;
@@ -93,25 +83,22 @@ void load_wasm()
9383 result = m3_LoadModule (runtime, module );
9484 if (result) FATAL (" LoadModule" , result);
9585
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 );
9888
9989 mem = m3_GetMemory (runtime, NULL , 0 );
10090 if (!mem) FATAL (" GetMemory" , " failed" );
101-
91+
10292 result = m3_FindFunction (&func_run, runtime, " run" );
10393 if (result) FATAL (" FindFunction" , result);
10494}
10595
106- void init_random ()
96+ void init_device ()
10797{
10898 // Try to randomize seed
10999 randomSeed ((analogRead (A5) << 16 ) + analogRead (A4));
110100 Serial.print (" Random: 0x" ); Serial.println (random (INT_MAX), HEX);
111- }
112101
113- void init_arcada ()
114- {
115102 if (!arcada.arcadaBegin ()) {
116103 FATAL (" arcadaBegin" , " failed" );
117104 }
@@ -127,7 +114,7 @@ void display_info()
127114 arcada.display ->setCursor (0 , 5 );
128115 arcada.display ->println (" Wasm3 v" M3_VERSION " (" M3_ARCH " @" + String (F_CPU/1000000 ) + " MHz)\n " );
129116 arcada.display ->println (" Dino game" );
130- arcada.display ->println (" by Ben Smith (binji)" );
117+ arcada.display ->println (" by Ben Smith (binji)" );
131118}
132119
133120void setup ()
@@ -136,14 +123,15 @@ void setup()
136123
137124 // Wait for serial port to connect
138125 // 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__);
140129
141130 uint32_t tend, tstart;
142131 TSTART ();
143- init_random ();
144- init_arcada ();
132+ init_device ();
145133 display_info ();
146- TFINISH (" Arcada init" );
134+ TFINISH (" Device init" );
147135
148136 TSTART ();
149137 load_wasm ();
@@ -152,24 +140,22 @@ void setup()
152140 Serial.println (" Running WebAssembly..." );
153141
154142 M3Result result;
155- const char * i_argv[ 1 ] = { NULL } ;
143+ uint64_t last_fps_print = 0 ;
156144
157145 while (true ) {
158- const uint32_t framestart = millis ();
146+ const uint64_t framestart = micros ();
159147
160148 // Process inputs
161149 uint32_t pressed_buttons = arcada.readButtons ();
162150 if (arcada.justPressedButtons () & ARCADA_BUTTONMASK_START) {
163151 // NVIC_SystemReset();
164152
165153 // Restart Dino game
166- init_random ();
167154 display_info ();
168155 load_wasm ();
169156 }
170157
171158 uint32_t * input = (uint32_t *)(mem + 0x0000 );
172-
173159 *input = 0 ;
174160 if (pressed_buttons & ARCADA_BUTTONMASK_A) { // Up
175161 *input |= 0x1 ;
@@ -179,20 +165,23 @@ void setup()
179165 }
180166
181167 // Render frame
182- result = m3_CallWithArgs (func_run, 0 , i_argv );
168+ result = m3_CallV (func_run);
183169 if (result) break ;
184170
185171 // Output to display
186172 arcada.display ->drawRGBBitmap (0 , 40 , (uint16_t *)(mem+0x5000 ), 160 , 75 );
187173
188- const uint32_t frametime = millis () - framestart;
189- // Serial.print("FPS: "); Serial.println(1000/frametime);
174+ const uint64_t frametime = micros () - framestart;
190175
191176 // 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 ;
194179 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;
196185 }
197186 }
198187
0 commit comments