Actions

SCHG How-to

Fix Song Restoration Bugs in Sonic 1's Sound Driver

From Sonic Retro

(Original guide by MarkeyJester and Clownacy)

There are two bugs with the track restoration system in Sonic 1's sound driver that will be fixed here. One for the DAC, and one for FM 6, with the latter bug stemming from how they share a channel. But first, the former:

Fixing the DAC fade-in bug

When collecting an extra life, the engine stores away the currently playing music track, and then plays the extra life jingle. Once the extra life jingle has finished playing, the previous track is restored at very low volume, and then fades into normal volume. The DAC (drum samples) does not have fading-in capabilities for this engine, so it does not resume until the music has finished fading into full volume.

Explaining the issue

If you pause the game while the music track is fading back in, and then unpause, the DAC channel does not resume; it remains mute. It seems that the pause routine not only sets the keys off, but it also turns off the left and right speaker panning for each channel (because of the release rate), and unpausing causes the system to reset all of the channel's panning/AMS/FMS values. Of course, if the music is going through a fade-in, then the DAC channel (occupying the FM 6 channel) must remain mute until the music has fully faded, hence, the panning/AMS/FMS value for FM 6 does not get updated while DAC is running, until of course; either a new track is played, the game is paused and unpaused again after fading, or the DAC channel changes speakers during its script.

Fixing the issue (Hivebrain)

We need to go to "loc_726D6:"

		bclr	#2,$40(a6)
		clr.b	$24(a6)
		rts

The first instruction clears the fade out mute bit, which was set by the "E4" flag (fade out into previous track), the second instruction clears the "currently fading out" flag (telling the system that fading out is complete), the "key on" values are resumed by the system already through natural course, all that's left is to reset the L/R/AMS/FMS of FM6 on the YM2612, like so:

loc_726D6:
		bclr	#2,$40(a6)
		clr.b	$24(a6)

		tst.b	$40(a6)					; is the DAC channel running?
		bpl.s	Resume_NoDAC				; if not, branch

		moveq	#$FFFFFFB6,d0				; prepare FM channel 3/6 L/R/AMS/FMS address
		move.b	$4A(a6),d1				; load DAC channel's L/R/AMS/FMS value
		jmp	sub_72764(pc)				; write to FM 6

Resume_NoDAC:
		rts

Now the DAC channel will resume correctly after fade-in.

Fixing the issue (GitHub)

We need to go to "@fadedone:"

		bclr	#2,v_music_dac_track+TrackPlaybackControl(a6)	; Clear 'SFX overriding' bit
		clr.b	f_fadein_flag(a6)				; Stop fadein
		rts

The first instruction clears the fade out mute bit, which was set by the "E4" flag (fade out into previous track), the second instruction clears the "currently fading out" flag (telling the system that fading out is complete), the "key on" values are resumed by the system already through natural course, all that's left is to reset the L/R/AMS/FMS of FM6 on the YM2612, like so:

; loc_726D6:
@fadedone:
		bclr	#2,v_music_dac_track+TrackPlaybackControl(a6)	; Clear 'SFX overriding' bit
		clr.b	f_fadein_flag(a6)				; Stop fadein

		tst.b	v_music_dac_track+TrackPlaybackControl(a6)					; is the DAC channel running?
		bpl.s	@Resume_NoDAC				; if not, branch

		moveq	#$FFFFFFB6,d0				; prepare FM channel 3/6 L/R/AMS/FMS address
		move.b	v_music_dac_track+TrackAMSFMSPan(a6),d1				; load DAC channel's L/R/AMS/FMS value
		jmp	WriteFMII(pc)				; write to FM 6

@Resume_NoDAC:
		rts

Now the DAC channel will resume correctly after fade-in.

Fixing the FM 6 restoration bug

When playing through Sonic 1's special stages, you may happen upon 100 rings and get an extra life. After the extra life's jingle stops playing, however, you'll find that the special stage's theme loses a track, FM 6 to be precise. But what causes that, and how can it be fixed?

Explaining the issue

As mentioned before, FM 6 and the DAC share hardware, so you can only have one playing at a time. The is done through the changing of some variables. Now, when the extra life jingle kicks in, the DAC is turned on, replacing FM 6. The song then plays, and, following that, the previous song restoration takes place. The problem here is that DAC isn't turned back off, stopping FM 6.

Fixing the issue (Hivebrain)

What we need to do is find the code that restores the track data, and then make it re-enable FM 6 if needed. To do that, go to "loc_72B14:", and scroll down, until just above "loc_72B9E:". There, you will see this:

		movea.l	a3,a5

