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