START:
    MTPS #0
    MOV #2,@#102
    MOV #102,@#100
    MOV #5400, @#177662
    MOV PC,R1
    ADD #PT3FILE-.,R1
    CALL INIT_ONLOAD
0:  WAIT
    CALL PLAY
    BR 0

INIT_ONLOAD:
    MOV PC,R0
TAGADDR:        
    MOV PC,R2
    ADD #SPCCOMS-.,R2
    MOV #32,R3
0:  ADD R0,(R2)+
    SOB R3,0
INIT_NEWSONG:
    MOV R1,MODULE_ADDRESS
    MOV R1,R5
    MOVB 100.(R5),DELAY
    ADD #200.,R1
    MOV R1,CURRENTPOSITION
    MOVB 102.(R5),R2
    ADD R2,R1
    INC R1
    MOV R1,LOOPPOSITION
    CLR R1
    BISB 104.(R5),R1
    SWAB R1
    BISB 103.(R5),R1
    ADD R5,R1
    MOV R1,PATTERNSPOINTER
    MOV #169.,R1
    ADD R5,R1
    MOV R1,ORNAMENTSPOINTERS
    MOV #105.,R1
    ADD R5,R1
    MOV R1,SAMPLESPOINTERS
    BICB #200,SETUP
    PUSH    R5
    MOV PC,R4
    ADD #T_PACK-.,R4
    MOV PC,R5
    ADD #T1_-.+98.,R5
    MOV #4,R3
    MOV #12.,R2
3$: MOV (R4)+,R0
    ASL R0
    BR 1$
2$: CLR R1
    BISB (R4)+,R1
    ADD R1,R0
    ADD R1,R0
1$: MOV R0,-(R5)
    SOB R2,2$
    MOVB (R4)+,R2
    INC R4
    BIC #1,R4
    SOB R3,3$
    POP R5
    MOV PC,R1
    ADD #VAR0START-.,R1
    MOV #VAR0END-VAR0START,R3
4$: CLRB (R1)+
    SOB R3,4$

    MOVB #1,DELAYCOUNTER
    MOV #0xF001,R0
    MOV EMPTYSAMORNADDR,R2
    MOV CHANNEL_AADDR,R4
    MOV (PC)+,R3
    .BYTE AY_TONA,AY_AMPLITUDEA
    CALL FILLSET
    MOV (PC)+,R3
    .BYTE AY_TONB,AY_AMPLITUDEB
    CALL FILLSET
    MOV (PC)+,R3
    .BYTE AY_TONC,AY_AMPLITUDEC
    CALL FILLSET

    MOVB 13.(R5),R0
    SUB #60,R0
    BCS 20$
    CMPB R0,#10.
    BLO 21$
20$:
    MOV #6,R0
21$:
    MOVB R0,VERSION         
    PUSH R0
    CMPB R0,#4
    MOVB 99.(R5),R0
    ROLB R0
    BICB #177770,R0

NOTETABLECREATOR:
    MOV R1, -(SP)
    MOV PC,R1      
    ADD #NT_DATA-.,R1
    ADD R0,R1
    ADD R0,R1
    ADD R0,R1
    CLR R2
    BISB (R1)+,R2
    MOV (PC)+,R0
    NOP
    TSTB (R1)+
    BEQ 11$
    MOV (PC)+,R0
    CLC
11$:
    MOV R0,M2
    CLR R3
    BISB (R1),R3
    ADD T_ADDR,R3
    ADD (SP)+,R2
    PUSH R3
    MOV NT_ADDR,R1
    PUSH R1
    MOV #12.,R4
13$:
    MOV (R2)+,R3
    PUSH R1
    MOV #8.,R5
12$:
    CLC
  	ROR R3
    CALL M2
    MOV R3,R0
    ADC R0
    MOV R0,(R1)
    ADD #24.,R1
    SOB R5,12$
    POP R1
    TST (R1)+
    SOB R4,13$
    POP R2
    POP R1
    MOV PC,R0
    ADD #TCOLD_1-.,R0
    CMP R1,R0
    BNE 16$
    MOVB #0xFD,NT_-TCOLD_1+56(R0)
16$:
    CLR R0
    BISB (R1)+,R0
    BEQ 14$
    CLR R5
    RORB R0
    ROL R5
    ASLB R0
    ADD R0,R2
    TST R5
    BEQ 15$
    DEC (R2)
    DEC (R2)
15$:
    INC (R2)
    SUB R0,R2
    BR 16$
14$:
    POP R0

VOLTABLECREATOR:
    MOV #21,R3
    CLR R1
    CMPB R0,#5
    MOV (PC)+,R0
    ASLB R0
    BHIS 1$
    DEC R3
    MOV R3,R1
    MOV (PC)+,R0
    NOP
1$:
    MOV R0,M2
    MOV VT_ADDR,R4
    MOV R4,R5
    MOV #16.,R2
    ADD R2,R5
    MOV #256.,R0
2$:
    CLRB (R4)+
    SOB R0,2$
5$:
    PUSH R3
    ADD R3,R1
    MOV #0,R3
    SBC R3
4$:
    MOVB R3,R0
    MOV R3,R4
    CLRB R4
    SWAB R4
    CALL M2
    ADCB R4
    MOVB R4,(R5)+
    ADD R1,R3
    INC R2
    MOV R2,R0
    BIC #177760,R0
    BNE 4$
    POP R3
    CMP R1,#119.
    BNE 3$
    INC R1
3$:
    TSTB R2
    BNE 5$
    JMP     REGOUT
M2:
    NOP
    RET

PLAY:	
    CLR ADDTOENVELOPE
    MOV CHANNEL_AADDR,R5
    CLRB AYREGS-CHANNEL_A+AY_MIXER(R5)
    MOVB #-1, AYREGS-CHANNEL_A+AY_ENVELOPETYPE(R5)
    DECB DELAYCOUNTER
    BGT 0PL2
    DECB CHP_NOTE_SKIP_COUNTER(R5)
    BNE 0PL1B
    MOV CHP_ADDRESS_IN_PATTERN(R5),R3
    TSTB (R3)
    BNE 0PL1A
    CLRB NOISE_BASE
    MOV CURRENTPOSITION,R0
 		INC R0
    MOVB (R0),R1
    CMPB #377,R1
    BNE 0PLNLP
    CALL CHECKLP
    MOV LOOPPOSITION,R0
    MOVB (R0),R1
0PLNLP:
    MOV R0,CURRENTPOSITION
    BIC #177400,R1
    ASL R1
    ADD @PATTERNSPOINTERADDR,R1
    MOV MODULE_ADDRESS,R2
    CLR R3
    BISB (R1)+,R3
    SWAB R3
    BISB (R1)+,R3
    SWAB R3
    ADD R2,R3
    CLR R0
    BISB (R1)+,R0
    SWAB R0
    BISB (R1)+,R0
    SWAB R0
    ADD R2,R0
    MOV R0,CHANNEL_B-CHANNEL_A+CHP_ADDRESS_IN_PATTERN(R5)
    CLR R0
    BISB (R1)+,R0
    SWAB R0
    BISB (R1)+,R0
    SWAB R0
    ADD R2,R0
    MOV R0,CHANNEL_C-CHANNEL_A+CHP_ADDRESS_IN_PATTERN(R5)
0PL1A:
    CALL PATTERNINTERPRETER
    MOV R3,CHP_ADDRESS_IN_PATTERN(R5)
0PL1B:
    ADD #CHP,R5
    DECB CHP_NOTE_SKIP_COUNTER(R5)
    BNE 0PL1C
    MOV CHP_ADDRESS_IN_PATTERN(R5),R3
    CALL PATTERNINTERPRETER
    MOV R3,CHP_ADDRESS_IN_PATTERN(R5)
0PL1C:
    ADD #CHP,R5
    DECB CHP_NOTE_SKIP_COUNTER(R5)
    BNE 0PL1D
    MOV CHP_ADDRESS_IN_PATTERN(R5),R3
    CALL PATTERNINTERPRETER
    MOV R3,CHP_ADDRESS_IN_PATTERN(R5)
0PL1D:
    MOVB DELAY,DELAYCOUNTER
0PL2:
    MOV CHANNEL_AADDR,R5
    CALL CHANGEREGISTERS
    CALL CHANGEREGISTERS
    CALL CHANGEREGISTERS

    MOVB NOISE_BASE,R0
    ADD ADDTONOISE,R0
    MOVB R0,AY_NOISE(R5)
    MOV ENVELOPE_BASE,R0
    ADD ADDTOENVELOPE,R0
    ADD CUR_ENV_SLIDE,R0
    MOVB R0,AY_ENVELOPE(R5)
    SWAB R0
    MOVB R0,AY_ENVELOPE+1(R5)
    MOV PC,R0
    ADD #CUR_ENV_DELAY-.,R0
    TSTB (R0)
    BEQ REGOUT
    DECB (R0)
    BNE REGOUT
    MOVB ENV_DELAY,(R0)
    ADD ENV_SLIDE_ADD,CUR_ENV_SLIDE
