Monday, October 9, 2017

Knuckles the Echidna in Sonic the Hedgehog 2

When the Sonic 2 cartridge is locked on to Sonic & Knuckles, special hardware in the Sonic & Knuckles cartridge maps the two games' ROMs onto the Mega Drive's address space as illustrated by the following diagram:

0x000000
0x200000
0x300000
0x340000
0x400000
Sonic & Knuckles
Main ROM
Sonic 2
Main ROM
Sonic & Knuckles patch ROM
empty space

Wait, what? Where did that third ROM come from?

Surprise! The Sonic & Knuckles cartridge actually contains two separate ROM chips: the main 16 megabit ROM and an auxiliary 2 megabit ROM. I do not know any specifics about how the auxiliary ROM gets mapped onto the Mega Drive's address space, beyond the fact that it's there by the time S&K detects the Sonic 2 ROM header in the lock-on slot:
S2orS3LockedOn:
    tst.w   d1
    beq.w   SonicAndKnucklesStartup
    move.b  #1,(SRAM_access_flag).l
    jmp     ($300000).l
As an aside, I assume this is where the "34 meg" figure on the back of the Sonic & Knuckles box comes from: 16 megs from the Sonic 3 ROM + 16 megs from the main Sonic & Knuckles ROM + 2 megs from the auxiliary ROM. This is a bit facetious, because as we've previously seen, when Sonic 3 is locked on, the two main ROMs take up the entirety of the Mega Drive's ROM address space, leaving no room for the auxiliary ROM. Not that it contains anything remotely useful towards running Sonic 3 & Knuckles, anyway.

So, what does it contain?

Last time, I tried to make it abundantly clear that Sonic 3 & Knuckles is really just Sonic & Knuckles running in a special mode that allows it to read stuff from the Sonic 3 cartridge. This is due to the design of the lock-on system, which maps the locked-on ROM to a different base address, making most of the code on the ROM unusable.

It's the same thing with Sonic 2. When the Sonic 2 cartridge is locked on to Sonic & Knuckles, its code becomes mostly unusable, and this is before we've even considered how we're going to add the Knuckles object to the game.

So let's not even go there. We're Sonic Team, we have the source code, so let's just add another ROM to the Sonic & Knuckles cartridge and put the entirety of Sonic 2's code in it, taking care to fix all the data pointers so they'll point at the rebased Sonic 2 ROM in the lock-on slot.

The cool thing about going completely overboard like this is that once again, it creates an opportunity to go back and fix bugs present in the original Sonic 2. For instance, the screen deformation in Emerald Hill Zone was finally fixed, as was the "Rexon crash" in Hill Top Zone.

Why a separate ROM, though? Why not put the Sonic 2 code in the main Sonic & Knuckles ROM?

Beats me. Maybe they projected it wouldn't fit, or maybe it just made it easier to develop Knuckles in Sonic 2 alongside Sonic 3 & Knuckles. This second point had some interesting consequences, which we'll take a look at next time.

14 comments:

  1. I assume this means the ROM can use the Sonic & Knuckles graphics for Knuckles and the Sonic 2 graphics and sounds for everything else, essentially.

    Can Knuckles in Sonic 2 work on emulators with the same way as with Sonic 3 & Knuckles, or is the patch ROM necessary as well?

    ReplyDelete
    Replies
    1. Yes, precisely.

      Of course the patch ROM is necessary, and that's exactly how you produce a working Knuckles in Sonic 2 ROM -- by gluing the three ROMs together as illustrated.

      Delete
    2. I also want to bring up that Knuckles in Sonic 2 exists for the same reason as the Super Emeralds: to compensate for making you buy the second game twice.

      Delete
    3. What? I bought each of Sonic 1, Sonic 2, Sonic 3 and Sonic & Knuckles exactly once, and none of them have duplicate content relative to one another.

      Delete
    4. I said that in the sense of buying two halves of what is supposed to be a single adventure for the price of two games.

      Delete
    5. You could argue that, sure. But I wonder which is the longer game: Sonic 1, or the first half of Sonic 3?

      Delete
    6. Depends. Sonic 1 has more levels, but Sonic 3 has longer levels. I say they're the same length.

      Delete
  2. So, here's a question! I'm not particularly great about processing what you're saying when you start talking about code, so apologies if it's been answered or if you have plans for it.

    Just how much of Sonic 3 is in the S&K ROM, and how much of Sonic 2 is on this extra ROM? Hypothetically, if you were to hack the S&K ROM to give you a menu to run Sonic 3 & Knuckles or Knuckles in Sonic 2, but you didn't add anything else, how would they run?

    From what it sounds like, you couldn't realistically run S3&K without certain Sonic 3 assets, but does the Sonic 2 cart really just act as an activation key?

    ReplyDelete
    Replies
    1. It's quite alright, I like answering questions.

      So the main Sonic & Knuckles ROM has all the code necessary for running Sonic 3 & Knuckles (except a few screen event bits as mentioned previously), and the patch ROM has all the code necessary for running Knuckles in Sonic 2 .

      Almost all of the data is read from the locked-on cartridge, though. That's all the level art, blocks, chunks, collision, layouts, and also music. The only stuff that's duplicated on the S&K cartridge is the stuff which doesn't take up much space; basically palettes and sprite mappings.

      So it's definitely more than just an activation key.

      You *could* potentially load up an S3&K savestate in S&K and fool around, so long as you start somewhere safe such as the level select or in an S&K level. The game would probably be playable, so long as you avoid going into Sonic 3 special/bonus stages.

      Because when you enter a Sonic 3 stage, the game tries to decompress all the Kosinski and Nemesis-compressed level data. Except Sonic 3's not there, so it's going to try decompressing garbage/blank data, which chokes the decompression routines, crashing the game.

      Actually, the S3 special stages are stored uncompressed, so it would happily gobble up the garbage data, except then the start coordinates are probably hosed, crashing the game anyway.

      I dunno. Try it and let me know what you find!

      Delete
    2. Oh yeah, I forgot about Sonic 2.

      Be sure to tell me if you figure out a way to play Sonic 2 without loading up the Sonic 2 title screen, level select, ending screens or any of its stages. If you manage to do that, then I guess it'll work. :)

      Delete
  3. Late, very late, but technically the Rexon bugfix was introduced in S2 REV02, which KiS2 was based on.

    ReplyDelete
    Replies
    1. Right, but the point I was making is that you could take a rev. A or B cartridge and lock it on to Sonic & Knuckles, and the Rexon bug will be fixed there, too. You sort of get the rev. C bugfixes for free in that regard.

      Delete
  4. I don't have a link to the article, but one of the games programmers (maybe yuji naka) mentioned that locking on with Sonic 2 was not officially planned for the game. It was a bonus feature they made on their own, without the higher-ups knowing. And it made them very nervous because it had to be 100% bugfree but they had to debug it on their own, without the test department knowing it (since it was not meant to be in the game). But luckily it turned out right.

    That could explain why it was using a separate rom chip.

    ReplyDelete