;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;	DRAGONFIRE
;		AKA
;	CHEESE
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;	MAY 21, 1982
;	COPYRIGHT 1982 BY IMAGIC
;	ROBERT GRAVES SMITH III
;

	processor	6502

RELEASE	EQU	1
PAL	EQU	0		;(PAL COLORS NOT IN THIS LISTING, OTHER PAL SHOULD WORK)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; STELLA EQUATES
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


VSYNC	EQU		$00			; VERT SYNC
VBLNK	EQU		$01			;
HSYNC	EQU		$02			; STROBE
MCRES	EQU		$03			; MASTER COUNTER RESET
P0SIZE	EQU		$04			; SIZE, NUMBER OF COPIES
P1SIZE	EQU		$05			;
P0CLR	EQU		$06			; COLOR
P1CLR	EQU		$07			;
PFCLR	EQU		$08			;
BKCLR	EQU		$09
PFCTRL	EQU		$0A			; PLAYFIELD CONTROL

P0REF	EQU		$0B			; REFLECT
P1REF	EQU		$0C			;

PF0		EQU		$0D			; PLAYFIELD REGISTERS
PF1		EQU		$0E			;
PF2		EQU		$0F			;

P0RES	EQU		$10			; RESETS
P1RES	EQU		$11			;
M0RES	EQU		$12			;
M1RES	EQU		$13			;
BRES	EQU		$14			;

SNDC0	EQU		$15			; SOUND CONTROL
SNDC1	EQU		$16			;
SNDF0	EQU		$17			; FREQUENCY
SNDF1	EQU		$18			;
SNDV0	EQU		$19			; VOLUME
SNDV1	EQU		$1A			;

P0GR	EQU		$1B			; GRAPHICS REGISTERS
P1GR	EQU		$1C			;
M0GR	EQU		$1D			;
M1GR	EQU		$1E			;
BGR		EQU		$1F			;

P0HM	EQU		$20			; HMOVES
P1HM	EQU		$21
M0HM	EQU		$22
M1HM	EQU		$23
BHM		EQU		$24			;

P0VDEL	EQU		$25			; VERTICAL DELAYS
P1VDEL	EQU		$26			;
BVDEL	EQU		$27			;

MP0RES	EQU		$28			; MISSILE TRACKING
MP1RES	EQU		$29			;
HMOV	EQU		$2A			; HORIZ MOVEMENT STROBE
CLRHM	EQU		$2B			; CLEAR HMOVE STROBE
CCLR	EQU		$2C			; CX REGISTER RESET STROBE

;READ ADDRESSES

M0PPCX	EQU		$00			; M0 PLAYER CX
M1PPCX	EQU		$01			; M1 PLAYER
P0BPCX	EQU		$02			; P0 BALL/PLAYFIELD CX
P1BPCX	EQU		$03			;
M0BPCX	EQU		$04			; M0 BALL/PLAYFIELD CX
M1BPCX	EQU		$05			;
BPCX	EQU		$06			; BALL PLAYFIELD CX
PPMMCX	EQU		$07			; P0/P1 MISSILE CX
POT0A	EQU		$08			; POT IN, LEFT
POT0B	EQU		$09			; POT IN, LEFT
POT1A	EQU		$0A			; POT IN, RIGHT
POT1B	EQU		$0B			; POT IN, RIGHT
TRIG0	EQU		$0C			; SCHMIDT TRIGGER LEFT
TRIG1	EQU		$0D			; RIGHT

; PIA ADDRESSES

PLRCTL	EQU		$280			; PORT A (CONTROLLERS)
DDRA	EQU		$281			; DATA DIRECTION A
FPCTL	EQU		$282			; FRONT PANEL SWITCHES
DDRB	EQU		$283			; DATA DRIECTION B
TIMER	EQU		$284			; PIA TIMER
TIMOUT	EQU		$285			; PIA TIMER FLAG
TIMD1	EQU		$294			; CLOCK / 1
TIMD8	EQU		$295			; CLOCK / 8
TIMD64	EQU		$296			; CLOCK / 64
TIMD1K	EQU		$297			; CLOCK / 1024

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; GAME EQUATES
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

TOP		EQU		$A0

	IF	PAL
NUP1S	EQU		10			; NUMBER OF PLAYER 1'S
	ELSE
NUP1S	EQU		9
	ENDIF
NUP0S	EQU		5			; NUMBER OF PLAYER 0'S

DEAD	EQU		$D0
SUPRESS	EQU		$FF
FBSPEED	EQU		2			;FIREBALL SPEED
WINDOW	EQU		20
CAVECLR	EQU		$0A

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; SOUND EQUATES
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FIRES	EQU		0			;FIRE FIREBALL
BONSND	EQU		1			;BONUS
JUMPS	EQU		2			;JUMP SOUND
EXSND	EQU		3			;EXPLOSION
DEATHS	EQU		4			;DEATH
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; RAM ALOCATION
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;		CODE
		SEG.U	RAM
		ORG	$80

RAND		DS	1		;
RANDH		DS	1
FLAGS		DS	1		; D7=0=LOGO, D6=1, SHOW VERSION NUM
				 		; D0 = RESET SWITCH

EYETOP		DS	1		; UPPER LIMIT OF EYE
EYEBOT		DS	1		; LOWER LIMIT OF EYE
NEWCNT		DS	1		; COUNTER FOR NEW BOARD SETUP
ATTRACT		DS	1		; 
GAMESTAT	DS	1		; D7 RESET=CASTLE SCREEN, D6 RESET=ATTR
PLYRSTAT	DS	1		; D7 SET=PLAYER INIT
DRAGSTAT	DS	1		; D6 SET=FLAMING
REFSHAD		DS	1		; REFLECT FOR PLAYER
GSEL		DS	1		; SELECT TIMER
GAMNUM		DS	1		; GAME NUMBER
T0			DS	1		;
T1			DS	1		;
T2			DS	1		;
T3			DS	1		;
CX			DS	1		; COLLISION POINTER
LIVES		DS	2		; LIFE COUNTERS
P1POINT		DS	1		;
P0POINT		DS	1		;
COUNT		DS	1		;

ZSTART
XSTART

P0XS		DS	NUP0S
DRAGONX		DS	1		;
EYEX		DS	1		; EYE XPOS
PLAYERX		DS	1		;
XEND

PLAYERY		DS	1		;
PLAYNO		DS	1		; PLAYER UP
WAVENO		DS	1		; WAVE NUM
FRAME		DS	2		; 16 BIT FRAME COUNTER
DEATH		DS	1		; DEATH FLAG
;
PLAYERLX	DS	1		;
PLAYERLY	DS	1		;
PLAYERDX	DS	1		;
PLAYERDY	DS	1		;
;
DRAGONDX	DS	1		;DRAGON DELTA
;
SCOREL0		DS	2		; LO SCORE
SCOREM0		DS	2		;
SCOREH0		DS	2	;

ZEND
; HMOVE VALUES
P1HMS		DS	NUP1S		;LIST OF HMOVES FOR P1'S
P0HMS		DS	NUP0S		;
DRAGONHM	DS	1		;
EYEHM		DS	1		;
PLAYERHM	DS	1		;

P0YS		DS	NUP0S		;
P0Y			DS	1		;
P1ID		DS	NUP1S		;
P0ID		DS	NUP0S		;

;MAP THE RAT & ARROW RAM OVER THE P0 ID LIST
RATTIME		EQU	*-4		; RAT TIMER
ARROWTIM	EQU	*-3		; ARROW TIMER
RATHM		EQU	*-2		; RAT HMOVE
ARROWHM		EQU	*-1		; ARROW HMOVE

PLAYERID	DS	1		;
P1INDCLR	DS	2		;
P0INDCLR	DS	2		;
P1IND		DS	2		;
P0IND		DS	2		;

DRAGTIME	DS	1		; DRAGON FLAMING TIMER
DRAGONAN	DS	1		;

; DRAGON AND SIXCHAR INDIRECTION
ARRWIND					; ARROW INDIRECTION
SIXIND0
DRAGH0		DS	2
;
ARRWINDC				; ARROW COLOR INDIRECTION
SIXIND1
DRAGH1		DS	2
;
SIXIND2
DRAGH2		DS	2		;
;
SIXIND3
DRAGH3		DS	2
;
SIXIND4
DELAY		DS	2		; INDIRECT JUMP INTO KERNEL

SIXIND5		DS	2		;

CAVESHAD   	DS	1		; SHADOW CAVE COLOR

SND1		DS	1		; SOUND ID
SND1TIME	DS	1		; SOUND TIMER
JUMPTIME	DS	1		;
SELTIME		DS	1		; SELECT SWITCH TIMER
SEA			DS	1		;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; MAIN PROGRAM
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	SEG	CODE
	ORG	$1000,0

START	SEI
		CLD			; NO DECIMAL
		LDX	#$00
		TXA
CLP		STA	$0,X	; CLEAR STELLA & RAM
		TXS
		INX
		BNE	CLP
;
; INITS
;
		DEC	ATTRACT
		LDA	#$11
		STA	PFCTRL
		STA	RANDH
		JSR	SETUPCA		; SET UP CASTELE SCREEN
		LDA	#150
		STA	PLAYERX
		STA	PLAYERY
		STA	RAND
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; SYNC FOR NEW FRAME
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SCRNTOP	LDA	#2			; VBLANK ON
		STA	VBLNK
		STA	HSYNC		; VSYNC FOR 3 SCAN LINES
		STA	VSYNC

	; SET UP HIGH BYTES FOR INDIRECTION
		LDA	#(ATREAS >>8 )
		STA	P1IND+1
		LDA	#MAN0 >>8
		STA	P0IND+1
		LDA	#TREASCLR >>8
		STA	P1INDCLR+1
		LDA	#MAN0C >> 8
		STA	P0INDCLR+1

; SET UP DRAGONS EYE OR FLAME
; 30 MC
;
		LDA	DRAGSTAT
		ASL
		ASL
		LDA	DRAGONDX
		ROL
		ROL
		AND	#$3
		TAX
		LDA	DRAGONX
		CLC
		ADC	OFFSETS,X
		STA	EYEX
