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



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

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

	lda #$D5
	sta DMAWRITELO
	lda #$FF
	sta DMAWRITEHI
	lda #$40
	sta DMAWRITEBANK			; destroy header $40FFC0
	lda #$00
	sta DMAREADDATA

	lda #$D5
	sta DMAWRITELO
	lda #$FF
	sta DMAWRITEHI
	lda #$00
	sta DMAWRITEBANK			; destroy header $FFC0
	lda #$00
	sta DMAREADDATA

	lda #$D5
	sta DMAWRITELO
	lda #$7F
	sta DMAWRITEHI
	lda #$00
	sta DMAWRITEBANK			; destroy header $7FC0
	lda #$00
	sta DMAREADDATA

;	lda #%00000011				; batt used, bootmode
;	sta CONFIGWRITESTATUS

	wai

	jump jPrintClearScreen

; ------ V2.00 DEBUG STUFF ---------------------
;	SetCursorPos 22, 1
;	PrintHexNum selectedEntry+1
;	PrintString "-"
;	PrintHexNum selectedEntry
;
;	SetCursorPos 2, 22
;	rep #A_8BIT
;	tsc					; print stack pointer (initial value: $1FFF)
;	sep #A_8BIT
;	xba
;	sta temp
;	PrintHexNum temp
;	xba
;	sta temp
;	PrintHexNum temp
; ------ END DEBUG -----------------------------

	SetCursorPos 3, 6
	PrintString "Load battery-backed SRAM ..."

	ldy #$0000

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

	SetCursorPos 4, 1
	jsr PrintTempEntry

	SetCursorPos 7, 6
	PrintString "Add/edit GameGenie codes ..."
	jsr PrintGGCodes

	SetCursorPos 20, 6
	PrintString "Play!"

	ldy #$0000

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

	SetCursorPos 21, 1
	jsr PrintTempEntry

	lda #$B8				; put cursor on "Play!" line
	sta cursorY
	lda #$38
	sta cursorX

	stz cursorYCounter			; reset scrolling parameters
	stz cursorYUp
	stz cursorYDown
	stz scrollY
	stz scrollYCounter
	stz scrollYUp
	stz scrollYDown

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



; **************************** Option loops ****************************

; cursorY values:
;	$30 = Load SRAM
;	$50 = Load GG TXT file
;	$60 = GG code 1
;	$70 = GG code 2
;	$80 = GG code 3
;	$90 = GG code 4
;	$A0 = GG code 5
;	$B8 = Play!

; cursorX values:
;	$38 = normal
;	$40 = indent



PlayLoop:
	wai

	lda Joy1New				; check for A button = start game
	and #%10000000
	beq __ACheck1Done

	jmp StartGame

__ACheck1Done:



	lda Joy1New+1				; check for Start button = start game
	and #%00010000
	beq __StartCheck1Done

	jmp StartGame

__StartCheck1Done:



	lda Joy1New+1				; check for B button = return to titlescreen
	and #%10000000
	beq __BCheck1Done

	jmp BackToIntro

__BCheck1Done:



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

	lda #$A0				; up pressed, switch to GG code loop
	sta cursorY

	jmp CheckIndent

__UpCheck1Done:



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

	lda #$30				; down pressed, switch to save RAM loop
	sta cursorY

	bra SaveRAMLoop

__DownCheck1Done:



	lda Joy1Press+1				; check for Select button = show decoded GG codes
	and #%00100000
	beq +					; if Select is released, clear decoded GG code lines

	jsr SelectButton

	bra __SelectCheck1Done
+
	jsr SelectReleased

__SelectCheck1Done:



	bra PlayLoop



SaveRAMLoop:
	wai

	lda Joy1New				; check for A button = launch SRAM browser
	and #%10000000
	beq __ACheck2Done

	lda #'O'
	sta nextModule				; return to options after picking SRAM file

	FindFile "SB.MAP"

	jump jCardLoadModule			; load SRAM browser

__ACheck2Done:



	lda Joy1New+1				; check for Start button = start game
	and #%00010000
	beq __StartCheck2Done

	jmp StartGame

__StartCheck2Done:



	lda Joy1New+1				; check for B button = return to titlescreen
	and #%10000000
	beq __BCheck2Done

	jmp BackToIntro

