Change small heuristics, add newlines between items

This commit is contained in:
2024-07-09 01:22:32 -05:00
parent 9106d9727f
commit 060a6b068b
9 changed files with 178 additions and 41 deletions

View File

@@ -7,7 +7,9 @@ use boy::memory::io::BusIO;
pub trait BusIOTools: BusIO {
/// Prints all successful reads and writes
fn trace(&mut self) -> TracingBus<Self>;
fn ascii(&mut self) -> AsciiSerial<Self>;
fn read_file(&mut self, path: impl AsRef<Path>) -> IoResult<&mut Self>
where
Self: Sized;
@@ -17,6 +19,7 @@ impl<T: BusIO> BusIOTools for T {
fn trace(&mut self) -> TracingBus<Self> {
TracingBus { bus: self }
}
fn ascii(&mut self) -> AsciiSerial<Self> {
AsciiSerial {
data: 0,
@@ -24,6 +27,7 @@ impl<T: BusIO> BusIOTools for T {
bus: self,
}
}
fn read_file(&mut self, path: impl AsRef<Path>) -> IoResult<&mut Self> {
let data = std::fs::read(path)?;
eprintln!("Read {} bytes.", data.len());
@@ -49,6 +53,7 @@ impl<'t, T: BusIO> BusIO for TracingBus<'t, T> {
// }
self.bus.read(addr)
}
fn write(&mut self, addr: usize, data: u8) -> Option<()> {
eprintln!("set [{addr:04x}], {data:02x}");
self.bus.write(addr, data)
@@ -60,11 +65,13 @@ impl<'t, T: BusIO> From<&'t mut T> for TracingBus<'t, T> {
Self { bus: value }
}
}
impl<'t, T: BusIO + AsRef<[u8]>> AsRef<[u8]> for TracingBus<'t, T> {
fn as_ref(&self) -> &[u8] {
self.bus.as_ref()
}
}
impl<'t, T: BusIO + AsMut<[u8]>> AsMut<[u8]> for TracingBus<'t, T> {
fn as_mut(&mut self) -> &mut [u8] {
self.bus.as_mut()
@@ -93,6 +100,7 @@ impl<'t, T: BusIO + ?Sized> BusIO for AsciiSerial<'t, T> {
_ => self.bus.read(addr),
}
}
fn write(&mut self, addr: usize, data: u8) -> Option<()> {
match addr {
0xff01 => {
@@ -111,6 +119,7 @@ impl<'t, T: BusIO + ?Sized> BusIO for AsciiSerial<'t, T> {
}
self.bus.write(addr, data)
}
fn diag(&mut self, param: usize) {
println!("debug: '{}'", self.string());
self.buf.clear();

View File

@@ -6,6 +6,7 @@ use std::iter::{Enumerate, Peekable};
pub trait Disassemble: Iterator<Item = u8> + Sized {
fn disassemble(self) -> Disassembler<Self>;
}
impl<T: Iterator<Item = u8> + Sized> Disassemble for T {
fn disassemble(self) -> Disassembler<Self> {
Disassembler::new(self)
@@ -29,27 +30,31 @@ impl<I: Iterator<Item = u8>> Iterator for Disassembler<I> {
impl<I: Iterator<Item = u8>> Disassembler<I> {
pub fn new(bytes: I) -> Self {
Disassembler {
bytes: bytes.enumerate().peekable(),
}
Disassembler { bytes: bytes.enumerate().peekable() }
}
pub fn index(&mut self) -> Option<usize> {
self.bytes.peek().map(|v| v.0)
}
pub fn imm8(&mut self) -> Option<u8> {
self.bytes.next().map(|v| v.1)
}
pub fn smm8(&mut self) -> Option<i8> {
self.imm8().map(|b| b as i8)
}
pub fn imm16(&mut self) -> Option<u16> {
let low = self.imm8()? as u16;
let high = self.imm8()? as u16;
Some(high << 8 | low)
}
pub fn smm16(&mut self) -> Option<i16> {
self.imm16().map(|w| w as i16)
}
pub fn print(&mut self, insn: Insn) -> Option<String> {
Some(match insn {
Insn::LdImm(reg) => format!("ld\t{reg}, {:02x}", self.imm8()?),
@@ -79,6 +84,7 @@ impl<I: Iterator<Item = u8>> Disassembler<I> {
_ => format!("{insn}"),
})
}
pub fn print_prefix_cb(&mut self) -> Option<String> {
let prefixed: Prefixed = self.imm8()?.into();
Some(format!("{prefixed}"))

View File

@@ -77,6 +77,7 @@ pub mod cli {
/// `=`: Equals Sign
Eq,
}
impl Display for Op {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
@@ -180,16 +181,20 @@ pub mod cli {
}
self
}
fn peek(&mut self) -> Option<&char> {
self.text.peek()
}
fn take(&mut self) -> &mut Self {
self.text.next();
self
}
fn emit(&mut self, kind: Token) -> Option<Token> {
Some(kind)
}
fn digits<const B: u32>(&mut self) -> Option<Token> {
let mut out = 0;
while let Some(Some(next)) = self.peek().map(|c| c.to_digit(B)) {
@@ -198,6 +203,7 @@ pub mod cli {
}
Some(Num(out))
}
fn base(&mut self) -> Option<Token> {
match self.peek() {
Some('b') => self.take().digits::<2>(),
@@ -207,6 +213,7 @@ pub mod cli {
_ => self.digits::<16>(),
}
}
fn ident(&mut self) -> Option<Token> {
let mut out = String::new();
while let Some(&c) = self.peek().filter(|&&c| c.is_alphanumeric() || c == '_') {
@@ -239,6 +246,7 @@ pub mod cli {
},
})
}
/// Checks the following character, producing the provided [Token]
/// if it matches the expected [char]. Otherwise, prodices [Token::Other].
fn chain(&mut self, expect: char, becomes: Token) -> Option<Token> {
@@ -255,7 +263,7 @@ pub mod cli {
}
.emit(Token::Op(Range))
}
fn string(&mut self) -> Option<Token> {
let mut value = String::new();
while let Some(c) = self.text.next_if(|&c| c != '"') {
@@ -264,8 +272,10 @@ pub mod cli {
self.take().emit(Token::Ident(value))
}
}
impl<I: Iterator<Item = char>> Iterator for Lexer<I> {
type Item = Token;
fn next(&mut self) -> Option<Self::Item> {
match self.sync().peek()? {
'[' => self.take().emit(Op(BrackOpen)),
@@ -310,6 +320,7 @@ pub mod cli {
pub trait Parsible: Iterator<Item = Token> + Sized {
fn parse(self) -> Parser<Self>;
}
impl<T: Iterator<Item = Token> + Sized> Parsible for T {
fn parse(self) -> Parser<Self> {
Parser::new(self)
@@ -330,9 +341,11 @@ pub mod cli {
fn peek(&mut self) -> Option<&Token> {
self.lexer.peek()
}
fn take(&mut self) -> Option<Token> {
self.lexer.next()
}
#[must_use]
fn then(&mut self, tok: Token) -> Option<&mut Self> {
(*self.peek()? == tok).then(|| {
@@ -472,6 +485,7 @@ pub mod cli {
};
continue;
}
if let Some(prec) = op.infix() {
if prec.before() < level {
break;
@@ -501,13 +515,16 @@ pub mod cli {
Term,
Sign,
}
impl Level {
fn level(self) -> u8 {
(self as u8) << 1
}
pub fn before(self) -> u8 {
self.level()
}
pub fn after(self) -> u8 {
self.level() + 1
}