;==========================================================================================
;
;   UNOFFICIAL SNES POWERPAK FIRMWARE V2.01 (CODENAME: "SIMBA")
;   (c) 2012/2013 by ManuLöwe (http://www.manuloewe.de/)
;
;	*** CF MODULE: SQ (QUESTIONS) ***
;	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



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

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

	ClearLine 9				; hide firmware version strings
	ClearLine 10

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

	wai

	SetCursorPos 11, 0
	PrintString "The game you've played features battery-backed SRAM\n"
	PrintString "to save your progress. Please choose an option:"

	SetCursorPos 14, 1

	jsr LoadLastGame			; load last game info

	lda saveName.sCluster			; if cluster=0, no file loaded previously
	bne __AutoSaveEntry
	lda saveName.sCluster+1
	bne __AutoSaveEntry
	lda saveName.sCluster+2
	bne __AutoSaveEntry
	lda saveName.sCluster+3
	bne __AutoSaveEntry

	PrintString "(No SRAM file loaded previously, auto-saving disabled)"

	lda #$A0
	sta cursorY

	bra __AutoSaveMenuNext

__AutoSaveEntry:
	lda #$88				; cursor line = (cursorY - $18) / $08 (in this case, $0E=14)
	sta cursorY

	PrintString "Save SRAM to the previously loaded file:"

	ldy #$0000

-	lda saveName, y				; copy save name to tempEntry for printing
	sta tempEntry, y
	iny
	cpy #$0038				; only copy 56 characters
	bne -

	SetCursorPos 15, 1
	jsr PrintTempEntry

__AutoSaveMenuNext:
	SetCursorPos 17, 1
	PrintString "Select a file ..."

	SetCursorPos 18, 1
	PrintString "Cancel and discard SRAM!"

	lda #$0D
	sta cursorX



; *************************** Questions loop ***************************

QuestionsLoop:
	wai

	lda Joy1New				; check for A button = make a selection
	and #%10000000
	bne ChooseButton

__ACheckDone:



	lda Joy1New+1				; check for d-pad up
	and #%00001000
	beq __UpCheckDone

	jsr PrevButton				; up pressed

__UpCheckDone:



	lda Joy1New+1				; check for d-pad down
	and #%00000100
	beq __DownCheckDone

	jsr NextButton				; down pressed

__DownCheckDone:



	bra QuestionsLoop



; ************************* Selection handler **************************

ChooseButton:
	lda cursorY
	cmp #$88				; if at "Save to previous file", do just that :-)
	beq __AutoSave

	cmp #$A0				; if at "Select a file", go to SRAM browser
	bne __CancelSaving
	jmp NextModuleSB

__CancelSaving:
	bra NextModuleSI			; else, cursor must be at "Cancel" (cursorY=$A8), so go to intro



__AutoSave:
	lda saveName.sCluster			; if cluster=0, no file loaded ...
	bne __AutoSaveComplete
	lda saveName.sCluster+1
	bne __AutoSaveComplete
	lda saveName.sCluster+2
	bne __AutoSaveComplete
	lda saveName.sCluster+3
	bne __AutoSaveComplete

	bra QuestionsLoop			; ... so don't do anything

__AutoSaveComplete:
	jsr SaveFile

__WaitForUserInput:
	wai
	lda Joy1New+1
	and #%11110000				; B, Y, Select, Start
	bne NextModuleSI

	lda Joy1New
	and #%11110000				; A, X, L, R
	bne NextModuleSI

	bra __WaitForUserInput



NextModuleSI:
	lda #%00000001				; no more battery flag
	sta CONFIGWRITESTATUS

	FindFile "SI.MAP"

	jump jPrintClearScreen

	jump jLoadRomVersion

	jump jCardLoadModule			; load intro module



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

	lda #$FF				; hide logo
	sta SpriteBuf1+17
	sta SpriteBuf1+21
	sta SpriteBuf1+25
	sta SpriteBuf1+29
	sta SpriteBuf2+1			; 32x32 sprites

	jump jPrintClearScreen

	SetCursorPos 11, 11
	PrintString "Loading ..."

	lda #'I'
	sta nextModule				; go to intro when done

	FindFile "SB.MAP"

	jump jCardLoadModule			; go to SRAM browser



; ************************* Selection cycling **************************

NextButton:					; $88 -> $A0 -> $A8
	lda cursorY
	clc
	adc #$08
	sta cursorY
	cmp #$B0
	bne __NextButtonDone

	lda #$88
	sta cursorY
	bra __NextButtonDone2

__NextButtonDone:
	lda cursorY
	cmp #$90
	bne __NextButtonDone2
	lda #$A0
	sta cursorY

__NextButtonDone2:

rts



PrevButton:					; $A8 -> $A0 -> $88
	lda cursorY
	sec
	sbc #$08
	sta cursorY
	cmp #$80
	bne __PrevButtonDone

	lda #$A8
	sta cursorY
	bra __PrevButtonDone2

__PrevButtonDone:
	lda cursorY
	cmp #$98
	bne __PrevButtonDone2
	lda #$88
	sta cursorY

__PrevButtonDone2:

rts



; ************************* Actual SRAM saving *************************

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

	ClearLine 17
	ClearLine 18

	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 #$00
	sta DMAWRITELO
	sta DMAWRITEHI
	lda #$F8
	sta DMAWRITEBANK

	SetCursorPos 17, 0			; these msgs are shown when saving a previously loaded 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."
rts



PrintTempEntry:
	stz tempEntry+56			; NUL-terminate entry string after 56 characters

	lda #<tempEntry
	sta strPtr
	lda #>tempEntry
	sta strPtr+1

	ldy #strPtr

	PrintString "%s"
rts



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

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