// SPDX-License-Identifier: MIT //! Debugging and tracing on 6502-python's tools, with the board's own events. //! //! - `session` gives a `sixfiveohtwo.DebugSession` on the board //! (breakpoints, memory watchpoints, history) with a side-effect-free peek. //! - [`BeamWatch`] stops a run at an ANTIC beam position or on a write to a //! chip register (GTIA $C0xx, ANTIC $D4xx, POKEY $E8xx by register number). //! - [`trace_jsonl`] writes one JSON line per CPU step in 6502-python's //! trace schema (`step_record_to_dict`) plus the board's bus events of that //! step: machine-cycle time, kind ("R", "W", "D" ANTIC DMA, "S" a read //! repeated while WSYNC holds RDY), address, value, and ANTIC's line/cycle. //! //! A transcription of a5200-python's `debug.py` (at cf82d44). Rust-forced differences: //! 6502-rust has no `DebugSession` (it transcribes only the trace writer, `trace::StepRecord`), //! so `session` is not offered and `trace_jsonl` builds each record itself from the CPU state //! before and after the step (`trace::next_boundary`, the instruction bytes by `peek`), with no //! `accesses` list (the board's `bus` events carry them). `BeamWatch` shares its `hit` and //! watched registers with its observer through `Rc>`, and its run methods take the //! CPU that holds the board. use std::cell::RefCell; use std::collections::HashSet; use std::io::Write; use std::rc::Rc; use serde_json::{json, Value}; use sixfiveohtwo::trace::{instruction_length, next_boundary, BoundaryKind, StepRecord}; use crate::board::{Cpu, Running}; /// DOC: the memory map ([AHRM] 13.2, 13.4; [FSM] decoder sheet). pub const CHIPS: [(&str, (i32, i32, i32)); 3] = [ ("gtia", (0xC000, 0xD000, 0x1F)), ("antic", (0xD400, 0xD500, 0x0F)), ("pokey", (0xE800, 0xF000, 0x0F)), ]; /// (chip, register, value, line, cycle, machine cycle) pub type Hit = (&'static str, i32, u8, i32, i32, u64); /// Run the board until ANTIC reaches (line, cycle) or a chip register is written. pub struct BeamWatch { pub register_writes: Rc>>, pub hit: Rc>>, } impl BeamWatch { pub fn new(cpu: &mut Cpu) -> Self { let register_writes: Rc>> = Rc::default(); let hit: Rc>> = Rc::default(); let (writes, found) = (Rc::clone(®ister_writes), Rc::clone(&hit)); cpu.bus .bus_observers .push(Box::new(move |kind, address, value, cycle, antic| { let writes = writes.borrow(); if kind != 'W' || writes.is_empty() { return; } for (chip, (low, high, mask)) in CHIPS { if low <= address && address < high && writes.contains(&(chip, address & mask)) { *found.borrow_mut() = Some(( chip, address & mask, value, antic.vcount, antic.hcount, cycle, )); } } })); BeamWatch { register_writes, hit, } } pub fn watch_register(&mut self, chip: &'static str, register: i32) { self.register_writes.borrow_mut().insert((chip, register)); } pub fn run_to_beam(&mut self, cpu: &mut Cpu, line: i32, cycle: i32, limit: u64) -> bool { let start = cpu.bus.cycles; while cpu.bus.cycles - start < limit { if cpu.bus.antic.vcount == line && cpu.bus.antic.hcount >= cycle { return true; } cpu.board_step(); } false } pub fn run_to_register_write(&mut self, cpu: &mut Cpu, limit: u64) -> Option { *self.hit.borrow_mut() = None; let start = cpu.bus.cycles; while self.hit.borrow().is_none() && cpu.bus.cycles - start < limit { cpu.board_step(); } *self.hit.borrow() } } /// Trace `steps` CPU steps to `stream`; returns the number written. pub fn trace_jsonl(cpu: &mut Cpu, stream: &mut dyn Write, steps: u64) -> std::io::Result { let events: Rc>> = Rc::default(); let sink = Rc::clone(&events); cpu.bus .bus_observers .push(Box::new(move |kind, address, value, cycle, antic| { sink.borrow_mut().push(json!([ cycle, kind.to_string(), address, value, antic.vcount, antic.hcount ])); })); let observer = cpu.bus.bus_observers.len() - 1; let mut result = Ok(steps); for sequence in 0..steps { events.borrow_mut().clear(); let before = cpu.capture_state(); let kind = next_boundary(&before); let instruction = if kind == BoundaryKind::Instruction { let pc = before.pc; let length = instruction_length(cpu.bus.peek(pc)); let data = (0..length as u16) .map(|i| cpu.bus.peek(pc.wrapping_add(i))) .collect(); Some((pc, data)) } else { None }; let cycles = cpu.step(); let record = StepRecord { sequence, kind, cycles, instruction, before, after: cpu.capture_state(), accesses: None, }; let mut line = record.to_value(); line["bus"] = Value::Array(events.borrow().clone()); if let Err(e) = writeln!(stream, "{line}") { result = Err(e); break; } } drop(cpu.bus.bus_observers.remove(observer)); result }