Monday, January 1, 2018

Once more, from the top

Happy new year! I have survived the holiday season, and if you are reading this, then so have you. Congratulations.

In anticipation of Sonic 3's upcoming anniversary, I've decided to once again spend the days leading up to it discussing stuff that can only be encountered while playing standalone Sonic 3, henceforth referred to as Sonic 3 alone or S3A for brevity. I'll start with a comment by reader Josh Wallen, who asks:
I'd love to learn the details of why entering Sonic 2's level select code and hotswapping the cartridge works, AND why it's so much easier than entering Sonic 3's _actual_ cheat. I seem to recall it having something to do with the FMV meaning the Genesis doesn't check the input on every frame, so maybe you have covered that one...
I haven't, but other people have done so in the past, which is probably what you're remembering. The intended window for entering the S3A level select code is during the short animation that occurs between the SEGA logo and the game's title screen, but as you note, the controllers aren't checked every frame, which causes some inputs to be dropped.


So what's going on? First off, in order to conserve ROM space, each frame in the animation is compressed with Kosinki compression, and must be sequentially decompressed to RAM before being displayed on screen.

The game's developers actually optimized this procedure by spliting a region of RAM into two buffers, so that while one frame is being decompressed into the first buffer, a completed frame in the second buffer can be sent to VRAM through DMA. When the first frame finishes decompressing, the two buffers are swapped and the contents of the first buffer are sent to VRAM, and at the same time a new frame starts decompressing into the second buffer.

Now, the game usually polls the controllers for player inputs during the vertical interrupt period between outputting each frame to the TV screen, thereby ensuring that the inputs are checked 60 times a second together with the game's video output. And therein lies the rub: some of the animation frames are so large, take more than a frame to decompress.

As a result, the game misses the vertical interrupt window and doesn't output a new frame to the TV screen, creating a "lag frame". Which isn't that bad, since we're just showing an already choppy animation at this point. What sucks is that the controllers aren't polled for input either, so any new button presses are silently dropped.

It just so happens that after decompressing each animation frame, the game introduces an artificial 4 frame pause, just to pace the animation. During this pause, the interrupt handler executes normally, so the controllers get polled properly.


As for the cartridge swap trick, that works because Sonic 2 and Sonic 3 use the same RAM address to store their "level select unlocked" flag, and RAM isn't cleared when the console is reset. I would advise against this method, though: you might short-circuit two pins before the ground connects, which could damage your stuff. I recommend just punching the S3A code in as quickly as you can, before the animation advances to the more problematic, larger frames.

5 comments:

  1. Happy new year! Anyway, I feel like they meant for the code to be hard in order to conceal information that was to be used in Sonic & Knuckles, much like how Sonic & Knuckles alone conceals various functions to avoid accessing Sonic 3 content... but the lag frames made it harder on accident. Safe to say, the Sega Saturn port seen in Sonic Jam, being on more powerful hardware, does not have this problem (and lets you put in the cheat at the title screen itself).

    ReplyDelete
  2. Or you could just buy a Game Genie and save yourself the trouble of either destroying your console by cartridge switching or destroying it from frustration. Happy new year!

    ReplyDelete
  3. Bit late, I know, but I'm guessing that the reason the Sonic Jam version of Sonic 3 has such a lightening-fast intro is because it _doesn't_ use that 4 frame pause, and/or doesn't have to wait for more than a frame for the art to decompress.

    ReplyDelete
  4. I could NEVER punch in that code. I was so grateful Sonic and Knuckles changed it to something much easier.

    ReplyDelete