;==========================================================================================
;
;   UNOFFICIAL SNES POWERPAK FIRMWARE V2.01 (CODENAME: "SIMBA")
;   (c) 2012/2013 by ManuLöwe (http://www.manuloewe.de/)
;
;	*** CF MODULE: SA (AUTO SRAM MATCHING) ***
;	Code in this file based on v1.0X code written by:
;	- bunnyboy (SNES PowerPak creator), (c) 2009
;
;==========================================================================================



; ****************************** Includes ******************************

	.INCLUDE "module_rommakeup.inc"		; MemoryMap, VectorTable, HeaderInfo

	.INCLUDE "global_variables.asm"		; Global Variables



; ************************ File extension table ************************
;
; File extensions to look for, mapped to search variables:
;
;           |     | +1  | +2  | +3  | +4  | +5  | +6  | +7  | +8  | +9  | +10 |
; ------------------------------------------------------------------------------
; extMatch1 |  S  |     |     |     |     |     |     |     |     |     |     |
; ------------------------------------------------------------------------------
; extMatch2 |  R  |     |     |     |     |     |     |     |     |     |     |
; ------------------------------------------------------------------------------
; extMatch3 |  M  |     |     |     |     |     |     |     |     |     |     |
; ------------------------------------------------------------------------------
;
; When adding one or more extensions, don't forget to increase the value
; of variable "extNum" in the "SAVEDIR FOUND" section down below!



; ******************************** Main ********************************

Main:
	sep #A_8BIT				; 8 bit Accumulator, 16 bit X/Y
	rep #XY_8BIT

	stz Joy1New				; reset input buttons
	stz Joy1New+1
	stz Joy1Press
	stz Joy1Press+1

	FindFile "SAVES.   "			; "SAVES" directory

	lda #$01				; number of file types to look for (1, SRM only)
	sta extNum
	stz extNum+1

	lda #'S'
	sta extMatch1

	lda #'R'
	sta extMatch2

	lda #'M'
	sta extMatch3

	lda #'S'				; use SDRAM buffer
	sta bufferType

	jump jCardLoadDir			; load save dir (.SRM files)

	ldy #$0000

__GetROMExtPosition:				; check where ROM extension starts
	lda gameName, y
	cmp #'.'
	bne __IncPosition

	lda gameName+1, y
	cmp #'s'
	beq __ROMExtStartsWithS			; found .s (for .sfc, .smc, .swc ROMs)
	cmp #'S'
	beq __ROMExtStartsWithS			; found .S

	cmp #'g'
	beq __ROMExtStartsWithG			; found .g (for .gd3 ROMs)
	cmp #'G'
	beq __ROMExtStartsWithG			; found .G

	cmp #'f'
	beq __ROMExtStartsWithF			; found .f (for .fig ROMs)
	cmp #'F'
	beq __ROMExtStartsWithF			; found .F

	cmp #'b'
	beq __ROMExtStartsWithB			; found .b (for .bin ROMs)
	cmp #'B'
	beq __ROMExtStartsWithB			; found .B



__IncPosition:					; go to the next character
	iny
	cpy #$007A				; 122 chars
	bne __GetROMExtPosition
	bra __NoROMExtFound



__ROMExtStartsWithS:
	iny					; skip the "f"/"m"/"w", only check for "c"
	iny
	lda gameName+1, y
	cmp #'c'
	beq __ROMExtSuccess
	cmp #'C'
	beq __ROMExtSuccess
	bra __GetROMExtPosition



__ROMExtStartsWithG:
	iny					; skip the "d", only check for "3"
	iny
	lda gameName+1, y
	cmp #'3'
	beq __ROMExtSuccess
	bra __GetROMExtPosition



__ROMExtStartsWithF:
	iny					; skip the "i", only check for "g"
	iny
	lda gameName+1, y
	cmp #'g'
	beq __ROMExtSuccess
	cmp #'G'
	beq __ROMExtSuccess
	bra __GetROMExtPosition



__ROMExtStartsWithB:
	iny					; skip the "i", only check for "n"
	iny
	lda gameName+1, y
	cmp #'n'
	beq __ROMExtSuccess
	cmp #'N'
	beq __ROMExtSuccess
	bra __GetROMExtPosition



__NoROMExtFound:
	jmp __DirFindEntryLongFailed		; if no ROM extension is found at all, we can't go on searching



__ROMExtSuccess:
	iny					; to get rid of the "+1"

	sty temp				; save Y value

	jsr DirFindEntryLong			; start searching, back here means matching save file found

	ldy temp

__CopySaveName:
;	SetCursorPos 0, 0
;	PrintString "CopySaveName"
	lda tempEntry, y			; copy <SaveNameWeJustFound.srm>
	sta saveName, y
	dey
	bpl __CopySaveName

	lda tempEntry.tempCluster		; copy save cluster
	sta saveName.sCluster
	lda tempEntry.tempCluster+1
	sta saveName.sCluster+1
	lda tempEntry.tempCluster+2
	sta saveName.sCluster+2
	lda tempEntry.tempCluster+3
	sta saveName.sCluster+3



NextModule:
	FindFile "SO.MAP"

	jump jCardLoadModule			; load options module



DirFindEntryLong:
	rep #A_8BIT				; A = 16 bit

	stz selectedEntry			; reset selectedEntry

	lda filesInDir				; only do the search if directory isn't empty
	beq __DirFindEntryLongFailed



__DirFindEntryLongLoop:
	sep #A_8BIT
;	SetCursorPos 0, 0
;	PrintString "DirFindEntryLongLoop"

	lda #'S'				; use SDRAM buffer
	sta bufferType

	jump jDirGetEntry			; put entry into tempEntry

	ldy temp				; load index position of last extension character

	lda tempEntry, y			; check if entry matches, working backwards for efficiency :-)
	cmp #'m'
	beq +
	cmp #'M'
	beq +
	bra __IncrementEntryIdx
+
	dey
	lda tempEntry, y
	cmp #'r'
	beq +
	cmp #'R'
	beq +
	bra __IncrementEntryIdx
+
	dey
	lda tempEntry, y
	cmp #'s'
	beq _f
	cmp #'S'
	beq _f
	bra __IncrementEntryIdx
__
	dey
	lda tempEntry, y
	cmp gameName, y
	bne __IncrementEntryIdx
	cpy #$0000
	bne _b
rts						; all chars match



__IncrementEntryIdx:
	rep #A_8BIT

	inc selectedEntry			; increment entry index

	lda selectedEntry			; check for max. no. of files
	cmp filesInDir
	bne __DirFindEntryLongLoop



__DirFindEntryLongFailed:
	sep #A_8BIT

	pla					; clean up the stack as there's no rts from "jsr DirFindEntryLong"
	pla					; if no entry was found

	jmp NextModule
