Sonic and Sega Retro Message Board: Sonic 1 "Mega PCM" driver - Sonic and Sega Retro Message Board

Jump to content

Hey there, Guest!  (Log In · Register) Help
Loading News Feed...
 

Sonic 1 "Mega PCM" driver A new DAC driver for Sonic 1, version 1.1 is out!

#1 User is offline vladikcomper 

Posted 11 June 2012 - 10:16 AM

  • Posts: 165
  • Joined: 13-June 09
  • Gender:Not Telling
  • Location:Russia
  • Project:Sonic Winter Adventures
  • Wiki edits:1
05-Aug-2012 - Version 1.1 is out! Check out this post for the info (clickie). Links in this post are also up to date.
28-Oct-2012 - New guide: "How to increase playback quality with Mega PCM".
03-Mar-2013 - Added "Fixing SEGA PCM playback" section into the "How to increase playback quality with Mega PCM" guide.


* * *

"Mega PCM" is my new DAC driver for Sonic 1, coded from the ground up. We already have JMan's and S1HL's custom drivers, which provided us with a lot of good possibilities over the native Sonic 1's DAC driver. I personally used JMan's driver all the time, finding it the best for my purposes. However, like most Z80 drivers, it had some limits and quirks, which were impossible to overpass. All these 'quirks' are actually caused by the hardware design: the Z80 is very limited in accessing ROM sections (this is done via a small 32 KB window) and the processor has to do all DAC timing itself, which adds many difficulties into programming it.

In my driver, I attempted to get rid of some unpleasant limits most of Z80 drivers usually have. I also aimed to code it in the most optimized way to keep playback rates high, while the code is overloaded with a lot of logic I have to program to make overpassing limits possible. To my surprise, I succeded, Mega PCM currently plays sound faster than YM's maximum output frequency (~27 kHz), so there is some room remains for future additions ;)

The features

  • Automatic bank-switching
    Forget about the banks, put your samples where you like, how you like. You no longer have to align samples on 32 KB boundary and care if they cross the boundary.

  • Unlimited sample size
    Samples are no more limited to 32 KB. "Mega PCM" is capable of playing samples of absolutely any size, as long as it can fit your ROM space.

  • Two sound formats supported
    These are 4-bit DPCM and 8-bit PCM.
    The first format was widely used by Sonic 1 to Sonic 3K for DAC samples as it takes only half of the space a normal PCM sound would. 8-bit PCM, however, is the 'native' format for Sega's YM2612 chip, it takes more space but provides a better sound quality.

  • Extended playback controls: Stop, Pause, Loop, Priority
    "Mega PCM" can pause and continue sample playback, so if you play a long sample it won't be cut off after you pause the game.
    You can also loop samples (good for DAC-songs) and can tell "Mega PCM" not to overwrite some samples (good for in-game voice clips).

  • DAC panning
    You can store your sample to play in Left or Right headphone only.

  • Up to $5F DAC samples allowed
    For instance, JMan's driver allowed $1F DAC sample due to its usage of 8-bit pointers in the code. However, in reality DAC table size is only limited by the RAM size, but the SMPS only allows up $5F different DAC samples (slots $81-$CF).


How-to Install "Mega PCM"

If you got interested in trying it out, here we go!
In this guide, I'll tell you how to put Mega PCM driver into your hack, how to improve SMPS slightly to suit new driver's needs and go wild with including new DACs!
This guide also includes bonus: "How-to play voices in-game" (see below)

First off, download this: https://dl.dropbox.c...aPCM_v11_Bin.7z
This is almost ready to use "Mega PCM" setup with Sonic 3 samples set (except for timpani, S3's doesn't fit well here). Copy all files from my archive into your hack's folder and we'll start.

The guide is for Hivebrain's Disassembly.

1. Replacing old driver and driver loading routine

Open Sonic1.asm and find 'Kos_Z80' label, you'll see the following code:

Kos_Z80:	incbin	sound\z80_1.bin
		dc.w ((SegaPCM&$FF)<<8)+((SegaPCM&$FF00)>>8)
		dc.b $21
		dc.w (((EndOfRom-SegaPCM)&$FF)<<8)+(((EndOfRom-SegaPCM)&$FF00)>>8)
		incbin	sound\z80_2.bin
		even


Delete everything, and put this instead:

	include	'MegaPCM.asm'


Now, goto 'SoundDriverLoad' routine and replace all the code with this:

