huc6280/
state.rs

1// SPDX-License-Identifier: MIT
2//! Values for capturing and restoring HuC6280 processor state (`state.py`).
3//!
4//! Differences forced by Rust: the field widths are the types', so of the
5//! reference's constructor checks only three remain for
6//! [`CpuState::validate`]: `irq_disable` and `polled_irq_lines` are three
7//! bits and B is not stored.
8
9use sixfiveohtwo::frame::PollFields;
10
11/// Complete CPU-owned state at an instruction boundary.
12///
13/// Registers, the mapping registers, the clock switch, the three maskable
14/// interrupt inputs and their disable gate, and the frame's poll state (the
15/// requests the host has made and the core has not yet taken, and what
16/// decides the next `step()`). Host memory, devices, scheduling and cycle
17/// counters are excluded: restoring this value restores the processor, not a
18/// machine. The fields from `halted` to `skipped_poll` are 6502-rust's
19/// frame's, as in its `CpuState`; `polled_irq_lines` is this core's.
20#[derive(Clone, Copy, Debug, PartialEq, Eq)]
21pub struct CpuState {
22    pub a: u8,
23    pub x: u8,
24    pub y: u8,
25    pub s: u8,
26    /// Logical, 16 bits.
27    pub pc: u16,
28    /// P as the core keeps it: T is bit 5, B (bit 4) is never stored (U4, U5).
29    pub p: u8,
30    /// MPR0-MPR7.
31    pub mpr: [u8; 8],
32    pub high_speed: bool,
33    /// The last value TAM or TMA moved (what TMA #$00 returns; U11).
34    pub mpr_latch: u8,
35    /// The IRQ1, IRQ2 and TIQ input levels and the disable register (IRQ2,
36    /// IRQ1, TIQ; 1 = masked).
37    pub irq1: bool,
38    pub irq2: bool,
39    pub tiq: bool,
40    pub irq_disable: u8,
41    /// Always false: the HuC6280 has no JAM, WAI or STP.
42    pub halted: bool,
43    pub reset_pending: bool,
44    /// An enabled maskable source is asserted.
45    pub maskable_interrupt_pending: bool,
46    /// An NMI edge latched and not yet taken.
47    pub non_maskable_interrupt_pending: bool,
48    /// The I flag the last instruction's poll saw, when that differs in
49    /// timing from P (CLI, SEI, PLP); `None` otherwise.
50    pub polled_i: Option<bool>,
51    /// The IRQ line and the NMI latch as the last poll saw them, when that
52    /// is not what the requests above say; `None` when the poll agrees with
53    /// them.
54    pub polled_maskable_interrupt: Option<bool>,
55    pub polled_non_maskable_interrupt: Option<bool>,
56    /// The last step was BRK, an interrupt entry or reset, which do not
57    /// poll: the next boundary runs an instruction.
58    pub skipped_poll: bool,
59    /// The maskable lines the last instruction's poll saw (IRQ2, IRQ1, TIQ in
60    /// bits 0-2, the `$1403` layout), when a line changed after the poll, in
61    /// the instruction's last cycle; `None` when the poll saw `irq1`, `irq2`
62    /// and `tiq` as they stand. It picks the vector and decides a vanished
63    /// interrupt (C18). The lines hold until the next boundary only: the
64    /// boundary clears them (the reference's clean rewrite, 6ce348e), so a
65    /// capture right after an interrupt entry reports `None`. Added in issue
66    /// 0005; `None`, the default, is how a state from before it loads.
67    pub polled_irq_lines: Option<u8>,
68}
69
70impl Default for CpuState {
71    fn default() -> Self {
72        CpuState {
73            a: 0,
74            x: 0,
75            y: 0,
76            s: 0xFF,
77            pc: 0,
78            p: 0x04,
79            mpr: [0; 8],
80            high_speed: false,
81            mpr_latch: 0,
82            irq1: false,
83            irq2: false,
84            tiq: false,
85            irq_disable: 0,
86            halted: false,
87            reset_pending: false,
88            maskable_interrupt_pending: false,
89            non_maskable_interrupt_pending: false,
90            polled_i: None,
91            polled_maskable_interrupt: None,
92            polled_non_maskable_interrupt: None,
93            skipped_poll: false,
94            polled_irq_lines: None,
95        }
96    }
97}
98
99impl CpuState {
100    /// The frame's fields of this state.
101    pub fn poll_fields(&self) -> PollFields {
102        PollFields {
103            halted: self.halted,
104            reset_pending: self.reset_pending,
105            maskable_interrupt_pending: self.maskable_interrupt_pending,
106            non_maskable_interrupt_pending: self.non_maskable_interrupt_pending,
107            polled_i: self.polled_i,
108            polled_maskable_interrupt: self.polled_maskable_interrupt,
109            polled_non_maskable_interrupt: self.polled_non_maskable_interrupt,
110            skipped_poll: self.skipped_poll,
111        }
112    }
113
114    /// Check what the reference's constructor enforces beyond the field
115    /// widths: `irq_disable` in 0..=7, `polled_irq_lines` `None` or in
116    /// 0..=7, and B (bit 4 of P) clear.
117    pub fn validate(&self) -> Result<(), String> {
118        if self.irq_disable > 7 {
119            return Err("irq_disable must be an integer in range 0x00..0x07".into());
120        }
121        if matches!(self.polled_irq_lines, Some(lines) if lines > 7) {
122            return Err("polled_irq_lines must be an integer in range 0x00..0x07".into());
123        }
124        if self.p & 0x10 != 0 {
125            return Err("p must have B (bit 4) clear: B is not stored".into());
126        }
127        Ok(())
128    }
129}