Skip to content

Commit

Permalink
Fix nes emulator to allow building without the debugger.
Browse files Browse the repository at this point in the history
  • Loading branch information
uglyoldbob committed Jan 31, 2024
1 parent 0aeb090 commit 285bc28
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 37 deletions.
2 changes: 1 addition & 1 deletion common/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ impl UserInput {
UserInput::NoInput => "None".to_string(),
}
}
}
}
2 changes: 1 addition & 1 deletion common/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,4 @@ impl PersistentStorage {
PersistentStorage::Volatile(v) => &mut v[..],
}
}
}
}
2 changes: 1 addition & 1 deletion nes/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["egui-multiwin", "debugger", "rom_status"]
default = ["egui-multiwin", "rom_status"]
debugger = []
rom_status = []
puffin = ["dep:puffin", "dep:puffin_egui"]
Expand Down
6 changes: 5 additions & 1 deletion nes/rust/src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ impl NesCpu {
}

/// Returns true when a breakpoint is active
#[cfg(feature = "debugger")]
pub fn breakpoint(&self) -> bool {
let mut b = false;
if self.done_fetching {
Expand Down Expand Up @@ -12549,7 +12550,10 @@ impl NesCpu {
},
_ => {
if s.subcycle == 1 {
s.copy_debugger(format!("*JAM ${:02x}", o));
#[cfg(feature = "debugger")]
{
s.copy_debugger(format!("*JAM ${:02x}", o));
}
s.subcycle = 2;
}
}
Expand Down
13 changes: 10 additions & 3 deletions nes/rust/src/emulator_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,16 @@ impl NesEmulatorData {
let ppu = NesPpu::new();
let apu = NesApu::new();

let breakpoints = self.cpu.breakpoints.clone();
self.cpu = NesCpu::new();
self.cpu.breakpoints = breakpoints;
#[cfg(feature = "debugger")]
{
let breakpoints = self.cpu.breakpoints.clone();
self.cpu = NesCpu::new();
self.cpu.breakpoints = breakpoints;
}
#[cfg(not(feature = "debugger"))]
{
self.cpu = NesCpu::new();
}

self.cpu_peripherals = NesCpuPeripherals::new(ppu, apu);
self.mb = mb;
Expand Down
17 changes: 10 additions & 7 deletions nes/rust/src/ppu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,14 +1064,16 @@ impl NesPpu {
self.attributetable_shift[1]
};
let extra_palette_bits = (attribute >> (2 * combined)) & 3;

if let Some((x, y)) = self.bg_debug {
if cycle == x && self.scanline_number == y as u16 {
//println!("PIXEL {},{} is {:x} {:x}", cycle, self.scanline_number, self.vram_address, self.scrollx)
#[cfg(feature = "debugger")]
{
if let Some((x, y)) = self.bg_debug {
if cycle == x && self.scanline_number == y as u16 {
//println!("PIXEL {},{} is {:x} {:x}", cycle, self.scanline_number, self.vram_address, self.scrollx)
}
}
if cycle == 8 {
//println!("PIXEL2 {},{} is {:x} {:x}", cycle, self.scanline_number, self.vram_address, self.scrollx)
}
}
if cycle == 8 {
//println!("PIXEL2 {},{} is {:x} {:x}", cycle, self.scanline_number, self.vram_address, self.scrollx)
}
let lower_bits = (upper_bit << 1) | lower_bit;

Expand Down Expand Up @@ -1483,6 +1485,7 @@ impl NesPpu {
}

/// Renders all sprites
#[cfg(feature = "debugger")]
pub fn render_sprites(&self, buf: &mut Box<RgbImage>, bus: &NesMotherboard) {
let allsprites = self.get_64_sprites();
for (i, pixel) in buf.data.chunks_exact_mut(3).enumerate() {
Expand Down
50 changes: 41 additions & 9 deletions nes/rust/src/windows/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,34 @@ impl TrackedWindow for MainNesWindow {
}
#[cfg(not(feature = "debugger"))]
{
c.cycle_step(&mut self.sound, &mut self.filter);
{
c.cycle_step(&mut sound, &mut self.audio_streaming, &mut self.filter);
}
if c.cpu_peripherals.ppu_frame_end() {
if !self.paused {
let image = c
.cpu_peripherals
.ppu_get_frame()
.to_pixels_egui()
.resize(c.local.configuration.scaler);
c.local.image = image;
}
self.recording.send_frame(&c.local.image);
if let Some(olocal) = &mut c.olocal {
if let Some(network) = &mut olocal.network {
if network.role() == NodeRole::PlayerHost {
let _e = network.video_data(&c.local.image);
}
}
}

if self.mouse_delay > 0 {
self.mouse_delay -= 1;
if self.mouse_delay == 0 {
self.mouse = false;
self.mouse_miss = false;
}
}
break 'emulator_loop;
}
}
Expand Down Expand Up @@ -925,8 +951,11 @@ impl TrackedWindow for MainNesWindow {
if r.hovered() {
if let Some(pos) = r.hover_pos() {
let coord = pos - r.rect.left_top();
c.cpu_peripherals.ppu.bg_debug =
Some(((coord.x / zoom) as u8, (coord.y / zoom) as u8));
#[cfg(feature = "debugger")]
{
c.cpu_peripherals.ppu.bg_debug =
Some(((coord.x / zoom) as u8, (coord.y / zoom) as u8));
}

let pixel = c.local.image.get_pixel(coord / zoom);
self.mouse_vision = !self.mouse_miss
Expand Down Expand Up @@ -957,12 +986,15 @@ impl TrackedWindow for MainNesWindow {
}
});

if let Some(s) = &mut self.sound_stream {
if c.paused && !self.paused {
self.paused = s.pause().is_ok();
}
if !c.paused && self.paused {
self.paused = s.play().is_err();
#[cfg(feature = "debugger")]
{
if let Some(s) = &mut self.sound_stream {
if c.paused && !self.paused {
self.paused = s.pause().is_ok();
}
if !c.paused && self.paused {
self.paused = s.play().is_err();
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion nes/rust/src/windows/name_table_dump_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ impl TrackedWindow for DumpWindow {
if pos.x >= 0.0 && pos.y >= 0.0 {
let pixelx = (pos.x / zoom).floor() as u8;
let pixely = (pos.y / zoom).floor() as u8;
c.cpu_peripherals.ppu.bg_debug = Some((pixelx, pixely));
#[cfg(feature = "debugger")]
{
c.cpu_peripherals.ppu.bg_debug = Some((pixelx, pixely));
}
let x = (pos.x / (8.0 * zoom)).floor() as usize;
let y = (pos.y / (8.0 * zoom)).floor() as usize;
let left = x < 32;
Expand Down
32 changes: 19 additions & 13 deletions nes/rust/src/windows/sprite_dump_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ impl TrackedWindow for DumpWindow {
egui_multiwin::egui::CentralPanel::default().show(&egui.egui_ctx, |ui| {
ui.label("PPU Sprite Dump Window");
egui_multiwin::egui::ScrollArea::vertical().show(ui, |ui| {
c.cpu_peripherals.ppu.render_sprites(&mut self.buf, &c.mb);
#[cfg(feature = "debugger")]
{
c.cpu_peripherals.ppu.render_sprites(&mut self.buf, &c.mb);
}
c.cpu_peripherals
.ppu
.render_palette(&mut self.palette, &c.mb);
Expand Down Expand Up @@ -137,18 +140,21 @@ impl TrackedWindow for DumpWindow {
let num = col + row * 16;

ui.label(format!("Sprite number is {:x}", num));
let sprites = c.cpu_peripherals.ppu.get_64_sprites();
ui.label(format!(
"Sprite tile is {:x} {:x}, attribute is {:x}",
sprites[num].tile_num(sprites[num].y(), 16),
sprites[num].tile_num(sprites[num].y() + 8, 16),
sprites[num].attribute(),
));
ui.label(format!(
"Location is {},{}",
sprites[num].x(),
sprites[num].y()
));
#[cfg(feature = "debugger")]
{
let sprites = c.cpu_peripherals.ppu.get_64_sprites();
ui.label(format!(
"Sprite tile is {:x} {:x}, attribute is {:x}",
sprites[num].tile_num(sprites[num].y(), 16),
sprites[num].tile_num(sprites[num].y() + 8, 16),
sprites[num].attribute(),
));
ui.label(format!(
"Location is {},{}",
sprites[num].x(),
sprites[num].y()
));
}
}
}
}
Expand Down

0 comments on commit 285bc28

Please sign in to comment.