REGOUT:
    MOV #177714, R5
    MOV #362, R1
    MOV PC,R4
    ADD #AYREGS-.+AY_ENVELOPETYPE,R4
    MOVB (R4),R0
    BMI 1$
    MOV R1,(R5)
    COMB R0
    MOVB R0,(R5)
1$:
    INC R1
2$:
    MOV R1,(R5)
    MOVB -(R4),R0
    COMB R0
    MOVB R0,(R5)
    INCB R1
    BMI 2$
    RET

PD_ORSM:
    CLRB CHP_ENVELOPE_ENABLED(R5)
    CALL SETORNAMENT
    MOVB (R3)+,R0
    BR PD_SAM_
PD_SAM:
    SUB #0xD0,R0
    ASL R0
PD_SAM_:
    ADD @SAMPLESPOINTERSADDR,R0
    CLR R1
    BISB (R0)+,R1
    SWAB R1
    BISB (R0),R1
    SWAB R1
    TST R1
    BEQ 3$
    ADD MODULE_ADDRESS,R1
    MOV R1,CHP_SAMPLEPOINTER(R5)
3$:
    BR PD_LOOP

PD_VOL:
    BIC #0xFFF0,R0
    ASL R0
    ASL R0
    ASL R0
    ASL R0
    MOVB R0,CHP_VOLUME(R5)
    BR PD_LOOP

PD_EOFF:
    CLRB CHP_ENVELOPE_ENABLED(R5)
    CLRB CHP_POSITION_IN_ORNAMENT(R5)
    BR PD_LOOP

PD_SORE:
    BIC #0xFFF0,R0
    DECB R0
    BNE PD_ENV
    MOVB (R3)+,CHP_NUMBER_OF_NOTES_TO_SKIP(R5)
    BR PD_LOOP

PD_ENV:
    CALL SETENVELOPE
    CLRB CHP_POSITION_IN_ORNAMENT(R5)
    BR PD_LOOP

PD_ORN:
    CALL SETORNAMENT
    BR PD_LOOP

PD_ESAM:
    CLRB CHP_ENVELOPE_ENABLED(R5)
    BIC #0xFFF0,R0
    BEQ 1$
    CALL SETENVELOPE
1$:
    CLRB CHP_POSITION_IN_ORNAMENT(R5)
    MOVB (R3)+,R0
    BR PD_SAM_

PATTERNINTERPRETER:
  			MOV     CHP_NOTE(R5), PRNOTE
  			MOV     CHP_CURRENT_TON_SLIDING(R5), PRSLIDING

PD_LOOP: 	CLR     R0
  			BISB    (R3)+, R0
  			CMPB    R0, #0xF0
  			BHIS    PD_ORSM         
  			CMPB    R0, #0xD0
  			BEQ     PD_FIN          
  			BHI     PD_SAM          
  			CMPB    R0, #0xC0
  			BEQ     PD_REL          
  			BHI     PD_VOL
  			CMPB    R0, #0xB0
  			BEQ     PD_EOFF         
  			BHI     PD_SORE         
          
  			CMPB    R0, #0x50
  			BHIS    PD_NOTE         
  			CMPB    R0, #0x40
  			BHIS    PD_ORN          
  			CMPB    R0, #0x20
  			BHIS    PD_NOIS        
  			CMPB    R0, #0x10
  			BHIS    PD_ESAM        
         
  			ASL     R0
        ADD SPCCOMSADDR,R0
        MOV     (R0), -(SP)  
  			BR      PD_LOOP

PD_NOIS:
        BIC     #0xFFE0, R0
  			MOVB    R0, NOISE_BASE
  			BR      PD_LOOP

PD_REL: 
        CLRB    CHP_ENABLED(R5)
  			BR      PD_RES

PD_NOTE:
        SUB     #0x50, R0
  			MOV     R0, CHP_NOTE(R5)
  			MOVB    #-1, CHP_ENABLED(R5)

PD_RES: 
        MOV     R5, R0
  			CLR     (R0)+   
  			CLR     (R0)+   
  			CLR     (R0)+   
  			CLR     (R0)+   
  			CLR     (R0)+   
  			CLR     (R0)    

PD_FIN: 
        MOVB    CHP_NUMBER_OF_NOTES_TO_SKIP(R5), CHP_NOTE_SKIP_COUNTER(R5)
  			RET

