*         1015    180684
*
*
** TUNES.S **
** ASTEROIDS FOR THE ATARI 3600 **
** THIS FILE CONTAINS THE SOUND DRIVER. **

	.ORG     CODE+$2000
***** TUNE DATA *****

* TUNTAB: THE ACTUAL VALUE INFO.  THIS TABLE SHOULD BEGIN ON A PAGE BOUNADARY
TUNTAB:

* CONSTANT VALUES (RE-USED CONSTANTS WITH THE 'PLAY FOREVER' BIT SET)
TCONST00:
	.DC.B	     $40
TCONST01:
	.DC.B	     $41
TCONST02:
	.DC.B	     $42
TCONST04:
	.DC.B	     $44
TCONST06:
	.DC.B	     $46
TCONST08:
	.DC.B	     $48
TCNTDOWN:
	.DC.B	     $0A,$09,$08,$07,$06,$05,$04,$03,$02,$01,$FF

				;00 - VICTOR REZ OUT
TVICTORC:
	.DC.B	     $4D
TVICTORF:
	.DC.B	     $03,$02,$01,$02,$03,$20
TVICTORV:
	.DC.B	     $98,$FE,(TS1V+1)&255
				;01 - HYPERSPACE
THYPERC:
	.DC.B	     $0D,$24
THYPERF:
	.DC.B	    $1F,$1D,$1B,$1A,$18,$17,$15,$14,$13,$12,$11,$10,$0F,$0E,$0D,$2C
THYPERV:
	.DC.B	     $86,$0A,$85,$08,$06,$90,$04,$02,$FF
				;02 - TWINKLE
TTWINKC	EQU	TCONST04
TTWINKF:
	.DC.B	    $06,$04,$01,$05,$02,$03,$01,$06,$04,$05,$02,$03,$04,$01,$03,$25
TTWINKV:
	.DC.B	     $45
				;03 - BUZZ
TBUZZC	EQU	TCONST08
TBUZZF	EQU	TCONST00
TBUZZV:
	.DC.B	     $01,$01,$02,$03,$04,$06,$08,$08,$0C,$0F,$0F,$FF
				;04 - XTRA
TXTRAC	EQU	TCONST04
TXTRAF:
	.DC.B	     $86,$1F,$18,$1F,$95,$14,$FF
TXTRAV:
	.DC.B	     $08,$00,$08,$00,$08,$00,$FE,(TCNTDOWN+1)&255
				;05 - SHIPE
TSHIPEC	EQU	TCONST08
TSHIPEF:
	.DC.B	     $5F
TSHIPEV	EQU	TCNTDOWN
				;06 - UFOSE
TUFOSEC   EQU     TCONST08
TUFOSEF:
	.DC.B	     $88,$03,$49
TUFOSEV:
	.DC.B	     $09,$07,$09,$07,$FE,(TCNTDOWN+5)&255
				;07 - UFOLE
TUFOLEC	EQU	TCONST08
TUFOLEF:
	.DC.B	     $8A,$09,$43
TUFOLEV	EQU	TUFOSEV
				;08 - UFOS
TUFOSC:
	.DC.B	$07,$23
TUFOSF	EQU	TCONST00
TUFOSV	EQU	TCONST04
				;09 - UFOL
TUFOLC:
	.DC.B	$0F,$03,$07,$23
TUFOLF	EQU	TCONST02
TUFOLV	EQU	TCONST04
				;0A - ROCKSE
TROCKSEC	EQU	TCONST08
TROCKSEF:
	.DC.B	$4B
TROCKSEV	EQU	(TCNTDOWN+2) & 255
				;0B - ROCKME
TROCKMEC	EQU	TCONST08
TROCKMEF:
	.DC.B	$51
TROCKMEV	EQU	(TCNTDOWN+2) & 255
				;0C - ROCKLE
TROCKLEC	EQU	TCONST08
TROCKLEF:
	.DC.B	$5A
TROCKLEV	EQU	(TCNTDOWN+1) & 255
				;0D - USHOT
