Put Ristar/S3K tempo algorithm to Sonic 1

Discussion in 'Tutorials' started by AURORA☆FIELDS, Mar 27, 2017.


    AURORA☆FIELDS the cute one here Member

    Oct 7, 2011
    Nathan's arms <3
    So, in this short tutorial, we will be placing the tempo algorithm used in S3K and Ristar into Sonic the Hedgehog 1. I will also instruct how to edit the tempos of each song so that they work properly. The tempo algorithm in Ristar differs from Sonic 1 as such, that rather than using a time out, instead it uses an accumulator to find when another delay needs to be added. The end result is, that you have way more wiggle-room with your tempo values, and are not limited to to the awkward way the default system does it.

    So, we begin with opening up sonic1.asm in hivebrain 2005, or s1.sounddriver.asm in Git disassembly. We want to remove the old routine for handling tempos. Go to sub_7260C or TempoWait, and delete it. Next, go to loc_71B82 or @driverinput, and replace these 3 lines;

            subq.b    #1,1(a6)
            bne.s    loc_71B9E
            jsr    sub_7260C(pc)
            subq.b    #1,v_main_tempo_timeout(a6)    ; Has main tempo timer expired?
            bne.s    @skipdelay
            jsr    TempoWait(pc)
    with this:

            move.b    2(a6),d0        ; get tempo to d0
            add.b    d0,1(a6)        ; add to accumulator
            bcc.s    loc_71B9E        ; if carry clear, branch
    .ch =    $40+$E
        rept 10
            addq.b    #1,.ch(a6)        ; add 1 to duration
    .ch =        .ch+$30
            move.b    v_main_tempo(a6),d0    ; get tempo to d0
            add.b    d0,v_main_tempo_timeout(a6); add to accumulator
            bcc.s    @skipdelay        ; if carry clear, branch
    .ch =    v_music_track_ram+zTrackDurationTimeout
        rept (v_music_track_ram_end-v_music_track_ram)/zTrackSz
            addq.b    #1,.ch(a6)        ; add 1 to duration
    .ch =        .ch+zTrackSz
    Go to loc_72068 or @nospeedshoes, and change the second line to:
            clr.b    1(a6)
            clr.b    v_main_tempo_timeout(a6)
    You may now build if you are curious. Most music should play faster than usual, with the exception of 91, the credits jingle.

    Next step is to fix this. You can use this tool to convert tempos from Sonic 1 to the new method. You need to then convert the speed-up tempos, at byte_71A94 or SpeedUpIndex to have correct speed shoes tempo. Next, you need to convert tempos in the actual SMPS tracks. You can do this by hand or use my automated tool (bin only). Drag and drop each file into the executable to convert. If you are using ASM, simply find each reference to tempo (such as smpsHeaderTempo (second arg) and smpsSetTempoMod). With binary music, you can change byte 5 to the converted one, and find each EA byte inside a music track, and change the next byte to the converted one.

    Now, if you are using bin music, just find each music track include, and change the extension from "bin" to "smp", and you're set! Hopefully everything works as intended.

    Flamewing for the code to convert S1 tempo to S3K tempo.
    ValleyBell for Ristar sound driver disassembly.
    MarkeyJester for help with C and luv.
    Last edited: Mar 27, 2017
    EMK-20218, Renegade, KCEXE and 5 others like this.
  2. EMK-20218

    EMK-20218 The Fuss Maker Member

    Aug 8, 2008
    Jardim Capelinha, São Paulo
    It's one of the things I waited most during all these days working with SMPS and it's finally true. And even a tool for convert the tempos... seriously... thank you very much, Nats!
    AURORA☆FIELDS likes this.