;==========================================================================================
;
;   UNOFFICIAL SNES POWERPAK FIRMWARE V2.02 (CODENAME: "SIMBA")
;   (c) 2012/2013 by ManuLöwe (http://www.manuloewe.de/)
;
;	*** CF MODULE: SN (ROM BROWSER & LOADER) ***
;	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  |  S  |  S  |  G  |  S  |  B  |  F  |     |     |     |     |
; ------------------------------------------------------------------------------
; extMatch2 |  M  |  W  |  F  |  D  |  P  |  I  |  I  |     |     |     |     |
; ------------------------------------------------------------------------------
; extMatch3 |  C  |  C  |  C  |  3  |  C  |  N  |  G  |     |     |     |     |
; ------------------------------------------------------------------------------
;
; 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 #$07				; number of file types to look for (7, see table above)
	sta extNum
	stz extNum+1

	lda #'S'				; using the least possible amount of lda/sta commands
	sta extMatch1
	sta extMatch1+1
	sta extMatch1+2
	sta extMatch1+4

	lda #'M'
	sta extMatch2

	lda #'W'
	sta extMatch2+1

	lda #'F'
	sta extMatch1+6
	sta extMatch2+2

	lda #'P'
	sta extMatch2+4

	lda #'C'
	sta extMatch3
	sta extMatch3+1
	sta extMatch3+2
	sta extMatch3+4

	lda #'G'
	sta extMatch1+3
	sta extMatch3+6

	lda #'D'
	sta extMatch2+3

	lda #'3'
	sta extMatch3+3

	lda #'B'
	sta extMatch1+5

	lda #'I'
	sta extMatch2+5
	sta extMatch2+6

	lda #'N'
	sta extMatch3+5

	InitFileBrowser



FileBrowserLoop:
	wai

.IFDEF SHOWDEBUGMSGS

	SetCursorPos 22, 24
	PrintHexNum selectedEntry+1
	PrintString "-"
	PrintHexNum selectedEntry
	stz BGPrintMon

	SetCursorPos 23, 24
	PrintHexNum filesInDir+1
	PrintString "-"
	PrintHexNum filesInDir
	stz BGPrintMon

.ENDIF

	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 game / load dir
	beq __ACheckDone

	lda #'S'
	sta bufferType

	jump jDirGetEntry			; get selected entry

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

	jmp NextModule

+	jump jNextDir

__ACheckDone:

	lda Joy1New+1
	and #%10000000				; B = go up one directory
	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

	jmp NextModuleSI			; ... if so, load intro module

+	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:

	lda Joy1New+1
	and #%00010000				; Start = load last game info
	beq __StartCheckDone

	jsr LoadLastGame

	jmp NextModuleSOLast

__StartCheckDone:

	jmp FileBrowserLoop			; end of loop



NextModule:
	ldy #$0000

	rep #A_8BIT				; A = 16 bit

-	lda tempEntry, y			; copy game name
	sta gameName, y
	iny
	iny
	cpy #$0080				; 128 bytes
	bne -

	lda gameName.gCluster			; copy game cluster to source cluster
	sta sourceCluster

	lda gameName.gCluster+2
	sta sourceCluster+2

	sep #A_8BIT				; A = 8 bit

	lda #<sectorBuffer1
	sta destLo
	lda #>sectorBuffer1
	sta destHi				; put first sector into sector RAM
	stz destBank

	stz sectorCounter
	stz bankCounter

	jump jClusterToLBA			; sourceCluster -> first sourceSector

	lda #kDestWRAM
	sta destType

	jump jCardReadSector			; sector -> WRAM

	ldy #$0000
	
	lda sectorBuffer1, y			; check for ASCII string "SNES-SPC700"
	cmp #'S'
	bne NextModuleSA

	iny

	lda sectorBuffer1, y
	cmp #'N'
	bne NextModuleSA

	iny

	lda sectorBuffer1, y
	cmp #'E'
	bne NextModuleSA

	iny

	lda sectorBuffer1, y
	cmp #'S'
	bne NextModuleSA

	iny

	lda sectorBuffer1, y
	cmp #'-'
	bne NextModuleSA

	iny

	lda sectorBuffer1, y
	cmp #'S'
	bne NextModuleSA

	iny

	lda sectorBuffer1, y
	cmp #'P'
	bne NextModuleSA

	iny

	lda sectorBuffer1, y
	cmp #'C'
	bne NextModuleSA

	iny

	lda sectorBuffer1, y
	cmp #'7'
	bne NextModuleSA

	iny

	lda sectorBuffer1, y
	cmp #'0'
	bne NextModuleSA

	iny

	lda sectorBuffer1, y
	cmp #'0'
	bne NextModuleSA

	jmp NextModuleSP			; SPC file detected, go to player



NextModuleSA:
	jsr ShowLoadingMsg

	FindFile "SA.MAP"

	jump jCardLoadModule			; load auto save loading module



NextModuleSI:
	FindFile "SI.MAP"

	jump jPrintClearScreen

	jump jLoadRomVersion

	jump jCardLoadModule			; return to titlescreen



NextModuleSP:
	jsr ShowLoadingMsg

	stz nextModule				; make sure not to leave SPC player again

	FindFile "SP.MAP"

	jump jCardLoadModule			; load SPC player module



NextModuleSOLast:
	jsr ShowLoadingMsg

	FindFile "SO.MAP"

	jump jCardLoadModule			; load options module



; ************************ Show loading message ************************

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

	stz scrollY				; make sure prior scrolling won't affect string position
	stz scrollYCounter
	stz scrollYUp
	stz scrollYDown
	stz speedCounter
	stz speedScroll

	jump jPrintClearScreen

	SetCursorPos 11, 11
	PrintString "Loading ..."
rts



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

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

	.INCLUDE "global_func_pagenav.inc.asm"
