
	section "player",code_p

;------------------------------------------------------------------------------
;                                 TEST PLAYING
;------------------------------------------------------------------------------

testplay
	bsr.w	mt_init

	lea	$dff000,a6
	move.w	$1c(a6),d0
	move.w	#$3fff,$9a(a6)		;kill interupts

waitbutt
	cmp.b	#-1,6(a6)
	bne.b	waitbutt

	movem.l	d0/a6,-(a7)
	bsr.w	mt_music
	movem.l	(a7)+,d0/a6
	
	btst	#6,$bfe001
	bne.b	waitbutt

	move.w	#$7fff,	dc.b 	0,5,6,7,8,10,11,13,16,19,22,26,32,43,64,128
mt_vibratotable	dc.b   	  0, 24, 49, 74, 97,120,141,161
		dc.b 	180,197,212,224,235,244,250,253
		dc.b 	255,253,250,244,235,224,212,197
		dc.b 	180,161,141,120, 97, 74, 49, 24
mt_periodtable	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	;0
		dc.w	850,802,757,715,674,637,601,567,535,505,477,450
		dc.w	425,401,379,357,337,318,300,284,268,253,239,225
		dc.w	213,201,189,179,169,159,150,142,134,126,119,113	;1
		dc.w	844,796,752,709,670,632,597,563,532,502,474,447
		dc.w	422,398,376,355,335,316,298,282,266,251,237,224
		dc.w	211,199,188,177,167,158,149,141,133,125,118,112	;2
		dc.w	838,791,746,704,665,628,592,559,528,498,470,444
		dc.w	419,395,373,352,332,314,296,280,264,249,235,222
		dc.w	209,198,187,176,166,157,148,140,132,125,118,111	;3
		dc.w	832,785,741,699,660,623,588,555,524,495,467,441
		dc.w	41er dmareg. init)
mt_dmawaiter=3			;no.of rasterlines waiting for dma

; INIT_PLAYING ----------------------------------------------------------------

mt_init	movem.l	d0-a6,-(a7)
	lea	mt_pm(pc),a5
	move.l	mt_dataptr(pc),a0

	lea	762(a0),a1			;patternlenght
	move.l	(a1)+,d0
	add.l	d0,a1				;samplestarts
			
	lea	mt_samplestarts(pc),a2
	moveq	#30,d1
mt_samploop
	moveq	#0,d0
	move.w	(a0),d0				;samplelength in words
	add.l	d0,d0				;samplelength in bytes
	move.l	a1,(a2)+			;save sample adress
	add.l	d0,a1				;next sample
	addq.l	#8,a0				;next sample info
	dbf	d1,mt_samploop

	or.b	#2,$bfe001			;filter off
	move.b	#6,mt_speed-mt_pm(a5)
	clr.b	mt_counter-mt_pm(a5)
	clr.w	mt_songpos-mt_pm(a5)
	clr.w	mt_patternpos-mt_pm(a5)
	move.l	$78.w,mt_oldirq-mt_pm(a5)	;save old_irq
	lea	$dff096,a0
	clr.w	$a8-$96(a0)
	clr.w	$b8-$96(a0)
	clr.w	$c8-$96(a0)
	clr.w	$d8-$96(a0)			;volume voice 1-4 = 0
	moveq	#$f,d0
	move.w	d0,(a0)				;no sound_dma

; CREATE PERIOD MULTAB (FOR FASTER F)

; SHOW 1.SCREEN 

	lea	planecop,a0
	move.w	d2,6(a0)
	swap	d2
	move.w	d2,2(a0)

; CLEAR 2.SCREEN

.wb	btst	#14,2(a6)
	bne.b	.wb
	move.l	#$01000000,$40(a6)
	move.w	#0,$66(a6)
	move.l	d0,$54(a6)
	move.w	#256*64+[screenbreite/2],$58(a6)
	move.l	d1,a1
	
; DRAW 3.SCREEN

	lea	multab(pc),a0
	lea	mt_chan1temp(pc),a2		;adresse st-structur
	lea	ss_sample1(pc),a4		;adresse showsample
	moveq	#0,d5				;y adder
	moveq	#0,d3				;x start
	bsr.b	.drawit

	lea	48(a2),a2			;adresse st-structur
	lea	30(a4),a4			;adresse showsample
	moveq	#64,d5
	moveq	#0,d3				;x start
	bsr.b	.drawit

	lea	48(a2),a2			;adresse st-structur
	lea	30(a4),a4			;adresse showsample
	move.w	#128,d5
	moveq	#0,d3
	bsr.b	.drawit

	move.w	#192,d5
	moveq	#0,d3
	lea	48(a2),a2			;adresse st-structur
	lea	30(a4),a4			;adresse showsample