SoundDriverLoad:			; XREF: GameClrRAM; TitleScreen
		nop
		move.w	#$100,d0
		move.w	d0,($A11100).l
		move.w	d0,($A11200).l
		lea	(MegaPCM).l,a0
		lea	($A00000).l,a1
		move.w	#(MegaPCM_End-MegaPCM)-1,d1

	@Load:	move.b	(a0)+,(a1)+
		dbf	d1,@Load
		moveq	#0,d1
		move.w	d1,($A11200).l
		nop
		nop
		nop
		nop
		move.w	d0,($A11200).l
		move.w	d1,($A11100).l
		rts
; End of function SoundDriverLoad


2. Editing SMPS to work with the driver

Find 'sub_71B4C' label and scroll down until you see this part:

		btst	#7,($A01FFD).l
		beq.s	loc_71B82
		move.w	#0,($A11100).l	; start	the Z80
		nop	
		nop	
		nop	
		nop	
		nop	
		bra.s	sub_71B4C
; ===========================================================================

loc_71B82:


Delete or comment out this code.

Spoiler


Then, goto 'loc_71C88' label, find and delete these lines:

		btst	#3,d0
		bne.s	loc_71CAC


Spoiler


Next, find 'loc_71C44' label and in the very beginning add this code:

		move.b	($A04000).l,d2
		btst	#7,d2
		bne.s	loc_71C44
		move.b	#$2A,($A04000).l


Spoiler


The following changes will make "Mega PCM" pause playback when the game pauses and continue it when the game unpauses.

Find 'loc_71E7C' and at the very end, just before BRA, add this line:

		move.b	#$7F,($A01FFF).l; pause DAC


Spoiler


Next, find 'loc_71EDC' and replace all the code until label 'loc_71EFE' with this:

loc_71EDC:
		adda.w	d3,a5
		dbf	d4,loc_71EC4

		lea	$340(a6),a5
		btst	#7,(a5)
		beq.s	@UnpauseDAC
		btst	#2,(a5)
		bne.s	@UnpauseDAC
		move.b	#-$4C,d0
		move.b	$A(a5),d1
		jsr	sub_72722(pc)

@UnpauseDAC:
		move.b	#0,($A01FFF).l	; unpause DAC


Spoiler


And the last touch, let's make playback stop properly (not only mute) when SMPS stops the sound.
Goto 'loc_725B6' (a part of 'Sound_E4' code) and before the 'bra.w sub_729B6' add this line:

		move.b	#$80,($A01FFF).l ; stop DAC playback


Spoiler


That's it!

How-to use "Mega PCM"

Open MegaPCM.asm. This file includes compiled "Mega PCM" Z80-code and stores table of samples to play. To make working with driver easier, I added some constants and macros to setup table and include samples.

Scroll down in the file until you see this:

; ---------------------------------------------------------------
; DAC Samples Table
; ---------------------------------------------------------------

	DAC_Entry	$08, Kick, dpcm			; $81	- Kick
	DAC_Entry	$08, Snare, dpcm		; $82	- Snare
	DAC_Entry	$1B, Timpani, dpcm		; $83	- Timpani
	dc.l	0,0					; $84	- <Free>
	dc.l	0,0					; $85	- <Free>
	dc.l	0,0					; $86	- <Free>
	dc.l	0,0					; $87	- <Free>
	DAC_Entry	$12, Timpani, dpcm		; $88	- Hi-Timpani
	DAC_Entry	$15, Timpani, dpcm		; $89	- Mid-Timpani
	DAC_Entry	$1B, Timpani, dpcm		; $8A	- Mid-Low-Timpani
	DAC_Entry	$1D, Timpani, dpcm		; $8B	- Low-Timpani

MegaPCM_End:

; ---------------------------------------------------------------
; DAC Samples Files
; ---------------------------------------------------------------

	IncludeDAC	Kick, bin
	IncludeDAC	Snare, bin
	IncludeDAC	Timpani, bin
	even


First block is a DAC samples table, storing what sample to play and how to play it. It will be loaded to Z80 memory along with "Mega PCM" code.

Second block includes sound files into your ROM. 'IncludeDAC' macro searches for file with given name and extension in 'dac' folder, which you've added to your hack among with other files.

For example,
	IncludeDAC	Kick, bin

will include 'dac\kick.bin' file.

The macro includes all the files 'as is', except for files with 'wav' extension. For WAV-files, the macro cuts off the header, or you will hear a short noise when play them.

BIN-files usually used for DPCM format. The fact "Mega PCM" plays this format means you may rip DACs from any Sonic game and play it without conversion to PCM, which makes file two times heavier, but doesn't help with the quality. If you wish to convert PCM sound to DPCM, you can use JMan's compressor: http://selbi.se.funp...e/s1sndcomp.exe

