Skip to content

Commit

Permalink
Put Arc<Mutext<Gameboy>> in both GUI and Debugger
Browse files Browse the repository at this point in the history
but this isn't the way we agreed to share the Gameboy
Look at the UI, though!
  • Loading branch information
diogotito committed Mar 19, 2024
1 parent 4717e5e commit 84df887
Show file tree
Hide file tree
Showing 2 changed files with 274 additions and 143 deletions.
95 changes: 53 additions & 42 deletions fpt-cli/src/debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt;
use std::fs::File;
use std::io::Write;
use std::rc::Rc;
use std::sync::{Arc, Mutex, MutexGuard};

use fpt::Gameboy;
use hlua::AnyHashableLuaValue as LuaValue;
Expand Down Expand Up @@ -42,14 +43,14 @@ fn alias_expand(cmd: String, dti: &mut DebuggerTextInterface) -> String {
}

#[derive(Debug)]
enum Breakpoint {
pub enum Breakpoint {
OnPc(u16),
OnOpcode(u8),
OnCB(u8),
}

impl fmt::Display for Breakpoint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Breakpoint::OnPc(pc) => {
write!(f, "breakpoint: {}", pc)
Expand All @@ -75,68 +76,69 @@ impl Breakpoint {
}
}
pub struct Debugger {
gameboy: Gameboy,
gameboy: Arc<Mutex<Gameboy>>,
breakpoints: Vec<Breakpoint>,
}

impl Debugger {
fn new() -> Debugger {
let gameboy = Gameboy::new();
fn new() -> Self {
let gameboy = Arc::new(Mutex::new(Gameboy::new()));
Self::with_gameboy(gameboy)
}

Debugger {
pub fn with_gameboy(gameboy: Arc<Mutex<Gameboy>>) -> Self {
Self {
gameboy,
breakpoints: Vec::new(),
}
}

fn check(&self) -> bool {
for breakpoint in self.breakpoints.iter() {
if breakpoint.check(&self.gameboy) {
pub fn gameboy(&self) -> MutexGuard<'_, Gameboy> {
self.gameboy.lock().unwrap()
}

pub fn check(&self) -> bool {
for breakpoint in &self.breakpoints {
if breakpoint.check(&self.gameboy()) {
return true;
}
}

false
}

fn start(&mut self) {
pub fn start(&mut self) {
let mut gameboy = self.gameboy();
loop {
println!(
"{:#02X}: {}",
self.gameboy.cpu().pc(),
self.gameboy.cpu().decode()
);
println!("{:#02X}: {}", gameboy.cpu().pc(), gameboy.cpu().decode());
if self.check() {
self.gameboy.instruction();
gameboy.instruction();
break;
}
self.gameboy.instruction();
gameboy.instruction();
}
}

fn next(&mut self) {
println!(
"{:#02X}: {}",
self.gameboy.cpu().pc(),
self.gameboy.cpu().decode()
);
self.gameboy.instruction();
pub fn next(&mut self) {
let mut gameboy = self.gameboy();
println!("{:#02X}: {}", gameboy.cpu().pc(), gameboy.cpu().decode());
gameboy.instruction();
}

fn set_breakpoint(&mut self, breakpoint: Breakpoint) {
pub fn set_breakpoint(&mut self, breakpoint: Breakpoint) {
self.breakpoints.push(breakpoint);
}

fn list_breakpoints(&self) -> String {
pub fn list_breakpoints(&self) -> String {
self.breakpoints
.iter()
.map(|breakpoint| breakpoint.to_string())
.intersperse("\n".to_string())
.collect::<String>()
}

fn pc(&mut self) -> u16 {
self.gameboy.cpu().pc()
pub fn pc(&mut self) -> u16 {
self.gameboy().cpu().pc()
}
}

Expand Down Expand Up @@ -264,63 +266,63 @@ impl DebuggerTextInterface<'_> {
debug_commands.set(
"af",
hlua::function0(move || -> LuaValue {
LuaValue::LuaNumber(d1.borrow_mut().gameboy.cpu().af().into())
LuaValue::LuaNumber(d1.borrow_mut().gameboy().cpu().af().into())
}),
);

let d1 = dbg_pointer.clone();
debug_commands.set(
"bc",
hlua::function0(move || -> LuaValue {
LuaValue::LuaNumber(d1.borrow_mut().gameboy.cpu().bc().into())
LuaValue::LuaNumber(d1.borrow_mut().gameboy().cpu().bc().into())
}),
);

let d1 = dbg_pointer.clone();
debug_commands.set(
"de",
hlua::function0(move || -> LuaValue {
LuaValue::LuaNumber(d1.borrow_mut().gameboy.cpu().de().into())
LuaValue::LuaNumber(d1.borrow_mut().gameboy().cpu().de().into())
}),
);

let d1 = dbg_pointer.clone();
debug_commands.set(
"hl",
hlua::function0(move || -> LuaValue {
LuaValue::LuaNumber(d1.borrow_mut().gameboy.cpu().hl().into())
LuaValue::LuaNumber(d1.borrow_mut().gameboy().cpu().hl().into())
}),
);

let d1 = dbg_pointer.clone();
debug_commands.set(
"sp",
hlua::function0(move || -> LuaValue {
LuaValue::LuaNumber(d1.borrow_mut().gameboy.cpu().sp().into())
LuaValue::LuaNumber(d1.borrow_mut().gameboy().cpu().sp().into())
}),
);

let d1 = dbg_pointer.clone();
debug_commands.set(
"mem",
hlua::function1(move |address: u16| -> LuaValue {
LuaValue::LuaNumber(d1.borrow_mut().gameboy.cpu().mem8(address).into())
LuaValue::LuaNumber(d1.borrow_mut().gameboy().cpu().mem8(address).into())
}),
);

let d1 = dbg_pointer.clone();
debug_commands.set(
"next_cb",
hlua::function0(move || -> LuaValue {
LuaValue::LuaNumber(d1.borrow_mut().gameboy.cpu().next_cb().into())
LuaValue::LuaNumber(d1.borrow_mut().gameboy().cpu().next_cb().into())
}),
);

let d1 = dbg_pointer.clone();
debug_commands.set(
"clock_cycle",
hlua::function0(move || -> LuaValue {
LuaValue::LuaString(d1.borrow_mut().gameboy.cpu().clock_cycles().to_string())
LuaValue::LuaString(d1.borrow_mut().gameboy().cpu().clock_cycles().to_string())
}),
);

Expand All @@ -329,10 +331,10 @@ impl DebuggerTextInterface<'_> {
"load_rom",
hlua::function1(move |filename: String| -> LuaValue {
let rom = std::fs::read(filename).unwrap();
d1.borrow_mut().gameboy.load_rom(&rom);
d1.borrow_mut().gameboy().load_rom(&rom);
let game_name = String::from_utf8(
d1.borrow()
.gameboy
.gameboy()
.bus()
.memory()
.slice(0x134..0x143)
Expand All @@ -350,7 +352,11 @@ impl DebuggerTextInterface<'_> {
LuaValue::LuaString(
(0..0xFFFF)
.map(|i| {
format!("{:#02X} {:#02X}", i, d1.borrow_mut().gameboy.cpu().mem8(i))
format!(
"{:#02X} {:#02X}",
i,
d1.borrow_mut().gameboy().cpu().mem8(i)
)
})
.intersperse("\n".to_string())
.collect::<String>(),
Expand All @@ -365,7 +371,11 @@ impl DebuggerTextInterface<'_> {
LuaValue::LuaString(
(start..end)
.map(|i| {
format!("{:#02X} {:#02X}", i, d1.borrow_mut().gameboy.cpu().mem8(i))
format!(
"{:#02X} {:#02X}",
i,
d1.borrow_mut().gameboy().cpu().mem8(i)
)
})
.intersperse("\n".to_string())
.collect::<String>(),
Expand All @@ -387,8 +397,9 @@ impl DebuggerTextInterface<'_> {
.expect("Couldn't write PGM header");

// Our Game Boy's framebuffer seems to have a direct correspondence to this!
let d1 = d1.borrow_mut();
let frame = d1.gameboy.get_frame();
let d1 = d1.borrow();
let gameboy = d1.gameboy();
let frame = gameboy.get_frame();

for line in frame.array_chunks::<160>() {
let pgm_line = line
Expand Down
Loading

0 comments on commit 84df887

Please sign in to comment.