// SPDX-License-Identifier: MIT //! Boundary traces in the family's version 1 JSON Lines schema (`trace.py`, //! and `debug.py`'s `BoundaryKind`, `next_boundary` and access kinds). //! //! A record carries the boundary kind (`instruction`, `wai`, `stp`, `reset`, //! `nmi`, `irq`), the cycles, the instruction's address and bytes (no //! mnemonic: the reference's reader disassembles the bytes for the row), the //! complete state before and after (the reference's `CPUState` fields), and //! every bus access when recorded, one per cycle: `"rs"` an opcode fetch //! (`sync`), `"rf"` another read of the instruction stream at PC (`fetch`), //! `"r"` any other read, `"w"` a write. The reference's //! `w65c02_python.trace.read_trace(stream, row)` and `first_trace_divergence` //! compare a trace written here with one its `W65C02Session` wrote. Only //! writing is transcribed; comparing is the reference's job. //! //! Differences forced by Rust: none in the format. The record is built by the //! host (`src/bin/trace.rs`) where the reference's `DebugSession` builds it. use serde_json::{json, Map, Value}; use crate::core::I; use crate::cpu::Row; use crate::dispatch::{form_bytes, opcodes_for}; use crate::state::CpuState; pub const TRACE_SCHEMA_VERSION: u32 = 1; /// Kind of work performed by one `step()` call. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum BoundaryKind { Instruction, /// A cycle spent waiting after WAI. Wai, /// A cycle spent stopped after STP. Stp, Reset, Nmi, Irq, } impl BoundaryKind { pub const fn as_str(self) -> &'static str { match self { BoundaryKind::Instruction => "instruction", BoundaryKind::Wai => "wai", BoundaryKind::Stp => "stp", BoundaryKind::Reset => "reset", BoundaryKind::Nmi => "nmi", BoundaryKind::Irq => "irq", } } } /// What the next `step()` will do, decided exactly as the core's `step()` decides. pub fn next_boundary(state: &CpuState) -> BoundaryKind { if state.reset_pending { return BoundaryKind::Reset; } if state.stopped { return BoundaryKind::Stp; } let mut nmi = state.non_maskable_interrupt_pending; let mut irq = state.maskable_interrupt_pending; let mut masked = state.p & I != 0; if state.waiting { // A request ends the wait, and the step that finds it is the entry or, // with IRQ and I set, the next instruction (U6). DOC (DS 3.4); the // timing of the wake is HYPOTHESIS (U6). if !(nmi || irq) { return BoundaryKind::Wai; } } else { if state.skipped_poll { return BoundaryKind::Instruction; } if let Some(seen) = state.polled_non_maskable_interrupt { nmi = seen; } if let Some(seen) = state.polled_maskable_interrupt { irq = seen; } if let Some(seen) = state.polled_i { masked = seen; } } if nmi { return BoundaryKind::Nmi; } if irq && !masked { return BoundaryKind::Irq; } BoundaryKind::Instruction } /// How many bytes the instruction with this opcode occupies on `row`, as the /// reference's disassembler decodes it (`FORM_BYTES` of the row's form). pub fn instruction_length(row: &Row, opcode: u8) -> usize { form_bytes(opcodes_for(row)[opcode as usize].3) } /// A bus access as the trace records it. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Access { /// `"rs"`: an opcode fetch (`sync`). Sync(u16, u8), /// `"rf"`: another read at PC (`fetch`). Fetch(u16, u8), /// `"r"`: a data, stack, vector or dummy read. Read(u16, u8), /// `"w"`. Write(u16, u8), } /// One processor boundary. #[derive(Clone, Debug)] pub struct StepRecord { pub sequence: u64, pub kind: BoundaryKind, pub cycles: u32, /// The instruction's address and bytes, for an instruction boundary. pub instruction: Option<(u16, Vec)>, pub before: CpuState, pub after: CpuState, pub accesses: Option>, } fn state_value(state: &CpuState) -> Value { json!({ "a": state.a, "x": state.x, "y": state.y, "s": state.s, "pc": state.pc, "p": state.p, "waiting": state.waiting, "stopped": state.stopped, "halted": state.halted, "reset_pending": state.reset_pending, "maskable_interrupt_pending": state.maskable_interrupt_pending, "non_maskable_interrupt_pending": state.non_maskable_interrupt_pending, "polled_i": state.polled_i, "polled_maskable_interrupt": state.polled_maskable_interrupt, "polled_non_maskable_interrupt": state.polled_non_maskable_interrupt, "skipped_poll": state.skipped_poll, }) } impl StepRecord { /// The versioned JSON representation (keys sorted, as the reference writes them). pub fn to_value(&self) -> Value { let mut record = Map::new(); record.insert("version".into(), json!(TRACE_SCHEMA_VERSION)); record.insert("sequence".into(), json!(self.sequence)); record.insert("kind".into(), json!(self.kind.as_str())); record.insert("cycles".into(), json!(self.cycles)); let instruction = match &self.instruction { None => Value::Null, Some((address, data)) => { let hex: String = data.iter().map(|b| format!("{b:02x}")).collect(); json!({"address": address, "data": hex}) } }; record.insert("instruction".into(), instruction); record.insert("before".into(), state_value(&self.before)); record.insert("after".into(), state_value(&self.after)); if let Some(accesses) = &self.accesses { let list: Vec = accesses .iter() .map(|access| match *access { Access::Sync(address, value) => json!(["rs", address, value]), Access::Fetch(address, value) => json!(["rf", address, value]), Access::Read(address, value) => json!(["r", address, value]), Access::Write(address, value) => json!(["w", address, value]), }) .collect(); record.insert("accesses".into(), Value::Array(list)); } Value::Object(record) } /// One JSON Lines line, without the newline. pub fn to_line(&self) -> String { self.to_value().to_string() } }