From 59de3766b673a53f25b024fc1f123c366b684bcc Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Tue, 23 Aug 2022 11:04:40 -0600 Subject: [PATCH] Make R reset the cart in native runtime --- runtimes/native/src/backend/main.c | 32 +++++++++++++-------- runtimes/native/src/backend/main.h | 6 ++++ runtimes/native/src/backend/window_glfw.c | 4 +++ runtimes/native/src/backend/window_minifb.c | 4 +++ 4 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 runtimes/native/src/backend/main.h diff --git a/runtimes/native/src/backend/main.c b/runtimes/native/src/backend/main.c index 5467d38b..b0369fff 100644 --- a/runtimes/native/src/backend/main.c +++ b/runtimes/native/src/backend/main.c @@ -8,6 +8,7 @@ #include "../runtime.h" #include "../wasm.h" #include "../window.h" +#include "main.h" #if defined(_WIN32) #include @@ -15,6 +16,11 @@ #define DISK_FILE_EXT ".disk" +static uint8_t* memory; +static w4_Disk disk = {0}; +static uint8_t* wasmData; +static size_t wasmLength; + typedef struct { // Should be the 4 byte ASCII string "CART" (1414676803) uint32_t magic; @@ -23,7 +29,7 @@ typedef struct { char title[128]; // Length of the cart.wasm bytes used to offset backwards from the footer - uint32_t cartLength; + uint32_t wasmLength; } FileFooter; static long audioDataCallback (cubeb_stream* stream, void* userData, @@ -117,10 +123,12 @@ static void trimFileExtension (char *path) { } } +void resetCart () { + w4_runtimeInit(memory, &disk); + w4_wasmLoadModule(wasmData, wasmLength); +} + int main (int argc, const char* argv[]) { - uint8_t* cartBytes; - size_t cartLength; - w4_Disk disk = {0}; const char* title = "WASM-4"; char* diskPath = NULL; @@ -139,9 +147,9 @@ int main (int argc, const char* argv[]) { footer.title[sizeof(footer.title)-1] = '\0'; title = footer.title; - cartBytes = malloc(footer.cartLength); - fseek(file, -sizeof(FileFooter) - footer.cartLength, SEEK_END); - cartLength = fread(cartBytes, 1, footer.cartLength, file); + wasmData = malloc(footer.wasmLength); + fseek(file, -sizeof(FileFooter) - footer.wasmLength, SEEK_END); + wasmLength = fread(wasmData, 1, footer.wasmLength, file); fclose(file); // Look for disk file @@ -161,11 +169,11 @@ int main (int argc, const char* argv[]) { } fseek(file, 0, SEEK_END); - cartLength = ftell(file); + wasmLength = ftell(file); fseek(file, 0, SEEK_SET); - cartBytes = malloc(cartLength); - cartLength = fread(cartBytes, 1, cartLength, file); + wasmData = malloc(wasmLength); + wasmLength = fread(wasmData, 1, wasmLength, file); fclose(file); // Look for disk file @@ -178,10 +186,10 @@ int main (int argc, const char* argv[]) { audioInit(); - uint8_t* memory = w4_wasmInit(); + memory = w4_wasmInit(); w4_runtimeInit(memory, &disk); - w4_wasmLoadModule(cartBytes, cartLength); + w4_wasmLoadModule(wasmData, wasmLength); w4_windowBoot(title); diff --git a/runtimes/native/src/backend/main.h b/runtimes/native/src/backend/main.h new file mode 100644 index 00000000..7ebb14e8 --- /dev/null +++ b/runtimes/native/src/backend/main.h @@ -0,0 +1,6 @@ +#ifndef MAIN_H_ +#define MAIN_H_ + +void resetCart (); + +#endif // MAIN_H_ diff --git a/runtimes/native/src/backend/window_glfw.c b/runtimes/native/src/backend/window_glfw.c index 5b1929af..a27b3482 100644 --- a/runtimes/native/src/backend/window_glfw.c +++ b/runtimes/native/src/backend/window_glfw.c @@ -7,6 +7,7 @@ #include "../window.h" #include "../runtime.h" +#include "main.h" static uint32_t table[256]; static GLuint paletteLocation; @@ -143,6 +144,9 @@ static void onGlfwError(int error, const char* description) } static void update (GLFWwindow* window) { + if (glfwGetKey(window, GLFW_KEY_R)) { + resetCart(); + } // Keyboard handling uint8_t gamepad = 0; if (glfwGetKey(window, GLFW_KEY_X)) { diff --git a/runtimes/native/src/backend/window_minifb.c b/runtimes/native/src/backend/window_minifb.c index bdd3c831..e684c5da 100644 --- a/runtimes/native/src/backend/window_minifb.c +++ b/runtimes/native/src/backend/window_minifb.c @@ -3,6 +3,7 @@ #include "../window.h" #include "../runtime.h" +#include "main.h" static uint32_t pixels[160*160]; @@ -26,6 +27,9 @@ void w4_windowBoot (const char* title) { do { // Keyboard handling const uint8_t* keyBuffer = mfb_get_key_buffer(window); + if (keyBuffer[KB_KEY_R]) { + resetCart(); + } // Player 1 uint8_t gamepad = 0;