Language…
7 users online: Firstnamebutt, netyo, playagmes169, pnaha, qantuum, tOaO, Zavok - Guests: 257 - Bots: 446
Users: 64,795 (2,378 active)
Latest user: mathew

Save routine patch help

First I want to clarify that I do know MarioE made a patch that optimizes saving and stuff and that might just solve my problem. However this is my first hack which I am making primarily to learn, and when I decided to implement a collectible items mechanic (like the star coins from NSMB) that was the first time I had to actually patch the game's code (because I have to store it to SRAM and stuff).

Here is my code:

Code
header
lorom

!Freespace = $158000			;Point this to your goddam freespace before complaining

org $009BC9

	JSL STORESRAM			;Hijack save game routine
	NOP				;\Two NOPs to fill out empty space
	NOP				;/

org !Freespace

!Codelength = End-Code
db "STAR"
dw !Codelength-1
dw !Codelength-1^$FFFF
Code:

	STORESRAM:

		PHP				;Preserve processor flags
		SEP #$30			;A and index 8 bit
		LDA $010A			;Load save file
		ASL A				;\
		ASL A				; |Multiply by 8 since each file uses 8 bytes
		ASL A				;/
		TAX				;X now contains save file times 8 (0, 8 or 16)
		REP #$20			;A 16 bit
		LDA $06F9			;\
		STA $70035A,x			; |
		LDA $06FB			; |
		STA $70035C,x			; |Store Buffer in RAM to SRAM, offset by file
		LDA $06FD			; |
		STA $70035E,x			; |
		LDA $06FF			; |
		STA $700360,x			;/
		SEP #$20			;A 8 bit
		PLP				;Restore processor flags

		PHB				;\
		PHK				; |The code the JSL to STORESRAM overwrites
		PLB				; |
		LDX $010A			;/

		RTL				;End custom save routine

End:

What I wanted to happen was for the game to just store my buffer ($06F9) to SRAM when the player saves the game. What actually happens is that the game freezes (music keeps going tho) when the overworld loads.

EDIT: I thought the ROM might be corrupted so I tested with a fresh one. Same results.

allow shy guy emojis in post footers you cowards!
You're using a JSL to hijack some stack-using opcodes. That won't work; you must use JML instead, and a JML back to return (you can stick a label after the JML and aim for that).

Additionally, it's hard to stick a hijack into a PHK; it acts differently if you move it.
There are two solutions. Method 1: Replace it with LD_ #$02 : PH_ (replace _ with whichever of A, X or Y is 8bit and unused at the moment; X seems promising). Method 2: Just move the hijack to two bytes later, and leave the PHB and PHK there; you've got two NOPs there you don't need.

Finally, you've got a SEP #$20 before a PLP; that does nothing useful. Delete the SEP. The existence, or lack thereof, of that SEP won't create any bugs, but there's no point adding unneeded opcodes.
<blm> zsnes users are the flatearthers of emulation
Awwww yeah! I tried your magic and find myself incredibly relieved to see the game not crashing anymore! Also, for future reference, how would I know I can't hijack with JSL?

allow shy guy emojis in post footers you cowards!
Quote
Also, for future reference, how would I know I can't hijack with JSL?


In this particular instance you can't because the code you're overwriting is a stack related, as Alcaro said. I'm sure you know what PHB, PHK, and PLB do. That being said, when you JSL it relies on the stack to hold where it should return to when you hit the RTL at the end. However, since you mess with the stack with the PHB : PHK : PLB, it gets confused and doesn't know where to return to. That's why you don't want to JSL and RTL here.

e: Thought my layout was stretching the tables, but nope it's not me.

Give Mario some love?~