summaryrefslogtreecommitdiff
path: root/mknes.c
diff options
context:
space:
mode:
authorPeter Fors <peter.fors@mindkiller.com>2025-05-24 15:45:54 +0200
committerPeter Fors <peter.fors@mindkiller.com>2025-05-24 15:45:54 +0200
commita8e0c141b0184d629504b9f0ee8dbc4fefb90934 (patch)
treecff0f7e64ce58dc8f1e4ff4501743d9b94449c94 /mknes.c
parente28ad1546509de31b706f0fd300a906e5bc55199 (diff)
3011fps
Diffstat (limited to 'mknes.c')
-rw-r--r--mknes.c61
1 files changed, 42 insertions, 19 deletions
diff --git a/mknes.c b/mknes.c
index 7220450..9e87a33 100644
--- a/mknes.c
+++ b/mknes.c
@@ -8,6 +8,8 @@
#include <string.h>
#include <immintrin.h>
+#define printf(...)
+
#define BUFFER_WIDTH 256
#define BUFFER_HEIGHT 240
#define WINDOW_WIDTH 320 * 3
@@ -105,7 +107,7 @@ static uint32_t frames; // debug information
#include "callbacks.c"
-struct nes_state nstate;
+// struct nes_state nstate;
int main(int argc, char **argv) {
#ifdef _WIN32
@@ -118,13 +120,17 @@ int main(int argc, char **argv) {
init_opcode_lut();
init_opcode_ud_lut();
// protect_opcode_lut();
- ppu_reset(&nstate);
+
+ struct nes_state *nstate = aligned_alloc(4096, (sizeof(struct nes_state) + 4095) & ~4096);
+
+
+ ppu_reset(nstate);
// ines2_load(&nstate, "data/0000/10-Yard Fight (USA, Europe).nes");
// ines2_load(&nstate, "data/0000/Balloon Fight (USA).nes");
// ines2_load(&nstate, "data/0000/Excitebike (Japan, USA).nes");
// ines2_load(&nstate, "data/0000/Ice Climber (USA, Europe, Korea).nes");
// ines2_load(&nstate, "data/0000/Kung Fu (Japan, USA).nes");
- ines2_load(&nstate, "data/0000/Super Mario Bros. (World) (HVC-SM).nes");
+ ines2_load(nstate, "data/0000/Super Mario Bros. (World) (HVC-SM).nes");
// ines2_load(&nstate, "data/Super Mario Bros. (W) (V1.0) [!].nes");
// ines2_load(&nstate, "data/Super Mario Bros. (JU) [!].nes");
// ines2_load(&nstate, "data/0000/Urban Champion (World).nes");
@@ -146,13 +152,13 @@ int main(int argc, char **argv) {
// ines2_load(&nstate, "data/0000/Xevious - The Avenger (USA).zip");
// ines2_load(&nstate, "data/tv.nes");
- ines2_load(&nstate, "data/Life Force (USA).zip"); // 2002
+ // ines2_load(&nstate, "data/Life Force (USA).zip"); // 2002
// ines2_load(&nstate, "data/0003/Flipull - An Exciting Cube Game (Japan) (En).zip");
// ines2_load(&nstate, "data/0003/Friday the 13th (USA).zip");
// ines2_load(&nstate, "data/0003/Ghostbusters (Japan).zip");
// ines2_load(&nstate, "data/0003/Gradius (USA).zip");
- // ines2_load(&nstate, "data/0007/Battletoads (USA).zip");
+ // ines2_load(nstate, "data/0007/Battletoads (USA).zip");
// ines2_load(&nstate, "data/0007/Beetlejuice (USA).zip");
// ines2_load(&nstate, "data/0007/Cabal (USA).zip");
@@ -167,22 +173,22 @@ int main(int argc, char **argv) {
// ines2_load(&nstate, "data/Blaster Master (USA).zip");
- mapper_setup(&nstate);
- uint32_t lo = nstate.mapper.prg_read(&nstate, 0xfffc);
- uint32_t hi = nstate.mapper.prg_read(&nstate, 0xfffd);
- nstate.cpu.pc = (hi << 8) | lo;
+ mapper_setup(nstate);
+ uint32_t lo = nstate->mapper.prg_read(nstate, 0xfffc);
+ uint32_t hi = nstate->mapper.prg_read(nstate, 0xfffd);
+ nstate->cpu.pc = (hi << 8) | lo;
#if 1
for(uint32_t i = 0; i < 0x5000; ++ i) {
- while(!nstate.ppu.frame_ready) {
+ while(!nstate->ppu.frame_ready) {
// PROFILE_NAMED("nes emulator");
- cpu_tick(&nstate);
+ cpu_tick(nstate);
}
- nstate.ppu.frame_ready = 0;
+ nstate->ppu.frame_ready = 0;
frames++;
}
return 0;
-#endif
+#else
struct timer_handle *timer = timer_new(FRAME_INTERVAL_NS);
if(!timer) {
@@ -198,10 +204,12 @@ int main(int argc, char **argv) {
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "NES Emulator", 0, 0);
if(window) {
- glfwSetWindowUserPointer(window, (void*)&nstate);
+ glfwSetWindowUserPointer(window, (void*)nstate);
glfwSetWindowAspectRatio(window, 320, 240); // Need to set a 4:3 resolution for things to look correct!
glfwSetWindowSizeLimits(window, WINDOW_WIDTH, WINDOW_HEIGHT, GLFW_DONT_CARE, GLFW_DONT_CARE);
+ glfwSetInputMode(window, GLFW_STICKY_KEYS, GLFW_TRUE);
+
glfwMakeContextCurrent(window);
opengl_setup();
glfwSetKeyCallback(window, key_callback);
@@ -226,15 +234,28 @@ int main(int argc, char **argv) {
timer_wait(timer);
glfwPollEvents();
- while(!nstate.ppu.frame_ready) {
+ uint8_t input = 0;
+
+ if(glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS) { input |= (1 << 0); }
+ if(glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS) { input |= (1 << 1); }
+ if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { input |= (1 << 2); }
+ if(glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_PRESS) { input |= (1 << 3); }
+ if(glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) { input |= (1 << 4); }
+ if(glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) { input |= (1 << 5); }
+ if(glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) { input |= (1 << 6); }
+ if(glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) { input |= (1 << 7); }
+
+ nstate->ppu.input[0] = input;
+
+ while(!nstate->ppu.frame_ready) {
// PROFILE_NAMED("nes emulator");
- cpu_tick(&nstate);
+ cpu_tick(nstate);
}
- nstate.ppu.frame_ready = 0;
+ nstate->ppu.frame_ready = 0;
frames++;
uint32_t * restrict dst = display_buffer; //buffer;
- uint8_t * restrict src = nstate.pixels;
+ uint8_t * restrict src = nstate->pixels;
for(uint32_t y = 0; y < 240; ++y) {
for(uint32_t x = 0; x < 256; ++x) {
uint8_t val = *src++;
@@ -248,7 +269,7 @@ int main(int argc, char **argv) {
glfwSwapBuffers(window);
}
- printf("total frames: %6.6d total cycles: %ld\n", frames, nstate.cpu.cycles);
+ printf("total frames: %6.6d total cycles: %ld\n", frames, nstate->cpu.cycles);
glfwDestroyWindow(window);
} else {
@@ -261,6 +282,8 @@ int main(int argc, char **argv) {
}
timer_destroy(timer);
// free_nes_state(&nstate);
+#endif
+
#ifdef _WIN32
timeEndPeriod(1);
#endif