// 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`, `wait_idle`, //! `stop_idle`, `reset`, `abort`, `nmi`, `irq`), the cycles, the instruction's //! 24-bit address and bytes (no mnemonic: the reference's reader disassembles //! the bytes), the complete state before and after (the reference's //! `CPUState` fields), and every bus access when recorded, one per cycle: //! `[kind, address, value]`, the kind `"r"`, `"w"` or `"i"` (an IO cycle, //! value 0) followed by the pins the chip asserted, in the order `d` VDA, `p` //! VPA, `v` VPB, `l` MLB (`"rdp"` an opcode fetch, `"wdl"` a read-modify-write's //! write). The reference's `w65c816.trace.read_trace` and //! `first_trace_divergence` read and compare a trace written here. Only //! writing is transcribed; comparing is the reference's job. //! //! Differences forced by Rust: none in the format. A record is written with //! its keys sorted and no spaces, as the reference's `write_trace` writes one, //! so that a trace written here equals, byte for byte, the reference's with //! the instruction's `mnemonic` and `operands` left out //! (`scripts/reference_trace.py` writes that form). use serde_json::{json, Map, Value}; use crate::core::{I, MLB, VDA, VPA, VPB}; 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, /// An IO cycle while WAI waits (or the one that ends it). WaitIdle, /// An IO cycle while STP has stopped the clock. StopIdle, Reset, Abort, Nmi, Irq, } impl BoundaryKind { pub const fn as_str(self) -> &'static str { match self { BoundaryKind::Instruction => "instruction", BoundaryKind::WaitIdle => "wait_idle", BoundaryKind::StopIdle => "stop_idle", BoundaryKind::Reset => "reset", BoundaryKind::Abort => "abort", BoundaryKind::Nmi => "nmi", BoundaryKind::Irq => "irq", } } } /// What the next `step()` will do, decided exactly as `W65C816.step()` decides. pub fn next_boundary(state: &CpuState) -> BoundaryKind { if state.reset_pending { return BoundaryKind::Reset; } if state.halted { return if state.waiting { BoundaryKind::WaitIdle } else { BoundaryKind::StopIdle }; } if state.skipped_poll { return BoundaryKind::Instruction; } if state.abort_pending { return BoundaryKind::Abort; } let nmi = state.polled_non_maskable_interrupt; if nmi.unwrap_or(state.non_maskable_interrupt_pending) { return BoundaryKind::Nmi; } let irq = state.polled_maskable_interrupt; let masked = match state.polled_i { None => state.p & I != 0, Some(seen) => seen, }; if irq.unwrap_or(state.maskable_interrupt_pending) && !masked { return BoundaryKind::Irq; } BoundaryKind::Instruction } /// The trace's text for a cycle: `r`, `w` or `i`, then the pins asserted. pub fn access_kind(direction: char, kind: u8) -> String { let mut text = String::with_capacity(5); text.push(direction); if kind & VDA != 0 { text.push('d'); } if kind & VPA != 0 { text.push('p'); } if kind & VPB != 0 { text.push('v'); } if kind & MLB != 0 { text.push('l'); } text } /// One bus cycle as the trace records it: direction (`'r'`, `'w'`, `'i'`), /// the cycle's kind bits, address, value (0 for an IO cycle). #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Access { pub direction: char, pub kind: u8, pub address: u32, pub value: u8, } /// One processor boundary. #[derive(Clone, Debug)] pub struct StepRecord { pub sequence: u64, pub kind: BoundaryKind, pub cycles: u32, /// The instruction's 24-bit address and bytes, for an instruction boundary. pub instruction: Option<(u32, Vec)>, pub before: CpuState, pub after: CpuState, pub accesses: Option>, } pub fn state_value(state: &CpuState) -> Value { json!({ "a": state.a, "x": state.x, "y": state.y, "s": state.s, "d": state.d, "dbr": state.dbr, "pbr": state.pbr, "pc": state.pc, "p": state.p, "e": state.e, "waiting": state.waiting, "stopped": state.stopped, "abort_pending": state.abort_pending, "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| { json!([ access_kind(access.direction, access.kind), access.address, access.value ]) }) .collect(); record.insert("accesses".into(), Value::Array(list)); } Value::Object(record) } }