[Mario Bros Deluxe] [ASM] Coins won't increase anymore

Started by Kaffkas, July 21, 2019, 05:50:30 AM

Previous topic - Next topic

Kaffkas

I am currently trying to give a 1-Up when 50 Coins are collected, but the Coin counter doesn't increase anymore. Here's the original code that increases the Coin amount:
 
ld a, (C1F2)    #Loads the current Coins into a
inc a           #Increases Coins by 1


This is the code I came up with:


        ld a, (C1F2)    #Loads the Current coins into a
        jp 3A32         #Jumps to some free space


$3A32   cp a, 50        #Compare the Amount of Coins with 50
        jp nz, 3A3D     #Skips to  $3A3D
        inc a           #Increase the Coins by 1
        ld a, (C17F)    #Loads the Current Lives into a
        inc a           #Increases the Current Lives by 1
        ret             #Returns
$3A3D   inc a           #Increases the amount of Coins
        ret             #Returns




#


Cyneprepou4uk

You might wanna increase coins first and compare them afterwards, instead of increasing it here and there. It is also more correct because by the looks of it you get +1up when collecting 51th coin. And I'm pretty sure 50 in hexadecimal is not 50 in decimal.

And I don't know what platform this is, but maybe you also need to put A back into address after increasing it.

Kaffkas

Quote from: Cyneprepou4uk on July 21, 2019, 09:22:15 AM
And I don't know what platform this is,
It's the Game Boy Color Game, forgot to put it in the Title, sorry.

Quote from: Cyneprepou4uk on July 21, 2019, 09:22:15 AM
but maybe you also need to put A back into address after increasing it.

This gave me the Idea to jump back after the the Original Coin increase, and now it kind of works.
This is my code now:

         inc a
         cp a, 03
         jp nz, 3A3C
         ld a, (C17F)
         inc a
$3A3C    jp 2D7B


The problem with this is, when I have 2 Coins and collect a third one, the Amount of my current Lives -1 gets added to my coins, not giving me an 1-Up.

Cyneprepou4uk

I don't know gbc assembly. Sounds like you write code in some text file and hope it works. Use debugger and try to figure out your problem

danke

Quote from: Kaffkas on July 21, 2019, 05:50:30 AM
I am currently trying to give a 1-Up when 50 Coins are collected, but the Coin counter doesn't increase anymore. Here's the original code that increases the Coin amount:
 
ld a, (C1F2)    #Loads the current Coins into a
inc a           #Increases Coins by 1


This is the code I came up with:


        ld a, (C1F2)    #Loads the Current coins into a
        jp 3A32         #Jumps to some free space


$3A32   cp a, 50        #Compare the Amount of Coins with 50
        jp nz, 3A3D     #Skips to  $3A3D
        inc a           #Increase the Coins by 1
        ld a, (C17F)    #Loads the Current Lives into a
        inc a           #Increases the Current Lives by 1
        ret             #Returns
$3A3D   inc a           #Increases the amount of Coins
        ret             #Returns


Looks like your original code is missing some. We'd need to see more to see what your routine is missing.

Also, you're comparing it against 80 decimal/50 hex, not sure if the coins are stored in hex or decimal.

tvtoon

I didn't research this version, but knowing how others handle data, I think you are coming for a BCD digit or pixel mapping stuff. Not exact hexadecimal stuff, check the VRAM.

KingMike

Also, after adding +1 to the lives count, are you storing it back to the lives counter?
And I presume you want to reset the number of coins collected to zero afterwards, don't you?

What you are doing is reading the lives count to the CPU register A, then increasing A. That does not affect the lives counter unless you write A back to the lives count. Is the part I think you are missing.
And you may also want to add a check for max number of lives, to avoid the problem the original NES version of the game had.
"My watch says 30 chickens" Google, 2018

Kaffkas

So, I took another look at it, and this is what I found out:

This is the original Coin Increase and 100 Coin Comparison:

2D75:    ld a, (C1F2) | Loads Coins into a
2D78:    inc a        | Increases Coins by 1
2D79:    cp a, 64     | Compares Lives with 100
2D7B:    jr nz, 2D82  | Jump to 2D82 when Coins are not equal to 100
2D7D:    call 2D4E    | This just loads the Lives into a and increases them
2D80:    ld a, 00     | Load 0 into a
2D82:    ld (C1F2), a | Sets the Coins to 0
2D85:    ret          | Returns



This is what I came up with now (I compare the Coins to 3 and 100):

2D75:   ld a, (C1F2)
2D78:   jp 3A51

3A51:   cp a, 03      | Compare Coins with 3
        jp nz, 2D82   | Jump to 2D82 when the Coins are not equal to 3
        jp z,  3A6B   | Jump to 3A6B when the Coins are equal to 3
        cp a,  64     | Compare Coins with 100
        jp nz, 2D82   | Jump to 2D82 when the Coins are not equal to 100
        jp z,  2D7D   | Jump to 2D7D when the Coins are equal to 100

3A6B:   call 2D4E
        jp 2D85


When I test this Line by Line with the BGB Debugger, it looks like it works as intended, but the Coins stop increasing at 2 and I get an 1-Up for every Coin I collect.

danke


cp a,  03
jp z,  3A6B
jp     2D82
cp a,  64
jp z,  2D7D
jp     2D82


Try that?

Kaffkas

Quote from: danke on July 28, 2019, 01:43:54 PM

cp a,  03
jp z,  3A6B
jp     2D82
cp a,  64
jp z,  2D7D
jp     2D82


Try that?

Still the same Problem, it starts to Increase the Lives when I have 2 Coins.

Cyneprepou4uk

#11
Like I told you before, you need to increase coins first and write them back so these problems won't occur if you don't know how to create conditions and don't mess up in a process. I recommend you to draw a diagram and follow it. Use this for example https://www.lucidchart.com

This is how a correct subroutine should look like:

Call $3A51 (or whatever) subroutine

($3A51) Load coins
Increase coins
Compare to 50 coins
Jump to (1) if true
Compare to 100 coins
Jump to (2) if false
Load 0 to A
(1) Call increase lifes subroutine (my guess is A is not used here, by the looks of original code from your post, despite what you wrote in a comment to this subroutine)
(2) Write coins
Return from $3A51

And this is a safe version in case first one is too complicated for you or if I misunderstood gbc assembly

($3A51) Load coins
Increase coins
Write coins
Load coins
Compare to 50 coins
Jump to (1) if true
Compare to 100 coins
Jump to (2) if false
Load 0 to A
Write coins
(1) Call increase lifes subroutine
(2) Return from $3A51

And don't forget to clean up original code afterwards

yugisokubodai

Quote from: Kaffkas on July 21, 2019, 05:50:30 AM
I am currently trying to give a 1-Up when 50 Coins are collected, but the Coin counter doesn't increase anymore. Here's the original code that increases the Coin amount:
 
ld a, (C1F2)    #Loads the current Coins into a
inc a           #Increases Coins by 1


This is the code I came up with:


        ld a, (C1F2)    #Loads the Current coins into a
        jp 3A32         #Jumps to some free space


$3A32   cp a, 50        #Compare the Amount of Coins with 50
        jp nz, 3A3D     #Skips to  $3A3D
        inc a           #Increase the Coins by 1
        ld a, (C17F)    #Loads the Current Lives into a
        inc a           #Increases the Current Lives by 1
        ret             #Returns
$3A3D   inc a           #Increases the amount of Coins
        ret             #Returns


You forgot to store life after increasing it.
You also forgot to reset the counter when it's 50.
兵法の勝ちを取りても
世の海を渡りかねたる
石の船かな

Kaffkas

I've managed to make it work as I intended. Here's the Code:


3A51: inc a
      cp a, 32
      jp nz, 3A70
      call 2D54
      ld a, (C1F2)
      inc a
3A5E: ld (C1F2), a
      jp 2D85

3A70: cp a, 64
      jp z, 2D7B
      jp 3A5E

The problem was that I haven't used "ld (C1F2), a" to load the updated Coins back from a at the end, as Cyneprepou4uk told me.
I've also left the Original Code unchanged (except the jump to 3A51), since I wouldn't have to squeeze in the 100 Coin Comparison somewhere else.
Anyway, thanks for helping me getting this to work.