Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'step' debugger command #79

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
step
pineman committed Jul 23, 2024
commit 2edf8b68dbfdc8a1b8a62d1ca42f0150610ea281
18 changes: 7 additions & 11 deletions fpt-egui/src/main.rs
Original file line number Diff line number Diff line change
@@ -171,19 +171,18 @@ impl FPT {
let mut frame: Option<fpt::ppu::Frame> = None;
let delta_time = ui.input(|i| i.unstable_dt) as f64;
self.accum_time += delta_time;
// TODO: should limit to "a frame" taking self.slow_factor into account (so 10 frames at 0.1 or 0.1 frames at 10).
// TODO: should limit to a 60fps frame, taking self.slow_factor into account (so 10 frames at 0.1, or 0.1 frames at 10)
let cycles_want = self.accum_time.div_euclid(T_CYCLE * self.slow_factor) as u32;
let mut cycles_ran = 0;
while cycles_ran < cycles_want && !self.gb.paused() {
// TODO: care for double speed mode
self.gb.step();
self.cycles_since_last_frame += 1;
if self.cycles_since_last_frame == self.gb.cycles_in_one_frame() {
let cycles = self.gb.step();
self.cycles_since_last_frame += cycles;
if self.cycles_since_last_frame >= self.gb.cycles_in_one_frame() {
frame = Some(*self.gb.get_frame()); // Copies the whole [u8; WIDTH * HEIGHT] into frame
self.gb_frame_count += 1;
self.cycles_since_last_frame = 0;
}
cycles_ran += 1;
cycles_ran += cycles;
}
self.accum_time -= cycles_ran as f64 * T_CYCLE * self.slow_factor;
frame
@@ -284,7 +283,7 @@ impl FPT {
cpu_register!(ui, "A": cpu.a(), "F": cpu.f()); ui.end_row();
cpu_register!(ui, "B": cpu.b(), "C": cpu.c()); ui.end_row();
cpu_register!(ui, "D": cpu.d(), "E": cpu.e()); ui.end_row();
cpu_register!(ui, "H": cpu.a(), "L": cpu.f()); ui.end_row();
cpu_register!(ui, "H": cpu.h(), "L": cpu.l()); ui.end_row();
});
});
ui.separator();
@@ -401,10 +400,6 @@ impl FPT {
.clone_from(&self.debug_console.command);
self.debug_console.command = String::new();
}

//if let Some(e) = self.gb.cpu_mut().dbg_events.pop_front() {
// self.dc.push(e);
//}
});
}

