Sonic and Sega Retro Message Board: Sonic 2 Clone Driver v2 - Sonic and Sega Retro Message Board

Jump to content

Hey there, Guest!  (Log In · Register) Help
  • 6 Pages +
  • ◄ First
  • 3
  • 4
  • 5
  • 6
    Locked
    Locked Forum

Sonic 2 Clone Driver v2 v2.3 | and other SMPS-related ramblings

#61 User is offline Clownacy 

Posted 06 October 2014 - 08:14 AM

  • Needs to make an avatar
  • Posts: 310
  • Joined: 06-July 13
  • Gender:Male
  • Location:Englandland
v2.2.4
  • Fixed FM6 fade-in
  • Modified cfFadeToPrevious to work on FM and PSG tracks
  • Optimised some locrets (recycling!)
  • Annotated Vladikcomper's modified WriteFMI(I)
  • Removed a startZ80 that I missed back in v2.0
  • Renamed several constants to be more S1-like
  • Restored original local labels (With tweaks for compatibility)
  • Optimised lea Clone_Driver_RAM into .w
  • Optimised 68k version of PlaySega
  • Ported S3K's NoteFill
  • Optimised some 'bsr & bra' into 'pea & bra'
  • Further optimised the waitYM macro by using tst.b instead of btst
  • Optimised all btst #7 to tst
  • Even further optimised waitYM by moving ym2612_a0 to a register
  • Updated channel RAM addresses description
  • Optimised NoteFillUpdate


Hey look, optimisations!

Updating:
You must overwrite _smps2asm_inc.asm with the new one. It contains a new feature.
This post has been edited by Clownacy: 27 February 2015 - 08:21 AM

#62 User is offline MarkeyJester 

Posted 06 October 2014 - 12:10 PM

  • Clouded in obscurity.
  • Posts: 1595
  • Joined: 22-July 08
  • Gender:Male
  • Location:Japan
  • Wiki edits:16
Well, if no one else is gonna take a damn interest, I will...

Congratulations on your fixes and optimisations, how close do you feel to reaching the final goal?

#63 User is offline Clownacy 

Posted 06 October 2014 - 12:51 PM

  • Needs to make an avatar
  • Posts: 310
  • Joined: 06-July 13
  • Gender:Male
  • Location:Englandland
Oh, don't worry, it gets interest. It's just usually behind-the-scenes. I'm sure people would be a bit more vocal if this was on SSRG. At least, the Q&A thread seems to point to that.

I feel I reached my goal back in v2.2.1. That was when I replicated all of S2's features. Since then I've been optimising and fixing bugs (and trying to make S3K songs compatible, but that's never going to happen). My buggery with SMPS is far from over, though: I'm getting familiar with SMPS-PCM, and I'm hoping to rebase the Clone Driver to SMPS 68k type 2.
This post has been edited by Clownacy: 08 October 2014 - 10:12 AM

#64 User is offline Clownacy 

Posted 15 October 2014 - 04:41 PM

  • Needs to make an avatar
  • Posts: 310
  • Joined: 06-July 13
  • Gender:Male
  • Location:Englandland
v2.2.4.1
  • Optimised WriteFMI(I) with a nice trick I picked up from Ristar's driver
  • Optimised some writes to psg_input
  • Optimised all adda.x #x,aN to lea x(aN),aN
  • Optimised FM and PSG's .gotduration by replacing them with SetDuration_pea
  • Added a nop to WaitYM to avoid missed writes
  • Optimised PSG flags by reordering their checks in order of most-to-least common
  • Replaced a 'branch to rts' with an rts
  • Restored the music and SFXs to their 'as-of-2011' state. They shouldn't be modified in the first place
  • Added to _smps2asm_inc.asm to fix DAC typo


Hey, I did it: SMPS 68k is now too fast! First there was the concern about the optimised PlaySega being too fast, and now, I managed to make WriteFMI fast enough to rocket ahead of the YM2612. Anyone still using 2.2.4, try running your hack on hardware to see what I mean. It's fixed here, but still...

I've just about optimised this to the best of my abilities, so I thought I'd wrap these all together before I get to bug-fixing and feature-adding, which are gonna take much longer to do.

Updating:
You must overwrite _smps2asm_inc.asm with the new one. It contains a compatibility fix.
This post has been edited by Clownacy: 27 February 2015 - 08:19 AM

#65 User is offline redquebec 

Posted 16 October 2014 - 09:59 AM

  • Posts: 17
  • Joined: 29-April 14
Thanks again and again Clownacy! :)

