Clownacy/Sonic-2-Clone-Driver-v2

Clone Driver occasionally locks up 68k waiting for Z80

Pacca64 opened this issue · 4 comments

Very rarely, the Clone Driver v2 will freeze gameplay when attempting to change songs. It seems to happen in the SMPS_waitZ80 macro under "DACUpdateSample". The odd thing is, if I skip past the bne loop using regens' debugger, everything continues as normal. The music plays properly, DAC samples work as normal, and gameplay is completely unaffected.

I can provide a savestate, rom and maybe even some source code in private if necessary.

Strange. The only thing I can imagine stopping a Z80 bus request would be an unfortunately-timed H-Int. A savestate and ROM would probably help, yes.

Which version of the driver is this happening with anyway? The nightly or 2.7?

Should be 2.7, I got it from the releases page. I'll PM you the rom and savestate on SSRG.

Right, I think I might have found what it was. It turns out V-Int can occur while the V-Int from the last frame is still running. This normally shouldn't happen, but a 68k sound driver can easily make V-Int that slow. Anyhow, this isn't too big of a problem until the V-Int just so happens to occur between a Z80 bus request, and the idle loop that waits for the bus to be handed over. This is the only thing I can imagine causing the crash.

I think this guess holds some water, considering the crash happens specifically when a song starts, which takes a lot of cycles, and could easily push the V-Int into the next frame.

Presumably, S1 avoided this because it only stops the Z80 once, and it's very early in V-Int, making it unlikely for another V-Int to interrupt it before the check occurs.

To avoid this, add these macros:

; ---------------------------------------------------------------------------
; stop the Z80
; ---------------------------------------------------------------------------
SMPS_stopZ80_safe macro
	move.w	sr,-(sp)
	move.w	#$2700,sr	; mask off interrupts
	SMPS_stopZ80
	endm

; ---------------------------------------------------------------------------
; start the Z80
; ---------------------------------------------------------------------------
SMPS_startZ80_safe macro
	SMPS_startZ80
	move.w	(sp)+,sr
	endm

...and then replace all the SMPS_stopZ80/SMPS_startZ80 macros in Sonic 2 Clone Driver v2.asm with SMPS_stopZ80_safe and SMPS_startZ80_safe.

I have yet to find this issue after implementing the fix. I'll reopen this if I run into it again.