fastrom

!SRAMSize = #$01	;SRAM Size amount of bytes = 2048*decimal value (game default: #$01)

incsrc CoinDefines.asm

macro LoadIfStarted()
TYA
LSR A
TAY
LDA #$FFFE		; If you don't want this code comment it out
-			; It checks if the file has been started and if not it won't load anything to RAM
CLC			; and it will clear the SRAM
ADC #$008F
DEY
BPL -
TAX
LDA $700000,x
BEQ INITSRAM
ORA $700000-2,x
CMP #$6060
BEQ INITSRAM
endmacro

org $00FFD8
	db !SRAMSize	

org $009BC9		; Save Game Routine
autoclean JML SaveGame
	;PHB	; replaced opcodes
	;PHK
	;PLB
	;LDX $010A

org $009E5C		; Load Game Routine (executed when 1 or 2 player is chosen)
autoclean JML LoadGame
	;STZ $0DD5	; replaced opcodes
	;STZ $0DB3

org $009B3F		; Erase gane routine
autoclean JML EraseGame
	;BNE $2C
	;LDY #$02

freecode

print "Code inserted at: ", pc

SAVETable:
; amount of bytes to save : Address to save : SRAM address to save to
dw $0001 : dl !ramspace : dl $700400
dw $0000	; End

INC:
dw $0000 : dw $0030 : dw $0060		; Amount of bytes to increase by when using a different file (1-3)
					; Low and High byte
BINC:
db $00 : db $00 : db $00		; Amount of bytes to increase by when using a different file (1-3)
					; Bank byte

macro GetReady()
LDA SAVETable+2,x	;\ Get RAM address
STA $00			;/
LDA SAVETable+5,x	;\
CLC			; | Get SRAM address
ADC $0B			; |
STA $03			;/
SEP #$20		;
LDA SAVETable+4,x	;\ Get bank byte for RAM
STA $02			;/
LDA SAVETable+7,x	;\
CLC			; | Get the bank byte for SRAM
ADC $0D			; |
STA $05			;/
REP #$20		;
LDY #$0000		; Y = #$0000
endmacro

SaveGame:
PHK			;\ Set DBR
PLB			;/
LDY $010A		; Get game file 
LDA BINC,y		;\ Get bank byte to increase by
STA $0D			;/
TYA			;\
ASL A			; | Y*2
TAY			;/
REP #$30		; A, X, Y = 16-bit
LDA INC,y		;\ Read low and high byte to increase by
STA $0B			;/
LDX #$0000		; X = #$0000
.setup			;
LDA SAVETable,x		;\ Get amount of bytes to write
BEQ .end		;/
STA $0E			; Store to scratch RAM
%GetReady()		; Set up RAM
.loop			;
SEP #$20		;
LDA [$00],y		; Load RAM
STA [$03],y		; Store to SRAM
REP #$20		;
INY			; Y+1
DEC $0E			; Check if we wrote to all SRAM
BNE .loop		; Back if we haven't
TXA			; X->A
INC A			; A+1
ASL A			;\
ASL A			; | A*8
ASL A			;/
TAX			; A->X
BRA .setup		; Back we go

.end
SEP #$30		;
PHB			;\
PHA			; | Set DBR
PLB			;/
LDX $010A		; Get game file
JML $809BCF		;


LoadGame:
STZ $0DD5		;\ Restore old code
STZ $0DB3		;/
PHB			;
PHK			;
PLB			;
LDY $010A		; Get game file 
LDA BINC,y		; Get bank byte to increase by
STA $0D			;
TYA			;\
ASL A			; | Y*2
TAY			;/
REP #$30		; A, X, Y = 16-bit
LDA INC,y		;\ Read low and high byte to increase by
STA $0B			;/
%LoadIfStarted()	;
LDX #$0000
.setup
LDA SAVETable,x
BEQ .end
STA $0E
%GetReady()
.loop
SEP #$20
LDA [$03],y
STA [$00],y
REP #$20
INY
DEC $0E
BNE .loop
TXA
INC A
ASL A
ASL A
ASL A
TAX
BRA .setup

.end
EndLoad:
SEP #$30
PLB
JML $809E62


EraseGame:
BEQ +
JML $809B6D           

INITSRAM:
SEP #$30
LDA $010A
CLC
ADC #$03
EOR #$07
BRA INITIT

+
PHB			;\
PHK			; | Set DBR
PLB			;/
LDA $0DDE		;\ If we don't want to erase any file, don't even waste time
BEQ EndIt		;/
INITIT:			;
STA $0D			; Store the files we want to erase in $0D
REP #$10		;
Redo:			;
SEP #$20		;
LDY #$0002		;\
LDX #$0000		; |
LDA $0D			; |
BEQ EndIt		; |
AND #$01		; |
BNE Clear		; |
DEY			; | Erase the correct SRAM addr.
LDA $0D			; |
AND #$02		; |
BNE Clear		; |
DEY			; |
LDA $0D			; |
AND #$04		; |
BNE Clear		;/
EndIt:			;
SEP #$20		;
LDA $0DDE		;\
CMP #$0F		; | If this is not the erase game mode, branch
BEQ EndLoad		;/
PLB			;
SEP #$10		;
LDY #$02		; Old code
JML $809B43  		; Return

Clear:
TRB $0D
LDA SAVETable+7,x	;\
CLC			; | Get the bank byte for SRAM
ADC BINC,y		; |
STA $05			;/
REP #$20		; A, X, Y = 16-bit
LDA SAVETable,x		;\ 
BEQ Redo		; | Read size
STA $0E			;/
TYA			;\
AND #$00FF		; |
ASL A			; | Y*2
TAY			;/
LDA SAVETable+5,x	;\
CLC			; | Get SRAM address
ADC INC,y		; |
STA $03			;/
LDY #$0000		; Y = #$0000
.loop			;
SEP #$20		;
LDA #$00		;\ Write zero to SRAM
STA [$03],y		;/
REP #$20		;
INY			; Y+1
DEC $0E			;\ Loop until we wrote everything
BNE .loop		;/
TXA			;\
INC A			; |
ASL A			; | Get the next table entry
ASL A			; |
ASL A			; |
TAX			; |
SEP #$20		;/
BRA Clear+2		;
