use std::{cell::RefCell, rc::Rc};
use sdl3::{keyboard::Scancode, EventPump};
use super::{ppi::PPI, psg::PSG, vdp::Vdp};
pub struct Ports<'a> {
vdp: Rc<RefCell<Vdp<'a>>>,
ppi: Rc<RefCell<PPI>>,
psg: PSG,
}
impl<'a> Ports<'a> {
pub fn new(vdp: Rc<RefCell<Vdp<'a>>>, ppi: Rc<RefCell<PPI>>, psg: PSG) -> Self {
Self { vdp, ppi, psg }
}
pub fn read_port(&self, address: u16) -> u8 {
let ad = (address & 0xFF) as u8;
match ad {
0xa8..=0xab => self.ppi.borrow().read_port(ad),
0xa0..=0xa2 => self.psg.read_port(ad),
0x98..=0x9b => self.vdp.borrow_mut().read_port(ad),
_ => {
log::error!("ReadPort: {:02x}\n", ad);
0
}
}
}
pub fn write_port(&mut self, address: u16, b: u8) {
let ad = (address & 0xFF) as u8;
match ad {
0xa8..=0xab => self.ppi.borrow_mut().write_port(ad, b),
0xa0..=0xa2 => self.psg.write_port(ad, b),
0x90..=0x91 => {
}
0x98..=0x9b => self.vdp.borrow_mut().write_port(ad, b),
0x00..=0x01 => {
}
_ => {
log::info!("WritePort: {:02x} -> {:02x}", ad, b);
}
}
}
pub fn check_keycodes(&mut self, event_pump: &mut EventPump) {
self.ppi.borrow_mut().check_keycodes(event_pump);
}
pub fn is_key_down(&mut self, scan_code: Scancode) -> bool {
self.ppi.borrow_mut().is_key_down(scan_code)
}
}