WAV-files should use "8-bit unsigned PCM" format. PCM sound can be also presented with 'RAW' extension (raw sound without header), some trackers support it.

Sound frequency can be up to ~30 kHz for PCM and ~32 for DPCM (Yeah, suddenly the more complex format got a higher playback rate, seems my optimized code is way fast. Actually, the fact it processes 2 DAC writes per loop helps a lot.)
But actually, YM2612 can't play DAC faster than ~27 kHz (it will lead to missed writes). However, emulators don't emulate this and if you don't care about playback quality on real hardware, go wild.

DAC samples table uses 'DAC_Entry' macros to store sample, which has the following format:
DAC_Entry <pitch>, <sample name>, <flags>

The real 8-byte format used by "Mega PCM" you can see in the macro's definition.

Pitch sets how fast the sample sound should play. The lower pitch is, the faster it will play.

Sample Name refers to sample name as defined by 'IncludeDAC' macro.

Flags field accepts the following flags:
* pcm - selects PCM format
* dpcm - selects DPCM format
* loop - makes sample loop itself
* pri - priority flag, playback won't be interrupted if new sample requested. Playback will respond only to stop/pause commands in this mode.
* panL - play in Left headphone only
* panR - play in Right headphone only
* panLR - play in both headphones

You can combine flags like this: dpcm+panL+loop+pri

Note: if no pan flags were set, "Mega PCM" won't change DAC panning, it will leave setting by SMPS.

Bonus: How-to play voices in game

Here is a small example of using "Mega PCM" to play voice clips during the game.

Download this clip: https://dl.dropbox.c...7401/V_Hurt.wav
Move it to 'dac' folder in you disassembly.

In the very end of MegaPCM.asm file, just before the 'even', add:
	IncludeDAC	V_Hurt,wav


Now we should add it in one of DAC table's slot, take slot $84 for example, it's free. We'll need "Mega PCM" to play this as PCM sound with high priority (or else it can be interrupted by another DACs in BGM).
Modify the table so it will look like this:

	DAC_Entry	$08, Kick, dpcm			; $81	- Kick
	DAC_Entry	$08, Snare, dpcm		; $82	- Snare
	DAC_Entry	$1B, Timpani, dpcm		; $83	- Timpani
	DAC_Entry	$07, V_Hurt, pcm+pri		; $84	- 'Hurt' voice
	dc.l	0,0					; $85	- <Free>
	dc.l	0,0					; $86	- <Free>
	dc.l	0,0					; $87	- <Free>
	DAC_Entry	$12, Timpani, dpcm		; $88	- Hi-Timpani
	DAC_Entry	$15, Timpani, dpcm		; $89	- Mid-Timpani
	DAC_Entry	$1B, Timpani, dpcm		; $8A	- Mid-Low-Timpani
	DAC_Entry	$1D, Timpani, dpcm		; $8B	- Low-Timpani


Done! Sample is now playable. However, we must program the game to play it. In order to do this, find 'PlaySound' routine and add a new routine above:

; ---------------------------------------------------------------------------
; Subroutine to	play a DAC sample
; ---------------------------------------------------------------------------

PlaySample:
	move.w	#$100,($A11100).l	; stop the Z80
@0	btst	#0,($A11100).l
	bne.s	@0
	move.b	d0,$A01FFF
	move.w	#0,($A11100).l
	rts


This routine will allow you to play DAC samples in game. Our new sample is $84, let's play it when Sonic's hurt.

Goto 'Hurt_Sound' and replace
	jsr	(PlaySound_Special).l

with
		moveq	#$FFFFFF84,d0
		jsr	PlaySample

That's it. Enjoy!

"Mega PCM" source code

"Mega PCM" has an open source code, enjoy!

Version 1.1 Source Code: https://dl.dropbox.c...1_SourceCode.7z
Version 1.0 Source Code: https://dl.dropbox.c...0_SourceCode.7z

I accept any ideas on improving driver's core, bug reports etc. You are free to modify driver for your purposes or create something new of it.
This post has been edited by vladikcomper: 03 March 2013 - 07:25 AM

#2 User is offline Bareirito 

Posted 11 June 2012 - 04:07 PM

  • Posts: 102
  • Joined: 20-October 08
  • Gender:Male
  • Location:Argentina
  • Project:Sonic - Mystery of the Chaos Emeralds
  • Wiki edits:4
While, I'm not in the hacking mood (yet), I must say this is epic and I wonder why people doesn't get interested on this! This is wicked stuff! :)

