don't click here

Sonic CD decompilation

Discussion in 'Engineering & Reverse Engineering' started by BenoitRen, Jul 17, 2023.

  1. BenoitRen

    BenoitRen

    Tech Member
    904
    547
    93
    That would indeed be neat, which is why merging all the code is exactly what I've been doing this past month! ;) That effort has a separate Git repo.
     
  2. Blastfrog

    Blastfrog

    See ya starside. Member
    Oh, so those videos show it running in a unified engine? I thought you were decompiling the individual stages one at a time.

    [​IMG]

    Anyhow, these notes may or may not be useful:

    The following 4 items are directly pertinent to regional OST differences as implemented in the game.

    1. Title screen delay til demo is shorter in JP.
    2. Act clear duration til next stage is shorter in JP.
    3. Stardust Speedway 3 Good Future plays "B" mix during Metal Sonic race in JP, unlike US that continues to use "G" mix. JP switches back to "G" mix the moment the scroll lock is disabled after the conclusion of the race.*
    4. Time Attack plays the duplicate title theme (track 27) in the JP version instead of Little Planet (track 32) as in the US version.

    *Amusingly, the "G" mix track just restarts in US, seems they forgot to remove that part. Same goes for the bad future for either region, the track still restarts once the scroll is unlocked.

    The following is supplementary info:

    It takes 3 seconds to time travel in PC, 5 in MCD. (approximately, unsure of exact frame durations.) (not that this has anything to do with OST stuff, but worth noting as a major difference)
    In the PC version, the "D.A. Garden" is instead called "Play Music" on the title screen.
    Track 35 is replaced with a duplicate of the unused track 2 in the PC version, annoyingly enough.

    D.A. Garden track list and notes:

    Palmtree Panic
    Palmtree Panic "P" Mix [1]
    Palmtree Panic "G" Mix
    Palmtree Panic "B" Mix
    Collision Chaos
    Collision Chaos "P" Mix [1]
    Collision Chaos "G" Mix
    Collision Chaos "B" Mix
    Tidal Tempest
    Tidal Tempest "P" Mix [1]
    Tidal Tempest "G" Mix
    Tidal Tempest "B" Mix
    Quartz Quadrant
    Quartz Quadrant "P" Mix [1]
    Quartz Quadrant "G" Mix
    Quartz Quadrant "B" Mix
    Wacky Workbench
    Wacky Workbench "P" Mix [1]
    Wacky Workbench "G" Mix
    Wacky Workbench "B" Mix
    Stardust Speedway
    Stardust Speedway "P" Mix [1]
    Stardust Speedway "G" Mix
    Stardust Speedway "B" Mix
    Metallic Madness
    Metallic Madness "P" Mix [1]
    Metallic Madness "G" Mix
    Metallic Madness "B" Mix
    Final Fever
    Little Planet
    Game Over
    Zone Clear
    Boss!! [2]
    Invincible!! [2]
    Speed Up!! [2]
    Title [2]
    Special Stage [2]
    Opening [2] [3]
    Ending [2] [4]

    [1] Exclusive to PC version. (Past tracks)
    [2] Omitted from list in PC version.
    [3] Titled "Sonic - You Can Do Anything" in JP version.
    [4] Titled "Cosmic Eternity - Believe In Yourself" in JP version.

    As an aside, the tracks omitted from the PC version are 7 tracks, that corresponds with the 7 added. Weird that they'd just cut off the last few just to shove the past tracks in, rather than actually extend the list.
     
    Last edited: Feb 4, 2025
    • Informative Informative x 1
    • List
  3. BenoitRen

    BenoitRen

    Tech Member
    904
    547
    93
    The decompilation of the ELF files that have debug symbols was finished mid-July last year. After that, I moved on to decompiling the PC version's executable, as I couldn't make anything of the PS2 version's control program due to lack of debug symbols.

    While I made great progress decompiling SONICCD.EXE, all of its graphics operations were abstracted by an Intel RDX layer, and I got discouraged. I then used the obtained experience to successfully decompile Sonic & Knuckles Collection's C++ wrapper over the summer.

    In November last year, Clownacy showed off his attempt at creating a control program for Sonic CD, and this reinvigorated my courage. I proceeded to decompile the rest of SONICCD.EXE, then tried making my own control program for Palmtree Panic.

    When I succeeded, I decided to take it one step further: do away with the moduled approach and merge all the code. I did so one round at a time during January, making corrections to the decompilation along the way.

    With all that said, thanks for the notes! I had attempted to look up the differences myself, but didn't find a detailed list, so this is a big help!
     
    • Informative Informative x 1
    • List
  4. Blastfrog

    Blastfrog

    See ya starside. Member
    Sure thing! One thing I noticed when comparing the MCD versions (playing both simultaneously with same inputs), is that loading itself takes longer in JP. But I figured that's not too relevant, who wants to wait longer? Also, "Saved Games" in PC is called "RAM Data" in MCD, but meh, that's more hardware specific. On that note, the save screen itself is organized and functions differently. PC's D.A. Garden seems to let you scroll the Little Planet very, very far off-screen. I need to check the MCD version to see whether it behaves like this, or if it has some sensible limit.

    Also, minor nitpick on the title of the repository, it would be more grammatically correct if it said "respun" instead of "respinned." :P

    Curious if you're considering any bug and/or consistency fixes. Wacky Workbench for instance infamously has a broken palette after the flash where it reverts some colors to their 510 proto versions. Incidentally, it also is the only round where Sonic retains his 510 proto spindash and peelout charge time and velocities after that build.
     
  5. BenoitRen

    BenoitRen

    Tech Member
    904
    547
    93
    "respinned" is not grammatically correct? Damn, I thought I had found a cool name. "respun" doesn't have the same ring to it. :(

    I've already applied some bug fixes to make it closer to the Sega Mega CD version (most notably, there's a chunk missing in the background of R11D in the C version). I'm open to fixing bugs present in the original version, but preferably with a toggle for people preferring the authentic experience.

    Speaking of Wacky Workbench, I wonder if it was intentional that Sonic never accelerates to his fast run animation in that round. If you use the Super Peel Out, he does use his fastest running animation.

    I've been tackling the title screen's graphics logic, and I need to rant. The files that store tiles and sprites contain metadata that, among others, specify which palette line is being used. Yet, for reasons that escape me, the data on palette line usage in the title screen's files is wrong. It is, instead, defined in arrays in a source file. Why?!
     
  6. Brainulator

    Brainulator

    Regular garden-variety member Member
    I say screw leftover umlaut past tenses. Just do whatever you think is best.
     
  7. BenoitRen

    BenoitRen

    Tech Member
    904
    547
    93
    Worked in earnest on the title screen today, now that everything was clear in my head. It was partially successful:

    upload_2025-2-8_23-3-5.png

    upload_2025-2-8_23-3-24.png
     
  8. Chimes

    Chimes

    The One SSG-EG Maniac Member
    998
    700
    93
    I demand a GIF of that warped sky
     
    • Agree Agree x 2
    • Like Like x 1
    • List
  9. nineko

    nineko

    I am the Holy Cat Tech Member
    6,390
    539
    93
    italy
    I'll do you one better: why is Gamora I want someone to implement that title screen in a Sonic CD hack.
     
  10. BenoitRen

    BenoitRen

    Tech Member
    904
    547
    93
    It's looking much better now:

    upload_2025-2-9_23-46-44.png

    Not only the palette line data is wrong. The dimensions of the sprites are also wrong! That's why the game has a custom sprite loading function for this screen.

    This custom function also loads two external bitmaps into two slots. One to replace the 1995 copyright notice with a 1996 one, and one with the "EXIT" text.

    Spent a lot of time trying to find out why Sonic wasn't wagging his finger. First, I found out that thanks to Ghidra I filled in the struct containing the timing data incorrectly. After fixing that, and confirming that the correct sprites were being selected, I noticed that the sprite counter wasn't reset to zero in subsequent executions of OEDraw. Always remember to initialise your variables!

    Little Planet even bobs up and down, which for some reason it doesn't do in Sonic Gems Collection.

    No idea how the scaled clouds are implemented, as it's hidden behind a hmx_ddagrid_set_scan_module call.

    Next up: finding out why the menu options don't display, and animating the water.
     
  11. Blastfrog

    Blastfrog

    See ya starside. Member
    So I was testing the D.A. Garden again, both PC and MCD. Actually, looks like the PC version does have the same or similar scroll boundaries.

    Amusingly, you can cause an integer overflow on the planet rotation by holding B for a very long time, eventually it will start rotating the opposite direction, slow down, and then start accelerating again in the direction it began rotating in.

    I’m unaware if there is any way for the end user to zoom or rotate the planet in PC, seeing as the only inputs beyond up/down/left/right are space and escape, and in D.A. Garden escape returns you to the title screen and space opens the tracklist. I wonder if there is unused code remaining for rotation and zooming.
     
    • Informative Informative x 1
    • List
  12. MainMemory

    MainMemory

    Kate the Wolf Tech Member
    4,799
    385
    63
    SonLVL
    Yeah, the X key handles rotation and the C key handles zoom.
     
    • Informative Informative x 2
    • List
  13. BenoitRen

    BenoitRen

    Tech Member
    904
    547
    93
    Today's progress report: made the menu options show up (they didn't because of a bug I introduced in the graphics refactoring, oops), fixed the left and right arrows (decompilation error), and I just managed to animate the water!

    The "Exit" option is still missing because I'm not loading external bitmaps yet, but I'll add it later.

    That leaves the clouds, which will require me to implement what Clownacy termed "coordinate conversion". What's odd is that the cloud sprites have a lower priority than Little Planet's sprite, yet in practice you can clearly see that they display in front of it.

    I guess I'll have to skip those sprites when queueing sprites for blitting, and add code for queueing coordinate conversed graphics.

    Getting a bit worried about the mess I might be making, because ideally I could consolidate all drawing code, but it's better that I focus on getting all of this working, first.
     
  14. BenoitRen

    BenoitRen

    Tech Member
    904
    547
    93
    Can you tell me how to get started? I tried reading Devon's explanation, but for someone not familiar with the Mega CD's internals, it's not detailed enough for me. The demo refers to Tonc's explanation of Mode 7, but I don't understand it, either. Finally, I tried looking at Clownmdemu's code, but need some context.

    The title screen's clouds have to use some form of coordinate conversion, because the PC version uses the SMORPH effect on them, just like it does for the Special Stages and D.A. Garden. But the input consists of two 160x88 images instead of a big 4096x4096 one, which is not a size that is made up of 16x16 and 32x32 stamps.

    Honestly, I'm lost.
     
  15. Devon

    Devon

    Please do not contact me, overwhelmed with stuff Tech Member
    1,519
    1,870
    93
    your mom
    Maybe some visual examples would help?






    Basically, you have a source image, which is constructed from stamps, and a "trace vector" table, which describes start and delta values for each line drawn in the rendered image. The start values define where in the source image to start reading pixels from, and the delta values describe the rate in which is scans the source image as it draws the line. The smaller the delta values, the smaller the distance the scanner travels, which results in a upscaling effect. Of course, the inverse applies as well.
     
    Last edited: Feb 12, 2025
  16. BenoitRen

    BenoitRen

    Tech Member
    904
    547
    93
    Thanks, I think I get it now, now.

    Now I have to figure out how to get the right coordinates from the trace table. For example, the first StartX is 4160000. If I assume that's 13.3 fixed point, I get a value of 520000 (right shift 3, or divide by 8), which is still way too high. Or am I misunderstanding the notation?

    If I'm not, what's the point of using 11 decimals for the delta (5.11 fixed point) when the start values have only 3 decimals?
     
  17. Devon

    Devon

    Please do not contact me, overwhelmed with stuff Tech Member
    1,519
    1,870
    93
    your mom
    On the Mega CD, the start and delta values are stored as 16-bit values, so a start position of 4160000 is impossible. With the start values, the reason the integer part is 13 bits is because it needs to be able to cover the maximum 4096x4096 source image resolution + some leeway for drawing outside of it (if the repeat flag is unset). With the delta values, having such a high integer part would be completely absurd and pointless, and plus, fractional precision is much more important here so that smoother scaling (especially upscaling) and rotation can be achieved.
     
  18. BenoitRen

    BenoitRen

    Tech Member
    904
    547
    93
    "Hmm, maybe the values are half integer and half fractional. Let's try that. OSHI-"



    I was so surprised to see it suddenly working!

    Like I mentioned before, Little Planet bobs up and down here, unlike on Sonic Gems Collection. But that's not all! I restored the animated left and right arrows, as their animation was missing. There was code for them, but it was broken, and only accounted for one arrow showing up at a time.
     
  19. BenoitRen

    BenoitRen

    Tech Member
    904
    547
    93
    The past week, I've been refactoring my code, and I've glued the title screen and the stages together. Now, you can choose "New Game" and start playing. Things are coming together!

    Today, I've added the 1996 copyright and the Exit option bitmaps:

    upload_2025-2-18_17-14-18.png

    However, I'm not entirely happy with how I've added them to the bitmap loading function.

    Currently, I use the metadata of the compressed sprites file to calculate how much memory I need to allocate for the sprite bitmap data. To add the two aforementioned bitmaps, I had to load them and add their sizes to the count. Next, I extract the sprites as normal, then copy the data of the two bitmaps behind the rest of the data.

    In my opinion, it took a bit too much code. But I don't think I can improve it unless I change the logic to allocate memory per sprite.

    Finally, earlier today, I was curious how big the executable had become. The debug version is almost 4 MB. I compiled a release version with O2 optimisation for the Pentium MMX: 891 KB. If I turn off optimisation for a fairer comparison with the original code, it's 1065 KB.

    The original executable is 113 KB. But that's just the core program. There are 70 DLLs for the levels, which on average are 130 KB a piece. That means there's about 9100 KB, or 8,89 MB, of level code.

    That's a lot of duplicate code and data I removed. :)
     
    • Like Like x 10
    • Useful Useful x 1
    • List
  20. BenoitRen

    BenoitRen

    Tech Member
    904
    547
    93
    I've been thinking of releasing a preview version of the executable for everyone to try out, but as it's made for SDL1, the result would be either a postage stamp size window, or full-screen with bilinear filtering. Not ideal.

    That's why I've added compatibility with SDL2. It was pretty easy, save for figuring out how to get 8-bit pixel data to a 32-bit texture. Thankfully, someone wanted to do the same thing in the past, and documented how to do it.

    Now you can enjoy it full-screen, integer-scaled to your resolution. I've attached the Windows executable to this post.

    Next, you need to put the following in the same folder as the executable:
    • SDL2.
    • The high quality sound effects file from Sonic CD PC 1995, PCM.CMP, which has been archived on Google Drive and MediaFire.
    • The BGM file BGM_US.AFS from Sonic Gems Collection. Yes, I'm using the US version for now.
    • The R1, R3, R4, R5, R6, R7, R8, and TITLE folders (and their contents, though the .ELF files aren't necessary) from Sonic Gems Collection.
    For the uninitiated, Sonic CD's files on the Sonic Gems Collection DVD are contained in an archive called SONICCD.CVM in the SONICCD folder. Use 7-Zip or another program that can open archives. It's not a ZIP archive, so changing the extension to .zip won't work.

    You can only play through the levels, there's no Special Stage, and save data isn't implemented. Use the arrow keys to move, and space bar to jump. This comes with no warranty, yada yada.

    Enjoy!
     

    Attached Files: