deltabeard / Peanut-GB

A Game Boy (DMG) emulator single header library written in C99. Performance is prioritised over accuracy.
https://projects.deltabeard.com/peanutgb/
276 stars 35 forks source link

Super Mario Land 2 crashes to title screen #70

Closed diddyholz closed 1 year ago

diddyholz commented 1 year ago

When trying to start a level in Super Mario Land 2, the game goes back to the title screen instead of loading the level. This can be reproduced with the SDL2 example.

The issue seemed to have appeared somewhere in c75ac197baf13274d340a050228855f9ada862f4 and its follow up commits as it worked perfectly fine in 0280730697393e8e4ff549a898042a1d42b2865b.

deltabeard commented 1 year ago

Confirmed. It must be to do with the way I'm handling LCD states. Once I have time, I'll have written a good debugger that will be capable of finding the exact bug that's causing this.

deltabeard commented 1 year ago

The following patch fixes this issue for Super Mario Land 2, but breaks Dr. Mario.

diff --git a/peanut_gb.h b/peanut_gb.h
index f5d2d2e..d59dd83 100644
--- a/peanut_gb.h
+++ b/peanut_gb.h
@@ -3340,8 +3340,7 @@ void __gb_step_cpu(struct gb_s *gb)
                gb->counter.lcd_count += inst_cycles;

                /* New Scanline */
-               if((gb->hram_io[IO_STAT] & STAT_MODE) & IO_STAT_MODE_VBLANK_OR_TRANSFER_MASK &&
-                               gb->counter.lcd_count >= LCD_LINE_CYCLES)
+               if(gb->counter.lcd_count >= LCD_LINE_CYCLES)
                {
                        gb->counter.lcd_count -= LCD_LINE_CYCLES;

I still need to figure out why this breaks Dr. Mario. My hypothesis is that this may be to do with the changes in LCD timing with Sprite Fetch Abortion which I don't think that peanut-gb takes into account.

deltabeard commented 1 year ago

I also need to add automatic testing of various games to make sure that I don't break previously working games with new commits. I'm thinking of playing previously recorded joypad inputs at specific frames and then checking that we reach a certain program counter or that the LCD display is the same.

deltabeard commented 1 year ago

Could you please test 58dbb7d9c3cda1560b196a3f5c9a4fcb2c0d3356?

diddyholz commented 1 year ago

I‘ll test it later today 👍

diddyholz commented 1 year ago

This seems to work. Thank you!

deltabeard commented 1 year ago

No problem. Be aware that there may be some sprite glitches due to #69.

deltabeard commented 1 year ago

The sprite glitch in #69 has been fixed with 5d1578f51449c959df16702cbba471479c92b076.