;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; "Vertical Auto Scroll" generator
; By Jimmy52905
; Fixed by yoshicookiezeus
; Uses first extra bit: YES
; If extra bit is set, the generator makes the screen scroll downwards instead of upwards
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

STOP		dcb $10		; screen number the sprite should stop at when scrolling downwards

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	dcb "INIT"
	dcb "MAIN"                                    
	PHB                     
	PHK                     
	PLB                     
	JSR Main 
	PLB                     
	RTL      

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

Main:
	LDA $9D		;\ If sprites are locked...
	BNE Return	;/ Go to "Return".

	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 $18B9	;\ If the first extra bit is set...
	AND #$40	; |
	BNE Down	;/ Go to "Down".

	STZ $55		; Make sure that new tiles are uploaded from the top of the screen

	LDA $1464	;\
	ORA $1465	; |If the screen position is at the top of the level...
	BEQ Return	;/ Go to "Return".

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

Return:
	RTS		;Return from Subroutine.

Down:
	LDA #$02	;\ Make sure that new tiles are uploaded from the bottom of the screen
	STA $55		;/

	LDA $1465	;\ If the screen position is at screen 10 (last screen of a vertical layer 2 level)...
	CMP STOP	; |
	BEQ Return	;/ Go to "Return".

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

	RTS		;Return from Subroutine.