*EIGHT WAY SCROLLING DAN PINAL
*EQUATES
SDLSTL	EQU	$230
STICK0	EQU	$278
DLISTL	EQU	$D402
HSCROL	EQU	$D404
VSCROL	EQU	$D405
SETVBV	EQU	$E45C
XITVBV	EQU	$E462
*
SCREN	EQU	$0000
WIDTH	EQU	$0400
HEIGHT	EQU	$0040
SCRENH	EQU	40
SCRENV	EQU	20
WINDOW	EQU	$9C40+800		;FOR A 40K OR 48K COMPUTER WITH CARTRIDGE
*
	ORG	$4000
*
START
	LDA	#$00
	STA	ENABLE			;SET VBI FLAG TO OFF
* INIT SCROLL VARIABLES TO STARTUP VALUES
	STA	MAPH			;SET SCREEN COORD. TO 0
	STA	MAPH+1
	STA	MAPV
	STA	MAPV+1
	STA	FINEV
	STA	VSCROL
	LDA	#$0C
	STA	FINEH
	STA	HSCROL
* INIT BASE ADDRESS TO SCREEN ADDRESS
	LDA	#>SCREN
	STA	BASE
	LDA	#<SCREN
	STA	BASE+1
	JSR	WRITEDL			;WRITE NEW DISPLAY LIST
* TELL ANTIC WHERE NEW DLIST IS
	LDA	#>NDLIST
	STA	SDLSTL
	LDA	#<NDLIST
	STA	SDLSTL+1
* SETUP VBLANK
	LDA	#$07			;DEFFERED VBI
	LDY	#>VBI
	LDX	#<VBI
	JSR	SETVBV
	LDA	#$01
	STA	ENABLE			;TURN VBLANK FLAG TO ON
	RTS				;BACK TO YOUR MONITOR
VBI
	CLD				;JUST A PRECAUTION
	LDA	ENABLE			;CHECK THE SOFTWARE FLAG
	BNE	CKSTK			;OK
	JMP	XVBI			;LEAVE VBI
CKSTK
* COPY OLD MAP VARIABLES IN CASE NEW VALUES ARE INVALID
	LDA	FINEH
	STA	NEWFH
	LDA	FINEV
	STA	NEWFV
	LDA	#$00
	STA	CHANGE			;SET CHANGE FLAG TO 0
* USE JOYSTICK VALUE TO INDEX INTO TABLE OF +1,0, OR -1
	LDX	STICK0
	LDA	HOFFS,X
	STA	HOFF
	LDA	VOFFS,X
	STA	VOFF
CHECKH
	LDA	HOFF
	BEQ	CHECKV			;IF 0 NO CHANCE LEAVE
* ADD OFFSET TO FINEH
	CLC
	LDA	NEWFH
	ADC	HOFF
	STA	NEWFH
	CMP	#$10			;TIME TO CRUDE SCROLL?
	BEQ	RSHIFT
	CMP	#$0B			;TIME TO CRUDE SCROLL?
	BEQ	LSHIFT
	STA	FINEH
	STA	HSCROL
	JMP	CHECKV			;GO CHECK VERTICAL
RSHIFT
	LDA	MAPH			;CHECK IF HORIZONTAL ALREADY AT 0?
	ORA	MAPH+1
	BEQ	CHECKV			;CAN'T GO LESS THAN 0
	SEC
	LDA	MAPH
	SBC	#$01
	STA	MAPH
	LDA	MAPH+1
	SBC	#$00
	STA	MAPH+1
	LDA	#$0C
	STA	FINEH
	STA	HSCROL
	INC	CHANGE			;SET FLAG TO WRITE NEW DISPLAY LIST
	JMP	CHECKV
LSHIFT
* FIRST CHECK IF ALREADY AT LIMIT
	LDA	MAPH
	CMP	#>WIDTH-SCRENH
	BNE	AA1
	LDA	MAPH+1
	CMP	#<WIDTH-SCRENH
	BEQ	CHECKV
AA1
* CHANGE MAPH & SET FINE SCROLL
	CLC
	LDA	MAPH
	ADC	#$01
	STA	MAPH
	LDA	MAPH+1
	ADC	#$00
	STA	MAPH+1
	LDA	#$0F
	STA	FINEH
	STA	HSCROL
	INC	CHANGE			;SET FLAG FOR CKLIST
CHECKV
* VERTICAL WORKS SAME AS HORIZONTAL
	LDA	VOFF
	BEQ	CKLIST			;LEAVE IF No CHANGES TO MAKE
* ADD OFFSET TO FINE SCROLL VALUE
	CLC
	ADC	NEWFV
	STA	NEWFV
* CHECK TO SEE IF IT'S TIME TO CRUDE SCROLL
	CMP	#$08
	BEQ	DSHIFT
	CMP	#$FF
	BEQ	USHIFT
	STA	FINEV
	STA	VSCROL
	JMP	CKLIST
