;==========================================================================================
;
;   UNOFFICIAL SNES POWERPAK FIRMWARE V2.02 (CODENAME: "SIMBA")
;   (c) 2012/2013 by ManuLöwe (http://www.manuloewe.de/)
;
;	*** CF MODULE: SD (DEVELOPER'S NOTE) ***
;
;==========================================================================================



; ****************************** 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 #$FF				; hide cursor
	sta cursorX
	lda #$F0
	sta cursorY

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

	lda #$00
	jump jspcPlay

	lda #224
	jump jspcSetModuleVolume



; **************************** Text page 1 *****************************

Show1stPage:
	jump jPrintClearScreen

 	SetCursorPos 2, 0
	PrintString "Greetings, fellow SNES gamer! :-)\n\n"
	PrintString "Thank you so much for downloading and installing my un-\n"
	PrintString "official firmware v2.02 for the SNES PowerPak.\n\n"
	PrintString "Even as a huge fan of this awesome flash cartridge ever\n"
	PrintString "since it was released in late 2009, I always felt bugged\n"
	PrintString "about its rather flat, quick-and-dirty style menu. Thus,\n"
	PrintString "I finally decided to try and give it a minor redesign\n"
	PrintString "during the summer of 2012.\n\n"
	PrintString "Starting off as a simple font hack, this project quickly\n"
	PrintString "evolved into a full-blown GUI overhaul as I discovered\n"
	PrintString "how much more was possible, and necessary. Eventually,\n"
	PrintString "after obtaining The Sourcecode(tm), I was able to pro-\n"
	PrintString "gress ever faster and smoother, the result of which is\n"
	PrintString "finally here for you to enjoy."

	SetCursorPos 22, 2
	PrintString " A: cont. - B/Y: start/stop music - Start: exit"

	jsr FromMosaic



Page1Loop:
	jump jspcProcess

	wai

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

	jsr ToMosaic				; A pressed, switch to page 2
	bra Show2ndPage

__ACheck1Done:

	lda Joy1New+1				; check for B button = (re-)start music
	and #%10000000
	beq __BCheck1Done

	lda #$00
	jump jspcPlay

	lda #224
	jump jspcSetModuleVolume

__BCheck1Done:

	lda Joy1New+1				; check for Y button = stop music
	and #%01000000
	beq __YCheck1Done

	jsr StopMusic

__YCheck1Done:

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

	jmp ResetSystem				; Start pressed, reset to intro

__StartCheck1Done:

	bra Page1Loop



; **************************** Text page 2 *****************************

Show2ndPage:
	jump jPrintClearScreen

 	SetCursorPos 2, 0
	PrintString "While horizontal high resolution (Mode 5) allows the\n"
	PrintString "filebrowser to display well over 50 characters per en-\n"
	PrintString "try, your PowerPak's fresh look is also comprised of a\n"
	PrintString "custom font, three fancy logo/cursor sprite sets and\n"
	PrintString "six individual HDMA background designs to choose from.\n\n"
	PrintString "In case you're wondering, the music playing in the back-\n"
	PrintString "ground was composed and arranged for the SNES by myself.\n"
	PrintString "Can you name all four SNES classics whose soundtracks\n"
	PrintString "I drew most of my inspiration from ...?\n\n"
	PrintString "Thanks for reading, but now please do start playing! :-)"

	SetCursorPos 16, 0
	PrintString "Long live the SNES PowerPak!\n\n"
	PrintString "(c) 2012/2013 by www.ManuLoewe.de"

	SetCursorPos 22, 2
	PrintString " A: back - B/Y: start/stop music - Start: exit"

	jsr FromMosaic



; ************************ PowerPak Logo page 2 ************************

	lda #$9A				; X
	sta SpriteBuf1+16
	lda #$80				; Y
	sta SpriteBuf1+17
	lda #$04				; tile num
	sta SpriteBuf1+18
	lda #$00				; vhoopppc Vert Horiz priOrity Palette Charmsb
	sta SpriteBuf1+19

	lda #$BA				; X
	sta SpriteBuf1+20
	lda #$80				; Y
	sta SpriteBuf1+21
	lda #$08				; tile num
	sta SpriteBuf1+22
	lda #$00				; vhoopppc Vert Horiz priOrity Palette Charmsb
	sta SpriteBuf1+23

	lda #$9A				; X
	sta SpriteBuf1+24
	lda #$A0				; Y
	sta SpriteBuf1+25
	lda #$44				; tile num
	sta SpriteBuf1+26
	lda #$00				; vhoopppc Vert Horiz priOrity Palette Charmsb
	sta SpriteBuf1+27

	lda #$BA				; X
	sta SpriteBuf1+28
	lda #$A0				; Y
	sta SpriteBuf1+29
	lda #$48				; tile num
	sta SpriteBuf1+30
	lda #$00				; vhoopppc Vert Horiz priOrity Palette Charmsb
	sta SpriteBuf1+31

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



Page2Loop:
	jump jspcProcess

	wai

	lda Joy1New				; check for A button
	and #%10000000
	beq __ACheck2Done

	lda #$FF
	sta SpriteBuf1+17			; A pressed, hide PowerPak logo and switch to page 1
	sta SpriteBuf1+21
	sta SpriteBuf1+25
	sta SpriteBuf1+29
	sta SpriteBuf2+1			; 32x32 sprites

	jsr ToMosaic

	jmp Show1stPage

__ACheck2Done:

	lda Joy1New+1				; check for B button = (re-)start music
	and #%10000000
	beq __BCheck2Done

	lda #$00
	jump jspcPlay

	lda #224
	jump jspcSetModuleVolume

__BCheck2Done:

	lda Joy1New+1				; check for Y button = stop music
	and #%01000000
	beq __YCheck2Done

	jsr StopMusic

__YCheck2Done:

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

	bra ResetSystem				; Start pressed, reset to intro

__StartCheck2Done:

	bra Page2Loop



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

ResetSystem:
	lda #$0E				; reduce screen brightness by 1
	sta $2100

	lda #224

-	wai

	sec					; fade out music within 224/8 = 28 frames (~ 0.5 seconds)
	sbc #8
	pha

	jump jspcSetModuleVolume

	jump jspcProcess

	pla

	pha					; make the screen fade to black at the same time

	lsr a					; volume / 16 = screen brightness :-)
	lsr a
	lsr a
	lsr a

	sta $2100

	pla
	bne -

	jump jspcStop

	jump jspcProcess

	wai

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



; **************************** Music stuff *****************************

StopMusic:
	lda #224

-	wai
	sec					; fade out music within 224/8 = 28 frames (~ 0.5 seconds)
	sbc #8
	pha

	jump jspcSetModuleVolume

	jump jspcProcess

	pla
	bne -

	jump jspcStop

	jump jspcProcess

	wai
rts



; **************************** Mosaic stuff ****************************

ToMosaic:
	lda #$03				; enable mosaic on BG1 & BG2, start with smallest block size

-	sta $2106
	wai					; show mosaic for one frame
	clc
	adc #$10				; increase block size by 1
	cmp #$B3				; block size 10 processed on last iteration?
	bne -
rts



FromMosaic:
	lda #$93				; enable mosaic on BG1 & BG2, start with block size 9

-	sta $2106
	wai					; show mosaic for one frame
	sec
	sbc #$10				; reduce block size by 1
	cmp #$F3				; smallest block size ($03) processed on last iteration?
	bne -

	stz $2106				; turn off mosaic effect
rts
