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OBIKESTRIGHT1E & $FF00)
;	STA	OTHERBIKE +1 
;	LDA	#OBIKESTRIGHT1E & $FF
;	ADC	TEMP0
;	STA	OURBIKE
;	LDA	#(OBIKESTRIGHT1E & $FF00)
;	STA	OURBIKE +1 

	PTR	OBIKESTRIGHT1E,OURBIKE
	PTR	OBIKECOLOR1E,OURBIKECOLOR
	PTR	OTBIKESTRIGHT1E-17,OTHERBIKE
	PTR	OTBIKECOLOR1E-17,OTHERBIKECOLOR
	PTR	OTBIKE1MASKE-17,OTHERBIKEMASK

DRIVE:
	DEC	HOLDCNT
	BNE	SAME
	LDY	MAP
	LDA	@TRACK(Y)
;	LDA	#0 ;FREDS ROAD
	TAX	
	LDA	X,ROADTABH
	STA	ROADSTART+1
	LDA	X,ROADTABL
	STA	ROADSTART
	INY
	LDA	@TRACK(Y)
	STA	HOLDCNT
	INY
	CPY	#TRACKSTART-TRACKEND
	BNE	.DRIVE1
	LDY	#0
.DRIVE1:
	STY	MAP

SAME:	
	JSR	joystick
	LDX	PERSHIFT
;	LDX	#16
	LDA	X,PERSTABL
	STA	PERSPECTIVE
	LDA	X,PERSTABH
	STA	PERSPECTIVE+1
	STAI	0,PF0
	STA	PF1
	STA	PF2
;
;
;	WAIT FOR VSYNC ....
;
;
	LDY	#44	;36
.TIMLOP:
	LDA	INTIM
	BNE	.TIMLOP
	WSYNC 			;?
	STY	TIM64T
	VSYNCON
	WSYNC
	WSYNC
	WSYNC
	WSYNC
	VSYNCOFF
;********** END OF INTERLACE CODE ********************
;*************************************
;START OF FREE EVEN PART 2
;**********************************************
;MORE ROOM FOR CODE THAT NEEDS TO BE DONE ON EVEN FRAMS HERE
	JSR	TABLEDRIVER

	JMP	EVERYFRAME

;HERES FOR CODE THAT NEEDS TO BE DONE ON EVERY FRAME


;--------------------------------------------------------------------------

EVERYFRAME:
	STAI	$60,COLUBK
	.INCLUDE	"MDRVR.S"
	WSYNC
	LDY	#6
.LL:	DEY
	BNE	.LL
	RESP0
	RESP1

.TIMLOP1: LDA	INTIM
	BNE	.TIMLOP1
	WSYNC
;	JMP	MOTORKER
.include	"skymtn.ker"

;-----------------------------------------------------------------

ODD:
	LDY	#35 ;DEH5	;32	; 35
	WSYNC
	STY	TIM64T
	STAI	0,GRP0
	STA	GRP1
	STA	PF0
	STA	PF1
	STA	PF2
;;;;;;;;;;
;KERNAL TEST CODE

;	LDA	FRMCNT
;	AND	#$F
;	BNE	.GOO
;	DEC	TEST
;	BPL	.GOO
;	STAI	150,TEST
	
;.GOO:	LDX	TEST
;	LDA	HMTAB,X
;	LDA	#$73
	STAI	$78,OURBIKEHM

;	LDA	FRMCNTH
;	EOR	#$FF
;	AND	#$7F
;	TAX
;	LDA	HMTAB,X
	LDA	#$76
	STA	OTHERBIKEHM

	STAI	$2,CLOUD0HM
	STAI	$8,CLOUD1HM
	PTR	box00-60,CLOUD1 ;51
	PTR	box01-60,CLOUD0
	PTR	CLOUDCOLOR-60,CLOUD0COLOR
	PTR	CLOUDCOLOR-60,CLOUD1COLOR
;;;;;;;;;;
;END KERNAL TEST CODE
;;

	
	PTR	OBIKESTRIGHT1O,OURBIKE
	PTR	OBIKECOLOR1O,OURBIKECOLOR

	PTR	OTBIKE1MASKO-17,OTHERBIKEMASK	
