Skip to content

Commit

Permalink
Make R reset the cart in native runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
desttinghim committed Aug 23, 2022
1 parent 608f77c commit 59de376
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
32 changes: 20 additions & 12 deletions runtimes/native/src/backend/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
#include "../runtime.h"
#include "../wasm.h"
#include "../window.h"
#include "main.h"

#if defined(_WIN32)
#include <windows.h>
#endif

#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;
Expand All @@ -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,
Expand Down Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);

Expand Down
6 changes: 6 additions & 0 deletions runtimes/native/src/backend/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef MAIN_H_
#define MAIN_H_

void resetCart ();

#endif // MAIN_H_
4 changes: 4 additions & 0 deletions runtimes/native/src/backend/window_glfw.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "../window.h"
#include "../runtime.h"
#include "main.h"

static uint32_t table[256];
static GLuint paletteLocation;
Expand Down Expand Up @@ -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)) {
Expand Down
4 changes: 4 additions & 0 deletions runtimes/native/src/backend/window_minifb.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "../window.h"
#include "../runtime.h"
#include "main.h"

static uint32_t pixels[160*160];

Expand All @@ -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;
Expand Down

0 comments on commit 59de376

Please # to comment.