;ASC Sound Master player v1.13. Build 26/08/2024
;based on original ASM 1.12 player
;disassmed by Sand'MhM for POS-WT'24
;
;Warning!
;1. player non relocable!
;2. No about track info in player
;3. Support all older version of ASM 1.xx

;playercompile	equ #c000


;			ORG playercompile

			ld	de, song_here
			jr	initer		
player		jp	playsong
			jp	muteay

initer 
		;готовим буфер
		xor	a
		ld b,song_here-bufferA	;#65
		ld hl, bufferA
		ld (hl),a
		inc hl
		djnz $-2
		;инитим наш трак
		ld	(position+1), a
		ld	(plva00ff+1), a
		ld	(plvb00ff), a
		ld	(plvc00ff), a
		inc	a
		ld	(quant), a
;		ld	de, song_here
		ld	b, d
		ld	c, e
		ld	a, (de)
		inc	de
indelfromsong 
		ld	(quant_otygral+1), a
		ld	a, (de)
		inc	de
		ld	(setposto0+1), a
		call	inhl2de_addbchl
		ld	(postbladr+1), hl
ine1	call	inhl2de_addbchl
		ld	(smp_calc+1), hl
		call	inhl2de_addbchl
ine2	ld	(img_calc+1), hl
		ld	a, (de)
		inc	de
		ld	(cheklastpos+1), a
		ld	(pattpos_addrtbl+1), de
muteay	xor	a
		ld	hl, _AyR0_tone
		ld	de, AyR1_tone
		ld	bc, #D
		ld	(hl), a
		ldir
		dec	a
		ld	(AyR7_mixer), a
		jp	_outer
;----------------------------------------------
playsong
		ld	hl, quant
		dec	(hl)
		jr	nz, igraem_delay
quant_otygral 		 
		ld	(hl), 6
		ld	a, (bufferA)
		ld	hl,  bufferB_+1
		or	(hl)
		ld	hl, bufferC
		or	(hl)
		bit	0, a
		call	z, pattpos_addrtbl
		ld	ix, bufferA
		call	ixobrabotka
		ld	ix,  bufferB_+1
		call	ixobrabotka
		ld	ix, bufferC
		call	ixobrabotka
igraem_delay 		 
		ld	ix, bufferA
		ld	bc, (_AyR0_tone)
		call	ploya_to_no_vo
		ld	(_AyR0_tone), bc
		ld	(_AyR8_vola), a
		ld	ix,  bufferB_+1
		ld	bc, (_AyR2_tone)
		call	ploya_to_no_vo
		ld	(_AyR2_tone), bc
		ld	(_AyR9_volb), a
		ld	ix, bufferC
		ld	bc, (_AyR4_tone)
		call	ploya_to_no_vo
		ld	(_AyR4_tone), bc
		ld	(_AyR10_volc), a
		ld	a, (mxrvar)
		rlca
		ld	hl, rel_ok_and_back
		or	(hl)
		rlca
		ld	hl,  bufferA+1
		or	(hl)
		ld	(AyR7_mixer), a


;------------------------------------------
		;тут поем в АУк
_outer	ld	hl, AyR13_envshap
		ld	de, #FFBF
		ld	c, #FD
		ld	a, (hl)
		inc	a
		ld	a, #D
		jr	nz, outall
		dec	a
		dec	hl
outall	ld	b, a
		inc	a
		and	9
		or	(hl)
		ld	a, b
		jr	nz, out2
		dec	a
		dec	hl
		ld	b, d
		out	(c), a
		ld	b, e
		outi
		inc	a
out2	ld	b, d
		out	(c), a
		ld	b, e
		outd
		dec	a
		jp	p, outall
		ld	(AyR13_envshap), a
		ret

;----------------------------------------------
inhl2de_addbchl 	 
			ex	de, hl
inhl2de_addbchlnoex 	 
			ld	e, (hl)
			inc	hl
			ld	d, (hl)
			inc	hl
			ex	de, hl
			add	hl, bc
			ret
