AsciiSerial: Use String instead of byte vec

This commit is contained in:
John 2024-07-09 01:33:08 -05:00
parent 6755d318ba
commit 6cee907f1c

View File

@ -1,6 +1,6 @@
//! A dummied out implementation of a Bus, which is filled entirely with RAM //! A dummied out implementation of a Bus, which is filled entirely with RAM
use std::{borrow::Cow, io::Result as IoResult, path::Path}; use std::{io::Result as IoResult, path::Path};
use boy::memory::io::BusIO; use boy::memory::io::BusIO;
@ -21,11 +21,7 @@ impl<T: BusIO> BusIOTools for T {
} }
fn ascii(&mut self) -> AsciiSerial<Self> { fn ascii(&mut self) -> AsciiSerial<Self> {
AsciiSerial { AsciiSerial { data: 0, buf: String::new(), bus: self }
data: 0,
buf: vec![],
bus: self,
}
} }
fn read_file(&mut self, path: impl AsRef<Path>) -> IoResult<&mut Self> { fn read_file(&mut self, path: impl AsRef<Path>) -> IoResult<&mut Self> {
@ -81,17 +77,10 @@ impl<'t, T: BusIO + AsMut<[u8]>> AsMut<[u8]> for TracingBus<'t, T> {
/// Implements a hacky serial port for extracting data /// Implements a hacky serial port for extracting data
pub struct AsciiSerial<'t, T: BusIO + ?Sized> { pub struct AsciiSerial<'t, T: BusIO + ?Sized> {
data: u8, data: u8,
buf: Vec<u8>, buf: String,
bus: &'t mut T, bus: &'t mut T,
} }
impl<'t, T: BusIO + ?Sized> AsciiSerial<'t, T> {
/// Gets the contents of the data buffer
pub fn string(&self) -> Cow<str> {
String::from_utf8_lossy(&self.buf)
}
}
impl<'t, T: BusIO + ?Sized> BusIO for AsciiSerial<'t, T> { impl<'t, T: BusIO + ?Sized> BusIO for AsciiSerial<'t, T> {
fn read(&self, addr: usize) -> Option<u8> { fn read(&self, addr: usize) -> Option<u8> {
match addr { match addr {
@ -104,13 +93,16 @@ impl<'t, T: BusIO + ?Sized> BusIO for AsciiSerial<'t, T> {
fn write(&mut self, addr: usize, data: u8) -> Option<()> { fn write(&mut self, addr: usize, data: u8) -> Option<()> {
match addr { match addr {
0xff01 => { 0xff01 => {
// eprintln!("'{data:02x}'"); self.data = data;
self.buf.push(data);
} }
0xff02 => { 0xff02 => {
if data & 0x80 != 0 { if data & 0x80 != 0 {
// eprintln!("SERIAL => {:02x}", self.data); if self.data == b'\n' {
eprintln!("tx: {data:02x}, buf: {:02x?}", self.buf); println!("dbg> \x1b[1m{}\x1b[0m", self.buf);
self.buf.clear();
} else {
self.buf.push(self.data as char);
}
let interrupt = self.bus.read(0xff0f)? | (1 << 3); let interrupt = self.bus.read(0xff0f)? | (1 << 3);
self.bus.write(0xff0f, interrupt)?; self.bus.write(0xff0f, interrupt)?;
} }
@ -121,8 +113,6 @@ impl<'t, T: BusIO + ?Sized> BusIO for AsciiSerial<'t, T> {
} }
fn diag(&mut self, param: usize) { fn diag(&mut self, param: usize) {
println!("debug: '{}'", self.string());
self.buf.clear();
self.bus.diag(param) self.bus.diag(param)
} }
} }