huc6280/
cpu.rs

1//! The public HuC6280 CPU (`cpu.py`): the frame's four hooks, the step, and
2//! state capture.
3//!
4//! The host owns memory and every device: it gives the CPU a [`Bus`], calls
5//! [`HuC6280::step`], and runs its devices inside the callbacks for as long
6//! as each cycle lasts on its board. See the reference's
7//! docs/handoff-brief.md, "The bus contract".
8//!
9//! Differences forced by Rust: the reference's class is nine mixins on
10//! `CPUFrame`; here each mixin is an `impl` block in its own module and the
11//! frame is a field ([`Pins::frame`]) reached through 6502-rust's [`Part`]
12//! trait, whose provided `step()` is the frame's.
13
14use sixfiveohtwo::frame::{Frame, Part};
15
16use crate::control::{IRQ1_BIT, IRQ2_BIT, TIQ_BIT};
17use crate::core::{Bus, HuC6280, T};
18use crate::state::CpuState;
19
20impl<B: Bus> Part for HuC6280<B> {
21    fn frame(&self) -> &Frame {
22        &self.pins.frame
23    }
24
25    fn frame_mut(&mut self) -> &mut Frame {
26        &mut self.pins.frame
27    }
28
29    fn status(&self) -> u8 {
30        self.p
31    }
32
33    fn accept_reset(&mut self) -> u32 {
34        self.reset_sequence()
35    }
36
37    fn halted_cycle(&mut self) -> u32 {
38        unimplemented!("the HuC6280 has no halted state")
39    }
40
41    fn enter_interrupt(&mut self, kind: u8) -> u32 {
42        self.enter_interrupt_sequence(kind)
43    }
44
45    fn execute(&mut self) -> u32 {
46        self.execute_sequence()
47    }
48
49    fn boundary_interrupt(&mut self) -> u8 {
50        self.boundary_interrupt_sequence()
51    }
52}
53
54impl<B: Bus> HuC6280<B> {
55    /// One instruction: the fetch, then the handler's cycles (`_execute`).
56    pub(crate) fn execute_sequence(&mut self) -> u32 {
57        self.cycles = 0;
58        let pc = self.pc;
59        let opcode = self.cycle_read(pc);
60        self.pc = pc.wrapping_add(1);
61        self.pins.opcode = opcode;
62        // U4: the memory-operation flag is cleared in the fetch cycle of the
63        // next instruction; the handler sees what it was.
64        self.t = self.p & T;
65        self.p &= !T;
66        self.dispatch_opcode(opcode);
67        self.cycles
68    }
69
70    /// Execute one instruction, interrupt entry or reset; return its cycle
71    /// count ([`Part::step`], the frame's).
72    ///
73    /// The fork: the usual boundary (no reset, not halted, nothing requested,
74    /// nothing the last poll left: `Part::step` would skip the interrupt
75    /// decision too) is tested in one go and runs the instruction here; any
76    /// other boundary is `Part::step`'s.
77    #[inline]
78    pub fn step(&mut self) -> u32 {
79        let frame = &self.pins.frame;
80        if frame.reset_pending
81            | frame.halted
82            | (frame.requests != 0)
83            | frame.polled.is_some()
84            | frame.polled_i.is_some()
85        {
86            return self.step_at_a_request();
87        }
88        let cycles = self.execute_sequence();
89        let frame = &mut self.pins.frame;
90        if let Some(deferred) = frame.deferred {
91            // A bus callback changed a request in the last cycle, after the poll.
92            frame.deferred = None;
93            frame.polled = Some(deferred);
94        }
95        cycles
96    }
97
98    /// A boundary with something to decide: the frame's own step.
99    #[inline(never)]
100    fn step_at_a_request(&mut self) -> u32 {
101        Part::step(self)
102    }
103
104    /// Run the start sequence at the next boundary (the frame's).
105    pub fn request_reset(&mut self) {
106        self.pins.frame.request_reset();
107    }
108
109    /// Latch an NMI edge (the frame's).
110    pub fn request_non_maskable_interrupt(&mut self) {
111        self.pins.frame.request_non_maskable_interrupt();
112    }
113
114    // -- state -----------------------------------------------------------
115
116    /// Return a snapshot of all CPU-owned state at a boundary.
117    ///
118    /// No host reads, no side effects. Memory and devices are the host's and
119    /// are not included.
120    pub fn capture_state(&self) -> CpuState {
121        let (irq1, irq2, tiq) = self.pins.irq_lines();
122        let poll = self.pins.frame.poll_fields();
123        CpuState {
124            a: self.a,
125            x: self.x,
126            y: self.y,
127            s: self.s,
128            pc: self.pc,
129            p: self.p,
130            mpr: self.mpr,
131            high_speed: self.pins.high_speed,
132            mpr_latch: self.mpr_latch,
133            irq1,
134            irq2,
135            tiq,
136            irq_disable: self.pins.irq_disable,
137            halted: poll.halted,
138            reset_pending: poll.reset_pending,
139            maskable_interrupt_pending: poll.maskable_interrupt_pending,
140            non_maskable_interrupt_pending: poll.non_maskable_interrupt_pending,
141            polled_i: poll.polled_i,
142            polled_maskable_interrupt: poll.polled_maskable_interrupt,
143            polled_non_maskable_interrupt: poll.polled_non_maskable_interrupt,
144            skipped_poll: poll.skipped_poll,
145        }
146    }
147
148    /// Restore a captured CPU state without touching the host.
149    pub fn restore_state(&mut self, state: &CpuState) -> Result<(), String> {
150        state.validate()?;
151        self.a = state.a;
152        self.x = state.x;
153        self.y = state.y;
154        self.s = state.s;
155        self.pc = state.pc;
156        self.p = state.p;
157        self.mpr = state.mpr;
158        let pins = &mut self.pins;
159        pins.high_speed = state.high_speed;
160        self.mpr_latch = state.mpr_latch;
161        pins.lines =
162            (if state.irq1 { IRQ1_BIT } else { 0 }) | (if state.irq2 { IRQ2_BIT } else { 0 });
163        pins.lines |= if state.tiq { TIQ_BIT } else { 0 };
164        pins.lines_before = pins.lines;
165        pins.poll_lines = pins.lines;
166        pins.irq_disable = state.irq_disable;
167        pins.frame.restore_poll(&state.poll_fields());
168        Ok(())
169    }
170}