__BCheck2Done:



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

	lda #$B8				; up pressed, switch to play loop
	sta cursorY

	jmp PlayLoop

__UpCheck2Done:



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

	lda #$50				; down pressed, switch to GG code loop
	sta cursorY

	bra GGCodeLoop

__DownCheck2Done:



	lda Joy1Press+1				; check for Select button = show decoded GG codes
	and #%00100000
	beq +					; if Select is released, clear decoded GG code lines

	jsr SelectButton

	bra __SelectCheck2Done
+
	jsr SelectReleased

__SelectCheck2Done:



	jmp SaveRAMLoop



GGCodeLoop:
	wai

	lda Joy1New				; check for A button
	and #%10000000
	bne +

	jmp __ACheck3Done
+
	lda cursorY				; check cursor ($50 = above codes)
	cmp #$50
	bne +

	jsr ClearOutGG				; clear out all previously entered codes ...

	FindFile "SG.MAP"

	jump jCardLoadModule			; ... and load GameGenie TXT browser module
+
	jump jGameGenieNextChar			; otherwise, manipulate GG code

__ACheck3Done:



	lda Joy1New+1				; check for Start button = start game
	and #%00010000
	beq __StartCheck3Done

	jmp StartGame

__StartCheck3Done:



	lda Joy1New+1				; check for B button
	and #%10000000
	beq __BCheck3Done

	lda cursorY				; check cursor ($50 = above codes)
	cmp #$50
	bne +

	jmp BackToIntro				; return to titlescreen
+
	jump jGameGeniePrevChar			; otherwise, manipulate GG code

__BCheck3Done:



	lda Joy1New				; check for X/R button
	and #%01010000
	beq __XRCheckDone

	lda cursorY				; check cursor ($50 = above codes)
	cmp #$50
	beq __XRCheckDone

	jump jGameGenieNextChar			; manipulate GG code

__XRCheckDone:



	lda Joy1New+1				; check for Y/L button
	and #%01000000				; Y
	bne +

	lda Joy1New
	and #%00100000				; L
	bne +

	bra __YLCheckDone
+
	lda cursorY				; check cursor ($50 = above codes)
	cmp #$50
	beq __YLCheckDone

	jump jGameGeniePrevChar			; manipulate GG code

__YLCheckDone:



	lda Joy1Press				; check for L+R+X buttons
	and #%01110000
	cmp #%01110000
	bne __LRXCheckDone

	jsr ClearOutGG				; if all three are pressed, clear out all GG codes
	jsr PrintGGCodes			; and refresh code display

__LRXCheckDone:



	lda Joy1Press+1				; check for Select button = show decoded GG codes
	and #%00100000
	beq +					; if Select is released, clear decoded GG code lines

	jsr SelectButton

	bra __SelectCheck3Done
+
	jsr SelectReleased

__SelectCheck3Done:



NorthCheck:					; check for d-pad up, set cursorY accordingly
	lda Joy1New+1
	and #%00001000
	bne NorthGG5
	jmp SouthCheck

NorthGG5:
	lda cursorY
	cmp #$A0
	bne NorthGG4
	lda #$90
	sta cursorY
	bra CheckIndent

NorthGG4:
	lda cursorY
	cmp #$90
	bne NorthGG3
	lda #$80
	sta cursorY
	bra CheckIndent

NorthGG3:
	lda cursorY
	cmp #$80
	bne NorthGG2
	lda #$70
	sta cursorY
	bra CheckIndent

NorthGG2:
	lda cursorY
	cmp #$70
	bne NorthGG1
	lda #$60
	sta cursorY
	bra CheckIndent

NorthGG1:
	lda cursorY
	cmp #$60
	bne NorthGGLabel
	lda #$50
	sta cursorY
	bra CheckIndent

NorthGGLabel:					; cursorY = $50, switch to SRAM loop
	lda #$30
	sta cursorY
	lda #$38
	sta cursorX

	jmp SaveRAMLoop



CheckIndent:
	jsr AtIndent				; add indention if on GG code line
	lda temp
	cmp #$01
	bne +

	lda #$40				; on GG code line
	sta cursorX
	jmp CheckDone
+
	lda #$38				; not on GG code line
	sta cursorX
	jmp CheckDone



SouthCheck:					; check for d-pad down, set cursorY accordingly
	lda Joy1New+1
	and #%00000100
	beq WestCheck

