;fast(long) player,can be INCLUDEd
;#E21 3.31 by Nick/GDC Воронеж'97
;#D86 3.51 fix Mm<M,MOnS+eR Самара 26xii00
;#C03 3.695 fix AlCo 1vi01-26xi05
;#C11 3.699 fix AlCo (3xxx chnA/B when old 3xxx ends только что)
        ifndef pt3_patchaddr
pt3_patchaddr=0 ;physical addresses in module
        ENDIF 
        ifndef pt3_frq
pt3_frq=0;1;all freq tables(3.4x)
        ENDIF 
        ifndef pt3_msx
pt3_msx=0
        ENDIF 
        ifndef pt3_smpfix
pt3_smpfix=1;vol,T,E/N cumulations in SMPs
        ENDIF 
        ifndef pt3_port3
pt3_port3=1;tone port.
        ENDIF 
        ifndef pt3_vib6
pt3_vib6=1;vibrate
        ENDIF 
        ifndef pt3_ts2
pt3_ts2=0;2nd TS player;играет пат47 вместо пат0 и т.д.
        ENDIF 

        ifndef jp_
        MACRO jp_ addr
        JR addr ;JP
        ENDM 
        ENDIF 

        ifndef m4
        MACRO m4
        SLA L
        SLA L
        ENDM 
        ENDIF 

        ifndef shut
        MACRO shut
       ;LD HL,#E
       IF pt3_msx
        LD BC,#A0
       ELSE 
        LD BC,-3
       ENDIF 
        DEC L
        OUT (C),L
       IF pt3_msx
        LD C,#A1
       ELSE 
        LD B,#BF
       ENDIF 
        OUT (C),A;0/255
        JR NZ,$-10
        ENDM 
        ENDIF 

        ifndef esld_
        MACRO esld_
Eef_sld LD A,(DE)
        INC DE
        LD (Esl_ist+1),A
        LD A,(DE)
	ld L,A
        INC DE
        LD A,(DE)
	ld H,A
        INC DE
        LD (Esl_ids+1),HL
        LD A,6
	ld (E_ins0),A
        RET 
        ENDM 
        ENDIF 

        ifndef l_
        MACRO l_ addr
        LD A,(DE)
        INC DE
        LD (addr),A
        RET 
        ENDM 
        ENDIF 

       ;IF0 $-#8000
;StAnDaLoNe
        ;ORG #C000
;begin
       ;ENDIF 
compile
        LD HL,module
        JR INIT
        JP PLAY
       IF pt3_frq
        JR STOP
       ENDIF 
INIT
        DI 
        LD (sav_SP0+1),SP
        LD (ia_pos0+1),HL
      IF pt3_frq
        PUSH HL
        LD DE,100
        ADD HL,DE
        PUSH DE
        PUSH HL
        DEC HL
        LD E,(HL)
        LD HL,NT_DATA
        LD BC,T_
        ADD HL,DE
        ADD HL,DE
        ADD HL,DE
        LD E,(HL)
        CALL MAKEfrq
        POP HL
        POP DE
        POP BC
      ELSE 
        LD B,H
	ld C,L
        LD DE,100
        ADD HL,DE
      ENDIF 
        LD A,(HL)
        INC HL
        LD (temp+1),A
        LD SP,HL
        ADD HL,DE
        LD (Psa_beg+1),HL
        POP AF
        LD E,A
        ADD HL,DE
        LD (Psa_lop+1),HL
        POP HL
        ADD HL,BC
        LD (Psa_chn+1),HL
       LD HL,tba_smp
ia_s0   EX DE,HL
        POP HL
        ADD HL,BC
        EX DE,HL
        LD (HL),E
        INC L
        LD (HL),D
        INC L
        JR NZ,ia_s0
        INC L
        LD H,L
        LD (A_qty),HL
        LD (C_qty),HL
sav_SP0 LD SP,0
STOP
       LD HL,DUMMYORN
        LD (Ao_adr+1),HL
        LD (Bo_adr+1),HL
        LD (Co_adr+1),HL
        LD (Asi_adr+1),HL
        LD (Bsi_adr+1),HL
        LD (Cs_adr+1),HL
        LD (Ach_adr+1),HL
        XOR A
        LD (Am_vol+1),A
        LD (Bm_vol+1),A
        LD (Cm_vol+1),A
       LD (Ei_form+1),A ;no glu glu after switch module
       SUB 16 ;FIX
       LD (Agi_vol+1),A
       LD (Bgi_vol+1),A
        LD (Cg_vol+1),A
       SBC A,A
        LD (Avb_lok+1),A
        LD (Cvb_lok+1),A
        LD (Bvb_lok+1),A
       XOR A
       IF !pt3_frq
       NOP 
       ENDIF 
        shut
        RET 

       IF !pt3_frq
	esld_
       ENDIF 

tba_smp DS 64,0
tba_orn DS 32,0
        DISPLAY $
tab_vol
frq_A   DB 0,0
frq_B   DB 0,0
frq_C   DB 0,0
N_frq   DB 0 ;;4
        DB 0     ;mix    ;no_use
vol_A   DB 0
vol_B   DB 0
vol_C   DB 0
frq_E   DB 0,0
E_form  DB 0 ;;4
DUMMYORN
DUMMYSMP
       DS 2
        DB 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1
        DB 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2
        DB 0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3
        DB 0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4
        DB 0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5
        DB 0,0,1,1,2,2,2,3,3,4,4,4,5,5,6,6
        DB 0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7
        DB 0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8
        DB 0,1,1,2,2,3,4,4,5,5,6,7,7,8,8,9
        DB 0,1,1,2,3,3,4,5,5,6,#7,#7,#8,#9,#9,#A
        DB 0,1,1,2,3,4,4,5,6,7,#7,#8,#9,#A,#A,#B
        DB 0,1,2,2,3,4,5,6,6,7,#8,#9,#A,#A,#B,#C
        DB 0,1,2,3,3,4,5,6,7,8,#9,#A,#A,#B,#C,#D
        DB 0,1,2,3,4,5,6,7,7,8,#9,#A,#B,#C,#D,#E
        DB 0,1,2,3,4,5,6,7,8,9,#A,#B,#C,#D,#E,#F
tab_frq
        DS #C0 

        ifndef tabl
        MACRO tabl channel
       IF !pt3_port3
	if channel==1
Aef_nsT=0
	endif
	if channel==2
Bef_nsT=0
	endif
	if channel==3
Cef_nsT=0
	endif
       ENDIF 
       IF !pt3_vib6
	if channel==1
Aef_vib=0
	endif
	if channel==2
Bef_vib=0
	endif
	if channel==3
Cef_vib=0
	endif
       ENDIF 

	if channel==1
	DW Aef_slT ;1/2xxx
        DW Aef_nsT ;3xxx
        DW Aef_dSm ;4.xx
        DW Aef_dOr ;5.xx
        DW Aef_vib ;6.xx
	endif
	if channel==2
	DW Bef_slT ;1/2xxx
        DW Bef_nsT ;3xxx
        DW Bef_dSm ;4.xx
        DW Bef_dOr ;5.xx
        DW Bef_vib ;6.xx
	endif
	if channel==3
	DW Cef_slT ;1/2xxx
        DW Cef_nsT ;3xxx
        DW Cef_dSm ;4.xx
        DW Cef_dOr ;5.xx
        DW Cef_vib ;6.xx
	endif

       IF !pt3_vib6
        DS 4,1
       ELSE 
	if channel==1
Aef_vib
	endif
	if channel==2
Bef_vib
	endif
	if channel==3
