o:
JSR init_sound
 MOVE.L $6C,save_intvec
 MOVE.L #sound_main,$6C
wait_lmb:
 BTST #6,$BFE001
 BNE wait_lmb
 MOVE.W #$F,$DFF096
 MOVE.L save_intvec,$6C
 CLR.L D0
 RTS

init_sound:
	MOVE.L #inst1,start_inst1
	MOVE.L #inst2,start_inst2
	MOVE.L #inst3,start_inst3
	MOVE.L #inst4,start_inst4
	MOVE.L #inst5,start_inst5
	MOVE.L #inst6,start_inst6
	MOVE.L #inst7,start_inst7
	MOVE.L #inst8,start_inst8
	MOVE.L #inst9,start_inst9
	MOVE.L #inst10,start_inst10
	MOVE.L #inst11,start_inst11
	MOVE.L #inst12,start_inst12
	MOVE.L #inst13,start_inst13
	MOVE.L #inst14,start_inst14
	MOVE.L #inst15,start_inst15
	MOVE.L #start_inst1,A0
	CLR.L D0
init_loop:
	MOVE.L 0(A0,D0.L),A1
	CLR.L (A1)
	ADDQ.L #4,D0
	CMPI.L #$3C,D0
	BNE init_loop
	MOVE.W #0,$DFF0A8
	MOVE.W #0,$DFF0B8
	MOVE.W #0,$DFF0C8
	MOVE.W #0,$DFF0D8
	CLR.L cur_pattern       ;aktueller Pattern
	CLR.L z_pattern		;Zaehler Pattern
	CLR.L cur_note_table
	LEA sound_name,A0
	MOVE.B 470(A0),anz_patterns+1
	MOVE.B 471(A0),no_funktion+1
	RTS
TRACK1:dc.w 0,0,0,0,0,0,0,0,0,0,1 
TRACK2:DC.W 0,0,0,0,0,0,0,0,0,0,2 
TRACK3:DC.W 0,0,0,0,0,0,0,0,0,0,4 
TRACK4:DC.W 0,0,0,0,0,0,0,0,0,0,8 
z_pattern:DC.L 0
cur_pattern:DC.L 0
counter:DC.L 0
cur_note_table:DC.L 0
sound_name:	DC.B 'pretend'
		ss=*-sound_name
		blk.b 20-ss,0	;Name auf 20 Stellen erweitern

inst_name1:	dc.b 'strings2'
		is1=*-inst_name1
		blk.b 22-is1,0	;auf 22 Stellen erweitern
len_instr1:	DC.W $12F2
vol_instr1:	DC.W $3C
rep_instr1:	DC.W $1236
rep_len_instr1:	DC.W $81E

inst_name2:	dc.b 'jahrmarkt1'
		is2=*-inst_name2
		blk.b 22-is2,0
len_instr2:	DC.W $1324
vol_instr2:	dc.w $40
rep_instr2:	dc.w 0
rep_len_instr2:	dc.w 1

inst_name3:	;name 3 
		is3=*-inst_name3
		blk.b 22-is3,0
len_instr3:	dc.w 0
vol_instr3:	dc.w 0
rep_instr3:	dc.w 0
rep_len_instr3:	dc.w 0

inst_name4:	;name 4 
		is4=*-inst_name4
		blk.b 22-is4,0
len_instr4:	dc.w 0
vol_instr4:	dc.w 0
rep_instr4:	dc.w 0
rep_len_instr4:	dc.w 0

inst_name5:	;name 5 
		is5=*-inst_name5
		blk.b 22-is5,0
len_instr5:	dc.w 0
vol_instr5:	dc.w 0
rep_instr5:	dc.w 0
rep_len_instr5:	dc.w 0

inst_name6:	;name 6 
		is6=*-inst_name6
		blk.b 22-is6,0
len_instr6:	dc.w 0
vol_instr6:	dc.w 0
rep_instr6:	dc.w 0
rep_len_instr6:	dc.w 0

inst_name7:	;name 7 
		is7=*-inst_name7
		blk.b 22-is7,0
len_instr7:	dc.w 0
vol_instr7:	dc.w 0
rep_instr7:	dc.w 0
rep_len_instr7:	dc.w 0

