;==========================================================================================
;
;   UNOFFICIAL SNES POWERPAK FIRMWARE V2.01 (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

UpCheck:
	lda Joy1+1
	and #%00001000
	beq UpDone

UpPressed:
	lda cursorYCounter
	bne UpDone
	lda scrollYCounter
	bne UpDone

UpScroll:
	lda #$08
	sta speedScroll
	lda #$01
	sta speedCounter

	jump jScrollUp

	ldx #$0015				; 21 frames
-
	wai
	lda Joy1+1
	and #%00001000
	beq UpDone
	dex
	cpx #$0000
	bne -

UpHeld:
	lda cursorYCounter
	bne UpDone
	lda scrollYCounter
	bne UpDone

UpScroll2:
	lda #$08
	sta speedScroll
	lda #$01
	sta speedCounter

	jump jScrollUp

	ldx #$0003				; 3 frames
-
	wai
	lda Joy1+1
	and #%00001000
	beq UpDone
	dex
	cpx #$0000
	bne -
	bra UpHeld

UpDone:



DownCheck:
	lda Joy1+1
	and #%00000100
	beq DownDone

DownPressed:
	lda cursorYCounter
	bne DownDone
	lda scrollYCounter
	bne DownDone

DownScroll:
	lda #$08
	sta speedScroll
	lda #$01
	sta speedCounter

	jump jScrollDown

	ldx #$0015				; 21 frames
-
	wai
	lda Joy1+1
	and #%00000100
	beq DownDone
	dex
	cpx #$0000
	bne -

DownHeld:
	lda cursorYCounter
	bne DownDone
	lda scrollYCounter
	bne DownDone

DownScroll2:
	lda #$08
	sta speedScroll
	lda #$01
	sta speedCounter

	jump jScrollDown

	ldx #$0003				; 3 frames
-
	wai
	lda Joy1+1
	and #%00000100
	beq DownDone
	dex
	cpx #$0000
	bne -
	bra DownHeld

DownDone:



LeftCheck:
	lda Joy1Press+1
	and #%00000010
	beq LeftDone

LeftPressed:
	lda cursorYCounter
	bne LeftDone
	lda scrollYCounter
	bne LeftDone

LeftScroll:
	lda #$04
	sta speedScroll
	lda #$02
	sta speedCounter

	jump jScrollUp

LeftDone:



RightCheck:
	lda Joy1Press+1
	and #%00000001
	beq RightDone

RightPressed:
	lda cursorYCounter
	bne RightDone
	lda scrollYCounter
	bne RightDone

RightScroll:
	lda #$04
	sta speedScroll
	lda #$02
	sta speedCounter

	jump jScrollDown

RightDone:



LCheck:
	lda Joy1New
	and #%00100000
	beq LDone

	jsr PageUp

LDone:



RCheck:
	lda Joy1New
	and #%00010000
	beq RDone

	jsr PageDown

RDone:



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

	lda #'S'
	sta bufferType

	jump jDirGetEntry			; get selected entry

	lda tempEntry.tempDirFlag		; check if directory
	cmp #$01
	beq +

	jmp NextModuleSOI

+	jump jNextDir

__ACheckDone:



BCheck:						; B = go up one directory
	lda Joy1New+1
	and #%10000000
	beq __BCheckDone

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

	lda sourceCluster+1
	cmp rootDirCluster+1
	bne __NotRootDir

	lda sourceCluster+2
	cmp rootDirCluster+2
	bne __NotRootDir

	lda sourceCluster+3
	cmp rootDirCluster+3
	bne __NotRootDir

	lda nextModule				; ... if yes, go to previous module, depending on setting
	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)

__NotRootDir:
	lda #$00				; otherwise, load entry $0001, which is
	sta selectedEntry+1			; always "/.." except for when already in root dir
	lda #$01
	sta selectedEntry

	lda #'S'
	sta bufferType

	jump jDirGetEntry

	jump jNextDir

__BCheckDone:

	jmp SRAMBrowserLoop



NextModuleSOI:
	ldy #$0000

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

;	stz saveName+$35			; save name is NUL-terminated in SO.MAP

	lda saveName.sCluster			; copy save cluster to source cluster
	sta sourceCluster
	lda saveName.sCluster+1
	sta sourceCluster+1
	lda saveName.sCluster+2
	sta sourceCluster+2
	lda saveName.sCluster+3
	sta sourceCluster+3

	lda #%00000001
	sta CONFIGWRITESTATUS			; no more battery flag

	lda nextModule
	cmp #'I'
	beq NextModuleSI

__NextModuleSOcancel:
	FindFile "SO.MAP"
	
	jmp NextModuleSOIDone



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

NextModuleSOIDone:

	jump jCardLoadModule			; load options or intro module



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

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