diff options
author | Ben Bridle <bridle.benjamin@gmail.com> | 2024-10-28 20:25:01 +1300 |
---|---|---|
committer | Ben Bridle <bridle.benjamin@gmail.com> | 2024-10-28 20:29:12 +1300 |
commit | 1a830a3d1b9d99653322d5ae49ea8165de7ed9d0 (patch) | |
tree | 798e77b6fcf2438b1c2538a67efe856a2f7cb979 /src/emulators/headless_emulator.rs | |
parent | 03c4b069e1806af256730639cefdae115b24401a (diff) | |
download | bedrock-pc-1.0.0-alpha1.zip |
Rewrite emulatorv1.0.0-alpha1
This is a complete rewrite and restructure of the entire emulator
project, as part of the effort in locking down the Bedrock specification
and in creating much better tooling for creating and using Bedrock
programs.
This commit adds a command-line argument scheme, an embedded assembler,
a headless emulator for use in non-graphical environments, deferred
window creation for programs that do not access the screen device,
and new versions of phosphor and bedrock-core. The new version of
phosphor supports multi-window programs, which will make it possible to
implement program forking in the system device later on, and the new
version of bedrock-core implements the final core specification.
Diffstat (limited to 'src/emulators/headless_emulator.rs')
-rw-r--r-- | src/emulators/headless_emulator.rs | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/src/emulators/headless_emulator.rs b/src/emulators/headless_emulator.rs new file mode 100644 index 0000000..418ea9c --- /dev/null +++ b/src/emulators/headless_emulator.rs @@ -0,0 +1,124 @@ +use crate::*; +use bedrock_core::*; + + +pub struct HeadlessDeviceBus { + pub sys: SystemDevice, + pub mem: MemoryDevice, + pub mat: MathDevice, + pub clk: ClockDevice, + pub loc: LocalDevice, + pub rem: RemoteDevice, + pub fs1: FileDevice, + pub fs2: FileDevice, +} + +impl HeadlessDeviceBus { + pub fn new(config: &EmulatorConfig) -> Self { + Self { + sys: SystemDevice::new(), + mem: MemoryDevice::new(), + mat: MathDevice::new(), + clk: ClockDevice::new(), + loc: LocalDevice::new(config), + rem: RemoteDevice::new(), + fs1: FileDevice::new(), + fs2: FileDevice::new(), + } + } +} + +impl DeviceBus for HeadlessDeviceBus { + fn read(&mut self, port: u8) -> u8 { + match port & 0xf0 { + 0x00 => self.sys.read(port & 0x0f), + 0x10 => self.mem.read(port & 0x0f), + 0x20 => self.mat.read(port & 0x0f), + 0x30 => self.clk.read(port & 0x0f), + 0x80 => self.loc.read(port & 0x0f), + 0x90 => self.rem.read(port & 0x0f), + 0xa0 => self.fs1.read(port & 0x0f), + 0xb0 => self.fs2.read(port & 0x0f), + _ => 0 + } + } + + fn write(&mut self, port: u8, value: u8) -> Option<Signal> { + match port & 0xf0 { + 0x00 => self.sys.write(port & 0x0f, value), + 0x10 => self.mem.write(port & 0x0f, value), + 0x20 => self.mat.write(port & 0x0f, value), + 0x30 => self.clk.write(port & 0x0f, value), + 0x80 => self.loc.write(port & 0x0f, value), + 0x90 => self.rem.write(port & 0x0f, value), + 0xa0 => self.fs1.write(port & 0x0f, value), + 0xb0 => self.fs2.write(port & 0x0f, value), + _ => None + } + } + + fn wake(&mut self) -> bool { + macro_rules! rouse { + ($id:expr, $dev:ident) => { + if self.sys.can_wake($id) && self.$dev.wake() { + self.sys.wake_id = $id; + self.sys.asleep = false; + return true; + } + }; + } + rouse!(0xb, fs2); + rouse!(0xa, fs1); + rouse!(0x9, rem); + rouse!(0x8, loc); + rouse!(0x3, clk); + rouse!(0x2, mat); + rouse!(0x1, mem); + rouse!(0x0, sys); + return false; + } +} + + +pub struct HeadlessEmulator { + pub br: BedrockEmulator<HeadlessDeviceBus>, + pub debug: DebugState, +} + +impl HeadlessEmulator { + pub fn new(config: &EmulatorConfig, debug: bool) -> Self { + Self { + br: BedrockEmulator::new(HeadlessDeviceBus::new(config)), + debug: DebugState::new(debug), + } + } + + pub fn load_program(&mut self, bytecode: &[u8]) { + self.br.core.mem.load_program(bytecode); + } + + pub fn run(&mut self, debug: bool) -> EmulatorSignal { + loop { + match self.br.evaluate(BATCH_SIZE, self.debug.enabled) { + Some(Signal::Fork) => { + self.br.core.mem.pc = 0; + self.br.core.wst.sp = 0; + self.br.core.rst.sp = 0; + } + Some(Signal::Sleep) => loop { + if self.br.dev.wake() { break; } + std::thread::sleep(MIN_TICK_DURATION); + } + Some(Signal::Halt) => { + self.debug.print("Program halted, exiting."); + self.debug.debug_summary(&self.br.core); + return EmulatorSignal::Halt; + } + Some(Signal::Debug1) => if debug { + self.debug.debug_summary(&self.br.core); + } + _ => (), + } + } + } +} |