Skip to content

Commit

Permalink
Fix timer for Blargg interrupt test?
Browse files Browse the repository at this point in the history
  • Loading branch information
Baekalfen committed Sep 15, 2024
1 parent f7f6e03 commit d13246a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
2 changes: 0 additions & 2 deletions pyboy/core/timer.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ cdef class Timer:
cdef void reset(self) noexcept nogil
@cython.locals(divider=cython.int)
cdef bint tick(self, uint64_t) noexcept nogil
@cython.locals(divider=cython.int, cyclesleft=cython.uint)
cdef int64_t cycles_to_interrupt(self) noexcept nogil

cdef void save_state(self, IntIOInterface) noexcept
cdef void load_state(self, IntIOInterface, int) noexcept
10 changes: 4 additions & 6 deletions pyboy/core/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def tick(self, _cycles):
if cycles == 0:
return False
self.last_cycles = _cycles

self.DIV_counter += cycles
self.DIV += (self.DIV_counter >> 8) # Add overflown bits to DIV
self.DIV_counter &= 0xFF # Remove the overflown bits
Expand All @@ -54,21 +55,18 @@ def tick(self, _cycles):
divider = self.dividers[self.TAC & 0b11]

ret = False
if self.TIMA_counter >= 1 << divider:
self.TIMA_counter -= 1 << divider # Keeps possible remainder
while self.TIMA_counter >= (1 << divider):
self.TIMA_counter -= (1 << divider) # Keeps possible remainder
self.TIMA += 1

if self.TIMA > 0xFF:
self.TIMA = self.TMA
self.TIMA &= 0xFF
ret = True
# return True
# self._cycles_to_interrupt = ((0x100 - self.TIMA) * (1 << divider)) - self.TIMA_counter
self._cycles_to_interrupt = ((0x100 - self.TIMA) << divider) - self.TIMA_counter
return ret

def cycles_to_interrupt(self):
return self._cycles_to_interrupt

def save_state(self, f):
f.write(self.DIV)
f.write(self.TIMA)
Expand Down

0 comments on commit d13246a

Please sign in to comment.