* MISC TANK SUBROUTINES
* THIS IS D:TANKSUBS.SRC
DLI
	PHA
	LDA	#<ATARI
	STA	WSYNC
	STA	CHBASE
	PLA
	RTI
;
RESTART
	JSR	DOSCREEN	;RESTORE SCREEN MAZE
*
* CLEAR GAME VARIABLES
	LDA	#$00
	TAX
AW0
	STA	FIRSTVAR,X
	INX
	CPX	#LASTVAR-FIRSTVAR
	BCC	AW0
	BEQ	AW0
* REPOSITION TANKS
* FIRST ERASE ANY OLD DATA
	LDA	#$00
	TAX
AW1
	STA	MISSILES,X
	STA	PLAYR0,X
	STA	PLAYR1,X
	STA	PLAYR2,X
	STA	PLAYR3,X
	DEX
	BNE	AW1
	LDA	STARTV		;MIDSCREEN
	STA	PLAYRV
	STA	POTMP1
	LDA	STARTV+1
	STA	PLAYRV+1
	STA	POTMP3
	LDA	#<PLAYR0
	STA	POTMP2
	LDA	#<PLAYR1
	STA	POTMP4
	LDY	#$0F
AW2
	LDA	TANK0,Y
	STA	(POTMP1),Y
	STA	(POTMP3),Y
	DEY
	BPL	AW2
	LDA	STARTH
	STA	PLAYRH
	STA	HPOSP0
	LDA	STARTH+1
	STA	PLAYRH+1
	STA	HPOSP1
* SET FLAGS FOR GAME IN PROGRESS
	LDX	#$13
AW3
	LDA	SMSG,X
	STA	LINE3,X
	DEX
	BPL	AW3
	LDA	#$60
	STA	TIMER
	LDA	#$60		;SET GAME TIME TO 3:00
	STA	SECONDS
	LDA	#$02
	STA	MINUTES
	LDA	#$01
	STA	HITCLR		;CLEAR ANY ERRONEOUS COLLISIONS
	STA	GAME
	STA	ENABLE
	RTS
DOSCREEN
* RESET SCREEN
	LDA	#MAZE
	STA	POTMP0
	LDA	#<MAZE
	STA	POTMP1
	LDA	#SCREEN
	STA	POTMP2
	LDA	#<SCREEN
	STA	POTMP3
	CLC
	LDA	#400		;SET UP TO MOVE 400 BYTES TO SCREEN
	ADC	#SCREEN
	STA	TEMPO
	LDA	#<400
	ADC	#<SCREEN
	STA	TEMP1
	LDY	#$00
AX2
	LDA	(POTMP0),Y	;TAKE A BYTE
	STA	(POTMP2),Y	;MOVE TO SCREEN MEM
	INC	POTMP0		;INCREMENT LOAD ADDRESS BY I
	BNE	AX3
	INC	POTMP1		;INCREMENT HIBYTE IF NECC.
AX3
	INC	POTMP2		;INCREMENT STORE ADDRESS BY ONE
	BNE	AX4
	INC	POTMP3		;INCREMENT HIBYTE IF NECCESSARY
AX4
	LDA	POTMP2
	CMP	TEMPO		;IS LOW BYTE--LOWBYTE OF ENDING ADDRESS
	BNE	AX2		;NO? KEEP MOVING THEM
	LDA	POTMP3
	CMP	TEMP1		;HIBYTE--HIBYTE OF ENDING ADDRESS?
	BNE	AX2		;KEEP MOVING THEM TILL ENDING ADDRESS REACHED
	LDX	#$13
AX5
	LDA	L1MSG,X
	STA	LINE1,X
	LDA	L2MSG,X
	STA	LINE2,X
	LDA	#$00
	STA	LINE3,X
	STA	LINE4,X
	DEX
	BPL	AX5
	RTS
