huc6280/
cpu.rs

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