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        &mut self.cpu.bus
41    }
42
43    #[inline]
44    pub fn frame(&self) -> i64 {
45        self.cpu.bus.timeline.frame
46    }
47
48    pub fn step(&mut self) {
49        self.cpu.step();
50    }
51
52    /// Run until the VCE starts the next frame.
53    pub fn run_frame(&mut self) {
54        let start = self.frame();
55        while self.frame() == start {
56            self.cpu.step();
57        }
58    }
59
60    /// Run whole instructions until the master clock has reached `mclk`.
61    pub fn run_until(&mut self, mclk: i64) {
62        while self.cpu.bus.mclk < mclk {
63            self.cpu.step();
64        }
65    }
66}