LOOKAHEAD
	LDA	NEWH		;GET HORIZ. POS.
	AND	#$07		;GET # OF BITS INTO SCREEN CHR
	STA	XOFF		;SAVE IT
	LDA	NEWV
	AND	#$07		;GET # OF SCAN LINES DOWN INTO SCREEN CHR
	STA	YOFF		;SAVE IT
	SEC
	LDA	NEWH
	SBC	#$30		;-LEFT EDGE
	LSR
	LSR
	LSR			;/8
	STA	CHRX
	SEC
	LDA	NEWV
	SBC	#$20		;-TOP EDGE
	LSR
	LSR
	LSR			;/8
	STA	CHRY
	LDX	#$14		;20 BYTES/ROW
	JSR	MULTIPLY
	CLC
	LDA	CHRX
	ADC	RESULT
	STA	CHRLO
	LDA	#<SCREEN
	ADC	RESULT+1
	STA	CHRHI
	JSR	DOCPTR
	LDA	#$00
	STA	DPOINTER
LABEL
	LDY	YOFF
	LDA	(DATAP1),Y
	STA	BYTE
	LDA	(DATAP2),Y
	STA	BYTE+1
	LDY	XOFF
	BEQ	AY2
AY1
	ASL	BYTE+1		;COMBINE TWO BYTES
	ROL	BYTE
	DEY
	BNE	AY1
AY2
	LDY	DPOINTER
	LDA	(POTMP1),Y	;GET BYTE OF TANK DATA
	AND	BYTE		;MASK IT WITH OVERLAPPING 8 BITS OF SCREEN
	BNE	COLLISION	;COLLISION RESULT NON ZERO
* SET FOR NEXT POSITION
AY20
	INC	YOFF		;SET FOR NEXT BYTE OF CHARACTER DATA
	LDA	YOFF
	CMP	#$08		;HAVE WE DGONE THROUGH ALL 8 BYTES OF CHAR DATA
	BNE	AY4		;NO.
	LDA	#$00		;SET POINTER FOR 1ST BYTE OF CHR BELOW
	STA	YOFF
	CLC
	LDA	CHRLO
	ADC	#$14		;WIDTH
	STA	CHRLO
	BCC	AY3
	INC	CHRHI		;GO GET POINTERS TO CHRDATA FOR CHARACTERS ON THE NEXT LINE DOWN
AY3	JSR	DOCPTR
AY4
	INC	DPOINTER	;SET FOR NEXT BYTE OF TANK DATA
	LDA	DPOINTER
	CMP	#$10		;DONE ALL 16 BYTES OF TANK DATA
	BNE	LABEL		;00 BACK T:LL THEY ARE ALL DONE
	CLC			;SET FLAG FOR POSITION OK.
	RTS
COLLISION
	SEC			;FLAG POS. NO GOOD
	RTS
* GET CHR & ADJACENT CHR
DOCPTR
	LDY	#$01
	LDX	#$03
AZ1
	LDA	#$00
	STA	DATAP1,X
	LDA	(CHRLO),Y
	AND	#%00111111	;MASK OFF COLOR BITS
	STA	CHR1,Y
	ASL
	ROL	DATAP1,X
	ASL
	ROL	DATAP1,X
	ASL
	ROL	DATAP1,X
	DEX
	STA	DATAP1,X
	CLC
	LDA	DATAP1+1,X
	ADC	#<CHRSET
	STA	DATAP1+1,X
	DEX
	DEY
	BPL	AZ1
	RTS
* 8 BIT MULTIPLY
* MULTIPLIES ACC BY X REG.
* STORES RESULT (L,H) IN RESULT & RESULT+1
MULTIPLY
	STA	M1		;MULTIPLIER
	STX	M2		;MULTIPLICAND
	LDA	#$0
	STA	RESULT
	STA	REsuLT+1
	LDX	#$08
BA1
	ASL	RESULT
	ROL	RESULT+1
	ASL	M2
	BCC	BA2
	CLC
	LDA	RESULT
	ADC	M1
	STA	RESULT
	BCC	BA2
	INC	RESULT+1
BA2
	DEX
	BNE	BA1
	RTS