; RANDOM NUMBER GENERATOR
RANDOM	LDA	RAND
		ROR
		ROR
		ROR
		EOR	RANDH
		ASL
		ASL
		ROL	RAND
		ROL	RANDH
		LDA	RAND
;
		STA	HSYNC
		LDA	CAVESHAD	; SETUP PLAYFIELD COLOR
		EOR	#$C			; DROP 2 LUMES
		AND	ATTRACT
		STA	PFCLR
		LDA	#$0
		STA	HSYNC
		STA	VSYNC		; OFF GOES VSYNC

	IF PAL
		LDA	#$42
	ELSE	
		LDA	#$2C
	ENDIF
		STA	TIMD64
;
; SOMEONE DIE THIS FRAME?
;
		LDX	PLAYNO
		LDA	LIVES,X
		BPL	OUT23
; START DEATH, SOMEONE DEAD!
	IF PAL
		LDA	#150+15
		STA	PLAYERY
		LDA	#150
		STA	PLAYERX
	ELSE
		LDA	#150
		STA	PLAYERY
		STA	PLAYERX
	ENDIF

		LDY	SND1TIME
		DEY
		BEQ	SKIP39
		LDX	#DEATHS
		CPX	SND1
		BEQ	OUT23
		JSR	STARTSND
		JMP	OUT23
SKIP39
;
		LDA	GAMNUM
		LSR
		BCS	PLAY2		; TWO PLAYER

; END GAME
ENDGAME	JSR	SETUPCA
		STA	LIVES		;(A = 0)
		STA	LIVES+1
		STA	GAMESTAT
		LDA	#150
		STA	PLAYERY
		STA	PLAYERX
		BNE	OUT23
; 2 PLAYER VERSION
PLAY2	LDA	LIVES
		BPL	ALIVE0
		LDA	LIVES+1
		BMI	ENDGAME		; BOTH DEAD
; O DEAD, 1 ALIVE
ALIVE1	LDA	PLAYNO
		EOR	#$1
		STA	PLAYNO
		JSR	SETUPCA
		BEQ	OUT23
ALIVE0 LDY WAVENO
		INY
		CPY	#9
		BCS	ALIVE1
		STY	WAVENO
		BCC	ALIVE1		; START NEW WAVE
OUT23
;
; ANIMATE MAN
;
		LDA	FRAME
		AND	#$7
		TAY
		AND	#$1
		BNE	OUT16
; SPECIAL CASE, EXPLODING MAN
		LDA	PLAYERID
		CMP	#(SUPINE & $FF)
		BCC	SKIP40
;EXPLODING MAN
		CPY	#0
		BNE	OUT16
		ADC	#$F
		CMP	#(SUPINE2+1) & $FF
		BCC	WRID
;EXPLOSION OVER
		LDA	#$8C
		STA	PLAYERX
		LDA	#0
		STA	PLAYERY
		STA	PLAYERDY
		LDA	#$80
		STA	PLYRSTAT
		LDX	PLAYNO
		DEC	LIVES,X
		LDA	#MAN0 & $FF
WRID	STA	PLAYERID
		JMP	OUT16
SKIP40
; FORCE KNEELING MAN IF CASTLE AND DOWN STICK
		BIT	GAMESTAT
		BMI	SKIP07		; DOING DRAGON SCREEN
		LDA	PLRCTL		; LOAD STICK
		LDX	PLAYNO
		BEQ	SKIP09
		ASL
		ASL
		ASL
		ASL
SKIP09	AND	#$20
		BNE	SKIP23
		STA	PLAYERDX	;KNEELING MAN CAN'T MOVE
		LDA	#MAN4 & $FF
		BNE	SKIP75
SKIP23
SKIP07
		LDA	PLAYERDX
		ORA	PLAYERDY
		BNE	SKIP76
DOMAN0	LDA	#(MAN0 & $FF)	;FORCE STILL PICTURE
		BEQ	SKIP75
SKIP76	LDA	PLAYERID
		CMP	#(MAN2 & $FF)
		BCS	DOMAN0
		ADC	#$10
SKIP75	STA	PLAYERID
OUT16
; SET UP SCORE FOR VERSION NUMBER
		BIT FLAGS
; SET UP VERSION NUMBER
NOLOGO	BVC	OUT03
		LDA	GAMNUM
		AND	#$1
		LSR
		STA	SCOREM0
		ADC	#1
		STA	SCOREL0
		LDA	GAMNUM
		LSR
		TAX
		INX
		STX	SCOREH0
OUT03



;
; REMOVE ONE EMPTY SLOT IN LIST OF P0'S
;
		LDX	#0
		STX	T0
;
LOOP2	LDY	P0YS,X
		CPY	#DEAD
		BNE	SKIP08
		INC	T0
LOOP3	INX				; BUMP LOOP COUNT
		CPX	#NUP0S
		BNE	LOOP2
		BEQ	OUT06
SKIP08	LDA	T0
		BEQ	LOOP3
;READING VLID ENTRY
LOOP4	LDA	P0ID,X		; GET ID
		PHA
		LDA	P0XS,X		
		DEX
		STA	P0XS,X
		PLA
		STA	P0ID,X
		STY	P0YS,X
		INX				; POINT TO OLD ENTRY
		INX				; POINT OVER
		LDY	P0YS,X
		CPX	#NUP0S		; CHECK FOR MAX
		BNE	LOOP4
OUT06
;
; ROOM CLEAR?
;
		LDX	#NUP1S-1
		LDA	#(SPACE10 & $FF)
ENDLOOP	CMP	P1ID,X
		BNE	OUT11
		DEX
		BNE	ENDLOOP
;
		LDA	#(ODOOR & $FF)
		STA	P1ID+NUP1S-1
		STX	P1HMS+NUP1S-1	; X = 0
OUT11
;
; FIREBALL BIRTH
;
		BIT	GAMESTAT	;DON'T DO IF CASTLE SCREEN
		BPL	OUT10

		LDX	#$FF
		LDA	PLAYERY		; IF PLAYER HIDDEN, QUIT
		BEQ	OUT10
;SCAN LIST TO FIND AVAILABLE SLOT
SERCHLP	INX
		CPX	#NUP0S-1
		BCS	OUT10
		LDA	P0YS,X
		CMP	#DEAD
		BNE	SERCHLP

;FOUND A SLOT
GOTONE	CPX	#0			;ALREADY TOP OF LIST?
		BEQ	SKIP17
		DEX				;SEE IF ROOM
		LDA	P0YS,X
		CMP	#42			;MIN SEPARATION
		BCC	OUT10
		INX				;ROOM TO SPARE
SKIP17	LDA	DRAGONX
		ADC	#$14		;OFFSET TO DRAGON HEAD
		BIT	DRAGONDX	;TRAVEL LEFT OR RIGHT
		BPL	SKIP16		;RIGHT
		SBC	#$12
SKIP16	STA	T0			;CHECK FOR PRESENSE IN FLAME WINDOW
		CMP	PLAYERX
		BCC	SUBT0
		SBC	PLAYERX
		BCS	SKIP29		;ALWAYS TAKEN
;
SUBT0	LDA	PLAYERX
		SEC
		SBC	T0
SKIP29	CMP	#WINDOW
		BCS	OUT10
		LDA	T0
		STA	P0XS,X
		LDA	#$10		;START FLAME TIMER
		STA	DRAGTIME
		LDA	#$40
		STA	DRAGSTAT	;ENABLE GRAPHICS
		STX	T1
		LDX	#FIRES		;START SOUND
		JSR	STARTSND
		LDX	T1
		LDA	#17			;SET UP FLAME
		STA	EYETOP
		LDA	#10
		STA	EYEBOT
		LDA	#0
		STA	P0YS,X
		LDA	#(FB0 & $FF)
		STA	P0ID,X
OUT10
		INC	FRAME
		BNE	NOINC
; CHANGE PLAYER NUMBER IF 2 PLAYER AND GAME OVER
		BIT FLAGS
		BVS	OUT21
		BIT	GAMESTAT
		BVS	OUT21
		LDA	GAMNUM
		LSR
		BCC	OUT21
		LDA	PLAYNO
		EOR	#1
		STA	PLAYNO
OUT21
;
		INC	FRAME+1
		BNE	NOINC
		LDA	#$F3
		STA	ATTRACT
		LDA	#$0
		STA	GAMESTAT
NOINC
;
; PLAYER/ FIRE BALL COLLISIONS
;
		LDA	#0
		STA	T0
		BIT	GAMESTAT
		BPL	OUT04
		LDA	PLAYERID		;DONT CX WITH EXPLODING MAN
		CMP	#(SUPINE & $FF)
		BCS	OUT04
		LDA	PLAYERY			;CHECK FOR Y CX FIRST
		BEQ	OUT04
		SEC
		SBC	#16
		BCC	NEGNUM
		STA	T0
NEGNUM	CLC
		ADC	#32
		STA	T1
;CHECK IN Y
		LDX	#NUP0S-1
PCXLOOP	LDA	P0YS,X
		CMP	#DEAD
		BEQ	NEXT07
		CMP	T0
		BCC	NEXT07
		CMP	T1
		BCC	MAYBE
NEXT07	DEX
		BPL	PCXLOOP
		BMI	OUT04
;CHECK IN X
MAYBE	LDA	 PLAYERX
		SBC	#6
		BCC	NEGNUM1
		CMP	P0XS,X
		BCS	NEXT07		;NO CX
NEGNUM1
		ADC	#12
		CMP	P0XS,X
		BCC	NEXT07
;COLLISION!!
		LDA	#(DOOR & $FF)
		STA	P1ID
		LDA	#DEAD		;EXPLODE FIREBALL
		STA	P0YS,X
		LDA	#SUPINE & $FF
		STA	PLAYERID
		LDX	#EXSND		;SOUND
		JSR	STARTSND
OUT04
;
; DERIVE HMOVE AND DELAY VALUES FOR OBJECTS
;
DOHMS	LDX	#XEND-XSTART-1	;LOAD NUMBER OF OBJECTS
DOHMOVES	LDA	P0XS,X		;LOAD XPOS
		LSR					;/16
		LSR
		LSR
		LSR
		SEC
		ADC	P0XS,X
		LSR
		LSR
		LSR
		LSR
		STA	T0			;DELAY VALUE
		CLC
		ADC	P0XS,X
		AND	#$F			;GET HMOVE VALUE 0-E
		SEC
		SBC	#$7			;MAKE 0-E FORMAT INTO SIGNED FOR HMOV
		EOR	#$FF
		ASL
		ASL
		ASL
		ASL				;60 = CLOCK 0
		ORA	T0
		STA	P0HMS,X
		DEX
		BPL	DOHMOVES