;
pattpos_addrtbl 	 
			ld	hl, 0
position	ld	de, 0
			add	hl, de
			ld	a, e
			inc	a
cheklastpos	cp	1
			jr	c, contpos
setposto0	ld	a, 0
contpos		ld	(position+1), a
			ld	a, (hl)
			add	a, a
			add	a, (hl)
			add	a, a
			ld	h, d
			ld	l, a
postbladr	ld	bc, 0
			add	hl, bc
			call	inhl2de_addbchlnoex
			ld	(plvara), hl
			call	inhl2de_addbchl
			ld	(plvarb), hl
			call	inhl2de_addbchl
			ld	(plvarc), hl
			ld	hl, bufferA
			set	0, (hl)
			ld	hl,  bufferB_+1
			set	0, (hl)
			ld	hl, bufferC
			set	0, (hl)
			xor	a
			ld	(xoravara), a
			ld	(xoravarb), a
			ld	(xoravarc), a
			ret
;
ixobrabotka	dec	(ix+#13)
			ret	p
			res	6, (ix+0)
			res	7, (ix+0)
			xor	a
			ld	(ix+#A), a
			ld	(ix+#10), a
			ld	e, (ix+#14)
			ld	d, (ix+#15)

somecoms 	ld	a, (de)
			inc	de
			cp	#60
			jp	c, whicomx
			ld	hl, somecoms
			push	hl
			sub	#F0 ; 'р'
			jr	nc, commands_jphl
			add	a, #10
			jr	c, setres4ix0
			add	a, #20
			jr	c, img_calc
			add	a, #20
			jr	c, smp_calc
			add	a, #40
			ld	(ix+#12), a
			ret
;
smp_calc	ld	bc, 0
			call	calc_ahlbc
			ld	(ix+#16), l
			ld	(ix+#17), h
			ret
;
img_calc 	ld	bc, 0
			call	calc_ahlbc
			ld	(ix+#1C), l
			ld	(ix+#1D), h
			ret
;
bc_div8		ld	a, c
			sra	b
			rra
			sra	b
			rra
			sra	b
			rra
			sra	b
			rra
			ld	c, a
			ret

;
calc_ahlbc	add	a, a
			ld	h, 0
			ld	l, a
			add	hl, bc
			ld	a, (hl)
			inc	hl
			ld	h, (hl)
			ld	l, a
			add	hl, bc
			ret
;
setres4ix0	res	4, (ix+0)
			ld	(ix+#F), a
			ret	nz
			set	4, (ix+0)
			ld	(ix+#F), #F
			ret
 
;------------------------------------------------
				;считаем адреса команд композиции
commands_jphl	ld	hl, com_tbl
				ld	b, 0
				ld	c, a
				add	hl, bc
				ld	c, (hl)
				add	hl, bc
				ld	c, b
				jp	(hl)

com_tbl			db com_SHOCK1976-$                  
				db com_holdsmpl-$
				db com_holdimg-$
				db com_holdinstr-$
				db com_quant-$
				db com_glisup-$
				db com_glisdn-$
				db com_portdn-$
				db com_ZXSOSMBM95-$
				db com_portup-$
				db com_ZXSOSMBM95-$
				db com_sldvol-$
				db com_ZXSOSMBM95-$
				db iio1-$
				db com_ZXSOSMBM95-$
 
;---------------------------------------------
		;обработка команд композиции
com_SHOCK1976
		ld	a, (de)
		inc	de
		ld	(ix+#B), a
iio1	ret
;
com_holdinstr 
		call	com_holdimg
com_holdsmpl 
		set	6, (ix+0)
		ret
com_holdimg 		 
		set	7, (ix+0)
		ret
;
com_quant
		ld	a, (de)
		inc	de
		ld	(quant), a
		ld	(quant_otygral+1), a
		ret
;
com_glisup
		ld	a, (de)
		neg
com_glis 		 
		inc	de
		push	de
		ld	(comgls_var), a
		add	a, a
		ld	l, a
		sbc	a, a
		ld	h, a
		add	hl, hl
		add	hl, hl
		add	hl, hl
		ex	de, hl
		ld	a, #FF
		jr	com_glisw
com_glisdn 
		ld	a, (de)
		jr	com_glis
com_portdn 
		set	6, (ix+0)
		ld	c, (ix+6)
		ld	b, (ix+7)
com_portup 
		ld	a, (de)
		inc	de
		ld	(comgls_var), a
		push	de
		ld	a, (de)
		cp	#56 ; 'V'
		jr	c, com_po1
		ld	c, (ix+6)
		ld	b, (ix+7)
		ld	a, (ix+2)
com_po1	call	notetoner
		push	de
		ld	a, (ix+2)
		call	notetoner
		ex	de, hl
		call	bc_div8
		add	hl, bc
		pop	de
		or	a
		sbc	hl, de
		add	hl, hl
		add	hl, hl
		add	hl, hl
		add	hl, hl
		push	af
		push	hl
		jr	nc, com_po2
		ex	de, hl
		xor	a
		ld	h, a
		ld	l, a
		sbc	hl, de
com_po2	ld	a, (comgls_var)
		neg
com_po3	ld	c, a
		ld	b, #11
		xor	a
com_po4	adc	a, a
		add	a, c
		jr	c, com_po5
		sub	c
		or	a
com_po5	adc	hl, hl
		djnz	com_po4
		ex	de, hl
		ld	b, 0
		ld	c, a
		pop	hl
		pop	af
		jr	c, com_po6
		ld	a, d
		cpl
		ld	d, a
		ld	a, e
		cpl
		ld	e, a
		inc	de
		ld	a, b
		cpl
		ld	b, a
		ld	a, c
		cpl
		ld	c, a
		inc	bc
com_po6 add	hl, bc
		ld	(ix+6),	l
		ld	(ix+7),	h
		ld	a, (comgls_var)
com_glisw 		 
		ld	(ix+8),	e
		ld	(ix+9),	d
		ld	(ix+#A), a
		pop	de
		ret
;
com_sldvol 
		ld	a, (de)
		inc	de
		add	a, a
		add	a, a
		rlca
		jr	nc, com_sv1
		xor	#F8
		add	a, 8
com_sv1	ld	(ix+#11), a
		ld	(ix+#10), a
		ret
;
com_ZXSOSMBM95 
		ld	(AyR13_envshap), a
		ret
;
whicomx	cp	#56
		jr	c, nexttt
		cp	#5E
		jr	c, nochen_
		jr	z, com_realise
		res	2, (ix+0)
		jr	nochen_
;
com_realise 		 
		set	3, (ix+0)
		jr	nochen_

;--------------------------------------
nexttt 
		ld	(ix+2),	a
		ld	a, (ix+#B)
		ld	(ix+#C), a
		ld	a, (ix+#A)
		dec	a
		jp	p, ne1
		xor	a
		ld	(ix+6),	a
		ld	(ix+7),	a
ne1		bit	6, (ix+0)
		jr	nz, ne2
		xor	a
		ld	(ix+#E), a
		ld	(ix+4),	a
		ld	(ix+5),	a
		ld	l, (ix+#16)
		ld	h, (ix+#17)
		ld	(ix+#18), l
		ld	(ix+#19), h
		set	2, (ix+0)
		res	3, (ix+0)
		res	1, (ix+0)
ne2		bit	7, (ix+0)
		jr	nz, env_
		ld	l, (ix+#1C)
		ld	h, (ix+#1D)
		ld	(ix+#1E), l
		ld	(ix+#1F), h
		ld	(ix+3),	0
env_	bit	4, (ix+0)
		jr	z, nochen_
		ld	a, (de)
		inc	de
		ld	(AyR11_envfine), a
nochen_	ld	a, (ix+#12)
		ld	(ix+#13), a
		ld	(ix+#14), e
		ld	(ix+#15), d
		ld	a, (de)
		inc	a
		ret	nz
		res	0, (ix+0)
		ret

;---------------------------------------
ploya_to_no_vo 		 
		xor	a
		bit	1, (ix+0)
		jr	nz, backret
		bit	2, (ix+0)
		jr	z, backret
		call	obrabx
		call	obraby
		bit	3, (ix+1)
		jr	nz, plo1
		ld	a, (ix+7)
		add	a, (ix+#C)
		ld	(_AyR6_nois), a
plo1	ld	a, (ix+2)
		add	a, (ix+3)
		cp	#56
		jr	c, plo2
		rla
		sbc	a, a
		cpl
		and	#55
plo2	call	notetoner
		ld	l, (ix+4)
		ld	h, (ix+5)
		add	hl, de
		ld	c, (ix+6)
		ld	b, (ix+7)
		call	bc_div8
		add	hl, bc
		ld	b, h
		ld	c, l
		ld	a, (ix+#A)
		or	a
		jr	z, plo4
		jp	m, plo3
		dec	(ix+#A)
plo3	ld	l, (ix+6)
		ld	h, (ix+7)
		ld	e, (ix+8)
		ld	d, (ix+9)
		add	hl, de
		ld	(ix+6),	l
		ld	(ix+7),	h
plo4 	ld	a, (ix+0)
		cpl
		and	#30
		ld	a, (ix+#D)
		jr	nz, backret
		or	#10
backret	ret
;
obrabx	ld	a, (ix+#10)
		ld	c, 0
		or	a
		jr	z, obx2
		sub	8
		cp	8
		jr	nc, obx1
		rra
		sbc	a, a
		scf
		rla
		ld	c, a
		ld	a, (ix+#11)
obx1	ld	(ix+#10), a
		ld	a, c
		call	obx12
obx2	ld	l, (ix+#18)
		ld	h, (ix+#19)
		ld	d, h
		ld	e, l
		ld	a, (hl)
		inc	hl
		ld	c, (hl)
		inc	hl
		ld	b, (hl)
		inc	hl
		cp	#20 ; ' '
		jr	c, obx7
		bit	7, a
		jr	nz, obx5
obx3	bit	6, a
		jr	nz, obx6
obx4	bit	5, a
		jr	z, obx7
		set	1, (ix+0)
		jr	obx7
obx5	ld	(ix+#1A), e
		ld	(ix+#1B), d
		jr	obx3
obx6	bit	3, (ix+0)
		jr	nz, obx4
		ld	l, (ix+#1A)
		ld	h, (ix+#1B)
obx7	ld	(ix+#18), l
		ld	(ix+#19), h
		ld	e, c
		ld	c, a
		ld	a, e
		rla
		sbc	a, a
		ld	d, a
		ld	l, (ix+4)
		ld	h, (ix+5)
		add	hl, de
		ld	(ix+4),	l
		ld	(ix+5),	h
		ld	a, b
		and	9
		ld	(ix+1),	a
		ld	a, b
		and	6
		cp	2
		set	5, (ix+0)
		jr	z, obx8
		res	5, (ix+0)
		jr	c, obx8
		sub	5
		call	obx12
obx8	ld	a, b
		rrca
		rrca
		rrca
		rrca
		and	#F
		add	a, (ix+#E)
		jp	p, obx9
		xor	a
obx9	cp	#10
		jr	c, obx10
		ld	a, #F
obx10	ld	d, a
		ld	b, (ix+#F)
obx11	add	a, d
		djnz	obx11
		and	#F0 ; 'р'
		rrca
		rrca
		rrca
		rrca
		ld	(ix+#D), a
		bit	5, (ix+0)
		jr	z, obry4
		bit	3, (ix+1)
		jr	z, obry4
		ld	a, c
		add	a, a
		add	a, a
		add	a, a
		sra	a
		sra	a
		sra	a
		ld	hl, AyR11_envfine
		add	a, (hl)
		ld	(hl), a
		ret
obx12	add	a, (ix+#E)
		jp	p, obx13
		cp	#F1 ; 'с'
		adc	a, 0
		jr	obx14
obx13	cp	#10
		adc	a, #FF
obx14	ld	(ix+#E), a
		ret
;
obraby	ld	l, (ix+#1E)
		ld	h, (ix+#1F)
		ld	d, h
		ld	e, l
		ld	a, (hl)
		inc	hl
		ld	b, (hl)
		inc	hl
		cp	#20 ; ' '
		jr	c, obry3
		bit	7, a
		jr	nz, obry2
obry1	bit	6, a
		jr	z, obry3
		ld	l, (ix+#20)
		ld	h, (ix+#21)
		jr	obry3
obry2	ld	(ix+#20), e
		ld	(ix+#21), d
		jr	obry1
obry3	ld	(ix+#1E), l
		ld	(ix+#1F), h
		ld	c, a
		ld	a, b
		add	a, (ix+3)
		ld	(ix+3),	a
obry4	ld	a, c
		and	#10
		neg
		or	c
		add	a, (ix+#C)
		ld	(ix+#C), a
		ret
;
notetoner 		 
		add	a, a
		ld	d, 0
		ld	e, a
		ld	hl, _tone_table
		add	hl, de
		ld	e, (hl)
		inc	hl
		ld	d, (hl)
		ret

;-------------------табличка тональностей (обрезаная, как и в редакторе)
_tone_table			dw #EDC ;0
					dw #E07
					dw #D3E	;1
					dw #C80
					dw #BCC
					dw #B22
					dw #A82
					dw #9EC
					dw #95C
					dw #8D6
					dw #858
					dw #7E0
					dw #76E
					dw #704
					dw #69F	;2
					dw #640
					dw #5E6
					dw #591
					dw #541
					dw #4F6
					dw #4AE
					dw #46B
					dw #42C
					dw #3F0
					dw #3B7
					dw #382
					dw #34F	;3
					dw #320
					dw #2F3
					dw #2C8
					dw #2A1
					dw #27B
					dw #257
					dw #236
					dw #216
					dw #1F8
					dw #1DC
					dw #1C1
					dw #1A8	;4
					dw #190
					dw #179
					dw #164
					dw #150
					dw #13D
					dw #12C
					dw #11B
					dw #10B
					dw #FC
					dw #EE
					dw #E0
					dw #D4	;5
					dw #C8
					dw #BD
					dw #B2
					dw #A8
					dw #9F
					dw #96
					dw #8D
					dw #85
					dw #7E
					dw #77
					dw #70
					dw #6A	;6
					dw #64
					dw #5E
					dw #59
					dw #54
					dw #50
					dw #4B
					dw #47
					dw #43
					dw #3F
					dw #3C
					dw #38
					dw #35	;7
					dw #32
					dw #2F
					dw #2D
					dw #2A
					dw #28
					dw #26
					dw #24
					dw #22
					dw #20
					dw #1E
					dw #1C
;тут должа была бы быть восьмая октава, но ее нет... ну и ладно...
;------------------------------------------------------
		;буфферы
;
bufferA 	 
		ds 11
xoravara 		 
		ds 7
plva00ff 		 
		ds 2
plvara	ds 13
bufferB_ 		 
		ds 2
rel_ok_and_back 	 
		ds 7
beg_rel_tbl_and_buffer
		ds 3

xoravarb
		ds 8
plvb00ff
		nop
plvarb	ds 14
bufferC nop
mxrvar 	ds 10
xoravarc
		ds 8
plvc00ff 
		nop
plvarc 	ds 14


;------------------------------
				;реги АУка
_AyR0_tone		db 0 
AyR1_tone		db 0			 
_AyR2_tone		db 0			 
AyR3_tone		db 0
_AyR4_tone		db 0			 
AyR5_tone		db 0
_AyR6_nois		db 0			 
AyR7_mixer		db 0			 
_AyR8_vola		db 0			 
_AyR9_volb		db 0			 
_AyR10_volc		db 0
AyR11_envfine	db 0			 
AyR12 			db 0
AyR13_envshap	db 0

;---------------------------------
quant			db 0                 
comgls_var		ds 27

song_here	