TUSHOTC	EQU	TCONST04
TUSHOTF:
	.DC.B	$03,$0F,$12,$13,$13,$14,$14,$15,$16,$16,$17,$FF
TUSHOTV:
	.DC.B	$03,$05,$07,$84,$06,$05,$04,$82,$03,$02,$01
				;0E - SHOT
TSHOTC	EQU	TCONST04
TSHOTF:
	.DC.B	$0C,$0D,$0E,$0F,$10,$11,$12,$13,$14,$15,$16
	.DC.B	$17,$18,$19,$1A,$1B,$1C,$1D,$1E,$1F,$FF
TSHOTV:
	.DC.B	     $82,$FE,(TCNTDOWN+1) & 255
				;0F - BEATL
TBEATLC	EQU	TCONST06
TBEATLF:
	.DC.B	$53
TBEATLV:
	.DC.B	$09,$FF
				;10 - BEATH
TBEATHC	EQU	TCONST06
TBEATHF:
	.DC.B	     $54
TBEATHV	EQU	TBEATLV
				;11 - THRUST
TTHRUSTC	EQU	TCONST06
TTHRUSTF:
	.DC.B	$10,$0E,$0D,$0E,$0C,$0E,$0F,$2E
TTHRUSTV	EQU	TCONST04
				;12 - S1
TS1C	EQU	TCONST04
TS1F:
	.DC.B	     $10,$13,$0C,$08,$05,$03,$02,$21
TS1V:
	.DC.B	     $A0,$07,$05,$03,$01,$FF
				;13 - S2
TS2C	EQU	TCONST04
TS2F:
	.DC.B	     $07,$21
TS2V:
	.DC.B	     $01,$02,$03,$04,$05,$06,$07,$08,$FE,(TCNTDOWN+2)&255
				;14 - S3
TS3C	EQU	TCONST04
TS3F:
	.DC.B	     $09,$05,$07,$2A
TS3V:
	.DC.B	     $8C,$07,$00,$05,$00,$03,$00,$01,$FF
				;15 - S4
TS4C	EQU	TCONST04
TS4F:
	.DC.B	     $1A,$12,$2D
TS4V:
	.DC.B	     $9B,$FE,(TS1V+1)&255
				;16 - S5
TS5C	EQU	TCONST01
TS5F	EQU	TCONST00
TS5V:
	.DC.B	$04,$00,$04,$00,$04,$00,$8A,$00,$04,$00,$04
	.DC.B	$00,$04,$00,$85,$04,$00,$04,$00,$04,$FF
				;17 - S6
TS6C	EQU	TCONST01
TS6F:
	.DC.B	     $04,$06,$26
TS6V:
	.DC.B	     $92,$01,$02,$03,$FE,(TCNTDOWN+6)&255
