Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emulation of 68K bus access refresh delays #407

Open
ekeeke opened this issue Jan 29, 2022 · 6 comments
Open

Emulation of 68K bus access refresh delays #407

ekeeke opened this issue Jan 29, 2022 · 6 comments

Comments

@ekeeke
Copy link
Owner

ekeeke commented Jan 29, 2022

As verified on real hardware (cf. https://gendev.spritesmind.net/forum/viewtopic.php?f=2&t=2323), there are periodical refresh access slots on 68K bus which can delay access from 68K CPU:

  • in cartridge slot area (0x000000-0x3FFFFF)
  • in work RAM area (0xE00000-0xFFFFFF)

These delays are actually not emulated in Genesis Plus GX and are the cause of graphical issues or crashes in the following games (the code of these games is somehow bugged / unsafe and is causing some race conditions with hardware or concurrently running software that were by luck not occurring on real hardware because of the tight timings but are exposed in emulator as it is running the code a little bit faster than real hardware):

@Tippek
Copy link

Tippek commented Feb 5, 2022

Primitive fix for Super Airwolf and Pac-man 2:
https://github.com/ekeeke/Genesis-Plus-GX/blob/master/core/m68k/m68kcpu.c#L305
if((m68k.cycles-refresh_cycles)>=(128*7)) { m68k.cycles += 2*7; refresh_cycles = m68k.cycles; }
with unsigned int refresh_cycles

for Megadrive 2 * 7, but for SCD probably 2 * 4.
based on https://plutiedev.com/mirror/kabuto-hardware-notes#bus-system
+2 cycles every 128

@ekeeke
Copy link
Owner Author

ekeeke commented Feb 6, 2022

Yes, that's more or less the same way I initially tried to implement it but unfortunately this is not enough to fix Clue graphics issues without the aforementioned hack.

The two other games only need CPU execution to be shifted a little every lines/frames but Clue requires very accurate timings as the race condition window is very tight: the bug occurs if the CPU is running too fast but also if it is too slow. I think it only works correctly if the fixed ROM delays are accurately emulated (i.e only in case of access to cartridge/expansion area within the refresh cycle window) and if RAM delays are also accurately emulated (this one is more complex from what I observed on real hw as it seems to be a variable delay, between 0 and 3 cycles, depending how CPU access to RAM is aligned with the RAM refresh slot).

@Tippek
Copy link

Tippek commented Feb 6, 2022

I did noticed CPU still very fast in GPGX even with this refresh delay. http://gendev.spritesmind.net/forum/viewtopic.php?f=8&p=37391#p37391 - this tests on my MD1 Japan, so I guess it also need refresh delay for RAM (I guess about 3 cycles every 120 cycles), but don't sure its helps, because GPGX CPU very fast as I did said.

Is it fix correct? https://github.com/ekeeke/Genesis-Plus-GX/blob/master/core/vdp_ctrl.c#L2258
because it's not equal x*7

@ekeeke
Copy link
Owner Author

ekeeke commented Feb 6, 2022

Is it fix correct? https://github.com/ekeeke/Genesis-Plus-GX/blob/master/core/vdp_ctrl.c#L2258
because it's not equal x*7

This is part of the hack mentionned in initial post to get Clue/Microcosm games working: indeed, these 2 games appear to have a big VRAM update (done with CPU writes instead of DMA as it should have been) interrupted by VINT, which screws the code register by updating VDP registers and make all the further writes to VRAM invalid when the interrupted routine continues its processing. Adding 2 M-cycles (less than one cpu cycle) on every invalid access was the minimal value I found to work in order to get sufficient delay but it is indeed not accurate at all (and does not happen on real hardware), as said above, this is hack (that I would like to get ride of by implementing accurate refresh delays).

@Silanda
Copy link

Silanda commented Oct 5, 2022

FWIW, Tippek's fix seems to alleviate a hitherto unreported issue with graphic corruption before the bonus stages in the PAL version of Art of Fighting, so it might be another game affected by this. Interestingly, neither the US version or the PAL version run in NTSC mode exhibit the problem.

Running with the CPU speed at 125% also stops the glitching.

@ekeeke
Copy link
Owner Author

ekeeke commented Feb 23, 2024

Basic emulation of periodic 68K cartridge/expansion bus refresh delay added in 47761b9

Implementation is a little different from Tippek's original proposition as it gave one-cycle error for many instructions when running this test ROM: https://gendev.spritesmind.net/forum/viewtopic.php?f=8&t=3321 but otherwise it's very similar.

This also gives very close results to real hardware when running Sik's 68K benchmark test ROM (real hardware shows 88AD / 88AE)
image

Those two test ROMs are attached below for future references.
test_inst_speed.zip
68kbench.zip

As expected, this fixes libretro#205

https://bitbucket.org/eke/genesis-plus-gx/issues/444/crashes-in-pacman-2-new-adventures-pac-jr was actually fixed by 405c545

FIFO access delay timing hacks (previously required by Microcosm and Clue to compensate lack of 68k bus refresh delays emulation) were reverted in c04a942

Without the hacks, Microcosm still boots fine and Clue also in most cases, although the latter can still occasionally display corrupted tiles depending on when or how START button is pressed to access title screen (from experiment, it seems that holding START until title screen appears is always fine but repetitively smashing the START button can cause graphical issues to occur). It seems that additional RAM refresh delays are required to be safe so I leave this issue opened until this is properly implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants