Skip to content

Commit

Permalink
pretend MBC1 is MBC3 & quickly implement HALT (#98)
Browse files Browse the repository at this point in the history
* pretend MBC1 is MBC3

* quickly implement HALT
  • Loading branch information
pineman committed Aug 10, 2024
1 parent 38f57f4 commit b918fcc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 35 deletions.
75 changes: 40 additions & 35 deletions fpt/src/lr35902.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct LR35902 {
clock_cycles: u64,
inst_cycle_count: u8,
branch_taken: bool,
halted: bool,
bus: Bus,
debugger: Debugger,
}
Expand Down Expand Up @@ -74,6 +75,7 @@ impl LR35902 {
clock_cycles: 0,
inst_cycle_count: 0,
branch_taken: false,
halted: false,
bus: bus.clone(),
// Debugging
debugger: Debugger::new(bus.clone()),
Expand Down Expand Up @@ -639,45 +641,48 @@ impl LR35902 {
/// But, for now, it's easier to run multiple cycles in each step().
pub fn step(&mut self) -> u8 {
let inst = self.decode();
if !self.halted {
// Only actually mutate CPU state on the last t-cycle of the instruction
if self.inst_cycle_count() + 1 < inst.cycles {
self.set_inst_cycle_count(self.inst_cycle_count() + 1);
return 1;
}

// Only actually mutate CPU state on the last t-cycle of the instruction
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 0;
}
if self.debugger.match_instrpoint(inst.opcode) {
return 0;
}
self.update_code_listing(inst);
if self.debugger.match_breakpoint(self.pc()) {
return 0;
}
if self.debugger.match_instrpoint(inst.opcode) {
return 0;
}

self.execute(inst);
self.execute(inst);

if !self.mutated_pc() {
self.set_pc(self.pc() + inst.size as u16);
if !self.mutated_pc() {
self.set_pc(self.pc() + inst.size as u16);
}
}

let intr_service_routine_cycles = if self.ime { self.run_interrupts() } else { 0 };

if self.debugger.step {
self.set_paused(true);
self.debugger.step = false;
if intr_service_routine_cycles != 0 && self.halted {
self.halted = false;
}
if !self.halted {
if self.debugger.step {
self.set_paused(true);
self.debugger.step = false;
}

let inst_cycles = if inst.kind == InstructionKind::Jump && !self.mutated_pc() {
inst.cycles_not_taken
} else {
inst.cycles
};
self.set_clock_cycles(
self.clock_cycles() + inst_cycles as u64 + intr_service_routine_cycles as u64,
);
self.set_inst_cycle_count(0);
self.set_mutated_pc(false);
}

let inst_cycles = if inst.kind == InstructionKind::Jump && !self.mutated_pc() {
inst.cycles_not_taken
} else {
inst.cycles
};
self.set_clock_cycles(
self.clock_cycles() + inst_cycles as u64 + intr_service_routine_cycles as u64,
);
self.set_inst_cycle_count(0);
self.set_mutated_pc(false);

1 + intr_service_routine_cycles
}

Expand Down Expand Up @@ -1279,9 +1284,9 @@ impl LR35902 {
}
0x76 => {
// HALT
// Take care for halt bug: https://gbdev.io/pandocs/halt.html
// TODO: Take care for halt bug: https://gbdev.io/pandocs/halt.html
// https://rgbds.gbdev.io/docs/v0.6.1/gbz80.7/#HALT
todo!("0x76 HALT")
self.halted = true;
}
0x77 => {
// LD (HL),A
Expand Down
Empty file added fpt/src/memory/mbc1.rs
Empty file.
2 changes: 2 additions & 0 deletions fpt/src/memory/mbc_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub fn create_mbc(cartridge_data: &[u8]) -> Option<Box<RefCell<dyn Cartridge>>>
// https://gbdev.io/pandocs/The_Cartridge_Header.html#0147--cartridge-type
match get_cartridge_type(cartridge_data) {
0x00 => Some(Box::new(RefCell::new(NoMbcCartridge::new(cartridge_data)))), // rom only
// TODO: lies, figure out where mbc1 differs from mbc3
0x01 => Some(Box::new(RefCell::new(Mbc3Cartridge::new(cartridge_data)))),
0x0F..=0x13 => Some(Box::new(RefCell::new(Mbc3Cartridge::new(cartridge_data)))),
_ => None,
}
Expand Down
1 change: 1 addition & 0 deletions fpt/src/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl Bus {
}

pub fn read(&self, address: Address) -> u8 {
// TODO
let mut rng = rand::thread_rng();
if address == 0xff04 {
let r: u8 = rng.gen();
Expand Down

0 comments on commit b918fcc

Please sign in to comment.