Making the ultimate Sega Genesis sound driver

For anything related to sound (YM2612, PSG, Z80, PCM...)

Moderator: BigEvilCorporation

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Making the ultimate Sega Genesis sound driver

Post by Stef » Thu Jun 21, 2012 9:53 am

The title of the topic is kinda exaggerated but the idea is there :)
SGDK contains some Z80 sound drivers but none of them can be really used as a "in game" sound drivers... PCM drivers support PCM only (without FM nor PSG) which made them not really suited and TFM as MVS does not support easy SFX playing.

What i want to do is to make a new driver supporting both play music as play SFX commands, also i want to provide more than the usual 6 FM + 4 PSG or 5 FM + 1 PCM + 4 PSG.

For instance i think it is very possible to do 5 FM + 4 PCM + 4 PSG.
For that PCM would be fixed at 8 bits @16 Khz and 256 bytes aligned for instance. Also the Z80 would only handle FM registers and PSG registers writes without any complex format decoding.

I believe VGM is already working this way (only give writes command for FM and PSG + PCM stuff) it's why i would like to stay on this "standard" format. I think VGM can actually support that type of driver (i.e. 5 FM + 4 PCM + 4 PSG) as you can specify severals PCM sources and formats but Shiru or Delek are better placed than me to reply that question.

So initially the idea of this topic is to discuss about what could be the ultimate Sega Genesis sound driver to gives the system the best music we could have then try to implement it :)

- The driver has to run entirely on Z80, the 68k CPU pass basic command as "play/stop music" or "play sfx on channel x".
- The driver should permit usage of 6 FM / 5 FM + DAC and 4 PSG.

For me the big of the discussion is about PCM handling.
Shiru wrote sometime ago a 4 PCM player driver (6 bits @ 16 Khz).
Samples has to be 256 bytes boundary but the driver was using about 50% of CPU time, leaving enough slot to handle FM and PSG.
Inspired by his code (which mainly use buffering) i wrote a lot of others Z80 drivers :
4 PCM 8 bits @16 Khz
4 PCM 8 bits @16 Khz with envelop support
2 DPCM 4 bits @22 Khz ...
1 PCM 8 bits @8/11/16/22/32 Khz
...

Given all that possibilities i wonder what could be the best format for general music creation on Sega Genesis.
I think the more PCM channels we have, the best it is... but the more PCM channels we have, lower the playback rate is.

For instance we can do :
- FM + PSG + 8 PCM@8/12 Khz
- FM + PSG + 6 PCM@12/16 Khz
- FM + PSG + 4 PCM@16/20 Khz
- FM + PSG + 3 PCM@22 Khz
...

The second playback rate is the "optimist" one (with a good driver).

Also we can though about 4 bit ADPCM but then we reduce the number of channels to gain ROM space :
- FM + PSG + 4 ADPCM@8/11 Khz
- FM + PSG + 2 ADPCM@16/20 Khz

How do you the see the ideal format ? Discussion is open :D

Shiru : FM + PSG + 2PCM 7 bits @variable rate (up to 23 Khz)
Stef : FM + PSG + 4PCM 8 bits @16 Khz
Last edited by Stef on Thu Jun 21, 2012 11:38 am, edited 2 times in total.

Shiru
Very interested
Posts: 786
Joined: Sat Apr 07, 2007 3:11 am
Location: Russia, Moscow
Contact:

Post by Shiru » Thu Jun 21, 2012 10:22 am

It is possible to get like 4 channels with 23 KHz (only samples at Z80 side), but this is actually not really worth it. First, sound quality is limited by the serial DAC of the YM2612 and requesting the bus - it starts to be noticeable at rates like 16 KHz. Second, 4 channels means 6 bits, which is really quiet - you would need to make music quiet as well, so overall volume will be much lower than the normal.

So for PCM, my personal opinion is that a 2-channel driver that allows various sample rates (up to 23 KHz) would be the best.

FM/PSG at Z80 side means either gaps in the sound, or a buffering system for FM writes, so I'm not sure that it is a good idea.

By the way, there is a problem with low sample rates and 256-byte buffering - response latency gets too large, so it starts to be not suitable for drums in the music (they are delayed by like 1-2 frames). So smaller buffer is preferable.

