Actions

Sandbox

From Sonic Retro

Revision as of 09:13, 13 January 2013 by Tailsguy24 (talk | contribs)

The following guide was made by Tailsguy24, and is not yet complete. DO NOT edit/delete it for any reason.

How to Port Knuckles into Sonic 2

It surprises me that nobody else took up responsibility for making this guide, considering I, as a major fan of hacks, have seen Knuckles implemented in Sonic 2 on more than one occasion. Today, I'll be explaining how you, too, can implement Knuckles in your Sonic 2 hack.

What You'll Need

  • This guide (no duh, really)
  • The Hg disassembly of Sonic 2
  • The one known disassembly of Knuckles in Sonic 2, found on the following page: http://info.sonicretro.org/Disassemblies
  • The ConTEXT text editor (because Notepad will take WAY too long)
  • Your favorite Genesis emulator (I recommend Kega Fusion but you can use any Genesis emulator you'd like)

IMPORTANT NOTE

DO NOT try to rebuild the ROM once you've started this guide or you will get build errors.

Also, there is quite a lot of code added in this tutorial and it will make the ASM file's size (and to an extent, the ROM's size) a lot bigger than they were before. You may want to consider porting Sonic 1's sound driver to Sonic 2 first, as it can take the amount of data shifting caused by extending the level index past $10 in Sonic 2 and still work properly.

Step 1: Creating the Knuckles object

First, open up s2.asm in ConTEXT and search for "dc.l ObjNull." This should come up: <asm>

               dc.l ObjNull ; Obj4C
               dc.l ObjNull ; Obj4D
               dc.l ObjNull ; Obj4E
               dc.l ObjNull ; Obj4F

</asm> Change this line: <asm>

               dc.l ObjNull ; Obj4C

</asm>

To this: <asm>

               dc.l Obj4C   ; Knuckles

</asm>

We've created our Knuckles object. Now let's get started.

Step 2: Creating the offset table

Now that you have created your Knuckles object, go to whatever folder you saved the Knuckles in Sonic 2 disassembly to, open s2k.asm in ConTEXT, and scroll down or search until you find the following code: <asm> Obj01_Index: dc.w Obj01_Init-Obj01_Index  ; 0 ; ... dc.w Obj01_Control-Obj01_Index  ; 1 dc.w Obj01_Hurt-Obj01_Index  ; 2 dc.w Obj01_Dead-Obj01_Index  ; 3 dc.w Obj01_Gone-Obj01_Index  ; 4 dc.w Obj01_Respawning-Obj01_Index ; 5 </asm> Back in s2.asm, copy and paste the following piece of code above the description of the Masher object: <asm>

----------------------------------------------------------------------------
Object 01 - Knuckles
----------------------------------------------------------------------------

Obj01: ; a0=character tst.w (Debug_placement_mode).w ; is debug mode being used? beq.s Obj01_Normal ; if not, branch jmp (DebugMode).l

---------------------------------------------------------------------------
loc_19F5C

Obj01_Normal: moveq #0,d0 move.b routine(a0),d0 move.w Obj01_Index(pc,d0.w),d1 jmp Obj01_Index(pc,d1.w)

End of function Obj01
----------------------------------------------------------------------------

</asm> Now copy the code I mentioned earlier in this step, and paste it below the code displayed above. Then, replace all of the "dc.w"'s with "offsetTableEntry.w"'s, delete the "-Obj01_Index" on the end of each line, move all of the entries down one line, add "offsetTable" to the top, and change all of the 01's in the whole section of code to 4C's. If you're following this guide correctly, your code should look like this: <asm>

----------------------------------------------------------------------------
Object 4C - Knuckles
----------------------------------------------------------------------------

Obj4C: ; a0=character tst.w (Debug_placement_mode).w ; is debug mode being used? beq.s Obj4C_Normal ; if not, branch jmp (DebugMode).l

---------------------------------------------------------------------------
loc_19F5C

Obj4C_Normal: moveq #0,d0 move.b routine(a0),d0 move.w Obj4C_Index(pc,d0.w),d1 jmp Obj4C_Index(pc,d1.w)