; RESET EYE OR FLAME
		STA	HSYNC
		LDA	EYEHM
		STA	BHM
		AND	#$F
		TAY
		NOP
		NOP
		NOP
EYELOOP DEY
		BPL	EYELOOP
		STA	BRES



; SET UP INDIRECT JUMP FOR MOVING FOUR CHAR
		LDA	DRAGONHM
		LSR
		LSR
		LSR
		LSR
		TAY
		LDA	DELAYS,Y
		STA	DELAY			;SET UP INDIRECT JUMP
		LDA	#(W11 >> 8)
		STA	DELAY+1
	;
; RESET TOP OF LIST P0
		LDA	FRAME
		LSR
		BCS	DOPLYR1
		LDA	P0HMS
		LDX	#1
		STX	P0POINT
		LDX	P0ID
		LDY	P0YS
		CPY	#DEAD
		BNE	SKIP11
DOPLYR1	LDA PLAYERHM
		LDX	#NUP0S
		STX	P0POINT
		LDX	PLAYERID
		LDY	PLAYERY
SKIP11	STA	HSYNC
		STX	P0IND
		STX	P0INDCLR
		STY	P0Y
		STA	P0HM
		AND	#$F			;GET DELAY
		TAY
RLOOPP0	DEY
		BPL	RLOOPP0
		STA	P0RES
		STA	HSYNC
		STA	HMOV
		STY	CX			;PUT FF INTO CX
		INY
		STY	P1SIZE
		STY	P0SIZE
;
; LAST LINES BEFORE KERNEL
;
		LDY	#NUP1S		;NUMBER OF BANDS
		STY	P1POINT
	IF PAL
		LDX	#$A5
	ELSE
		LDX	#$A5-15		;SETUP LINE COUNT
	ENDIF

		LDY	#0
		STY	COUNT
		LDA	REFSHAD		;REFLECT PLAYER 1
		STA	P0REF
		STA	CLRHM
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; END OF VERTICAL BLANK
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
VBOUT	BIT	TIMOUT		;WAIT FOR VERTICAL BLANK OVER
		BPL	VBOUT
		STA	HSYNC
		STY	VBLNK		;ENABLE ELECTRON BEAM

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; KERNELS BGINS HERE
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		STY	BKCLR
		BIT	GAMESTAT
		BMI	DOCAVE
		STY	PF0
		STY	PF1
		STY	PF2
	IF PAL
		LDA	#$DA
	ELSE
		LDA	#$C8
	ENDIF
		STA	TIMD64
		BNE	DOCASTLE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;CAVE TOP KERNEL
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DOCAVE	LDY	#$03
CAVELOOP	STA	HSYNC
		STA	HSYNC
		STA	HSYNC
		LDA	CAVE0,Y
		STA	PF0
		LDA	CAVE1,Y
		STA	PF1
		LDA	CAVE2,Y
	
		DC	$8D		; STA PF2 (ABSOLUTE)
		DC.W	PF2

		DEY
		BPL	CAVELOOP
		NOP
		NOP
		JMP	KENTRY			;MC 33
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; CASTLE KERNALS
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DOCASTLE
		LDA	#$15
		STA	PFCTRL
;
		LDA	#(HFB0 >>8)
		STA	ARRWIND+1
		LDX	#(HFB0 & $FF)	;HORIZONTAL FIREBALL
		LDA	FRAME
		
		AND	#4
		BNE	SKIP41
		LDX	#(HFB1 & $FF)
SKIP41	STX	ARRWIND
		LDX	#14
		STX	T0				;COUNT FOR ARROW
;
		LDY	#$0A
CASLOOP	LDX	#7
CASLOOP0	STA	HSYNC
		LDA	CASTLE0,Y
		STA	PF0
		LDA	CASTLE1,Y
		STA	PF1
		DEX
		BPL	CASLOOP0
		DEY
		BPL	CASLOOP
		INY
		STA	T1
;
		LDA	RATHM
		STA	T2				;SAVE HMOVE VALUE
		AND	#$F
		STA	T3				;HOLD RAT DELAY
		STA	HSYNC
		NOP
		NOP
		LDa	ARROWHM	
		LDX	#25				;LINE COUNT
		STA	P1HM
		AND	#$F
		TAY
RESLOOP0	DEY
		BPL	RESLOOP0
		STA	P1RES
		STA	HSYNC
		STA	HMOV
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;ARROW/RAT/MAN KERNAL
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
WALKLOOP	STA	HSYNC
		STA	P0CLR			;MC 3
		LDA	T1				;MC 6
		STA	P0GR			;MC 9
		LDY	T0				;MC 12
		LDA	(ARRWIND),Y		;MC 17
		STA	P1GR			;MC 20
		LDA	#$44			;MC 22 LOAD RED COLOR
		STA	P1CLR			;MC 25
;
		LDA	T2
		STA	P1HM			;MC 31
MKERNEL	TXA
		SEC
		SBC	P0Y
		TAY
		AND	#$F0
		BNE	SKIP95
		LDA	(P0IND),Y
		STA	T1
		LDA	(P0INDCLR),Y
SKIP95	DEX
		BMI	SHIELDK
		DEC	T0
		BPL	WALKLOOP
;
RESET	STA	HSYNC
		STA	P0CLR
		LDA	T1
		STA	P0GR
		LDY	T3
;
		TXA
		SEC
;
RATLOOP	DEY
		BPL	RATLOOP
		STA	P1RES
		STA	HSYNC
		STA	HMOV
		SBC	P0Y
		TAY
		LDA	(P0IND),Y
		STA	P0GR
		LDA	(P0INDCLR),Y
		STA	P0CLR
SKIP04	DEX
		LDY	#$9
		STY	T0
		JMP	MKERNEL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; SHIELD KERNAL
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SHIELDK
		LDA	P0BPCX	;PLAYER/WALL CX
		STA	HSYNC
		STA	CX
		LDA	#0
		STA	P1GR
		STA	P0GR
		STX	P0REF
		STX	PF0
		STX	PF1
		STX	PF2
		LDY	#$11
		NOP
		STA	P1RES	;MC 30
		STY	PFCTRL
		LDA	#3
		STA	P1SIZE
		STA	P0SIZE
		LDX	WAVENO
		LDA	DRAGCLR,X
		STA	P0RES	;MC 51
		AND	ATTRACT
		STA	P1CLR
		STA	P0CLR
		LDX	#9
		STA	HSYNC
		LDA	CAVESHAD
		EOR	#$2
		AND	ATTRACT
		STA	PFCLR
		STA	HSYNC
SHIELDLP	STA	HSYNC
		LDA	SHIELD,X
		STA	P1GR
		STA	P0GR
		DEX
		BPL	SHIELDLP
;
;
		STA	HSYNC
		LDY	#$80
		STY	PF1
		STY	PF2

	IF	PAL
		LDY	#30
	ELSE
		LDY	#20
	ENDIF
MOATLOOP	STA	HSYNC
		DEY
		BPL	MOATLOOP
		LDA	SEA
		STA	BKCLR
		STA	HSYNC

	IF PAL
		LDA	#$B4
	ELSE
		LDA	#$84
	ENDIF

		AND	ATTRACT
		STA	BKCLR
;
; RETURN TO MAIN SCREEN
;
		JMP	TIMLOOP1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; CASTLE SCREEN ROUTINES
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
MOVERAT	LDA	PLAYERID
		CMP	#(SUPINE & $FF)
		BCS	SKIP06
		LDA	PLAYERX
		CMP	#12
		BCS	SKIP06
		JMP	SETUPDR
SKIP06
;
; MOVE RATS AND ARROWS
;
		LDX	#1
MRATLOOP	LDY	RATTIME,X
		BEQ	MOVE
		DEC	RATTIME,X
		BNE	NEXT08
		STX	T1
		LDX	#FIRES
		JSR	STARTSND
		LDX	T1
MOVE	TXA
		TAY
		LDA	RATHM,X
MOVELOOP	SEC
		SBC	#$10
		BVC	NOWRAP
		SEC
		SBC	#$F
NOWRAP	CMP	#$CA
		BNE	SKIP02
		LDA	#$60
SKIP02
		CMP	#$40
		BNE	SKIP03
		LDA	RAND
		AND	#$1F
		ADC	WAVENO
		EOR	#$3F
		ASL
		STA	RATTIME,X
		LDA	#$40
		BNE	NEXT09
SKIP03	DEY
		BPL	MOVELOOP
NEXT09	STA	RATHM,X
NEXT08	DEX
		BPL	MRATLOOP
	;
; JUMP
;
J		BIT	GAMESTAT
		BVC	OUT18
		LDA	PLAYERDY
		BNE	SKIP24
		LDY	PLAYERID
		CPY	#(SUPINE & $FF)
		BCS	OUT18
		LDX	PLAYNO
		LDY	TRIG0,X
		BMI	SKIP24
		LDX	#JUMPS
		JSR	STARTSND
		LDA	#12
		STA	JUMPTIME
		LDA	#1
		STA	PLAYERDY
;
SKIP24
		CLC
		ADC	PLAYERY
		STA	PLAYERY
		BNE	SKIP27
		STA	PLAYERDY
SKIP27
		CMP	#$9
		BCC	OUT18
; AT TOP OF JUMP
		DEC	PLAYERY
		DEC	JUMPTIME
		BNE	OUT18
		LDY	#$FF
		STY	PLAYERDY
OUT18
;
; CX
;
		LDA	PLAYERID		;DONT CX WITH EXPLODING MAN
		CMP	#(SUPINE & $FF)
		BCS	OUT19
		BIT	PPMMCX
		BPL	OUT19
		BIT	CX
		BMI	OUT19			;LEAVE IF TOUCHING WALL
		LDA	#(SUPINE & $FF)
		STA	PLAYERID
		LDX	#EXSND			;TURN ON EXPLOSION
		JSR	STARTSND
		LDA	#$4A
		STA	ARROWHM			;START FIREBALLS AGAIN
		STA	RATHM
		LDA	#0
		STA	RATTIME
		STA	ARROWTIM
