1use alloc::{boxed::Box, string::String, vec::Vec};
2pub mod system;
3pub mod console;
4
5pub use system::{SystemDevice, SYSTEM_ID};
6pub use console::{Console, BufferConsole, CONSOLE_ID, SharedBuf};
7use polka::Value;
8use crate::memory::Heap;
9
10pub trait Device {
11 fn read(&mut self, port: u8) -> Result<(Value, bool), String>;
16
17 fn write(&mut self, port: u8, val: Value, is_handle: bool, heap: &mut Heap)
24 -> Result<(), String>;
25
26 fn write_bytes(&mut self, port: u8, bytes: &[u8], heap: &mut Heap) -> Result<(), String> {
30 for &b in bytes {
31 self.write(port, Value::from_int(b as i64), false, heap)?;
32 }
33 Ok(())
34 }
35}
36
37pub struct DeviceTable {
38 slots: Vec<Option<Box<dyn Device>>>,
39}
40
41impl DeviceTable {
42 pub fn new() -> Self {
43 let mut slots: Vec<Option<Box<dyn Device>>> = Vec::with_capacity(256);
44 for _ in 0..256 { slots.push(None); }
45 Self { slots }
46 }
47
48 pub fn install(&mut self, id: u8, dev: Box<dyn Device>) {
49 self.slots[id as usize] = Some(dev);
50 }
51
52 pub fn get_mut(&mut self, id: u8) -> Option<&mut Box<dyn Device>> {
53 self.slots[id as usize].as_mut()
54 }
55
56 pub fn take(&mut self, id: u8) -> Option<Box<dyn Device>> {
57 self.slots[id as usize].take()
58 }
59}