C_PORTM:
        CLRB    CHP_SIMPLEGLISS(R5)
  			MOVB    (R3)+, R0
 			  INC     R3
  			INC     R3
  			MOVB    R0, CHP_TON_SLIDE_DELAY(R5)
  			MOVB    R0, CHP_TON_SLIDE_COUNT(R5)
  			MOV     CHP_NOTE(R5), R2
  			MOV     R2, CHP_SLIDE_TO_NOTE(R5)
  			ASL     R2
        ADD   NT_ADDR,R2
        MOV   (R2),R2
  			MOV     PRNOTE, R1
  			MOV     R1, CHP_NOTE(R5)
  			ASL     R1
        ADD   NT_ADDR,R1
        MOV     (R1), R1
  			SUB     R1, R2
  			MOV     R2, CHP_TON_DELTA(R5)   
  			MOV     CHP_CURRENT_TON_SLIDING(R5), R1
  			CMPB    VERSION, #6
  			BLO     OLDPRTM         
  			MOV     PRSLIDING, R1
  			MOV     R1, CHP_CURRENT_TON_SLIDING(R5)
OLDPRTM:
        CLR     R0
  			BISB    (R3)+, R0       
  			SWAB    R0
  			BISB    (R3)+, R0       
  			SWAB    R0
  			TST     R0              
  			BPL     1$
  			NEG     R0
1$: 		SUB     R1, R2          
  			BPL     2$              
  			NEG     R0
2$: 		MOV     R0, CHP_TON_SLIDE_STEP(R5)
  			CLRB    CHP_CURRENT_ONOFF(R5)
  			RET

C_GLISS:
        MOVB    (PC), CHP_SIMPLEGLISS(R5)
  			MOVB    (R3)+, R0
  			MOVB    R0, CHP_TON_SLIDE_DELAY(R5)
  			BNE     GL36
 
  			CMPB    VERSION, #7     
  			BLO     GL36
  			INCB    R0  
GL36: 	MOVB    R0, CHP_TON_SLIDE_COUNT(R5)
  			MOVB    (R3)+, CHP_TON_SLIDE_STEP(R5) 
  			MOVB    (R3)+, CHP_TON_SLIDE_STEP + 1(R5) 
  			CLRB    CHP_CURRENT_ONOFF(R5)
  			RET

C_SMPOS:
        MOVB    (R3)+, CHP_POSITION_IN_SAMPLE(R5)
  			RET

C_ORPOS:
        MOVB    (R3)+, CHP_POSITION_IN_ORNAMENT(R5)
  			RET

C_VIBRT:
        MOVB    (R3), CHP_ONOFF_DELAY(R5)
  			MOVB    (R3)+, CHP_CURRENT_ONOFF(R5)
  			MOVB    (R3)+, CHP_OFFON_DELAY(R5)
  			CLRB    CHP_TON_SLIDE_COUNT(R5)
  			CLR     CHP_CURRENT_TON_SLIDING(R5)
  			RET

C_ENGLS:
        MOV PC,R0
        ADD #ENV_DELAY-.,R0
        MOVB    (R3), (R0)
        MOVB    (R3)+, CUR_ENV_DELAY - ENV_DELAY(R0)
  			MOVB    (R3)+, ENV_SLIDE_ADD - ENV_DELAY(R0)
  			MOVB    (R3)+, ENV_SLIDE_ADD - ENV_DELAY + 1(R0)
  			RET

C_DELAY:
        MOVB    (R3)+, DELAY
  			RET

SETENVELOPE:
        MOVB    #0x10, CHP_ENVELOPE_ENABLED(R5)
        MOV AYREGSADDR,R4
  			MOVB    R0, AY_ENVELOPETYPE(R4)
  			MOVB    (R3)+, ENVELOPE_BASE1    
  			MOVB    (R3)+, ENVELOPE_BASE        
  			CLRB    CUR_ENV_DELAY
  			CLR     CUR_ENV_SLIDE
C_NOP:  RET

SETORNAMENT:
        BIC     #0xFFF0, R0
  			ASL     R0
  			ADD     @ORNAMENTSPOINTERSADDR, R0
  			CLR     R1
  			BISB    (R0)+, R1
  			SWAB    R1
  			BISB    (R0), R1
  			SWAB    R1
  			TST     R1
  			BNE     1$
  			MOV EMPTYSAMORNADDR,R1 
  			BR      2$
1$: 		ADD     MODULE_ADDRESS, R1
2$: 		MOV     R1, CHP_ORNAMENTPOINTER(R5)
  			CLRB    CHP_POSITION_IN_ORNAMENT(R5)
  			RET

