summaryrefslogtreecommitdiff
path: root/mknes.c
diff options
context:
space:
mode:
Diffstat (limited to 'mknes.c')
-rw-r--r--mknes.c63
1 files changed, 61 insertions, 2 deletions
diff --git a/mknes.c b/mknes.c
index 0027ce6..bd7911c 100644
--- a/mknes.c
+++ b/mknes.c
@@ -8,19 +8,78 @@
#include "memory.c"
#include "cpu.c"
#include "ines2.c"
+#include "mapper.c"
+struct nes_state nstate;
-static void render_callback() {
+static uint32_t frames;
+static void render_callback(void) {
+ clear_buffer();
+
+ while(!nstate.ppu.frame_ready) {
+ cpu_tick(&nstate);
+ }
+ nstate.ppu.frame_ready = 0;
+ frames++;
+
+ uint32_t *dst = RENDER_START(0,0);
+ uint8_t *src = nstate.ppu.pixels;
+ for(uint32_t y = 0; y < 240; ++y) {
+ for(uint32_t x = 0; x < 256; ++x) {
+ uint8_t val = *src++;
+ if(val >= 64) val = 0;
+ dst[x] = nes_palette[val];
+ }
+ dst += BUFFER_WIDTH;
+ }
+}
+
+static void shutdown_callback() {
+ printf("%d\n", frames);
}
static void audio_callback(int16_t *buffer, size_t frames) {
}
-static void init_callback() {
+#include <sys/mman.h>
+#include <unistd.h>
+
+void protect_opcode_lut(void) {
+ uintptr_t addr = (uintptr_t)opcode_lut;
+ size_t page_size = getpagesize();
+ uintptr_t page = addr & ~(page_size - 1);
+
+ if(mprotect((void*)page, page_size, PROT_READ) != 0) {
+ perror("mprotect");
+ abort();
+ }
+}
+
+static void init_callback(void) {
+ setbuf(stdout, 0);
+ init_opcode_lut();
+ init_opcode_ud_lut();
+ // protect_opcode_lut();
+ ppu_reset(&nstate);
+ // ines2_load(&nstate, "data/nrom/Super Mario Bros. (World) (HVC-SM).nes");
+ // ines2_load(&nstate, "data/nrom/10-Yard Fight (USA, Europe).nes");
+ // ines2_load(&nstate, "data/nrom/Balloon Fight (USA).nes");
+ ines2_load(&nstate, "data/nrom/Excitebike (Japan, USA).nes");
+ // ines2_load(&nstate, "data/nrom/Ice Climber (USA, Europe, Korea).nes");
+ // ines2_load(&nstate, "data/nrom/Kung Fu (Japan, USA).nes");
+ // ines2_load(&nstate, "data/nrom/Super Mario Bros. (World) (HVC-SM).nes");
+ // ines2_load(&nstate, "data/nrom/Urban Champion (World).nes");
+ // ines2_load(&nstate, "data/nrom/Wrecking Crew (World).nes");
+
+
+ mapper_setup(&nstate);
+ uint32_t lo = nstate.mapper.read(&nstate, 0xfffc);
+ uint32_t hi = nstate.mapper.read(&nstate, 0xfffd);
+ nstate.cpu.pc = (hi << 8) | lo;
}
// int main(void) {