;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; "Vertical Auto Scroll" generator
; By Jimmy52905
; WARNING: Do NOT place this generator in the same screen as the main entrance. Place it
; at least one screen higher instead, or else a strange glitch will occur.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FLAG = $13D8
	dcb "INIT"
	STZ FLAG
	RTL
	dcb "MAIN"                                    
	PHB                     
	PHK                     
	PLB                     
	JSR Main 
	PLB                     
	RTL      

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Main sprite code
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Main:
	LDA $1463
	CMP #$0E	;If at screen 0E or passed...
	BCS Return

	LDA $71		;\
	CMP #$09	; |
	BEQ Return	; |If Mario is dying...
			; |
	CMP #$06	; |
	BEQ Return	; |
			; |
	CMP #$05	; |
	BEQ Return	; |Or changing to another level...
			; |
	CMP #$01	; |Or getting hurt...
	BEQ Return	;/ Go to "Return".

    LDA $9D
	BNE SpritesLocked ; branch to a place where the code isnt run


	LDA $13		;\
	LSR A		; |
	BCC Return	;/ Only run code every other frame.

	STZ $1412	;Disable Vertical Scroll so that the code can run properly.

	
	
	
	
	
	
	
	
	
	
	
	
	LDA FLAG		;check up/down flag
	BNE GoDown		;If the flag is not 0 (going up), we will go down instead.

	LDA $1464	;\ Before going up, we must first check...
        BEQ GoDownFlag	;/If it is at the top already, we must go down. However, if it is not at the top yet....

	REP #$20	;16-bit (accumulator).
	DEC $1464	;Make the screen scroll upwards automatically.
	SEP #$20	;8-bit (accumulator).

LDA $14
AND #$3F	;01, 03, 07, 0F, 1F, 3F, 7F, FF (faster to slower)
BNE nosfx
LDA #$60
STA $1DFC
nosfx:

	RTS

GoDown:
	LDA $1464	;\Before going down, we must first check...
	CMP #$C0	; |If the screen position is already at the bottom of the level or not.
	BEQ GoUpFlag	;/If it is at the bottom already, we must go up. However, if it is not at the bottom yet....

	REP #$20	;16-bit (accumulator).
	INC $1464	;Make the screen scroll downwards automatically.
	SEP #$20	;8-bit (accumulator).
	
	LDA $14
AND #$3F	;01, 03, 07, 0F, 1F, 3F, 7F, FF (faster to slower)
BNE nosfx
LDA #$61
STA $1DFC
nosfx:
	RTS

GoDownFlag:
	LDA #$01
	STA FLAG	;Going down flag.
	RTS		;Return from Subroutine.
GoUpFlag:
	STZ FLAG	;Going up flag.

SpritesLocked:
Return:
	RTS