SPCCOMS:
        .WORD   C_NOP-TAGADDR
  			.WORD   C_GLISS-TAGADDR
  			.WORD   C_PORTM-TAGADDR
  			.WORD   C_SMPOS-TAGADDR
  			.WORD   C_ORPOS-TAGADDR
  			.WORD   C_VIBRT-TAGADDR
  			.WORD   C_NOP-TAGADDR
  			.WORD   C_NOP-TAGADDR
  			.WORD   C_ENGLS-TAGADDR
  			.WORD   C_DELAY-TAGADDR
  			.WORD   C_NOP-TAGADDR
  			.WORD   C_NOP-TAGADDR
  			.WORD   C_NOP-TAGADDR
  			.WORD   C_NOP-TAGADDR
  			.WORD   C_NOP-TAGADDR
  			.WORD   C_NOP-TAGADDR
AYREGSADDR:
        .WORD   AYREGS-TAGADDR
T_ADDR:
        .WORD   T_-TAGADDR        
NT_ADDR:
        .WORD   NT_-TAGADDR
EMPTYSAMORNADDR:
        .WORD   EMPTYSAMORN-TAGADDR
SPCCOMSADDR:
        .WORD   SPCCOMS-TAGADDR
SAMPLESPOINTERSADDR:
        .WORD   SAMPLESPOINTERS-TAGADDR
PATTERNSPOINTERADDR:
        .WORD   PATTERNSPOINTER-TAGADDR
ORNAMENTSPOINTERSADDR:
        .WORD   ORNAMENTSPOINTERS-TAGADDR
CHANNEL_AADDR:
        .WORD   CHANNEL_A-TAGADDR
VT_ADDR:
        .WORD   VT_-TAGADDR

CHANGEREGISTERS:
  			CLR     R1  
  			TSTB    CHP_ENABLED(R5)
  			BEQ     EX$0
  			MOV     CHP_ORNAMENTPOINTER(R5), R1 
  			MOVB    (R1)+, R4       
  			MOVB    (R1)+, R3       
  			MOVB    CHP_POSITION_IN_ORNAMENT(R5), R0    
  			ADD     R0, R1
  			INCB    R0
  			CMPB    R0, R3
  			BLO     CH_ORPS
  			MOVB    R4, R0
CH_ORPS:
        MOVB    R0, CHP_POSITION_IN_ORNAMENT(R5)
  			MOV     CHP_NOTE(R5), R0    
  			MOVB    (R1), R1    
  			ADD     R1, R0
  			BPL     CH_NTP
  			CLR     R0
CH_NTP: CMP     R0, #96.
  			BLO     CH_NOK
  			MOV     #95., R0
CH_NOK: ASL     R0
  			MOV     R0, -(SP)   
  			MOV     CHP_SAMPLEPOINTER(R5), R1   
  			MOVB    (R1)+, R4       
  			MOVB    (R1)+, R3       
  			MOVB    CHP_POSITION_IN_SAMPLE(R5), R0  
  			ADD     R0, R1
  			ADD     R0, R1
  			ADD     R0, R1
  			ADD     R0, R1
  			INCB    R0
  			CMPB    R0, R3
  			BLO     CH_SMPS
  			MOVB    R4, R0
CH_SMPS:
        MOVB    R0, CHP_POSITION_IN_SAMPLE(R5)
  			MOVB    (R1)+, R3   
  			MOVB    (R1)+, R4   
  			CLR     R2
  			BISB    (R1)+, R2   
  			SWAB    R2
  			BISB    (R1), R2
  			SWAB    R2
  			ADD     CHP_TON_ACCUMULATOR(R5), R2
  			BITB    #0x40, R4   
  			BEQ     CH_NOAC
  			MOV     R2, CHP_TON_ACCUMULATOR(R5)
CH_NOAC:
        MOV     (SP)+, R1   
        ADD NT_ADDR,R1
  			ADD     (R1), R2 
  			MOV     CHP_CURRENT_TON_SLIDING(R5), R1 
  			ADD     R1, R2      
  			BIC     #0xF000, R2
  			MOVB    CHP_TONE_REG(R5), R0
        ADD AYREGSADDR,R0
        MOV     R2, (R0)      
  			TSTB    CHP_TON_SLIDE_COUNT(R5)
  			BEQ     CH_AMP
  			DECB    CHP_TON_SLIDE_COUNT(R5)
  			BNE     CH_AMP
  			MOVB    CHP_TON_SLIDE_DELAY(R5), CHP_TON_SLIDE_COUNT(R5)
  			MOV     CHP_TON_SLIDE_STEP(R5), R2
  			ADD     R2, R1  
  			MOV     R1, CHP_CURRENT_TON_SLIDING(R5)
  			TSTB    CHP_SIMPLEGLISS(R5) 
  			BNE     CH_AMP
  			MOV     CHP_TON_DELTA(R5), R0
  			TST     R2          
  			BPL     PP1$
  			CMP     R1, R0
  			BLE     PP2$
  			BR      CH_AMP