SouthStart:
	lda cursorY
	cmp #$A0				; if at GG code #5, switch to "Play!" loop
	bne SouthGG4
	lda #$B8
	sta cursorY
	lda #$38
	sta cursorX
	jmp PlayLoop

SouthGG4:
	lda cursorY
	cmp #$90
	bne SouthGG3
	lda #$A0
	sta cursorY
	bra CheckIndent

SouthGG3:
	lda cursorY
	cmp #$80
	bne SouthGG2
	lda #$90
	sta cursorY
	bra CheckIndent

SouthGG2:
	lda cursorY
	cmp #$70
	bne SouthGG1
	lda #$80
	sta cursorY
	bra CheckIndent

SouthGG1:
	lda cursorY
	cmp #$60
	bne SouthGGLabel
	lda #$70
	sta cursorY
	bra CheckIndent

SouthGGLabel:
	lda #$60
	sta cursorY
	bra CheckIndent



WestCheck:					; check for d-pad left = move cursor left
	lda Joy1New+1
	and #%00000010
	beq __WestCheckDone

	jsr AtIndent				; but only if on GG code line ...
	lda temp
	cmp #$01
	bne __WestCheckDone

	lda cursorX
	cmp #$40				; ... and not at leftmost digit ...
	beq __WestCheckDone

	sec					; cursorX = cursorX - $10
	lda cursorX
	sbc #$10
	sta cursorX
	bra CheckDone

__WestCheckDone:



EastCheck:					; check for d-pad right = move cursor right
	lda Joy1New+1
	and #%00000001
	beq __EastCheckDone

	jsr AtIndent				; but only if on GG code line ...
	lda temp
	cmp #$01
	bne __EastCheckDone

	lda cursorX
	cmp #$B0				; ... and not at rightmost digit ...
	beq __EastCheckDone

	clc					; cursorX = cursorX + $10
	lda cursorX
	adc #$10
	sta cursorX

__EastCheckDone:



CheckDone:



	jmp GGCodeLoop



AtIndent:
	lda #$01
	sta temp

	lda cursorY				; check if we're above GG code lines
	cmp #$50
	beq +
rts

+
	lda #$00
	sta temp
rts



SelectButton:					; print decoded GG codes
	SetCursorPos 10, 8

	ldy #$0000
	jump jGameGenieDecode

	SetCursorPos 12, 8

	ldy #$0008
	jump jGameGenieDecode

	SetCursorPos 14, 8

	ldy #$0010
	jump jGameGenieDecode

	SetCursorPos 16, 8

	ldy #$0018
	jump jGameGenieDecode

	SetCursorPos 18, 8

	ldy #$0020
	jump jGameGenieDecode
rts



SelectReleased:
	ClearLine 10
	ClearLine 12
	ClearLine 14
	ClearLine 16
	ClearLine 18
rts



ClearOutGG:
;	phx
;	ldx #$0000
;	lda #$10				; $10 = underscore on GG code display
;-
;	sta gameGenie,x
;	inx
;	cpx #$0028				; 5 x 8 = 40 characters
;	bne -
;	plx

	ldy #$0000				; clear out codes one by one (might add way to clear out single codes ??)
	jump jGameGenieClear

	ldy #$0008
	jump jGameGenieClear

	ldy #$0010
	jump jGameGenieClear

	ldy #$0018
	jump jGameGenieClear

	ldy #$0020
	jump jGameGenieClear
rts



PrintGGCodes:					; this prints saved / previously entered codes (!)
	SetCursorPos 9, 7

	ldy #$0000
	jump jGameGeniePrint

	SetCursorPos 11, 7

	ldy #$0008
	jump jGameGeniePrint

	SetCursorPos 13, 7

	ldy #$0010
	jump jGameGeniePrint

	SetCursorPos 15, 7

	ldy #$0018
	jump jGameGeniePrint

	SetCursorPos 17, 7

	ldy #$0020
	jump jGameGeniePrint
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



StartGame:
	FindFile "SM.MAP"

	jump jCardLoadModule			; load mapper module



BackToIntro:
	FindFile "SI.MAP"

	jump jPrintClearScreen

	jump jLoadRomVersion

	jump jCardLoadModule			; return to titlescreen
