1use 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 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 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 #[inline]
75 pub fn step(&mut self) -> u32 {
76 Part::step(self)
77 }
78
79 pub fn request_reset(&mut self) {
81 self.pins.frame.request_reset();
82 }
83
84 pub fn request_non_maskable_interrupt(&mut self) {
86 self.pins.frame.request_non_maskable_interrupt();
87 }
88
89 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 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}