TmEE co.(TM)
Very interested
Posts: 2440
Joined: Tue Dec 05, 2006 1:37 pm
Location: Estonia, Rapla City
Contact:

Post by TmEE co.(TM) » Thu Jun 21, 2012 11:26 am

Digital mixer on cart + DAC so you can output sound directly and on systems without sound pins you can pass digital values to YM.

That is the only way to get the best sound quality for samples.
Mida sa loed ? Nagunii aru ei saa ;)
http://www.tmeeco.eu
Files of all broken links and images of mine are found here : http://www.tmeeco.eu/FileDen

HardWareMan
Very interested
Posts: 745
Joined: Sat Dec 15, 2007 7:49 am
Location: Kazakhstan, Pavlodar

Post by HardWareMan » Thu Jun 21, 2012 11:31 am

GEMS has already canceled?
Last edited by HardWareMan on Thu Jun 21, 2012 11:36 am, edited 1 time in total.

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Thu Jun 21, 2012 11:34 am

Shiru wrote:It is possible to get like 4 channels with 23 KHz (only samples at Z80 side), but this is actually not really worth it. First, sound quality is limited by the serial DAC of the YM2612 and requesting the bus - it starts to be noticeable at rates like 16 KHz. Second, 4 channels means 6 bits, which is really quiet - you would need to make music quiet as well, so overall volume will be much lower than the normal.

So for PCM, my personal opinion is that a 2-channel driver that allows various sample rates (up to 23 KHz) would be the best.

FM/PSG at Z80 side means either gaps in the sound, or a buffering system for FM writes, so I'm not sure that it is a good idea.

By the way, there is a problem with low sample rates and 256-byte buffering - response latency gets too large, so it starts to be not suitable for drums in the music (they are delayed by like 1-2 frames). So smaller buffer is preferable.
Thanks for replying :)

It's true the DAC quality suffers (a lot) from the BUS request latency but still we can really heard a difference between 16 Khz and 22 Khz.
16 Khz could be sufficient ans also take less place in ROM.
The 256 bytes alignment start to give troubles below 16 Khz and honestly in my case i would not even permit < 16 Khz rate but i guess that is a matter of opinion, and anyway we can always reduce buffer size to 64 bytes for instance :)

I do not see any problems with the FM and PSG writes from the Z80, usually the tracker does not send FM or PSG commands more than 50/60 times per second (i guess you can do it for some vibrato effect). If we fix the base Z80 loop to 1/60s then it's ok.

I'm modifying the first post to display different "vote" from people :)

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Thu Jun 21, 2012 11:41 am

HardWareMan wrote:GEMS has already canceled?
GEMS is just too limited, we can do really better than GEMS on genesis :D

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Thu Jun 21, 2012 11:44 am

TmEE co.(TM) wrote:Digital mixer on cart + DAC so you can output sound directly and on systems without sound pins you can pass digital values to YM.

That is the only way to get the best sound quality for samples.
Hehe, but we want that on stock megadrive.
By the way, you already wrote a nice sound driver, can you give us the specs of your drivers ? i believe it fits exactly in what Shiru described.

Shiru
Very interested
Posts: 786
Joined: Sat Apr 07, 2007 3:11 am
Location: Russia, Moscow
Contact:

Post by Shiru » Thu Jun 21, 2012 11:44 am

By the way, why I said 23 KHz and not 22050 or something - I suppose that selecting a frequency that is a multiple (or close to) of the internal DAC samplerate should provide better quality.

TmEE co.(TM)
Very interested
Posts: 2440
Joined: Tue Dec 05, 2006 1:37 pm
Location: Estonia, Rapla City
Contact:

Post by TmEE co.(TM) » Thu Jun 21, 2012 12:03 pm