;	LDA	FRMCNT
;	AND	#$E
;	ADC	#OTBIKE1MASKO & $FF
;	STA	OTHERBIKEMASK
	PTR	OTBIKESTRIGHT1O-17,OTHERBIKE
	PTR	OTBIKECOLOR1O-17,OTHERBIKECOLOR



;*************************************
;START OF FREE ODD PART 1
;**********************************************
;HERE IS FOR CODE THAT NEEDS TO BE DONE ON ODD FRAMES

	LDY	#44	;36
.TIMLOP2: 
	LDA	INTIM
	BNE	.TIMLOP2
	WSYNC
	STY	TIM64T
	LDA	@PLAYER1A(Y)
	LDA	@PLAYER1A(Y)
	LDA	@PLAYER1A(Y)
	LDA	@PLAYER1A(Y)
	LDA	@PLAYER1A(Y)
	LDA	@PLAYER1A(Y)
	LDA	@PLAYER1A(Y)
	LDA	@PLAYER1A(Y)
	nop
;	LDX	#8
;.DLOP:
;	DEX
;	BNE	.DLOP
;	NOP
;	NOP
	VSYNCON
	WSYNC
	WSYNC
	WSYNC
	WSYNC
	LDA	@PLAYER1A(Y)
	LDA	@PLAYER1A(Y)
	LDA	@PLAYER1A(Y)
	LDA	@PLAYER1A(Y)
	LDA	@PLAYER1A(Y)
	LDA	@PLAYER1A(Y)
	LDA	@PLAYER1A(Y)
	LDA	@PLAYER1A(Y)
;	WASTE3
;	nop
;	LDX	#8
;.DLOP:
;	DEX
;	BNE	.DLOP
;	NOP
;	NOP

	VSYNCOFF


;**********END OF INTERLACE CODE
;*************************************
;START OF FREE ODD PART 2
;**********************************************
;HERE IS ROOM FOR MORE CODE TO BE DONE ON ODD FRAMES


	JSR	TABLEDRIVER1

	JMP	EVERYFRAME

;
;	joystick handler
;
;

joystick:
	DEC	MTIME
	BNE	.HOLD
	STAI	0,CTLSWA	;SWITCHES TO INPUT
	LDA	SWCHA
	ROR
	ROR
	ROR
	ROR
	STA	TEMP0			; left joystick for now
.UP:
	LDA	TEMP0
	AND	#UP
	BEQ	.DOWN
	JMP	.LEFT
.DOWN:
	LDA	TEMP0
	AND	#DOWN
	BEQ	.LEFT
.LEFT:	
	LDA	TEMP0
	AND	#RIGHT
	BEQ	.RIGHT
	DEC	PERSHIFT
	LDA	#$FF
	CMP	PERSHIFT
	BNE	.RIGHT
	STAI	0,PERSHIFT
.RIGHT:	LDA	TEMP0
	AND	#LEFT
	BEQ	.SETWAIT
	INC	PERSHIFT
	LDA	#33
	CMP	PERSHIFT
	BNE	.SETWAIT
	STAI	32,PERSHIFT
.SETWAIT:
	STAI	3,MTIME
.HOLD:	
	rts
.INCLUDE	"SHIFTL.S"
.INCLUDE	"SHIFTR.S"
SWITCHLR:
	LDA	PLAYF
	CLC
	ADC	#5
	CMP	#MTNWIDTH
	BCC	.OK
	SBC	#MTNWIDTH
.OK:	STA	PLAYF
	RTS
SWITCHRL:
	LDA	PLAYF
	CLC
	ADC	#5
	CMP	#MTNWIDTH
	BCC	.OK
	SBC	#MTNWIDTH
.OK:	STA	PLAYF
	RTS
