ekeeke / Genesis-Plus-GX

An enhanced port of Genesis Plus - accurate & portable Sega 8/16 bit emulator
Other
672 stars 193 forks source link

Mortal Kombat (Sega CD) hangs while loading #533

Closed KamiLordus closed 5 months ago

KamiLordus commented 7 months ago

The game works properly until you defeat the first opponent on the arcade ladder. When the game tries to load the next stage/opponet, a black screen appears and the game hangs. It also happens that you can only select the character you want to fight and the game will not load the first fight. The latest patch is probably to blame, because I was playing recently (3-4 weeks ago) and there was no problem.

Win 11 Retroarch 1.16 Genesis core latest Mortal Kombat Sega CD - usa chd format.

Edit: I tested the Pal versions - MEGA CD (chd file format) and the same situation.

ekeeke commented 7 months ago

Yes, i think this commit introduced this regression, although the commit itself is probably not to blame but some other potential existing synchronization issues that would not have impact before because of less accurate emulation. I remember this game in particular freezing at some parts when I initially added Mega-CD support because it tried to access Word-RAM when it shouldn't and it eventually started working when I relaxed the emulation model.

I guess I will need to have a second look at it.

ekeeke commented 5 months ago

Fixed in https://github.com/ekeeke/Genesis-Plus-GX/commit/9f25226536df65d32abc327c9f06ee6f625e6828

For the record, it was indeed a regression introduced by previous commit adding emulation of SUB-CPU lock in case of Word-RAM access while it is allocated to Main-CPU. The initial implementation was very basic (it would simply halt SUB-CPU at the end of instruction execution then reexecute the instruction when Word-RAM is reallocated to SUB-CPU) but was not robust to some edge cases like, for example, when the same address register is used as source address for a read then updated with the result of the read (MOVEA (Ax),Ax instruction), which is what Mortal Kombat is doing (with Ax pointing to Word RAM) after allocating Word-RAM to MAIN-CPU. The implemented solution fixes this by saving address registers state when the locking access is done (then restoring them when SUB-CPU is unlocked and instruction reexecuted) and also take care of potential address registers post-increment or pre-decrement that could have happened during instruction processing before the access. It's probably a little over-engineered as games will likely mostly use MOVE or MOVEA instructions to access Word-RAM but this way it should be robust to all kind of instructions and is still more efficient than saving all address registers before each executed instruction.

KamiLordus commented 5 months ago

Thank you very much for the fix.