inst_name8:	;name 8 
		is8=*-inst_name8
		blk.b 22-is8,0
len_instr8:	dc.w 0
vol_instr8:	dc.w 0
rep_instr8:	dc.w 0
rep_len_instr8:	dc.w 0

inst_name9:	;name 9 
		is9=*-inst_name9
		blk.b 22-is9,0
len_instr9:	dc.w 0
vol_instr9:	dc.w 0
rep_instr9:	dc.w 0
rep_len_instr9:	dc.w 0

inst_name10:	;name 10 
		is10=*-inst_name10
		blk.b 22-is10,0
len_instr10:	dc.w 0
vol_instr10:	dc.w 0
rep_instr10:	dc.w 0
rep_len_instr10:dc.w 0

inst_name11:	;name 11 
		is11=*-inst_name11
		blk.b 22-is11,0
len_instr11:	dc.w 0
vol_instr11:	dc.w 0
rep_instr11:	dc.w 0
rep_len_instr11:dc.w 0

inst_name12:	;name 12 
		is12=*-inst_name12
		blk.b 22-is12,0
len_instr12:	dc.w 0
vol_instr12:	dc.w 0
rep_instr12:	dc.w 0
rep_len_instr12:dc.w 0

inst_name13:	;name 13 
		is13=*-inst_name13
		blk.b 22-is13,0
len_instr13:	dc.w 0
vol_instr13:	dc.w 0
rep_instr13:	dc.w 0
rep_len_instr13:dc.w 0

inst_name14:	;name 14 
		is14=*-inst_name14
		blk.b 22-is14,0
len_instr14:	dc.w 0
vol_instr14:	dc.w 0
rep_instr14:	dc.w 0
rep_len_instr14:dc.w 0

inst_name15:	;name 15 
		is15=*-inst_name15
		blk.b 22-is15,0
len_instr15:	dc.w 0
vol_instr15:	dc.w 0
rep_instr15:	dc.w 0
rep_len_instr15:dc.w 0


z_patterns:  DC.b $01	;05 =  Anzahl Patterns (max 63)
	       dc.b $78
;--    patterns (0-63)   ------ 
patterns:
dc.b $00,$00,$00,$01,$01 
ps=*-patterns
blk.b 64-ps,0
;--------------------
fi= *-sound_name
blk.b 600-fi,0
Notes:
;----------- Aufbau des Notentables --------
;Note0 INST0 /	Note1   Inst1 /	Note2  Inst2 / Note3  Inst3	
 dc.w $11d,$1000,0,0,0,0,$fe,$1000
 dc.w 0,0,0,0,0,0,0,0
 dc.w $1ac,$1000,0,0,0,0,0,0
 dc.w 0,0,0,0,0,0,0,0
 dc.w $153,$1000,0,0,0,0,$fe,$1000
 dc.w 0,0,0,0,0,0,0,0
 dc.w $fe,$1000,0,0,0,0,0,0		
 dc.w 0,0,0,0,0,0,0,0
 DC.W $11D,$1000,0,0,0,0,0,0
 dc.w 0,0,0,0,0,0,0,0
 DC.W $fe,$1000,0,0,0,0,0,0
 dc.w 0,0,0,0,0,0,0,0
 DC.W $11D,$1000,0,0,0,0,0,0
 dc.w 0,0,0,0,0,0,0,0
 DC.W $fe,$1000,0,0,0,0,0,0
 dc.w 0,0,0,0,0,0,0,0
 DC.W $11D,$1000,0,0,0,0,0,0
 dc.w 0,0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.W $fe,$1000,0,0,0,0,0,0
 dc.w 0,0,0,0,0,0,0,0
 DC.W $11D,$1000,0,0,0,0,0,0
