2021-10-06 23:14:56 +00:00
|
|
|
|
2021-10-07 16:41:01 +00:00
|
|
|
use std::rc::Rc;
|
2021-10-06 23:14:56 +00:00
|
|
|
use std::cell::{RefCell, RefMut};
|
|
|
|
|
|
|
|
use crate::error::Error;
|
2021-10-09 06:11:52 +00:00
|
|
|
use crate::interrupts::Signal;
|
2021-10-07 18:35:15 +00:00
|
|
|
use crate::memory::{Address, Addressable, Bus};
|
2021-10-09 06:11:52 +00:00
|
|
|
use crate::devices::{Device, Steppable, AddressableDeviceBox, InterruptableDeviceBox, Clock};
|
2021-10-06 23:14:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
pub struct System {
|
|
|
|
pub clock: Clock,
|
2021-10-07 18:35:15 +00:00
|
|
|
pub devices: Vec<Device>,
|
2021-10-07 16:41:01 +00:00
|
|
|
pub bus: RefCell<Bus>,
|
2021-10-06 23:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl System {
|
|
|
|
pub fn new() -> System {
|
|
|
|
System {
|
|
|
|
clock: 0,
|
2021-10-07 18:35:15 +00:00
|
|
|
devices: vec![],
|
2021-10-07 16:41:01 +00:00
|
|
|
bus: RefCell::new(Bus::new()),
|
2021-10-06 23:14:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 16:41:01 +00:00
|
|
|
pub fn get_bus(&self) -> RefMut<'_, Bus> {
|
|
|
|
self.bus.borrow_mut()
|
2021-10-06 23:14:56 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 16:41:01 +00:00
|
|
|
pub fn add_addressable_device(&mut self, addr: Address, device: AddressableDeviceBox) -> Result<(), Error> {
|
|
|
|
let length = device.borrow().len();
|
|
|
|
self.bus.borrow_mut().insert(addr, length, device.clone());
|
2021-10-07 18:35:15 +00:00
|
|
|
self.devices.push(Device::Addressable(device));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_interruptable_device(&mut self, device: InterruptableDeviceBox) -> Result<(), Error> {
|
|
|
|
//self.bus.borrow_mut().insert(addr, length, device.clone());
|
|
|
|
self.devices.push(Device::Interruptable(device));
|
2021-10-07 16:41:01 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-10-06 23:14:56 +00:00
|
|
|
|
|
|
|
pub fn step(&mut self) -> Result<(), Error> {
|
|
|
|
self.clock += 1;
|
2021-10-07 18:35:15 +00:00
|
|
|
for dev in &self.devices {
|
|
|
|
match dev {
|
|
|
|
Device::Addressable(dev) => dev.borrow_mut().step(&self),
|
|
|
|
Device::Interruptable(dev) => dev.borrow_mut().step(&self),
|
|
|
|
}?;
|
2021-10-06 23:14:56 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-10-07 18:35:15 +00:00
|
|
|
|
2021-10-08 17:52:15 +00:00
|
|
|
pub fn change_interrupt_state(&self, state: bool, priority: u8, number: u8) -> Result<(), Error> {
|
2021-10-07 18:35:15 +00:00
|
|
|
// TODO how does this find the specific device it's connected to?
|
2021-10-07 20:57:50 +00:00
|
|
|
|
|
|
|
// TODO for the time being, this will find the first device to handle it or fail
|
2021-10-08 17:52:15 +00:00
|
|
|
println!("system: interrupt state changed to {} ({})", state, priority);
|
2021-10-07 20:57:50 +00:00
|
|
|
for dev in &self.devices {
|
|
|
|
match dev {
|
|
|
|
Device::Interruptable(dev) => {
|
2021-10-08 17:52:15 +00:00
|
|
|
return dev.borrow_mut().interrupt_state_change(&self, state, priority, number);
|
2021-10-07 20:57:50 +00:00
|
|
|
},
|
|
|
|
_ => { },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Err(Error::new(&format!("unhandled interrupt: {:x}", number)));
|
2021-10-07 18:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exit_error(&mut self) {
|
|
|
|
for dev in &self.devices {
|
|
|
|
match dev {
|
|
|
|
Device::Addressable(dev) => dev.borrow_mut().on_error(&self),
|
|
|
|
Device::Interruptable(dev) => dev.borrow_mut().on_error(&self),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|