#3 User is offline vladikcomper 

Posted 11 June 2012 - 04:07 PM

  • Posts: 165
  • Joined: 13-June 09
  • Gender:Not Telling
  • Location:Russia
  • Project:Sonic Winter Adventures
  • Wiki edits:1
Update: Just found a small bug in "How-to play voices in-game" section.
Playback flags were not set correctly in my example.

Fixed.

#4 User is offline ashthedragon 

Posted 11 June 2012 - 05:16 PM

  • Dragon Team
  • Posts: 1038
  • Joined: 23-February 08
  • Gender:Female
  • Location:Spain
  • Project:Sonic Paradise & Sonic Ages
Although I don't have the skills to use it, I think this is an absolutely brillant work! Very well done! It surely will make a lot of good to the community, and I hope lots of hacks make use of it

#5 User is offline Hitaxas 

Posted 11 June 2012 - 05:31 PM

  • SEGA: Sorry Classic Sonic, we are sending you back to 1994
  • Posts: 1370
  • Joined: 30-September 07
  • Gender:Male
  • Location:McCalla, AL
  • Project:Sonic: Super Deformed (head director) - Slowly working on it.
  • Wiki edits:196
This custom driver setup is awesome! I tested it out on a fresh Sonic 1 hack, messed around with adding voices to Sonic. Impressive. I might consider switching to this driver, currently I use the S1HL driver with tons of samples added.


Edit: I failed at reading the opening paragraph. This is a custom driver, not a modification of the original.

Edit2: I did find an issue with this driver. The first act of a level, everything works fine, music and voices. However, the dac turns to static when the next act loads.
This post has been edited by Hitaxas: 11 June 2012 - 06:19 PM

#6 User is offline PsychoSk8r 

Posted 12 June 2012 - 02:16 AM

  • HighKnights
  • Posts: 2567
  • Joined: 11-July 07
  • Gender:Male
  • Location:Walsall, UK
  • Project:30 Day Project: Revisited.A New Release!
  • Wiki edits:19
Really like the idea of the driver, but not too keen on the sound of the glitch above. Can anyone else confirm the glitch also? (I'm too damn lazy, and busy. Let's call it "Efficient"). =P

#7 User is offline vladikcomper 

Posted 12 June 2012 - 07:47 AM

  • Posts: 165
  • Joined: 13-June 09
  • Gender:Not Telling
  • Location:Russia
  • Project:Sonic Winter Adventures
  • Wiki edits:1
Thanks for the replies :)

View PostHitaxas, on 11 June 2012 - 05:31 PM, said:

I did find an issue with this driver. The first act of a level, everything works fine, music and voices. However, the dac turns to static when the next act loads.

Weird, something like this never happened when I tested it on a blank disassembly. I've just done some random tests and still no glitches happen.
It's hard to say if it's bug of the driver or not without seeing it. This can be something wrong with SMPS or even with your code (constant noise will happen if you keep sending driver DAC requests in a loop, I.e. if code to play samples works every frame).
This post has been edited by vladikcomper: 12 June 2012 - 07:48 AM

#8 User is offline PsychoSk8r 

Posted 12 June 2012 - 01:19 PM

  • HighKnights
  • Posts: 2567
  • Joined: 11-July 07
  • Gender:Male
  • Location:Walsall, UK
  • Project:30 Day Project: Revisited.A New Release!
  • Wiki edits:19
Well, that was exactly the info I wanted. Cheers! Gonna download this, and have some fun testing it. :D

#9 User is offline Aerosol 

Posted 12 June 2012 - 01:40 PM

  • FML
  • Posts: 6381
  • Joined: 27-April 08
  • Gender:Male
  • Location:New York
  • Project:Sonic (?): Coming summer of 2055...?
Knuckles Chaotix uses 8-bit PCM samples doesn't it? So you're saying that Mega PCM can play samples of this quality in the genesis games now?

#10 User is offline Puto 

Posted 12 June 2012 - 02:03 PM

  • Shin'ichi Kudō, detective.
  • Posts: 1988
  • Joined: 31-July 05
  • Gender:Male
  • Location:Portugal, Oeiras
  • Project:Part of Team Megamix, but haven't done any actual work in ages.
  • Wiki edits:51
We've had support for 8-bit PCM samples with jman's driver for years now. That's not news.

#11 User is offline Aerosol 

Posted 12 June 2012 - 02:16 PM

  • FML
  • Posts: 6381
  • Joined: 27-April 08
  • Gender:Male
  • Location:New York
  • Project:Sonic (?): Coming summer of 2055...?
