diff --git a/src/devices/mod.rs b/src/devices/mod.rs new file mode 100644 index 0000000000000000000000000000000000000000..ada19f9138884470af7746f6fa424080b2601f8a --- /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 0000000000000000000000000000000000000000..c52d2c0a2f93b379e60c983925fd32e32e9b42b4 --- /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 0000000000000000000000000000000000000000..27067a802811db0482137a9ab163d1631907fb68 --- /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 327e15b47a8cb23c0229468fb541e03263ab2bc2..d7b433b2675a5bd4984c686fa7f6b68c2d9c6736 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 86a725760ebe3f191118a6f09f3bfa9e2070ca06..65f078b6c41d8ffc046f827b7143210e5266d1d3 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 ecfc382fac0603258a7c17839643ce76db8777dd..ac97c6f576d425b6509c3fab32abaccc5d1c0344 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(); - } - } -}