use std::cell::RefCell;
use std::rc::Rc;
use super::Z80;
pub trait InputDevice {
fn input(&self) -> u8;
}
pub trait OutputDevice {
fn output(&self, val: u8);
}
impl Z80 {
pub fn install_input(&mut self, index: u8, device: Box<InputDevice>) {
self.input_devices.insert(index, device);
}
pub fn install_output(&mut self, index: u8, device: Box<OutputDevice>) {
self.output_devices.insert(index, device);
}
}
#[derive(Default, PartialEq, Clone)]
pub struct BufInput {
input: Rc<RefCell<Vec<u8>>>,
}
impl InputDevice for BufInput {
fn input(&self) -> u8 {
self.input.borrow_mut().pop().unwrap()
}
}
impl BufInput {
pub fn new(v: Vec<u8>) -> Self {
Self {
input: Rc::new(RefCell::new(v)),
}
}
}
#[derive(Default, PartialEq, Clone)]
pub struct BufOutput {
output: Rc<RefCell<Vec<u8>>>,
}
impl BufOutput {
pub fn result(&self) -> Vec<u8> {
self.output.borrow_mut().to_vec()
}
}
impl OutputDevice for BufOutput {
fn output(&self, val: u8) {
self.output.borrow_mut().push(val)
}
}