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::dispatch::OPCODES;
19use crate::state::CpuState;
20
21impl<B: Bus> Part for HuC6280<B> {
22    fn frame(&self) -> &Frame {
23        &self.pins.frame
24    }
25
26    fn frame_mut(&mut self) -> &mut Frame {
27        &mut self.pins.frame
28    }
29
30    fn status(&self) -> u8 {
31        self.p
32    }
33
34    fn accept_reset(&mut self) -> u32 {
35        self.reset_sequence()
36    }
37
38    fn halted_cycle(&mut self) -> u32 {
39        unimplemented!("the HuC6280 has no halted state")
40    }
41
42    fn enter_interrupt(&mut self, kind: u8) -> u32 {
43        self.enter_interrupt_sequence(kind)
44    }
45
46    fn execute(&mut self) -> u32 {
47        self.execute_sequence()
48    }
49
50    fn boundary_interrupt(&mut self) -> u8 {
51        self.boundary_interrupt_sequence()
52    }
53}
54
55impl<B: Bus> HuC6280<B> {
56    /// One instruction: the fetch, then the handler's cycles (`_execute`).
57    pub(crate) fn execute_sequence(&mut self) -> u32 {
58        self.cycles = 0;
59        let pc = self.pc;
60        let opcode = self.cycle_read(pc);
61        self.pc = pc.wrapping_add(1);
62        self.pins.opcode = opcode;
63        // U4: the memory-operation flag is cleared in the fetch cycle of the
64        // next instruction; the handler sees what it was.
65        self.t = self.p & T;
66        self.p &= !T;
67        let (op, mode, ..) = OPCODES[opcode as usize];
68        self.dispatch(op, mode);
69        self.cycles
70    }
71
72    /// Execute one instruction, interrupt entry or reset; return its cycle
73    /// count ([`Part::step`], the frame's).
74    #[inline]
75    pub fn step(&mut self) -> u32 {
76        Part::step(self)
77    }
78
79    /// Run the start sequence at the next boundary (the frame's).
80    pub fn request_reset(&mut self) {
81        self.pins.frame.request_reset();
82    }
83
84    /// Latch an NMI edge (the frame's).
85    pub fn request_non_maskable_interrupt(&mut self) {
86        self.pins.frame.request_non_maskable_interrupt();
87    }
88
89    // -- state -----------------------------------------------------------
90
91    /// Return a snapshot of all CPU-owned state at a boundary.
92    ///
93    /// No host reads, no side effects. Memory and devices are the host's and
94    /// are not included.
95    pub fn capture_state(&self) -> CpuState {
96        let (irq1, irq2, tiq) = self.pins.irq_lines();
97        let poll = self.pins.frame.poll_fields();
98        CpuState {
99            a: self.a,
100            x: self.x,
101            y: self.y,
102            s: self.s,
103            pc: self.pc,
104            p: self.p,
105            mpr: self.mpr,
106            high_speed: self.pins.high_speed,
107            mpr_latch: self.mpr_latch,
108            irq1,
109            irq2,
110            tiq,
111            irq_disable: self.pins.irq_disable,
112            halted: poll.halted,
113            reset_pending: poll.reset_pending,
114            maskable_interrupt_pending: poll.maskable_interrupt_pending,
115            non_maskable_interrupt_pending: poll.non_maskable_interrupt_pending,
116            polled_i: poll.polled_i,
117            polled_maskable_interrupt: poll.polled_maskable_interrupt,
118            polled_non_maskable_interrupt: poll.polled_non_maskable_interrupt,
119            skipped_poll: poll.skipped_poll,
120        }
121    }
122
123    /// Restore a captured CPU state without touching the host.
124    pub fn restore_state(&mut self, state: &CpuState) -> Result<(), String> {
125        state.validate()?;
126        self.a = state.a;
127        self.x = state.x;
128        self.y = state.y;
129        self.s = state.s;
130        self.pc = state.pc;
131        self.p = state.p;
132        self.mpr = state.mpr;
133        let pins = &mut self.pins;
134        pins.high_speed = state.high_speed;
135        self.mpr_latch = state.mpr_latch;
136        pins.lines =
137            (if state.irq1 { IRQ1_BIT } else { 0 }) | (if state.irq2 { IRQ2_BIT } else { 0 });
138        pins.lines |= if state.tiq { TIQ_BIT } else { 0 };
139        pins.lines_before = pins.lines;
140        pins.poll_lines = pins.lines;
141        pins.irq_disable = state.irq_disable;
142        pins.frame.restore_poll(&state.poll_fields());
143        Ok(())
144    }
145}