EX$0:		BR      CH_EXIT
PP1$:		CMP     R1, R0
  			BLT     CH_AMP
PP2$:		MOV     CHP_SLIDE_TO_NOTE(R5), CHP_NOTE(R5)
  			CLRB    CHP_TON_SLIDE_COUNT(R5)
  			CLR     CHP_CURRENT_TON_SLIDING(R5)
CH_AMP: MOVB    CHP_CURRENT_AMPLITUDE_SLIDING(R5), R0   
  			BITB    #0x80, R3       
  			BEQ     CH_NOAM
  			BITB    #0x40, R3       
  			BEQ     CH_AMIN
  			CMPB    R0, #15.            
  			BEQ     CH_NOAM
  			INCB    R0              
  			BR      CH_SVAM
CH_AMIN:
        CMPB    R0, #-15.       
  			BEQ     CH_NOAM
  			DECB    R0              
CH_SVAM:
        MOVB    R0, CHP_CURRENT_AMPLITUDE_SLIDING(R5)
CH_NOAM:
        MOV     R4, R1          
  			BIC     #0xFFF0, R1     
  			ADD     R1, R0          
  			BPL     CH_APOS
  			CLR     R0
CH_APOS:
        CMP     R0, #16.
  			BLO     CH_VOL
  			MOV     #15., R0            
CH_VOL:	BISB    CHP_VOLUME(R5), R0  
        ADD VT_ADDR,R0
        MOVB    (R0), R1     
  			BITB    #1, R3          
  			BNE     CH_NOEN
  			BISB    CHP_ENVELOPE_ENABLED(R5), R1    
CH_NOEN:        
  			MOV     R3, R0
  			ASR     R0
  			BIC     #0xFFE0, R0     
  			BITB    #0x80, R4       
  			BEQ     NO_ENSL
  			BIT     #0x10, R0       
  			BEQ     3$              
  			BIS     #0xFFE0, R0     
3$: 		ADD     CHP_CURRENT_ENVELOPE_SLIDING(R5), R0
  			BITB    #0x20, R4       
  			BEQ     NO_ENAC
  			MOV     R0, CHP_CURRENT_ENVELOPE_SLIDING(R5)
NO_ENAC:
        ADD     R0, ADDTOENVELOPE
  			BR      CH_MIX
NO_ENSL:
        MOVB    CHP_CURRENT_NOISE_SLIDING(R5), R3   
  			ADD     R3, R0
  			MOV     R0, ADDTONOISE
  			BITB    #0x20, R4       
  			BEQ     CH_MIX
  			MOVB    R0, CHP_CURRENT_NOISE_SLIDING(R5)
CH_MIX: ASR     R4  
  			BIC     #177667, R4
  			MOVB R4,-(SP)
  			MOV AYREGSADDR,R4
  			BISB    (SP)+, AY_MIXER(R4)
CH_EXIT:  
        MOVB    CHP_AMPL_REG(R5), R0
        ADD AYREGSADDR,R0
        MOVB    R1, (R0)
  			ASRB    AY_MIXER(R4)
  			TSTB    CHP_CURRENT_ONOFF(R5)
  			BEQ     RET0$
  			DECB    CHP_CURRENT_ONOFF(R5)
  			BNE     RET0$
  			MOVB    CHP_ONOFF_DELAY(R5), R0
  			COMB    CHP_ENABLED(R5)
  			BNE     CH_ONDL
  			MOVB    CHP_OFFON_DELAY(R5), R0
CH_ONDL:
        MOVB    R0, CHP_CURRENT_ONOFF(R5)
RET0$: 	ADD     #CHP, R5
  			RET
CHECKLP:
        BISB    #200, SETUP
  			BITB    #1, SETUP
  			BNE     1$
  			RET
1$: 		TST     (SP)+
  			INCB    DELAYCOUNTER
  			MOV     CHANNEL_AADDR,R5
  			INCB    CHP_NOTE_SKIP_COUNTER(R5)
     		CLR     AYREGS - CHANNEL_A + AY_AMPLITUDEA(R5)
  			CLRB    AYREGS - CHANNEL_A  + AY_AMPLITUDEC(R5)
  			MOVB    #77, AYREGS - CHANNEL_A + AY_MIXER(R5)
  			JMP     REGOUT

