;*******************************************************************************
; C H A M E L E O N   DSP Assembler file                                       *
;*******************************************************************************
; Copyright (C) 2001-2002 Soundart                                             *
; www.soundart-hot.com                                                         *
; support@soundart-hot.com                                                     *
;*******************************************************************************
; Modified by Aleix Riera
; aleix@fjarre.com
; www.fjarre.com/~aleix
;*******************************************************************************
;
; MODULE: Addklim (ONLY 1 SAMPLE / BUFFER_SIZE)
;	This block of code adds a constant value to the input buffer:
;
;	output[i] = input[i] + K
;
;	ADDED: Compare the result value with a constant,
;	and verify the condition:
;		result>CONSTANT => OK
;		else		=> result=CONSTANT
;	NOTE: This is made to avoid values near 0 (to LPF filter),
;	and negative values.
;
; ARGUMENTS:
;	(X)   = In		- Input sample (Y)
;	(X+1) = Out		- Output sample (Y)
;	(X+2) = Constant	- Constant to add
;	(X+3) = Constant	- Constant to compare
;
;******************************************************************************
module_addklim:
	define	R_In			'R5'
	define	R_Out			'R1'

;===============================CODE=====================================

	MOVE	X:(R_X)+,R_In		; (X)   = In (Y)
	MOVE	X:(R_X)+,R_Out		; (X+1) = Out (Y)
	MOVE	X:(R_X)+,X0		; (X+2) = Constant
	MOVE	X:(R_X)+,Y1		; (X+3) = Constant (CMP)

	TFR	X0,A		Y:(R_In),Y0
	
	;.LOOP	#BUFFER_SIZE
	;----------------------------------------------------------------
	  ADD	Y0,A		Y:(R_In)+,Y0
	  NOP
	  CMP	Y1,A
	  TFR	Y1,A	IFLE
	  NOP
	  TFR	X0,A		A,Y:(R_Out)+
	;----------------------------------------------------------------
	;.ENDL

;===============================CODE=====================================

	undef	R_In
	undef	R_Out

	RTS