;==========================================================================================
;
;   UNOFFICIAL SNES POWERPAK FIRMWARE V2.01 (CODENAME: "SIMBA")
;   (c) 2012/2013 by ManuLöwe (http://www.manuloewe.de/)
;
;	*** CF MODULE: SC (CUSTOMIZATION) ***
;
;==========================================================================================



; ****************************** 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

	jump jPrintClearScreen

; ------ V2.00 DEBUG STUFF ---------------------
;	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 2, 2
	PrintString "Set DMA mode (currently "	; function DMACursorX completes this ;-)

	SetCursorPos 4, 3
	PrintString "on      off"

	SetCursorPos 7, 2
	PrintString "Choose logo and cursor:"

	SetCursorPos 9, 3
	PrintString "Power   Simba   SFC dots"

	SetCursorPos 14, 2
	PrintString "Choose background design:"

	SetCursorPos 16, 3
	PrintString "Ocean   Aero    Flare   Nova    Dune    Sky"

	SetCursorPos 22, 10
	PrintString " Start: apply"

	jsr GetCurrentLogo
	jsr ShowLogo

	lda #$38				; put cursorY on DMA mode line
	sta cursorY

	jsr DMACursorX				; look up DMA setting for correct cursorX position

	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



; ************************ Customization loops *************************

DMALoop:
	wai

; ------ V2.00 DEBUG STUFF ---------------------
;	SetCursorPos 4, 22
;	PrintNum configDMA
;	PrintNum dontUseDMA
; ------ END DEBUG -----------------------------

	lda Joy1New+1				; check for d-pad left
	and #%00000010
	beq __LeftCheck1Done

	lda configDMA
	cmp #$00				; only if current DMA setting is "off" ...
	bne __LeftCheck1Done

	lda #$01				; ... switch it on ...
	sta configDMA
	jsr DMACursorX				; ... and move the cursor

__LeftCheck1Done:



	lda Joy1New+1				; check for d-pad right
	and #%00000001
	beq __RightCheck1Done

	lda configDMA
	cmp #$01				; only if current DMA setting is "on" ...
	bne __RightCheck1Done

	lda #$00				; ... switch it off ...
	sta configDMA
	jsr DMACursorX				; ... and move the cursor

__RightCheck1Done:



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

	lda #$60				; down pressed
	sta cursorY
	jsr LogoCursorX				; check for current logo no. & set cursorX accordingly
	bra LogoLoop				; switch to logo loop

__DownCheck1Done:



	lda Joy1Press+1				; check for Start button
	and #%00010000
	beq __StartCheck1Done

	jmp ResetSystem				; Start pressed, reset to intro

__StartCheck1Done:

	bra DMALoop



LogoLoop:
	wai

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

	lda #$38				; up pressed
	sta cursorY
	jsr DMACursorX				; set cursorX according to current DMA setting
	bra DMALoop				; and switch to DMA loop

__UpCheck2Done:



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

	lda #$98				; down pressed, switch to design loop
	sta cursorY
	jsr DesignCursorX			; check for current design no. & set cursorX accordingly
	bra DesignLoop

__DownCheck2Done:



	lda Joy1New+1				; check for d-pad left
	and #%00000010
	beq __LeftCheck2Done

	lda configLogo
	cmp #$01				; if current logo is logo #1, don't do anything
	beq __LeftCheck2Done

	dec configLogo				; otherwise, decrease logo no.
	jsr LogoCursorX				; and move the cursor
	jsr GetCurrentLogo			; and reload logo
	jsr ShowLogo

__LeftCheck2Done:



	lda Joy1New+1				; check for d-pad right
	and #%00000001
	beq __RightCheck2Done

	lda configLogo
	cmp #$03				; if current logo is logo #3, don't do anything
	beq __RightCheck2Done

	inc configLogo				; otherwise, increase logo no.
	jsr LogoCursorX				; and move the cursor
	jsr GetCurrentLogo			; and reload logo
	jsr ShowLogo

__RightCheck2Done:



	lda Joy1Press+1				; check for Start button
	and #%00010000
	beq __StartCheck2Done

	jmp ResetSystem				; Start pressed, reset to intro

__StartCheck2Done:

	bra LogoLoop



DesignLoop:
	wai

; ------ V2.00 DEBUG STUFF ---------------------
;	SetCursorPos 4, 22
;	PrintNum configBGdesign
; ------ END DEBUG -----------------------------

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

	lda #$60				; up pressed, switch to logo loop
	sta cursorY
	jsr LogoCursorX				; check for current logo no. & set cursorX accordingly
	jmp LogoLoop