; ZEICHNEN --------------------------------------------------------------------

.drawit
	moveq	#0,d0
	tst.w	10(a4)				;neuer ausschlag ???
	beq.b	.nonewaus

	move.l	14(a4),(a4)			;sample adress
	clr.w	4(a4)				;showadder
	move.w	18(a4),6(a4)			;period
	move.w	20(a4),8(a4)			;samplelenght
	clr.w	10(a4)				;clr anschlag
.nonewaus
	move.w	4(a4),d0			;showadder
	add.w	6(a4),d0			;add period
	move.w	d0,4(a4)			;new showadder sichern
	cmp.w	8(a4),d0			;showadder > samplelenght
	blo.b	.nolenght

	cmp.w	#2,28(a4)			;check replenght
	blo.b	.noloop				;<=2 (=kein loop) ???
	move.l	24(a4),(a4)			;set loopstart	
	move.w	28(a4),d1			;looplenght
	sub.w	#viewlength,d1
	move.w	d1,8(a4)			;=new samplelenght
	move.l	(a4),a3
	clr.w	4(a4)				;show adder = 0
	bra.b	.nolenght	
.noloop
	lea	leersample(pc),a3
	move.l	a3,(a4)				;sampleadress = leersample
	clr.l	4(a4)				;showadder & viewadder = 0
	clr.w	8(a4)				;samplelenght = 0
	bra.b	.newlenght
.nolenght
	move.l	(a4),a3				;sampleadress
	add.l	d0,a3				;add showadder
.newlenght
	moveq	#64,d4				;max volume
	sub.b	19(a2),d4			;act.vol. - max vol.
	add.w	d4,d4

	if viewlength<=128
	moveq	#viewlength-1,d7		;anzahl samples
	else
	move.w	#viewlength-1,d7
	endc
	
	move.w	#128,d6				;sample + screenadder 
.drawloop
	moveq	#0,d1				
	move.b	(a3)+,d1			;sample
	ext.w	d1				;sample byte auf wordsize
	beq.w	.addready			;= 0 ???
	bpl.b	.ispos				;ist positiv

	add.w	d4,d1				;add volume
	ble.b	.addready
	moveq	#0,d1
	bra.b	.addready
.ispos
	sub.w	d4,d1				;sub volume
	bge.b	.addready
	moveq	#0,d1

.addready
	add.w	d6,d1

	if shifter>0
	lsr.w	#shifter,d1
	endc

	move.w	d3,d0				;x
	addq.w	#1,d3				;x + 1
	add.w	d5,d1				;y adder

	if xyswap=1
	exg	d0,d1
	endc

	cmp.w	#255,d1
	bcc.b	.nodraw

	move.w	d0,d2
	lsr.w	#3,d0
	add.w	d1,d1
	add.w	(a0,d1.w),d0
	not.w	d2
	bset	d2,(a1,d0.w)			;set pixel in plane
	if doubley=1
	if screenbreite>127
	add.w	#screenbreite,d0
	bset	d2,(a1,d0.w)
	else
	bset	d2,screenbreite(a1,d0.w)
	endc
	endc

.nodraw	
	dbf	d7,.drawloop
	rts

; AUSWETUNG (ÜBER VBLANC NACH SOUNDREPLAYER) ----------------------------------

checksound
	move.b	mt_counter(pc),d0
	bne.b	.nochange			;mt_counter = 0 -> new note

	lea	mt_chan1temp(pc),a0		;adresse st-structur
	lea	ss_sample1(pc),a1		;adresse showsample
	lea	mt_periodtable(pc),a2
	lea	sample_rate(pc),a3
	moveq	#48,d0
	moveq	#30,d1
	moveq	#35*2,d4
	moveq	#3,d7
.checkit
	tst.w	(a0)
	beq.b	.nomodify
	move.w	(a0),10(a1)			;save anschlag
	move.l	4(a0),14(a1)			;sampleadress
	move.w	16(a0),d3			;period
	move.w	#viewlength,d5
	moveq	#0,d2
.checkloop
	cmp.w	(a2,d2.w),d3
	bge.b	.leavecheckloop
	addq.w	#2,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       