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
  • 7 Pages +
  • ◄ First
  • 5
  • 6
  • 7
    Locked
    Locked Forum

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

#91 User is offline Clownacy 

Posted 15 May 2016 - 11:07 AM

  • Layin' the Wax and Spinnin' the Sounds
  • Posts: 616
  • Joined: 06-July 13
  • Gender:Male
  • Location:Englandland
Sonic 2 Clone Driver v2 is now on GitHub!

While still a work-in-progress, there have been some pretty major changes since the last update. One noteworthy change is that music/SFX data like addresses, flags, priorities, and speed shoes tempos, all are grouped together in so-called 'metadata tables', to make adding or changing music/SFX easier. There's also been an attempt to abstract away parts of the driver, to make installation and use easier. For example, PlayMusic, SoundDriverLoad, and other subroutines, have been moved to a separate 'Functions' file in the sound folder, where I can change whatever I want, without the user needing to copy/paste the new version of PlaySoundStereo or whatever into their s2.asm/sonic.asm. Many parts of the driver have also been made 'disassembly-independent', making theoretical ports to S3K and the like easier.

Oh, and there's backwards-compatibility with the official SMPS2ASM, and MegaPCM's DPCM decoder has been sped up a bit. Just in case you were interested in hearing about actual features. Also, speaking of DAC, there's a branch that uses the original Mega PCM, instead of the custom one, in case you want the older version's higher playback speed, even at the cost of a few features.
This post has been edited by Clownacy: 16 May 2016 - 10:28 AM

#92 User is offline Clownacy 

Posted 02 July 2016 - 08:59 AM

  • Layin' the Wax and Spinnin' the Sounds
  • Posts: 616
  • Joined: 06-July 13
  • Gender:Male
  • Location:Englandland
v2.6
It sure has been a while since the last update.

