; thxplay: The interrupt code called by the CIA or VSYNC interrupt

; interrupt structure
int	dc.l	0,0		; ln_succ, ln_pred
	dc.b	NT_INTERRUPT,-1	; ln_type, ln_pri
	dc.l	intname		; ln_name
	dc.l	0		; iv_data
	dc.l	intcode		; iv_code

intname	dc.b	"Nora Batty's original THX2 player (tm)",0
	cnop	0,4

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

intcode	movem.l	d0-d7/a0-a6,-(sp)

; main playroutine (see play.asm)

	move.l	THX+thxBSS_P(pc),a0
	tst.b	thx_pPlaying(a0)
	beq.s	.exit			; skip if not playing
	bsr.s	THX+thxInterrupt	; play one frame of music


; thxSignalEnd() support (see misc.asm)

	tst.b	thx_pSongEnd(a0)
	beq.s	.sig_end		; skip if not songend
	move.w	dosig(pc),d0
	beq.s	.sig_end		; skip if trigger unset
	move.l	4.w,a6
	move.l	task(pc),a1
	move.l	signals(pc),d0
	jsr	_LVOSignal(a6)		; Signal(task, signals)
	lea	dosig(pc),a0
	clr.w	(a0)			; reset trigger
.sig_end

; bangnote support (see note.asm)
	lea	chan0(pc),a0
	move.b	(a0)+,d0		; test bn_on of channel 0
	beq.s	.no0
	moveq	#0,d4
	bsr.s	_playnote		; if on, play note on channel 0
.no0	lea	chan1(pc),a0
	move.b	(a0)+,d0		; test bn_on of channel 1
	beq.s	.no1
	moveq	#1,d4
	bsr.s	_playnote
.no1	lea	chan2(pc),a0
	move.b	(a0)+,d0		; test bn_on of channel 2
	beq.s	.no2
	moveq	#2,d4
	bsr.s	_playnote
.no2	lea	chan3(pc),a0
	move.b	(a0)+,d0		; test bn_on of channel 3
	beq.s	.no3
	moveq	#3,d4
	bsr.s	_playnote
.no3

	
.exit	movem.l	(sp)+,d0-d7/a0-a6
	ori.w	#%100,ccr	; set Z flag
	rts


; bang one channel
; a0=ptr to bangnote struct + 1
_playnote
	moveq	#0,d1
	move.b	(a0),d1		; d1=note
	clr.b	(a0)+		; reset note after one play

	moveq	#0,d3
	move.b	(a0),d3		; d3=ins
	clr.b	(a0)+		; reset instrument after one play

	moveq	#0,d2
	move.b	(a0)+,d2	; d2=fx

	moveq	#0,d0
	move.b	(a0)+,d0	; d0=fxval

	lea	THX(pc),a1

	ifd	_USE020_        ; include appropriate bang routine
	incbin	bin/thx-bang020.bin
	else
	incbin	bin/thx-bang000.bin
	endc
