Users browsing this thread: 1 Guest(s)
How I Made My First Custom Code

10-29-2016, 09:16 PM, (This post was last modified: 10-29-2016, 09:21 PM by Turbotastic.)
#1
Posts: 35
Threads: 8
Thanks Received: 5
Thanks Given: 14
Joined: Dec 2015
Reputation: 0
Status
Debrave
How I Made My First Custom Code
As a newbie to hacking, I would like to share my experience in how I cobbled together basic custom code in the hopes it would help other confused newbies out.

First, let me begin that I never would have been able to do without assassin's commented C2 disassembly which I will be quoting excepts from. I also need to thank my fellow forumers for letting me pester them with questions.

As I wrote in another thread, I began to play around with the code by setting various aspects of the hex for the magic menu in battle to EA, the NOP value.  I ended up causing all the active party members to share a magic list, albeit in a bugged manner. I idly pondered if there were a way to make a non-bugged way of doing this.

Then it hit me how simple it was to do this.

For the uninitiated, during battle the Magic menu is generated by two passes.  The first pass is to accumulate all the fully learned spells known by ANY character in battle. The second pass removes the spells not known by an invidivual character from his or her menu, leaving only their personal spells.

However, there is one character exception to the second pass, Gogo. Going through the first pass (pulling together everyone's spells) is precisely how Gogo's in-battle magic menu is built. The code for Gogo bypasses the second check. So why not just treat everyone like Gogo and bypass the existing code? So I replaced...

Code:
C2/56C9: BD D8 3E     LDA $3ED8,X

...with the same jump used for Gogo.

Code:
C2/56C9: 4C E5 56     JMP $56E5

Thus like with Gogo, everyone's magic menu was shared in battle, and best of all, the characters could learn spells normally.

That worked...too well.

For the majority of the game, pooling everyone's spells is fine. In the beginning of the game, though, magic is restricted to Terra and Celes for plot and gameplay reasons.  That infamous scene with Terra, Locke, and Edgar loses its punch and meaning if Locke or Edgar are casting spells to trigger it.

So I had to put in a check to disallow this behavior before getting Magicite.  This would be simple enough, since there is already code in place that checks for this condition at the end of battle, and I could just replicate the original code to have the same effect.

For this, I got the "bright idea" Big Grin  to change where to put the jump, since it would be jumping to brand new code.  I decided to replace...

Code:
C2/56CC: C9 0C        CMP #$0C
C2/56CE: F0 15        BEQ $56E5

...with...

Code:
C2/56CC: 4C 69 64     JMP $6469

I then added my new code in the free space in C2.

Code:
C2/6469: 85 F1        STA $F1
C2/646B: AD BC 3E     LDA $3EBC
C2/646E: 29 08        AND #$08
C2/6470: F0 09        BEQ $647B // branch to line before Gogo jump line
C2/6472: A5 F1        LDA $F1
C2/6474: C9 0C        CMP #$0C
C2/6476: F0 05        BEQ $647D // branch to Gogo jump line
C2/6478: 4C D0 56     JMP $56D0
C2/647B: A5 F1        LDA $F1
C2/647D: 4C E5 56     JMP $56E5

Veterans will know what happened when I implemented this mess of hex, fired up my rom, and stepped into battle. This predictably crashed my ROM at the first random encounter.

So this was wrong...

While I tried to figure out what I'd done wrong here I had another realization...I was making this way too complicated. My original jump sufficed without the check for Gogo. Since Gogo would not be obtained until after the Zozo incident and since everyone would be behaving like Gogo anyway, there'd be no point in loading the character index...and there would be no need to "store" it.

So I started the code anew and came up with this:

Code:
C2/6469: AD BC 3E     LDA $3EBC
C2/646C: 29 08        AND #$08
C2/646E: F0 06        BEQ $6476 // branch to line Gogo jump line
C2/6470: BD D8 3E     LDA $3ED8,X
with
C2/6473: 4C CC 56     JMP $56CC
C2/6476: 4C E5 56     JMP $56E5


Still, when I tested this, something was wrong and I wasn't sure where I erred. I NOPed the branch and realized it was not producing the behavior I intended. I replaced the line at C2/646E with the following:


Code:
C2/646E: D0 06        BNE $6476 // branch to line before Gogo jump line

I changed the branch if equal check to a branch if not equal check...and it was a success!


FINAL REPLACEMENT CODE (HEX)

Code:
C2/56C9: 4C 69 64     JMP $6469

Code:
C2/6469: AD BC 3E     LDA $3EBC
C2/646C: 29 08        AND #$08
C2/646E: D0 06        BNE $6476 // branch to Gogo jump line
C2/6470: BD D8 3E     LDA $3ED8,X
with
C2/6473: 4C CC 56     JMP $56CC
C2/6476: 4C E5 56     JMP $56E5

All that trouble for 19 bytes!

To be sure, there are much more efficient ways to accomplish what little I did, and it wouldn't surprise me if there were a patch out there that had this same functionality.  But I did learn from this experience, and I hope someone else would learn from my mistakes as well.
  Find
Quote  



Forum Jump:


Theme by Madsiur2017Custom Graphics by JamesWhite