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



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

; More includes at end of file!

	.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" a few lines 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

	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

	InitFileBrowser



SRAMBrowserLoop:
	wai

	DpadUpHold2Scroll

	DpadDownHold2Scroll

	DpadLeftScrollUp

	DpadRightScrollDown

	lda Joy1New
	and #%00100000				; left shoulder button
	beq __LCheckDone

	jsr PageUp

__LCheckDone:

	lda Joy1New
	and #%00010000				; right shoulder button
	beq __RCheckDone

	jsr PageDown

__RCheckDone:

	lda Joy1New
	and #%10000000				; A = select file / load dir
	beq __ACheckDone

	lda #'S'
	sta bufferType

	jump jDirGetEntry			; get selected entry

	lda tempEntry.tempFlags			; check for "dir" flag
	and #$01
	bne +

	jmp NextModuleSOI

+	jump jNextDir

__ACheckDone:

	lda Joy1New+1
	and #%10000000				; B = go up one directory / return to where we were
	beq __BCheckDone

	rep #A_8BIT

	lda sourceCluster			; check if current dir = root dir ...
	cmp rootDirCluster
	bne +

	lda sourceCluster+2
	cmp rootDirCluster+2
	bne +

	sep #A_8BIT

	lda nextModule				; ... if so, go back, depending on the previous module
	cmp #'O'
	beq __NextModuleSOcancel		; go back to options without an SRM file selected

	jmp __NextModuleSIcancel		; go back to SQ.MAP via SI.MAP ("battery used" flag is still set)

+	lda.w #$0001				; otherwise, load entry $0001, which is always "/.." (except for when in root dir)
	sta selectedEntry

	sep #A_8BIT

	lda #'S'
	sta bufferType

	jump jDirGetEntry

	jump jNextDir

__BCheckDone:

	jmp SRAMBrowserLoop			; end of loop



NextModuleSOI:
	rep #A_8BIT				; A = 16 bit

	ldy #$0000

-	lda tempEntry, y
	sta saveName, y
	iny
	iny
	cpy #$0080				; 128 bytes
	bne -

	lda saveName.sCluster			; copy save cluster to source cluster
	sta sourceCluster

	lda saveName.sCluster+2
	sta sourceCluster+2

	sep #A_8BIT				; A = 8 bit

	lda #%00000001
	sta CONFIGWRITESTATUS			; no more battery flag

	lda nextModule
	cmp #'I'
	beq NextModuleSI

__NextModuleSOcancel:
	FindFile "SO.MAP"
	
	jump jCardLoadModule			; load options module



NextModuleSI:
	lda #$00
	sta DMAWRITELO
	sta DMAWRITEHI
	lda #$F8
	sta DMAWRITEBANK

	lda #$FF				; hide cursor
	sta cursorX
	lda #$F0
	sta cursorY

	jump jPrintClearScreen

	SetCursorPos 17, 0			; these msgs are shown when saving a newly selected file
	PrintString "Saving SRAM file to CF card ..."

	lda #kSourceSDRAM
	sta sourceType

	jump jCardWriteFile

	SetCursorPos 17, 0
	PrintString "SRAM file saved successfully!  "	; trailing spaces make "ClearLine 17" unnecessary ;-)

	SetCursorPos 18, 0
	PrintString "Press any button to return to the titlescreen."

-	wai
	lda Joy1New+1				; wait for user input
	and #%11110000				; B, Y, Select, Start
	bne +
	lda Joy1New
	and #%11110000				; A, X, L, R
	bne +
	bra -
+

__NextModuleSIcancel:
	FindFile "SI.MAP"

	jump jPrintClearScreen

	jump jLoadRomVersion

	jump jCardLoadModule			; load intro module



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

	.INCLUDE "global_func_pagenav.inc.asm"	; Module-specific functions