Directly below it, insert this:

		tst.b	$40(a6)			; is the DAC channel running?
		bmi.s	Restore_NoFM6		; if it is, branch

		moveq	#$2B,d0			; DAC enable/disable register
		moveq	#0,d1			; Disable DAC
		jsr	sub_7272E(pc)

Restore_NoFM6:

Now FM 6 will be restored when needed.

Fixing the issue (GitHub)

What we need to do is find the code that restores the track data, and then make it re-enable FM 6 if needed. To do that, go to "@restoreramloop:", and below it, you will see this:

		dbf	d0,@restoreramloop

		bset	#2,v_music_dac_track+TrackPlaybackControl(a6)	; Set 'SFX overriding' bit

between it, insert this:

		move.b	#$2B, d0	; Register: DAC mode (bit 7 = enable)
		moveq	#$00, d1	; Value: DAC mode disable
		jsr	WriteFMI(pc)	; Write to YM2612 Port 0 [sub_7272E]

Now FM 6 will be restored when needed.

Fixing the 0FM/DAC fade-in bug

Most of you will probably never make SMPS files that use only the 3 PSG channels, but if you like to experiment with SMPS music, you might encounter this bug. In general, the song might or might not load. The behaviour can be random, because it uses uninitialized registers.

Fixing the issue

Go to 'loc_72068:' or '@nospeedshoes:' and below it, you'll find this.

		move.b	4(a3),d4	; [!] load tempo dividing timing
		moveq	#$30,d6		; [!] load F8 gosub coord. flag pointer
		move.b	#1,d5		; [!] Note duration for first "note"

These load a few default values that are important for the driver setup. If there are 0 FM/DAC tracks, the beq.w @bmg_fmloadloop or loc_72098 skips them and the PSG tracks will be set up with garbage values. (Since you're lucky, an unmodded Sonic 1 driver just won't play anything at all. But it can also play the first note and hang then.)

The fix is quite simple, just move these 3 lines a bit up somewhere above of the beq.w so that they get processed even if there are no FM/DAC channels. I recommend to move them between addq.w #6,a4 and moveq #0,d7.

SCHG How-To Guide: Sonic the Hedgehog (16-bit)
Fixing Bugs
Fix Demo Playback | Fix a Race Condition with Pattern Load Cues | Fix the SEGA Sound | Display the Press Start Button Text | Fix the Level Select Menu | Fix the Hidden Points Bug | Fix Accidental Deletion of Scattered Rings | Fix Ring Timers | Fix the Walk-Jump Bug | Correct Drowning Bugs | Fix the Death Boundary Bug | Fix the Camera Follow Bug | Fix Song Restoration Bugs | Fix the HUD Blinking | Fix the Level Select Graphics Bug | Fix a remember sprite related bug
Changing Design Choices
Change Spike Behavior | Collide with Water After Being Hurt | Fix Special Stage Jumping Physics | Improve the Fade In\Fade Out Progression Routines | Fix Scattered Rings' Underwater Physics | Remove the Speed Cap | Port the REV01 Background Effects | Port Sonic 2's Level Art Loader | Retain Rings Between Acts | Add Sonic 2 (Simon Wai Prototype) Level Select | Improve ObjectMove Subroutines | Port Sonic 2 Level Select
Adding Features
Add Spin Dash ( Part 1 (GitHub)/(Hivebrain) / Part 2 / Part 3 / Part 4 ) | Add Eggman Monitor | Add Super Sonic | Add the Air Roll
Sound Features
Expand the Sound Index | Play Different Songs Per Act | Port Sonic 2 Final Sound Driver | Port Sonic 3's Sound Driver | Port Flamewing's Sonic 3 & Knuckles Sound Driver | Change The SEGA Sound
Extending the Game
Load Chunks From ROM | Add Extra Characters | Make an Alternative Title Screen | Use Dynamic Tilesets | Make GHZ Load Alternate Art | Make Ending Load Alternate Art | Add a New Zone | Set Up the Goggle Monitor | Add New Moves | Add a Dynamic Collision System | Dynamic Special Stage Walls System | Extend Sprite Mappings and Art Limit | Enigma Credits | Use Dynamic Palettes
Miscellaneous
Convert the Hivebrain 2005 Disassembly to ASM68K
Split Disassembly Guides
Set Up a Split Disassembly | Basic Level Editing | Basic Art Editing | Basic ASM Editing (Spin Dash)