#66 User is offline Irixion 

Posted 16 October 2014 - 10:26 AM

  • Posts: 1414
  • Joined: 30-December 04
  • Gender:Male
  • Location:Ontario, Canada
  • Project:Life
  • Wiki edits:152
Very exciting! :) Any planned optimizations? It's fantastic to see the Clone Driver made into something not so broken. I'll probably have a peek at it later when I get home.

#67 User is offline Clownacy 

Posted 16 October 2014 - 01:17 PM

  • Needs to make an avatar
  • Posts: 310
  • Joined: 06-July 13
  • Gender:Male
  • Location:Englandland
As I said, the driver's just about as optimised as I can get it. Not without overhauling major components, that is. But that'll have to wait for when I can effectively work with the hardware. It took me until now to realise how easy it is for the 68k to be too quick for the YM2612, so there's a lot of ground to cover.

I've decided to start cracking down on the bugs in this driver inherited from S1's driver. Those have been nothing but grief since the beginning. For example, play sound $FB, fade music, and then $0A, DEZ's theme. The PSG goes mad. Silver Sonic's reving plays noise, too.

#68 User is offline Super Egg 

Posted 16 October 2014 - 01:38 PM

  • Master of MS Paint.
  • Posts: 144
  • Joined: 01-July 10
  • Gender:Male
  • Location:Tomball, TEXAS
  • Project:Sonic 2 beta 3 hoax, SONIC X ABRIDGED BITCH!!!
  • Wiki edits:46
Funny, I don't have that issue on my S2B z80 driver =P

Jokes aside, nice to see you finally finished this thing. Seems you've beat me in the race of "Who can release an alternative to the Original Clone Driver or Flamewing driver."

#69 User is offline Clownacy 

Posted 04 November 2014 - 12:07 PM

  • Needs to make an avatar
  • Posts: 310
  • Joined: 06-July 13
  • Gender:Male
  • Location:Englandland
Time for more messing around with Sonic 1's driver! There's been a comment in the Clone Driver v2 for some time mentioning this, but I never went into much detail.

Standard SMPS 68k Type 1a/1b/2 has a smaller FM_Notes table (array?) than what's in Sonic 1. This is achieved through additions to FMSetFreq. You can backport this by doing the following:

First, let's study a Type 2's FMSetFreq. Here's Super Shinobi II's (ValleyBell disasm):

GetFMFreq:				; CODE XREF: ProcessFMTrack+20p

; FUNCTION CHUNK AT 00069996 SIZE 00000008 BYTES

		subi.b	#$80,d5
		beq.s	loc_69996
		add.b	8(a5),d5
		andi.l	#$7F,d5
		divu.w	#$C,d5	; new instruction
		swap	d5	; new instruction
		lsl.w	#1,d5
		lea	FMFreqs,a0
		move.w	(a0,d5.w),d6
		swap	d5	; new instruction
		andi.w	#7,d5	; new instruction
		moveq	#$B,d0	; new instruction
		lsl.w	d0,d5	; new instruction
		or.w	d5,d6	; new instruction
		move.w	d6,$10(a5)
		rts
; End of function GetFMFreq


As you can see, there are several new instructions. These use FM_Notes/FMFreqs, not just as raw data, as Sonic 1 does, but as base data, to which the input note is added to, calculating the appropriate octave. Because of this, data for each and every octave is no longer needed, allowing you to get away with a much smaller table, which covers only the one.

Let's see about fitting this in Sonic 1's driver (modified SMPS 68k Type 1b).

