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        // A line that changed after the last bus access changed in the last
68        // cycle, after the poll (U21): the next boundary decides on the lines
69        // as they stood at that access. The frame keeps only whether an
70        // enabled line was asserted; a swap of sources leaves that unchanged
71        // (issue 0005).
72        let pins = &mut self.pins;
73        pins.polled_lines = if pins.frame.deferred.is_some() && pins.lines != pins.lines_at_access {
74            Some(pins.lines_at_access)
75        } else {
76            None
77        };
78        self.cycles
79    }
80
81    /// Execute one instruction, interrupt entry or reset; return its cycle
82    /// count ([`Part::step`], the frame's).
83    ///
84    /// The fork: the usual boundary (no reset, not halted, nothing requested,
85    /// nothing the last poll left: `Part::step` would skip the interrupt
86    /// decision too) is tested in one go and runs the instruction here; any
87    /// other boundary is `Part::step`'s. The polled lines (issue 0005) need
88    /// no test of their own: `Part::step` skips the boundary decision that
89    /// would use them up at such a boundary too, and `execute_sequence`
90    /// rewrites them at its end on either path.
91    #[inline]
92    pub fn step(&mut self) -> u32 {
93        let frame = &self.pins.frame;
94        if frame.reset_pending
95            | frame.halted
96            | (frame.requests != 0)
97            | frame.polled.is_some()
98            | frame.polled_i.is_some()
99        {
100            return self.step_at_a_request();
101        }
102        let cycles = self.execute_sequence();
103        let frame = &mut self.pins.frame;
104        if let Some(deferred) = frame.deferred {
105            // A bus callback changed a request in the last cycle, after the poll.
106            frame.deferred = None;
107            frame.polled = Some(deferred);
108        }
109        cycles
110    }
111
112    /// A boundary with something to decide: the frame's own step.
113    #[inline(never)]
114    fn step_at_a_request(&mut self) -> u32 {
115        Part::step(self)
116    }
117
118    /// Run the start sequence at the next boundary (the frame's).
119    pub fn request_reset(&mut self) {
120        self.pins.frame.request_reset();
121    }
122
123    /// Latch an NMI edge (the frame's).
124    pub fn request_non_maskable_interrupt(&mut self) {
125        self.pins.frame.request_non_maskable_interrupt();
126    }
127
128    // -- state -----------------------------------------------------------
129
130    /// Return a snapshot of all CPU-owned state at a boundary.
131    ///
132    /// No host reads, no side effects. Memory and devices are the host's and
133    /// are not included.
134    pub fn capture_state(&self) -> CpuState {
135        let (irq1, irq2, tiq) = self.pins.irq_lines();
136        let poll = self.pins.frame.poll_fields();
137        CpuState {
138            a: self.a,
139            x: self.x,
140            y: self.y,
141            s: self.s,
142            pc: self.pc,
143            p: self.p,
144            mpr: self.mpr,
145            high_speed: self.pins.high_speed,
146            mpr_latch: self.mpr_latch,
147            irq1,
148            irq2,
149            tiq,
150            irq_disable: self.pins.irq_disable,
151            halted: poll.halted,
152            reset_pending: poll.reset_pending,
153            maskable_interrupt_pending: poll.maskable_interrupt_pending,
154            non_maskable_interrupt_pending: poll.non_maskable_interrupt_pending,
155            polled_i: poll.polled_i,
156            polled_maskable_interrupt: poll.polled_maskable_interrupt,
157            polled_non_maskable_interrupt: poll.polled_non_maskable_interrupt,
158            skipped_poll: poll.skipped_poll,
159            polled_irq_lines: self.pins.polled_lines,
160        }
161    }
162
163    /// Restore a captured CPU state without touching the host.
164    pub fn restore_state(&mut self, state: &CpuState) -> Result<(), String> {
165        state.validate()?;
166        self.a = state.a;
167        self.x = state.x;
168        self.y = state.y;
169        self.s = state.s;
170        self.pc = state.pc;
171        self.p = state.p;
172        self.mpr = state.mpr;
173        let pins = &mut self.pins;
174        pins.high_speed = state.high_speed;
175        self.mpr_latch = state.mpr_latch;
176        pins.lines =
177            (if state.irq1 { IRQ1_BIT } else { 0 }) | (if state.irq2 { IRQ2_BIT } else { 0 });
178        pins.lines |= if state.tiq { TIQ_BIT } else { 0 };
179        pins.poll_lines = pins.lines;
180        pins.polled_lines = state.polled_irq_lines;
181        pins.irq_disable = state.irq_disable;
182        pins.frame.restore_poll(&state.poll_fields());
183        // The reference's _restore_poll ends in its _confirm, which sets
184        // _lines_at_access; Frame::restore_poll cannot reach the lines.
185        pins.lines_at_access = pins.lines;
186        Ok(())
187    }
188}