Cef_vib
	endif
	LD A,(DE)
        INC DE
	if channel==1
        JR AVIB
	endif
	if channel==2
        JR BVIB
	endif
	if channel==3
        JR CVIB
	endif
       ENDIF 
        DW Eef_sld
        DW eff_tmp
       IF (($-18)&#ff00)!=($&#ff00)
        DISPLAY "ERROR eff ",channel
       ENDIF 

       IF pt3_vib6
;0ef_vib
       ;LD A,(DE)
       ;INC DE
	if channel==1
AVIB    LD (Avb_ist+1),A;fix fr/st
	endif
	if channel==2
BVIB    LD (Bvb_ist+1),A;fix fr/st
	endif
	if channel==3
CVIB    LD (Cvb_ist+1),A;fix fr/st
	endif
       IF channel==3
        LD (Cvb_stp+1),A
       ENDIF 
        LD A,(DE)
        INC DE
	if channel==1
        LD (Avb_ifr+1),A;fix st/fr
	endif
	if channel==2
        LD (Bvb_ifr+1),A;fix st/fr
	endif
	if channel==3
        LD (Cvb_ifr+1),A;fix st/fr
	endif
       IF (channel)!=3
        LD A,6
	if channel==1
	ld (A_ins4),A
	else
	ld (B_ins4),A
	endif
       ENDIF 
        RET 
       ENDIF 
        ENDM 
        ENDIF 

A_eff
	tabl 1

B_eff
	tabl 2

C_eff
	tabl 3

A_qty   NOP 
B_qty   NOP 
C_qty   NOP 

PLAY
	JP play_0
       EXX ;for B'<>1
        LD A,#C3
	ld (PLAY),A
;--- install_A ----
        XOR A
        LD L,A
	ld H,A
       LD DE,#618
A_gli   LD (Asl_dsp+1),HL
A_ins0  JR A_ins0Q
       LD (As_dsp+1),A ;note
       IF pt3_smpfix
        LD (As_Nsl+1),A
        LD (As_Esl+1),A
        LD (As_dtn+1),HL
       ENDIF 
       LD (Ao_dsp+1),A ;todo VARIABLE?
        LD (Asl_stp+1),A
       IF pt3_vib6
        LD (Avb_stp+1),A
       ENDIF 
Avb_cpl CPL 
        LD (Avb_lok+1),A
       IF pt3_port3
       LD A,(A_ins3)    ;3.699
       CP E;24          ;
       jr z,$+8           ;
       LD A,(Api_lok+1) ;
       OR A             ;
       jr z,$+7           ;
       ENDIF 
Asi_not LD A,0
        LD (As_note+1),A
Asi_adr LD HL,0
        LD (As_adr+1),HL
       IF pt3_smpfix
       LD A,16
       LD (As_Vsl+1),A
       ENDIF 
A_ins0Q

Agi_vol LD A,0         ;global_volume
avol    LD (Ag_vol+1),A

A_ins2  JR A_ins2Q
        XOR A          ;ornament
        LD (Ao_dsp+1),A
Aoi_adr LD HL,0
        LD (Ao_adr+1),HL
Ami_vol LD A,0
        LD (Am_vol+1),A
A_ins2Q
A_ins3  JR A_ins3Q
;port & sld
Api_stp LD A,0
        LD (Asl_stp+1),A
        LD (Asl_sts+1),A
Api_tfr LD HL,0
        LD (Asl_tfr+1),HL
       IF pt3_port3
Api_not LD A,0
        LD (Asl_not+1),A
Api_lok LD A,0
        LD (Asl_plk-1),A
       ENDIF 
       IF pt3_vib6
         XOR A
         LD (Avb_stp+1),A
       ENDIF 
A_ins3Q
        IF pt3_vib6
A_ins4  JR A_ins4Q
       LD A,E;24        ;vib
        LD (A_ins4),A
Avb_ist LD A,0
        LD (Avb_sts+1),A
        LD (Avb_stp+1),A
Avb_ifr LD A,0
        LD (Avb_frq+1),A
A_ins4Q
        ENDIF 
A_ins5   JR A_ins5Q
Asi_dsp  LD A,0         ;disp_sample
         LD (As_dsp+1),A
        LD A,E;24
         LD (A_ins5),A
A_ins5Q
A_ins6   JR A_ins6Q
Aoi_dsp  LD A,0
         LD (Ao_dsp+1),A;disp_ornament
        LD A,E;24
         LD (A_ins6),A
A_ins6Q
;--- install_B ---

        XOR A
        LD L,A,H,A
B_gli   LD (Bsl_dsp+1),HL
B_ins0  JR B_ins0Q
        LD (Bs_dsp+1),A
       IF pt3_smpfix
        LD (Bs_Nsl+1),A
        LD (Bs_Esl+1),A
        LD (Bs_dtn+1),HL
       ENDIF 
        LD (Bo_dsp+1),A
        LD (Bsl_stp+1),A
       IF pt3_vib6
        LD (Bvb_stp+1),A
       ENDIF 
Bvb_cpl CPL 
        LD (Bvb_lok+1),A
       IF pt3_port3
       LD A,(B_ins3)    ;3.699
       CP E;24          ;
       jr z,$+8           ;
       LD A,(Bpi_lok+1) ;
       OR A             ;
       jr z,$+7           ;
       ENDIF 
Bsi_not LD A,0
        LD (Bs_note+1),A
Bsi_adr LD HL,0
        LD (Bs_adr+1),HL
       IF pt3_smpfix
       LD A,16
       LD (Bs_Vsl+1),A
       ENDIF 
B_ins0Q

Bgi_vol LD A,0
bvol    LD (Bg_vol+1),A

B_ins2  JR B_ins2Q
        XOR A
        LD (Bo_dsp+1),A
Boi_adr LD HL,0
        LD (Bo_adr+1),HL
Bmi_vol LD A,0
        LD (Bm_vol+1),A
B_ins2Q
B_ins3  JR B_ins3Q
Bpi_stp LD A,0
        LD (Bsl_stp+1),A
        LD (Bsl_sts+1),A
Bpi_tfr LD HL,0
        LD (Bsl_tfr+1),HL
       IF pt3_port3
Bpi_not LD A,0
        LD (Bsl_not+1),A
Bpi_lok LD A,0
        LD (Bsl_plk-1),A
       ENDIF 
       IF pt3_vib6
        XOR A
        LD (Bvb_stp+1),A
       ENDIF 
B_ins3Q
       IF pt3_vib6
B_ins4  JR B_ins4Q
       LD A,E;24
        LD (B_ins4),A
Bvb_ist LD A,0
        LD (Bvb_sts+1),A
        LD (Bvb_stp+1),A
Bvb_ifr LD A,0
        LD (Bvb_frq+1),A
B_ins4Q
       ENDIF 
B_ins5  JR B_ins5Q
Bsi_dsp LD A,0
        LD (Bs_dsp+1),A
       LD A,E;24
        LD (B_ins5),A
B_ins5Q
B_ins6  JR B_ins6Q
Boi_dsp LD A,0
        LD (Bo_dsp+1),A
       LD A,E;24
        LD (B_ins6),A
B_ins6Q
;--- install_E ---
Ei_form LD A,0         ;env_form
        LD (E_form),A ;;4
        OR A
        JR Z,temp
Ei_frq  LD HL,0        ;env_frq
        LD (E_frq+1),HL
        XOR A
        LD L,A
	ld H,A
        LD (Ei_form+1),A
        LD (Esl_frq+1),HL
        LD (Esl_stp+1),A

temp    LD A,3
	ld (int_qty+1),A
Ni_frq  LD A,0
	ld (N_frq),A ;;4

E_ins0  JR Ei_Q
Esl_ids LD HL,0        ;stp_sld_env
        LD (Esl_sds+1),HL
Esl_ist LD A,0         ;stp_sld_p
        LD (Esl_stp+1),A
        LD (Esl_sts+1),A
       LD A,E;24
        LD (E_ins0),A
Ei_Q
;--- nota_A + calc next position ---
       LD HL,A_qty
       DEC (HL)
        LD H,E
      ;LD L,1 ;E
        LD B,E
        EXX 
        JR NZ,AqtyQ
      ;LD (avol+2),A
Ach_adr LD DE,tab_vol+16
        LD A,(DE)
        OR A
        JR NZ,Ps_n0
       LD (Ni_frq+1),A ;;5!!!
       LD D,A          ;;
        LD (sav_SP2+1),SP
Psa_beg LD HL,module+201
        LD A,(HL)
      ;LD E,A           ;6
        INC A       ;AAA;6
        JR NZ,Ps_n1 ;NC ;6
Psa_lop LD HL,module+201
Ps_n1
       IF pt3_ts2
        LD A,47*3
        SUB (HL)
        LD E,A
       ELSE 
        LD E,(HL)       ;6
       ENDIF 
        INC HL
        LD (Psa_beg+1),HL

Psa_chn LD HL,0
        ADD HL,DE
       ADD HL,DE
        LD SP,HL
	if pt3_patchaddr
        pop de ;DE=adr_chn_A
        POP HL ;HL=adr_chn_B
        LD (Bch_adr+1),HL
        POP HL ;HL=adr_chn_C
        LD (Cch_adr+1),HL
	else
        POP HL
ia_pos0 LD BC,0
        ADD HL,BC
        EX DE,HL       ;DE=adr_chn_A
        POP HL
        ADD HL,BC      ;HL=adr_chn_B
        LD (Bch_adr+1),HL
        POP HL
        ADD HL,BC      ;HL=adr_chn_C
        LD (Cch_adr+1),HL
	endif
sav_SP2 LD SP,0
Ps_n0
        LD H,compile/256
        CALL An_clc1
        LD (Ach_adr+1),DE
Ai_qty  LD A,0
	ld (A_qty),A
AqtyQ  LD A,1
       DJNZ $+4
       LD A,34
       LD (A_gli),A
        EXX 
        LD A,B
	ld (A_ins2),A
        LD A,H
	ld (A_ins0),A
        LD A,E
	ld (A_ins3),A

play_0  LD L,0        ;smp_sl_env
        EXX 
N_add   LD IX,0        ;HX=noise LX=mix
        LD (sav_SP1+1),SP

;
        ifndef sam1
        MACRO sam1 channel
	if channel==1
Avb_lok
	endif
	if channel==2
Bvb_lok
	endif
	if channel==3
Cvb_lok
	endif

	LD A,-1
        LD H,A
        INC A

	if channel==1
        JP Z,Avb_n0
	endif
	if channel==2
        JP Z,Bvb_n0
	endif
	if channel==3
        JP Z,Cvb_n0
	endif

	if channel==1
As_adr
	endif
	if channel==2
Bs_adr
	endif
	if channel==3
Cs_adr
	endif
	LD SP,DUMMYSMP ;sample
        POP DE         ;loop E=beg D=end

curs_dsp=$
	if channel==1
As_dsp 
	endif
	if channel==2
Bs_dsp 
	endif
	if channel==3
Cs_dsp 
	endif
	LD A,0
        LD L,A
        INC A
        CP D
        JR C,$+3       ;bug NZ/C
        LD A,E
        LD (curs_dsp+1),A
        m4
        ADD HL,SP
        LD SP,HL
        POP DE ;D= Nm ts ns Tm v3 v2 v1 v0
        LD A,D ;E= sv +- N4 N3 N2 N1 N0 Em

       IF pt3_smpfix
        OR #F0
        LD L,A
        LD A,E
        ADD A,A

curs_Vsl=$
	if channel==1
As_Vsl
	endif
	if channel==2
Bs_Vsl
	endif
	if channel==3
Cs_Vsl
	endif
	LD A,16
        JR NC,1f ;\0s_VslG
        JP M,2f ;\0s_n2
        SUB 1
        JR $+4
2 ;\0s_n2
	CP 31
        ADC A,0
        LD (curs_Vsl+1),A
1 ;\0s_VslG
        ADD A,L
        JR C,$+3
        XOR A
        CP 16
        JR C,$+4
        LD A,15
       ELSE 
        AND 15
       ENDIF 

	if channel==1
Ag_vol 
	endif
	if channel==2
Bg_vol 
	endif
	if channel==3
Cg_vol 
	endif
	ADD A,#F0
        LD L,A
	ld H,tab_vol/256
        LD A,(HL)
        SRL E
        JR C,$+4
	if channel==1
Am_vol
	endif
	if channel==2
Bm_vol
	endif
	if channel==3
Cm_vol
	endif
	OR 0
        EXA 
        LD A,D
        RLCA 
        JR C,5f ;\0s_n5

       IF pt3_smpfix
curs_Nsl=$
	if channel==1
As_Nsl 
	endif
	if channel==2
Bs_Nsl 
	endif
	if channel==3
Cs_Nsl 
	endif
	LD A,0
        ADD A,E
        BIT 5,D
        JR Z,$+5
        LD (curs_Nsl+1),A
        LD HX,A
       ELSE 
        LD HX,E
       ENDIF 
        jp_ 7f ;\0s_n7

5 ;\0s_n5
	LD A,E
        AND 31
        CP 16
        JR C,$+4
        OR #F0

       IF pt3_smpfix
curs_Esl=$
	if channel==1
As_Esl 
	endif
	if channel==2
Bs_Esl 
	endif
	if channel==3
Cs_Esl 
	endif
	ADD A,0
        BIT 5,D
        JR Z,$+5
        LD (curs_Esl+1),A
       ENDIF 

       EXX 
       ADD A,L
       LD L,A
       EXX 
7 ;\0s_n7  
	LD A,D
        RRCA 
        RRCA 
        AND 36
        ENDM 
        ENDIF
; 

        ifndef sam2
        MACRO sam2 channel
        LD LX,A

       IF pt3_smpfix
        POP BC
curs_dtn=$
	if channel==1
As_dtn
	endif
	if channel==2
Bs_dtn
	endif
	if channel==3
Cs_dtn
	endif
	LD HL,0        ;dsp_frq_smp
        ADD HL,BC
       BIT 6,D
        JR Z,$+5
        LD (curs_dtn+1),HL
        EX DE,HL
       ELSE 
        POP DE
       ENDIF 

	if channel==1
Ao_adr
	endif
	if channel==2
Bo_adr
	endif
	if channel==3
Co_adr
	endif
	LD SP,DUMMYORN ;ornament
        POP BC         ;loop C=beg B=end
curo_dsp=$
	if channel==1
Ao_dsp
	endif
	if channel==2
Bo_dsp
	endif
	if channel==3
Co_dsp
	endif
	LD A,0
        LD L,A
        INC A
        CP B
        JR C,$+3       ;bug NZ/C
        LD A,C
        LD (curo_dsp+1),A
        LD H,0
        ADD HL,SP

       IF pt3_port3
	if channel==1
        LD A,(As_note+1)
	endif
	if channel==2
        LD A,(Bs_note+1)
	endif
	if channel==3
        LD A,(Cs_note+1)
	endif
       ELSE 
	if channel==1
As_note
	endif
	if channel==2
Bs_note
	endif
	if channel==3
Cs_note
	endif
	LD A,0
       ENDIF 

        ADD A,(HL)     ;dsp_orn
        ADD A,A
     JR NC,$+3
     XOR A
        LD L,A
	ld H,tab_frq/256
        LD SP,HL
        POP HL
        ADD HL,DE

cursl_dsp=$
	if channel==1
Asl_dsp
	endif
	if channel==2
Bsl_dsp
	endif
	if channel==3
Csl_dsp
	endif
	LD BC,0
        ADD HL,BC
	if channel==1
        LD (frq_A),HL
	endif
	if channel==2
        LD (frq_B),HL
	endif
	if channel==3
        LD (frq_C),HL
	endif

	if channel==1
Asl_stp
	endif
	if channel==2
Bsl_stp
	endif
	if channel==3
Csl_stp
	endif
	LD A,0
        DEC A
        JP M,0f ;\0sl_s0
        JR NZ,1f; \0sl_s1
	if channel==1
Asl_tfr
	endif
	if channel==2
Bsl_tfr
	endif
	if channel==3
Csl_tfr
	endif
	LD DE,0
       ADD HL,DE
       EX DE,HL
        ADD HL,BC
        LD (cursl_dsp+1),HL

       IF pt3_port3
        JR 2f ;\0sl_sts     ;port off/on
	if channel==1
Asl_plk
Asl_not
	endif
	if channel==2
Bsl_plk
Bsl_not
	endif
	if channel==3
Csl_plk
Csl_not
	endif
	LD A,0
        LD L,A
        ADD HL,HL
        LD H,tab_frq/256
        LD SP,HL
        POP HL
curs_note=$
	if channel==1
As_note
	endif
	if channel==2
Bs_note
	endif
	if channel==3
Cs_note
	endif
	CP 0
        JR NC,$+3
        EX DE,HL
        SBC HL,DE
        JR C,2f ;\0sl_sts ;BUG if the portamento step exceeds the distance between notes?
        LD (curs_note+1),A
        XOR A
        LD L,A
        LD (cursl_dsp+1),HL
        JR 1f ;$+4
       ENDIF 

2 ; \0sl_sts
	if channel==1
Asl_sts
	endif
	if channel==2
Bsl_sts
	endif
	if channel==3
Csl_sts
	endif
	LD A,0
1 ; \0sl_s1
	if channel==1
	LD (Asl_stp+1),A
	endif
	if channel==2
	LD (Bsl_stp+1),A
	endif
	if channel==3
	LD (Csl_stp+1),A
	endif
0 ; \0sl_s0
	EXA 

	if channel==1
Avb_n0 LD (vol_A),A
	endif
	if channel==2
Bvb_n0 LD (vol_B),A
	endif
	if channel==3
Cvb_n0 LD (vol_C),A
	endif

       IF pt3_vib6
curvb_stp=$
	if channel==1
Avb_stp
	endif
	if channel==2
Bvb_stp
	endif
	if channel==3
Cvb_stp
	endif
	LD A,0
        DEC A
        JP M,9f ;\0smpQ
        JR NZ,1f ;\0vb_n1
        DEC A           ;;
	if channel==1
        LD HL,Avb_lok+1;;
	endif
	if channel==2
        LD HL,Bvb_lok+1;;
	endif
	if channel==3
        LD HL,Cvb_lok+1;;
	endif
        XOR (HL)        ;;
        LD (HL),A       ;;4
	if channel==1
Avb_frq
	endif
	if channel==2
Bvb_frq
	endif
	if channel==3
Cvb_frq
	endif
	LD A,0
        JR NZ,1f ;$+4
	if channel==1
Avb_sts
	endif
	if channel==2
Bvb_sts
	endif
	if channel==3
Cvb_sts
	endif
	LD A,0
1; \0vb_n1
	LD (curvb_stp+1),A
       ENDIF 
9; \0smpQ
        ENDM 
        ENDIF 
;

	sam1 1
        RRCA 
        RRCA 
	sam2 1

	sam1 2
        RRCA 
        OR LX
	sam2 2

	sam1 3
        OR LX
	sam2 3

;---- sampler_E ----
Esl_    EXX 
        LD A,L
Esl_frq LD DE,0        ;stp_sl_env
E_frq   LD HL,0        ;frq_env
        ADD A,L        ;stp_smp_sl_env
        LD L,A
        ADD HL,DE
        LD (frq_E),HL
Esl_stp LD A,0
        DEC A
        JP M,sav_SP1
        JR NZ,E_n0
Esl_sds LD HL,0
        ADD HL,DE
        LD (Esl_frq+1),HL
Esl_sts LD A,0
E_n0    LD (Esl_stp+1),A

;---- out_to_processor ----

sav_SP1 LD SP,0
out_
       IF pt3_msx
        LD DE,#A0A1
        LD HL,frq_A
       DUP 6
        LD C,D
        OUT (C),L
        LD C,E
        OUTI 
       EDUP 
        LD C,D
        OUT (C),L
        LD A,HX        ;noise
        LD (N_add+3),A
        ADD A,(HL) ;;4
        OUT (#A1),A
        INC L
        OUT (C),L
       LD A,LX        ;mix TODO (HL)
       AND %00111111
       OR %10000000 ;portA=input, portB=output
        OUT (#A1),A
        INC L
        OUT (C),L
        LD C,E
        OUTI 
       DUP 4
        LD C,D
        OUT (C),L
        LD C,E
        OUTI 
       EDUP 
        LD A,(HL) ;;4
        OR A
        JR Z,int_qty
        LD C,D
        OUT (C),L
        OUT (#A1),A
        LD (HL),0 ;;4
       ELSE ;~msx
        LD DE,#FFBF
        LD BC,-3
        LD HL,frq_A
       DUP 6
        OUT (C),L
        LD B,E
        OUTI 
        LD B,D
       EDUP 
        OUT (C),L
        LD A,HX        ;noise
        LD (N_add+3),A
        ADD A,(HL) ;;4
        LD B,E
        OUT (C),A
        INC L
        LD B,D
        OUT (C),L
       LD A,LX        ;mix TODO (HL)
        LD B,E
        OUT (C),A
        INC L
       DUP 5
        LD B,D
        OUT (C),L
        LD B,E
        OUTI 
       EDUP 
        LD A,(HL) ;;4
        OR A
        JR Z,int_qty
        LD B,D
        OUT (C),L
        LD B,E
        OUT (C),A
        LD (HL),0 ;;4
       ENDIF 

;--- calc_next_positon_channels ---
int_qty LD A,3
        DEC A
        LD (int_qty+1),A
        JR Z,notaC;if tmp=1,CALL notaB first!!!
        DEC A
        RET NZ

;--- nota_B ---
       LD HL,B_qty
       DEC (HL)
        LD DE,#618
        LD H,E
      ;LD L,1 ;E
        LD B,E
        EXX 
        JR NZ,BqtyQ
      ;LD (bvol+2),A
Bch_adr LD DE,0
        LD H,compile/256
        CALL Bn_clc1
        LD (Bch_adr+1),DE
Bi_qty  LD A,0
	ld (B_qty),A
BqtyQ  LD A,1
       DJNZ $+4
       LD A,34
       LD (B_gli),A
        EXX 
        LD A,B
	ld (B_ins2),A
        LD A,H
	ld (B_ins0),A
       ;LD A,L
       ;ld (B_ins1),A
        LD A,E
	ld (B_ins3),A
        RET 

;--- nota_C ---
notaC
       INC A;LD A,1
        LD (PLAY),A
       LD HL,C_qty
       DEC (HL)
        RET NZ
Cch_adr LD DE,0
        LD H,compile/256
        CALL Cn_clc1
        LD (Cch_adr+1),DE
Ci_qty  LD A,0
	ld (C_qty),A
       DEC B
       RET NZ
        LD H,B
	ld L,B
        LD (Csl_dsp+1),HL
        RET 

;
        ifndef not_
        MACRO not_ channel

       IF (channel)!=3
	if channel==1
Ani_v0
	else
Bni_v0
	endif
	LD A,47
52 ;\0nivolU
	if channel==1
	LD (Avb_cpl),A
	else
	LD (Bvb_cpl),A
	endif
        EXX 
        LD H,D
        EXX 
        RET 

	if channel==1
Ani_not
	else
Bni_not
	endif
	if channel==1
	LD (Asi_not+1),A
	else
	LD (Bsi_not+1),A
	endif
        XOR A
       LD B,1
       jp_ 52b ;\0nivolU
       ENDIF 

5 ;\0ni_vol
	if channel==1
	JR Z,Ani_v0
	endif
	if channel==2
	JR Z,Bni_v0
	endif
	if channel==3
	JR Z,Cni_v0
	endif
        DUP 4
        ADD A,A
        EDUP 
	if channel==1
        LD (Agi_vol+1),A
	endif
	if channel==2
        LD (Bgi_vol+1),A
	endif
	if channel==3
        LD (Cgi_vol+1),A
	endif
        jp_ 2f ;\0n_clc2

6 ;\0ni_vqe
	JR Z,51f ;\0ni_v1
        DEC A
        JR NZ,61f ;\0ni_e0
        LD A,(DE)
        INC DE
	if channel==1
        LD (Ai_qty+1),A
	endif
	if channel==2
        LD (Bi_qty+1),A
	endif
	if channel==3
        LD (Ci_qty+1),A
	endif
        jp_ 2f ;\0n_clc2

61 ;\0ni_e0
	LD (Ei_form+1),A
        LD A,(DE)
        INC DE
        LD (Ei_frq+2),A
        LD A,(DE)
        INC DE
        LD (Ei_frq+1),A
        LD A,16
51 ;\0ni_v1
	if channel==1
	LD (Ami_vol+1),A
	endif
	if channel==2
	LD (Bmi_vol+1),A
	endif
	if channel==3
	LD (Cmi_vol+1),A
	endif
       IF (channel)!=3
        EXX 
        LD B,D
        EXX 
       ENDIF 
       jp_ 2f ;\0n_clc2

3 ;\0ni_s_o
	ADD A,A
        ADD A,tba_orn&#ff
        LD L,A
        LD C,(HL)
        INC L
        LD B,(HL)
	if channel==1
        LD (Aoi_adr+1),BC
	endif
	if channel==2
        LD (Boi_adr+1),BC
	endif
	if channel==3
        LD (Coi_adr+1),BC
	endif
        XOR A
12 ;\0ni_sm0
	if channel==1
	LD (Ami_vol+1),A
	endif
	if channel==2
	LD (Bmi_vol+1),A
	endif
	if channel==3
	LD (Cmi_vol+1),A
	endif
       IF (channel)!=3
        EXX 
        LD B,D
        EXX 
       ELSE 
        XOR A
        LD (Co_dsp+1),A
       ENDIF 
        LD A,(DE)
        INC DE
11 ;\0nism0U
	ADD A,tba_smp&#ff
        LD L,A
        LD C,(HL)
        INC L
        LD B,(HL)
	if channel==1
        LD (Asi_adr+1),BC
	endif
	if channel==2
        LD (Bsi_adr+1),BC
	endif
	if channel==3
        LD (Csi_adr+1),BC
	endif

1 ;\0n_clc1
	if channel==1
An_clc1
	endif
	if channel==2
Bn_clc1
	endif
	if channel==3
Cn_clc1
	endif
	LD BC,#1020
2 ;\0n_clc2
	LD A,(DE)
        INC DE
        ADD A,B
        JR C,3b ;\0ni_s_o
        ADD A,C
        JR C,4f ;\0ni_smp
        ADD A,B
        JR C,5b ;\0ni_vol
        ADD A,B
        JR C,6b ;\0ni_vqe
        ADD A,96
	if channel==1
        JR C,Ani_not
	endif
	if channel==2
        JR C,Bni_not
	endif
	if channel==3
        JR C,Cni_not
	endif
        ADD A,B
        JR C,8f ;\0ni_orn
       IF channel==2
        ADD A,C
        JR C,Bni_noi
        ADD A,B
       ELSE 
        ADD A,48
       ENDIF 
        JR C,10f ;\0ni_vse
        RLA 
	if channel==1
	display "A_eff=",A_eff
	display +(#ff00&A_eff)+((A_eff+30)&#ff)
        LD HL,+(#ff00&A_eff)+((A_eff+30)&#ff)
	endif
	if channel==2
	display "B_eff=",B_eff
	display +(#ff00&B_eff)+((B_eff+30)&#ff)
        LD HL,+(#ff00&B_eff)+((B_eff+30)&#ff)
	endif
	if channel==3
	display "C_eff=",C_eff
	display +(#ff00&C_eff)+((C_eff+30)&#ff)
        LD HL,+(#ff00&C_eff)+((C_eff+30)&#ff)
	endif
        ADD A,L
        LD L,A
        LD A,(HL)
        INC L
        LD H,(HL)
        LD L,A
        PUSH HL
       IF channel==3
       IF pt3_port3
       LD A,(Cs_note+1)
       LD (CnsTnxt+1),A
       ENDIF 
       ENDIF 
        jp_ 2b ;\0n_clc2
       IF channel==2
Bni_noi
	LD (Ni_frq+1),A;global_noise
        jp_ 2b ;\0n_clc2
       ENDIF 

10 ;\0ni_vse
	JR Z,12b ;\0ni_sm0
        LD (Ei_form+1),A
        LD A,(DE)
        INC DE
        LD (Ei_frq+2),A
        LD A,(DE)
        INC DE
        LD (Ei_frq+1),A
        LD A,16
        jp_ 12b ;\0ni_sm0

4 ;\0ni_smp
	RET Z
        ADD A,A
        jp_ 11b ;\0nism0U

8 ;\0ni_orn
	ADD A,A
        ADD A,tba_orn&#ff
        LD L,A
        LD C,(HL)
        INC L
        LD B,(HL)
	if channel==1
        LD (Aoi_adr+1),BC
	endif
	if channel==2
        LD (Boi_adr+1),BC
	endif
	if channel==3
        LD (Coi_adr+1),BC
	endif
       IF (channel)!=3
        EXX 
        LD B,D
        EXX 
       ELSE 
        XOR A
        LD (Co_dsp+1),A
       ENDIF 
        jp_ 1b ;\0n_clc1
        ENDM 
        ENDIF 
;

	not_ 1

	not_ 2

Cgi_vol=Cg_vol
Cmi_vol=Cm_vol
Coi_adr=Co_adr
Csi_adr=Cs_adr
Cni_v0
       IF pt3_vib6
        LD (Cvb_stp+1),A
       ENDIF 
        DEC A
        LD (Cvb_lok+1),A
        RET 

	not_ 3

Cni_not LD (Cs_note+1),A
        XOR A
        LD H,A,L,A
        LD (Cs_dsp+1),A
        LD (Co_dsp+1),A
        LD (Csl_stp+1),A
        LD (Cvb_lok+1),A
       IF pt3_vib6
        LD (Cvb_stp+1),A
       ENDIF 
       IF pt3_smpfix
        LD (Cs_Nsl+1),A
        LD (Cs_Esl+1),A
        LD (Cs_dtn+1),HL
       LD A,16
	ld (Cs_Vsl+1),A
       ENDIF 
       LD B,1
        RET 

;---- special_effects_COM.xxxx ----
Aef_dSm LD A,6
	ld (A_ins5),A
        l_ Asi_dsp+1
Aef_dOr LD A,6
	ld (A_ins6),A
        l_ Aoi_dsp+1
Bef_dSm LD A,6
	ld (B_ins5),A
        l_ Bsi_dsp+1
Bef_dOr LD A,6
	ld (B_ins6),A
        l_ Boi_dsp+1
Cef_dSm l_ Cs_dsp+1
Cef_dOr l_ Co_dsp+1

Cpi_stp=Csl_stp
Cpi_tfr=Csl_tfr

        ifndef _slT
        MACRO _slT channel
	LD A,(DE)
        INC DE
	if channel==1
        LD (Api_stp+1),A
	endif
	if channel==2
        LD (Bpi_stp+1),A
	endif
	if channel==3
        LD (Cpi_stp+1),A
	endif
       IF channel==3
        LD (Csl_sts+1),A
       ENDIF 
        LD A,(DE),L,A
        INC DE
        LD A,(DE),H,A
        INC DE
	if channel==1
        LD (Api_tfr+1),HL
	endif
	if channel==2
        LD (Bpi_tfr+1),HL
	endif
	if channel==3
        LD (Cpi_tfr+1),HL
	endif
       IF pt3_port3
	if channel==1
        LD A,Asl_sts-Asl_plk
        LD (Api_lok+1),A;port_off
	endif
	if channel==2
        LD A,Bsl_sts-Bsl_plk
        LD (Bpi_lok+1),A;port_off
	endif
	if channel==3
        LD A,Csl_sts-Csl_plk
        LD (Cpi_lok+1),A;port_off
	endif
       ENDIF 
        ENDM 
        ENDIF 

Aef_slT
	_slT 1
        EXX 
        LD E,D
        EXX 
        RET 

Bef_slT
	_slT 2
        EXX 
        LD E,D
        EXX 
        RET 

Cef_slT
	_slT 3
       IF pt3_vib6
        XOR A
        LD (Cvb_stp+1),A
       ENDIF 
        RET 

       IF pt3_port3
        ifndef _nsT
        MACRO _nsT channel
	LD A,(DE)
        INC DE
	if channel==1
        LD (Api_stp+1),A
	endif
	if channel==2
        LD (Bpi_stp+1),A
	endif
	if channel==3
        LD (Cpi_stp+1),A
	endif
       IF channel==3
        LD (Csl_sts+1),A
       ENDIF 
        INC DE
        INC DE
        LD A,(DE)
	ld L,A
        INC DE
        LD A,(DE)
	ld H,A
        INC DE
	if channel==1
        LD (Api_tfr+1),HL
	endif
	if channel==2
        LD (Bpi_tfr+1),HL
	endif
	if channel==3
        LD (Cpi_tfr+1),HL
	endif
        XOR A
       LD B,A
        ENDM 
        ENDIF 

Aef_nsT
	_nsT 1
        LD (Api_lok+1),A;port_on
      ; LD HL,Asi_not+1
      ; LD A,(HL)
       LD A,(Asi_not+1)
        LD (Api_not+1),A
      ;LD A,(As_note+1)
      ; LD (HL),A
        EXX 
        LD E,D
        EXX 
        RET 

Bef_nsT
	_nsT 2
        LD (Bpi_lok+1),A
      ; LD HL,Bsi_not+1
      ; LD A,(HL)
       LD A,(Bsi_not+1)
        LD (Bpi_not+1),A
      ;LD A,(Bs_note+1)
      ; LD (HL),A
        EXX 
        LD E,D
        EXX 
        RET 

Cef_nsT
	_nsT 3
       IF pt3_vib6
        LD (Cvb_stp+1),A
       ENDIF 
        LD (Csl_plk-1),A
        LD HL,Cs_note+1
        LD A,(HL)
        LD (Csl_not+1),A
CnsTnxt LD (HL),0
        RET 
      ENDIF 

eff_tmp
	l_ temp+1

       IF pt3_frq
	esld_

MAKEfrq
;Roshin
        INC HL
        EX DE,HL
        ADD HL,BC
        LD A,(DE)
       LD (COPF3),A
        INC DE
        LD A,(DE)
        ADD A,C
        LD C,A
        ADC A,B
        SUB C
        LD B,A
        PUSH BC
        LD DE,tab_frq
        PUSH DE
        LD B,12
COPF1   PUSH BC
        LD C,(HL)
        INC HL
        PUSH HL
        LD B,(HL)
        PUSH DE
        EX DE,HL
        LD DE,23
        LD HX,8
COPF2   SRL B
        RR C
;COPF3   NOP ;AND A
        LD A,C
COPF3   ADC A,D ;=ADC 0
        LD (HL),A
        INC HL
        LD A,B
        ADC A,D
        LD (HL),A
        ADD HL,DE
        DEC HX
        JR NZ,COPF2
        POP DE
        INC DE,DE
        POP HL
        INC HL
        POP BC
        DJNZ COPF1

        POP HL
        POP DE

        ;LD A,E
        ;CP TCOLD_1
        ;JR NZ,CORR_1
        ;LD A,#FD
	;ld (FrTab+#2E),A
CORR_1  LD A,(DE)
        AND A
        RET Z
        INC DE
        RRA 
       PUSH HL
        PUSH AF
        ADD A,A
        LD C,A
        ADD HL,BC
        POP AF
        INC (HL)
        JR NC,$+4
        DEC (HL)
	dec (HL)
       POP HL
       ;AND A
       ;SBC HL,BC
        JR CORR_1

NT_DATA
        DB T_NEW_0-T_
       ADC A,D
        DB TCNEW_0-T_
       ;DB T_OLD_0-T_
       ;AND A
       ;DB TCOLD_0-T_

        DB T_NEW_1-T_
        AND A
        DB TCNEW_1-T_
       ;DB T_OLD_1-T_
       ;AND A
       ;DB TCOLD_1-T_

        DB T_NEW_2-T_
       ADC A,D
        DB TCNEW_2-T_
       ;DB T_OLD_2-T_
       ;NOP
       ;DB TCOLD_2-T_

        DB T_NEW_3-T_
       ADC A,D
        DB TCNEW_3-T_
       ;DB T_OLD_3-T_
       ;NOP
       ;DB TCOLD_3-T_
T_
;COLD_0 DB #00+1,#04+1,#08+1,#0A+1,#0C+1,#0E+1,#12+1,#14+1
;       DB #18+1,#24+1,#3C+1,0
TCNEW_1
TCOLD_1 DB #5C+1,0
;COLD_2 DB #30+1,#36+1,#4C+1,#52+1,#5E+1,#70+1,#82,#8C,#9C
;       DB #9E,#A0,#A6,#A8,#AA,#AC,#AE,#AE,0
TCNEW_3 DB #56+1
TCOLD_3 DB #1E+1,#22+1,#24+1,#28+1,#2C+1,#2E+1,#32+1,#BE+1,0
TCNEW_0 DB #1C+1,#20+1,#22+1,#26+1,#2A+1,#2C+1,#30+1,#54+1
        DB #BC+1,#BE+1,0
TCNEW_2 DB #1A+1,#20+1,#24+1,#28+1,#2A+1,#3A+1,#4C+1,#5E+1
        DB #BA+1,#BC+1,#BE+1,0

;first 12 values of tone tables

T_NEW_1
T_OLD_1 DW #0EF8*2,#0E10*2,#0D60*2,#0C80*2,#0BD8*2,#0B28*2
        DW #0A88*2,#09F0*2,#0960*2,#08E0*2,#0858*2,#07E0*2
;_OLD_2 DW #0D3E*2,#0C80*2,#0BCC*2,#0B22*2,#0A82*2,#09EC*2
;       DW #095C*2,#08D6*2,#0858*2,#07E0*2,#076E*2,#0704*2
T_NEW_3
T_OLD_3 DW #0CDA*2,#0C22*2,#0B73*2,#0ACF*2,#0A33*2,#09A1*2
        DW #0917*2,#0894*2,#0819*2,#07A4*2,#0737*2,#06CF*2
T_OLD_0=T_OLD_3+2
T_NEW_0=T_OLD_3+2
        DW #066D*2
T_NEW_2 DW #0D10*2,#0C55*2,#0BA4*2,#0AFC*2,#0A5F*2,#09CA*2
        DW #093D*2,#08B8*2,#083B*2,#07C5*2,#0755*2,#06EC*2
       ENDIF 

	DISPLAY $-compile
       IF pt3_vib6
Cvb_ifr=Cvb_frq
Cvb_ist=Cvb_sts
       ENDIF 
       IF pt3_port3
Cpi_lok=Csl_plk-2
       ENDIF 

