Programming for the Gameboy/Hardware errata and bugs

16-bit Increment/Decrement bug

edit

When the following instructions are used, and the contents of their 16-bit target register is between $FE00 and $FEFF, a flaw in the Gameboy hardware will write trash to OAM RAM, corrupting sprites. Only sprites 1 & 2 ($FE00 & $FE04) are not affected by these instructions.

; rr is bc, de, or hl

inc rr
dec rr
ldi a, (hl)
ldd a, (hl)
ldi (hl), a
ldd (hl), a

This bug does not affect the Gameboy Color or Advance, only the Gameboy, Gameboy Pocket, and Super Gameboy.

Halt bug

edit

The halt has a flaw that occurs whenever it is executed and interrupts are disabled. When this occurs, the instruction counter will "duplicate" the next byte, and read it twice. For example,

halt
inc b ; B will be incremented twice, assuming interrupts are disabled, as inc b is a 1 byte instruction.

To avoid this, always put a nop directly after a halt, to avoid unwanted side-effects.

Odd behavior will occur if two halt instructions are put in a row and interrupts are disabled, which will softlock the Gameboy CPU and require the user to reset it.

halt
halt ; CPU will hang if interrupts are disabled

This bug does not affect the Gameboy Color or Advance, only the Gameboy, Gameboy Pocket, and Super Gameboy.