// SPDX-License-Identifier: MIT //! Boundary traces in the reference's version 1 JSON Lines schema //! (`trace.py` and `debug.py`'s `BoundaryKind`, `next_boundary` and accesses; //! the reference's docs/trace-schema.md). //! //! A record carries the boundary kind (`instruction` or `stop_idle`), the //! cycles, the instruction's address and bytes (no mnemonic: "a producer in //! another language should omit both", the schema says; the reference's reader //! disassembles the bytes), the state before and after, and every cycle when //! recorded, as `[kind, address, value, dummy]`: `"read"`, `"write"` or //! `"internal"` (address and value `null`). The reference's //! `spc700_python.trace.read_trace` and `first_trace_divergence` compare a //! trace written here with one its `DebugSession` wrote. //! //! Differences forced by Rust: none in the format. A record is written with //! sorted keys and no spaces, as the reference's `write_trace` writes one, so a //! trace written here equals the reference's byte for byte once that one's //! `mnemonic` and `operands` are left out (`scripts/reference_trace.py`). use serde_json::{json, Map, Value}; use crate::opcodes::OPCODES; 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, /// One idle cycle of a stopped CPU (SLEEP, STOP). StopIdle, } impl BoundaryKind { pub const fn as_str(self) -> &'static str { match self { BoundaryKind::Instruction => "instruction", BoundaryKind::StopIdle => "stop_idle", } } } /// What the next `step()` will do: `stop_idle` when stopped, else an instruction. pub fn next_boundary(state: &CpuState) -> BoundaryKind { if state.stopped { BoundaryKind::StopIdle } else { BoundaryKind::Instruction } } /// The bytes of the instruction at `address`, as the reference's `disassemble` /// reads them (its opcode table's length; the address wraps at $FFFF). pub fn instruction_bytes(peek: &mut dyn FnMut(u16) -> u8, address: u16) -> Vec { let opcode = peek(address); (0..OPCODES[opcode as usize].length as u16) .map(|i| { if i == 0 { opcode } else { peek(address.wrapping_add(i)) } }) .collect() } /// One bus cycle as the trace records it. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Access { Read(u16, u8, bool), // address, value, dummy Write(u16, u8), Internal, } /// One processor boundary. #[derive(Clone, Debug)] pub struct StepRecord { pub sequence: u64, pub kind: BoundaryKind, pub cycles: u32, pub instruction: Option<(u16, 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, "sp": state.sp, "pc": state.pc, "psw": state.psw, "stopped": state.stopped, }) } 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::Read(address, value, dummy) => json!(["read", address, value, dummy]), Access::Write(address, value) => json!(["write", address, value, false]), Access::Internal => json!(["internal", null, null, false]), }) .collect(); record.insert("accesses".into(), Value::Array(list)); } Value::Object(record) } }