;==========================================================================================
;
;   UNOFFICIAL SNES POWERPAK FIRMWARE V2.02 (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

	DpadUpHold2Scroll

	DpadDownHold2Scroll

	DpadLeftScrollUp

	DpadRightScrollDown

	lda Joy1New
	and #%00100000				; left shoulder button
	beq __LCheckDone

	jsr PageUp

__LCheckDone:

	lda Joy1New
	and #%00010000				; right shoulder button
	beq __RCheckDone

	jsr PageDown

__RCheckDone:

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

	lda #'S'
	sta bufferType

	jump jDirGetEntry			; get selected entry

	lda tempEntry.tempFlags			; check for "dir" flag
	and #$01
	bne +

	jmp NextModule

+	jump jNextDir

__ACheckDone:

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

	rep #A_8BIT

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

	lda sourceCluster+2
	cmp rootDirCluster+2
	bne +

	sep #A_8BIT

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

+	lda.w #$0001				; otherwise, load entry $0001, which is always "/.." (except for when in root dir)
	sta selectedEntry

	sep #A_8BIT

	lda #'S'
	sta bufferType

	jump jDirGetEntry

	jump jNextDir

__BCheckDone:

	lda Joy1New+1
	and #%00010000				; Start = return to options
	beq __StartCheckDone

	jmp NextModuleSO

__StartCheckDone:

	jmp TXTBrowserLoop			; end of loop



NextModule:
	jump jGameGenieClearAll			; clear out all previously entered codes

	rep #A_8BIT				; A = 16 bit

	lda tempEntry.tempCluster		; copy TXT file cluster to source cluster
	sta sourceCluster

	lda tempEntry.tempCluster+2
	sta sourceCluster+2

	sep #A_8BIT				; A = 8 bit

	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

	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				; CHANGEME --> read 256 bytes per line only ??
	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				; CHANGEME --> why read 256 bytes only ??
	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.Codes, x
	sta temp

;  PrintString " A"
;  PrintHexNum temp

rts



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

;  PrintString " C"
;  PrintHexNum temp

rts



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

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