End of function Obj4C
----------------------------------------------------------------------------

Obj4C_Index: offsetTable

       	offsetTableEntry.w Obj4C_Init           	  ; 0

offsetTableEntry.w Obj4C_Control  ; 2 offsetTableEntry.w Obj4C_Hurt  ; 4 offsetTableEntry.w Obj4C_Dead  ; 6 offsetTableEntry.w Obj4C_Gone  ; 8 offsetTableEntry.w Obj4C_Respawning  ; $A ... </asm> Now that THAT is done and over with, let's continue to importing Knuckles' many modes (or whatever you people call them).

Step 3: Obj4C_Init

Going back to s2k.asm , you should see this piece of code right below the code I mentioned earlier in Step 2: <asm>

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

Obj01_Init:  ; ... addq.b #2,$24(a0) move.b #$13,$16(a0) move.b #9,$17(a0) move.l #SK_Map_Knuckles,4(a0)  ; SK_Map_Knuckles move.b #2,$18(a0) move.b #$18,$19(a0) move.b #4,1(a0) move.w #$600,($FFFFF760).w move.w #$C,($FFFFF762).w move.w #$80,($FFFFF764).w tst.b ($FFFFFE30).w bne.s Obj01_Init_Continued move.w #$780,2(a0) bsr.w Adjust2PArtPointer2_Useless move.b #$C,$3E(a0) move.b #$D,$3F(a0) move.w 8(a0),($FFFFFE32).w move.w $C(a0),($FFFFFE34).w move.w 2(a0),($FFFFFE3C).w move.w $3E(a0),($FFFFFE3E).w

Obj01_Init_Continued:  ; ... move.b #0,$2C(a0) move.b #4,$2D(a0) move.b #0,($FFFFFE19).w move.b #$1E,$28(a0) sub.w #$20,8(a0) add.w #4,$C(a0) move.w #0,($FFFFEED2).w move.w #$3F,d2

loc_3153EC:  ; ... bsr.w Knuckles_RecordPositions subq.w #4,a1 move.l #0,(a1) dbf d2,loc_3153EC add.w #$20,8(a0) sub.w #4,$C(a0) </asm> All we need to do is copy this, paste it below our already ported code, and change all of the 01's to 4C's. Now you should have this below the offset table: <asm>

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

Obj4C_Init:  ; ... addq.b #2,$24(a0) move.b #$13,$16(a0) move.b #9,$17(a0) move.l #SK_Map_Knuckles,4(a0)  ; SK_Map_Knuckles move.b #2,$18(a0) move.b #$18,$19(a0) move.b #4,1(a0) move.w #$600,($FFFFF760).w move.w #$C,($FFFFF762).w move.w #$80,($FFFFF764).w tst.b ($FFFFFE30).w bne.s Obj4C_Init_Continued move.w #$780,2(a0) bsr.w Adjust2PArtPointer2_Useless move.b #$C,$3E(a0) move.b #$D,$3F(a0) move.w 8(a0),($FFFFFE32).w move.w $C(a0),($FFFFFE34).w move.w 2(a0),($FFFFFE3C).w move.w $3E(a0),($FFFFFE3E).w

Obj4C_Init_Continued:  ; ... move.b #0,$2C(a0) move.b #4,$2D(a0) move.b #0,($FFFFFE19).w move.b #$1E,$28(a0) sub.w #$20,8(a0) add.w #4,$C(a0) move.w #0,($FFFFEED2).w move.w #$3F,d2

loc_3153EC:  ; ... bsr.w Knuckles_RecordPositions subq.w #4,a1 move.l #0,(a1) dbf d2,loc_3153EC add.w #$20,8(a0) sub.w #4,$C(a0) </asm>

Step 4: Obj4C_Control

This one is the most important. It actually makes up approximately 90% of the code for Knuckles. Therefore, we definitely need to port this.

Part 1: Starting off

