; CETTE ROUTINE AFFICHE UNE IMAGE IFF AU FORMAT ACBM
; 5 BITPLANES  32 COULEURS   ET JOUE UNE MUSIQUE SOUND TRACKER
; LA ROUTINE UTILISEE EST LA PLAYROUTINE IV  DE D.O.C.
; --------------------------------------------------------------
; CODED BY BLUE MAX (OF CANAL CRACKER) IN OCTOBER 1988
; THANK TO HYSTERIA FOR HELP AND SOURCES


openlibrary=	-408
closelibrary=	-414
execbase=	$04

open=		-30
close=		-36
read=		-42

	move.l		execbase,a6
	lea		gfxname,a1
	jsr		openlibrary(a6)
	move.l		d0,gfxbase
	move.w		#$0020,$dff096
	
	move.l		gfxbase,a0
	add.l		#$32,a0
	move.w		#$0080,$dff096
	move.l		(a0),oldcopper
	move.l		#newcopper,(a0)
	move.w		#$8080,$dff096

	move.l		execbase,a6
	lea		dosname,a1
	clr.l		d0
	jsr		openlibrary(a6)
	move.l		d0,dosbase
	move.l		dosbase,a6
	move.l		#picname,d1
	move.l		#1005,d2
	jsr		open(a6)
	move.l		d0,ipic
	move.l		d0,d1
	move.l		#$60000,d2
	move.l		#$ffff,d3
	jsr		read(a6)
	move.l		ipic,d1
	jsr		close(a6)
	move.l		picdata,a0
	move.l		#255,d1
	move.l		#$50000,a1
	move.l		#$53000,a2
	move.l		#$56000,a3
	move.l		#$59000,a4
	move.l		#$5c000,a5

bcl1:	move.l		#9,d2
bcl2:	move.l		(a0),(a1)+
	move.l		10240(a0),(a2)+
	move.l		20480(a0),(a3)+
	move.l		30720(a0),(a4)+
	add.l		#40960,a0
	move.l		(a0),(a5)+
	sub.l		#40960,a0
	add.l		#4,a0
	dbra		d2,bcl2
	modulo=		$0
	add.l		#modulo,a1
	add.l		#modulo,a2
	add.l		#modulo,a3
	add.l		#modulo,a4
	add.l		#modulo,a5
	dbra		d1,bcl1

	move.l		#31,d1
	move.l		codata,a0
	move.l		#lcopper,a1
couleur:clr.l		d0
	clr.l		d3
	move.b		(a0)+,d3
	lsl.w		#4,d3
	add.b		(a0)+,d3
	move.b		(a0)+,d0
	lsr		#4,d0
	add.l		d0,d3
	move.w		d3,(a1)
	add.l		#4,a1
	dbra		d1,couleur

waity:	btst		#6,$bfe001
	jsr		start

	move.w		#$4000,$dff09a
	move.w		#$c000,$dff09a
	move.l		execbase,a6
	move.l		gfxbase,a1
	jsr		start

	move.l		gfxbase,a0
	add.l		#$32,a0
	move.w		#$0080,$dff096
	move.l		oldcopper,(a0)
	move.w		#$8080,$dff096
	move.l		execbase,a6
	move.l		#$8020,$dff096
	rts

newcopper:
	dc.w		$0180