*
* TIINDEX: TABLE OF INITIAL INDICES AND DURATIONS
TIINDEX:
	.DC.B	TVICTORC&255,TVICTORF&255,TVICTORV&255,3 ;SOUND 0 - VICTOR REZOUT
	.DC.B	THYPERC&255,THYPERF&255,THYPERV&255,1	;SOUND 1 - HYPERSPACE
	.DC.B	TTWINKC&255,TTWINKF&255,TTWINKV&255,2	;SOUND 2 - TWINKLE
	.DC.B	TBUZZC&255,TBUZZF&255,TBUZZV&255,3	;SOUND 3 - BUZZ
	.DC.B	TXTRAC&255,TXTRAF&255,TXTRAV&255,3	;SOUND 4 - XTRA MAN
	.DC.B	TSHIPEC&255,TSHIPEF&255,TSHIPEV&255,9	;SOUND 5 - SHIP EXP
	.DC.B	TUFOSEC&255,TUFOSEF&255,TUFOSEV&255,3	;SOUND 6 - MR. BILL EXP
	.DC.B	TUFOLEC&255,TUFOLEF&255,TUFOLEV&255,4	;SOUND 7 - SLUGGO EXP
	.DC.B	TUFOSC&255,TUFOSF&255,TUFOSV&255,3	;SOUND 8 - MR. BILL
	.DC.B	TUFOLC&255,TUFOLF&255,TUFOLV&255,5	;SOUND 9 - SLUGGO
	.DC.B	TROCKSEC&255,TROCKSEF&255,TROCKSEV&255,4 ;SOUND A - SMALL ROCK EX
	.DC.B	TROCKMEC&255,TROCKMEF&255,TROCKMEV&255,6 ;SOUND B - MED. ROCK EX
	.DC.B	TROCKLEC&255,TROCKLEF&255,TROCKLEV&255,8 ;SOUND C - LARGE ROCK EX
	.DC.B	TUSHOTC&255,TUSHOTF&255,TUSHOTV&255,2	;SOUND D - UFO SHOT
	.DC.B	TSHOTC&255,TSHOTF&255,TSHOTV&255,1	;SOUND E - HERO SHOT
	.DC.B	TBEATLC&255,TBEATLF&255,TBEATLV&255,6	;SOUND F - LO HEART BEAT
	.DC.B	TBEATHC&255,TBEATHF&255,TBEATHV&255,6	;SOUND 10 - HI HEART BEA
	.DC.B	TTHRUSTC&255,TTHRUSTF&255,TTHRUSTV&255,2 ;SOUND 11 - SHIP THRUST
	.DC.B	TS1C&255,TS1F&255,TS1V&255,4		;SOUND 12 - SPACE SOUND1
	.DC.B	TS2C&255,TS2F&255,TS2V&255,8		;SOUND 13 - SPACE SOUND2
	.DC.B	TS3C&255,TS3F&255,TS3V&255,3		;SOUND 14 - SPACE SOUND3
	.DC.B	TS4C&255,TS4F&255,TS4V&255,9		;SOUND 15 - SPACE SOUND4
	.DC.B	TS5C&255,TS5F&255,TS5V&255,5		;SOUND 16 - SPACE SOUND5
	.DC.B	TS6C&255,TS6F&255,TS6V&255,6		;SOUND 17 - SPACE SOUND6

*
***** ACTUAL ROUTINES FOR TUNE DRIVER *****
* DOTUNE2:        PLAY TUNE IN AC ONLY IF IT IS NOT ALREADY PLAYING
DOTUNE2:
	LDX     #1		;SEE IF THIS TUNE IS ALREADY PLAYING
TFINDLOP:
	CMP     TUNNUM,X
	BEQ     TALREADY	;ALREADY PLAYING, DON'T PLAY AGAIN
	DEX
	BPL     TFINDLOP
	JSR     DOTUNE	;NOT YET PLAYING, START UP HIS THEME
TALREADY:
	RTS


* DOTUNE1:        PLAY TUNE IN AC, KILLING THE TUNE IF IT IS ALREADY PLAYING
DOTUNE1:
	PHA
	JSR     KILLTUNE
	PLA
;	JMP     DOTUNE		;FALL THROUGH ...

* DOTUNE:         PLAY TUNE IN AC IF POSSIBLE
DOTUNE:
	LDX     #1		;FIND A FREE CHANNEL
TUNFRELP:
	LDY     TUNNUM,X
	BMI     TUNIN		;FF = FREE
	DEX
	BPL     TUNFRELP

	;NONE FREE
	CMP     #$10		;LOW PRI. SOUNDS NEVER BUMP ANYTHING
	BCS     TUNRTS
	LDX     #1		;FIND A LOWER PRIORITY CHANNEL
TUNPRILP:
	CMP     TUNNUM,X
	BCC     TUNIN		;FOUND LOWER PRIORITY: BUMP IT
	DEX
	BPL     TUNPRILP

	;SORRY CHARLIE...
TUNRTS:
	RTS

* TUNIN:  INSTALL THE TUNE IN AC INTO CHANNEL IN X
TUNIN:
	STA     TUNNUM,X
	ASL			;FIND ROW IN INITIALIZATION TABLE
	ASL			;(THERE ARE FOUR ENTRIES PER ROW)
	TAY
