From ab84ad75629b0a4124221023ca91411d2cd62a32 Mon Sep 17 00:00:00 2001
From: Ben Bridle <ben@derelict.engineering>
Date: Tue, 25 Mar 2025 12:46:49 +1300
Subject: Restructure program

This commit also includes changes to devices according to the latest
devices specification, in particular the math and system devices.
---
 src/devices/system_device.rs | 125 ++++++++++++++++++++++---------------------
 1 file changed, 64 insertions(+), 61 deletions(-)

(limited to 'src/devices/system_device.rs')

diff --git a/src/devices/system_device.rs b/src/devices/system_device.rs
index 383bb08..10ddad1 100644
--- a/src/devices/system_device.rs
+++ b/src/devices/system_device.rs
@@ -1,76 +1,69 @@
-use bedrock_core::*;
+use crate::*;
 
 
 pub struct SystemDevice {
+    /// Name and version of this system.
     pub name: ReadBuffer,
+    /// Authors of this system.
     pub authors: ReadBuffer,
-    pub can_wake: u16,
-    pub wake_id: u8,
+    /// Mask of all devices waiting to wake from sleep.
+    pub wakers: u16,
+    /// Device that most recently woke the system.
+    pub waker: u8,
+    /// True if the system has been put to sleep.
     pub asleep: bool,
+    /// Mask of all available devices.
+    pub devices: u16,
+    /// Name of the first custom devices.
+    pub custom1: ReadBuffer,
+    /// Name of the second custom devices.
+    pub custom2: ReadBuffer,
+    /// Name of the third custom devices.
+    pub custom3: ReadBuffer,
+    /// Name of the fourth custom devices.
+    pub custom4: ReadBuffer,
 }
 
-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 authors_str = pkg_authors.replace(":", "\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,
+            0x0 => 0x00,
+            0x1 => 0x00,
+            0x2 => self.waker,
             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,
+            0x6 => 0x00,
+            0x7 => 0x00,
+            0x8 => self.name.read(),
+            0x9 => self.authors.read(),
+            0xa => 0x00,
             0xb => 0x00,
             0xc => 0x00,
             0xd => 0x00,
-            0xe => 0x00,
-            0xf => 0x00,
+            0xe => read_h!(self.devices),
+            0xf => read_l!(self.devices),
             _ => unreachable!(),
         }
     }
 
     fn write(&mut self, port: u8, value: u8) -> Option<Signal> {
         match port {
-            0x0 => self.name.pointer = 0,
-            0x1 => self.authors.pointer = 0,
+            0x0 =>   write_h!(self.wakers, value),
+            0x1 => { write_l!(self.wakers, value);
+                     self.asleep = true;
+                     return Some(Signal::Sleep); },
             0x2 => (),
-            0x3 => (),
+            0x3 => return Some(Signal::Fork),
             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);
-            },
+            0x8 => self.name.pointer = 0,
+            0x9 => self.authors.pointer = 0,
             0xa => (),
-            0xb => return Some(Signal::Fork),
+            0xb => (),
             0xc => (),
             0xd => (),
             0xe => (),
@@ -81,31 +74,41 @@ impl Device for SystemDevice {
     }
 
     fn wake(&mut self) -> bool {
-        true
+        false
     }
 }
 
 
-pub struct ReadBuffer {
-    pub bytes: Vec<u8>,
-    pub pointer: usize,
-}
-
-impl ReadBuffer {
-    pub fn from_str(text: &str) -> Self {
+impl SystemDevice {
+    pub fn new(devices: u16) -> Self {
         Self {
-            bytes: text.bytes().collect(),
-            pointer: 0,
+            name: get_name(),
+            authors: get_authors(),
+            wakers: 0,
+            waker: 0,
+            asleep: false,
+            devices,
+            custom1: ReadBuffer::new(),
+            custom2: ReadBuffer::new(),
+            custom3: ReadBuffer::new(),
+            custom4: ReadBuffer::new(),
         }
     }
+}
 
-    pub fn read(&mut self) -> u8 {
-        let pointer = self.pointer;
-        self.pointer += 1;
-        match self.bytes.get(pointer) {
-            Some(byte) => *byte,
-            None => 0,
-        }
-    }
+
+fn get_name() -> ReadBuffer {
+    let pkg_version = env!("CARGO_PKG_VERSION");
+    let pkg_name = env!("CARGO_PKG_NAME");
+    ReadBuffer::from_str(&format!("{pkg_name}/{pkg_version}"))
 }
 
+fn get_authors() -> ReadBuffer {
+    let pkg_authors = env!("CARGO_PKG_AUTHORS");
+    let mut authors_string = String::new();
+    for author in pkg_authors.split(':') {
+        authors_string.push_str(author);
+        authors_string.push('\n');
+    }
+    ReadBuffer::from_str(&authors_string)
+}
-- 
cgit v1.2.3-70-g09d2