Actions

SCHG How-to

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

From Sonic Retro

m (Spelling and formatting fixes)
m (Text replacement - "</asm>" to "</syntaxhighlight>")
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)
 
<asm>  ld  a, (LifeCounter)
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)
 
<asm>  ld  a, ($D292)
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 explanation, 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:
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.  
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)
 
<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)
 
<asm>        ld      a, ($D298)
 
inc    a
 
inc    a
ld      (LifeCounter), a</asm>
+
ld      (LifeCounter), a</syntaxhighlight>
 
To this:
 
To this:
 
<asm>  ld      a, (LifeCounter)
 
<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:
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|{{PAGENAME}}]]

Revision as of 21:28, 20 December 2015

(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:

<asm> 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</syntaxhighlight> Now, delete every line after ret nz and add this: <asm> ld a, (LifeCounter)

       and $0F
       rlca

and $1E add a, $10 ld ($DBA9), a

       ret</syntaxhighlight>

This sets up the last nibble of the life counter. This is what your code should look like at this: <asm> 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</syntaxhighlight> 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: <asm>

  ld   a, (LifeCounter)
       and  $F0
       rrca

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

       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 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: <asm> res 1, (hl) ld a, ($D298) inc a ld (LifeCounter), a ld a, SFX_ExtraLife ld ($DD04), a jp LABEL_25AC</syntaxhighlight> First, bellow res 1,(hl) add these lines: <asm> ld a, (LifeCounter)

       cp      $99
       ret     z</syntaxhighlight>

This will allow the game to cap the life counter after 99 lives. Next we need to change these lines: <asm> ld a, ($D298) inc a ld (LifeCounter), a</syntaxhighlight> To this: <asm> ld a, (LifeCounter)

       add     a, $01
       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. This is what the routine looks like after everything is added: <asm> 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</syntaxhighlight>