Actions

SCHG How-to

Difference between revisions of "Add a Second Digit To the Lifecounter in Sonic 2 SMS"

From Sonic Retro

(Created page with "{{GuideBy|Ravenfreak}} How to add a second digit to the life counter in Sonic 2 SMS Alright I always hated that you couldn't have more than 9 lives in Sonic 2, so I decided to ch...")
 
m (Text replacement - "SCHG How-tos (Sonic the Hedgehog 2 (Master System)]]" to "SCHG How-tos (Sonic the Hedgehog 2 (Master System))]]")
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{GuideBy|Ravenfreak}}
 
{{GuideBy|Ravenfreak}}
How to add a second digit to the life counter in Sonic 2 SMS
+
 
 
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
 
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:
+
the SVN, this is from the wiki version of the disassembly. First look for CapLifeCounterValue in s2.asm. It should look like this:
  
<asm>  LABEL_25AC:
+
<syntaxhighlight lang="asm">  LABEL_25AC:
 
ld  a, ($D292)
 
ld  a, ($D292)
 
or  a
 
or  a
Line 16: Line 16:
 
add  a, $10
 
add  a, $10
 
ld  ($DBA9), a
 
ld  ($DBA9), a
ret</asm>
+
ret</syntaxhighlight>
 
Now, delete every line after ret nz and add this:
 
Now, delete every line after ret nz and add this:
<asm>  ld  a, (LifeCounter)
+
<syntaxhighlight lang="asm">  ld  a, (LifeCounter)
 
         and $0F
 
         and $0F
 
         rlca
 
         rlca
Line 24: Line 24:
 
add  a, $10
 
add  a, $10
 
ld  ($DBA9), a
 
ld  ($DBA9), a
         ret</asm>
+
         ret</syntaxhighlight>
 
This sets up the last nibble of the life counter. This is what your code should look like at this:
 
This sets up the last nibble of the life counter. This is what your code should look like at this:
<asm>  ld  a, ($D292)
+
<syntaxhighlight lang="asm">  ld  a, ($D292)
 
or  a
 
or  a
 
ret  nz
 
ret  nz
Line 37: Line 37:
 
and  $1E
 
and  $1E
 
add  a, $10
 
add  a, $10
ld  ($DBA9), a</asm>
+
ld  ($DBA9), a</syntaxhighlight>
Alright time for a brief explination, the rlca operand allows the display to be updated, while the other parts of the code updates the display.  
+
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:
 
We need to change it to where it can set another nibble for the counter. Right after "ld ($DBA9), a" add this:
<asm>
+
<syntaxhighlight lang="asm">
 
   ld  a, (LifeCounter)
 
   ld  a, (LifeCounter)
 
         and  $F0
 
         and  $F0
Line 49: Line 49:
 
add  a, $10
 
add  a, $10
 
ld  ($DBAF), a
 
ld  ($DBAF), a
         ret</asm>
+
         ret</syntaxhighlight>
 
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
 
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.  
 
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:
 
Find Collision_Monitor_Life in the source code, it should look like this:
<asm>  res    1, (hl)
+
<syntaxhighlight lang="asm">  res    1, (hl)
 
ld      a, ($D298)
 
ld      a, ($D298)
 
inc    a
 
inc    a
Line 59: Line 59:
 
ld      a, SFX_ExtraLife
 
ld      a, SFX_ExtraLife
 
ld      ($DD04), a
 
ld      ($DD04), a
jp      LABEL_25AC</asm>
+
jp      LABEL_25AC</syntaxhighlight>
 
First, bellow res  1,(hl) add these lines:
 
First, bellow res  1,(hl) add these lines:
<asm>  ld      a, (LifeCounter)
+
<syntaxhighlight lang="asm">  ld      a, (LifeCounter)
 
         cp      $99
 
         cp      $99
         ret    z</asm>
+
         ret    z</syntaxhighlight>
 
This will allow the game to cap the life counter after 99 lives. Next we need to change these lines:
 
This will allow the game to cap the life counter after 99 lives. Next we need to change these lines:
<asm>        ld      a, ($D298)
+
<syntaxhighlight lang="asm">        ld      a, ($D298)
 
inc    a
 
inc    a
ld      (LifeCounter), a</asm>
+
ld      (LifeCounter), a</syntaxhighlight>
 
To this:
 
To this:
<asm>  ld      a, (LifeCounter)
+
<syntaxhighlight lang="asm">  ld      a, (LifeCounter)
 
         add    a, $01
 
         add    a, $01
         daa    (LifeCounter),a</asm>
+
         daa    (LifeCounter),a</syntaxhighlight>
 
The "daa" operand is used for counters, which is why the "inc" operand wouldn't work here. After that, save build and enjoy.
 
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:
 
This is what the routine looks like after everything is added:
<asm>  CapLifeCounterValue:
+
<syntaxhighlight lang="asm">  CapLifeCounterValue:
 
LABEL_25AC:
 
LABEL_25AC:
 
         ld  a, ($D292)
 
         ld  a, ($D292)
Line 104: Line 104:
 
         ld      a, SFX_ExtraLife
 
         ld      a, SFX_ExtraLife
 
ld      ($DD04), a
 
ld      ($DD04), a
jp      LABEL_25AC</asm>
+
jp      LABEL_25AC</syntaxhighlight>
 
 
  
[[Category:SCHG_How-tos|{{PAGENAME}}]]
+
[[Category:SCHG How-tos (Sonic the Hedgehog 2 (Master System))]]|{{PAGENAME}}]]

Latest revision as of 11:18, 25 August 2018

(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
|Add a Second Digit To the Lifecounter in Sonic 2 SMS]]