Skip to content
Snippets Groups Projects
Verified Commit 93e0d10e authored by João Magalhães's avatar João Magalhães :rocket:
Browse files

chore: separation of the devices structure

Allowing better directory usage.
Created initial printer file.
parent a76524f2
No related branches found
No related tags found
1 merge request!23Support for serial data transfer 🔌
Pipeline #2500 failed
pub mod printer;
pub mod stdout;
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
}
}
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();
}
}
}
......@@ -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
......
......@@ -2,6 +2,7 @@
pub mod apu;
pub mod cpu;
pub mod devices;
pub mod data;
pub mod gb;
pub mod gen;
......
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();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment