summaryrefslogtreecommitdiff
path: root/src/devices/system_device.rs
diff options
context:
space:
mode:
authorBen Bridle <bridle.benjamin@gmail.com>2024-10-28 20:25:01 +1300
committerBen Bridle <bridle.benjamin@gmail.com>2024-10-28 20:29:12 +1300
commit1a830a3d1b9d99653322d5ae49ea8165de7ed9d0 (patch)
tree798e77b6fcf2438b1c2538a67efe856a2f7cb979 /src/devices/system_device.rs
parent03c4b069e1806af256730639cefdae115b24401a (diff)
downloadbedrock-pc-1a830a3d1b9d99653322d5ae49ea8165de7ed9d0.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/devices/system_device.rs')
-rw-r--r--src/devices/system_device.rs114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/devices/system_device.rs b/src/devices/system_device.rs
new file mode 100644
index 0000000..bcffb86
--- /dev/null
+++ b/src/devices/system_device.rs
@@ -0,0 +1,114 @@
+use bedrock_core::*;
+
+
+pub struct SystemDevice {
+ pub name: ReadBuffer,
+ pub authors: ReadBuffer,
+ pub can_wake: u16,
+ pub wake_id: u8,
+ pub asleep: bool,
+}
+
+impl SystemDevice {
+ pub fn new() -> Self {
+ let pkg_version = env!("CARGO_PKG_VERSION");
+ let pkg_name = env!("CARGO_PKG_NAME");
+ let pkg_authors = env!("CARGO_PKG_AUTHORS");
+ let name_str = format!("{pkg_name}, {pkg_version}");
+ let mut authors_str = String::new();
+ for author in pkg_authors.split(":") {
+ authors_str.push_str(&format!("{author}, 2024\n"));
+ }
+ Self {
+ name: ReadBuffer::from_str(&name_str),
+ authors: ReadBuffer::from_str(&authors_str),
+ can_wake: 0,
+ wake_id: 0,
+ asleep: false,
+ }
+ }
+
+ pub fn can_wake(&self, dev_id: u8) -> bool {
+ test_bit!(self.can_wake, 0x8000 >> dev_id)
+ }
+}
+
+impl Device for SystemDevice {
+ fn read(&mut self, port: u8) -> u8 {
+ match port {
+ 0x0 => self.name.read(),
+ 0x1 => self.authors.read(),
+ 0x2 => 0x00,
+ 0x3 => 0x00,
+ 0x4 => 0x00,
+ 0x5 => 0x00,
+ 0x6 => 0b1111_1100,
+ 0x7 => 0b0000_0000, // TODO: Update when fs and stream implemented
+ 0x8 => 0x00,
+ 0x9 => 0x00,
+ 0xa => self.wake_id,
+ 0xb => 0x00,
+ 0xc => 0x00,
+ 0xd => 0x00,
+ 0xe => 0x00,
+ 0xf => 0x00,
+ _ => unreachable!(),
+ }
+ }
+
+ fn write(&mut self, port: u8, value: u8) -> Option<Signal> {
+ match port {
+ 0x0 => self.name.pointer = 0,
+ 0x1 => self.authors.pointer = 0,
+ 0x2 => (),
+ 0x3 => (),
+ 0x4 => (),
+ 0x5 => (),
+ 0x6 => (),
+ 0x7 => (),
+ 0x8 => write_h!(self.can_wake, value),
+ 0x9 => {
+ write_l!(self.can_wake, value);
+ self.asleep = true;
+ return Some(Signal::Sleep);
+ },
+ 0xa => (),
+ 0xb => return Some(Signal::Fork),
+ 0xc => (),
+ 0xd => (),
+ 0xe => (),
+ 0xf => (),
+ _ => unreachable!(),
+ };
+ return None;
+ }
+
+ fn wake(&mut self) -> bool {
+ true
+ }
+}
+
+
+pub struct ReadBuffer {
+ pub bytes: Vec<u8>,
+ pub pointer: usize,
+}
+
+impl ReadBuffer {
+ pub fn from_str(text: &str) -> Self {
+ Self {
+ bytes: text.bytes().collect(),
+ pointer: 0,
+ }
+ }
+
+ pub fn read(&mut self) -> u8 {
+ let pointer = self.pointer;
+ self.pointer += 1;
+ match self.bytes.get(pointer) {
+ Some(byte) => *byte,
+ None => 0,
+ }
+ }
+}
+