FILLSET:
        MOV     R0, CHP_NOTE_SKIP_COUNTER(R4)
  			MOV     R2, CHP_ADDRESS_IN_PATTERN(R4)  
  			MOV     R2, CHP_ORNAMENTPOINTER(R4) 
  			MOV     R2, CHP_SAMPLEPOINTER(R4)   
  			MOV     R3, CHP_TONE_REG(R4)
  			ADD     #CHP, R4
  			RET

AY_TONA = 0
AY_TONB = 2
AY_TONC = 4
AY_NOISE = 6
AY_MIXER = 7
AY_AMPLITUDEA = 8.
AY_AMPLITUDEB = 9.
AY_AMPLITUDEC = 10.
AY_ENVELOPE = 11.
AY_ENVELOPETYPE = 13.

CHP_POSITION_IN_ORNAMENT = 0     
CHP_POSITION_IN_SAMPLE = 1     
CHP_CURRENT_AMPLITUDE_SLIDING= 2     
CHP_CURRENT_NOISE_SLIDING = 3     
CHP_CURRENT_ENVELOPE_SLIDING = 4     
CHP_CURRENT_TON_SLIDING = 6     
CHP_TON_ACCUMULATOR = 8.    
CHP_TON_SLIDE_COUNT = 10.   
CHP_CURRENT_ONOFF = 11.   
CHP_ONOFF_DELAY = 12.   
CHP_OFFON_DELAY = 13.   
CHP_ENVELOPE_ENABLED = 14.   
CHP_SIMPLEGLISS = 15.   
CHP_ENABLED = 16.   
CHP_ADDRESS_IN_PATTERN = 18.   
CHP_ORNAMENTPOINTER = 20.   
CHP_SAMPLEPOINTER = 22.   
CHP_SLIDE_TO_NOTE = 24.   
CHP_NOTE = 26.   
CHP_TON_SLIDE_STEP = 28.   
CHP_TON_DELTA = 30.   
CHP_NUMBER_OF_NOTES_TO_SKIP = 32.   
CHP_TON_SLIDE_DELAY = 33.   
CHP_NOTE_SKIP_COUNTER = 34.   
CHP_VOLUME = 35.   
CHP_TONE_REG = 36.   
CHP_AMPL_REG = 37.   
CHP = 38.

T_:

TCOLD_0:
 			      .BYTE 0x00 + 1, 0x04 + 1, 0x08 + 1, 0x0A + 1, 0x0C + 1, 0x0E + 1, 0x12 + 1, 0x14 + 1
 			      .BYTE 0x18 + 1, 0x24 + 1, 0x3C + 1, 0
TCNEW_1:
TCOLD_1: 	  .BYTE 0x5C + 1, 0
TCOLD_2: 	  .BYTE 0x30 + 1, 0x36 + 1, 0x4C + 1, 0x52 + 1, 0x5E + 1, 0x70 + 1, 0x82, 0x8C
 			      .BYTE 0x9C, 0x9E, 0xA0, 0xA6, 0xA8, 0xAA, 0xAC, 0xAE, 0xAE, 0

TCNEW_3: 	  .BYTE 0x56 + 1
TCOLD_3: 	  .BYTE 0x1E + 1, 0x22 + 1, 0x24 + 1, 0x28 + 1, 0x2C + 1, 0x2E + 1, 0x32 + 1, 0xBE + 1, 0

TCNEW_0: 	  .BYTE 0x1C + 1, 0x20 + 1, 0x22 + 1, 0x26 + 1, 0x2A + 1, 0x2C + 1, 0x30 + 1, 0x54 + 1
 			      .BYTE 0xBC + 1, 0xBE + 1, 0

TCNEW_2: 	  .BYTE 0x1A + 1, 0x20 + 1, 0x24 + 1, 0x28 + 1, 0x2A + 1, 0x3A + 1, 0x4C + 1, 0x5E + 1
 			      .BYTE 0xBA + 1, 0xBC + 1, 0xBE + 1, 0

EMPTYSAMORN: 
 			      .BYTE 0, 1, 0, 0x90, 0, 0 
 			      .EVEN
