Actions

SCHG How-to

Add a new zone in Sonic 1

From Sonic Retro

Revision as of 21:15, 4 August 2010 by HCKTROX (talk | contribs)

(Original guide by FraGag)

In Sonic 1, there are technically 7 zones (not counting the special stage):

This guide will help you to add a new zone (which I'll name Alpha Beta Zone (ABZ) in this guide) as zone 07 using Hivebrain's Sonic 1 disassembly. Remember to change ABZ to the name of your zone(s) in the given examples. Additional zones could be added by following these steps again. Some steps only need to be done once to support up to 256 zones; those steps are clearly identified as such.

Note that it is important to follow all these steps before attempting to try running the game, unless you entirely removed the feature related to the step you're about to follow. Otherwise, the game could try to load the wrong data or run invalid code.

Palette cycling routine

The PalCycle_Load routine is used to dynamically replace some colors in the palette at predefined intervals. The routine uses an offset table to run the code corresponding to the zone the player is currently playing. You must add a routine (although it can be empty) for each level you add to the game, otherwise strange things will happen. Here is what PalCycle should look like after adding one zone: <asm>PalCycle: dc.w PalCycle_GHZ-PalCycle dc.w PalCycle_LZ-PalCycle dc.w PalCycle_MZ-PalCycle dc.w PalCycle_SLZ-PalCycle dc.w PalCycle_SYZ-PalCycle dc.w PalCycle_SBZ-PalCycle dc.w PalCycle_GHZ-PalCycle dc.w PalCycle_ABZ-PalCycle</asm> You will probably want to replace ABZ with what corresponds to the name of your new zone.

Once you've added the zone to the offset table, we must define the routine. If you want no cycling palette, you can use this routine: <asm>; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||


PalCycle_ABZ: ; XREF: PalCycle rts

End of function PalCycle_ABZ</asm>

Put this code close to the offset table, either right after the table, or after the end of PalCycle_SBZ (just before Pal_TitleCyc).

Collision pointers

The new zone needs collision data. You may already have this data if you used SonED2. To define collision for your new zone, open up _inc\Collision index pointers.asm and add a line for your zone. If this is the first zone you add, you will also need to define collision for the ending sequence "zone", as the collision is usually loaded elsewhere for the ending sequence. For the ending sequence, the collision data from Green Hill Zone will do. If you used collision data from another zone for your own, you can point to that zone's collision data directly; otherwise, you must point to your own data and incbin that data.

Here is what _inc\Collision index pointers.asm should look like with one additional zone, assuming the zone uses independent collision data: <asm>; ---------------------------------------------------------------------------

Collision index pointers
---------------------------------------------------------------------------

dc.l Col_GHZ dc.l Col_LZ dc.l Col_MZ dc.l Col_SLZ dc.l Col_SYZ dc.l Col_SBZ dc.l Col_GHZ ; Ending sequence dc.l Col_ABZ</asm> If you used independant collision data, you must also include it in your ROM. If want to keep your disassembly organized, you would include it where the rest of the collision data is defined. Find Col_SBZ, and after the even under it, add something like the following: <asm>Col_ABZ: incbin collide\abz.bin ; ABZ index even</asm>

Level size array

The level size array is used to set the level boundaries and to limit the camera movement when initially loading a level. The data is located in misc\lvl_size.bin. Each act has 12 ($C) bytes and since the game reserves 4 acts for each zone for simplicity's sake, you will need to add 48 ($30) bytes for a zone. Look at the values used for the other zones to determine what works, and make sure to test it in the game.

Start location array

This array is used to position Sonic when starting a level, or restarting a level when a lamppost has not been hit yet. The data is located in misc\sloc_lev.bin. Each act has 4 bytes: 2 bytes for the X position and 2 bytes for the Y position.

Unknown data related to the level size

There is some unknown data that is placed in RAM, which seems to be mostly unused. For the sake of completeness, we're going to add data for each new zone. The data is located at dword_61B4. By looking at the data itself, we can see that every zone has the same data, except for Green Hill Zone and the ending (which uses GHZ art). Let's use the one that appears the most often: <asm>dword_61B4: dc.l $700100, $1000100 dc.l $8000100, $1000000 dc.l $8000100, $1000000 dc.l $8000100, $1000000 dc.l $8000100, $1000000 dc.l $8000100, $1000000 dc.l $700100, $1000100 dc.l $8000100, $1000000</asm>

Background scrolling speed

This subroutine is used to set the scroll speed of some backgrounds. If your background isn't too fancy, you can just use an empty routine. First, edit the offset table like this: <asm>BgScroll_Index: dc.w BgScroll_GHZ-BgScroll_Index, BgScroll_LZ-BgScroll_Index dc.w BgScroll_MZ-BgScroll_Index, BgScroll_SLZ-BgScroll_Index dc.w BgScroll_SYZ-BgScroll_Index, BgScroll_SBZ-BgScroll_Index dc.w BgScroll_End-BgScroll_Index, BgScroll_ABZ-BgScroll_Index</asm> Then, add an empty routine after the routine for the ending. <asm>; ===========================================================================

BgScroll_ABZ: ; XREF: BgScroll_Index rts </asm>

Background layer deformation

When moving in the level, the background lines sometimes scroll at different speeds. This is controlled by the background layer deformation routines. If you just want a plain scrolling background, you can use the routine for Labyrinth Zone. Otherwise, you'll have to program your own routine.

Here's what Deform_Index should look like: <asm>Deform_Index: dc.w Deform_GHZ-Deform_Index, Deform_LZ-Deform_Index dc.w Deform_MZ-Deform_Index, Deform_SLZ-Deform_Index dc.w Deform_SYZ-Deform_Index, Deform_SBZ-Deform_Index dc.w Deform_GHZ-Deform_Index, Deform_ABZ-Deform_Index</asm>

Dynamic screen resizing (and other level events)

This set of routines controls the camera and level boundaries when moving around in the level. They also create the boss objects in act 3. Add an entry for your new zone: <asm>Resize_Index: dc.w Resize_GHZ-Resize_Index, Resize_LZ-Resize_Index dc.w Resize_MZ-Resize_Index, Resize_SLZ-Resize_Index dc.w Resize_SYZ-Resize_Index, Resize_SBZ-Resize_Index dc.w Resize_Ending-Resize_Index, Resize_ABZ-Resize_Index</asm> Then add the routine itself. If you have absolutely no need for dynamic screen resizing, you can simply create an empty routine: <asm>; ---------------------------------------------------------------------------

Alpha Beta Zone sequence dynamic screen resizing (empty)
---------------------------------------------------------------------------

Resize_ABZ: ; XREF: Resize_Index rts

===========================================================================</asm>

If you need dynamic screen resizing (or think you might eventually), you can take this as a base to have separate code for each act (in the original guide, this table contain a bug. Bug fixed by HCKTROX): <asm>; ---------------------------------------------------------------------------

Alpha Beta Zone sequence dynamic screen resizing (empty)
---------------------------------------------------------------------------

Resize_ABZ: ; XREF: Resize_Index moveq #0,d0 move.b ($FFFFFE11).w,d0 add.w d0,d0 move.w Resize_ABZx(pc,d0.w),d0 jmp Resize_ABZx(pc,d0.w)

===========================================================================

Resize_ABZx: dc.w Resize_ABZ1-Resize_ABZx dc.w Resize_ABZ2-Resize_ABZx dc.w Resize_ABZ3-Resize_ABZx

===========================================================================

Resize_ABZ1: rts

===========================================================================

Resize_ABZ2: rts

===========================================================================

Resize_ABZ3: rts

===========================================================================</asm>

Zone title card

First-time fix

You probably want to give your zone a cool name too, huh? Before you add new zones, you'll need to edit the title card object a little, because it's made to support only 6 zones. This only needs to be done once, and will support any number of zones (up to 256).

First, go to Obj34_CheckFZ and replace this: <asm>Obj34_CheckFZ: move.w d0,d2 cmpi.w #$502,($FFFFFE10).w ; check if level is FZ bne.s Obj34_LoadConfig moveq #6,d0 ; load title card number 6 (FZ) moveq #$B,d2 ; use "FINAL" mappings</asm> with this: <asm>Obj34_CheckFZ: move.w d0,d2 cmpi.w #$502,($FFFFFE10).w ; check if level is FZ bne.s Obj34_CheckNew moveq #6,d0 ; load title card number 6 (FZ) moveq #$B,d2 ; use "FINAL" mappings

Obj34_CheckNew: cmpi.b #7,($FFFFFE10).w ; check if level is in the new zones blo.s Obj34_LoadConfig addq.b #$C-7,d2 ; use correct mappings</asm>

Zone-specific data

When that's done, you need to add the actual data for your new zone. This section might be useful to edit the title card: SCHG:Sonic_the_Hedgehog/Text Editing#Level Title Cards. You should put the data for your new zone at the end of the mappings, i.e. after byte_CB8A, just before the even. You'll also need to add a pointer to your new mapping at Map_obj34: <asm>Map_obj34: dc.w byte_C9FE-Map_obj34 dc.w byte_CA2C-Map_obj34 dc.w byte_CA5A-Map_obj34 dc.w byte_CA7A-Map_obj34 dc.w byte_CAA8-Map_obj34 dc.w byte_CADC-Map_obj34 dc.w byte_CB10-Map_obj34 dc.w byte_CB26-Map_obj34 dc.w byte_CB31-Map_obj34 dc.w byte_CB3C-Map_obj34 dc.w byte_CB47-Map_obj34 dc.w byte_CB8A-Map_obj34 dc.w TitleCard_ABZ-Map_obj34</asm> This is what the mapping for Alpha Beta Zone looks like: <asm>TitleCard_ABZ: dc.b 9 dc.b $F8, 5, 0, 0, $B0 ; A dc.b $F8, 5, 0, $26, $C0 ; L dc.b $F8, 5, 0, $36, $D0 ; P dc.b $F8, 5, 0, $1C, $E0 ; H dc.b $F8, 5, 0, 0, $F0 ; A dc.b $F8, 5, 0, 4, $10 ; B dc.b $F8, 5, 0, $10, $20 ; E dc.b $F8, 5, 0, $42, $30 ; T dc.b $F8, 5, 0, 0, $40 ; A</asm> You will also need to position the parts of the title card manually in Obj34_ConData. Add a line like this at the end of the table <asm> dc.w 0, $120, $FF04, $144, $41C, $15C, $21C, $15C ; ABZ</asm>

Level order

The level order tells which level to load when an act has been completed. The data is stored in misc\lvl_ord.bin. Each act takes 2 bytes (the zone and the act), so each zone takes 8 bytes. Again, there is no data for the ending zone, so add 8 "00" bytes at the end of the file, then add the zone and act the game should bring the player to when the level is complete.

Don't understand how it works? Let's take an example: let's say you just completed Marble Zone act 2. The game needs to know what level it will load next. Since Marble Zone is zone 02, skip 2 * 8 bytes, i.e. 16 or $10 bytes. Then, skip 2 more bytes for act 1. At 00000012, there's "02 02", which means that zone 02 (Marble Zone) act 02 (act 3 — remember that the zone and act numbers are zero-based) will be loaded.

If you want to incorporate your new zone in the level flow, you'll need to edit at least one of the values to point to your new zone, then at the end of your zone, point back to one of the existing zones (or yet another new zone).

Music playlists

First-time fix (optional)

Sonic 1 has 2 music playlists: one to play music when loading the level and one to resume it when invincibility wears off. To simplify things, you could simply use the same playlist; otherwise, you'll have to keep them synchronized. To do that, remove those lines: <asm>; ---------------------------------------------------------------------------

Music to play after invincibility wears off
---------------------------------------------------------------------------

MusicList2: incbin misc\muslist2.bin even</asm> Then, replace MusicList2 with MusicList. You can now delete muslist2.bin, but you may also want to keep it in case something went wrong.

Zone-specific data

Now, edit muslist1.bin (and muslist2.bin if you're keeping it). Each zone takes a byte and tells which music track to play. This file already has 00 for zone 07; you should replace it with the actual track you want to play. If you have more than 8 zones, just add a byte. The even in the main ASM file will ensure that everything builds correctly. If you use both playlists, they should have the same size.

Animated level graphics

Some zones have animated art in the foreground or in the background. This is handled by AniArt_Load. The zones that don't have any animated art use AniArt_none, and to keep things simple, this is what I will do for ABZ: <asm>AniArt_Index: dc.w AniArt_GHZ-AniArt_Index, AniArt_none-AniArt_Index dc.w AniArt_MZ-AniArt_Index, AniArt_none-AniArt_Index dc.w AniArt_none-AniArt_Index, AniArt_SBZ-AniArt_Index dc.w AniArt_Ending-AniArt_Index, AniArt_none-AniArt_Index</asm>

Object debug list

If you didn't remove debug mode from your hack, you must add an object debug list to your zone. First, add a pointer to the new list for your zone in _inc\Debug list pointers.asm: <asm> dc.w Debug_GHZ-DebugList dc.w Debug_LZ-DebugList dc.w Debug_MZ-DebugList dc.w Debug_SLZ-DebugList dc.w Debug_SYZ-DebugList dc.w Debug_SBZ-DebugList dc.w Debug_Ending-DebugList dc.w Debug_ABZ-DebugList</asm> Then, add the new list to the main ASM file: <asm>; ---------------------------------------------------------------------------

Debug list - Alpha Beta
---------------------------------------------------------------------------

Debug_ABZ: include "_inc\Debug list - ABZ.asm"</asm> Finally, create a file with the actual list: <asm>; ---------------------------------------------------------------------------

Debug list - Alpha Beta
---------------------------------------------------------------------------

dc.w 3 dc.l Map_obj25+$25000000 dc.b 0, 0, $27, $B2 dc.l Map_obj26+$26000000 dc.b 0, 0, 6, $80 dc.l Map_obj79+$79000000 dc.b 1, 0, 7, $A0 even</asm>

Pattern load cues (zone and animals)

Zone-specific data

The pattern load cues are used to load art to be displayed on the screen. The way the PLCs are arranged in Sonic 1, there will be some problems with the animal PLCs, but the zone PLCs are retrieved from the main level load blocks, so no special treatment is required for those.

First, open _inc\Pattern load cues.asm and add the PLCs for your new zone at the end of the table at the beginning: <asm> dc.w PLC_ABZ-ArtLoadCues, PLC_ABZ2-ArtLoadCues, PLC_ABZAnimals-ArtLoadCues</asm> If you have added other PLCs before this, you may need to adjust some indices in the code below. If you declare your PLCs in another way, the code in this section won't work. The rest of this section only needs to be followed once.

First-time fix

Now, since the animals PLCs are scattered across the table, we are going to use this new subroutine to load the correct PLC. Put it just before LoadPLC: <asm>; ---------------------------------------------------------------------------

Subroutine to load the art for the animals for the current zone
---------------------------------------------------------------------------
||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||


LoadAnimalPLC: moveq #0,d0 move.b ($FFFFFE10).w,d0 cmpi.w #7,d0 bhs.s LoadAnimalPLC_New addi.w #$15,d0 bra.s LoadPLC

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

LoadAnimalPLC_New: subi.w #7,d0 ; multiply d0 by 3 move.w d0,d1 add.w d0,d0 add.w d1,d0 ; add $22 (this is the index of the animal PLC for the first added zone) addi.w #$22,d0 ; bra.s LoadPLC

End of function LoadAnimalPLC</asm>

Now that we have this subroutine, we can replace the two instances in the code that load the animal graphics. Go to Level_ClrCardArt and replace this: <asm>Level_ClrCardArt: moveq #2,d0 jsr (LoadPLC).l ; load explosion patterns moveq #0,d0 move.b ($FFFFFE10).w,d0 addi.w #$15,d0 jsr (LoadPLC).l ; load animal patterns (level no. + $15)</asm> with this: <asm>Level_ClrCardArt: moveq #2,d0 jsr (LoadPLC).l ; load explosion patterns jsr (LoadAnimalPLC).l ; load animal patterns</asm> Then, go to Obj34_ChangeArt and replace this: <asm>Obj34_ChangeArt: ; XREF: Obj34_ChkPos2 cmpi.b #4,$24(a0) bne.s Obj34_Delete moveq #2,d0 jsr (LoadPLC).l ; load explosion patterns moveq #0,d0 move.b ($FFFFFE10).w,d0 addi.w #$15,d0 jsr (LoadPLC).l ; load animal patterns</asm> with this: <asm>Obj34_ChangeArt: ; XREF: Obj34_ChkPos2 cmpi.b #4,$24(a0) bne.s Obj34_Delete moveq #2,d0 jsr (LoadPLC).l ; load explosion patterns jsr (LoadAnimalPLC).l ; load animal patterns</asm>

Zone palette

Your new level needs a palette, otherwise it will be all black or full of random colors. Open _inc\Pallet pointers.asm and add these lines at the end of the file: <asm> dc.l Pal_ABZ dc.w $FB20 dc.w $17</asm> Then, add this line after Pal_Ending: <asm>Pal_ABZ: incbin pallet\abz.bin</asm>

Main level load blocks

The main level load blocks tell the game what to load for a specific zone. They point to the level patterns, to the 16x16 blocks, to the 256x256 chunks, the 2 pattern load cues used to load the art for the level and the palette used in the level. This information is stored in _inc\Main level load blocks.asm.

By looking at how this data is defined for the existing levels and reading the comments at the beginning of the file, you should be able to figure out how to configure your zone. Remember to take the actual PLC indices from the PLC table (in _inc\Pattern load cues.asm). Here is how ABZ is defined (this goes just before the even directive): <asm> dc.l Nem_ABZ+$20000000 dc.l Blk16_ABZ+$21000000 dc.l Blk256_ABZ dc.b 0, $8C, $14, $14</asm> You must also incbin the data for the zone. Add these lines before Nem_Eggman: <asm>Blk16_ABZ: incbin map16\abz.bin even Nem_ABZ: incbin artnem\8x8abz.bin ; ABZ primary patterns even Blk256_ABZ: incbin map256\abz.bin even</asm>

Level layout index

This is probably one of the first things you'd have thought of, yet we're doing it only now! Still, it must be done, so go to Level_Index. This huge array defines the foreground layout, the background layout and another layout that appears to be unused (most of the time, it is empty). For simplicity, I will assume that you have 3 acts, and that all your acts use the same background (look at Scrap Brain Zone for an example of a zone with different backgrounds).

First, add entries at the end of the table for your new zone, like this (byte_6A320 points to an empty layout): <asm> dc.w Level_ABZ1-Level_Index, Level_ABZbg-Level_Index, byte_6A320-Level_Index dc.w Level_ABZ2-Level_Index, Level_ABZbg-Level_Index, byte_6A320-Level_Index dc.w Level_ABZ3-Level_Index, Level_ABZbg-Level_Index, byte_6A320-Level_Index dc.w byte_6A320-Level_Index, byte_6A320-Level_Index, byte_6A320-Level_Index</asm> Then, incbin the layouts. Add this after byte_6A320: <asm>Level_ABZ1: incbin levels\abz1.bin even Level_ABZ2: incbin levels\abz2.bin even Level_ABZ3: incbin levels\abz3.bin even Level_ABZbg: incbin levels\abzbg.bin even</asm>

Objects positions index

First-time fix

Your levels would be boring if they didn't have objects, wouldn't they? However, there's a small problem with how the object layouts are organized in the ROM: the platforms on conveyor belts from Labyrinth Zone and Scrap Brain Zone use data that is put in the same data table as the rest of the object layouts, so we can't add data for new zones without breaking them. Fortunately, it's not too hard to work around this issue by editing the way they are referenced. This only needs to be done once.

First, we will add 2 labels to effectively break the table in 3. We will then use those labels in their respective tables to compute the offsets. The end of the table should look like this: <asm>; ... dc.w ObjPos_End-ObjPos_Index, ObjPos_Null-ObjPos_Index dc.w ObjPos_End-ObjPos_Index, ObjPos_Null-ObjPos_Index dc.w ObjPos_End-ObjPos_Index, ObjPos_Null-ObjPos_Index dc.w ObjPos_End-ObjPos_Index, ObjPos_Null-ObjPos_Index

ObjPos_LZxpf_Index: dc.w ObjPos_LZ1pf1-ObjPos_LZxpf_Index, ObjPos_LZ1pf2-ObjPos_LZxpf_Index dc.w ObjPos_LZ2pf1-ObjPos_LZxpf_Index, ObjPos_LZ2pf2-ObjPos_LZxpf_Index dc.w ObjPos_LZ3pf1-ObjPos_LZxpf_Index, ObjPos_LZ3pf2-ObjPos_LZxpf_Index dc.w ObjPos_LZ1pf1-ObjPos_LZxpf_Index, ObjPos_LZ1pf2-ObjPos_LZxpf_Index

ObjPos_SBZ1pf_Index: dc.w ObjPos_SBZ1pf1-ObjPos_SBZ1pf_Index, ObjPos_SBZ1pf2-ObjPos_SBZ1pf_Index dc.w ObjPos_SBZ1pf3-ObjPos_SBZ1pf_Index, ObjPos_SBZ1pf4-ObjPos_SBZ1pf_Index dc.w ObjPos_SBZ1pf5-ObjPos_SBZ1pf_Index, ObjPos_SBZ1pf6-ObjPos_SBZ1pf_Index dc.w ObjPos_SBZ1pf1-ObjPos_SBZ1pf_Index, ObjPos_SBZ1pf2-ObjPos_SBZ1pf_Index</asm> Then, we will edit the platform objects to reference the data by using these new labels. Go to loc_12460. Replace these two lines: <asm> addi.w #$70,d0 lea (ObjPos_Index).l,a2</asm> with this single line: <asm> lea (ObjPos_LZxpf_Index).l,a2</asm> Now, go to loc_1639A. Similarly, replace these two lines: <asm> addi.w #$80,d0 lea (ObjPos_Index).l,a2</asm> with this line: <asm> lea (ObjPos_SBZ1pf_Index).l,a2</asm>

Zone-specific data

Now that this is done, we can add object layouts for new zones right before ObjPos_LZxpf_Index (without making the platforms behave incorrectly), like this: <asm> dc.w ObjPos_ABZ1-ObjPos_Index, ObjPos_Null-ObjPos_Index dc.w ObjPos_ABZ2-ObjPos_Index, ObjPos_Null-ObjPos_Index dc.w ObjPos_ABZ3-ObjPos_Index, ObjPos_Null-ObjPos_Index dc.w ObjPos_ABZ1-ObjPos_Index, ObjPos_Null-ObjPos_Index</asm> Then, incbin the object positions: <asm>ObjPos_ABZ1: incbin objpos\abz1.bin even ObjPos_ABZ2: incbin objpos\abz2.bin even ObjPos_ABZ3: incbin objpos\abz3.bin even</asm>

Build errors

After applying these steps on a clean Sonic 1 disassembly, I got the following build errors:

  • Error : Branch (32784 bytes) is out of range (near Obj89_Move)
  • Error : Illegal value (136)

The first error points to this line: <asm> bra.w DisplaySprite</asm> Replace bra.w with jmp to fix this error. If you've further altered your hack, you may encounter similar "out of range" errors. If that happens, replace bra.s with bra.w or bra.w with jmp.

The second errors occurs on this line: <asm> move.l LoopTileNums(pc,d0.w),($FFFFF7AC).w</asm> Since we extended the start location array, the loop tile numbers are a bit too far away. You can fix this quickly by moving these 4 lines just after LevSz_Unk: <asm> moveq #0,d0 move.b ($FFFFFE10).w,d0 lsl.b #2,d0 move.l LoopTileNums(pc,d0.w),($FFFFF7AC).w</asm>

Conclusion

Alpha Beta Zone

If you've applied these steps correctly, you should be able to build your ROM and enjoy your new level. Here's a list of the binary files you need to add to be able to build your ROM:

  • pallet\abz.bin
  • map16\abz.bin
  • artnem\8x8abz.bin
  • map256\abz.bin
  • collide\abz.bin
  • levels\abz1.bin
  • levels\abz2.bin
  • levels\abz3.bin
  • levels\abzbg.bin
  • objpos\abz1.bin
  • objpos\abz2.bin
  • objpos\abz3.bin

If you didn't do so yet, you may want to follow Puto's guide to fix the SEGA sound.