pcengine_rust/cpu_io/
mod.rs1pub 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
29pub 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 pub fn read(&mut self, offset: u32, mclk: i64, cd_attached: bool) -> u8 {
72 let offset = offset & 0x1FFF;
73 if offset < 0x0C00 {
74 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 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}