88#include "../runtime.h"
99#include "../wasm.h"
1010#include "../window.h"
11+ #include "main.h"
1112
1213#if defined(_WIN32 )
1314#include <windows.h>
1415#endif
1516
1617#define DISK_FILE_EXT ".disk"
1718
19+ static uint8_t * memory ;
20+ static w4_Disk disk = {0 };
21+ static uint8_t * wasmData ;
22+ static size_t wasmLength ;
23+
1824typedef struct {
1925 // Should be the 4 byte ASCII string "CART" (1414676803)
2026 uint32_t magic ;
@@ -23,7 +29,7 @@ typedef struct {
2329 char title [128 ];
2430
2531 // Length of the cart.wasm bytes used to offset backwards from the footer
26- uint32_t cartLength ;
32+ uint32_t wasmLength ;
2733} FileFooter ;
2834
2935static long audioDataCallback (cubeb_stream * stream , void * userData ,
@@ -117,10 +123,12 @@ static void trimFileExtension (char *path) {
117123 }
118124}
119125
126+ void resetCart () {
127+ w4_runtimeInit (memory , & disk );
128+ w4_wasmLoadModule (wasmData , wasmLength );
129+ }
130+
120131int main (int argc , const char * argv []) {
121- uint8_t * cartBytes ;
122- size_t cartLength ;
123- w4_Disk disk = {0 };
124132 const char * title = "WASM-4" ;
125133 char * diskPath = NULL ;
126134
@@ -139,9 +147,9 @@ int main (int argc, const char* argv[]) {
139147 footer .title [sizeof (footer .title )- 1 ] = '\0' ;
140148 title = footer .title ;
141149
142- cartBytes = malloc (footer .cartLength );
143- fseek (file , - sizeof (FileFooter ) - footer .cartLength , SEEK_END );
144- cartLength = fread (cartBytes , 1 , footer .cartLength , file );
150+ wasmData = malloc (footer .wasmLength );
151+ fseek (file , - sizeof (FileFooter ) - footer .wasmLength , SEEK_END );
152+ wasmLength = fread (wasmData , 1 , footer .wasmLength , file );
145153 fclose (file );
146154
147155 // Look for disk file
@@ -161,11 +169,11 @@ int main (int argc, const char* argv[]) {
161169 }
162170
163171 fseek (file , 0 , SEEK_END );
164- cartLength = ftell (file );
172+ wasmLength = ftell (file );
165173 fseek (file , 0 , SEEK_SET );
166174
167- cartBytes = malloc (cartLength );
168- cartLength = fread (cartBytes , 1 , cartLength , file );
175+ wasmData = malloc (wasmLength );
176+ wasmLength = fread (wasmData , 1 , wasmLength , file );
169177 fclose (file );
170178
171179 // Look for disk file
@@ -178,10 +186,10 @@ int main (int argc, const char* argv[]) {
178186
179187 audioInit ();
180188
181- uint8_t * memory = w4_wasmInit ();
189+ memory = w4_wasmInit ();
182190 w4_runtimeInit (memory , & disk );
183191
184- w4_wasmLoadModule (cartBytes , cartLength );
192+ w4_wasmLoadModule (wasmData , wasmLength );
185193
186194 w4_windowBoot (title );
187195
0 commit comments