pcengine_rust/cpu_io/
mod.rs

1//! The HuC6280's on-die peripherals that are not the CPU: timer, $1400 interrupt
2//! controller, I/O port and the I/O buffer (`cpu_io/__init__.py`; Aubrey's rule: in the board,
3//! behind a module boundary).
4//!
5//! Nothing here uses the rest of the crate except the `sockets` traits, so the module can move
6//! into a core or a repository of its own if a second consumer appears (a Data East arcade
7//! board's HuC6280). Never copied.
8//!
9//! `CpuIo` answers the hardware-page offsets $0800-$17FF: the PSG range is forwarded to an
10//! object the board supplies, so this module does not use the PSG.
11//!
12//! Differences forced by Rust: the PSG and the CD-sense level are arguments of each access
13//! (the reference holds a reference and a callable); a change of the disable mask is left in
14//! `IrqController::mask_changed` for the board to hand the CPU when the access returns (the
15//! reference calls back from inside the write; nothing touches the CPU in between).
16
17pub mod io_buffer;
18pub mod io_port;
19pub mod irq_ctl;
20pub mod timer;
21
22pub use io_buffer::IoBuffer;
23pub use io_port::IoPort;
24pub use irq_ctl::IrqController;
25pub use timer::Timer;
26
27use crate::sockets::ControllerPort;
28
29/// What CpuIo needs from the sound generator: its register writes.
30pub trait PsgPins {
31    fn write(&mut self, offset: u32, value: u8, mclk: i64);
32}
33
34pub struct CpuIo {
35    pub buffer: IoBuffer,
36    pub timer: Timer,
37    pub irq: IrqController,
38    pub port: IoPort,
39}
40
41impl CpuIo {
42    pub fn new(country_pc_engine: bool, pad: Option<Box<dyn ControllerPort>>) -> CpuIo {
43        CpuIo {
44            buffer: IoBuffer::new(0),
45            timer: Timer::new(),
46            irq: IrqController::new(),
47            port: IoPort::new(country_pc_engine, pad),
48        }
49    }
50
51    pub fn reset(&mut self) {
52        self.buffer.reset();
53        self.timer.reset();
54        self.irq.reset();
55        self.port.reset();
56    }
57
58    pub fn catch_up(&mut self, mclk: i64) {
59        self.timer.catch_up(mclk);
60    }
61
62    pub fn next_event(&self) -> Option<i64> {
63        self.timer.next_event()
64    }
65
66    pub fn tiq(&self) -> bool {
67        self.timer.irq
68    }
69
70    /// `offset` is the hardware-page offset, $0800-$17FF. Loads the buffer, as pcetech 3.
71    pub fn read(&mut self, offset: u32, mclk: i64, cd_attached: bool) -> u8 {
72        let offset = offset & 0x1FFF;
73        if offset < 0x0C00 {
74            // PSG: every address returns the buffer in full
75            return self.buffer.value;
76        }
77        let value = if offset < 0x1000 {
78            self.timer.read(self.buffer.value, mclk)
79        } else if offset < 0x1400 {
80            self.port.read(cd_attached)
81        } else {
82            self.timer.catch_up(mclk);
83            self.irq.read(offset, self.buffer.value, self.timer.irq)
84        };
85        self.buffer.value = value;
86        value
87    }
88
89    pub fn write(&mut self, offset: u32, value: u8, mclk: i64, psg: &mut impl PsgPins) {
90        let offset = offset & 0x1FFF;
91        self.buffer.value = value;
92        if offset < 0x0C00 {
93            psg.write(offset & 0x0F, value, mclk);
94        } else if offset < 0x1000 {
95            self.timer.write(offset, value, mclk);
96        } else if offset < 0x1400 {
97            self.port.write(value);
98        } else {
99            self.timer.catch_up(mclk);
100            if self.irq.write(offset, value) {
101                self.timer.acknowledge();
102            }
103        }
104    }
105
106    /// A read with no side effects, for the debugger and the lockstep probe.
107    pub fn peek(&mut self, offset: u32, cd_attached: bool) -> u8 {
108        let offset = offset & 0x1FFF;
109        let buf = self.buffer.value;
110        if offset < 0x0C00 {
111            return buf;
112        }
113        if offset < 0x1000 {
114            return self.timer.count | (buf & 0x80);
115        }
116        if offset < 0x1400 {
117            return self.port.read(cd_attached);
118        }
119        self.irq.read(offset, buf, self.timer.irq)
120    }
121}