halt in IME=0 && IF&IE==0

Discussion of programming and development for the original Game Boy and Game Boy Color.
Post Reply
User avatar
xuling93
Posts: 7
Joined: Mon Apr 13, 2015 7:52 am

halt in IME=0 && IF&IE==0

Post by xuling93 » Mon Oct 15, 2018 5:33 pm

i see opcode halt about http://www.devrs.com/gb/files/faqs.html#HALT

There is such a description.
------------------------------------------------------------------------------
HALT operates differently depending on whether the Interrupt Master Enable (IME) is set or reset.
Assembly command EI sets IME to 1.
Assembly command DI resets IME to 0.
IME = 0 - [$ff0f].AND.[$ffff] = 0 :
HALT is aborted. Next instruction is executed normally.
If IME is set to 1 at a later time then a halt condition
will occur one instruction after IME is set to 1 to complete
the halt that was not allowed to finish earlier.
------------------------------------------------------------------------------
If the description is correct... emm..

At IME=0, [$ff0f].AND.[$ffff] = = 0
If DMG executes halt, it will not cause a pause.
In this case, DMG will continue to execute instructions after halt?

User avatar
ISSOtm
Posts: 46
Joined: Fri Jan 04, 2019 5:31 pm
Location: France, right of a pile of consoles
Contact:

Re: halt in IME=0 && IF&IE==0

Post by ISSOtm » Fri Jan 04, 2019 6:31 pm

That's incorrect. The correct behavior of HALT is as follows:
  • If IME == 1:
    Execution is paused until IE & IF & $1F != 0
  • If IME == 0:
    • If IE & IF & $1F == 0:
      Execution is paused until IE & IF & $1F != 0, after which execution resumes normally.
      This is basically the same as IME = 1, but the interrupt handler is not called.
    • If IE & IF & $1F != 0:
      Execution doesn't pause, but the byte after the HALT is read twice (PC fails to increment).
      This would cause single-byte instructions to be simply repeated (such as `dec a`), and multi-byte instructions will read the opcode again as the first operand byte, and the last operand byte will be executed as an opcode. (Example: `ld a, $AF` = `3E AF` will turn into `3E 3E AF` = `ld a, $3E ; xor a`)
According to beware (BGB's developer), the game The Smurfs relies on the latter behavior, whereas this emulator detection code I've written relies on the former. (Note that this code only passes on GBA's, and relies on dodgy HDMA behavior for the most part)
The French Lord of Laziness (and a huge Legend of Zelda fan)
https://github.com/ISSOtm
ASMu is laifu <3

Post Reply