From ce554c68ac4b0b779f37fa02f1b946e75f002080 Mon Sep 17 00:00:00 2001 From: JakeBreath Date: Mon, 10 Aug 2026 23:08:49 -0500 Subject: [PATCH] backup --- src/bus.rs | 18 ++++++++++++------ src/main.rs | 7 ++++--- src/ppu.rs | 1 + 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/bus.rs b/src/bus.rs index a766358..ccfb63d 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -12,14 +12,18 @@ pub struct NesBus { cart: Cartridge, ppu: Ppu, openbus: u8, // last byte on the CPU data bus - controller: Controller, + controller: Controller, // P1 ($4016) + controller2: Controller, // P2 ($4017) - no controller plugged in for now } impl NesBus { pub fn new(cart: Cartridge) -> Self { - Self { ram: [0; 0x800], - cart, ppu: Ppu::new(), - openbus: 0, controller: Controller::new(Box::new(MaskInput::new())), } + Self { + ram: [0; 0x800], + cart, ppu: Ppu::new(), openbus: 0, + controller: Controller::new(Box::new(MaskInput::new())), + controller2: Controller::new(Box::new(MaskInput::new())), // reads 0 forever + } } pub fn tick_ppu(&mut self, cycles: u64) { @@ -48,7 +52,8 @@ impl Bus for NesBus { let result = match address { 0x0000..=0x1FFF => self.ram[(address & 0x07FF) as usize], // Zero-Page my beloved 0x2000..=0x3FFF => self.ppu.read(&mut self.cart, address, self.openbus), - 0x4016 | 0x4017 => self.controller.read(), + 0x4016 => self.controller.read(), + 0x4017 => self.controller2.read(), 0x4000..=0x401F => self.openbus, // APU regs: unmapped reads return bus contents 0x4020..=0x5FFF => self.openbus, // expansion: open bus 0x6000..=0x7FFF => self.cart.read_prg_ram(address), // SRAM reads from the cartridge @@ -63,7 +68,8 @@ impl Bus for NesBus { 0x0000..=0x1FFF => self.ram[(address & 0x07FF) as usize] = value, 0x2000..=0x3FFF => self.ppu.write(&mut self.cart, address, value), 0x4014 => self.ppu.oam_dma(&self.ram, value), - 0x4016 | 0x4017 => self.controller.write_strobe(value), + 0x4016 => self.controller.write_strobe(value), + 0x4017 => self.controller2.write_strobe(value), 0x6000..=0x7FFF => self.cart.write_prg_ram(address, value), 0x8000..=0xFFFF => self.cart.write_prg(address, value), _ => {} // Everything else dropped for now diff --git a/src/main.rs b/src/main.rs index f4e70a2..45f05e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -122,10 +122,11 @@ impl GuiApp { impl eframe::App for GuiApp { // Non-UI work: emulation runs here, before painting. fn logic(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { - self.renderer.poll_input(self.bus.controller_mut()); // keyboard -> $4016/$4017 - run_one_frame(&mut self.bus, &mut self.cpu); // one emulated frame + self.renderer.poll_input(self.bus.controller_mut()); + run_one_frame(&mut self.bus, &mut self.cpu); self.renderer.present(self.bus.framebuffer(), 256, 240); - ctx.request_repaint(); // keep it running at 60fps + // cap at 60fps: one emulated frame per 16.67ms, not "as fast as possible" + ctx.request_repaint_after(std::time::Duration::from_millis(16)); } // Painting: draw the framebuffer texture. diff --git a/src/ppu.rs b/src/ppu.rs index f88e95d..0bd681e 100644 --- a/src/ppu.rs +++ b/src/ppu.rs @@ -1,6 +1,7 @@ use crate::cartridge::Cartridge; use crate::mapper::Mirroring; + // 4-byte OAM entry: Y, tile, attribute, X #[derive(Clone, Copy, Default)] struct Sprite {