blk.w 32,0
L06B18a:DC.L 0
start_inst1:	DC.L 0
start_inst2:	DC.L 0
start_inst3:	DC.L 0
start_inst4:	DC.L 0
start_inst5:	DC.L 0
start_inst6:	DC.L 0
start_inst7:	DC.L 0
start_inst8:	DC.L 0
start_inst9:	DC.L 0
start_inst10:	DC.L 0
start_inst11:	DC.L 0
start_inst12:	DC.L 0
start_inst13:	DC.L 0
start_inst14:	DC.L 0
start_inst15:	DC.L 0
anz_patterns:DC.W 0
no_funktion:DC.W 0
anz_channels:DC.W 0
;---------   Frequenztable ------------------------------
F_TAB:	DC.W $358,$328,$2FA,$2D0,$2A6,$280,$25C,$23A,$21A
        DC.W $1FC,$1E0,$1C5,$1AC,$194,$17D,$168,$153,$140
	DC.W $12E,$11D,$10D,$FE,$F0,$E2,$D6,$CA,$BE,$AA 
        DC.W $AA,$A0,$97,$8F,$87,$7F,$78,$71,0

inst1:blk.b 10000,0
inst2:blk.b 10000,0
inst3:DC.l 0
inst4:DC.l 0
inst5:DC.l 0
inst6:DC.l 0
inst7:DC.l 0
inst8:DC.l 0
inst9:DC.l 0
inst10:DC.l 0
inst11:DC.l 0
inst12:DC.l 0
inst13:DC.l 0
inst14:DC.l 0
inst15:DC.l 0
 
play_note:MOVEM.L D0-d7/a0-A6,-(A7)
 ADDQ.L #1,counter
 MOVE.L counter,D0
 CMPI.L #6,D0
 BNE L06FE86
 CLR.L counter
 BRA L06FF2C
L06FE86:MOVE.L #TRACK3,A6
 CMPI.B #0,3(A6)
 BEQ play_end
 CMPI.L #1,counter
 BEQ L06FEE0
 CMPI.L #2,counter
 BEQ L06FEEC
 CMPI.L #3,counter
 BEQ L06FEFA
 CMPI.L #4,counter
 BEQ L06FEE0
 CMPI.L #5,counter
 BEQ L06FEEC
 BRA play_end
L06FEE0:CLR.L D0
 MOVE.B 3(A6),D0
 LSR.B #4,D0
 BRA L06FF02
L06FEEC:CLR.L D0
 MOVE.B 3(A6),D0
 ANDI.B #$F,D0
 BRA L06FF02
L06FEFA:MOVE.W $10(A6),D2
 BRA L06FF22
L06FF02:MULU #2,D0
 CLR.L D1
 MOVE.W $10(A6),D1
 LEA F_TAB,A0
L06FF12:MOVE.W 0(A0,D0.L),D2
 CMP.W (A0),D1
 BEQ L06FF22
 ADDQ.L #2,A0
 BRA L06FF12
L06FF22:MOVE.W D2,$DFF0C6
 BRA play_end
L06FF2C:LEA sound_name,A0
 MOVE.L A0,A3
 ADDA.L #$C,A3
 MOVE.L A0,A2
 ADDA.L #$1D8,A2	;Start Patterns => a2
 ADDA.L #$258,A0	;Start Notes => a0
 MOVE.L cur_pattern,D0	;momentaner Pattern => d0
 MOVE.B 0(A2,D0.L),D1	;zu Bsis Pattern dazuaddieren = akt Pattern
 MULU #$400,D1		;D1 auf Beginn der Noten akt Pattern
 ADD.L z_pattern,D1	;wievielte Note in Liste
 MOVE.L D1,cur_note_table	
 CLR.W anz_channels
 MOVE.L #$DFF0A0,A5
 LEA TRACK1,A6
 BSR get_parameters
 MOVE.L #$DFF0B0,A5
 LEA TRACK2,A6
 BSR get_parameters
 MOVE.L #$DFF0C0,A5
 LEA TRACK3,A6
 BSR get_parameters
 MOVE.L #$DFF0D0,A5
 LEA TRACK4,A6
 BSR get_parameters
 BRA L070062