PERSTABL:
	.DC.B	PERS1 & $FF
	.DC.B	PERS1 & $FF
	.DC.B	PERS1 & $FF
	.DC.B	PERS1 & $FF
	.DC.B	PERS1 & $FF
	.DC.B	PERS1 & $FF
	.DC.B	PERS1 & $FF
	.DC.B	PERS1 & $FF
	.DC.B	PERS2 & $FF
	.DC.B	PERS2 & $FF
	.DC.B	PERS2 & $FF
	.DC.B	PERS3 & $FF
	.DC.B	PERS3 & $FF
	.DC.B	PERS4 & $FF
	.DC.B	PERS5 & $FF
	.DC.B	PERS6 & $FF
	.DC.B	PERS7 & $FF
	.DC.B	PERS8 & $FF
	.DC.B	PERS9 & $FF
	.DC.B	PERS10 & $FF
	.DC.B	PERS10 & $FF
	.DC.B	PERS11 & $FF
	.DC.B	PERS12 & $FF
	.DC.B	PERS12 & $FF
	.DC.B	PERS12 & $FF
	.DC.B	PERS13 & $FF
	.DC.B	PERS13 & $FF
	.DC.B	PERS13 & $FF
	.DC.B	PERS13 & $FF
	.DC.B	PERS13 & $FF
	.DC.B	PERS13 & $FF
	.DC.B	PERS13 & $FF
	.DC.B	PERS13 & $FF
	.DC.B	PERS13 & $FF

PERSTABH:
	.DC.B	(PERS1 & $FF00)/256
	.DC.B	(PERS1 & $FF00)/256
	.DC.B	(PERS1 & $FF00)/256
	.DC.B	(PERS1 & $FF00)/256
	.DC.B	(PERS1 & $FF00)/256
	.DC.B	(PERS1 & $FF00)/256
	.DC.B	(PERS1 & $FF00)/256
	.DC.B	(PERS1 & $FF00)/256
	.DC.B	(PERS2 & $FF00)/256
	.DC.B	(PERS2 & $FF00)/256
	.DC.B	(PERS2 & $FF00)/256
	.DC.B	(PERS3 & $FF00)/256
	.DC.B	(PERS3 & $FF00)/256
	.DC.B	(PERS4 & $FF00)/256
	.DC.B	(PERS5 & $FF00)/256
	.DC.B	(PERS6 & $FF00)/256
	.DC.B	(PERS7 & $FF00)/256
	.DC.B	(PERS8 & $FF00)/256
	.DC.B	(PERS9 & $FF00)/256
	.DC.B	(PERS10 & $FF00)/256
	.DC.B	(PERS10 & $FF00)/256
	.DC.B	(PERS11 & $FF00)/256
	.DC.B	(PERS12 & $FF00)/256
	.DC.B	(PERS12 & $FF00)/256
	.DC.B	(PERS12 & $FF00)/256
	.DC.B	(PERS13 & $FF00)/256
	.DC.B	(PERS13 & $FF00)/256
	.DC.B	(PERS13 & $FF00)/256
	.DC.B	(PERS13 & $FF00)/256
	.DC.B	(PERS13 & $FF00)/256
	.DC.B	(PERS13 & $FF00)/256
	.DC.B	(PERS13 & $FF00)/256
	.DC.B	(PERS13 & $FF00)/256
	.DC.B	(PERS13 & $FF00)/256
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ROADTABL:
	.DC.B	ROAD19 & $FF

	.DC.B	ROAD17 & $FF
	.DC.B	ROAD15 & $FF
	.DC.B	ROAD13 & $FF
	.DC.B	ROAD11 & $FF
	.DC.B	ROAD9 & $FF
	.DC.B	ROAD7 & $FF
	.DC.B	ROAD5 & $FF
	.DC.B	ROAD3 & $FF
	.DC.B	ROAD1 & $FF
	.DC.B	ROAD_1 & $FF
	.DC.B	ROAD_3 & $FF
	.DC.B	ROAD_5 & $FF
	.DC.B	ROAD21 & $FF
	.DC.B	ROAD23 & $FF
	.DC.B	ROAD25 & $FF
	.DC.B	ROAD27 & $FF
	.DC.B	ROAD29 & $FF
	.DC.B	ROAD31 & $FF
	.DC.B	ROAD33 & $FF
	.DC.B	ROAD35 & $FF
	.DC.B	ROAD37 & $FF
	.DC.B	ROAD39 & $FF
	.DC.B	ROAD_7 & $FF
	.DC.B	ROAD_9 & $FF
	.DC.B	ROAD_11 & $FF