OUT19	STA	CCLR
		RTS
;
; MORE SUBROUTINES
;
SETUPCA
		ROL	GAMESTAT
		CLC
		ROR	GAMESTAT
		LDA	#$60
		STA	ARROWHM
		STA	RATHM
		LDA	#$8C
		STA	PLAYERX
		LDA	#NUP1S-1
		STA	NEWCNT
;
; CLEAR P0 LISR
;
		LDX	#NUP0S-1
		LDA	#DEAD
KILLLOOP	STA	P0YS,X
		DEX
		BPL	KILLLOOP
		LDA	#0
		STA	ARROWTIM
		STA	RATTIME
		STA	PLAYERDX
		STA	PLAYERDY
		STA	PLAYERY
		STA	DRAGSTAT
		RTS

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; MAIN KERNEL
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		ORG	$1500

LDZERO0	NOP							;MC 49
		NOP							;MC 51
		LDA	#0						;MC 53
		STA	T1						;MC 56
		BEQ	SKIP10					;MC 59
TOENDK	JMP	ENDK
LOOP1	STA	P0CLR					;MC 73
		LDA	T1						;MC 76
;
KERNEL	STA	P0GR					;MC 3
		LDA	(P1IND),Y				;MC 8
		STA	P1GR					;MC 11
		LDA	(P1INDCLR),Y			;MC 16
		STA	P1CLR					;MC 19
;
		JSR	WASTE12					
		DEX							
KENTRY	TXA							;MC 35
		SEC							;MC 37
		SBC	P0Y						;MC 40
		TAY							;MC 42
		AND	#$F0					;MC 44
		BNE	LDZERO0					;MC 46/47
		LDA	(P0IND),Y				;MC 51
		STA	T1						;MC 54
		LDA	(P0INDCLR),Y			;MC 59
SKIP10
		DEC	COUNT					;MC 64
		LDY	COUNT					;MC 67
		BPL	LOOP1					;MC 69/70
;
; LINE 11 SETUP P1
;
		DEC	P1POINT					;MC 74
		BMI	TOENDK					;END KERNEL
		STA	P0CLR					;MC 3
		LDA	T1						;MC 6
		STA	P0GR					;MC 9
;SETUP P1
		LDY	P1POINT					;MC 12
		LDA	PPMMCX					;MC 15 P1/P0 CX
		BPL	EQUTIME					;MC 17/18
		STY	CX						;MC 20 SAVE LINE NUM OF CX
EQUTIME	STY	CCLR					;MC 23 CLEAR CX
		LDA	P1ID,Y					;MC 27
		STA	P1IND					;MC 30
		STA	P1INDCLR				;MC 33
		LDA	P1HMS,Y					;MC 37
		STA	P1HM					;MC 40 SET UP HMOVE
		DEX							;MC 42
		AND	#$F						;MC 44 GET DELAY VALUE
		STA	T0						;MC 47
		JMP	SKIP05					;MC 50
;
;
;
LDZERO2	LDA	#0
		TAY
		BEQ	SKIP12
LDZERO4	LDA	#0
		BEQ	SKIP13
;
;
SKIP05	TXA							;MC 52
		SEC							;MC 54
		SBC	P0Y						;MC 57
		TAY							;MC 59
		AND	#$F0					;MC 61
		BNE	LDZERO2					;MC 63/64
		LDA	(P0IND),Y				;MC 68
		STA	P0GR					;MC 71
SKIP12
;
; LINE 12 RESET P1 KERNEL
;
		STA	HSYNC
		LDA	(P0INDCLR),Y			;MC 5
		STA	P0CLR					;MC 8
		DEX							;MC 10
		SEC							;MC 12	;PREPARE FOR P0
		TXA							;MC 14
		LDY	T0						;MC 17
RESETLP	DEY							;MC 19
		BPL	RESETLP					;MC 21/22
		STA	P1RES					;MC 24
;
		SBC	P0Y						;MC 73
		STA	HSYNC					;MC 76
;
; LINE 13 CHECK FOR P0 RESET
;
		TAY							;MC 2
		AND	#$F0					;MC 4
		BNE	LDZERO4					;MC 6/7
		LDA	(P0INDCLR),Y			;MC 11
		STA	P0CLR					;MC 14
		LDA	(P0IND),Y				;MC 19
		STA	P0GR					;MC 22
SKIP13	STA	T1						;MC 25 (15 IF NO P0)
		TXA							;MC 27
;
		LDY	P0POINT					;MC 42
		DEX							;MC 44
		CMP	P0Y						;MC 48
		BCS	NORESET					;MC 50/51
; IF THIS PATH IS TAKEN, NO P0, SO MAX COUNT IS 39
		CPY	#NUP0S					;MC 
		BEQ	DOPLYR					;MC 
		LDA	P0YS,Y					;MC 43
;
		CMP	#DEAD					;MC 
		BNE	SKIP99					;MC 
;PUT PLAYER ON SCREEN
DOPLYR	LDA	PLAYERHM				;MC 
		STA	P0HM					;MC 
		AND	#$F						;MC 
		STA	T2						;MC 
		LDA	PLAYERY					;MC 
		STA	P0Y						;MC 
		LDA	PLAYERID				;MC 
		JMP	CONT0					;MC 
SKIP99
		STA	P0Y						;MC 56
		LDA	P0HMS,Y					;MC 
		STA	P0HM					;MC 46
		AND	#$F						;MC 
		STA	T2						;MC 49
		LDA	P0ID,Y					;MC 66
		INY							;MC 
CONT0	STA	HSYNC					;MC 74
;
; LINE 14 RESET P0
;
		STA	P0IND					;MC 
		STA	P0INDCLR				;MC 
		STY	P0POINT					;WASTE 3
		LDY	T2						;MC 
		NOP							;MC 
		DEX							;MC 17
RP0LP	DEY							;MC 19
		BPL	RP0LP					;MC 21/22
		STA	P0RES					;MC 24/?
;
; LINE 15 SETUP TO RETURN TO MAIN KERNEL
;
		STA	HSYNC					;MC 
		STA	HMOV					;MC 3
		NOP							;MC 5
		NOP							;MC 7
		NOP							;MC 9
		JMP	AENTRY					;MC 12
;
TOENDK1	JMP	ENDK
;
; LINE 13 NO P0 RESET, RUN INSTEAD OF P0 RESET
;
NORESET	TXA							;MC 53
		SEC							;MC 55
		SBC	P0Y						;MC 58
		TAY							;MC 60
		AND	#$F0					;MC 62
		BNE	SKIP14					;MC 64/65
		LDA	(P0IND),Y				;MC 69
		STA	T1						;MC 72
SKIP14
;
; LINE 14 MAPS TO P0 RESET LINE
;
		STA	HSYNC					;MC 75 MAX
		LDA	T1						;MC 3
		STA	P0GR					;MC 6
		LDA	(P0INDCLR),Y			;MC 11
		STA	P0CLR					;MC 14
		DEX							;MC 16
		TXA							;MC 18
		SEC							;MC 20
		SBC	P0Y						;MC 23
		TAY							;MC 25
		AND	#$F0					;MC 27
		BNE	LDZERO3					;MC 29/30
		LDA	(P0IND),Y				;MC 44
		STA	T1						;MC 47
SKIP15
		LDA	(P0INDCLR),Y			;MC 52
		STA	HSYNC					;MC 55
;
; LINE 15 MAPS TO P1 SETUP LINE
;
		STA	HMOV					;MC 3
		STA	P0CLR					;MC 6
		LDA	T1						;MC 9
		STA	P0GR					;MC 12
AENTRY	DEX							;MC 14
		NOP							;MC 16
		NOP							;MC 18
		NOP							;MC 20
		NOP							;MC 22
		LDA	#10						;MC 24 SET UP LINE COUNT FOR KERNEL
		STA	COUNT					;MC 27
		STA	CLRHM					;MC 30
		JMP	KENTRY					;MC 33
;
LDZERO3	LDY	#0
		BEQ	SKIP15

ENDK
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; DRAGON KERNEL
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		LDA	DRAGSTAT			; TURN ON FLAME?
		ROL
		ROL
		ROL
		ROL
		STA	BGR
;
		LDX	WAVENO
		LDA	#$4C				; FLAME COLOR
		STA	PFCLR
;
		LDY	#0
		STY	P0GR

	IF PAL
		LDA	#$C0
	ELSE
		LDA	#$C0
	ENDIF

		STA	TIMD8

; RESET PLAYERS FOR DRAGON
		INY
		STY	P1SIZE
		STY	P0SIZE
		LDA	DRAGONHM
		STA	HSYNC
		STA	P1HM
		CLC
		ADC	#$10
		STA	P0HM
		NOP
		AND	#$0F
		TAY
DRLOOP	DEY
		BPL	DRLOOP
		STA	P1RES				; POSITION PLAYERS
		STA	P0RES
;LAST LINE BEFORE KERNEL
		STA	HSYNC
		STA	HMOV
		TAY
DELLOOP	DEY
		BPL	DELLOOP
		JMP	(DELAY)					;MC 14 JUMP TO PROPER TIME WASTER

