From 7caf88171f89234eca2a020961041800caba84cd Mon Sep 17 00:00:00 2001 From: FyloZ Date: Sat, 4 May 2024 22:16:12 -0400 Subject: [PATCH] CPU NMI implementation --- cpu/cpu.c | 19 +++++++++++++++++-- include/system.h | 1 + main.c | 9 ++------- ppu/ppu.c | 12 ++++++++++++ system.c | 2 +- 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/cpu/cpu.c b/cpu/cpu.c index e044309..d9afa4b 100644 --- a/cpu/cpu.c +++ b/cpu/cpu.c @@ -34,6 +34,7 @@ void cpu_init(CPU *cpu) { cpu->y = 0x00; cpu->status = 0x04; cpu->oam_dma_triggered = false; + cpu->nmi_requested = false; } void print_registers(CPU cpu, byte op, unsigned long cycle_count) { @@ -49,6 +50,16 @@ void print_registers(CPU cpu, byte op, unsigned long cycle_count) { cycle_count); } +void cpu_process_nmi(System *system) { + cpu_stack_push_context(system); + + address handler_addr = mem_get_word(system, 0xfffa); + log_debug("NMI %#04x", handler_addr); + + system->cpu.nmi_requested = false; + system->cpu.program_counter = handler_addr; +} + void oam_dma_upload(System *system) { byte page_high_addr = *system->ppu.oam_dma_register; address page_addr = ((address) page_high_addr) << 8; @@ -64,16 +75,20 @@ void oam_dma_upload(System *system) { } void cpu_cycle(System *system) { + if (system->cpu.nmi_requested) { + cpu_process_nmi(system); + } + if (system->cpu.oam_dma_triggered) { oam_dma_upload(system); system->cpu.oam_dma_triggered = false; return; } - CPU registers = system->cpu; + CPU cpu = system->cpu; byte op = cpu_get_next_byte(system); - print_registers(registers, op, system->cycle_count); + print_registers(cpu, op, system->cycle_count); process_op_code(system, op); } diff --git a/include/system.h b/include/system.h index 4dc6e05..2a69e0c 100644 --- a/include/system.h +++ b/include/system.h @@ -28,6 +28,7 @@ typedef struct cpu { byte y; byte status; bool oam_dma_triggered; + bool nmi_requested; } CPU; typedef struct system { diff --git a/main.c b/main.c index 03a5e96..b7aa128 100644 --- a/main.c +++ b/main.c @@ -36,14 +36,9 @@ int main() { } system_start(&system); - start_debugger(&system); -// system_loop(&system); +// start_debugger(&system); + system_loop(&system); system_uninit(&system); return EXIT_SUCCESS; - -// system_uninit(&system); - - -// return win(); } \ No newline at end of file diff --git a/ppu/ppu.c b/ppu/ppu.c index 040be59..cda8b1e 100644 --- a/ppu/ppu.c +++ b/ppu/ppu.c @@ -1,5 +1,17 @@ // // Created by william on 12/30/23. +// https://www.reddit.com/r/EmuDev/comments/evu3u2/comment/fgr03ms/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button +// +// 1. Make sure you have NMI implemented on CPU (pretty straightforward) +// 2. Implement PPUSTATUS vblank flag (simple) and PPUCTRL NMI flag + background address flag (simple) +// 3. Implement PPUADDR/PPUDATA so that the nametables are filled out +// 4. Now you have some data your PPU can actually read for rendering background. Render it scanline by scanline - just follow the wiki on this. Maybe the timing will be bad, it doesn't matter for this game. Start off with rendering tiles based on the pattern table ID, don't try and fetch patterns. +// 5. Fix the inevitable bugs with your PPUDATA implementation until you see a blocky version of the Donkey Kong screen. +// 6. Now fetch pattern table data using the nametable data. If it looks "wrong" make sure you are consuming the background address flag. Start off with black and white, then pick two colors to mix for the two bits. Now you should have something like https://i.imgur.com/7OIpHgd.png +// 7. (Optional) implement palette reads (I'm skipping this for now). +// 8. Implement OAMDMA (and OAMDATA I guess, I implemented one on top of the other) +// 9. Now you should have sprite data to render. Implement the logic for copying from primary OAM to scanline OAM. I'm doing it all as one step (not smearing it over up to 256 cycles like the actual hardware). Skip the confusing sprite overflow junk. +// 10. This is where I'm stuck. I think I need to read the "sprites" section of https://wiki.nesdev.com/w/index.php/PPU_rendering very carefully. // #include diff --git a/system.c b/system.c index 324ccbb..7a493fe 100644 --- a/system.c +++ b/system.c @@ -35,7 +35,7 @@ void system_loop(System *system) { long frame = 1; long cpu_cycle_count = 0; while (true) { - log_info("Frame %d", frame); +// log_info("Frame %d", frame); while (system->cycle_count < cpu_cycle_per_frame * frame) { if (cpu_cycle_count == system->cycle_count) {