From 93e0d10e2f78436f33dd64ba2e68bbfbe38b3604 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com>
Date: Sat, 15 Apr 2023 23:01:56 +0100
Subject: [PATCH] chore: separation of the devices structure Allowing better
 directory usage. Created initial printer file.

---
 src/devices/mod.rs     |  2 ++
 src/devices/printer.rs | 27 +++++++++++++++++++++++++++
 src/devices/stdout.rs  | 34 ++++++++++++++++++++++++++++++++++
 src/gb.rs              | 10 +++++++++-
 src/lib.rs             |  1 +
 src/serial.rs          | 37 ++-----------------------------------
 6 files changed, 75 insertions(+), 36 deletions(-)
 create mode 100644 src/devices/mod.rs
 create mode 100644 src/devices/printer.rs
 create mode 100644 src/devices/stdout.rs

diff --git a/src/devices/mod.rs b/src/devices/mod.rs
new file mode 100644
index 00000000..ada19f91
--- /dev/null
+++ b/src/devices/mod.rs
@@ -0,0 +1,2 @@
+pub mod printer;
+pub mod stdout;
diff --git a/src/devices/printer.rs b/src/devices/printer.rs
new file mode 100644
index 00000000..c52d2c0a
--- /dev/null
+++ b/src/devices/printer.rs
@@ -0,0 +1,27 @@
+use crate::serial::SerialDevice;
+
+pub struct PrinterDevice {
+}
+
+impl PrinterDevice {
+    pub fn new() -> Self {
+        Self {}
+    }
+}
+
+impl Default for PrinterDevice {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl SerialDevice for PrinterDevice {
+    fn send(&mut self) -> u8 {
+        0xff
+    }
+
+    fn receive(&mut self, byte: u8) {
+        print!("{}", byte as char);
+        // @TODO: implement this one
+    }
+}
diff --git a/src/devices/stdout.rs b/src/devices/stdout.rs
new file mode 100644
index 00000000..27067a80
--- /dev/null
+++ b/src/devices/stdout.rs
@@ -0,0 +1,34 @@
+use std::io::{stdout, Write};
+
+use crate::serial::SerialDevice;
+
+pub struct StdoutDevice {
+    flush: bool
+}
+
+impl StdoutDevice {
+    pub fn new(flush: bool) -> Self {
+        Self {
+            flush
+        }
+    }
+}
+
+impl Default for StdoutDevice {
+    fn default() -> Self {
+        Self::new(true)
+    }
+}
+
+impl SerialDevice for StdoutDevice {
+    fn send(&mut self) -> u8 {
+        0xff
+    }
+
+    fn receive(&mut self, byte: u8) {
+        print!("{}", byte as char);
+        if self.flush {
+            stdout().flush().unwrap();
+        }
+    }
+}
diff --git a/src/gb.rs b/src/gb.rs
index 327e15b4..d7b433b2 100644
--- a/src/gb.rs
+++ b/src/gb.rs
@@ -9,7 +9,7 @@ use crate::{
     rom::Cartridge,
     serial::Serial,
     timer::Timer,
-    util::read_file,
+    util::read_file, devices::{stdout::StdoutDevice, printer::PrinterDevice},
 };
 
 use std::collections::VecDeque;
@@ -356,6 +356,14 @@ impl GameBoy {
         self.clock_freq = value;
         self.apu().set_clock_freq(value);
     }
+
+    pub fn attach_stdout_serial(&mut self) {
+        self.serial().set_device(Box::<StdoutDevice>::default());
+    }
+
+    pub fn attach_printer_serial(&mut self) {
+        self.serial().set_device(Box::<PrinterDevice>::default());
+    }
 }
 
 /// Gameboy implementations that are meant with performance
diff --git a/src/lib.rs b/src/lib.rs
index 86a72576..65f078b6 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -2,6 +2,7 @@
 
 pub mod apu;
 pub mod cpu;
+pub mod devices;
 pub mod data;
 pub mod gb;
 pub mod gen;
diff --git a/src/serial.rs b/src/serial.rs
index ecfc382f..ac97c6f5 100644
--- a/src/serial.rs
+++ b/src/serial.rs
@@ -1,5 +1,3 @@
-use std::io::{stdout, Write};
-
 use crate::warnln;
 
 pub trait SerialDevice {
@@ -34,7 +32,7 @@ impl Serial {
             bit_count: 0,
             byte_receive: 0x0,
             int_serial: false,
-            device: Box::<PrintDevice>::default(),
+            device: Box::<NullDevice>::default(),
         }
     }
 
@@ -182,7 +180,7 @@ pub struct NullDevice {
 
 impl NullDevice {
     pub fn new() -> Self {
-        Self { }
+        Self {}
     }
 }
 
@@ -200,34 +198,3 @@ impl SerialDevice for NullDevice {
     fn receive(&mut self, _: u8) {
     }
 }
-
-pub struct PrintDevice {
-    flush: bool
-}
-
-impl PrintDevice {
-    pub fn new(flush: bool) -> Self {
-        Self {
-            flush
-        }
-    }
-}
-
-impl Default for PrintDevice {
-    fn default() -> Self {
-        Self::new(true)
-    }
-}
-
-impl SerialDevice for PrintDevice {
-    fn send(&mut self) -> u8 {
-        0xff
-    }
-
-    fn receive(&mut self, byte: u8) {
-        print!("{}", byte as char);
-        if self.flush {
-            stdout().flush().unwrap();
-        }
-    }
-}
-- 
GitLab