r/EmuDev • • Oct 25 '26

Need help with PPU background rendering

Building a Game Boy (DMG) emulator in Rust. The CPU passes Blargg test roms & the SM83 JSON test suite.

Current Behavior:

  1. Dr. Mario: Title screen renders properly, but after a few frames, the graphics get garbled up.
After a few seconds
  1. Tetris: Stuck rendering a screen grid of 00 tiles on startup.
Stuck on 0's
  1. Blargg Test Roms: Renders correctly.
  • OAM, DMA & Sprites have not been implemented yet, just the background and the window.
  • The Joypad returns 0xCF

I have no idea on where to even look for the bug. Any direction would be very helpful. Thank You.

6 Upvotes

9 comments sorted by

View all comments

3

u/HowziCanReddit Oct 25 '26 edited Oct 25 '26

Double check that your background rendering function casts the tile index to i8 when LCDC Bit 4 is set to 0.

Also make sure your scanline loop correctly cycles through modes:

  • Mode 2 (OAM Scan): ~80 dots.
  • Mode 3 (Pixel Transfer): ~172 to 289 dots (depending on window/sprites).
  • Mode 0 (HBlank): Remaining dots to complete 456 dots per scanline.
  • Mode 1 (VBlank): Scanlines 144 to 153 (456 dots each).

3

u/HowziCanReddit Oct 25 '26 edited Oct 25 '26

Also consider on real GB hardware, the CPU cannot access Video Memory ($8000–$9FFF) while the PPU is in Mode 3 (Pixel Transfer), because the PPU is actively reading tile maps/data from VRAM. So, in your memory write handler for $8000–$9FFF, check the current PPU mode; if the PPU is currently in Mode 3, ignore CPU writes (and similarly restrict OAM $FE00–$FE9F during Modes 2 and 3).

1

u/[deleted] Oct 26 '26

I've checked the addressing mode and it seems correct. And I have added restrictions to the VRAM and OAM during mode 2 and 3. But I think you were in the right direction, after garbling, Dr. Mario turns off the LCD and then never wakes up, repeatedly restarting. Maybe it's due to the something returning FF as garbage data but I will have to further test that.

1

u/HowziCanReddit Oct 26 '26 edited Oct 26 '26

1. When a game clears LCDC Bit 7 (turning off the display), the LY register (FF44) must immediately reset to 0 and remain frozen at 0 for as long as the LCD remains off. If your emulator keeps incrementing scanlines or leaves LY at whatever value it had when the display was turned off, games waiting for LY == 0 or expecting a fresh init state will hang indefinitely. Furthermore, when the LCD is disabled the PPU mode bits (bits 0–1 of FF41) must always read as 0 (HBlank), regardless of the internal state. While the LCD is disabled, all VRAM ($8000–$9FFF) and OAM ($FE00–$FE9F) restrictions are completely lifted. The CPU has completely free, unrestricted read/write access at any time because the PPU is inactive and not contending for the bus.

  • In your write_vram and write_oam functions, add an override: if the LCD is disabled, always allow the read/write immediately.

2. When the game sets LCDC Bit 7 back to 1 to turn the screen on, the PPU restarts. On real DMG hardware, the first scanline after enabling the LCD typically begins in Mode 0 for ~80 dots before transitioning into normal operation. Make sure your dot/scanline counter resets cleanly so it doesn’t pick up mid-frame with corrupted timing.

  • In your LCDC write handler, when bit 7 transitions from 1 to 0, explicitly set ppu.ly = 0, ppu.mode = 0, and pause/reset your dot counter.

1

u/[deleted] Oct 26 '26

Thanks but the issue was with the CPU interrupt handling ordering. I used to first check for pending interrupts then ticked the CPU twice which generated more interrupts.