diff --git a/pyboy/core/cpu.py b/pyboy/core/cpu.py index 311b431a2..5c7259af5 100644 --- a/pyboy/core/cpu.py +++ b/pyboy/core/cpu.py @@ -109,35 +109,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 diff --git a/pyboy/core/opcodes.py b/pyboy/core/opcodes.py index d90ede52e..2c8f3e783 100644 --- a/pyboy/core/opcodes.py +++ b/pyboy/core/opcodes.py @@ -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 diff --git a/pyboy/core/opcodes_gen.py b/pyboy/core/opcodes_gen.py index 21933e40a..2bd2e39ae 100644 --- a/pyboy/core/opcodes_gen.py +++ b/pyboy/core/opcodes_gen.py @@ -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()