And evidently, I didn't know that. Thanks for kinda-sorta answering my question, though.

#12 User is offline vladikcomper 

Posted 13 June 2012 - 01:51 PM

  • Posts: 165
  • Joined: 13-June 09
  • Gender:Not Telling
  • Location:Russia
  • Project:Sonic Winter Adventures
  • Wiki edits:1
I should've released it at the very beginning, but was lazy to do it...

A clean Sonic 1 Disassembly with Mega PCM driver included:
Download: https://dl.dropbox.c...with_MegaPCM.7z
If you're not in mood to deal with a massive guide yet, you can check out how it looks like and even play with the driver (it's easy).
If you're not sure if you followed some steps of the guide correctly, you may also check yourself.

Everything is done directly as the guide in the first post says, nothing more. Section 'How-to play voices in-game' is also applied.
For your convenience, I marked all changed lines in SMPS code (section 2 of the guide) with '++' comment, so you will find them easily. All code to be deleted was just commented out.

#13 User is offline Eduardo Knuckles 

Posted 13 June 2012 - 11:47 PM

  • I'm too ugly to gain a girl
  • Posts: 360
  • Joined: 07-January 07
  • Gender:Male
  • Location:Sâo Paulo, Brazil
  • Project:Sonic & Bean In Eggland, Project S.A.M.G., Vortex Of Time, Odin Games
  • Wiki edits:66

View PostAerosolSP, on 12 June 2012 - 01:40 PM, said:

Knuckles Chaotix uses 8-bit PCM samples doesn't it? So you're saying that Mega PCM can play samples of this quality in the genesis games now?

View PostPuto, on 12 June 2012 - 02:03 PM, said:

We've had support for 8-bit PCM samples with jman's driver for years now. That's not news.
This sound quality was the same since Sonic The Hedgehog 1. I'm not sure if other Genesis games can handle better quality without the need of pitch changing. I think that this is the DAC's default or the maximum quality limitation itself.

View Postvladikcomper, on 13 June 2012 - 01:51 PM, said:

I should've released it at the very beginning, but was lazy to do it...

A clean Sonic 1 Disassembly with Mega PCM driver included:
Download: https://dl.dropbox.c...with_MegaPCM.7z
If you're not in mood to deal with a massive guide yet, you can check out how it looks like and even play with the driver (it's easy).
If you're not sure if you followed some steps of the guide correctly, you may also check yourself.

Everything is done directly as the guide in the first post says, nothing more. Section 'How-to play voices in-game' is also applied.
For your convenience, I marked all changed lines in SMPS code (section 2 of the guide) with '++' comment, so you will find them easily. All code to be deleted was just commented out.
Fabulous job, man. As always, we can expect awesome stuff coming from your hands. To be honest, I got in trouble following the steps of the guide (...because its insertion was the only hard part on it), but the management is a lot easier than the management of the samples in the S1HL driver and also on a non-public driver revision I have. Thanks for post this!
This post has been edited by Eduardo Knuckles: 13 June 2012 - 11:49 PM

#14 User is offline KingofHarts 

Posted 30 June 2012 - 09:39 AM

  • Amigo
  • Posts: 1198
  • Joined: 07-August 10
  • Gender:Male
  • Location:On the way back home to AMERICA!
  • Project:Sonic Triad Studio
  • Wiki edits:1
Fine tune this a little bit more, and I'll post this onto the wiki under the Sound Features section.

#15 User is offline vladikcomper 

Posted 11 July 2012 - 02:34 PM

  • Posts: 165
  • Joined: 13-June 09
  • Gender:Not Telling
  • Location:Russia
  • Project:Sonic Winter Adventures
  • Wiki edits:1


Coming Soon:
  • How to improve sample playback quality
  • How to play digital songs with "Mega PCM"
  • A new version of "Mega PCM" with few improvements and bug-fixes


Note that you can play digital audio tracks even now, as "Mega PCM" can easily manage with these. First post contains enough info to get started, so if you're impatient, feel free to try this out =P

Few words about upcoming 'How to improve sound quality' guide. Due to way Sonic 1 controls Z80, the processor is being stopped quite a lot which means, digital sound stops processing. This drops sound quality quite a lot. In my guide, I'll explain how to modify SMPS and a bit of game engine to reduce Z80 suspend times and achieve near-perfect quality results like in the video shown.
This post has been edited by vladikcomper: 11 July 2012 - 02:42 PM

  • 3 Pages +
  • 1
  • 2
  • 3
    Locked
    Locked Forum

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