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