Stef wrote:Hehe, but we want that on stock megadrive.
By the way, you already wrote a nice sound driver, can you give us the specs of your drivers ? i believe it fits exactly in what Shiru described.
* Z80 based
* Does not use VBL but YM timers instead allowing very fine control on tempo
* 2x 7bit signed PCM channels with 3 levels of volume up to 45KHz. It plays sounds in 256 byte blocks and as Shiru mentioned, it has trouble at lower sample rates (11KHz does not work well at all).
* Buffers 2KB of music data every pattern to minimize bank switching
* FM + PSG SFX, 2x FM channels, or any PSG channel. However PSG was an afterthought so there is no channel management there. Only noise channel can be safely used as music does not make use of it.
* Plays back ginormous tracker modules instead of compact "command streams"
* Does not like lots of DMA and has other jitter related problems so samples aren't so great, but good enough for percussion.
* I am no longer working on it. When I get a new sound system worked out it will get released to public domain in slightly crippled form to prevent "misuse". Full version can be acquired from me none the less, I just got some restrictions and requirements. I am bad corporate creature :P


Jitter is the biggest problem, and you can never get fully rid of it due to MD beign an async design. All cycle counting goes to hell very quick, especially when DMA is involved or you touch ROM form Z80 side.
It is why I am going external hardware route, I am not seeing the point in fighting with the bank register and DMA.
Mida sa loed ? Nagunii aru ei saa ;)
http://www.tmeeco.eu
Files of all broken links and images of mine are found here : http://www.tmeeco.eu/FileDen

djcouchycouch
Very interested
Posts: 710
Joined: Sat Feb 18, 2012 2:44 am

Post by djcouchycouch » Thu Jun 21, 2012 1:12 pm

What kind of user tools would have to be created so that users can make content for this ultimate sound driver?

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Thu Jun 21, 2012 1:22 pm

Thanks for the specs TmEE co.(TM) :)

DMA is a big problem when you deal with Z80 driver, honestly i just try to not use DMA at all when i have sample playback on Z80 :p
If we want to get the most from the Z80 we will probably have to count with cycle and not use interrupt nor YM timers... which make DMA even more problematic ! The ROM latency is not that awful however, in my driver i used a penalty of 3 cycles per ROM access and it seems to be close to that on real hardware.

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Thu Jun 21, 2012 1:28 pm

djcouchycouch wrote:What kind of user tools would have to be created so that users can make content for this ultimate sound driver?
Hehe, that is the second part of the problem :p
I spoke about VGM because i would like this "ultimate driver" format to be somewhat VGM compliant :) and so having the classic VGM tracker as Shiru's tracker or Delek's tracker being able to create music for...
Of course nothing yet is done and maybe nothing will be done, but i think that would permit to create awesome musics for the MD :)

TmEE co.(TM)
Very interested
Posts: 2440
Joined: Tue Dec 05, 2006 1:37 pm
Location: Estonia, Rapla City
Contact:

Post by TmEE co.(TM) » Thu Jun 21, 2012 2:42 pm

I don't settle for anything less than a native tracker.

I am right now working on sound tools for SMS :
http://www.smspower.org/forums/viewtopic.php?t=13604
Mida sa loed ? Nagunii aru ei saa ;)
http://www.tmeeco.eu
Files of all broken links and images of mine are found here : http://www.tmeeco.eu/FileDen

powerofrecall
Very interested
Posts: 237
Joined: Fri Apr 17, 2009 7:35 pm
Location: USA

Post by powerofrecall » Thu Jun 21, 2012 2:47 pm

It's been a while since I've been on these boards or in the Sega community at all, but I had an idea a while back that might be relevant. I guess it would depend on what kind of overhead or performance penalty constantly communicating with the z80 (taking the 68000 off the bus, correct?) would have.

The basic idea would be to take a VGM and compress it some simple way, RLE or something, and to have an unpacker running on the 68000 at vblank or whatever to decompress it in chunks and feed it to a z80 driver, a couple kb at a time or something. I don't know if the layout of the data in a VGM is really workable on the z80 though since as far as I know it wouldn't lend itself to optimization like a big loop unroll or anything. Just a thought though, in that you could have maybe a more complex or compressed format in ROM that gets unpacked by the 68k into simple linear commands to send to the z80 driver.

TmEE co.(TM)
Very interested
Posts: 2440
Joined: Tue Dec 05, 2006 1:37 pm
Location: Estonia, Rapla City
Contact:

Post by TmEE co.(TM) » Thu Jun 21, 2012 3:05 pm

One is better off converting the VGM to some more friendly format
Mida sa loed ? Nagunii aru ei saa ;)
http://www.tmeeco.eu
Files of all broken links and images of mine are found here : http://www.tmeeco.eu/FileDen

Post Reply