Actions

SCHG How-to

Add a Second Digit To the Lifecounter in Sonic 2 SMS

From Sonic Retro

(Original guide by Ravenfreak)

Alright I always hated that you couldn't have more than 9 lives in Sonic 2, so I decided to change that. I didn't use the updated version on the SVN, this is from the wiki version of the disassembly. First look for CapLifeCounterValue in s2.asm. It should look like this:

   LABEL_25AC:
	ld   a, ($D292)
	or   a
	ret  nz
	ld   a, (LifeCounter)
	cp   $09				;cap the life display at 9
	jr   c, +
	ld   a, $09
+:	rlca
	and  $1E
	add  a, $10
	ld   ($DBA9), a
	ret

Now, delete every line after ret nz and add this:

   ld   a, (LifeCounter)
        and $0F
        rlca
	and  $1E
	add  a, $10
	ld   ($DBA9), a
        ret

This sets up the last nibble of the life counter. This is what your code should look like at this:

   ld   a, ($D292)
	or   a
	ret  nz
	ld   a, ($D292)
	or   a
	ret  nz
        ld   a, (LifeCounter)
        and $0F
        rlca
	and  $1E
	add  a, $10
	ld   ($DBA9), a

Alright time for a brief explanation, the rlca operand allows the display to be updated, while the other parts of the code updates the display. We need to change it to where it can set another nibble for the counter. Right after "ld ($DBA9), a" add this:

   ld   a, (LifeCounter)
        and  $F0
        rrca
	rrca
	rrca
	and  $1E
	add  a, $10
	ld   ($DBAF), a
        ret

This will allow the "x" on the counter to become the other nibble. That does allow the counter to display the proper numbers, but when Sonic gains a life after 9, it still displays the wrong value in the wrong nibble. So we need to change that. Find Collision_Monitor_Life in the source code, it should look like this:

   res     1, (hl)
	ld      a, ($D298)
	inc     a
	ld      (LifeCounter), a
	ld      a, SFX_ExtraLife
	ld      ($DD04), a
	jp      LABEL_25AC

First, bellow res 1,(hl) add these lines:

   ld      a, (LifeCounter)
        cp      $99
        ret     z

This will allow the game to cap the life counter after 99 lives. Next we need to change these lines:

        ld      a, ($D298)
	inc     a
	ld      (LifeCounter), a

To this:

   ld      a, (LifeCounter)
        add     a, $01
        daa     (LifeCounter),a

The "daa" operand is used for counters, which is why the "inc" operand wouldn't work here. After that, save build and enjoy. This is what the routine looks like after everything is added:

   CapLifeCounterValue:
LABEL_25AC:
        ld   a, ($D292)
	or   a
	ret  nz
        ld   a, (LifeCounter)
        and $0F
        rlca
	and  $1E
	add  a, $10
	ld   ($DBA9), a
        ld   a, (LifeCounter)
        and  $F0
        rrca
	rrca
	rrca
	and  $1E
	add  a, $10
	ld   ($DBAF), a
	ret
Collision_Monitor_Life:
	res     1, (hl)
        ld      a, (LifeCounter)
        cp      $99
        ret     z
	add     a, $01
	daa
        ld      (LifeCounter), a
        ld      a, SFX_ExtraLife
	ld      ($DD04), a
	jp      LABEL_25AC