@@ -546,6 +541,7 @@ impl FPT {

fn central_panel(&mut self, ctx: &Context, ui: &mut Ui) {
if !self.gb.cpu().paused() {
// TODO: only capture buttons if debug console is not focused
let buttons = Buttons {
a: ctx.input(|i| i.key_down(Key::A)),
b: ctx.input(|i| i.key_down(Key::S)),
7 changes: 7 additions & 0 deletions fpt/src/debug_interface.rs
Original file line number Diff line number Diff line change
@@ -50,10 +50,12 @@ pub enum DebugCmd {
ListBreakpoints,
ListWatchpoints,
Print(u16),
Step,
}

#[derive(Debug, PartialEq, Clone)]
pub enum DebugEvent {
Pause,
Continue,
RegisterBreakpoint(u16),
RegisterWatchpoint(u16),
@@ -64,6 +66,7 @@ pub enum DebugEvent {
Watchpoint(u16, u16),
Instrpoint(u16),
Print(u8),
Step,
}

impl fmt::Display for DebugEvent {
@@ -110,6 +113,8 @@ impl fmt::Display for DebugEvent {
DebugEvent::Print(value) => {
writeln!(f, "{:#04X}", value)
}
DebugEvent::Pause => writeln!(f, "pause"),
DebugEvent::Step => writeln!(f, "step"),
}
}
}
@@ -164,6 +169,8 @@ impl DebugCmd {
"lw" | "list_watchpoints" => Some(DebugCmd::ListWatchpoints),
"load" => Some(DebugCmd::Load(args.next().unwrap().to_string())),
"p" | "print" => print_cmd(args),
"s" | "step" => Some(DebugCmd::Step),
"pause" => Some(DebugCmd::Pause),
_ => None,
}
}
9 changes: 8 additions & 1 deletion fpt/src/debugger.rs
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ pub struct Debugger {
watchpoints: Vec<Watchpoint>,
instrpoints: Vec<Instrpoint>,
pub paused: bool,
pub step: bool,
dbg_events: VecDeque<DebugEvent>,
bus: Bus,
}
@@ -20,6 +21,7 @@ impl Debugger {
watchpoints: Vec::new(),
instrpoints: Vec::new(),
paused: false,
step: false,
dbg_events: VecDeque::new(),
bus,
}
@@ -29,7 +31,7 @@ impl Debugger {
match cmd {
DebugCmd::Pause => {
self.paused = true;
None
Some(DebugEvent::Pause)
}
DebugCmd::Continue => {
self.paused = false;
@@ -61,6 +63,11 @@ impl Debugger {
}
DebugCmd::Load(_) => None,
DebugCmd::Print(addr) => Some(DebugEvent::Print(self.bus.read(*addr as usize))),
DebugCmd::Step => {
self.paused = false;
self.step = true;
Some(DebugEvent::Step)
}
}
}

14 changes: 6 additions & 8 deletions fpt/src/lib.rs
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ pub mod ppu;
pub mod timer;

pub struct Gameboy {
t_cycle_counter: u8,
t_cycle_counter: u32,
bus: Bus,
cpu: LR35902,
ppu: Ppu,
@@ -133,17 +133,15 @@ impl Gameboy {
}

pub fn step(&mut self) -> u32 {
//let cycles = self.cpu.instruction() as u32;
self.cpu.t_cycle();
let cycles = self.cpu.t_cycle();
// TODO: care for double speed mode (need to run half as much dots)
self.ppu.step(1);

self.ppu.step(cycles);
// TODO: is this correct?
if self.t_cycle_counter == 3 {
self.timer.step();
}

self.t_cycle_counter = (self.t_cycle_counter + 1) % 4;
1
self.t_cycle_counter = (self.t_cycle_counter + cycles) % 4;
cycles
}

pub fn instruction(&mut self) -> u32 {
27 changes: 18 additions & 9 deletions fpt/src/lr35902.rs
Original file line number Diff line number Diff line change
@@ -632,29 +632,32 @@ impl LR35902 {

// Run instructions
/// Run one t-cycle - from actual crystal @ 4 or 8 MHz (double speed mode)
pub fn t_cycle(&mut self) {
pub fn t_cycle(&mut self) -> u32 {
let inst = self.decode();
self.set_inst_cycle_count(self.inst_cycle_count() + 1);
// Only actually mutate CPU state on the last t-cycle of the instruction
if self.inst_cycle_count() < inst.cycles {
return;
if self.inst_cycle_count() + 1 < inst.cycles {
self.set_inst_cycle_count(self.inst_cycle_count() + 1);
return 1;
}
self.update_code_listing(inst);
if self.debugger.match_breakpoint(self.pc()) {
return;
return 0;
}
if self.debugger.match_instrpoint(inst.opcode) {
return;
return 0;
}
self.set_inst_cycle_count(self.inst_cycle_count() + 1);
assert!(self.inst_cycle_count == inst.cycles);
self.execute(inst);
if !self.mutated_pc() {
self.set_pc(self.pc() + inst.size as u16);
}
let cycles = if inst.kind == InstructionKind::Jump && !self.mutated_pc() {
let cycles: u32 = if inst.kind == InstructionKind::Jump && !self.mutated_pc() {
inst.cycles_not_taken
} else {
inst.cycles
};
}
.into();
self.set_clock_cycles(self.clock_cycles() + cycles as u64);
self.set_mutated_pc(false);
self.set_inst_cycle_count(0);
@@ -678,14 +681,20 @@ impl LR35902 {
isv = 0x60;
self.bus.set_iflag(bw::set_bit8::<4>(iflag, false));
} else {
return;
return cycles;
}
// TODO: this is a big lie. we cant just run 5 cycles inside the function supposed to run 1 cycle lmao
self.set_ime(false);
self.push(self.pc());
self.set_pc(isv);
self.set_clock_cycles(self.clock_cycles() + 5);
return cycles + 5;
}
if self.debugger.step {
self.set_paused(true);
self.debugger.step = false;
}
cycles
}

/// Run one complete instruction - NOT a machine cycle (4 t-cycles)
2 changes: 1 addition & 1 deletion fpt/tests/lr35902.rs
Original file line number Diff line number Diff line change
@@ -1929,7 +1929,7 @@ fn test_pop(#[case] opcode: u8, #[case] register: &str, #[case] value: u16, #[ca
.with_mem8(0xff80, opcode)
.with_pc(0xff80)
.with_sp(sp)
.with_mem16(sp, dbg!(value));
.with_mem16(sp, value);
let mut sut = builder.clone().build();

// When