FMSetFreq:
		subi.b	#$80,d5			; Make it a zero-based index
		beq.s	TrackSetRest
		add.b	zTrackKeyOffset(a5),d5	; Add track key displacement
		andi.w	#$7F,d5			; Clear high byte and sign bit
		; first set of new instructions goes here
		lsl.w	#1,d5
		lea	FM_Notes(pc),a0
		move.w	(a0,d5.w),d6
		; second set of new instructions goes here
		move.w	d6,zTrackFreq(a5)	; Store new frequency
		rts	
; End of function FMSetFreq


Self explanatory. Next up is replacing Sonic 1's FM_Notes with Super Shinobi II's FMFreqs:

Find FM_Notes and delete all but the first dc.b line. That's all.

Note that this can be optimised a little, not the additions, but the stock code. For starters, the 'lsl.w #1,d5' can be replaced with 'add d5,d5', which is faster. Also, if you move FM_Notes closer to FMSetFreq, you can replace 'lea FM_Notes(pc),a0' and the line after it with 'move.w FM_Notes(pc,d5.w),d6' (in fact, if you didn't add the new code, you could make this line write straight to zTrackFreq(a5) instead of using d6 to do it).

Because this adds cycles to a very central piece of code, I won't be adding this to the Clone Driver v2, which is why I've made this post; if you want to apply this change, you can use this post to do so yourself.

EDIT: Huh. Turns out this isn't a Type 2-only feature. Type 1b and even Type 1a have this. It seems that Sonic Team deliberately made it this way. Weird. Guess they were trying to optimise the code.
This post has been edited by Clownacy: 16 November 2014 - 12:55 PM

#70 User is offline Clownacy 

Posted 18 November 2014 - 03:20 PM

  • Needs to make an avatar
  • Posts: 310
  • Joined: 06-July 13
  • Gender:Male
  • Location:Englandland
These rambling posts should probably be moved to their own topic... if any higher ups think so, don't let me stop you.

A bug fix!

Anyone who's tried to port VVZ act 1's music to S1 or S2's driver will be met with an unusual bug: Some channels lag behind (there's also a bug with notes and rests, but I won't cover that here). The cause is that some coord. flags are used incorrectly. Namely two FM-only flags on a PSG channel. The reason why S3K's driver doesn't suffer from any side-effects is because it has some additional code to deal with this. We'll be porting this code over to both of these drivers, creating a workaround for this error. You could correct the flags in the music itself, but we're adding this for 'catch all' sake.

First, let's examine cfSetPSGTone...

cfSetPSGTone

S1 (modified early(?) SMPS 68k Type 1b):
Spoiler


S2 (modified Z80 port of S1's driver):
Spoiler


S3K (modified SMPS Z80 Type 2):
Spoiler



While S1 and S2 are largely the same, S3K has a check for what channel is using the flag. This needs porting.

S1:
Spoiler


S2:
Spoiler



That's that done. But that's only one of them: we still need to add to the FM flag, cfSetVoice.

cfSetVoice

Again, here's a comparison of the three...

S1:
Spoiler


S2:
Spoiler


S3K:
Spoiler



Once again, S3K has a check (and even a new subroutine, but we don't need that). Let's port this.

S1:
Spoiler


S2:
Spoiler


Done. Wouldn't be surprised if this affects other songs.
This post has been edited by Clownacy: 19 November 2014 - 10:22 AM

#71 User is offline Clownacy 

Posted 26 November 2014 - 11:18 AM

  • Needs to make an avatar
  • Posts: 310
  • Joined: 06-July 13
  • Gender:Male
  • Location:Englandland
More on using a smaller FM frequencies table, this time for S2's driver. Also, info on a strange bug in S3K's driver.

Because of the nature of SMPS Z80 drivers, you may prioritise size over speed. If so, you may want to use a much smaller FM frequencies table, as seen in S3K's driver (modified SMPS Z80 Type 2). Despite being cousins in terms of relation (S2's driver is a Z80 port of an evolved SMPS 68k), S3K's code works near-perfectly in S2's driver, so we'll see about porting across this feature, using the very code S3K uses.

First, you need to follow the driver-related instructions here to relocate the frequency tables, otherwise the DAC playback will be broken. Note that adding the S3K frequencies is optional, and not required for this.

zFMSetFreq, in s2.sounddriver.asm. Above 'add a,a', add this code:

	ld	d, 8			; Each octave above the first adds this to frequency high bits
	ld	e, 0Ch			; 12 notes per octave
	ex	af, af'			; Exchange af with af'
	push	af
	xor	a			; Clear a (which will clear a')

-	ex	af,af'			; Exchange af with af'
	sub	e			; Subtract 1 octave from the note
	jr	c,+			; If this is less than zero, we are done
	ex	af,af'			; Exchange af with af'
	add	a,d			; One octave up
	jr	-			; Loop

+
	add	a,e			; Add 1 octave back (so note index is positive)


(You can find the unmodified version of this code in S3K's driver under zGetNextNote_cont)

This is responsible for the bulk of the calculation. In S2's driver, af' is used by the DAC playback, so we'll need to preserve its contents, as done by the 'push af'.

After 'ld de,(zFrequencies)', add in this code:

	ex	af, af'			; Exchange af with af'
	or	d			; a = high bits of frequency (including octave bits, which were in a)
	ld	d, a			; h = high bits of frequency (including octave bits)
	pop	af
	ex	af, af'			; Exchange af with af'


This code has seen significant modification from its source: using the correct register for the pointer, and restoring af'.

Now navigate to zFrequencies, and remove all but the first line of values.

You should now be clear to build and test. However, you will likely notice one strange bug: the spin dash release sound will play incorrectly. Analysis of S3K's copy of this sound shows that it has been modified from its S2 form. The most notable change is this:

In s2.asm, go to Sound3C. The value '$9000' has been changed to '$0000'. Making this change to your sound will fix the bug. But we will not leave it at that. Let the value remain $9000.

What that modification does is change the FM5 channel's pitch from $90 to $00. This pitch of $90 is unusual, as most other SFXs use values much closer to $00. It is possible that this value is a mistake.

You see, $90 is $10 with the sign bit set. $10 being much closer to $00, and even sounds exactly the same if used instead of $90. What pushes the idea of this being an error further is that, in an unmodified zFMSetFreq, the sign bit would be lost in the calculations. Most obviously at 'add a,a', where the sign bit would be lost to overflow. Our problem is that the code we ported from S3K doesn't have a chance to remove the sign bit, so it counts the value's octave incorrectly, leading to the sound playing incorrectly. While we could change the pitch value of the sound itself, a safer, more effective solution would be to modify S3K's code to remove the sign bit of the value:

Back in zFMSetFreq, above 'ld d, 8', add the instruction 'res 7,a'. This will remove the troublesome bit before the calculations are made. Test this new build to find that the bug has been fixed.

Note that this bug exists in S3K's driver. This can be seen by importing S2's spin dash release sound to the driver, and trying to play it.

#72 User is offline Clownacy 

Posted 29 December 2014 - 12:03 PM

  • Needs to make an avatar
  • Posts: 310
  • Joined: 06-July 13
  • Gender:Male
  • Location:Englandland
A while ago, I pushed the v2.2.4.1b update, which added Sonic 1 support. It wasn't too great, so I kept quiet on it. I intended to quickly replace it with a better one, but got caught up on the whole 'lay off the tiny updates' thing. Two months later, still not much to show off. I guess the best part is better S3K compatibility.

Anyway, v2.2.5:
  • Updated to latest Git (fixing a newly-pointed-out bug along the way)
  • Merged all compatibility layers into s1.sounddriver.compatibility.asm
  • Moved v2.2.4.1b's toggles to s1.sounddriver.compatibility.asm
  • Reverted some dangerous 'moveq's to 'move.b's
  • Optimised a branch under PSGUpdateVolFX
  • Optimised a branch under VolEnv_Reset
  • Removed a branch under VolEnv_Off by moving it above the branch's target
  • Size-optimised MegaPCM, reducing the uncompressed binary's size from 210h to 1E5h
  • Added another nop instruction to waitYM (the beginning of S2's title screen theme plays a goofy note in Regen with just the one)
  • Optimised FMSetFreq by making it write directly from FM_Notes to zTrackFreq
  • Replaced the 'clr.b's under bgmnot1up with a faster moveq/move.b combination
  • Removed the redundant 'clr.b v_sndprio'
  • Optimised Sound_PlayBGM and Sound_PlaySpecial by making them write their voice pointers directly to v_voice_ptr/v_special_voice_ptr (old leftover from v2.1's longword voice pointer hack)
  • Optimised SpeedUpMusic, SpeedUpMusic_1up, SlowDownMusic and SlowDownMusic_1up with some register usage
  • Optimised some zTrackVolume(a5) usage under DoFadeOut
  • Optimised .bgm_psgloadloop by using an addq to skip the 'redundant' byte
  • Optimised Sound_ChkValue by removing the redundant check for Sound Commands
  • bsr -> bra in .specfmdone
  • Renamed all s1.sounddriver files and moved them to the sound folder
  • Changed which v_sndprio clear is used in Sound_PlayBGM (Sonic 2 uses the last one)
  • Added new waitYMspec macro that uses parameter as target for tst
  • Used above modification to optimise WriteFMI
  • Optimised cfSetPSGNoise with better register use
  • Rearranged files at end of driver (Mega PCM, PSGs, music, sounds, others) to put many within 'SoundIndex(pc)' range
  • Fixed oversight in cfNoteFillS3K (d1 isn't loaded if tempo divider = 1), increasing compatibility with S3+ music
  • Added two missing S1 equates
  • Excluded nops from waitYM loop
  • Used more appropriate locret under FMNoteOn
  • Added S3K tweak to cfSetPSGTone and cfSetVoice, increasing compatibility with S3+ music


It seems that for the Xeno disasm users, they'll need to update their copy of AS: when I tried building, I got errors about the local labels.

Updating:
You must overwrite _smps2asm_inc.asm with the new one. You'll need to rename some of your files, also:
  • s1.sounddriver.music.asm - Sonic 2 Clone Driver v2 - Music.asm
  • s1.sounddriver.sounds.asm - Sonic 2 Clone Driver v2 - Sounds.asm
  • s1.sounddriver.other.asm - Sonic 2 Clone Driver v2 - Other.asm

These will need moving to the sound folder. You can delete the other s1.sounddriver.whatever files.

In s2.asm, find the 'include "s1.sounddriver.asm"', and replace it with 'include "sound\Sonic 2 Clone Driver v2 - Compatibility.asm"'.
This post has been edited by Clownacy: 27 February 2015 - 08:20 AM

#73 User is offline Clownacy 

Posted 20 February 2015 - 09:10 PM

  • Needs to make an avatar
  • Posts: 310
  • Joined: 06-July 13
  • Gender:Male
  • Location:Englandland
v2.2.5.1
  • Updated SMPS2ASM


Yay, unintentional coattail riding!

Anyway, this should do well in reversing some, if not all, of the damage done by expanding the PSG frequencies table way back when.

Updating:
You must overwrite _smps2asm_inc.asm with the new one; it's been updated.

Some music and SFXs have been updated, also. Check the timestamp of them, and replace where necessary.

As for updating your own music and SFXs... it seems the only change is the use of nMaxPSG and the sort. This should just amount to searching for, and equating, the correct notes. You can also assemble the files in S1's or one of the others' formats, and running it back through the new SMPS2ASM tool.
This post has been edited by Clownacy: 27 February 2015 - 08:18 AM

#74 User is offline Clownacy 

Posted 04 March 2015 - 09:43 AM

  • Needs to make an avatar
  • Posts: 310
  • Joined: 06-July 13
  • Gender:Male
  • Location:Englandland
v2.2.5.2
  • Added waitZ80 macro, for S1 compatibility
  • Added cfSendFMI
  • Replaced cfOpF9 with above
  • Added cfChanFMCommand
  • Added some flag documentation from S3K and flamewing's driver
  • Better-formatted Sonic 2 Clone Driver v2 - Compatibility.asm
  • Relocated DAC ID equates from _smps2asm_inc.asm to Sonic 2 Clone Driver v2 - Compatibility.asm
  • Made said IDs dynamic, in the same way as the music and SFXs
  • Corrected IDs to account for absent dHipHopHitKick3
  • Added id function to Sonic 2 Clone Driver v2 - Compatibility.asm


Turns out the DAC equates have been broken for a while. Ditching the use of enums, and switching the system S2 uses for its music, should make it so that this never happens again. Also, despite having the same name, the stopZ80 macros in S1 and S2 do not do the same thing. Nice.

Maybe I should actually get to fixing those bugs now...

Updating:
Replace...
  • _smps2asm_inc.asm
  • Sonic 2 Clone Driver v2.asm
  • Sonic 2 Clone Driver v2 - Constants.asm
  • Sonic 2 Clone Driver v2 - Compatibility.asm
  • MegaPCM.asm

This post has been edited by Clownacy: 06 March 2015 - 07:47 PM

#75 User is offline Clownacy 

Posted 09 March 2015 - 05:11 PM

  • Needs to make an avatar
  • Posts: 310
  • Joined: 06-July 13
  • Gender:Male
  • Location:Englandland
v2.2.6
  • Sonic 2-ifications
    • Added to FMUpdateTrack and PSGUpdateTrack the way S2's driver does
    • Made TrackSetRest no longer continue to FinishTrackUpdate
    • Fixed modulation on rests by adding a check to DoModulation (noticeable at start of ARZ's theme after it's looped)
    • Relocated a check in FMPrepareNote to occur a little earlier
    • Changed a reference to a locret in Sound_PlayBGM with one to .bgm_loadMusic, which should make it so that the extra life music can interrupt itself
    • Made .bgmnot1up clear v_fadeout_counter
    • .bmg_fmloadloop and .bgm_psgloadloop now set the 'track at rest' bit, avoiding hanging notes (noticable at start of DEZ's theme)
    • Optimised .silencefm6 using some S2 logic
    • Sound_PlaySFX no longer checks v_fadeout_counter
    • FadeOutMusic no longer calls FadeOutSFX
    • SpeedUpMusic and SlowDownMusic no longer set v_main_tempo_timeout
    • PSGSetVolume now corrects values that are >=$10
    • PSGSetFreq and .restpsg don't continue to FinishTrackUpdate
    • An 'add' in PSGSendVolume is replaced with an 'ori'
    • cfPanningAMSFMS has an additional check
    • Added a (commented out) call to FadeOutSFX in Sound_PlayBGM
  • .nospeedshoes no longer sets v_main_tempo_timeout to the header tempo. This is to avoid unintended tempo overflow on the first frame of playback, delaying the song by a frame. This also helps to prevent hanging notes (noticable at start of DEZ's theme)
  • Removed useless 'even' from under FMDACInitBytes
  • Removed last '0's from FMDACInitBytes and PSGInitBytes (were actually 'even's)


Finally, the bugs are gone.

Anyone who's used previous versions of this driver, even v2.0, may have noticed that the start of DEZ's theme has this strange tone. They might also have noticed that the start of ARZ's theme, after already looping once, has these extremely high-pitched noises in the right channel. With this version, they're finally gone. The result of a pair of bugs in S1's driver: modulation on rests, and hanging notes. In lieu of an actual explanation, the changelog should give you the pointers to where the fixes are.

If you want to hear the bugs, check this ROM.

Updating:
Replace...
  • _smps2asm_inc.asm
  • Sonic 2 Clone Driver v2.asm

This post has been edited by Clownacy: 09 March 2015 - 05:14 PM

  • 6 Pages +
  • ◄ First
  • 3
  • 4
  • 5
  • 6
    Locked
    Locked Forum

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users