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        // A line that changed after the last bus access changed in the last
70        // cycle, after the poll (U21): the next boundary decides on the lines
71        // as they stood at that access. The frame keeps only whether an
72        // enabled line was asserted; a swap of sources leaves that unchanged
73        // (issue 0005).
74        let pins = &mut self.pins;
75        pins.polled_lines = if pins.frame.deferred.is_some() && pins.lines != pins.lines_at_access {
76            Some(pins.lines_at_access)
77        } else {
78            None
79        };
80        self.cycles
81    }
82
83    /// Execute one instruction, interrupt entry or reset; return its cycle
84    /// count ([`Part::step`], the frame's).
85    #[inline]
86    pub fn step(&mut self) -> u32 {
87        Part::step(self)
88    }
89
90    /// Run the start sequence at the next boundary (the frame's).
91    pub fn request_reset(&mut self) {
92        self.pins.frame.request_reset();
93    }
94
95    /// Latch an NMI edge (the frame's).
96    pub fn request_non_maskable_interrupt(&mut self) {
97        self.pins.frame.request_non_maskable_interrupt();
98    }
99
100    // -- state -----------------------------------------------------------
101
102    /// Return a snapshot of all CPU-owned state at a boundary.
103    ///
104    /// No host reads, no side effects. Memory and devices are the host's and
105    /// are not included.
106    pub fn capture_state(&self) -> CpuState {
107        let (irq1, irq2, tiq) = self.pins.irq_lines();
108        let poll = self.pins.frame.poll_fields();
109        CpuState {
110            a: self.a,
111            x: self.x,
112            y: self.y,
113            s: self.s,
114            pc: self.pc,
115            p: self.p,
116            mpr: self.mpr,
117            high_speed: self.pins.high_speed,
118            mpr_latch: self.mpr_latch,
119            irq1,
120            irq2,
121            tiq,
122            irq_disable: self.pins.irq_disable,
123            halted: poll.halted,
124            reset_pending: poll.reset_pending,
125            maskable_interrupt_pending: poll.maskable_interrupt_pending,
126            non_maskable_interrupt_pending: poll.non_maskable_interrupt_pending,
127            polled_i: poll.polled_i,
128            polled_maskable_interrupt: poll.polled_maskable_interrupt,
129            polled_non_maskable_interrupt: poll.polled_non_maskable_interrupt,
130            skipped_poll: poll.skipped_poll,
131            polled_irq_lines: self.pins.polled_lines,
132        }
133    }
134
135    /// Restore a captured CPU state without touching the host.
136    pub fn restore_state(&mut self, state: &CpuState) -> Result<(), String> {
137        state.validate()?;
138        self.a = state.a;
139        self.x = state.x;
140        self.y = state.y;
141        self.s = state.s;
142        self.pc = state.pc;
143        self.p = state.p;
144        self.mpr = state.mpr;
145        let pins = &mut self.pins;
146        pins.high_speed = state.high_speed;
147        self.mpr_latch = state.mpr_latch;
148        pins.lines =
149            (if state.irq1 { IRQ1_BIT } else { 0 }) | (if state.irq2 { IRQ2_BIT } else { 0 });
150        pins.lines |= if state.tiq { TIQ_BIT } else { 0 };
151        pins.poll_lines = pins.lines;
152        pins.polled_lines = state.polled_irq_lines;
153        pins.irq_disable = state.irq_disable;
154        pins.frame.restore_poll(&state.poll_fields());
155        // The reference's _restore_poll ends in its _confirm, which sets
156        // _lines_at_access; Frame::restore_poll cannot reach the lines.
157        pins.lines_at_access = pins.lines;
158        Ok(())
159    }
160}