lcopper:
	dc.w		$0000,$0182,$0789
	dc.w		$0184,$0444,$0186,$0777
	dc.w		$0188,$0343,$018a,$0965
	dc.w		$018c,$0000,$018e,$0000
	dc.w		$0190,$0000,$0192,$0000
	dc.w		$0194,$0000,$0196,$0000
	dc.w		$0198,$0000,$019a,$0000
	dc.w		$019c,$0000,$019e,$0000
	dc.w		$01a0,$0000,$01a2,$0000
	dc.w		$01a4,$0000,$01a6,$0000
	dc.w		$01a8,$0000,$01aa,$0000
	dc.w		$01ac,$0000,$01ae,$0000
	dc.w		$01b0,$0000,$01b2,$0000
	dc.w		$01b4,$0000,$01b6,$0000
	dc.w		$01b8,$0000,$01ba,$0000
	dc.w		$01bc,$0000,$01be,$0000

	dc.w		$008e,$2c81,$0090,$41ff
	dc.w		$0092,$0038,$0094,$00d0
	dc.w		$0108,$0000,$010a,$0000
	dc.w		$0100,$5200,$0102,$0000
	dc.w		$00e0,$0005,$00e2,$0000
	dc.w		$00e4,$0005,$00e6,$3000
	dc.w		$00e8,$0005,$00ea,$6000
	dc.w		$00ec,$0005,$00ee,$9000
	dc.w		$00f0,$0005,$00f2,$c000

oldcopper:	dc.l	0
gfxname:	dc.b	"graphics.library",0,0
picname:	dc.b	"df0:hysteria.acbm",0,0
		even
ipic:		dc.l	0
dosbase:	dc.l	0
dosname:	dc.b	"dos.library",0
picdata:	dc.l	$600ba
codata:		dc.l	$60030
gfxbase:	dc.l	$0	
*************************************************
*                                               *
*          Soundtracker Replay-Routine          *
*    Version 24 (public) - Update 15.12.1987    *
*						*
*        Written 1987  by Karsten Obarski   	*
*      3000 Hannover 61  Anderter Strasse 1	*
*   For all AMIGA 68000-Assembler programmers   *
*                                               *
*               start   =   START               *
*          breakpoint   =   END                 *
*                bugs   =   not known           *
*      images to read   =   see bottom          *
*************************************************

START:	JSR	REPLAY_INIT		;nur einmal initialisieren
	JSR	REPLAY_ENABLE		;starten am anfang vom song

main:	move.l	#12000,d0
wait:	dbra	d0,wait
	JSR	REPLAY_SONG		;irq / loop einsprung (immer)
	btst	#6,$bfe001
	bne	main
	JSR	REPLAY_DISABLE		;zum zwischendurch anhalten 

	;jetzt entweder anderen programteil abarbeiten und
	;danach wieder REPLAY_ENABLE, oder programm beenden.
	;(oder sonstwas)
END:	rts

*************************************************
* begin of muzak replay module  
*************************************************

*------------------------------------------------
REPLAY_INIT:
	move.l	#snd1,sounds+0
	move.l	#snd2,sounds+4
	move.l	#snd3,sounds+8
	move.l	#snd4,sounds+12
	move.l	#snd5,sounds+16
	move.l	#snd6,sounds+20
	move.l	#snd7,sounds+24
	move.l	#snd8,sounds+28
	move.l	#snd9,sounds+32
	move.l	#snda,sounds+36
	move.l	#sndb,sounds+40
	move.l	#sndc,sounds+44
	move.l	#sndd,sounds+48
	move.l	#snde,sounds+52
	move.l	#sndf,sounds+56
	move.l	#sounds,a0
	clr.l	d0
rint1:	move.l	(a0,d0.l),a1
	clr.l	(a1)
	addq.l	#4,d0
	cmp.l	#60,d0
	bne	rint1
	lea.l	muzakdata,a0
	move.b	470(a0),numpat+1
	rts

*------------------------------------------------
REPLAY_ENABLE:
	move.w	#-1,plamod
	move.w	#00,$dff0a8
	move.w	#00,$dff0b8
	move.w	#00,$dff0c8
	move.w	#00,$dff0d8
	clr.w	timpos
	clr.l	trapos
	clr.l	patpos	
	rts

*------------------------------------------------
REPLAY_DISABLE:
	clr.w	plamod
	move.w	#00,$dff0a8
	move.w	#00,$dff0b8
	move.w	#00,$dff0c8
	move.w	#00,$dff0d8
	move.w	#$f,$dff096
	rts

*------------------------------------------------
REPLAY_SONG:
	movem.l	d0-d7/a0-a6,-(a7)
	cmp.w	#0,plamod
	beq.s	rps1
	addq.w	#1,timpos
	cmp.w	#6,timpos
	bne.s	effecthandler
	clr.w	timpos
	bra 	replayhandler
rps1:	movem.l	(a7)+,d0-d7/a0-a6
	rts

*------------------------------------------------
effecthandler:
	lea.l	datach0,a6
	cmp.b	#0,3(a6)
	beq.s	eff1
	move.l	#$dff0a0,a5
	bsr.s	eff5
eff1:	lea.l	datach1,a6
	cmp.b	#0,3(a6)
	beq.s	eff2
	move.l	#$dff0b0,a5
	bsr.s	eff5
eff2:	lea.l	datach2,a6
	cmp.b	#0,3(a6)
	beq.s	eff3
	move.l	#$dff0c0,a5
	bsr.s	eff5
eff3:	lea.l	datach3,a6
	cmp.b	#0,3(a6)
	beq.s	eff4
	move.l	#$dff0d0,a5
	bsr.s	eff5
eff4:	movem.l	(a7)+,d0-d7/a0-a6
	rts
eff5:	move.b	2(a6),d0
	and.b	#$0f,d0
	cmp.b	#1,d0
	beq	appreggiato
	cmp.b	#2,d0
	beq	pitchbend
	rts

*------------------------------------------------
appreggiato:
	cmp.w	#1,timpos
	beq.s	appre1
	cmp.w	#2,timpos
	beq.s	appre2
	cmp.w	#3,timpos
	beq.s	appre3
	cmp.w	#4,timpos
	beq.s	appre1
	cmp.w	#5,timpos
	beq.s	appre2
	rts
appre1:	clr.l	d0
	move.b	3(a6),d0
	lsr.b	#4,d0
	bra.s	appre4
appre2:	clr.l	d0
	move.b	3(a6),d0
	and.b	#$0f,d0
	bra.s	appre4
appre3:	move.w	16(a6),d2
	bra.s	appre6
appre4:	lsl.l	#1,d0
	clr.l	d1
	move.w	16(a6),d1
	lea.l	notetable,a0
appre5:	move.w	(a0,d0.l),d2
	cmp.w	(a0),d1
	beq.s	appre6
	addq.l	#2,a0
	bra.s	appre5
appre6:	move.w	d2,6(a5)
	rts

*------------------------------------------------
pitchbend:
	clr.l	d0
	move.b	3(a6),d0
	lsr.b	#4,d0
	cmp.b	#0,d0
	beq.s	pitch2
	add.w	d0,(a6)
	move.w	(a6),6(a5)
	rts
pitch2:	clr.l	d0
	move.b	3(a6),d0
	and.b	#$0f,d0
	cmp.b	#0,d0
	beq.s	pitch3
	sub.w	d0,(a6)
	move.w	(a6),6(a5)
pitch3:	rts

*------------------------------------------------
replayhandler:
	lea.l	muzakdata,a0
	move.l	a0,a3
	add.l	#12,a3
	move.l	a0,a2
	add.l	#472,a2
	add.l	#600,a0
	move.l	trapos,d0
	clr.l	d1
	move.b	(a2,d0.l),d1
	mulu	#1024,d1
	add.l	patpos,d1
	clr.w	enabit
	move.l	#$dff0a0,a5
	lea.l	datach0,a6
	bsr	soundhandler
	move.l	#$dff0b0,a5
	lea.l	datach1,a6
	bsr	soundhandler
	move.l	#$dff0c0,a5
	lea.l	datach2,a6
	bsr	soundhandler
	move.l	#$dff0d0,a5
	lea.l	datach3,a6
	bsr	soundhandler
	move.l	#300,d0
repl1:	dbra	d0,repl1
	move.l	#$8000,d0
	add.w	enabit,d0
	move.w	d0,$dff096
	move.l	#datach3,a6
	cmp.w	#1,14(a6)
	bne.s	repl2
	move.l	10(a6),$dff0d0
	move.w	14(a6),$dff0d4
