;==========================================================================================
;
;   UNOFFICIAL SNES POWERPAK FIRMWARE V2.02 (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				; if filesInDir <= maxFiles (i.e., there's only one "page"),
	cmp #maxFiles+1
	bcc __PgDnDone				; then do nothing at all

	sec
	sbc #maxFiles
	sta temp+2				; save filesInDir - maxFiles to temp var
	cmp selectedEntry			; check if selectedEntry is within the last "page"
	bcc __PgDnNextPage			; if so, show "last" page

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

.ACCU 16

	lda selectedEntry
	clc
	adc #maxFiles				; add maxFiles for "next page"
	bcs __PgDnOverflow			; carry set means new selectedEntry > $FFFF

	cmp temp+2				; check if new selectedEntry > filesInDir - maxFiles
	bcc __PgDnNextPage

__PgDnOverflow:
	lda temp+2				; overflow, set selectedEntry = filesInDir - maxFiles ("last" page)

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

	jsr PrintPage				; new parameters set, print next page

.ACCU 16

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

__PgDnDone:

	sep #A_8BIT				; A = 8 bit
rts



PageUp:
	rep #A_8BIT				; A = 16 bit

	lda filesInDir				; if filesInDir <= maxFiles (i.e., there's only one "page"),
	cmp #maxFiles+1
	bcc __PgUpDone				; then do nothing at all

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

.ACCU 16

	lda selectedEntry
	sec
	sbc #maxFiles				; subtract maxFiles for "previous page"
	bcs __PgUpNextPage			; carry set means no borrow required, continue

__PgUpUnderflow:				; carry clear --> underflow, selectedEntry < $0000
	lda #$0000				; set selectedEntry = $0000 for "first" page

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

	jsr PrintPage				; new parameters set, print previous page

.ACCU 16

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

__PgUpDone:

	sep #A_8BIT				; A = 8 bit
rts



SyncPage:
	php

	sep #A_8BIT				; A = 8 bit

	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

	stz temp+1

	rep #A_8BIT				; A = 16 bit

	lda selectedEntry
	sec					; subtract difference so that selectedEntry now corresponds
	sbc temp				; to the file at the top of the screen
	bcs +					; carry set means no borrow, continue
	lda #$0000				; carry clear --> underflow, selectedEntry < $0000, reset selectedEntry

+	sta selectedEntry

	plp					; restore processor status
rts



PrintPage:
	php

	sep #A_8BIT				; A = 8 bit

	stz temp				; reset file counter

	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

	plp					; restore processor status
rts

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