DSHIFT
* CHECK TO SEE IF MAP IS AT LIMIT
	LDA	MAPV
	CMP	#>HEIGHT-SCRENV
	BNE	AB1
	LDA	MAPV+1
	CMP	#<HEIGHT-SCRENV
	BEQ	CKLIST
AB1
* SET MAP VERT. OFFSET & RESET FINE SCROLL VALUE
	CLC
	LDA	MAPV
	ADC	#$01
	STA	MAPV
	LDA	MAPV+1
	ADC	#$00
	STA	MAPV+1
	LDA	NEWFV
	AND	#$07
	STA	FINEV
	STA	VSCROL
	INC	CHANGE
	JMP	CKLIST
USHIFT
* CHECK FOR MAP AT LIMIT
	LDA	MAPV
	ORA	MAPV+1
	BEQ	CKLIST
	SEC
	LDA	MAPV
	SBC	#$01
	STA	MAPV
	LDA	MAPV+1
	SBC	#$00
	STA	MAPV+1
	LDA	NEWFV
	AND	#$07
	STA	FINEV
	STA	VSCROL
	INC	CHANGE
CKLIST
	LDA	CHANGE			;CHECK IF MAP HORIZONTAL OR VERTICAL HAS BEEN CHANGE
	BEQ	XVBI			;NO NEED TO REWRITE DISPLAY LIST IF NO CHANGE
					;SET UP TO MULTIPLY WIDTH X MAP VERTICAL OFFSET
	LDA	#>WIDTH
	STA	RESULT
	LDA	#<WIDTH
	STA	RESULT+1
	LDA	#$00			;OVERFLOW SHOULD NOT BE NEEDED BUT ITS HERE ANYWAY
	STA	OVERFL
	STA	OVERFL+1
	LDX	#$11
	CLC
* THIS IS A 16 BIT MULTIPLY TO CALCULATE OFFSET FROM STARTING MA
MULT16	ROR	OVERFL+1
	ROR	OVERFL
	ROR	RESULT+1
	ROR	RESULT
	BCC	AC1
	CLC
	LDA	MAPV
	ADC	OVERFL
	STA	OVERFL
	LDA	MAPV+1
	ADC	OVERFL+1
	STA	OVERFL+1
AC1
	DEX
	BNE	MULT16
* NOW SCREEN ADDRESS IS ADDED
	CLC
	LDA	#>SCREN
	ADC	RESULT
	STA	BASE
	LDA	#<SCREN
	ADC	RESULT+1
	STA	BASE+1
* NOW THE COLUMN OFFSET IS ADDED TO FORM THE NEW BASE ADDRESS
	CLC				;FOR THE TOP
	LDA	BASE
	ADC	MAPH
	STA	BASE
	LDA	BASE+1
	ADC	MAPH+1
	STA	BASE+1
	JSR	WRITEDL			;WRITE NEW DISPLAY LIST
XVBI
	JMP	XITVBV
*
WRITEDL
* SPOT POINTS TO THE FIRST ADDRESS IN THE DISPLAY LIST.
	LDX	#$00
AD1
* STORE NEW ADDRESS IN DISPLAY LIST
	LDA	BASE
	STA	SPOT,X
	LDA	BASE+1
	STA	SPOT+1,X
* ADD MAP WIDTH TO GET ADDRESS OF NEXT LINE
	CLC
	LDA	#>WIDTH
	ADC	BASE
	STA	BASE
	LDA	#<WIDTH
	ADC	BASE+1
	STA	BASE+1
* SET TO POINT TO NEXT SET OFF ADDRESSES IN DLIST
	INX
	INX
	INX
	CPX	#60
	BNE	AD1
	RTS
*
HOFFS
	DB	$00,$00,$00,$00,$00,$01,$01,$01
	DB	$00,$FF,$FF,$FF,$00,$00,$00,$00
VOFFS
	DB	$00,$00,$00,$00,$00,$01,$FF,$00
	DB	$00,$01,$FF,$00,$00,$01,$FF,$00
NDLIST
	DB	$70,$70,$70
	DB	$72
SPOT
	DB	$00,$00
	DB	$72,$00,$00
	DB	$72,$00,$00
	DB	$72,$00,$00
	DB	$72,$00,$00
	DB	$72,$00,$00
	DB	$72,$00,$00
	DB	$72,$00,$00
	DB	$72,$00,$00
	DB	$72,$00,$00
	DB	$72,$00,$00
	DB	$72,$00,$00
	DB	$72,$00,$00
	DB	$72,$00,$00
	DB	$72,$00,$00
	DB	$72,$00,$00
	DB	$72,$00,$00
	DB	$72,$00,$00
	DB	$72,$00,$00
	DB	$52,$00,$00
	DB	$42
	DW	WINDOW
	DB	$02,$02,$02
	DB	$41
	DW	NDLIST
RESULT	DS	2
OVERFL	DS	2
MAPH	DS	2
MAPV	DS	2
HOFF	DS	2
VOFF	DS	2
BASE	DS	2
FINEH	DS	1
FINEV	DS	1
NEWFH	DS	1
NEWFV	DS	1
CHANGE	DS	1
ENABLE	DS	1