What's changed?
  • New DPCM tables were ported from SMPS-Treasure, speeding up DPCM loop (unlike SMPS-Treasure, these tables are generated by the DAC driver during initialisation, so they don't waste ROM like the volume table)
  • Made Spin Dash toggleable, so its code isn't assembled if you're not using it
  • Fixed bug during song initialisation that caused the last two sound queues to be accidentally cleared
  • Ported S2's version of Sound_Play (zCycleQueue)
  • Music/SFX/SpecSFX data has been merged into 'metadata tables', simplifying their addition or modification (for example, the music metadata table contains the music's pointer, speed-shoes tempo, and playback flags)
  • Restored compatibility with official (flamewing's) SMPS2ASM
  • All tracks use zTrack.VoicePtr now (saves some RAM)
  • Overall RAM cleanup
  • Some Z80 access was made 'safe' (interrupt can't interrupt Z80 bus request, and accidentally deassert it)
  • Fixed OOZ oil slide SFX (was updating FM volume on a PSG channel)
  • Added check to prevent above bug from having any negative effects, just like S2's driver
  • A branch is available that uses the stock v1.1 MegaPCM


There's also been a huge overhaul to the driver's file structure, and an attempt has been made to abstract away driver interaction, allowing me to make whatever under-the-hood changes I need, without the user needing to update segments of their s2.asm/sonic.asm themselves. Things such as speeding up the music, queueing a sound, or updating the driver, have all either been split into separate ASM files, or turned into macros. The installation guides have been overhauled for this new setup.

Since the driver's now on GitHub, I don't know if I'll be announcing 'versions' like this anymore. All changes will be pushed to GitHub when they're made, so you can fetch the latest version whenever you feel. For this update, like the others before it, I've done a fair amount of bugtesting, so perhaps it's a choice between stable and unstable releases.
This post has been edited by Clownacy: 02 July 2016 - 09:21 AM

#93 User is offline Clownacy 

Posted 12 August 2016 - 04:30 PM

  • Layin' the Wax and Spinnin' the Sounds
  • Posts: 616
  • Joined: 06-July 13
  • Gender:Male
  • Location:Englandland
v2.7
I'm gonna cut right to the chase.





What's changed?
  • Support for the 32X
  • Support for four PWM tracks
  • Fixed bug where Special SFX incorrectly mute PSG3 while it's being used by SFX (thanks, Markey)
  • Added proper volume cap check to DoFadeIn (thanks again, Markey)
  • Fixed bug where PSG 1&2 are not muted during SFX initialisation (Markey, stop, please)
  • Added a bugfix from S2's driver that stops cfStopTrack from altering the stack too much on DAC channels
  • Tried to reduce name collision with MegaPCM by adding a prefix to most labels
  • UpdateDAC has been restructured, so its stack usage is identical to FM and PSG (kind of undoing that last bugfix :(/> )
  • Made MegaPCM check that YM2612 isn't busy before enabling DAC (fixes muted drums at start of some songs)
  • SFX now properly update on the frame that a new song is played


So... yeah, PWM playback. I've gotten most of the ranting out of my system in the description of those YouTube videos, but I'll still list some details here (keep in mind, my terminology might be incorrect):

The SH2-based PWM driver itself is ripped straight out of Knuckles' Chaotix. It software-mixes four PWM channels, with stereo panning support, and a maximum sample rate of 11025Hz (which is interpolated to 22050Hz). This puts it at a bit of a disadvantage over the stock DAC driver from the likes of S1, whose Snare sample has a rate of roughly 22kHz, and the driver fully supports it. In that top video, I actually had to downsample the Snare to get it to play at full speed, and there's a noticeable quality drop. I might be able to modify the driver to remove that limitation at some point, and maybe even add support for more than 4 channels, but for now the PWM driver is entirely vanilla.

The PWM code can easily be 'plugged in' to an existing 32X project: on the SH2 side, the driver just needs to be called on every PWM interrupt, and the driver itself needs loading into the Cache on boot. To demonstrate this, I've included a 'stub SH2' program, based on Gardeguey's SH2 code (available as part of his Sonic 1 32X port), which only runs the PWM driver.

As you can see in the top video, also included is rudimentary support for PWM SFX: simply call the SMPS_PlayPWMSample function, and you can play any sample you want, at any volume/pan you want, on any channel you want. Right now, there's no priority system, so make sure to not play an SFX on a channel the music is using.

Also, yes, as the second video shows, Chaotix music can now be ported to the driver. But don't just use my driver because it's easy to port music to; it's 68k-based for crying out loud. Anyway, to dump Chaotix songs, I have a rough edit of SMPS2ASM here. A pre-converted set of Chaotix music is included with the driver.

On a related note, I've had to expand SMPS2ASM's format to support PWM tracks, while maintaining backwards compatibility with non-32X songs:

Standard SMPS2ASM header
Mus82_LZ_Header:
	smpsHeaderStartSong 1
	smpsHeaderVoice     Mus82_LZ_Voices
	smpsHeaderChan      $06, $03
	smpsHeaderTempo     $02, $06

	smpsHeaderDAC       Mus82_LZ_DAC
	smpsHeaderFM        Mus82_LZ_FM1,	$F4, $0C
	smpsHeaderFM        Mus82_LZ_FM2,	$E8, $0D
	smpsHeaderFM        Mus82_LZ_FM3,	$F4, $18
	smpsHeaderFM        Mus82_LZ_FM4,	$F4, $18
	smpsHeaderFM        Mus82_LZ_FM5,	$00, $12
	smpsHeaderPSG       Mus82_LZ_PSG1,	$D0, $02, $00, fTone_09
	smpsHeaderPSG       Mus82_LZ_PSG2,	$D0, $02, $00, fTone_09
	smpsHeaderPSG       Mus82_LZ_PSG3,	$00, $02, $00, fTone_04



Expanded SMPS2ASM 32X header
DoorIntoSummer_Header:
	smpsHeaderStartSong 3
	smpsHeaderVoice     DoorIntoSummer_Voices
	smpsHeaderChan      $07, $03, $04
	smpsHeaderTempo     $01, $25

	smpsHeaderDAC       DoorIntoSummer_DAC
	smpsHeaderFM        DoorIntoSummer_FM1,	$00, $09
	smpsHeaderFM        DoorIntoSummer_FM2,	$0C, $10
	smpsHeaderFM        DoorIntoSummer_FM3,	$0C, $10
	smpsHeaderFM        DoorIntoSummer_FM4,	$00, $10
	smpsHeaderFM        DoorIntoSummer_FM5,	$00, $10
	smpsHeaderFM        DoorIntoSummer_FM6,	$00, $10
	smpsHeaderPSG       DoorIntoSummer_PSG1,	$F4, $00, $00, KCVolEnv_0C
	smpsHeaderPSG       DoorIntoSummer_PSG2,	$F4, $03, $00, KCVolEnv_0C
	smpsHeaderPSG       DoorIntoSummer_PSG3,	$23, $01, $00, KCVolEnv_02
	smpsHeaderPWM       DoorIntoSummer_PWM1,	$00, $99
	smpsHeaderPWM       DoorIntoSummer_PWM2,	$00, $AA
	smpsHeaderPWM       DoorIntoSummer_PWM3,	$00, $99
	smpsHeaderPWM       DoorIntoSummer_PWM4,	$00, $99


First, there is the new smpsHeaderPWM (which just redirects to smpsHeaderFM, since they have the same variables), which is straight from Chaotix. And a new new feature, that isn't from Chaotix, is the added third value for smpsHeaderChan: the PWM channel count. Chaotix was hardcoded to expect every song to have four channels, which would be a pain to work around. Instead, a song has however many tracks you say it has. It's completely safe to leave the PWM count blank, instead of setting it to $00, like in a normal header. Support for removing the DAC track, however, has not been ported (in fact, the modified SMPS2ASM just inserts dummy DAC data into Chaotix dumps, for compatibility).

Do note, I haven't tested this thoroughly on hardware, since I don't own a 32X myself. But if someone would like to test it for me, I'd appreciate it (don't mind that all levels but GHZ crash, I just stripped out a lot of other zones' data while porting). The PWM's too quiet on Gens Rerecording (emulator bug?), and a separate possible emulator bug in Gens/GS causes the left speaker to be muted until the first PWM sample is played. I did get MarkeyJester to hardware-test for this specific bug (betcha didn't know what that ROM was for, at the time ;P), and it didn't seem to occur there, which is why I think these all are emulator things. PWM works fine in Kega, though.

The Compatibility flags SMPS_IsOn32X and SMPS_EnablePWM have been added, to enable all this. SMPS_IsOn32X is separate because it fixes Mega PCM to work on the 32X, while SMPS_EnablePWM, well, enables PWM. Just in case you wanted to go the DOOM route, and be on the 32X, and not use the PWM for... some reason.

For anyone who does use the 32X, you'll have to include more than one file this time: include "sound/Sonic 2 Clone Driver v2.asm" should still be done, and contains all the 68k and Z80 stuff. However, you will need to do 'include "sound/Sonic 2 Clone Driver v2 - PWM Driver.asm" and (if you're using the stub 32X program) include "sound/Sonic 2 Clone Driver v2 - Stub SH2.asm", also. This is so the SH2 stuff and 68k/Z80 stuff can be moved around separately, to fit your bank layout.

As for the Stub 32X program itself, its ROM address is defined as 'SH2_Start', its length is 'SH2_Length', and the entry points and VBRs are as follows:

		dc.l SH2_Entry					; Master SH2 initial PC
		dc.l s_EntryPoint				; Slave SH2 initial PC
		dc.l SH2_Master					; Master SH2 initial VBR address
		dc.l SH2_Slave					; Slave SH2 intitial VBR address


Aaaand... yeah. I think that's all.
This post has been edited by Clownacy: 12 August 2016 - 05:25 PM

#94 User is offline kazblox 

Posted 12 October 2016 - 08:55 PM

  • Posts: 107
  • Joined: 20-August 14
  • Gender:Male
  • Project:Sonic 3?
Why no love and support for the current version of Project Sonic 1 Two-Eight? It has no AS version, which sort of blocks me to switching to this.
This post has been edited by kazblox: 19 October 2016 - 11:34 AM

#95 User is offline Clownacy 

Posted 13 October 2016 - 03:01 AM

  • Layin' the Wax and Spinnin' the Sounds
  • Posts: 616
  • Joined: 06-July 13
  • Gender:Male
  • Location:Englandland
And that's my problem how? AS has been a requirement since literally day 1. I can't just conjure an asm68k version out of thin air. If you knew how to use Git, you'd know it's easy to combine the AS and Two-Eight branches, anyway.
This post has been edited by Clownacy: 13 October 2016 - 07:00 AM

#96 User is offline Caverns 4 

Posted 13 October 2016 - 02:25 PM

  • Posts: 321
  • Joined: 07-December 12
  • Gender:Male
  • Project:Sanik Quest: Journey To The Right
I Love what you've done with the Driver!

Unfortunately, I've kinda abandoned the thought of a 68k Driver, since I'm now heavily concerned about performance over audio quality, but if 32x support was more widespread, I'd love to incorperate something like this.

So, Knockles Chaotix uses a 68k sound driver then, or is that on the SH processors?

#97 User is offline Clownacy 

Posted 13 October 2016 - 03:39 PM

  • Layin' the Wax and Spinnin' the Sounds
  • Posts: 616
  • Joined: 06-July 13
  • Gender:Male
  • Location:Englandland
Neither? Chaotix uses the same type of driver as S3K. Made by the same person too, I think. Z80 handles FM and PSG playback, while the SH-2 does PWM. All the 68k does is act as a relay between the two.

#98 User is offline MarkeyJester 

Posted 14 October 2016 - 07:11 AM

  • Full of surprises, pull the ribbons for details~
  • Posts: 1758
  • Joined: 22-July 08
  • Gender:Male
  • Location:Japan
  • Wiki edits:16

View PostCaverns 4, on 13 October 2016 - 02:25 PM, said:

I Love what you've done with the Driver!

Unfortunately, I've kinda abandoned the thought of a 68k Driver, since I'm now heavily concerned about performance over audio quality, but if 32x support was more widespread, I'd love to incorperate something like this.

So, Knockles Chaotix uses a 68k sound driver then, or is that on the SH processors?

I wouldn't worry too much about performance in relation to the sound driver.

There was a big hoo-har a year or two back where the argument for using a z80 driver over a 68k driver was, CPU speed/performance. After heavy tests, the average total game time the sound driver took, was in fact 3.6% of the CPU. If you have so much going on that the game will start lagging with the 68k driver, then there's a huge chance that the game will still lag with the driver removed, since it's a really small window of performance improvement.

This is not to deter you from moving towards a z80 driver however, I do recommend it, but not for performance reasons, you also free up about 3% of RAM in the process (which is roughly 600-700 bytes), 3% of RAM is more beneficial to you than 3% of CPU speed, lag is an acceptable by-product of games for the Mega Drive, people will understand. It does come at a cost of course, the cost of digital audio quality, no matter how well the z80 driver is written, there will always be gaps in the PCM playback causing distortions, but some do a pretty good job considering. The issue should be whether or not you need the RAM space, if you need better performance, your efforts and time are better spent improving other aspects of the engine, you'll get more CPU time gain from that.

#99 User is offline Caverns 4 

Posted 18 October 2016 - 01:16 AM

  • Posts: 321
  • Joined: 07-December 12
  • Gender:Male
  • Project:Sanik Quest: Journey To The Right
You make a strong argument for the 68k - I know RAM space is very important, but it's not really within the scope of my current project to do anything to necessitate the.. $6A0 bytes, I believe, of RAM. Right now, the $500 empty bytes from vanilla Sonic 2 are unused in my hack as is, unless I use the build option to record demos anyway.

Knowing that, and the fact that the loss of quality of DAC/PCM using a Z80 continually annoys my OCD tendencies, I probably am better off using a 68k Driver, like the Clone Driver here... But I'm still not sure about that.

Going back to what Clownacy said, I'm kind of surprised by that. It seems like given the power of the SH processors, it would've made total sense to use a 68k driver, since they wouldn't have to worry about it being bottlenecked as easily... But it is what it is, and I'm not a professional programmer, so there's probably a reason for it I'm not aware of.

EDIT: I just looked at the main post again.. It only needs $39B bytes now. That works for me!
This post has been edited by Caverns 4: 18 October 2016 - 01:50 AM

#100 User is offline Clownacy 

Posted 18 October 2016 - 08:00 AM

  • Layin' the Wax and Spinnin' the Sounds
  • Posts: 616
  • Joined: 06-July 13
  • Gender:Male
  • Location:Englandland
Uhh, Chaotix only uses the SH2 for PWM, and 32X graphics. The game engine still runs on the 68k.

#101 User is offline redquebec 

Posted 19 October 2016 - 10:00 AM

  • Posts: 37
  • Joined: 29-April 14
Once again, thanks Clownacy for the hard work. Been using your driver in personal projects, love it.

I have a question for you. I've been trying to create accurate sample to use with your driver. But I have a hard time getting the sample rate right, I'm usually near, but it's far from correct. In "MegaPCM - DAC Table.asm", there are the samples pitch declared, am I right? I'd like to know if there is a formula that could translate easily from sample (Hz) into S2CDv2 pitch accurately. Thank you.
This post has been edited by redquebec: 19 October 2016 - 10:05 AM

#102 User is offline kazblox 

Posted 19 October 2016 - 11:38 AM

  • Posts: 107
  • Joined: 20-August 14
  • Gender:Male
  • Project:Sonic 3?

View PostClownacy, on 13 October 2016 - 03:01 AM, said:

And that's my problem how? AS has been a requirement since literally day 1. I can't just conjure an asm68k version out of thin air. If you knew how to use Git, you'd know it's easy to combine the AS and Two-Eight branches, anyway.


Relax, relax, I wasn't trying to anger nor blame anything on anyone. I could manually convert it, but I don't know if there would be any nuisances that require a change of the syntax. Though I could look at the current AS conversion myself to see what needs to be changed, so why am I still talking about this right now?
This post has been edited by kazblox: 19 October 2016 - 11:38 AM

#103 User is offline Clownacy 

Posted 19 October 2016 - 03:20 PM

  • Layin' the Wax and Spinnin' the Sounds
  • Posts: 616
  • Joined: 06-July 13
  • Gender:Male
  • Location:Englandland

View Postredquebec, on 19 October 2016 - 10:00 AM, said:

Once again, thanks Clownacy for the hard work. Been using your driver in personal projects, love it.

I have a question for you. I've been trying to create accurate sample to use with your driver. But I have a hard time getting the sample rate right, I'm usually near, but it's far from correct. In "MegaPCM - DAC Table.asm", there are the samples pitch declared, am I right? I'd like to know if there is a formula that could translate easily from sample (Hz) into S2CDv2 pitch accurately. Thank you.

According to a formula ValleyBell gave me, the way to get a playback speed from a pitch value is (3579545*2)/(244+(26*(X-1))), with X being the pitch value. That said, to get a pitch from a playback speed in Hz, you'd do ((((3579545*2)/Hz)-244)/26)+1. Note that these values are specific to my driver's custom Mega PCM driver, in DPCM mode.

Edit: https://github.com/C...a11bf7692aa2147 ;)
This post has been edited by Clownacy: 19 October 2016 - 05:42 PM

#104 User is offline redquebec 

Posted 19 October 2016 - 05:52 PM

  • Posts: 37
  • Joined: 29-April 14

View PostClownacy, on 19 October 2016 - 03:20 PM, said:



My man! :)

  • 7 Pages +
  • ◄ First
  • 5
  • 6
  • 7
    Locked
    Locked Forum

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