don't click here

Sonic 1 sound driver in Sonic 2

Discussion in 'Engineering & Reverse Engineering' started by Tweaker, Jun 8, 2007.

  1. Tweaker

    Tweaker

    Banned
    12,387
    2
    0
    For anyone who's worked with Sonic 2 before, you'll notice that managing music can be a bit of a hassle. Music has to be in a specific location, has to stay within a certain sound bank... and there's a limited number of sounds you can have in the game. However, the sound driver in Sonic 1 is entirely written in 68k (except for the DAC driver), making it much easier to edit, and much easier to manage music with. You can place music data anywhere, as the pointers use relative addressing, and there are no limitations on where you can place music data.

    So, when Puto comes to me with his own port of the Sonic 1 sound driver, and said he had problems, I showed him a personal port that Esrael made for Sonic 2 Megamix. Together, we were able to fully patch up all the issues that the driver has when ported to Sonic 2. Therefore, we decided we'd make a guide, that, when followed correctly, allows anybody to use the Sonic 1 sound driver in their hack! Included below is the Sonic 1 sound driver in its own seperate ASM file, which you can edit freely, along with the original folder structure for the sound driver's music data, sound data, etc.

    Anyway, on with the guide!

    ---------------------------------------------------

    Okay, first we're going to get the actual driver to load. Go to loc_45E, which should be this:
    Code (ASM):
    1. loc_45E:; CODE XREF: ROM:000004C2j
    2. ; ROM:00000562j ...
    3.         addq.l  #1,($FFFFFE0C).w
    4.         movem.l (sp)+,d0-a6
    5.         rte
    And change it to this...
    Code (ASM):
    1. loc_45E:; CODE XREF: ROM:000004C2j
    2. ; ROM:00000562j ...
    3.         jsr  Init_Sonic1_Sound_Driver; Esrael L. G. Neto
    4.                 addq.l  #1,($FFFFFE0C).w
    5.         movem.l (sp)+,d0-a6
    6.         rte
    Next, go to sub_130A, which should be this:
    Code (ASM):
    1. sub_130A:; CODE XREF: ROM:00000386p
    2.         nop
    3.         jmp loc_EC000
    4. ; End of function sub_130A
    And change it to this...
    Code (ASM):
    1. sub_130A:; CODE XREF: ROM:00000386p
    2.         nop
    3. ;jmp    loc_EC000
    4.         move.w  #$0100, ($00A11100); Esrael L. G. Neto Add Sonic 1 Sound Driver
    5.         move.w  #$0100, ($00A11200); Esrael L. G. Neto Add Sonic 1 Sound Driver
    6.         lea  Kos_Z80, A0; Esrael L. G. Neto Add Sonic 1 Sound Driver
    7.         lea  ($00A00000), A1; Esrael L. G. Neto Add Sonic 1 Sound Driver
    8.         bsr  KozDec_193A; Esrael L. G. Neto Add Sonic 1 Sound Driver
    9.         move.b  #((SegaPCM>>$10)&$FF), ($00A00019); Esrael L. G. Neto Add Sonic 1 Sound Driver
    10.         move.w  #$0000, ($00A11200); Esrael L. G. Neto Add Sonic 1 Sound Driver
    11.         nop     ; Esrael L. G. Neto Add Sonic 1 Sound Driver
    12.         nop     ; Esrael L. G. Neto Add Sonic 1 Sound Driver
    13.         nop     ; Esrael L. G. Neto Add Sonic 1 Sound Driver
    14.         nop     ; Esrael L. G. Neto Add Sonic 1 Sound Driver
    15.         move.w  #$0100, ($00A11200); Esrael L. G. Neto Add Sonic 1 Sound Driver
    16.         move.w  #$0000, ($00A11100); Esrael L. G. Neto Add Sonic 1 Sound Driver
    17.         rts     ; Esrael L. G. Neto Add Sonic 1 Sound Driver
    18. ; End of function sub_130A
    Next, find sub_135E, which should be this:
    Code (ASM):
    1. sub_135E:; CODE XREF: ROM:000037BCp
    2. ; ROM:0000399Cp ...
    3.         tst.b   ($FFFFFFE0).w
    4.         bne.s   loc_136A
    5.         move.b  d0,($FFFFFFE0).w
    6.         rts
    7. ; ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
    8.  
    9. loc_136A:; CODE XREF: sub_135E+4j
    10.         move.b  d0,($FFFFFFE4).w
    11.         rts
    12. ; End of function sub_135E
    And change it to this...
    Code (ASM):
    1. sub_135E:; CODE XREF: ROM:000037BCp
    2. ; ROM:0000399Cp ...
    3. ;tst.b  ($FFFFFFE0).w
    4. ;bne.s  loc_136A
    5. ;move.b d0,($FFFFFFE0).w
    6.         move.b  D0, ($FFFFF00A).w; Esrael L. G. Neto Change Sonic 1 Sound Driver       
    7.         rts
    8. ; ====================================================================================================================================
    9.  
    10. loc_136A:; CODE XREF: sub_135E+4j
    11. ;move.b d0,($FFFFFFE4).w
    12.         move.b  D0, ($FFFFF00A).w; Esrael L. G. Neto Change Sonic 1 Sound Driver
    13.         rts
    14. ; End of function sub_135E
    Next find sub_1370, and find this:
    Code (ASM):
    1. sub_1370:; CODE XREF: ROM:0000392Cp
    2. ; ROM:00003CB4p ...
    3.         move.b  d0,($FFFFFFE1).w
    4.         rts
    5. ; End of function sub_1370
    And replace it with this...
    Code (ASM):
    1. sub_1370:; CODE XREF: ROM:0000392Cp
    2. ; ROM:00003CB4p ...
    3. ;move.b d0,($FFFFFFE1).w
    4.                 move.b  D0, ($FFFFF00B).w; Esrael L. G. Neto Change Sonic 1 Sound Driver
    5.         rts
    6. ; End of function sub_1370
    Now, we're going to fix the broken sound effects, such as rings. Look for sub_1376. It should be this:
    Code (ASM):
    1. sub_1376:       ; CODE XREF: sub_11FC2+54j
    2.             ; sub_11FC2+B0j ...
    3.         move.b  d0,($FFFFFFE2).w
    4.         rts
    5. ; End of function sub_1376
    Replace it with this:
    Code (ASM):
    1. sub_1376:       ; CODE XREF: sub_11FC2+54j
    2.             ; sub_11FC2+B0j ...
    3.         bra.s   sub_135E; branch to normal sfx playing routine
    4.         move.b  d0,($FFFFFFE2).w
    5.         rts
    6. ; End of function sub_1376
    Now, look for sub_137C. It should be this:
    Code (ASM):
    1. sub_137C:; CODE XREF: sub_FE70+24p h+B576p ...
    2.         tst.b   1(a0)
    3.         bpl.s   locret_1386
    4.         move.b  d0,($FFFFFFE1).w
    5.  
    6. locret_1386:; CODE XREF: sub_137C+4j
    7.         rts
    8. ; End of function sub_137C
    Replace it with this:
    Code (ASM):
    1. sub_137C:; CODE XREF: sub_FE70+24p h+B576p ...
    2.         tst.b   1(a0)
    3.         bpl.s   locret_1386
    4. ;move.b d0,($FFFFFFE1).w
    5.         move.b  D0, ($FFFFF00B).w; Esrael L. G. Neto Change Sonic 1 Sound Driver
    6.  
    7. locret_1386:; CODE XREF: sub_137C+4j
    8.         rts
    9. ; End of function sub_137C
    Next, we're going to fix the music so it stops correctly when you pause. Find loc_13A6, which should be this:
    Code (ASM):
    1. loc_13A6:; CODE XREF: sub_1388+Ej
    2.         move.w  #1,($FFFFF63A).w
    3.         move.b  #-2,($FFFFFFE0).w
    And replace it with this...
    Code (ASM):
    1. loc_13A6:; CODE XREF: sub_1388+Ej
    2.         move.w  #1,($FFFFF63A).w
    3. ;move.b #-2,($FFFFFFE0).w
    4.         move.b  #$01,($FFFFF003).w; Esrael L. G. Neto Change Sonic 1 Sound Driver - Pause
    Next, go to loc_13F2, which should be this:
    Code (ASM):
    1. loc_13F2:; CODE XREF: sub_1388+4Aj
    2.         move.b  #-1,($FFFFFFE0).w
    And replace it with this...
    Code (ASM):
    1. loc_13F2:; CODE XREF: sub_1388+4Aj
    2. ;move.b #-1,($FFFFFFE0).w
    3.         move.b  #$80,($FFFFF003).w; Esrael L. G. Neto Change Sonic 1 Sound Driver - Pause
    Now, find loc_1400, which should be this:
    Code (ASM):
    1. loc_1400:; CODE XREF: sub_1388+52j sub_1388+5Aj
    2.         move.w  #1,($FFFFF63A).w
    3.         move.b  #-1,($FFFFFFE0).w
    4.         rts
    And replace it with this...
    Code (ASM):
    1. loc_1400:; CODE XREF: sub_1388+52j sub_1388+5Aj
    2.         move.w  #1,($FFFFF63A).w
    3. ;move.b #-1,($FFFFFFE0).w
    4.         move.b  #$01,($FFFFF003).w; Esrael L. G. Neto Change Sonic 1 Sound Driver - Pause
    5.         rts
    Next, find loc_541A, which should be this:
    Code (ASM):
    1. loc_541A:; CODE XREF: ROM:00005212j
    2. ; ROM:0000525Aj
    3.         move.b  #-1,($FFFFFFE0).w
    4.         move.b  #8,($FFFFF62A).w
    5.         bra.w   sub_3384
    And replace it with this...
    Code (ASM):
    1. loc_541A:; CODE XREF: ROM:00005212j
    2. ; ROM:0000525Aj
    3. ;move.b #-1,($FFFFFFE0).w
    4.         move.b  #$01,($FFFFF003).w; Esrael L. G. Neto Change Sonic 1 Sound Driver - Pause      
    5.         move.b  #8,($FFFFF62A).w
    6.         bra.w   sub_3384
    Now, we're going to fix the underwater palettes, since the sound driver uses some space in RAM that, in S2, is used for underwater palettes. Do a search and replace (ctrl+h) and replace all instances of "$FFFFF08" with "$FFFFFA8" - what this will do is make the game read from the Sonic 1 location for underwater palettes, which is unused in S2, and unaffected by the sound driver.

    Next, go to sub_2764. This is the Sonic 2 equivalent of "PalLoad4_Water" in Sonic 1, and what we're going to do is change a line in it to match the Sonic 1 routine, so the underwater palettes load to the correct address. The routine should look like this:
    Code (ASM):
    1. sub_2764:; CODE XREF: ROM:000042E4p
    2.         lea (PalPoint).l,a1
    3.         lsl.w   #3,d0
    4.         adda.w  d0,a1
    5.         movea.l (a1)+,a2
    6.         movea.w (a1)+,a3
    7.         suba.l  #$B00,a3
    8.         move.w  (a1)+,d7
    9.  
    10. loc_277A:; CODE XREF: sub_2764+18j
    11.         move.l  (a2)+,(a3)+
    12.         dbf d7,loc_277A
    13.         rts
    14. ; End of function sub_2764
    Change it to this:
    Code (ASM):
    1. sub_2764:; CODE XREF: ROM:000042E4p
    2.         lea (PalPoint).l,a1
    3.         lsl.w   #3,d0
    4.         adda.w  d0,a1
    5.         movea.l (a1)+,a2
    6.         movea.w (a1)+,a3
    7.         suba.l  #$80,a3
    8.         move.w  (a1)+,d7
    9.  
    10. loc_277A:; CODE XREF: sub_2764+18j
    11.         move.l  (a2)+,(a3)+
    12.         dbf d7,loc_277A
    13.         rts
    14. ; End of function sub_2764
    What this does is change how much the game subtracts from $FB00, in order to calculate the location to move underwater palettes to.

    The last thing we have to do in order to fix underwater palettes for good is to change a VDP instruction, which is what changes the palette fully to the underwater pallete once you're deep enough underwater, and the raster effects are no longer in effect. Do a search and replace for "-$69076AC0" and replace all instances of it with "$96FD9540" - this will make sure that when you're deep enough underwater, the correct palette is loaded.

    The final step is to grab a slightly modified version of the Sonic 1 sound driver from Hivebrain's 2005 Sonic 1 disassembly (link below), and insert it into the Sonic 2 disassembly - the location does not matter, though I personally reccomend the end of the ROM. You can also delete the Sonic 2 sound driver now, if you wish, though it is not required to do so.

    You can either use the include directive to insert S1 Sound Driver.asm, or you can simply copy/paste what's in there. Either will be sufficient.

    NOTE: Certain sound effects and music from Sonic 2 WILL be missing, and you WILL need to add these yourself if you wish to still have them in game. To add a sound effect/music, simply tack it at the end of SoundIndex or MusicIndex. You can also get rid of SoundD0Index (and related setup) if you wish, as Sonic 2 does not have any sound effects that require it. Kega may also mess up a tad bit, not sure why - if anyone can figure it out, let us know.

    Also, Sega sound is garbled. If you want to use it, either use a custom song, or figure out how to get it working. If anyone figures out how to do the latter, it would be appreciated if you shared your methods.

    Sound driver files: http://www.fileden.com/files/15774/S1SoundDriverFiles.rar

    CREDITS:
    Varion Icaria - Puto's sound driver port.
    Esrael - My driver port, plus fixes for things broken in Puto's port, such as hanging on the title card. Extra thanks for fixing the Sega sound.
    Puto - Fixes for underwater palette stuff, as well as special stage ring stuff, and various other fixes.
    StephenUK - Hints on how to fix the underwater stuff.
    drx - Hacking CulT's VDP calculator.
    Myself - I helped Puto fix the underwater stuff, and I wrote the guide. =P

    And that's it! Enjoy! Let me or Puto know if there's any issues left, and we'll try and patch them up.
     
  2. drx

    drx

    mfw Researcher
    2,254
    350
    63
    :rolleyes:
    Esrael puts nice watermarks in his stuff :P
     
  3. LocalH

    LocalH

    roxoring your soxors Tech Member
    One thing to be aware of when you do this - you're taking time away from the main program code on the 68k in order to run your music driver now, so the chance for the game to lag is a bit higher.
     
  4. Tweaker

    Tweaker

    Banned
    12,387
    2
    0
    I'm aware. But, at least in the case of the original Sonic 2, no more noticable lag is induced, even in 2-player splitscreen mode.
     
  5. StephenUK

    StephenUK

    Liquor in the front, poker in the rear Tech Member
    1,678
    0
    16
    Manchester, UK
    Quackshot Disassembly
    Except on the title card, where it has a small amount of lag. You can hear it in the music, but it's not a lot.
     
  6. Puto

    Puto

    Shin'ichi Kudō, detective. Tech Member
    2,013
    0
    16
    Portugal, Oeiras
    Part of Team Megamix, but haven't done any actual work in ages.
    I think we no longer have any lag on the title card. Either that, or it's such a small amount, that it's un-noticeable.
     
  7. Quickman

    Quickman

    be attitude for gains Tech Member
    5,595
    18
    18
    :x
    omg porjcet
    I must be unique in wanting to do the exact opposite to what this guide is doing - I'd rather port the Sonic 2 sound driver to Sonic 1 - the Sonic 2 sound driver is far more modular, so it's easier to drag it around as a bundle.

    (Besides which there's actually a disassembly of Sonic 1, whereas there isn't one for Sonic 2. :P)
     
  8. Puto

    Puto

    Shin'ichi Kudō, detective. Tech Member
    2,013
    0
    16
    Portugal, Oeiras
    Part of Team Megamix, but haven't done any actual work in ages.
    drx already did that once, it's somewhere here on s2b.
     
  9. drx

    drx

    mfw Researcher
    2,254
    350
    63
    :rolleyes:
    Yea, and an I did that before the ASM times if I remember correctly.
     
  10. Dracula

    Dracula

    Oldbie
    605
    0
    16
    I'm watching you!
    Converting NES Mappers to MMC5
    Awesome guide!
     
  11. Esrael

    Esrael

    Neto Tech Member
    304
    257
    63
    Brazil, São Paulo, Guarulhos
    Neto Assembler Editor / Sonic 2 Delta / Neto MD-DOS
    To Fix Sega Sound Change The Following instrunctions:

    Edit:
    Code (Text):
    1.  
    2. Replace  
    3.         move.b  $0F, ($00A00019); Esrael L. G. Neto Add Sonic 1 Sound Driver
    4.   With   
    5.         move.b  #((SegaPCm>>$10)&$FF), ($00A00019); Esrael L. G. Neto Add Sonic 1 Sound Driver
    And
    Edit: Use this code before SegaPCM this will calculate Segaaaa Bank

    Code (Text):
    1.         cnop $0, (((((*+$6978)>>$10)+$01)*$10000)-$6978); Esrael L. G. Neto Auto Detect Bank   
    2. SegaPCM:   
    3.         incbin  sound\segapcm.bin
     
  12. Puto

    Puto

    Shin'ichi Kudō, detective. Tech Member
    2,013
    0
    16
    Portugal, Oeiras
    Part of Team Megamix, but haven't done any actual work in ages.
    "Oops". Apparently, I made a mistake when I fixed the water palettes. Here's a fixed version of that part of the guide, with changes in bold:

    Also, for the record, you forgot to credit drx. I couldn't have fixed the underwater palettes without his VDP Calculator =P.
     
  13. Tweaker

    Tweaker

    Banned
    12,387
    2
    0
    Thanks, guide edited.
     
  14. Tweaker

    Tweaker

    Banned
    12,387
    2
    0
    Another edit; this one includes dynamic recalculation of the Sega sound's location, so the Sega sound now works perfectly. Redownload the sound driver files and make the necessary edits if you've already followed this guide before.
     
  15. Ultima

    Ultima

    Games Publisher Tech Member
    2,398
    1
    18
    London, England
    Publishing mobile games!
    Nice one, guys.
     
  16. Tweaker

    Tweaker

    Banned
    12,387
    2
    0
    Made another edit. This one fixes all sound effect errors for good, from what I can tell. Look over your implementation, make the edits, etc.
     
  17. Flygon

    Flygon

    Member
    Excuse me but is there a chance of a Xenowirl compatible version of this guide?
     
  18. Flygon

    Flygon

    Member
    It has, thanks for the help.

    Edit: I am confused as to how to put in the sound driver into the ROM, it says I can either put it in the include directory or whatever it is again or I could just put it at the end of the ROM, sad thing is I am not entirely sure how to do either, help?
     
  19. Hitaxas

    Hitaxas

    Retro 80's themed Twitch streamer ( on hiatus) Member
    If I still had a pc, I'd walk you through it with some pictures... but I don't and cannot.
    Anyway,open the asm file for the game, and before the "end of rom" line, paste the code. :thumbsup: