;******************************
;*
;* EeffectECommands 
;*
;* r0 = current channel struct
;* r6 = mode struct
;*	
	
EEffectSetGlissControl		;not supported
EEffectSetVibratoControl	;only sinus	
EEffectSetTremoloControl	;only sinus	
EEffectKarplusStrong		;no pt
EEffectFilterOn			;nothing to do, its an amiga paula hardware filter effect
		
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* EEffectNoteDelay
;*
;* r0 = current channel struct
;* r6 = mode struct
;*
;*
	
EEffectRetrigNote	;e9	

	push	r1
	push	r2
	push	r3
	push	r6
	push	r7
	
	;
	
	rqldi	r0,channelStCmd 	;get effect (cmd)
	rqldi	r6,modTickCounter
	movei	r3,$f
	ld	r1		;effect
	ld	r2		;tick counter
			
	and	r1,r1,r3	;000Y	

	;
	
	nop
	cmpeqi	r1,0	;y = 0
	brts	EEffectRetrigNoteCmdZero	;nothing to do
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;
	
	udivinit	4	;5 bit division (-1)
	udivsdd		r2	;divident = tick counter
	udivsdv		r1	;divider  = y
	
	udivstep	;5	
	udivstep        ;4
	udivstep        ;3
	udivstep        ;2
	udivstep        ;1 bits
	
	udivgr		r1	;get remainder	= tick counter % y
	nop
	
	cmpeqi	r1,0			;remainder == 0 -> update
	gpci	r7,2
	brts	modUpdateChannel	; yes -> update channel
	or	r6,r0,r0		;channel struct	delay slot
	nop				;delay slot
	nop				;delay slot
	nop				;delay slot
	
	;
	
EEffectRetrigNoteCmdZero	
	
	rqpop
	rqpop
	rqpop
	pop	r7
	pop	r6	
	pop	r3
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r2	;delay slot
	pop	r1	;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* EEffectNoteDelay
;*
;* r0 = current channel struct
;* r6 = mode struct
;*
	
EEffectNoteDelay	;ed

	push	r1
	push	r2
	push	r3
	push	r6
	push	r7
	
	;
	
	rqldi	r0,channelStCmd 	;get effect (cmd)
	rqldi	r6,modTickCounter
	movei	r3,$f
	ld	r1		;effect
	ld	r2		;tick counter
			
	and	r1,r1,r3	;000Y	
	
	nop
	cmpeq	r1,r2			;tick counter == y
	gpci	r7,2
	brts	modUpdateChannel	; yes -> update channel
	or	r6,r0,r0		;channel struct	delay slot
	nop				;delay slot
	nop				;delay slot
	nop				;delay slot
	
	;
	
	rqpop
	rqpop
	rqpop
	pop	r7
	pop	r6	
	pop	r3
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r2	;delay slot
	pop	r1	;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* EEffectPatternDelay
;*
;* r0 = current channel struct
;* r6 = mode struct
;*	

EEffectPatternDelay

	push	r1
	push	r2
	push	r3
	
	;
	
	rqldi	r0,channelStCmd 	;get effect (cmd)
	rqldi	r6,modTickCounter
	movei	r3,$f
	ld	r1		;effect
	ld	r2		;tick counter
			
	and	r1,r1,r3	;000Y	
	
	cmpeqi	r2,0		;tick counter == 0
	brtc	EEffectPatternDelayTickNotZero	;no
	addi	r1,1		;Y + 1		delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;
	
	ssto	modPatDelayTime	;store pat delay time
	stwo	r6,r1
	

EEffectPatternDelayTickNotZero	
	
	;
	
	rqpop
	rqpop
	rqpop
	pop	r3
	pop	r2	
	pop	r1	
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot

;******************************
;*
;* EEffectJumpLoop
;*
;* r0 = current channel struct
;* r6 = mode struct
;*
	
EEffectJumpLoop		;e6	

	push	r1
	push	r2
	push	r3
	
	;
	
	rqldi	r6,modTickCounter
	rqldi	r0,channelStCmd 	;get effect (cmd)
	rqldi	r6,modCurrentRowPositionStore
	ld	r2	;tick counter
	ld	r1	;effect
	ld	r3	;row position stored
	
	cmpeqi	r2,0	
	brtc	EEffectJumpLoopTickNotZero
	movei	r2,$f		;	delay slot
	nop			;	delay slot
	and	r1,r1,r2	;000Y 	delay slot
	nop			;	delay slot

	; UInt16 cmd = (UInt16)effect_y(currentChannel.n_cmd);

	cmpeqi	r1,0		;set loop position (y == 0)
	brtc	EEffectJumpLoopDoLoop
	rqldi	r0,channelStLoopCount		;	delay slot
	nop					;	delay slot
	nop					;	delay slot
	ld	r2				;r2 = loop count	delay slot
				
	ssto	channelStLoopPos
	stwo	r0,r3			;
	
	br	EEffectJumpLoopTickNotZero
	nop			;	delay slot
	nop			;	delay slot
	nop			;	delay slot
	nop			;	delay slot
	
               
		
EEffectJumpLoopDoLoop	

	nop	;r2 = loop count
	subqi	r3,r2,1		;r3 = loop count - 1
	cmpeqi	r2,0		;            loopCount == 0 ?
	movets	r2,r1,r3	;loopCount =		      y : (loopCount-1)
	
	ssto	channelStLoopCount
	stwo	r0,r2		;store loop count back
	
	rqldi	r0,channelStLoopPos
	rqldi	r6,modCurrentRowPosition
	nop
	ld	r3		;loop pos
	ld	r1		;current row position
	subi	r3,1
	
	
	cmplosi	r2,1		;
	movets	r1,r1,r3	
	
	ssto	modCurrentRowPosition
	stwo	r6,r1
	
EEffectJumpLoopTickNotZero	
	;
	
	rqpop
	rqpop
	rqpop
	pop	r3	
	pop	r2	
	pop	r1	
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* EEffectFinePortaUp
;*
;* r0 = current channel struct
;* r6 = mode struct
;*	

EEffectFinePortaUp

	push	r1
	push	r2
	push	r3
	push	r7
	
	;
	
	rqldi	r6,modTickCounter
	rqldi	r0,channelStCmd 	;get effect (cmd)
	rqldi	r0,channelStPeriode
	ld	r2	;tick counter
	ld	r1	;effect
	ld	r3	;periode
	
	cmpeqi	r2,0	
	brtc	EEffectFinePortaUpTickNotZero
	movei	r2,$f		;	delay slot
	nop			;	delay slot
	and	r1,r1,r2	;000Y 	delay slot
	nop			;	delay slot
	
	;
	
	dexti	$3
	movei	r2,$58
	add	r3,r3,r1	;periode += Y
	nop
	cmple	r3,r2		;	   (periode <= $358)
	movets	r3,r3,r2	;periode =		     ? periode : $358
	
	dump
	
	ssto	channelStPeriode
	stwo	r0,r3		;store periode back

	;set hardware periode
	
	push	r0
	push	r6

	dexti	>channelSetPeriode			
	movei	r2,channelSetPeriode
	gpci	r7,2				; r0 = value
	jmpi	r2,0                            ; r6 = channel struct
	or	r6,r0,r0	;channelstruct		delay slot
	or	r0,r3,r3	;value = periode	delay slot	
	nop			;delay slot
	nop			;delay slot
		
	rqpop
	rqpop
	nop
	pop	r6
	pop	r0
		
	;
	
EEffectFinePortaUpTickNotZero	
	
	rqpop
	nop
	nop
	pop	r7
	
	rqpop
	rqpop
	rqpop
	pop	r3	
	pop	r2	
	pop	r1	
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* EEffectFinePortaDown
;*
;* r0 = current channel struct
;* r6 = mode struct
;*	
	
	
EEffectFinePortaDown	

	push	r1
	push	r2
	push	r3
	push	r7
	
	;
	
	rqldi	r6,modTickCounter
	rqldi	r0,channelStCmd 	;get effect (cmd)
	rqldi	r0,channelStPeriode
	ld	r2	;tick counter
	ld	r1	;effect
	ld	r3	;periode
	
	cmpeqi	r2,0	
	brtc	EEffectFinePortaDownTickNotZero
	movei	r2,$f		;	delay slot
	nop			;	delay slot
	and	r1,r1,r2	;000Y 	delay slot
	nop			;	delay slot
	
	;
	
	movei	r2,$71
	sub	r3,r3,r1	;periode -= Y
	nop
	cmplos	r3,r2		;	   (periode < $71)
	movets	r3,r2,r3	;periode =		     ? $71 : periode 
	
	dump
	
	ssto	channelStPeriode
	stwo	r0,r3		;store periode back
	
	;set hardware periode
	
	push	r0
	push	r6

	dexti	>channelSetPeriode			
	movei	r2,channelSetPeriode
	gpci	r7,2				; r0 = value
	jmpi	r2,0                            ; r6 = channel struct
	or	r6,r0,r0	;channelstruct		delay slot
	or	r0,r3,r3	;value = periode	delay slot	
	nop			;delay slot
	nop			;delay slot
		
	rqpop
	rqpop
	nop
	pop	r6
	pop	r0
		
	;
	