First, we have to modify the Obj01_Modes offset table from s2k.asm so we can use it for our Knuckles. Below the code mentioned above, you should see this: <asm> Obj01_Control:  ; ... tst.w ($FFFFFFDA).w beq.s loc_315422 btst #4,($FFFFF605).w beq.s loc_315422 move.w #1,($FFFFFE08).w clr.b ($FFFFF7CC).w rts

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

loc_315422:  ; ... tst.b ($FFFFF7CC).w bne.s loc_31542E move.w ($FFFFF604).w,($FFFFF602).w

loc_31542E:  ; ... btst #0,$2A(a0) beq.s loc_31543E move.b #0,$21(a0) bra.s loc_315450

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

loc_31543E:  ; ... moveq #0,d0 move.b $22(a0),d0

and.w #6,d0 move.w Obj01_Modes(pc,d0.w),d1 jsr Obj01_Modes(pc,d1.w)

loc_315450:  ; ... cmp.w #$FF00,($FFFFEECC).w bne.s loc_31545E and.w #$7FF,$C(a0)

loc_31545E:  ; ... bsr.s Knuckles_Display bsr.w Knuckles_Super bsr.w Knuckles_RecordPositions bsr.w Knuckles_Water move.b ($FFFFF768).w,$36(a0) move.b ($FFFFF76A).w,$37(a0) tst.b ($FFFFF7C7).w beq.s loc_31548A tst.b $1C(a0) bne.s loc_31548A move.b $1D(a0),$1C(a0)

loc_31548A:  ; ... bsr.w Knuckles_Animate tst.b $2A(a0) bmi.s loc_31549A jsr TouchResponse

loc_31549A:  ; ... bra.w LoadKnucklesDynPLC

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

Obj01_Modes: dc.w Obj01_MdNormal-Obj01_Modes  ; 0 ; ... dc.w Obj01_MdAir-Obj01_Modes  ; 1 dc.w Obj01_MdRoll-Obj01_Modes  ; 2 dc.w Obj01_MdJump-Obj01_Modes  ; 3 </asm> Do the same thing that was done with the code in Step 1. It should now look like this: <asm> Obj4C_Control:  ; ... tst.w ($FFFFFFDA).w beq.s loc_315422 btst #4,($FFFFF605).w beq.s loc_315422 move.w #1,($FFFFFE08).w clr.b ($FFFFF7CC).w rts

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

loc_315422:  ; ... tst.b ($FFFFF7CC).w bne.s loc_31542E move.w ($FFFFF604).w,($FFFFF602).w

loc_31542E:  ; ... btst #0,$2A(a0) beq.s loc_31543E move.b #0,$21(a0) bra.s loc_315450

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

loc_31543E:  ; ... moveq #0,d0 move.b $22(a0),d0 and.w #6,d0 move.w Obj4C_Modes(pc,d0.w),d1 jsr Obj4C_Modes(pc,d1.w)

loc_315450:  ; ... cmp.w #$FF00,($FFFFEECC).w bne.s loc_31545E and.w #$7FF,$C(a0)

loc_31545E:  ; ... bsr.s Knuckles_Display bsr.w Knuckles_Super bsr.w Knuckles_RecordPositions bsr.w Knuckles_Water move.b ($FFFFF768).w,$36(a0) move.b ($FFFFF76A).w,$37(a0) tst.b ($FFFFF7C7).w beq.s loc_31548A tst.b $1C(a0) bne.s loc_31548A move.b $1D(a0),$1C(a0)

loc_31548A:  ; ... bsr.w Knuckles_Animate tst.b $2A(a0) bmi.s loc_31549A jsr TouchResponse

loc_31549A:  ; ... bra.w LoadKnucklesDynPLC

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

Obj4C_Modes: offsetTable

               offsetTableEntry.w Obj4C_MdNormal_Checks  ; 0 ; not airborne or rolling

offsetTableEntry.w Obj4C_MdAir  ; 2 ; airborne offsetTableEntry.w Obj4C_MdRoll  ; 4 ; rolling offsetTableEntry.w Obj4C_MdJump  ; 6 ; jumping </asm>

Part 2: Obj4C_MdNormal_Checks