use alloc::{boxed::Box, string::String, vec::Vec};
pub mod system;
pub mod console;
pub use system::{SystemDevice, SYSTEM_ID};
pub use console::{Console, BufferConsole, CONSOLE_ID, SharedBuf};
use polka::Value;
use crate::memory::Heap;
pub trait Device {
fn read(&mut self, port: u8) -> Result<(Value, bool), String>;
fn write(&mut self, port: u8, val: Value, is_handle: bool, heap: &mut Heap)
-> Result<(), String>;
fn write_bytes(&mut self, port: u8, bytes: &[u8], heap: &mut Heap) -> Result<(), String> {
for &b in bytes {
self.write(port, Value::from_int(b as i64), false, heap)?;
}
Ok(())
}
}
pub struct DeviceTable {
slots: Vec<Option<Box<dyn Device>>>,
}
impl DeviceTable {
pub fn new() -> Self {
let mut slots: Vec<Option<Box<dyn Device>>> = Vec::with_capacity(256);
for _ in 0..256 { slots.push(None); }
Self { slots }
}
pub fn install(&mut self, id: u8, dev: Box<dyn Device>) {
self.slots[id as usize] = Some(dev);
}
pub fn get_mut(&mut self, id: u8) -> Option<&mut Box<dyn Device>> {
self.slots[id as usize].as_mut()
}
pub fn take(&mut self, id: u8) -> Option<Box<dyn Device>> {
self.slots[id as usize].take()
}
}