TUNINLOP:
	LDA     TIINDEX,Y	;COPY IN TUNE INITIAL INDICES
	STA     TINDEX,X
	INY
	INX
	INX			;SKIP OVER THE OTHER CHANNEL
	CPX     #6		;2 CHANNELS * 3 ELEMENTS PER CHANNEL
	BCC     TUNINLOP
	LDA     TIINDEX,Y	;COPY IN DURATION
TDURINLP:
	DEX
	DEX
	BMI     TUNRTS
	STA     TDURTAB,X
	STA     TDURCNT,X
	BPL     TDURINLP	;BPL = JMP

* CLEARTUN:       KILL ALL TUNES, WHEREVER THEY LIE
CLEARTUN:
	LDX     #1
CLRLOOP:
	JSR     TOVER
	DEX
	BPL     CLRLOOP	;FALL THRU INIT HUMP

INITHUMP:
	LDA     #$20	;INITIALIZE BACKGROUND HEATBEAT COUNTER
	STA     BEATVAL
;	STA     BCOUNTER	;LET IT RIDE...
	LDA     #$90
	STA     BEATRATE
	RTS

* KILLTUNE:       KILLS *ONE INSTANCE OF* TUNE IN AC, IF POSSIBLE
KILLTUNE:
	LDX     #1		;FIND IT
KILLLOOP:
	CMP     TUNNUM,X
	BEQ     TOVER
	DEX
	BPL     KILLLOOP
	RTS

* TOVER: END THE TUNE IN COMPONENT X.  THIS IS BOTH CALLED AS A SUBROUTINE AND
*         BRANCHED INTO.
TOVER:
	TXA
	AND     #$01		;LOW BIT IS CHANNEL NUMBER
	TAX
	LDY     #$FF		;FF = NO TUNE
	STY     TUNNUM,X
	INY
	STY     AUDV0,X		;TURN OFF SOUND
	RTS

* TUNER:	CALLED ONCE PER FRAME, PLAYS THE TUNES.
TUNER:
	LDA     GAMSTATE	;PLAY TUNES ONLY IN GAMEPLAY STATE
	CMP     #PLAYST
	BNE     TUNRTS

	LDA     STATUS+25	;MAKE SURE UFO THEME IS PLAYING, IF ON
	BMI     TNOUFO
	AND     #$0F
	CLC
	ADC     #4
	JSR     DOTUNE2		;PLAY HIS THEME, IF NOT ALREADY PLAYING
TNOUFO:

	LDX     #1		;START WITH CHANNEL 1
TCHX:
	TXA			;PUSH CHANNEL NUMBER
	PHA
	LDA     TUNNUM,X
	BMI     TNOTUNE		;FF = NO TUNE PLAYING
	JSR     TLOOP
TNOTUNE:
	PLA			;RESTORE CHANNEL NUMBER
	TAX
	DEX
	BPL     TCHX
	RTS

* TLOOP:  LOOP THROUGH EACH OF THE THREE COMPONENTS OF CHANNEL X SIGNAL
TLOOP:
	LDY     TINDEX,X
	LDA     TUNTAB,Y
	CMP     #$FF
	BEQ     TOVER
	CMP     #$FE
	BEQ     TBRANCH
	CMP     #$FD
	BEQ     TCHAIN
	CMP     #$80		;BIT 7 = SET DURATION ONLY
	BCS     TSETDUR

	;TNORM: NORMAL NOTE
	AND     #$1F
	STA     AUDC0,X		;GO POUND SAND IN YOUR ASS
	DEC     TDURCNT,X	;DURATION UP YET?
	BNE     TNEXT		;NOPE
	LDA     TDURTAB,X	;YUP.  REPRIME DURATION COUNTER
	STA     TDURCNT,X
	LDA     TUNTAB,Y	;CHECK FOR SPECIAL BITS
	CMP     #$40		;BIT 6 = HOLD FOREVER (TILL TUNE END)
	BCS     TNEXT		;-> SIMPLY DON'T BUMP POINTER.
	CMP     #$20		;BIT 5 = RESTART THIS COMPONENT
	BCS     TRESTART

	INC     TINDEX,X	;BUMP POINTER TO NEXT 'NOTE'

