Skip to content

Commit 568ebe4

Browse files
committed
Make R reset the cart in native runtime
1 parent eb3cf22 commit 568ebe4

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

runtimes/native/src/backend/main.c

+20-12
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@
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+
1824
typedef 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

2935
static 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+
120131
int 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

runtimes/native/src/backend/main.h

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef MAIN_H_
2+
#define MAIN_H_
3+
4+
void resetCart ();
5+
6+
#endif // MAIN_H_

runtimes/native/src/backend/window_glfw.c

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "../window.h"
99
#include "../runtime.h"
10+
#include "main.h"
1011

1112
static uint32_t table[256];
1213
static GLuint paletteLocation;
@@ -143,6 +144,9 @@ static void onGlfwError(int error, const char* description)
143144
}
144145

145146
static void update (GLFWwindow* window) {
147+
if (glfwGetKey(window, GLFW_KEY_R)) {
148+
resetCart();
149+
}
146150
// Keyboard handling
147151
uint8_t gamepad = 0;
148152
if (glfwGetKey(window, GLFW_KEY_X)) {

runtimes/native/src/backend/window_minifb.c

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "../window.h"
55
#include "../runtime.h"
6+
#include "main.h"
67

78
static uint32_t pixels[160*160];
89

@@ -26,6 +27,9 @@ void w4_windowBoot (const char* title) {
2627
do {
2728
// Keyboard handling
2829
const uint8_t* keyBuffer = mfb_get_key_buffer(window);
30+
if (keyBuffer[KB_KEY_R]) {
31+
resetCart();
32+
}
2933

3034
// Player 1
3135
uint8_t gamepad = 0;

0 commit comments

Comments
 (0)