__UpCheck3Done:



	lda Joy1New+1				; check for d-pad left
	and #%00000010
	beq __LeftCheck3Done

	lda configBGdesign
	cmp #$01				; if current design is #1, don't do anything
	beq __LeftCheck3Done

	dec configBGdesign			; otherwise, decrease design no.
	jsr DesignCursorX			; and move the cursor

__LeftCheck3Done:



	lda Joy1New+1				; check for d-pad right
	and #%00000001
	beq __RightCheck3Done

	lda configBGdesign
	cmp #$06				; if current design is #6, don't do anything
	beq __RightCheck3Done

	inc configBGdesign			; otherwise, increase design no.
	jsr DesignCursorX			; and move the cursor

__RightCheck3Done:



	lda Joy1Press+1				; check for Start button
	and #%00010000
	beq __StartCheck3Done

	jmp ResetSystem				; Start pressed, reset to intro

__StartCheck3Done:

	bra DesignLoop



; *********************** Get/show current logo ************************

GetCurrentLogo:
	lda configLogo				; checking current logo
	cmp #$02				; $02 = Simba
	bne +
	jsr PrepareLogoSimba
	bra GetCurrentLogoDone
+
	cmp #$03				; $03 = four dots
	bne +
	jsr PrepareLogoSFCdots
	bra GetCurrentLogoDone
+						; else it must be the standard PowerPak logo
	jsr PrepareLogoPowerPak

GetCurrentLogoDone:

rts



PrepareLogoSimba:
	lda #$7C				; logo tile difference ($7C = Simba)
	sta temp
	lda #$02				; logo palette diff. ($02 = Simba)
	sta temp+1
	lda #$20				; tile num for "paw" cursor, next is palette
	sta temp+2
	lda #$02				; vhoopppc Vert Horiz priOrity Palette Charmsb
	sta temp+3
rts



PrepareLogoSFCdots:
	lda #$84				; logo tile diff. ($84 = SFC dots)
	sta temp
	lda #$04				; logo palette diff. ($04 = SFC dots)
	sta temp+1
	lda #$22				; tile num for "red dot" cursor, next is palette
	sta temp+2
	lda #$04				; vhoopppc Vert Horiz priOrity Palette Charmsb
	sta temp+3
rts



PrepareLogoPowerPak:
	lda #$00				; logo tile diff. ($00 = PowerPak)
	sta temp
;	lda #$00				; logo palette diff. ($00)
	sta temp+1
;	lda #$00				; standard PowerPak cursor
	sta temp+2
	lda #$06				; standard PowerPak cursor palette
	sta temp+3
rts



ShowLogo:
	lda #$9A				; X
	sta SpriteBuf1+16
	lda #$50				; Y
	sta SpriteBuf1+17
	lda #$04				; tile num
	clc
	adc temp				; plus diff, depending on current setting
	sta SpriteBuf1+18
	lda #$00				; vhoopppc Vert Horiz priOrity Palette Charmsb
	clc
	adc temp+1				; plus palette diff, depending on user setting
	sta SpriteBuf1+19

	lda #$BA				; X
	sta SpriteBuf1+20
	lda #$50				; Y
	sta SpriteBuf1+21
	lda #$08				; tile num
	clc
	adc temp				; plus diff, depending on current setting
	sta SpriteBuf1+22
	lda #$00				; vhoopppc Vert Horiz priOrity Palette Charmsb
	clc
	adc temp+1				; plus palette diff, depending on user setting
	sta SpriteBuf1+23

	lda #$9A				; X
	sta SpriteBuf1+24
	lda #$70				; Y
	sta SpriteBuf1+25
	lda #$44				; tile num
	clc
	adc temp				; plus diff, depending on current setting
	sta SpriteBuf1+26
	lda #$00				; vhoopppc Vert Horiz priOrity Palette Charmsb
	clc
	adc temp+1				; plus palette diff, depending on user setting
	sta SpriteBuf1+27

	lda #$BA				; X
	sta SpriteBuf1+28
	lda #$70				; Y
	sta SpriteBuf1+29
	lda #$48				; tile num
	clc
	adc temp				; plus diff, depending on current setting
	sta SpriteBuf1+30
	lda #$00				; vhoopppc Vert Horiz priOrity Palette Charmsb
	clc
	adc temp+1				; plus palette diff, depending on user setting
	sta SpriteBuf1+31

	lda #$AA
	sta SpriteBuf2+1			; 32x32 sprites

	lda temp+2				; tile num for cursor, next is palette
	sta SpriteBuf1+2
	lda temp+3				; vhoopppc Vert Horiz priOrity Palette Charmsb
	sta SpriteBuf1+3
rts



