Skip to content

Commit

Permalink
Only pre-check interrupts and halt in CPU tick
Browse files Browse the repository at this point in the history
  • Loading branch information
Baekalfen committed Sep 20, 2024
1 parent e0c2a27 commit a548e5d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
46 changes: 20 additions & 26 deletions pyboy/core/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,35 +106,29 @@ def set_interruptflag(self, flag):

def tick(self, cycles_target):
_cycles0 = self._cycles
self.bail = False
while self._cycles - _cycles0 < cycles_target:
if self.check_interrupts():
self.halted = False
# TODO: We return with the cycles it took to handle the interrupt
break
# return cycles

if self.halted and self.interrupt_queued:
# GBCPUman.pdf page 20
# WARNING: The instruction immediately following the HALT instruction is "skipped" when interrupts are
# disabled (DI) on the GB,GBP, and SGB.
self.halted = False
self.PC += 1
self.PC &= 0xFFFF
elif self.halted:
self._cycles += 4 # TODO: Number of cycles for a HALT in effect?
break

self.interrupt_queued = False
if self.check_interrupts():
self.halted = False
# break
# TODO: We return with the cycles it took to handle the interrupt
# return cycles

if self.halted and self.interrupt_queued:
# GBCPUman.pdf page 20
# WARNING: The instruction immediately following the HALT instruction is "skipped" when interrupts are
# disabled (DI) on the GB,GBP, and SGB.
self.halted = False
self.PC += 1
self.PC &= 0xFFFF
elif self.halted:
self._cycles += cycles_target
# self._cycles += 4 # TODO: Number of cycles for a HALT in effect?
# break
self.interrupt_queued = False

self.bail = False
while self._cycles - _cycles0 < cycles_target:
# TODO: cpu-stuck check for blargg tests?
# if cycles_target != 0:
# print(cycles_target)

# self.cycles_target = cycles_target
# if cycles_target == 0:
# return self.fetch_and_execute()

self._cycles += self.fetch_and_execute()
if self.bail: # Possible cycles-target changes
break
Expand Down
1 change: 1 addition & 0 deletions pyboy/core/opcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,7 @@ def LD_75(cpu): # 75 LD (HL),L

def HALT_76(cpu): # 76 HALT
cpu.halted = True
cpu.bail = True
return 4


Expand Down
1 change: 1 addition & 0 deletions pyboy/core/opcodes_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ def HALT(self):
# TODO: Implement HALT bug.
code.addlines([
"cpu.halted = True",
"cpu.bail = True",
"return " + self.cycles[0],
])
return code.getcode()
Expand Down

0 comments on commit a548e5d

Please sign in to comment.