T_PACK: .WORD 0x06EC
 			  .BYTE 0x0755-0x06EC
        .BYTE 0x07C5-0x0755
        .BYTE 0x083B-0x07C5
  			.BYTE 0x08B8-0x083B
  			.BYTE 0x093D-0x08B8
  			.BYTE 0x09CA-0x093D
  			.BYTE 0x0A5F-0x09CA
  			.BYTE 0x0AFC-0x0A5F
  			.BYTE 0x0BA4-0x0AFC
  			.BYTE 0x0C55-0x0BA4
  			.BYTE 0x0D10-0x0C55
  			.BYTE 13.   
  			.EVEN
  			.WORD 0x066D
  			.BYTE 0x06CF-0x066D
  			.BYTE 0x0737-0x06CF
  			.BYTE 0x07A4-0x0737
  			.BYTE 0x0819-0x07A4
  			.BYTE 0x0894-0x0819
  			.BYTE 0x0917-0x0894
  			.BYTE 0x09A1-0x0917
  			.BYTE 0x0A33-0x09A1
  			.BYTE 0x0ACF-0x0A33
  			.BYTE 0x0B73-0x0ACF
  			.BYTE 0x0C22-0x0B73
  			.BYTE 0x0CDA-0x0C22
  			.BYTE 12.   
  			.EVEN
  			.WORD 0x0704
  			.BYTE 0x076E-0x0704
  			.BYTE 0x07E0-0x076E
  			.BYTE 0x0858-0x07E0
  			.BYTE 0x08D6-0x0858
  			.BYTE 0x095C-0x08D6
  			.BYTE 0x09EC-0x095C
  			.BYTE 0x0A82-0x09EC
  			.BYTE 0x0B22-0x0A82
  			.BYTE 0x0BCC-0x0B22
  			.BYTE 0x0C80-0x0BCC
  			.BYTE 0x0D3E-0x0C80
  			.BYTE 12.   
  			.EVEN
  			.WORD 0x07E0
  			.BYTE 0x0858-0x07E0
  			.BYTE 0x08E0-0x0858
  			.BYTE 0x0960-0x08E0
  			.BYTE 0x09F0-0x0960
  			.BYTE 0x0A88-0x09F0
  			.BYTE 0x0B28-0x0A88
  			.BYTE 0x0BD8-0x0B28
  			.BYTE 0x0C80-0x0BD8
  			.BYTE 0x0D60-0x0C80
  			.BYTE 0x0E10-0x0D60
  			.BYTE 0x0EF8-0x0E10
  			.EVEN

SETUP:        .BYTE 0 
  
VERSION:      .BYTE 0
DELAY:        .BYTE 0 
ENV_DELAY:    .BYTE 0
 			        .EVEN
MODULE_ADDRESS:         .WORD 0 
SAMPLESPOINTERS:        .WORD 0 
ORNAMENTSPOINTERS:      .WORD 0 
PATTERNSPOINTER:        .WORD 0 
LOOPPOSITION:           .WORD 0 
CURRENTPOSITION:        .WORD 0 

VAR0START:  

PRNOTE:					        .WORD 0 
PRSLIDING:              .WORD 0 
ADDTOENVELOPE:          .WORD 0 
ENV_SLIDE_ADD:          .WORD 0
CUR_ENV_SLIDE:          .WORD 0
ADDTONOISE:             .WORD 0
DELAYCOUNTER:           .BYTE 0 
CUR_ENV_DELAY:          .BYTE 0
NOISE_BASE:             .BYTE 0
                        .EVEN

CHANNEL_A:              .BLKB   CHP
CHANNEL_B:              .BLKB   CHP
CHANNEL_C:              .BLKB   CHP

AYREGS:
VT_:                    .BLKB   14.
ENVELOPE_BASE:          .BYTE	0
ENVELOPE_BASE1:         .BYTE	0
VAR0END:
T_NEW_1:
T_OLD_1:
T1_:                    .BYTE	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
T_OLD_2:                .BYTE 	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
T_NEW_3:
T_OLD_3:                .BYTE	0,0   
T_NEW_0:
T_OLD_0:                .BYTE	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
T_NEW_2:
                        .BLKB   166. 
  
NT_DATA:                .BYTE   T_NEW_0-T1_, 0
                        .BYTE   TCNEW_0-T_      
                        .BYTE   T_OLD_0-T1_, 1
                        .BYTE   TCOLD_0-T_
                        .BYTE   T_NEW_1-T1_, 1
                        .BYTE   TCNEW_1-T_
                        .BYTE   T_OLD_1-T1_, 1
                        .BYTE   TCOLD_1-T_
                        .BYTE   T_NEW_2-T1_, 0
                        .BYTE   TCNEW_2-T_
                        .BYTE   T_OLD_2-T1_, 0
                        .BYTE   TCOLD_2-T_
                        .BYTE   T_NEW_3-T1_, 0
                        .BYTE   TCNEW_3-T_
                        .BYTE   T_OLD_3-T1_, 0
                        .BYTE   TCOLD_3-T_
                        .EVEN

NT_:                    .BLKW   96. 

PT3FILE:

    INSERT_FILE "2018_EA_demosong.pt3"