; ******************************* Reset ********************************

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

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

	jump jPrintClearScreen

	SetCursorPos 11, 9
	PrintString " Saving settings ..."

; ------ V2.00 DEBUG STUFF ---------------------
;	ldx #$0158				; 344 frames delay (to check message position)
;-
;	wai
;	dex
;	cpx #$0000
;	bne -
; ------ END DEBUG -----------------------------

	jsr SaveConfig

	lda #%10000001
	sta CONFIGWRITESTATUS			; ... reset PowerPak, stay in boot mode



; ************************** cursorX position **************************

DMACursorX:
	SetCursorPos 2, 14
	lda configDMA				; check for current DMA setting & set cursorX accordingly
	cmp #$00
	bne +
	lda #$40				; if DMA=0, cursorX=$40
	sta cursorX
	PrintString "off):"
	bra __DMACursorXDone
+
	lda #$20				; ... and $20 in any other case
	sta cursorX
	PrintString "on): "			; trailing space overwrites "off" setting's colon

__DMACursorXDone:

rts



LogoCursorX:
	lda configLogo				; check for current logo no. & set cursorX accordingly
	asl a
	asl a
	asl a
	asl a
	asl a
	sta cursorX
rts



DesignCursorX:
	lda configBGdesign			; check for current BG design & set cursorX accordingly
	asl a
	asl a
	asl a
	asl a
	asl a
	sta cursorX
rts



; ************************* Save configuration *************************

SaveConfig:
	FindFile "CONFIG.TXT"

	ldy #$0000				; let's completely rebuild everything

	lda #'D'
	sta sectorBuffer1, y
	iny
	lda #'M'
	sta sectorBuffer1, y
	iny
	lda #'A'
	sta sectorBuffer1, y
	iny
	lda #'='
	sta sectorBuffer1, y
	iny
	lda configDMA
	cmp #$00
	bne +
	lda #'0'
	sta sectorBuffer1, y
	bra __nextConfigChar1
+
	lda #'1'
	sta sectorBuffer1, y

__nextConfigChar1:
	iny

	lda #$0D				; $0D0A = next line
	sta sectorBuffer1, y
	iny
	lda #$0A
	sta sectorBuffer1, y
	iny

	lda #'L'
	sta sectorBuffer1, y
	iny
	lda #'O'
	sta sectorBuffer1, y
	iny
	lda #'G'
	sta sectorBuffer1, y
	iny
	lda #'O'
	sta sectorBuffer1, y
	iny
	lda #'='
	sta sectorBuffer1, y
	iny
	lda configLogo
	cmp #$02
	bne +
	lda #'2'
	sta sectorBuffer1, y
	bra __nextConfigChar2
+
	cmp #$03
	bne +
	lda #'3'
	sta sectorBuffer1, y
	bra __nextConfigChar2
+
	lda #'1'
	sta sectorBuffer1, y

__nextConfigChar2:
	iny

	lda #$0D				; $0D0A = next line
	sta sectorBuffer1, y
	iny
	lda #$0A
	sta sectorBuffer1, y
	iny

	lda #'B'
	sta sectorBuffer1, y
	iny
	lda #'G'
	sta sectorBuffer1, y
	iny
	lda #'D'
	sta sectorBuffer1, y
	iny
	lda #'E'
	sta sectorBuffer1, y
	iny
	lda #'S'
	sta sectorBuffer1, y
	iny
	lda #'I'
	sta sectorBuffer1, y
	iny
	lda #'G'
	sta sectorBuffer1, y
	iny
	lda #'N'
	sta sectorBuffer1, y
	iny
	lda #'='
	sta sectorBuffer1, y
	iny
	lda configBGdesign
	cmp #$02
	bne +
	lda #'2'
	sta sectorBuffer1, y
	bra __nextConfigChar3
+
	cmp #$03
	bne +
	lda #'3'
	sta sectorBuffer1, y
	bra __nextConfigChar3
+
	cmp #$04
	bne +
	lda #'4'
	sta sectorBuffer1, y
	bra __nextConfigChar3
+
	cmp #$05
	bne +
	lda #'5'
	sta sectorBuffer1, y
	bra __nextConfigChar3
+
	cmp #$06
	bne +
	lda #'6'
	sta sectorBuffer1, y
	bra __nextConfigChar3
+
	lda #'1'
	sta sectorBuffer1, y

__nextConfigChar3:
	iny

	lda #' '				; fill up the rest with spaces
-
	sta sectorBuffer1, y
	iny
	cpy #$0200
	bne -

	lda #<sectorBuffer1
	sta sourceLo
	lda #>sectorBuffer1
	sta sourceHi

	lda #kSourceWRAM
	sta sourceType

	jump jCardWriteFile
rts
