Skip to content

Commit

Permalink
Fix flush_tima tick bug
Browse files Browse the repository at this point in the history
Need to take into account that tima can tick several times during a tima
flush. This makes the emulator pass the instr_timing test
  • Loading branch information
nekronos committed Dec 3, 2016
1 parent fdd4ac9 commit ac1c734
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/gbc/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,15 @@ impl Timer {
}

fn flush_tima(&mut self, cycle_count: u32) -> bool {
self.tima_cycles = self.tima_cycles + cycle_count;

let cycles = self.tima_cycles;
let tima_cycles = self.tima_cycles + cycle_count;
let rate = CLOCKS[self.clock_select as usize];
let ticks = tima_cycles / rate;

let tick = cycles >= rate;

if tick {
self.tima_cycles = cycles - rate;
}
self.tima_cycles = tima_cycles - rate * ticks;

if self.enabled && tick {
let (tima, overflow) = self.tima.overflowing_add(1);
self.tima = if overflow { self.tma } else { tima };
if self.enabled {
let (tima, overflow) = self.tima.overflowing_add(ticks as u8);
self.tima = if overflow { self.tma.wrapping_add(tima) } else { tima };
overflow
} else {
false
Expand Down

0 comments on commit ac1c734

Please sign in to comment.