summaryrefslogtreecommitdiff
path: root/mknes.c
diff options
context:
space:
mode:
Diffstat (limited to 'mknes.c')
-rw-r--r--mknes.c87
1 files changed, 65 insertions, 22 deletions
diff --git a/mknes.c b/mknes.c
index e3e9ffd..f68493c 100644
--- a/mknes.c
+++ b/mknes.c
@@ -18,10 +18,25 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
+#include <stddef.h>
#include <math.h>
#include <string.h>
+
+
+
+static size_t state_dump_count;
+static FILE *state_dump_file;
+
+
+
+
+
+
+
+
+
#define BUFFER_WIDTH 256
#define BUFFER_HEIGHT 240
#define WINDOW_WIDTH 320 * 3
@@ -161,11 +176,11 @@ struct main_state state __attribute__((aligned(64)));
#include "mkfw.h"
#include "platform_opengl.c"
-static size_t sprite_counts[8];
static uint32_t frames; // debug information
-// static int32_t tas_frame_count;
+static int32_t tas_frame;
-// #include "smb_tas.h" // REMOVE ME
+// #include "helpers/battletoads_tas.h" // REMOVE ME
+#include "helpers/smb_tas.h" // REMOVE ME
// NES core
#include "mknes_mapper.h"
@@ -184,6 +199,21 @@ static uint32_t frames; // debug information
// struct nes_state nstate;
+static void dump_state(struct nes_state *state) {
+ size_t state_size = offsetof(struct nes_state, ram);
+
+ if(!state_dump_file) {
+ state_dump_file = fopen("state_dump.bin", "wb");
+ if(!state_dump_file) {
+ fprintf(stderr, "Failed to open state_dump.bin for writing\n");
+ return;
+ }
+ }
+
+ fwrite(state, 1, state_size, state_dump_file);
+ state_dump_count++;
+}
+
static void framebuffer_callback(struct mkfw_state *mkfw_window, int32_t width, int32_t height, float aspect_ratio) {
// state.screen_width = width;
// state.screen_height = height;
@@ -349,33 +379,38 @@ int main(int argc, char **argv) {
// Joypad input
uint8_t input = 0;
- if(window->keyboard_state[MKS_KEY_X]) { input |= (1 << 0); }
- if(window->keyboard_state[MKS_KEY_Z]) { input |= (1 << 1); }
- if(window->keyboard_state[MKS_KEY_SPACE]) { input |= (1 << 2); }
- if(window->keyboard_state[MKS_KEY_RETURN]) { input |= (1 << 3); }
- if(window->keyboard_state[MKS_KEY_UP]) { input |= (1 << 4); }
- if(window->keyboard_state[MKS_KEY_DOWN]) { input |= (1 << 5); }
- if(window->keyboard_state[MKS_KEY_LEFT]) { input |= (1 << 6); }
- if(window->keyboard_state[MKS_KEY_RIGHT]) { input |= (1 << 7); }
- nstate->ppu.input[0] = input;
+ // if(window->keyboard_state[MKS_KEY_X]) { input |= (1 << 0); }
+ // if(window->keyboard_state[MKS_KEY_Z]) { input |= (1 << 1); }
+ // if(window->keyboard_state[MKS_KEY_SPACE]) { input |= (1 << 2); }
+ // if(window->keyboard_state[MKS_KEY_RETURN]) { input |= (1 << 3); }
+ // if(window->keyboard_state[MKS_KEY_UP]) { input |= (1 << 4); }
+ // if(window->keyboard_state[MKS_KEY_DOWN]) { input |= (1 << 5); }
+ // if(window->keyboard_state[MKS_KEY_LEFT]) { input |= (1 << 6); }
+ // if(window->keyboard_state[MKS_KEY_RIGHT]) { input |= (1 << 7); }
+ // nstate->ppu.input[0] = input;
// Run NES emulation for one frame
while(!nstate->ppu.frame_ready) {
cpu_tick(nstate);
}
nstate->ppu.frame_ready = 0;
- if(nstate->ppu.open_bus > 0) {
- int32_t v = nstate->ppu.open_bus;
- v -= 5;
- if(v < 0) {
- v = 0;
- }
- nstate->ppu.open_bus = v;
- }
-
+ // if(nstate->ppu.open_bus > 0) {
+ // int32_t v = nstate->ppu.open_bus;
+ // v -= 5;
+ // if(v < 0) {
+ // v = 0;
+ // }
+ // nstate->ppu.open_bus = v;
+ // }
+ nstate->ppu.input[0] = tas_input[tas_frame++];
frames++;
+ // Dump state every frame starting from 2400
+ if(frames >= 2400 && frames <= 3100) {
+ dump_state(nstate);
+ }
+
// Convert NES pixels to display buffer
uint32_t * restrict dst = display_buffer;
uint8_t * restrict src = nstate->pixels;
@@ -395,12 +430,20 @@ int main(int argc, char **argv) {
// Render and swap buffers
render_frame();
mkfw_swap_buffers(window);
- timer_wait(timer);
+ if(!window->keyboard_state[MKS_KEY_F]) {
+ timer_wait(timer);
+ }
}
printf("total frames: %6d total cycles: %12llu\n", frames, (unsigned long long)nstate->cpu.cycles);
+ printf("state dumps created: %zu\n", state_dump_count);
+
+ if(state_dump_file) {
+ fclose(state_dump_file);
+ }
timer_destroy(timer);
+ free(nstate);
// free_nes_state(&nstate);
timer_shutdown();
mkfw_cleanup(window);