;
; KERNEL ENTRY POINTS
; WASTE 5-11 CYCLES (INCLUDING INDIRECT JUMP)
;
W11	DC	$A9			; LDA IMMED (LDA #A9,LDA #A9,LDA $EAA5 = 8)
W10	DC	$A9			; (LDA #A9, LDA #AD, LDA $EA = 7)
W9	DC	$A9			; (LDA #A9, LDA $EAA5 = 6)
W8	DC	$A9			; (LDA #AD, LDA $EA = 5)
W7	DC	$AD			; LDA ABS (LDA $EAA5 = 4)
W6	DC	$A5			; LDA ZP (LDA $EA = 3) 
W5	DC	$EA			; NOP	(NOP= 2) 

DKERNEL
		LDA	DRAGCLR,X
		AND	ATTRACT
		STA	P1CLR
		STA	P0CLR
		NOP
		LDY	#15
KERLOOP	CPY	EYETOP
		BCS	OFF
		CPY	EYEBOT
		ROL
		ROL
		NOP
SKIP25	STA	BGR
		JMP	SKIP26
OFF		LDA	ATTRACT	;WASTE 3 MC
		LDA	#0
		BEQ	SKIP25
SKIP26	LDA	DRAGONDX
		STA	P1REF
		STA	P0REF
		NOP
		NOP
		NOP
		NOP
;
;
		LDA	(DRAGH3),Y				;MC 42
		STA	P1GR					;MC 45
		LDA	(DRAGH2),Y				;MC 50
		STA	P0GR					;MC 53
		LDA	(DRAGH1),Y				;MC 58
		TAX							;MC 60
		LDA	(DRAGH0),Y				;MC 65

		STX	P1GR					;MC 68
		STA	P0GR					;MC 71
;
		DEY							;MC 73
		BPL	KERLOOP					;MC 75/76
		INY
		STY	P1GR
		STY	P0GR
;
TIMLOOP1	LDA	TIMER
		BNE	TIMLOOP1
		STA	HSYNC
		LDA	CAVESHAD				; HOLD COLOR
		STA	PFCLR
		LDX	#$FF
		STX	PF1
		STX	PF0
		STX	PF2
		LDA	#4						; WHICH SCORE?
		CLC
		ADC	PLAYNO
		TAY
		LDA	#(SPACE & $FF)
		STA	T2						; STORE SPACE FOR ZERO SUPRESSION

		LDA	#(NUMBERS >>8 )
		STA	SIXIND0+1
		STA	SIXIND0+3
		STA	SIXIND0+5
		STA	SIXIND0+7
		STA	SIXIND0+9
		STA	SIXIND0+11
	
		STA	CLRHM

;
; WRITE SCORE INDIRECTION
;
		LDX	#11
SIXILOOP	DEX
		LDA	SCOREL0,Y
		STY	T0					; HOLD INDEX
		PHA						; SAVE SCORE BYTE
		AND	#$F0
		LSR
		ADC	#(N0 & $FF)
		CMP	#(N0 & $FF)
		BNE	SKIP89				; NON ZERO
;
		LDA	T2
		BNE	SKIP88
SKIP89	LDY	#(N0 & $FF)
		STY	T2					; MAKE T2 "ZERO"
SKIP88	STA	SIXIND0,X
		DEX
		PLA						; GET SCORE BYTE
		DEX
		AND	#$F
		ASL
		ASL
		ASL
		ADC	#(N0 & $FF)			; CARRY WAS ALWAYS CLEAR
		CMP	#(N0 & $FF)
		BNE	SKIP86
;
		LDA	T2
		BNE	SKIP85
SKIP86	BIT	FLAGS
		BVS	SKIP85
		LDY	#(N0 & $FF)
		STY	T2
SKIP85
		STA	SIXIND0,X
		DEX
		CPX	#4						; ALWAYS STOP ZPRESS
		BCS	SKIP84
		BIT	FLAGS					; DONT SSTOP ZPRESS IF VERSION SHOWN
		BVS	SKIP84
		LDY	#(N0 & $FF)
		STY	T2
SKIP84
		LDY	T0
		DEY
		DEY
		BPL	SIXILOOP
;
		STA	HSYNC
		LDY	#1
		LDX	PLAYNO				; POINT TO PLAYER UP
		LDA	SCORECLR,X
		AND	ATTRACT
		STA	P0CLR
		STA	P1CLR
		LDA	#0
		STA	P1REF
		STA	P0REF
		LDA	#3					; DO SCORE RESET
		STA	P1SIZE				; TRIPLE COPIES CLOSE
		STA	P0SIZE
		LDA	#$F0
		STA	P1RES
		STA	P0RES
		STA	P1HM
		STA	HSYNC
		STA	HMOV
		STY	P1VDEL
		STY	P0VDEL
;
; DO SIX CHAR TWICE
;
		LDY	#$07
		JSR	SIXCHAR
;
		LDX	PLAYNO
		LDY	LIVES,X
		LDX	#10					; POINT INTO INDIRECTION
		LDA	#(MAN & $FF)
LIFELOOP	DEY
		BPL	WSPACE
		LDA	#(SPACE & $FF)
WSPACE	STA	SIXIND0,X
NEXT04	DEX
		DEX
		BPL	LIFELOOP
		LDY	#4
		JSR	SIXCHAR

		STY	P1VDEL
		STY	P0VDEL


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; OVERSCAN
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
OVSCAN
	IF PAL
		LDA	#$37
	ELSE
		LDA	#$23
	ENDIF

		STA	TIMD64
		LDA	#2
		STA	VBLNK
;
; WRITE CASTLE COLR
;
		LDY	WAVENO
		LDA	COLORS,Y
		AND	ATTRACT
		STA	CAVESHAD
;
; COUNT DOWN SELECT TIMER
;
		LDA	SELTIME
		BEQ	SKIP44
		DEC	SELTIME
SKIP44
;
; SKIP CX IF CASTLE SCREEN
;
		BIT	GAMESTAT
		BMI	DRAGCX
		JSR	MOVERAT
		JMP	NOCX
;
; P0/P1 CX DETECT
;
DRAGCX	LDA	FRAME
		LSR
		BCC	NOCX
		LDX	#$FF
		BIT	PPMMCX
		BMI	SKIP20
		LDX	CX
		BMI	NOCX
SKIP20	INX
		STX	CX						; CX POINTS TO HIT P1
		LDA	CX
		BNE	SKIP21					; NOT BOTTOM DOOR
; COLLISION WITH BOTTOM DOOR
		LDA	PLAYERDX				; PLAYER MOVING RIGHT?
		BMI	NOCX					; NO
		BEQ	NOCX					;NO
HIDE	LDA	#0
		STA	PLAYERY					; HIDE PLAYER
		STA	PLAYERDY
		LDA	#$80
		STA	PLYRSTAT
		LDA	#(DOOR & $FF)			; CLOSE DOOR
		STA	P1ID
		BNE	NOCX					; LEAVE
; TOP DOOR CX
SKIP21
		LDA	P1ID,X
		CMP	#(ODOOR & $FF)			; OPEN DOOR?
		BNE	SKIP22					;NO
		LDA	#1
		JSR	SCORE
		LDX	#7						;START SCORE SOUND
		JSR	STARTSND
; END WAVE HERE
		LDA GAMNUM
		LSR
		BCC NEWWAVE					; ONE PLAYER VERSION
; 2 PLAYER
		LDA	PLAYNO
		EOR	#1
		TAX
		LDY	LIVES,X					;OTHER PLAYER DEAD?
		BMI	SKIP36
		TAX							; PLAYER NUM
		STA	PLAYNO
		BNE	OUT22
NEWWAVE
SKIP36	LDY	WAVENO
		INY
		CPY	#9
		BCS	SKIP37
		STY	WAVENO
SKIP37
OUT22
		JSR	SETUPCA
		BEQ	NOCX
SKIP22
;
; SCORING VALUES
;
		LDY	#$FF
		LDA	P1ID,X
SCLOOP	INY
		CMP	BRKS,Y
		BCS	SCLOOP
SCONE	LDA	#(SPACE & $FF)
		STA	P1ID,X
		LDX	SCORESND,Y			; START SCORING SOUND
		JSR	STARTSND
		LDA	SCORES,Y
		ASL
		JSR	SCORE
NOCX
OUT05	STA	CCLR

;
; TURN OFF FLAMING IF TIMED OUT
;
		LDY	DRAGTIME
		BEQ	STOPFL
		DEC	DRAGTIME
		BNE	OUT17
; TURN OFF FALAMING BIT OF DRAGSTAT
STOPFL	LDA	#0
		STA	DRAGSTAT
		LDY	#6
		STY	EYETOP
		DEY
		STY	EYEBOT
OUT17
;
; DELTA FIREBALLS
;
		LDA	FPCTL			; DO FIREBALL SPEED WITH WAVENO E DIFF
		ASL					; SHIFT DIFFICULTY TO CARRY
		LDX	PLAYNO
		BNE	SKIP34
		ASL
SKIP34
		LDY	WAVENO
		LDA	#0
		ADC	FBSPEEDS,Y
		STA	T0
		LDX	#NUP0S-1
DEFBLOOP	LDA	P0YS,X
		CMP	#DEAD
		BEQ	NEXT05
		ADC	T0				; FIREBALL SPEED

	IF	PAL
		CMP	#$95+15
	ELSE
		CMP	#$95
	ENDIF

		BCC OKXX
		LDA	#DEAD
OKXX
SKIP18	STA	P0YS,X
NEXT05	DEX
	BPL	DEFBLOOP
;
; GAME RESET AND GAME SELECT
;
		BIT GAMESTAT		; RESET GAME ON TRIGGER IF GAME OVER
		BVS	SKIP46
		BIT	TRIG0
		BPL	NWGAME
SKIP46
		LDA	FPCTL
		LSR
		BCC	NWGAME
		LSR
		BCS	CONTVB
;SELECT SWITCH PRESSED
		LDA	SELTIME
		BNE	GMCONT
		LDA	#$1F
		STA	SELTIME
		LDA	#0
		STA	PLAYNO
		STA	GAMESTAT
		LDA	#150
		STA	PLAYERX
		STA	PLAYERY
		LDA	#$C0			; SET D6-D7 OF FLAGS
		STA	FLAGS
		INC	GAMNUM
		LDA	GAMNUM
		AND	#7				; VERSIONS 0-7 ONLY
		STA	GAMNUM
		BPL	GMCONT
NWGAME	LDA	#$81			; GAME RESET BRINGS YOU HERE
		STA	FLAGS			; TURN OFF LOGO (SET D7)
;
; START NEW GAME
;
		LDX	#ZEND-ZSTART-1
		LDA	#0
ZEROLOOP	STA	ZSTART,X
		DEX
		BPL	ZEROLOOP
		LDA	#6
		STA	LIVES
		STA	LIVES+1	
		JSR	SETUPCA			; SET UP CASTLE SCREEN
		LDA	#$40			; YOU HERE
		STA	GAMESTAT		; TURN OFF SOFT ATTRACT
		LDA	GAMNUM
		AND	#$FE
		STA	WAVENO			; SKIP WAVES ACCORDING TO GAME NUMBER
GMCONT
		LDA	#$FF			; SELECT SWITCH BRINGS YOU HERE
		STA	ATTRACT

CONTVB						; NO SWITCHES BRINGS YOU HERE

;
; DRAGON INTELLIGENCE
;
; WRITE MAX DRAGON SPEED
		LDA WAVENO
		LSR
		TAX
		INX
		INX
		STX	T2				; MAX SPEED
		LDA	FRAME
		AND	#$3
		BNE	OUT12

		LDX	PLAYERX
		LDA	PLAYERY
		BNE	SKIP35
		LDX	#80				; IF PLAYER HIDDEN, USE CENTER
SKIP35	STX	T1

		LDA	DRAGONX
		LDX	DRAGONDX
		BMI	GOLEFT
		ADC	#$19
GOLEFT
;
; DRAGON IGNORANVE WINDOW
;
		LDY	WAVENO
		CMP	T1
		BCS	NORIGHT
; DRGON < PLAYERX
		STA	T0				; STORE DRAGON POSITION
		LDA	T1
		SEC
		SBC	T0				; GET DIFFERENCE
		CMP	MINDMAX,Y		; TABLE OF IGNORANCE WIDTHS
		BCC OUT12			; IN WINDOW, DO NOTHING
GORIGHT	INX
		BEQ	GORIGHT
		BNE	WRITEDX
; DRAGON > PlAYERX
NORIGHT	SEC
		SBC	T1
		CMP	MINDMAX,Y
		BCC	OUT12
NOZERO0	DEX
		BEQ	NOZERO0
WRITEDX	TXA
		BPL	WRMAX			; CHECK ABS VALUE AGAINST DRAGMAX
		EOR	#$FF
		ADC	#0				; CARRY ALWAYS SET
WRMAX	CMP	T2
		BCS	NOWRITE
		STX	DRAGONDX
NOWRITE
OUT12
;
; DELTA DRAGON
;
		LDA	DRAGONDX
		CLC
		ADC	DRAGONX
		CMP	#129
		BCS	WRAP			;IF 0>A>130 THEN IT WRAPPED
		STA	DRAGONX
		BCC	OUT01
;COMPLEMENT DELTA
WRAP	LDX	#1
		BIT	DRAGONDX
		BMI	STOP
		LDX	#$FF
STOP						; TURN ON HOMING ONLY IF D1 SET
OUT13	STX	DRAGONDX
OUT01
;
; WRITE LO INDIRECTION FOR DRAGON
		LDA	DRAGONAN		;DRAGON ANIMATION COUNTER
		ROR
		ROR
		ROR
		AND	#$C0			;PUT ANIM NUMBER INTO D6-D7
		LDX	#$10
		BIT	DRAGSTAT
		BVC	SKIP31
		LDA	#$C0			;FLAMING GRX
		LDY	DRAGONDX
		BVS	SKIP92
SKIP31
		LDY	DRAGONDX
		BNE	SKIP92
		LDA	#$80			;STILL GRX
		BMI	SKIP93
SKIP92	BPL	SKIP93
		CLC
		ADC	#$30
		LDX	#$F0
SKIP93	STX	T0
		LDX	#7				;POINT TO INDIRECTION
		LDY	#(DRAGON0 >> 8)
INDLOOP	STY	DRAGH0,X
		DEX
		STA	DRAGH0,X
		CLC
		ADC	T0
		DEX
		BPL	INDLOOP
;
; ANIMATE DRAGON
;
		LDA	FRAME
		AND	#3
		BNE	OUT02
		DEC	DRAGONAN
		BPL	SKIP91
		LDA	#2
		STA	DRAGONAN
SKIP91
OUT02
;
; ALTER SIGNED DELTAS ACCORDING TO STICK
;
		LDA	PLRCTL				;GET STICK
		LDX	PLAYNO				;TWO PLAYER?
		BEQ	SKIP90
		ASL
		ASL
		ASL
		ASL
SKIP90
		LDX	#0
		LDY	#0
		BIT GAMESTAT			;SOFT ATTRACT ON?
		BVC	OUT07				; GO IF SO
		PHA
		LDA	SND1
		CMP	#EXSND
		PLA
		BCS	OUT07
		PHA
		LDA	PLAYERID			;EXPLODING MAN
		CMP	#(SUPINE & $FF)
		PLA
		BCS	OUT07
;
; SEE IF PLAYER WAITING OUTSIDE
; LEFT STICK STARTS GAME
;
		BIT	GAMESTAT
		BPL	OUT08
		BIT	PLYRSTAT
		BPL	OUT08
;
; WAITING FOR INPUT
;
		AND	#$F0			;GET TOP NIBBLE
		CMP	#$B0			;LEFT STICK?
		BNE	OUT07			;NO STICK
;
;LEFT STICK
		LDA	#(ODOOR & $FF)
		STA	P1ID
		LDA	#16
		STA	PLAYERY
		LDA	#$88
		STA	PLAYERX
		LDA	#0				;RESET D7 OF PLYRSTAT
		STA	PLYRSTAT
;
OUT08
		ASL					;GET RIGHT
		BCS	NR
		INX					;MARK RIGHT
NR		ASL
		BCS	NL
		DEX					;MARK LEFT
NL		ASL
		BCS	ND
		DEY					;MARK DOWN
ND		ASL
		BCS	NU
		INY					;MARK UP
NU
OUT07
;
; ALTER DELTAS ACCORDING TO X AND Y
;
		BIT	GAMESTAT
		BMI	SKIP32
		LDA	P0IND
		CMP	#(MAN4 & $FF)	;KNEELING MAN?
		BEQ	OUT20
		LDA	PLAYERY
		BNE	OUT20			;DONT CHANGE DELTA IF IN AIR
SKIP32
		TXA
		PHP
		BEQ	SKIP28
		LSR	FRAME+1
SKIP28
		TYA
		PHP
		LDX	#1
		BIT	GAMESTAT
		BMI	SKIP96
		PLP				;DONT DO Y'S IF CASTLE
		DEX
SKIP96
ALTLOOP	LDA	PLAYERDX,X
		PLP					;GET SIGN OF DELTADELTA
		BNE	SKIP94			;GO IF STICK PRESSED
;DECAY DELTA
		ORA	#0				;DELTA ZERO ?
		BEQ	ABORT0			;DONT CHANGE IF SO
		BPL	NEGDELT			;POS, DECREASE
		BMI	POSDELT			;NEG, INCREASE
SKIP94	PHP
		CPX	#0				;DOING X?
		BNE	SKIP77
		STA	REFSHAD
SKIP77	PLP
		BMI	NEGDELT
; POS DELTA, ADD
POSDELT	CLC
		ADC	#$7F/4
		BCS	OK00			;GO TO POSITIVE IS OK
		BVS	ABORT0			;DONT OVERFLOW TO NEGATIVE
		BVC	OK00
;NEGATIVE DELTA
NEGDELT	SEC
		SBC	#$7F/4
		BCC	OK00			;WRAP TO NEGATIVE OK
		BVS	ABORT0
OK00	STA	PLAYERDX,X
ABORT0	DEX
		BPL	ALTLOOP
OUT20
;
; APPLY DELTAS TO 16 BIT POSITION
; X=0 DELTA X, X=1 DELTAY
;
		LDX	#1
DELTLOOP	LDY	#0			;SPEED FOR CASTLE
		BIT	GAMESTAT		;WHICH SCREEEN?
		BPL	SKIP33
		INY		
		LDA	WAVENO
		CMP	#4
		BCC	SKIP33
		INY
SKIP33
		LDA	PLAYERDX,X		;LOAD DELTA
		ASL
		BCS	SUBDELTA
;ADD DELTA
ADDDELT	CLC
		PHA
		ADC	PLAYERLX,X
		STA	PLAYERLX,X
		BCC	NOCARRY0
		LDA	PLAYERX,X
		ADC	#0
		CMP	MAXLIMS,X		;CHECK AGAINST MAX VALUE
		BCS	NOCARRY0
		STA	PLAYERX,X
NOCARRY0	PLA
		DEY
		BPL	ADDDELT
		BMI	CONT01
;SUBTRACT DELTA
SUBDELTA	ROR				;RESTORE SIGN
		CMP	#$80			;FUCKING SPECIAL CASE
		BNE	SKIP97
		LDA	#$81
SKIP97
		EOR	#$FF			;COMPLEMENT
		CLC
		ADC	#1
		ASL					;*2
		STA	T0
SUBLOOP	LDA	PLAYERLX,X
		SEC
		SBC	T0
		STA	PLAYERLX,X
		BCS	CARRY0
		LDA	PLAYERX,X
		SBC	#0
		BCC	CARRY0			;BOTTOM LIMIT
		CMP	MINLIMS,X
		BCC	CARRY0
		STA	PLAYERX,X
CARRY0
		DEY
		BPL	SUBLOOP

CONT01	DEX
		BPL	DELTLOOP
;
; SET UP PLAYER 0
;
NEWBOARD	LDX	NEWCNT
		BEQ	OUT09
		LDA	RAND
		AND	#$7
		CLC
		ADC	WAVENO
		TAY
		LDA	TREATABL,Y
		STA	P1ID,X
;DRIVE XPOS
		LDA	RANDH
		AND	#$F
		TAY
		LDA	RANDH
		CPY	#$A					;RIGHT MAX
		BCC	INRANGE
		AND	#$F7				;ZERO D3
		ADC	#1
INRANGE	AND	#$EF				;DISALLOW ODD HMOVE VALUES
		STA	P1HMS,X
		DEC	NEWCNT
;SET UP LOWER DOOR
DODOORS	LDA	#(DOOR & $FF)
		STA	P1ID
		LDA	#$88
		STA	P1HMS
;
OUT09
;
		JSR	SOUND
;
; SET UP SEA COLOR
;
		LDA FRAME
		AND	#$7
		BNE	OUT100
		LDA	RAND
		AND	#$7

	IF PAL
		ORA	#$B0
	ELSE
		ORA	#$80
	ENDIF

		STA	SEA
OUT100
;
; ANIMATE FIREBALLS
;
		BIT	GAMESTAT
		BPL	OUT15
		LDA	FRAME
		AND	#$3				;FASTEST RATE
		BNE	OUT15
		LDX	#NUP0S-1
ALOOP1	LDA	P0YS,X
		CMP	#DEAD
		BEQ	NEXT01
		LDA	P0ID,X
;MOVING FIREBALL
		EOR	#$10
SKIP00	STA	P0ID,X
NEXT01	DEX
		BPL	ALOOP1
OUT14
;
; ANIMATE TREASURES
;
		LDX	#NUP1S-1
ALOOP0	LDA	P1ID,X
		CMP	#(TREAS & $FF)
		BCS	NEXT02
;ANIMATED TREASURES
ATR		LDY	#6
ALOOP2	DEY
		CMP	ATABLE,Y
		BNE	ALOOP2
		TYA
		EOR	#1
		TAY
		LDA	ATABLE,Y
		STA	P1ID,X
NEXT02	DEX
		BPL	ALOOP0
OUT15
;
OVOUT	BIT	TIMOUT				;WAIT FOR OVERSCAN TIMEOUT
		BPL	OVOUT
		JMP	SCRNTOP				; NEW FRAME !

;
;
; SUBROUTINES
;
; SETUP DRAGON SCREEN
;
SETUPDR	LDA	#$C0
		STA	GAMESTAT
		LDA	#0
		STA	BKCLR
		STA	PLAYERDY
		STA	PLAYERY
		STA	DRAGONX
		LDA	#1
		STA	DRAGONDX
		LDA	#$80
		STA	PLYRSTAT
		RTS
;
; START SOUND, HIGHTEST NUBER GET PRIOITIY
; X HAS SOUND NUMBER
; Z FLAG RESET ON RETURN
STARTSND
		LDA	SNDNOS,X
		CMP	SND1
		BCC	OUT25
		STA	SND1
		LDA	SNDTIMS,X
		STA	SND1TIME
OUT25	RTS
;
; SCORING VALUES
;
SCORES	DC		$82
		DC		$81
		DC		$30
		DC		$08

; TOP OF DRAGONS CAVE GRAPHICS
CAVE0	DC	$00,$60,$FF,$FF
CAVE1	DC	$00,$DB,$FF,$FF
CAVE2	DC	$00,$B6,$FF,$FF
;
; CASTLE COLORS
;
	IF PAL
COLORS	DC	$0A,$7A,$2A,$3A,$7A,$BA,$5A,$AA,$8A
	ELSE
COLORS	DC	$0A,$BA,$1A,$DA,$BA,$7A,$CA,$6A,$5A
	ENDIF

;FIREBALL SPEEDS
FBSPEEDS	DC	2,2,2,2,2,3,3,3,4


;SKIP TO PAGE BOUNDRY
	ORG	$1B00

; DRAGON 3
DRAGON8
	DC	$2E, $6C, $F9, $FF, $BF, $3F, $7F, $FF
	DC	$F7, $E0, $E0, $60, $30, $19, $1F, $0E
DRAGON9
	DC	$00, $F0, $FC, $FF, $FF, $FF, $FF, $8F
	DC	$07, $03, $81, $4F, $C3, $9E, $02, $3E
DRAGON10
	DC	$70, $61, $F9, $F1, $FF, $FF, $FC, $F8
	DC	$F1, $E0, $80, $00, $00, $02, $00, $10
DRAGON11
	DC	$00, $20, $90, $F8, $FF, $EF, $F8, $70
	DC	$E0, $00, $00, $00, $40, $00, $00, $00

; DRAGON 2
DRAGON4
	DC	$38, $70, $F8, $7F, $3F, $3F, $FF, $FF
	DC	$F3, $E0, $60, $30, $11, $1F, $0E, $00
DRAGON5
	DC	$00, $78, $FE, $FF, $FF, $FF, $FF, $CF
	DC	$87, $C3, $47, $C1, $8F, $02, $1E, $00
DRAGON6
	DC	$EC, $D9, $F9, $F1, $FF, $FF, $FC, $F8
	DC	$F1, $E0, $80, $00, $00, $00, $00, $00
DRAGON7
	DC	$00, $20, $90, $F8, $FF, $EF, $F9, $70
	DC	$E0, $00, $04, $08, $10, $40, $00, $00

; DRAGON 1
DRAGON0
	DC	$7E, $30, $71, $7B, $7F, $3F, $7F, $FF
	DC	$F7, $E0, $E0, $60, $30, $19, $1F, $0E
DRAGON1
	DC	$00, $F0, $FC, $FF, $FF, $FF, $FF, $8F
	DC	$07, $03, $87, $41, $CF, $82, $1E, $21
DRAGON2
	DC	$38, $61, $E1, $F1, $FF, $FF, $F8, $F0
	DC	$E1, $C0, $80, $00, $00, $00, $00, $00
DRAGON3
	DC	$00, $20, $90, $F8, $FF, $EF, $F9, $70
	DC	$E1, $02, $04, $04, $08, $00, $00, $00
;FLAMING DRAGON
DRAGON12
	DC	$7E, $30, $71, $7B, $7F, $3F, $7F, $FF
	DC	$F7, $E0, $E0, $60, $30, $19, $1F, $0E
DRAGON13
	DC	$00, $F0, $FC, $FF, $FF, $FF, $FF, $8F
	DC	$07, $03, $87, $41, $CF, $82, $1E, $21
DRAGON14
	DC	$70, $6E, $F8, $FC, $FF, $FF, $FB, $F1
	DC	$EB, $CB, $8F, $06, $03, $03, $07, $0E
DRAGON15
	DC	$00, $00, $00, $00, $00, $80, $C0, $C0
	DC	$F0, $E0, $E8, $F0, $E0, $30, $10, $18



MAN0
	DC	$00, $C2, $42, $4E, $58, $50, $70, $30
	DC	$30, $70, $78, $3C, $38, $60, $60, $60
MAN1
	DC	$00, $60, $20, $24, $3C, $38, $30, $30
	DC	$30, $30, $70, $38, $30, $60, $60, $60
MAN2
	DC	$00, $18, $28, $18, $38, $70, $70, $30
	DC	$30, $30, $F4, $B4, $38, $60, $60, $60
MAN4
	DC	$00, $7C, $60, $3C, $0E, $0E, $7E, $04
	DC	$0C, $0C, $00, $00, $00, $00, $00, $00
FB0
	DC	$00, $10, $5A, $1A, $48, $42, $1A, $3C
	DC	$6E, $FE, $3F, $6E, $3A, $3C, $18, $00
FB1
	DC	$52, $08, $40, $12, $02, $48, $00, $98
	DC	$3D, $7A, $5E, $FE, $74, $19, $24, $08

SUPINE
	DC	$00, $00, $00, $00, $C2, $4E, $48, $7C
	DC	$3E, $06, $06, $07, $3F, $03, $06, $06
SUPINE1
	DC	$00, $00, $00, $30, $31, $99, $FF, $38
	DC	$38, $78, $4C, $4F, $61, $20, $00, $00
SUPINE2
	DC	$00, $00, $20, $3B, $0A, $0A, $0E, $9C
	DC	$B8, $F0, $FC, $E4, $C4, $C0, $00, $00
HFB0
	DC	$00, $00, $04, $AA, $1F, $4A, $14, $00
	DC	$00, $00, $00, $00, $00, $00, $00
HFB1
	DC	$00
	DC	$00, $08, $5E, $07, $2F, $8A, $00, $00
	DC	$00, $00, $00, $00, $00, $00
	
;
; SIX CHAR ROUTINE
;
SIXCHAR
SCKRNL	STY	T1					;MC 64
		LDA	(SIXIND5),Y			;MC 69
		STA	P1GR				;MC 72
		STA	HSYNC				;MC 75 (76)
		LDA	(SIXIND4),Y			;MC 5
		STA	P0GR				;MC 8
		LDA	(SIXIND3),Y			;MC 13
		STA	P1GR				;MC 16
		LDA	(SIXIND2),Y			;MC 21
		STA	T2					;MC 24
		LDA	(SIXIND1),Y			;MC 29
		TAX						;MC 31
		LDA	(SIXIND0),Y			;MC 36
		TAY						;MC 38
		LDA	T2					;MC 41
		STA	P0GR				;MC 44
		STX	P1GR				;MC 47
		STY	P0GR				;MC 50
		STY	P1GR				;MC 53
		LDY	T1					;MC 56
		DEY						;MC 58
		BPL	SCKRNL				;MC 60/61
		INY						; MAKE Y=0
		STY	P1GR
		STY	P0GR
		STY	P1GR
		STY	P0GR
WASTE12	RTS
;
;SCORE
; A HAS BYTE CARRY CLEAR=ADD TO LO, CARRY SET=ADD TO MID SCORE
;
SCORE	SED
		LDX	PLAYNO				;PLAYER UP
		BCC	SKIP78				;ADD TO LO BYTE
		INX						;POINT TO MID BYTE
		INX
SKIP78	CLC
SCORLOOP	ADC	SCOREL0,X
		STA	SCOREL0,X
		BCC	OK01
		INX
		INX
		LDA	#1
		CPX	#6
		BCC	SCORLOOP
OK01	CLD
		RTS

;
; TREASURE SCORING DIVISIONS
	
BRKS	DC	(BRK1 & $FF),(BRK2 & $FF),(BRK3 & $FF),$FF		


;PAD TO NEXT PAGE BOUNDRY
	DC	$00, $00, $00

ATREAS
DIAMD0
	DC	$00, $10, $28, $28, $54, $54, $BA, $54
	DC	$28, $10
DIAMD1
	DC	$00, $10, $28, $28, $54, $54
	DC	$BA, $54, $28, $10
CROWN0
	DC	$00, $7E, $7E, $5A
	DC	$5A, $00, $81, $00, $24, $00
CROWN1
	DC	$00, $7E
	DC	$7E, $5A, $5A, $00, $00, $42, $18, $00
BRK1
HARPL0
	DC	$00, $7E, $95, $95, $95, $95, $56, $56
	DC	$42, $81
HARPL1
	DC	$00, $7E, $A9, $A9, $A9, $A9
	DC	$6A, $6A, $42, $81
TREAS
CHEST	
	DC	$00, $7E, $7E, $7E
	DC	$66, $FF, $54, $28, $00, $00
BRK2
HELMET	
	DC	$00, $42
	DC	$66, $66, $42, $E7, $BD, $BD, $99, $42
CHEST1
	DC	$00, $7C, $38, $10, $38, $10, $38, $6C
	DC	$AA, $AA
BRK3
LAMP	
	DC	$00, $3C, $18, $FE, $BF, $AD
	DC	$E1, $00, $00, $00
URN
	DC	$00, $7C, $38, $10
	DC	$38, $10, $38, $7C, $FE, $FE
JUG	
	DC	$00, $3C
	DC	$7E, $FF, $BF, $DF, $7E, $3C, $3C, $7E
DOOR
	DC	$00, $FF, $FE, $FF, $DF, $FF, $FE, $FF
	DC	$FF, $7E
ODOOR
	DC	$00, $8F, $8E, $8F, $8B, $8F
	DC	$8E, $8F, $C7, $7E
SPACE
SPACE10
	DC	$00, $00, $00, $00
	DC	$00, $00, $00, $00, $00, $00
NUMBERS
N0	
	DC	$38, $6C
	DC	$C6, $86, $86, $C6, $6C, $38
N1	
	DC	$08, $1C
	DC	$18, $18, $18, $18, $38, $10
N2	
	DC	$FC, $FE
	DC	$62, $30, $18, $8C, $CC, $78
N3	
	DC	$3C, $6E
	DC	$66, $C6, $1C, $0E, $66, $3C
N4	
	DC	$08, $1C
	DC	$0C, $FE, $CC, $6C, $3C, $1C
N5	
	DC	$FC, $8E
	DC	$0E, $EE, $BC, $80, $FC, $FC
N6	
	DC	$7C, $C6
	DC	$C6, $FC, $C0, $C0, $FC, $78
N7	
	DC	$E0, $E0
	DC	$70, $30, $18, $8C, $FE, $7E
N8	
	DC	$3C, $66
	DC	$CE, $7C, $64, $C6, $7C, $38
N9
	DC	$78, $CC
	DC	$0E, $7E, $CE, $CE, $CC, $78
MAN	
	DC	$00, $A0
	DC	$40, $E0, $40
	
DELAYS
	DC	W8 & $FF
	DC	W8 & $FF
	DC	W8 & $FF
	DC	W7 & $FF
	DC	W7 & $FF
	DC	W7 & $FF
	DC	W6 & $FF
	DC	W6 & $FF
	DC	W11 & $FF
	DC	W11 & $FF
	DC	W10 & $FF
	DC	W10 & $FF
	DC	W10 & $FF
	DC	W9 & $FF
	DC	W9 & $FF
	DC	W9 & $FF


;PAD TO NEXT PAGE BOUNDRY
	DC	$00, $00, $00, $00, $00

;
; COLORS FOR 16 LINE GRAPHICS
MAN0C
	DC	$00, $C8, $C8, $C8, $C8, $C8, $44, $00
	DC	$44, $44, $44, $44, $44, $18, $18, $12
MAN1C
	DC	$00, $C8, $C8, $C8, $C8, $C8, $44, $00
	DC	$44, $44, $44, $44, $44, $18, $18, $12
MAN2C
	DC	$00, $C8, $C8, $C8, $C8, $C8, $44, $00
	DC	$44, $44, $44, $44, $44, $18, $18, $12
MAN4C
	DC	$00, $C8, $C8, $C8, $44, $44, $44, $44
	DC	$18, $18, $00, $00
SCORESND 
	DC	$05, $06, $07, $08
FB0C
	DC	$00, $44, $44, $F6, $44, $44, $44, $44
	DC	$44, $44, $44, $44, $44, $44, $44, $42
FB1C
	DC	$00, $44, $44, $F6, $44, $44, $44, $44
	DC	$44, $44, $44, $44, $44, $44, $44, $42
SUPINEC
	DC	$00, $C8, $C8, $C8, $C8, $C8, $C8, $C8
	DC	$44, $00, $44, $44, $44, $44, $18, $12
SUPINE1C
	DC	$00, $12, $18, $18, $18, $44, $44, $44
	DC	$44, $C8, $C8, $C8, $C8, $18, $18, $12
SUPINE2C
	DC	$00, $C8, $C8, $C8, $C8, $C8, $44, $00
	DC	$44, $44, $44, $44, $18, $18, $18, $12
SHIELD
	DC	$00, $38, $44, $6C, $D6, $FE, $C6, $82
	DC	$82, $00

;
; RUNTIME SOUND	
;
SOUND	
		LDA	GAMESTAT
		BEQ	TURNOFF			; DISALLOW SOUND DURING ATTRACT
		LDA	SND1TIME
		BNE	SKIP43
		STA	SND1			; TUN OFF SOUND ID
		BEQ	TURNOFF
SKIP43	DEC	SND1TIME
; A HAS SOUND COUNTDOWN TIMER
		LDX	SND1
		LDY	CTLS,X
		STY	SNDC1
		LDY	FREQS,X
		DEX
		BPL	NOT0
;FIRE SOUND
; TIMER/8 INTO VOLUME
		LSR	
		LSR	
		LSR	
		JMP	TURNOFF
NOT0	DEX
;BONUS SOUND
		BMI	TURNOFF
NOT1	DEX
;JUMP SOUND
		BMI	TURNOFF
NOT2	DEX
		BPL	NOT3
;EXPLOSION
; RANDOM D4 INTO FREQ, 8 INTO VOL
		LDA	RAND
		ORA	#$10
		TAY
		LDA	#$8
		BNE	TURNOFF
NOT3
;DEATH SOUND
; 32-TIMER/2 INTO FREQ, TIMER/4 INTO VOL
		LSR
		EOR	#$1F
		TAY
		EOR	#$1F
		LSR
		STA	CAVESHAD
TURNOFF	STY	SNDF1
		STA	SNDV1
OUT24
;
; RUNNING SOUND
;
		LDY	#0
		LDA	PLAYERDX
		ORA	PLAYERDY
		BEQ	OUT42
		LDA	FRAME
		AND	#7
		BNE	OUT42
;HERE IF MOVING & EVERY EIGHTH FRAME
		BIT	GAMESTAT
		BVC	OUT42			;SOFT ATTRACT ON
		BMI	SKIP42
		LDA	PLAYERY
		BNE	OUT43
SKIP42
		;SET FREQ & VOLUME OF STEP
		LDA	#$1C
		STA	SNDF0
		LDY	#$A
OUT42	STY	SNDV0
		STY	SNDC0
OUT43
		RTS


;PAD TO PAGE BOUNDRY
	DC	$00, $00, $00

;
; TREASURE COLORS
;
TREASCLR

DIAMD0C
	DC	$00, $FF, $FF, $FF, $FF, $88, $88, $88
	DC	$FF, $FF
DIAMD1C
	DC	$00, $FA, $FA, $FA, $FA, $88
	DC	$FF, $88, $FA, $FA 
CROWN0C	
	DC	$00, $28, $44, $28
	DC	$28, $28, $28, $00, $78, $00
CROWN1C	
	DC	$00, $28
	DC	$44, $28, $28, $28, $28, $00, $78, $00
HARPL0C
	DC	$00, $D8, $88, $88, $88, $88, $88, $88
	DC	$88, $FF
HARPL1C
	DC	$00, $D8, $88, $88, $88, $88
	DC	$88, $88, $88, $FF
CHESTC	
	DC	$00, $14, $14, $14
	DC	$14, $12, $FC, $FC, $00, $00
HELMETC	
	DC	$00, $88
	DC	$88, $88, $18, $18, $18, $18, $18, $18
CHEST1C
	DC	$00, $16, $44, $16, $44, $16, $16, $16
	DC	$FF, $FF
LAMPC	
	DC	$00, $88, $14, $14, $14, $14
	DC	$14, $00, $00, $00
URNC	
	DC	$44, $44, $14, $44
	DC	$14, $44, $44, $44, $44, $14
JUGC	
	DC	$00, $18
	DC	$88, $18, $18, $18, $18, $88, $88, $18
DOORC
	DC	$00, $12, $12, $16, $12, $12, $12, $16
	DC	$12, $12
ODOORC	
	DC	$00, $12, $12, $16, $12, $12
	DC	$12, $16, $12, $12
MAXLIMS	
	DC	$94, $87
MINLIMS
	DC	$01, $10
SCORECLR
	DC	$00, $44
LINES	
	DC	$00, $10, $1F, $2E, $3D, $4C
	DC	$5B, $6A, $79, $88
OFFSETS	
	DC	$1C, $05, $19, $07


DRAGCLR
	DC	$D6, $18, $86, $64, $46, $18, $F6, $02
	DC	$0F
MINDMAX	
	DC	$50, $40, $30, $28, $20, $18, $18
	DC	$18, $18, $20
ATABLE	
	DC	$28, $32, $14, $1E, $00
	DC	$0A
CASTLE0
	DC	$70, $70, $F0, $D0, $D0, $D0, $F0
	DC	$F0, $50, $00, $00
CASTLE1	
	DC	$00, $00, $00, $00
	DC	$00, $00, $80, $80, $80, $00, $00
CTLS	
	DC	$08
	DC	$05, $01, $04, $0C
FREQS	
	DC	$1A, $0A, $15, $0A
	DC	$10
;
; SOUND TO USE
;
SNDNOS	
	DC	FIRES, BONSND, JUMPS, EXSND, DEATHS, BONSND, BONSND, BONSND, BONSND
;
; DURATION OF SOUNDS
SNDTIMS
	DC	$1F, $1F, $0F, $0F, $3F, $3F, $2F, $1F, $0F
; TREASURES TABLE
; USED FOR WEIGHTED RAMDOM SELECTION
TREATABL
	DC	URN & $FF
	DC	LAMP & $FF
	DC	JUG & $FF
	DC	CHEST1 & $FF
	DC	JUG & $FF
	DC	HELMET & $FF
	DC	CHEST1 & $FF
	DC	URN & $FF
	DC	LAMP & $FF
	DC	CHEST & $FF
	DC	HELMET & $FF
	DC	HARPL0 & $FF
	DC	LAMP & $FF
	DC	DIAMD1 & $FF
	DC	CROWN0 & $FF
	DC	JUG & $FF

	DC $F7		; TO MATCH RELEASE EPROM
VECTORS
	DC.W	START
	DC.W	START

END