ROADTABH:
	.DC.B	(ROAD19 & $FF00)/256
	.DC.B	(ROAD17 & $FF00)/256
	.DC.B	(ROAD15 & $FF00)/256
	.DC.B	(ROAD13 & $FF00)/256
	.DC.B	(ROAD11 & $FF00)/256
	.DC.B	(ROAD9 & $FF00)/256
	.DC.B	(ROAD7 & $FF00)/256
	.DC.B	(ROAD5 & $FF00)/256
	.DC.B	(ROAD3 & $FF00)/256
	.DC.B	(ROAD1 & $FF00)/256
	.DC.B	(ROAD_1 & $FF00)/256
	.DC.B	(ROAD_3 & $FF00)/256
	.DC.B	(ROAD_5 & $FF00)/256
	.DC.B	(ROAD21 & $FF00)/256
	.DC.B	(ROAD23 & $FF00)/256
	.DC.B	(ROAD25 & $FF00)/256
	.DC.B	(ROAD27 & $FF00)/256
	.DC.B	(ROAD29 & $FF00)/256
	.DC.B	(ROAD31 & $FF00)/256
	.DC.B	(ROAD33 & $FF00)/256
	.DC.B	(ROAD35 & $FF00)/256
	.DC.B	(ROAD37 & $FF00)/256
	.DC.B	(ROAD39 & $FF00)/256
	.DC.B	(ROAD_7 & $FF00)/256
	.DC.B	(ROAD_9 & $FF00)/256

	.DC.B	(ROAD_11 & $FF00)/256
MTNPTRTABL:
	.DC.B	MTNPLAY0 & $FF
	.DC.B	MTNPLAY1 & $FF
	.DC.B	MTNPLAY2 & $FF

	.DC.B	MTNPLAY3 & $FF
	.DC.B	MTNPLAY4 & $FF
	.DC.B	MTNPLAY5 & $FF

	.DC.B	MTNPLAYB0 & $FF
	.DC.B	MTNPLAYB1 & $FF
	.DC.B	MTNPLAYB2 & $FF

	.DC.B	MTNPLAYB3 & $FF
	.DC.B	MTNPLAYB4 & $FF
	.DC.B	MTNPLAYB5 & $FF
MTNPTRTABH:

	.DC.B	(MTNPLAY0 & $FF00)/256
	.DC.B	(MTNPLAY1 & $FF00)/256
	.DC.B	(MTNPLAY2 & $FF00)/256

	.DC.B	(MTNPLAY3 & $FF00)/256
	.DC.B	(MTNPLAY4 & $FF00)/256
	.DC.B	(MTNPLAY5 & $FF00)/256

	.DC.B	(MTNPLAYB0 & $FF00)/256
	.DC.B	(MTNPLAYB1 & $FF00)/256
	.DC.B	(MTNPLAYB2 & $FF00)/256

	.DC.B	(MTNPLAYB3 & $FF00)/256
	.DC.B	(MTNPLAYB4 & $FF00)/256
	.DC.B	(MTNPLAYB5 & $FF00)/256



SCROLLPFL:
	.DC.B	SCOLPF4 & $FF
	.DC.B	SCOLPF3 & $FF
	.DC.B	SCOLPF2 & $FF
	.DC.B	SCOLPF1 & $FF
	.DC.B	SCOLPF5 & $FF
	.DC.B	SCOLPF6 & $FF
;	.DC.B	SCOLPF7 & $FF
SCROLLPFH: 
	.DC.B	(SCOLPF4 &$FF00)/256
	.DC.B	(SCOLPF3 &$FF00)/256
	.DC.B	(SCOLPF2 &$FF00)/256
	.DC.B	(SCOLPF1 &$FF00)/256
	.DC.B	(SCOLPF5 &$FF00)/256
	.DC.B	(SCOLPF6 &$FF00)/256
;	.DC.B	(SCOLPF7 & $FF00)/256

SCROLLBKL:
	.DC.B	SCOLBK4 & $FF
	.DC.B	SCOLBK3 & $FF
	.DC.B	SCOLBK2 & $FF
	.DC.B	SCOLBK1 & $FF
	.DC.B	SCOLBK5 & $FF
	.DC.B	SCOLBK6 & $FF
;	.DC.B	SCOLBK7 & $FF

SCROLLBKH:
	.DC.B	(SCOLBK4 &$FF00)/256
	.DC.B	(SCOLBK3 &$FF00)/256
	.DC.B	(SCOLBK2 &$FF00)/256
	.DC.B	(SCOLBK1 &$FF00)/256
	.DC.B	(SCOLBK5 &$FF00)/256
	.DC.B	(SCOLBK6 &$FF00)/256
;	.DC.B	(SCOLBK7 &$FF00)/256
;;;

