pcengine_rust/system.rs
1//! The machine as a whole: a board, a CPU on it, and frame-by-frame running (`system.py`).
2//!
3//! The CPU is huc6280-rust's `HuC6280` with the board as its bus: one `step()` runs one
4//! instruction, one interrupt entry or one whole block transfer. The board prices every cycle
5//! inside its callbacks, so `step()`'s return value is never added to the clock.
6//!
7//! Differences forced by Rust: the CPU owns the board (`system.cpu.bus`, also
8//! `system.board()`), where the reference's `System` holds both side by side; `System::new` is
9//! the reference's `build` (`make_core`, then `Board.attach_cpu`). A CPU with other switches is
10//! the same type with its public fields set (`system.cpu.irq_poll_cycle = ...`), not a subclass.
11
12use huc6280::HuC6280;
13
14use crate::board::Board;
15
16pub struct System {
17 pub cpu: HuC6280<Board>,
18}
19
20impl System {
21 /// The HuC6280 core on this board's bus, with a reset requested: the first `step()` runs
22 /// the start sequence and reads the reset vector.
23 pub fn new(board: Board) -> System {
24 let mut cpu = HuC6280::new(board);
25 cpu.request_reset();
26 // Board.attach_cpu
27 let disable = cpu.bus.cpu_io.irq.disable;
28 cpu.pins.set_irq_disable(disable);
29 cpu.bus.deliver_lines(Some(&mut cpu.pins), true);
30 System { cpu }
31 }
32
33 #[inline]
34 pub fn board(&self) -> &Board {
35 &self.cpu.bus
36 }
37
38 #[inline]
39 pub fn board_mut(&mut self) -> &mut Board {
40 // the fork: whatever the caller changes, the event horizon is recomputed
41 self.cpu.bus.touch();
42 &mut self.cpu.bus
43 }
44
45 #[inline]
46 pub fn frame(&self) -> i64 {
47 self.cpu.bus.timeline.frame
48 }
49
50 pub fn step(&mut self) {
51 self.cpu.bus.touch();
52 self.cpu.step();
53 }
54
55 /// Run until the VCE starts the next frame.
56 pub fn run_frame(&mut self) {
57 self.cpu.bus.touch();
58 let start = self.frame();
59 while self.frame() == start {
60 self.cpu.step();
61 }
62 }
63
64 /// Run whole instructions until the master clock has reached `mclk`.
65 pub fn run_until(&mut self, mclk: i64) {
66 self.cpu.bus.touch();
67 while self.cpu.bus.mclk < mclk {
68 self.cpu.step();
69 }
70 }
71}