This commit is contained in:
2026-08-10 23:08:49 -05:00
parent 2e8244f0cf
commit ce554c68ac
3 changed files with 17 additions and 9 deletions
+12 -6
View File
@@ -12,14 +12,18 @@ pub struct NesBus {
cart: Cartridge, cart: Cartridge,
ppu: Ppu, ppu: Ppu,
openbus: u8, // last byte on the CPU data bus 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 { impl NesBus {
pub fn new(cart: Cartridge) -> Self { pub fn new(cart: Cartridge) -> Self {
Self { ram: [0; 0x800], Self {
cart, ppu: Ppu::new(), ram: [0; 0x800],
openbus: 0, controller: Controller::new(Box::new(MaskInput::new())), } 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) { pub fn tick_ppu(&mut self, cycles: u64) {
@@ -48,7 +52,8 @@ impl Bus for NesBus {
let result = match address { let result = match address {
0x0000..=0x1FFF => self.ram[(address & 0x07FF) as usize], // Zero-Page my beloved 0x0000..=0x1FFF => self.ram[(address & 0x07FF) as usize], // Zero-Page my beloved
0x2000..=0x3FFF => self.ppu.read(&mut self.cart, address, self.openbus), 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 0x4000..=0x401F => self.openbus, // APU regs: unmapped reads return bus contents
0x4020..=0x5FFF => self.openbus, // expansion: open bus 0x4020..=0x5FFF => self.openbus, // expansion: open bus
0x6000..=0x7FFF => self.cart.read_prg_ram(address), // SRAM reads from the cartridge 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, 0x0000..=0x1FFF => self.ram[(address & 0x07FF) as usize] = value,
0x2000..=0x3FFF => self.ppu.write(&mut self.cart, address, value), 0x2000..=0x3FFF => self.ppu.write(&mut self.cart, address, value),
0x4014 => self.ppu.oam_dma(&self.ram, 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), 0x6000..=0x7FFF => self.cart.write_prg_ram(address, value),
0x8000..=0xFFFF => self.cart.write_prg(address, value), 0x8000..=0xFFFF => self.cart.write_prg(address, value),
_ => {} // Everything else dropped for now _ => {} // Everything else dropped for now
+4 -3
View File
@@ -122,10 +122,11 @@ impl GuiApp {
impl eframe::App for GuiApp { impl eframe::App for GuiApp {
// Non-UI work: emulation runs here, before painting. // Non-UI work: emulation runs here, before painting.
fn logic(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { fn logic(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
self.renderer.poll_input(self.bus.controller_mut()); // keyboard -> $4016/$4017 self.renderer.poll_input(self.bus.controller_mut());
run_one_frame(&mut self.bus, &mut self.cpu); // one emulated frame run_one_frame(&mut self.bus, &mut self.cpu);
self.renderer.present(self.bus.framebuffer(), 256, 240); 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. // Painting: draw the framebuffer texture.
+1
View File
@@ -1,6 +1,7 @@
use crate::cartridge::Cartridge; use crate::cartridge::Cartridge;
use crate::mapper::Mirroring; use crate::mapper::Mirroring;
// 4-byte OAM entry: Y, tile, attribute, X // 4-byte OAM entry: Y, tile, attribute, X
#[derive(Clone, Copy, Default)] #[derive(Clone, Copy, Default)]
struct Sprite { struct Sprite {