repl2:	move.l	#datach2,a6
	cmp.w	#1,14(a6)
	bne.s	repl3
	move.l	10(a6),$dff0c0
	move.w	14(a6),$dff0c4
repl3:	move.l	#datach1,a6
	cmp.w	#1,14(a6)
	bne.s	repl4
	move.l	10(a6),$dff0b0
	move.w	14(a6),$dff0b4
repl4:	move.l	#datach0,a6
	cmp.w	#1,14(a6)
	bne.s	repl5
	move.l	10(a6),$dff0a0	
	move.w	14(a6),$dff0a4
repl5:	add.l	#16,patpos
	cmp.l	#1024,patpos
	bne.s	repl6
	clr.l	patpos
	addq.l	#1,trapos
	clr.l	d0
	move.w	numpat,d0
	move.l	trapos,d1
	cmp.w	d0,d1
	bne.s	repl6
	clr.l	trapos
repl6:	movem.l	(a7)+,d0-d7/a0-a6
	rts

*------------------------------------------------
soundhandler:
	move.l	(a0,d1.l),(a6)
	addq.l	#4,d1
	clr.l	d2
	move.b	2(a6),d2
	and.b	#$f0,d2
	lsr.b	#4,d2
	cmp.b	#0,d2
	beq.s	shand2
	clr.l	d3
	lea.l	sounds-4,a1
	move.l	d2,d4
	mulu	#4,d2
	mulu	#30,d4
	move.l	(a1,d2.l),4(a6)
	move.w	(a3,d4.l),8(a6)
	move.w	2(a3,d4.l),18(a6)
	move.w	4(a3,d4.l),d3
	cmp.w	#0,d3
	beq.s	shand1
	move.l	4(a6),d2
	add.l	d3,d2
	move.l	d2,4(a6)
	move.l	d2,10(a6)
	move.w	6(a3,d4),8(a6)
	move.w	6(a3,d4),14(a6)
	move.w	18(a6),8(a5) 
	bra.s	shand2
shand1:	move.l	4(a6),d2
	add.l	d3,d2
	move.l	d2,10(a6)
	move.w	6(a3,d4.l),14(a6)
	move.w	18(a6),8(a5) 
shand2:	cmp.w	#0,(a6)
	beq.s	shand3
	move.w	(a6),16(a6)
	move.w	20(a6),$dff096
	move.l	4(a6),0(a5)
	move.w	8(a6),4(a5)
	move.w	0(a6),6(a5)
	move.w	20(a6),d0
	or.w	d0,enabit
shand3:	rts

*------------------------------------------------
	datach0:	blk.l	5,0
			dc.w	1
	datach1:	blk.l	5,0
			dc.w	2
	datach2:	blk.l	5,0
			dc.w	4
	datach3:	blk.l	5,0
			dc.w	8
	sounds:		blk.l	15,0
	patpos:		dc.l	0
	trapos:		dc.l	0
	timpos:		dc.w	0
	enabit:		dc.w	0
	numpat:		dc.w	0
	plamod:		dc.w	0
	notetable:
	dc.w	856,808,762,720,678,640,604,570,538,508,480,453
	dc.w	428,404,381,360,339,320,302,285,269,254,240,226  
	dc.w	214,202,190,180,170,160,151,143,135,127,120,113,000

*------------------------------------------------
* read images here
*------------------------------------------------
	snd1:		blk.b	8900,0	;samples
	snd2:		blk.b	8500,0
	snd3:		blk.b	8800,0
	snd4:		blk.b	9000,0
	snd5:		blk.b	9900,0
	snd6:		blk.b	6500,0
	snd7:		blk.b	9400,0
	snd8:		blk.b	3400,0
	snd9:		blk.b	1400,0
	snda:		blk.b	4000,0
	sndb:		blk.b	6500,0
	sndc:		blk.b	2300,0
	sndd:		blk.b	3000,0
	snde:		blk.b	7100,0
	sndf:		blk.b	2,0
   muzakdata:		blk.b	9816,0	;songfile

*************************************************
* end of muzak replay module  
*************************************************