get_parameters:
 MOVE.L 0(A0,D1.L),(A6)
 ADDQ.L #4,D1
 CLR.L D2
 MOVE.B 2(A6),D2
 ANDI.B #-$10,D2
 LSR.B #4,D2
 CMPI.B #0,D2
 BEQ L070030
 CLR.L D3
 LEA L06B18A,A1
 MOVE.L D2,D4
 MULU #4,D2
 MULU #$1E,D4
 MOVE.L 0(A1,D2.L),4(A6)
 MOVE.W 0(A3,D4.L),8(A6)
 MOVE.W 2(A3,D4.L),$12(A6)
 MOVE.W 4(A3,D4.L),D3
 CMPI.W #0,D3
 BEQ L07001A
 MOVE.L 4(A6),D2
 ADD.L D3,D2
 MOVE.L D2,4(A6)
 MOVE.L D2,$A(A6)
 MOVE.W 6(A3,D4.L),8(A6)
 MOVE.W 6(A3,D4.L),$E(A6)
 MOVE.W $12(A6),8(A5)
 BRA L070030
L07001A:MOVE.L 4(A6),D2
 ADD.L D3,D2
 MOVE.L D2,$A(A6)
 MOVE.W 6(A3,D4.L),$E(A6)
 MOVE.W $12(A6),8(A5)
L070030:CMPI.W #0,(A6)
 BEQ L070060
 MOVE.W (A6),$10(A6)
 MOVE.W $14(A6),$DFF096
 MOVE.L 4(A6),0(A5)
 MOVE.W 8(A6),4(A5)
 MOVE.W 0(A6),6(A5)
 MOVE.W $14(A6),D0
 ADD.W D0,anz_channels
L070060:RTS
L070062:MOVE.L #$3E8,D0
wait:DBRA D0,wait
 MOVE.L #$8000,D0
 ADD.W anz_channels,D0
 MOVE.W D0,$DFF096
 MOVE.L #TRACK4,A6
 CMPI.W #1,$E(A6)
 BNE test_track3
 MOVE.L $A(A6),$DFF0D0
 MOVE.W $E(A6),$DFF0D4
test_track3:
 MOVE.L #TRACK3,A6
 CMPI.W #1,$E(A6)
 BNE test_track2
 MOVE.L $A(A6),$DFF0C0
 MOVE.W $E(A6),$DFF0C4
test_track2:MOVE.L #TRACK2,A6
 CMPI.W #1,$E(A6)
 BNE test_track1
 MOVE.L $A(A6),$DFF0B0
 MOVE.W $E(A6),$DFF0B4
test_track1:
 MOVE.L #TRACK1,A6
 CMPI.W #1,$E(A6)
 BNE next_note		;get next note in list
 MOVE.L $A(A6),$DFF0A0
 MOVE.W $E(A6),$DFF0A4
next_note:
 ADDI.L #$10,z_pattern	;Notenzahler um 4Noten erhoehen
 CMPI.L #$400,z_pattern		;Abfrage auf Ende Pattern
 BNE play_end
 CLR.L z_pattern		;wenn ja Patternzaehler loeschen
 ADDQ.L #1,cur_pattern
 CLR.L D0
 MOVE.W anz_patterns,D0
 MOVE.L cur_pattern,D1
 CMP.L D0,D1
 BNE play_end
 CLR.L cur_pattern
play_end:
 MOVEM.L (A7)+,D0-d7/a0-A6
 RTS

save_intvec:DC.l 0
 
sound_main:
 MOVEM.L D0-D1/A0-A1/A5-A6,-(A7)
 LEA $DFF000,A0
 MOVE.L 4,A6
 MOVE.W $1C(A0),D1	;d1= interrupt enable bits
 BTST #$E,D1		;free interrupts ???
 BEQ main_end		;no: ready 
 AND.W $1E(A0),D1	;choose interrupt request bits
 BTST #6,D1		;Blitter ready ?????
 BEQ test_vblank	;not ready => test vblank
 MOVEM.L $9C(A6),A1/A5	;A1 => iv_data  A5 => iv_code
 PEA -$24(A6)
 JMP (A5)
test_vblank:
 BTST #5,D1		;test VBlank Interrupt ?????
 BEQ test_intcop	;if VBlank then test cop_int
 BSR play_note
 MOVEM.L $90(A6),A1/A5
 PEA -$24(A6)
 JMP (A5)
test_intcop:
 BTST #4,D1		;Copper-Interrupt ?????
 BEQ main_end		;no copper_int
 MOVEM.L $84(A6),A1/A5
 PEA -$24(A6)
 JMP (A5)
main_end:
 MOVEM.L (A7)+,D0-D1/A0-A1/A5-A6
 RTE
 END