TNEXT:
	INX			;GO ON TO NEXT COMPONENT OF THIS
	INX			;CHANNEL (SKIP OVER OTHER CHANNEL)
	CPX     #6		;2 CHANNELS * 3 COMPONENTS PER CHANNEL
	BCC     TLOOP
	RTS

* TSETDUR: SET DURATION OF COMPONENT IN X TO VALUE IN AC.
TSETDUR:
	AND     #$7F		;LOW 7 BITS = NEW DURATION
	STA     TDURTAB,X
	STA     TDURCNT,X
	INC     TINDEX,X	;BUMP POINTER
	JMP     TLOOP		;AND CONTINUE

* TBRANCH: $FE,ADDR = BRANCH INTO RANDOM ADDR FOR THIS COMPONENT
TBRANCH:
	INY			;FETCH NEXT BYTE (NEW ADDRESS)
	LDA     TUNTAB,Y
	STA     TINDEX,X	;MASH IT INTO INDEX
	JMP     TLOOP

* TCHAIN: $FD,TUNNUM = CHAIN TO NEW TUNE.  NOTE: THIS MEANS *ALL* COMPONENTS
TCHAIN:
	INY			;FETCH NEXT BYTE (TUNNUM)
	LDA     TUNTAB,Y
	PHA
	JSR     TOVER		;END THIS TUNE
	PLA	
	JMP     DOTUNE		;JMP = JSR,RTS

* TRESTART: RESTART THIS COMPONENT OF TUNE
TRESTART:
	TXA
	AND     #$01		;LOW BIT IS CHANNEL NUMBER
	TAY
	LDA     TUNNUM,Y	;GET ITS TUNE NUMBER
	ASL			;FIND ROW IN INITIAL INDEX TABLE
	ASL			;(FOUR ENTRIES PER ROW)
	STA     TTEMP
	TXA			;GET COMPONENT INDEX
	LSR			;BITS 2-1 ARE COMPONENT NUMBER
	CLC
	ADC     TTEMP		;ADD ROW INDEX TO POSITION WITHIN ROW
	TAY
	LDA     TIINDEX,Y	;FETCH INITIAL INDEX
	STA     TINDEX,X	;POUND IT IN
	JMP     TNEXT		;AND DO THE NEXT COMPONENT

* ROUTINE TO PLAY THE BUMM, BUMM SOUND OF THE BACKGROUND HEART BEAT
* START OUT AT A SLOW RATE AND INCREASE SLOWLY

BACKSNDS:

* FIRST ADD RANDOM SPACE SOUNDS

	LDA     FRMCNT		;CHECK ONLY OCCASIONALLY
	AND     #$06
	BNE     UPRATE

	JSR     NEWRAND		;GET RANDOM VALUE
	CMP     #$06		;DO ONLY OCCASIONALLY
	BCS     UPRATE		;HAVE ONLY 6 RANDOM SPACE SOUNDS
	CLC			;ADD TO BASE
	ADC     #$12
	JSR     DOTUNE

* THEN UPDATE RATE

UPRATE:
	INC     BEATRATE	;INCREASE RATE
	BNE     OLDRATE

NEWRATE:
	LDA     #$90		;RESET BEATRATE
	STA     BEATRATE
	DEC     BEATVAL		;INCREASE RATE BY
	LDA     BEATVAL		; DECREMENTING BEATVAL
	CMP     #5		;DON'T LET GO BELOW 10
	BCS     OLDRATE
	LDA     #5
	STA     BEATVAL

* CHECK IF TIME FOR A BEAT
OLDRATE:
	DEC     BCOUNTER	;COUNTER COUNTS DOWN FROM
				; BEATVAL TO 0.
	BPL     DONEBEAT	;IS IT TIME FOR NEW BEAT?

THISTIME:
	LDA     BEATVAL		;YES, RESET COUNTER
	STA     BCOUNTER

	LDA     WHICHBT
	EOR     #$01		;TOGGLE TO OTHER TUNE
	STA     WHICHBT
	CLC
	ADC     #$0F		;ADD OFFSET TO TUNE
	JSR     DOTUNE		;PLAY IT
DONEBEAT:
	RTS

