rustynes_core/debug.rs
1//! Read-only debug snapshots for the frontend's egui debugger UI.
2//!
3//! Plain-data structs the UI can hold for the lifetime of a frame
4//! without borrowing into the emulator. The accessors on [`crate::Nes`]
5//! produce these once per visible frame at 60 Hz; a few KiB of cloning
6//! is cheap compared to actually running a frame.
7//!
8//! Determinism contract: opening the debugger must NOT alter the
9//! emulator's deterministic state. Every method in this module is
10//! read-only.
11
12pub use rustynes_mappers::MapperDebugInfo;
13
14/// Snapshot of CPU register file.
15#[derive(Debug, Clone)]
16pub struct CpuDebugView {
17 /// Accumulator.
18 pub a: u8,
19 /// X index register.
20 pub x: u8,
21 /// Y index register.
22 pub y: u8,
23 /// Stack pointer.
24 pub s: u8,
25 /// Program counter.
26 pub pc: u16,
27 /// Raw status flags.
28 pub p: u8,
29 /// `true` when the CPU is jammed (illegal halt opcode).
30 pub jammed: bool,
31 /// Cumulative CPU cycle count.
32 pub cycles: u64,
33}
34
35/// Snapshot of PPU state.
36#[derive(Debug, Clone)]
37pub struct PpuDebugView {
38 /// Current dot (0..=340).
39 pub dot: u16,
40 /// Current scanline.
41 pub scanline: i16,
42 /// Current frame count.
43 pub frame: u64,
44 /// PPUCTRL ($2000) snapshot.
45 pub ctrl: u8,
46 /// PPUMASK ($2001) snapshot.
47 pub mask: u8,
48 /// PPUSTATUS ($2002) snapshot.
49 pub status: u8,
50 /// OAMADDR ($2003) snapshot.
51 pub oam_addr: u8,
52 /// Loopy `v` register.
53 pub v: u16,
54 /// Loopy `t` register.
55 pub t: u16,
56 /// Fine X scroll.
57 pub fine_x: u8,
58 /// Write toggle (`w`).
59 pub w_toggle: bool,
60 /// 8x16 sprites enabled.
61 pub sprite_size_16: bool,
62 /// Background pattern table base (0 or 0x1000).
63 pub bg_pattern_base: u16,
64 /// Sprite pattern table base (0 or 0x1000).
65 pub sprite_pattern_base: u16,
66 /// NMI line currently asserted.
67 pub nmi_line: bool,
68}
69
70/// Snapshot of APU channel outputs and frame counter.
71#[derive(Debug, Clone)]
72pub struct ApuDebugView {
73 /// Pulse 1 raw output (0..=15).
74 pub pulse1: u8,
75 /// Pulse 2 raw output (0..=15).
76 pub pulse2: u8,
77 /// Triangle raw output (0..=15).
78 pub triangle: u8,
79 /// Noise raw output (0..=15).
80 pub noise: u8,
81 /// DMC output (0..=127).
82 pub dmc: u8,
83 /// v2.1.6 "Expansion Audio" — the most recent RAW on-cart expansion-audio
84 /// sample (VRC6/VRC7/FDS/MMC5/Namco 163/Sunsoft 5B), pre-UI-gain. `0.0`
85 /// when the board has no expansion audio. A read-only display tap for the
86 /// frontend Audio Mixer expansion oscilloscope / VU meter; sampling it does
87 /// not perturb the deterministic mix.
88 pub external: f32,
89 /// Frame counter IRQ pending.
90 pub frame_irq: bool,
91 /// DMC IRQ pending.
92 pub dmc_irq: bool,
93}
94
95/// Snapshot of mapper state — alias for [`rustynes_mappers::MapperDebugInfo`].
96pub type MapperDebugView = MapperDebugInfo;
97
98/// v3.7.0 (`T-HACK-BPS-RECORDER`, live ROM writes) — which ROM section of the
99/// cartridge image a debugger ROM read or write addresses.
100///
101/// Offsets within a section are the image's: PRG byte 0 is the first byte
102/// after the iNES header (and any trainer), CHR byte 0 the first byte after
103/// PRG-ROM. They are NOT CPU or PPU addresses, so a byte has one offset
104/// whatever bank currently maps it, which is what a patch file records.
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
106pub enum RomSpace {
107 /// PRG-ROM, the program (read by the CPU at `$8000-$FFFF` through the
108 /// mapper's PRG banking).
109 Prg,
110 /// CHR-ROM, the pattern tables (read by the PPU at `$0000-$1FFF` through
111 /// the mapper's CHR banking).
112 Chr,
113}
114
115impl RomSpace {
116 /// `"PRG"` or `"CHR"`, for messages.
117 #[must_use]
118 pub const fn label(self) -> &'static str {
119 match self {
120 Self::Prg => "PRG",
121 Self::Chr => "CHR",
122 }
123 }
124}
125
126/// Why a debugger ROM write (`Nes::debug_poke_rom`) changed nothing.
127#[derive(Debug, Clone, Copy, PartialEq, Eq)]
128pub enum RomWriteError {
129 /// The loaded board does not offer that ROM section as a plain buffer in
130 /// image order: a CHR-RAM board's CHR, a self-flashable board's PRG, a
131 /// copier's DRAM, an FDS disk or an NSF (see
132 /// `Mapper::debug_prg_rom_mut`).
133 Unsupported(RomSpace),
134 /// The offset is past the section's end.
135 OutOfRange {
136 /// The section.
137 space: RomSpace,
138 /// The offset asked for.
139 offset: usize,
140 /// The section's length.
141 len: usize,
142 },
143}
144
145impl core::fmt::Display for RomWriteError {
146 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
147 match self {
148 Self::Unsupported(space) => write!(
149 f,
150 "this board's {} is not ROM the debugger can edit",
151 space.label()
152 ),
153 Self::OutOfRange { space, offset, len } => write!(
154 f,
155 "{} offset {offset:#x} is past its {len:#x} bytes",
156 space.label()
157 ),
158 }
159 }
160}