;==========================================================================================
;
;   UNOFFICIAL SNES POWERPAK FIRMWARE V2.01 (CODENAME: "SIMBA")
;   (c) 2012/2013 by ManuLöwe (http://www.manuloewe.de/)
;
;	*** CF MODULE: SG (GAME GENIE CODE HANDLER) ***
;	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 |  T  |     |     |     |     |     |     |     |     |     |     |
; ------------------------------------------------------------------------------
; extMatch2 |  X  |     |     |     |     |     |     |     |     |     |     |
; ------------------------------------------------------------------------------
; extMatch3 |  T  |     |     |     |     |     |     |     |     |     |     |
; ------------------------------------------------------------------------------
;
; 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 #$01				; number of file types to look for (1, TXT only)
	sta extNum
	stz extNum+1

	lda #'T'
	sta extMatch1
	sta extMatch3

	lda #'X'
	sta extMatch2

	InitFileBrowser



TXTBrowserLoop:
	wai

UpCheck:
	lda Joy1+1
	and #%00001000
	beq UpDone

UpPressed:
	lda cursorYCounter
	bne UpDone
	lda scrollYCounter
	bne UpDone

UpScroll:
	lda #$08
	sta speedScroll
	lda #$01
	sta speedCounter

	jump jScrollUp

	ldx #$0015				; 21 frames
-
	wai
	lda Joy1+1
	and #%00001000
	beq UpDone
	dex
	cpx #$0000
	bne -

UpHeld:
	lda cursorYCounter
	bne UpDone
	lda scrollYCounter
	bne UpDone

UpScroll2:
	lda #$08
	sta speedScroll
	lda #$01
	sta speedCounter

	jump jScrollUp

	ldx #$0003				; 3 frames
-
	wai
	lda Joy1+1
	and #%00001000
	beq UpDone
	dex
	cpx #$0000
	bne -
	bra UpHeld

UpDone:



DownCheck:
	lda Joy1+1
	and #%00000100
	beq DownDone

DownPressed:
	lda cursorYCounter
	bne DownDone
	lda scrollYCounter
	bne DownDone

DownScroll:
	lda #$08
	sta speedScroll
	lda #$01
	sta speedCounter

	jump jScrollDown

	ldx #$0015				; 21 frames
-
	wai
	lda Joy1+1
	and #%00000100
	beq DownDone
	dex
	cpx #$0000
	bne -

DownHeld:
	lda cursorYCounter
	bne DownDone
	lda scrollYCounter
	bne DownDone

DownScroll2:
	lda #$08
	sta speedScroll
	lda #$01
	sta speedCounter

	jump jScrollDown

	ldx #$0003				; 3 frames
-
	wai
	lda Joy1+1
	and #%00000100
	beq DownDone
	dex
	cpx #$0000
	bne -
	bra DownHeld

DownDone:



LeftCheck:
	lda Joy1Press+1
	and #%00000010
	beq LeftDone

LeftPressed:
	lda cursorYCounter
	bne LeftDone
	lda scrollYCounter
	bne LeftDone

LeftScroll:
	lda #$04
	sta speedScroll
	lda #$02
	sta speedCounter

	jump jScrollUp

LeftDone:



RightCheck:
	lda Joy1Press+1
	and #%00000001
	beq RightDone

RightPressed:
	lda cursorYCounter
	bne RightDone
	lda scrollYCounter
	bne RightDone

RightScroll:
	lda #$04
	sta speedScroll
	lda #$02
	sta speedCounter

	jump jScrollDown

RightDone:



LCheck:
	lda Joy1New
	and #%00100000
	beq LDone

	jsr PageUp

LDone:



RCheck:
	lda Joy1New
	and #%00010000
	beq RDone

	jsr PageDown

RDone:



ACheck:						; A = select file / load dir
	lda Joy1New
	and #%10000000
	beq __ACheckDone

	lda #'S'
	sta bufferType

	jump jDirGetEntry			; get selected entry

	lda tempEntry.tempDirFlag		; check if directory
	cmp #$01
	beq +

	jmp NextModule

+	jump jNextDir

__ACheckDone:



BCheck:						; B = go up one directory / return to options
	lda Joy1New+1
	and #%10000000
	beq __BCheckDone

	lda sourceCluster			; check if current dir = root dir ...
	cmp rootDirCluster
	bne __NotRootDir

	lda sourceCluster+1
	cmp rootDirCluster+1
	bne __NotRootDir

	lda sourceCluster+2
	cmp rootDirCluster+2
	bne __NotRootDir

	lda sourceCluster+3
	cmp rootDirCluster+3
	bne __NotRootDir

	jmp NextModuleSO			; ... if yes, go back to options

__NotRootDir:
	lda #$00				; otherwise, load entry $0001, which is
	sta selectedEntry+1			; always "/.." except for when already in root dir
	lda #$01
	sta selectedEntry

	lda #'S'
	sta bufferType

	jump jDirGetEntry

	jump jNextDir

__BCheckDone:



StartCheck:					; Start = return to options
	lda Joy1New+1
	and #%00010000
	beq __StartCheckDone
	jmp NextModuleSO			; go back to options

__StartCheckDone:

	jmp TXTBrowserLoop



NextModule:
;	SetCursorPos 18,0
	lda tempEntry.tempCluster
	sta sourceCluster
	lda tempEntry.tempCluster+1
	sta sourceCluster+1
	lda tempEntry.tempCluster+2
	sta sourceCluster+2
	lda tempEntry.tempCluster+3
	sta sourceCluster+3			; copy to source cluster

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

	stz sectorCounter
	stz bankCounter

	jump jClusterToLBA			; sourceCluster -> first sourceSector

	lda #kDestWRAM
	sta destType

	jump jCardReadSector			; sector -> WRAM     lda gameCluster

	ldx #$0000
	ldy #$0000



ReadLoop:					; read GG codes from text file
	lda sectorBuffer1, y
	cmp #$20				; space - read to end of line, then next code
	beq ReadLine
	cmp #$0D				; CR
	beq NextCode
	cmp #$0A				; LF
	beq NextCode
	cmp #$1A				; EOF
	beq ReadLoopDone
	cmp #$30
	bcc NextChar				; under 0
	cmp #$3A
	bcc SaveCharNumJump			; between 0 and 9
	cmp #$41
	bcc NextChar				; under A
	cmp #$47
	bcc SaveCharAlphaJump			; between A and F
	bra NextChar

SaveCharAlphaJump:
	jsr SaveCharAlpha
	bra NextGGChar

SaveCharNumJump:
	jsr SaveCharNum
	bra NextGGChar

ReadLine:
	;PrintString "R"

	lda sectorBuffer1, y
	cmp #$0D
	beq NextCode
	cmp #$0A
	beq NextCode
	iny
	cpy #$0100
	beq ReadLoopDone
	bra ReadLine



NextCode:
;  PrintString "N"

	stx temp

 ; PrintHexNum temp

	lda temp
	and #%00000111
	beq NextChar				; already at the beginning of a code, skip
	lda temp
	and #%11111000				; go to beginning of current code
	clc
	adc #%00001000				; go to beginning of next code
	sta temp
	ldx temp
	lda temp
	cmp #$28
	beq ReadLoopDone
	bra NextChar

NextGGChar:
	inx					; next GG char
	cpx #$0028				; 40 GG chars = 5 codes x 8 chars
	beq ReadLoopDone

NextChar:
	iny
	cpy #$0100
	bne ReadLoop

ReadLoopDone:



NextModuleSO:
	FindFile "SO.MAP"

	jump jCardLoadModule			; load options module



SaveCharAlpha:
	lda sectorBuffer1, y			; put character into codes
	sec
	sbc #$41				; subtract 'A'
	clc
	adc #$0A				; add 0-9
	sta gameGenie, x
	sta temp

;  PrintString " A"
;  PrintHexNum temp

rts



SaveCharNum:
	lda sectorBuffer1, y			; put character into codes
	sec
	sbc #$30				; subtract '0'
	sta gameGenie, x
	sta temp

;  PrintString " C"
;  PrintHexNum temp

rts



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

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