🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.emulators.game-consoles
1 messages
1 total messages Started by John Ames Fri, 13 Nov 2026 23:57
Game Boy OAM corruption bug - details and workaround questions
#2
Author: John Ames
Date: Fri, 13 Nov 2026 23:57
52 lines
4306 bytes
I've been puttering around with GB homebrew a bit, mostly getting my
feet wet with the SM83 and getting my head around the architecture -
but one thing I just found out about is the OAM corruption bug on pre-
GBC models. The Pan docs have a writeup on it at:

https://gbdev.io/pandocs/OAM_Corruption_Bug.html

...but in brief, there are specific ways in which the SM83 core can
interfere with memory access for the period of each scanline where the
PPU is scanning the sprite table to see which ones to draw, and corrupt
it as a result. What I'm curious about is how to avoid it. (Of course I
could just target the GBC exclusively, but where's the fun in that?)

The obvious bit (don't touch the OAM outside VBlank) is easy enough, but
what gets me is, if I'm following this correctly, there are a few other
ways to trip over this that'd be more confining to work around:

* Any 16-bit value that passes through the CPU's internal inc/dec unit
  gets output on the address bus, and if it happens to be in the $FExx
  range this can trigger bus contention with the PPU & corrupt the OAM,
  *even if it's not a memory reference.* So incrementing/decrementing a
  register pair for math/count purposes is potentially hazardous.
* Per the SameBoy release notes, this also goes for LD SP, HL - but it
  doesn't say whether that's if the value loaded is $FExx; I'm assuming
  that's the case.
* There's also something about RST $38 triggering the issue, I guess
  since the opcode is $FF and however that comes over the bus looks
  like an $FExx address? But it talks about executing code from inside
  the OAM? Not super clear here.

Some of this isn't too bad (easy enough to avoid indirect auto-inc/dec
in the $FExx range, or putting code/the stack there.) Foregoing RST $38
would be a minor annoyance as it's one of only eight available short-
call instructions, but still livable. But the bit about non-address 16-
bit increment/decrement also being hazardous is more irksome. I can see
the following ways of working around it, and I don't like any of 'em: 

* "Don't use those numbers" didn't fly with the Pentium FDIV bug and it
  doesn't fly here.
* "Don't use 16-bit increment/decrement" is also annoying; the SM83 is
  *already* short on 16-bit operations compared to the Z80.
* "Pause during OAM scan" doesn't put arbitrary restrictions on the
  rest of the program, but if I'm tallying correctly that's almost
  1/6th of the frame time, per https://gbdev.io/pandocs/Rendering.html
* "Do something else during OAM scan" seems hard to make effective use
  of, as SM83 interrupt overhead takes almost half of that period.
  (Though I guess you could use it for a sample-playback routine.)

I'm curious to know how/if various developers got around this, over the
years - was there an accepted practice, did everyone do it their own
way, or did they just not bother?
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads