don't click here

New Sonic 1 PCM sound driver

Discussion in 'Engineering & Reverse Engineering' started by jman2050, Apr 28, 2006.

  1. jman2050


    Teh Sonik Haker Tech Member
    I thought this deserved it's own topic.

    Take this driver and compress it in the S1 ROM anywhere you like (Kosinski), and make sure to read the notes below. It's much smaller than the other driver because the PCm samples formerly found in the z80 ram are no longer there

    New S1 Music Driver notes:

    PCM playing is controlled by a table located at offset $200 of the uncompressed sound driver. The table's format is as follows.

    Bytes 1/2 - The location of the PCM sample in the bank of the ROM pointed to by bytes 6/7. $8000-based, little-endian
    Bytes 3/4 - The size of the PCM sample in words (bytes/2)
    Byte 5 - Sample rate/Pitch
    Bytes 6/7 - Rom Bank. Format is upper 9-bits of a 16-bit word, little endian (for easy conversion, take the start of the bank in ROM for example, offset $88000, and divide it by $100).
    Byte 8 - unused

    There are 14 slots of 8 bytes each for PCM definitions. To play them, just write $81 + the slot number (0-based) to $1FFF of the Z80 RAM area.

    Writing $90 to $1FFFwill play the sega sound. The bank location of this is still hardcoded, so instead, make a new definition for the Sega sound in an open PCM definition slot. This method of playing the Sega sound is depreciated.

    Writing $8F to $1FFF will play the multi-bank PCM (since we're playing slot $E, and $E + $81 is $8F). This is controlled by the 15th and final slot of the PCM table (slot $E). The format for the multi-bank PCM is a bit different.

    Bytes 1/2 - Same
    Bytes 3/4 - Size of the FIRST SECTION of the PCM sample in BYTES. Not WORDS.
    Byte 5 - Same
    Byte 6/7 - Same, with one note: The lower 7 bits MUST be zero for the bank switching to work properly
    byte 8 - Number of banks to span when playing the file.

    The rom bank is the starting bank of the PCM. The size refers to the number of bytes the PCm takes up in the FIRST bank. NOT the total size of the sample. This is because after this initial size when used as a counter is exhausted, it switches to the ROm bank and assumes a start position of $8000 and a size of $8000. Because of this, the routine will always end playing at the end of a bank. For this reason, make sure to position your PCM samples so that the very end of it coincides with the very end of a ROM bank.


    The default behavior of the driver is 14 PCM slots and 1 multi-bank PCM slot for a total of 15 slots. However, this can easily be changed. First, you have to change when the sega sound is played. To do that, go to offset $3D of the uncompressed file. The default is $0F. Change this to the number of slots you want (so if you want 18 slots, change it to $12). Do note that this will change the value you need to put into $1FFF in your game code in order to play the sega sound. This is the value of offset $3D + $81 (so in the previous example, you'd need to play $93 ($12+$81) into $1FFF). Then just apped the extra PCM data to the end of the table. There's plenty of RAM in the z80, so you can have an endless amount of PCM samples.

    You can also change the number of multi-bank PCM slots. Note that these will always be at te end of the PCM table, no matter how long it is. To do this, go to offset $96. The default is $E. This desribes which slot number to begin defining multi-bank PCM samples. So if you, for example, change it to $C, then every slot in the table from $C onward will be a multi-bank PCM definition. Combined with the change of the number of slots above, this would give you 12 normal PCM slots and 6 multi-bank PCm slots (lots 0-$B and slots $C-$11 respectively.

    And now, finally, the default behavior of this sound driver. These are my settings to test the driver with the uncompressed drum DAC samples from S1, as well as a custom PCM sample I played across multiple banks. Change them to whatever you need. Note that not every slot need be filled. As long as you don't attempt to play a slot with bad data, nothing bad should happen.

    Also, don't ask how to make these samples work in music files. You'll have to write your own game code for that, and I don't know the music format for S1. Ask Tweaker

    Slot 0
    Location: $9000
    Size: $352
    Sample rate: $19
    Bank: $800

    Slot 1
    Location: $A000
    Size: $770
    Sample rate: 1
    Bank $800

    Slots 2-D
    Location: $B000
    Size: $1016
    Sample rate: $1B
    Bank $800

    Slot E (multi-bank)
    Location: $A250
    Initial Size: $5DB0
    Sample rate: $B
    Bank: $880
    Banks to span: 8
    If you have any questions or comments please post them
  2. Tweaker


    All samples are uncompressed now, then?

    What are the PCM limitations for the new uncompressed samples?
  3. jman2050


    Teh Sonik Haker Tech Member
    Nothing except that normal samples can only be played within a bank. Attempting to go out of bounds could result in undefined behavior.
  4. Tweaker


    So with this, I could make normal DAC samples with the quality of, say, the sega sound or better (max hardware limit AKA)? That kicks major ass. Once Jim releases his program, I'll get right to work. :)
  5. Qjimbo


    Your friendly neighbourhood lemming. Oldbie
    It's coming...

    Awesome stuff Jman :) I'll get to work on implamenting this for the Sonic 1 part of UnlimiDAC.

    As for the hardware limit, technically it's as fast as you can write to the DAC, the Z80 is limited to 16khz though, but writing to it with the M68K could probably get faster rates.
  6. LocalH


    roxoring your soxors Tech Member
    To write with the 68k you have to effectively disable the Z80 by giving it a bus request. Also, some of the docs I've read state that you can't set the Z80 bank from the 68k side but only the Z80. If that's true, then you'd be limited to 32KB of sample data without using some Z80 code to swap banks.
  7. ddrmaxromance


    WOW! That looks great! :)

    Could you tell us the major features of this program? Such as you can import MIDI files, other Sonic game music easily, etc.

    .....Will this work with Windows 95?..... or 2000 at least?
  8. LocalH


    roxoring your soxors Tech Member
    Huge ass bump, because I can, and I have a question.

    Is there any problem with just doing away with the Sega sound completely and ignoring that part of the driver? I plan on using your PCM driver with the normal FM driver for my PYL game (with full credit of course), and I don't need the Sega sound at all. I would primarily be using multi-bank samples, as I seriously doubt many of these sounds would fit in 32KB. Is there any requirement for having at least one normal PCM slot or can they all be multi-bank? Also, do you (or anyone else) know the correlation between byte 5 and sample rates in terms of Hz? The particuar sounds I have (recordings from the actual show) are 8-bit/mono/11KHz, which I figure is playable by your routine with the right sample rates.
  9. Dracula


    I'm watching you!
    Converting NES Mappers to MMC5
    Cool! I'll try it.
  10. LocalH


    roxoring your soxors Tech Member
    Also, another question - does the DAC take signed or unsigned PCM data? I need to know so I can begin converting WAVs to raw PCM.

    Edit: NM (unless I'm wrong), I think it uses signed PCM.

    Edit 2: Hm, I had to use unsigned samples to make it work. And I can't get it to work anywhere but Kega as of right now, in a standalone test ROM (which I can provide). I think it's got something to do with my Z80 handshaking, as some of the source code I have seems to differ from what the Sega documentation says.

    <s>Also, there seems to be a glitch in the output when playing multi-bank samples, at what seems to be the point where the initial bank setup is exhausted. It has done this with three different samples, so it's not a problem in my PCM data.</s> My fuckup here, I'd miscalculated the first bank's length.
  11. kram1024


    Researcher / Hacker Oldbie
    at home, duh
    Part of the new Team Revamped; Kraminator Special Series disassemblies; Sonic 3 Sound Driver porting guides; KENS 1.5; smps studio; various non-sonic related things
    Sorry for the bump, but the link seems dead and I wish to make a s3 engine clone with this.
    Updated due to the fact that Tweaker just provided me with the file and I have uploaded it here: Retro Code Sniplets
  12. MarkeyJester


    Original, No substitute Resident Jester
    Sorry to bump (God knows how many times this thread has/will be bumbed lol), nineko has hosted this for us so it should be a parminant host (or at least a longtime host)

    now this isn't exactly the same as original one that was posted above by Jman [that one I don't have], this one has been modded slightly to fit my needs, the only real difference being is that the table doesn't start from offset 00000200, it starts from offset 00000150 insted, but that's the only minor change you need to be aware of.