1use serde_json::{json, Map, Value};
17
18use crate::core::I;
19use crate::dispatch::OPCODES;
20use crate::state::CpuState;
21
22pub const TRACE_SCHEMA_VERSION: u32 = 1;
23
24#[derive(Clone, Copy, Debug, PartialEq, Eq)]
26pub enum BoundaryKind {
27 Instruction,
28 Reset,
29 Nmi,
30 Irq1,
31 Irq2,
32 Tiq,
33}
34
35impl BoundaryKind {
36 pub const fn as_str(self) -> &'static str {
37 match self {
38 BoundaryKind::Instruction => "instruction",
39 BoundaryKind::Reset => "reset",
40 BoundaryKind::Nmi => "nmi",
41 BoundaryKind::Irq1 => "irq1",
42 BoundaryKind::Irq2 => "irq2",
43 BoundaryKind::Tiq => "tiq",
44 }
45 }
46}
47
48pub fn next_boundary(state: &CpuState) -> BoundaryKind {
51 if state.reset_pending {
52 return BoundaryKind::Reset;
53 }
54 if state.skipped_poll {
55 return BoundaryKind::Instruction;
56 }
57 let nmi = state
58 .polled_non_maskable_interrupt
59 .unwrap_or(state.non_maskable_interrupt_pending);
60 if nmi {
61 return BoundaryKind::Nmi;
62 }
63 let irq = state
64 .polled_maskable_interrupt
65 .unwrap_or(state.maskable_interrupt_pending);
66 let masked = state.polled_i.unwrap_or(state.p & I != 0);
67 if irq && !masked {
68 if state.tiq && state.irq_disable & 4 == 0 {
70 return BoundaryKind::Tiq;
71 }
72 if state.irq1 && state.irq_disable & 2 == 0 {
73 return BoundaryKind::Irq1;
74 }
75 return BoundaryKind::Irq2;
76 }
77 BoundaryKind::Instruction
78}
79
80pub fn instruction_length(opcode: u8) -> usize {
83 OPCODES[opcode as usize].3.bytes()
84}
85
86#[derive(Clone, Copy, Debug, PartialEq, Eq)]
88pub enum Access {
89 Read(u32, u8),
90 Write(u32, u8),
91 Dummy(u32),
93 Idle,
95}
96
97#[derive(Clone, Debug)]
99pub struct StepRecord {
100 pub sequence: u64,
101 pub kind: BoundaryKind,
102 pub cycles: u32,
103 pub instruction: Option<(u16, Vec<u8>)>,
105 pub before: CpuState,
106 pub after: CpuState,
107 pub accesses: Option<Vec<Access>>,
108}
109
110fn state_value(state: &CpuState) -> Value {
111 json!({
112 "a": state.a,
113 "x": state.x,
114 "y": state.y,
115 "s": state.s,
116 "pc": state.pc,
117 "p": state.p,
118 "mpr": state.mpr,
119 "high_speed": state.high_speed,
120 "mpr_latch": state.mpr_latch,
121 "irq1": state.irq1,
122 "irq2": state.irq2,
123 "tiq": state.tiq,
124 "irq_disable": state.irq_disable,
125 "halted": state.halted,
126 "reset_pending": state.reset_pending,
127 "maskable_interrupt_pending": state.maskable_interrupt_pending,
128 "non_maskable_interrupt_pending": state.non_maskable_interrupt_pending,
129 "polled_i": state.polled_i,
130 "polled_maskable_interrupt": state.polled_maskable_interrupt,
131 "polled_non_maskable_interrupt": state.polled_non_maskable_interrupt,
132 "skipped_poll": state.skipped_poll,
133 })
134}
135
136impl StepRecord {
137 pub fn to_value(&self) -> Value {
139 let mut record = Map::new();
140 record.insert("version".into(), json!(TRACE_SCHEMA_VERSION));
141 record.insert("sequence".into(), json!(self.sequence));
142 record.insert("kind".into(), json!(self.kind.as_str()));
143 record.insert("cycles".into(), json!(self.cycles));
144 let instruction = match &self.instruction {
145 None => Value::Null,
146 Some((address, data)) => {
147 let hex: String = data.iter().map(|b| format!("{b:02x}")).collect();
148 json!({"address": address, "data": hex})
149 }
150 };
151 record.insert("instruction".into(), instruction);
152 record.insert("before".into(), state_value(&self.before));
153 record.insert("after".into(), state_value(&self.after));
154 if let Some(accesses) = &self.accesses {
155 let list: Vec<Value> = accesses
156 .iter()
157 .map(|access| match *access {
158 Access::Read(address, value) => json!(["r", address, value]),
159 Access::Write(address, value) => json!(["w", address, value]),
160 Access::Dummy(address) => json!(["d", address, 0]),
161 Access::Idle => json!(["i", 0, 0]),
162 })
163 .collect();
164 record.insert("accesses".into(), Value::Array(list));
165 }
166 Value::Object(record)
167 }
168
169 pub fn to_line(&self) -> String {
171 self.to_value().to_string()
172 }
173}