EEffectFinePortaDownTickNotZero	
	
	rqpop
	nop
	nop
	pop	r7
	
	rqpop
	rqpop
	rqpop
	pop	r3	
	pop	r2	
	pop	r1	
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* EEffectSetFineTune
;*
;* r0 = current channel struct
;* r6 = mode struct
;*	

EEffectSetFineTune

	push	r1
	push	r2
	
	;
	
	rqldi	r0,channelStCmd 	;get effect (cmd)
	movei	r2,$f	
	nop
	ld	r1	;effect	
	nop				
	and	r1,r1,r2	;000Y 	
	
	ssto	channelStFineTune
	stwo	r0,r1		;finetune = Y

	;
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r2	;delay slot
	pop	r1	;delay slot
	nop		;delay slot
	nop		;delay slot

;******************************
;*
;* EEffectVolumeFineUp 
;*
;* r0 = current channel struct
;* r6 = mode struct
;*
	
EEffectVolumeFineUp		;a	

	push	r1
	push	r2
	
	;
	
	rqldi	r6,modTickCounter
	rqldi	r0,channelStCmd 	;get effect (cmd)
	nop
	ld	r2	;tick counter
	ld	r1	;effect
	
	cmpeqi	r2,0	
	brtc	EEffectVolumeFineUpTickNotZero
	movei	r2,$f		;	delay slot
	nop			;	delay slot
	and	r1,r1,r2	;000Y 	delay slot
	nop			;	delay slot
	
	rqldi	r0,channelStVolume
	nop
	nop
	ld	r2		;current volume
	nop
	
	add	r2,r2,r1	;current volume + y
	nop
	
	cmplosi	r2,0		;     (r2 < 0) (signed)
	bicts	r2,r2		;r2 =	      ? (r2 & ~ r2) : r2	;note: (r2 & ~ r2) = 0
	
	movei	r1,64
	cmploi	r2,65		;     (r2 < 65) (unsigned)
	movets	r2,r2,r1	;r2 =	       ? r2 : 64
	
	ssto	channelStVolume
	stwo	r0,r2		;store current volume
		
EEffectVolumeFineUpTickNotZero	
	
	;
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r2	;delay slot
	pop	r1	;delay slot
	nop		;delay slot
	nop		;delay slot

;******************************
;*
;* EEffectVolumeFineUp 
;*
;* r0 = current channel struct
;* r6 = mode struct
;*

EEffectVolumeFineDown		;b

	push	r1
	push	r2
	
	;
	
	rqldi	r6,modTickCounter
	rqldi	r0,channelStCmd 	;get effect (cmd)
	nop
	ld	r2	;tick counter
	ld	r1	;effect
	
	cmpeqi	r2,0	
	brtc	EEffectVolumeFineDownTickNotZero
	movei	r2,$f		;	delay slot
	nop			;	delay slot
	and	r1,r1,r2	;000Y 	delay slot
	nop			;	delay slot
	
	rqldi	r0,channelStVolume
	nop
	nop
	ld	r2		;current volume
	nop
	
	sub	r2,r2,r1	;current volume - y
	nop
	
	cmplosi	r2,0		;     (r2 < 0) (signed)
	bicts	r2,r2		;r2 =	      ? (r2 & ~ r2) : r2	;note: (r2 & ~ r2) = 0
	
	movei	r1,64
	cmploi	r2,65		;     (r2 < 65) (unsigned)
	movets	r2,r2,r1	;r2 =	       ? r2 : 64
	
	ssto	channelStVolume
	stwo	r0,r2		;store current volume
		
EEffectVolumeFineDownTickNotZero	
	
	;
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r2	;delay slot
	pop	r1	;delay slot
	nop		;delay slot
	nop		;delay slot

;******************************
;*
;* EEffectNoteCut 
;*
;* r0 = current channel struct
;* r6 = mode struct
;*

EEffectNoteCut	

	push	r1
	push	r2
	push	r3
	
	;
	
	rqldi	r0,channelStCmd 	;get effect (cmd)
	rqldi	r6,modTickCounter
	movei	r3,$f
	ld	r1		;effect
	ld	r2		;tick counter
			
	and	r1,r1,r3	;000Y	
	nop
	
	cmpeq	r2,r1		;tick counter == y
	brtc	EEffectNoteCutTickNotY	;no
	movei	r1,0		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;yes
	
	ssto	channelStVolume
	stwo	r0,r1		;clear volume

EEffectNoteCutTickNotY	
	
	;
	
	rqpop
	rqpop
	rqpop
	pop	r3
	pop	r2	
	pop	r1	
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot