;==========================================================================================
;
;   UNOFFICIAL SNES POWERPAK FIRMWARE V2.01 (CODENAME: "SIMBA")
;   (c) 2012/2013 by ManuLöwe (http://www.manuloewe.de/)
;
;	*** FUNCTIONS: PER-PAGE NAVIGATION ***
;
;==========================================================================================



; ********************** Page-by-page navigation ***********************

PageDown:
	rep #A_8BIT				; A = 16 bit

	lda filesInDir				; make a subtraction: filesInDir - maxFiles = result (in Accu.)
	sec
	sbc #maxFiles
	bmi __PageDownDone			; if result is negative or zero (i.e., there's only one "page"),
	beq __PageDownDone			; then do nothing at all

	cmp #maxFiles				; if result <= maxFiles (i.e., there are no more than two "pages"),
	bcc __ShowNextPage			; then show "last page"
	beq __ShowNextPage
	
	sec					; same thing if result - maxFiles <= selectedEntry
	sbc #maxFiles				; (i.e., selectedEntry is within the last two "pages")
	cmp selectedEntry
	bcc __AddMaxFiles			; but then we have to re-add maxFiles we just subtracted first :-)
	beq __AddMaxFiles

	sep #A_8BIT				; A = 8 bit

	jsr SyncPage				; make selectedEntry = entry no. of file at top of screen

	rep #A_8BIT				; A = 16 bit

	lda selectedEntry

__AddMaxFiles:
	clc
	adc #maxFiles				; add maxFiles for "next page"

__ShowNextPage:
	sta selectedEntry
	pha					; push selectedEntry (16 bit) onto stack

	sep #A_8BIT				; A = 8 bit

	jsr PrintPage				; new parameters set, print next page

	rep #A_8BIT				; A = 16 bit

	pla					; pull selectedEntry (16 bit) from stack
	sta selectedEntry

__PageDownDone:

	sep #A_8BIT				; A = 8 bit
rts



PageUp:
	rep #A_8BIT				; A = 16 bit

	lda filesInDir
	cmp #maxFiles
	bcc __PageUpDone			; if filesInDir <= maxFiles, do nothing
	beq __PageUpDone

	lda selectedEntry
	cmp #maxFiles
	bcc __ShowFirstPage			; if selectedEntry <= maxFiles, show "first" page
	beq __ShowFirstPage

	sep #A_8BIT				; A = 8 bit

	jsr SyncPage				; make selectedEntry = entry no. of file at top of screen

	rep #A_8BIT				; A = 16 bit

	lda selectedEntry
	sec
	sbc #maxFiles				; subtract maxFiles for "previous page"
	bpl +					; if result isn't negative, fine

__ShowFirstPage:
	lda #$0000				; otherwise, show "first" page as well
+
	sta selectedEntry

__ShowPrevPage:
	pha					; push selectedEntry (16 bit) onto stack

	sep #A_8BIT				; A = 8 bit

	jsr PrintPage				; new parameters set, print previous page

	rep #A_8BIT				; A = 16 bit

	pla					; pull selectedEntry (16 bit) from stack
	sta selectedEntry

__PageUpDone:

	sep #A_8BIT				; A = 8 bit
rts



SyncPage:
	lda cursorY
	sec
	sbc #cursorYmin				; subtract indention
	lsr a
	lsr a					; divide by 8 to get the difference between selectedEntry
	lsr a					; and entry no. of the file at the top of the screen
	sta temp

	lda #$00
	sta temp+1

	rep #A_8BIT				; A = 16 bit, required for higher signed number magnitude (+/-32K)

	lda selectedEntry
	sec					; subtract difference so that selectedEntry now corresponds
	sbc temp				; to the file at the top of the screen
	bpl +					; if result isn't negative, fine
	lda #$0000				; otherwise, selectedEntry = $0000
+
	sta selectedEntry

	sep #A_8BIT				; A = 8 bit
rts



PrintPage:
	lda #$00				; reset file counter
	sta temp

	jump jPrintClearScreen

	SetCursorPos 0, 0

-	inc temp				; increment file counter

	jump jDirPrintEntry

	rep #A_8BIT				; A = 16 bit

	inc selectedEntry			; increment entry index

	sep #A_8BIT				; A = 8 bit

	lda temp				; check if printY max reached, jump out
	cmp #maxFiles
	bne -

	lda #cursorYmin
	sta cursorY				; reset cursor and scrolling parameters

	stz cursorYCounter
	stz cursorYUp
	stz cursorYDown

	stz scrollY
	stz scrollYCounter
	stz scrollYUp
	stz scrollYDown

	stz speedCounter
	stz speedScroll

	lda #insertStandardTop
	sta insertTop

	lda #insertStandardBottom
	sta insertBottom
rts

; **********************************************************************
