/**
	\file miscAsm.s
	
*/
#include <vs1005h.h>
#include <vstypes.h>
#include "../miscAsm.h"


#ifdef ASM

/*
void SwapLR(register __i0 s_int16 *data, register __c0 s_int16 wordsPerSample, register __a0 s_int16 stereoSamples);
*/

	.sect code,SwapLR
	.export _SwapLR
_SwapLR:
	add a0,ones,a0;	ldx (i6)+1,null
	stx le,(I6)+1;	sty ls,(I6)
	jns $9
	add c0,ones,c0;	sty lc,(I6)
	nop
	jzs $2
	nop
	
	loop a0,$3-1			// 32-bit path: only handle 16 LSbs
	ldx (i0)+1,null

	ldx (i0)+2,a0
	ldx (i0)-2,i7
	stx i7,(i0)+2
	stx a0,(i0)+2
$3:
	j $9
	nop

$2:
	loop a0,$1-1			// 16-bit path
	nop

	ldx (i0)+1,a0
	ldx (i0)-1,i7
	stx i7,(i0)+1
	stx a0,(i0)+1
$1:

$9:
	ldy (I6)-1,lc
	jr
	ldx (I6)-1,le;	ldy (I6),ls


/*
void Monoize(register __i0 s_int16 *data, register __c0 s_int16 wordsPerSample, register __a0 s_int16 stereoSamples);
*/

	.sect code,Monoize
	.export _Monoize
_Monoize:
	add a0,ones,a0;	ldx (i6)+1,null
	stx le,(I6)+1;	sty ls,(I6)
	stx b0,(i6)+1;	sty lc,(I6)
	jns $9
	stx a1,(i6);	sty b1,(I6)

	add c0,ones,c0
	ldx (i0)+1,a1
	jzs $2
	nop

	ldx (i0)+2,a1
	loop a0,$3-1			// 32-bit path: only handle 16 LSbs
	asr a1,a0;	ldx (i0)+2,b1

	asr b1,b0;	ldx (i0)+2,a1
	addc a0,b0,a0;	ldx (i0)-6,b1
	stx a0,(i0)+2
	asr a1,a0;	stx a0,(i0)+6
$3:
	j $9
	nop

$2:
	loop a0,$1-1			// 16-bit path
	asr a1,a0;	ldx (i0)+1,b1

	asr b1,b0;	ldx (i0)+1,a1
	addc a0,b0,a0;	ldx (i0)-3,b1
	stx a0,(i0)+1
	asr a1,a0;	stx a0,(i0)+3
$1:

$9:
	ldx (i6)-1,a1; ldy (I6),b1
	ldx (i6)-1,b0; ldy (I6),lc
	jr
	ldx (I6)-1,le;	ldy (I6),ls


/*
void InvertCh(register __i0 s_int16 *data, register __c0 s_int16 wordsPerSample,
     register __a0 s_int16 stereoSamples);
*/

	.sect code,InvertCh
	.export _InvertCh
_InvertCh:
	add a0,ones,a0;	ldx (i6)+1,null
	stx le,(I6)+1;	sty ls,(I6)
	jns $9
	stx b0,(i6);	sty lc,(I6)

	add c0,ones,c0
	nop
	jzs $2
	ldc MR_SAT|MR_INT,mr0

	ldx (i0)+1,null; ldy (i6)+1,null
	sty i1,(i6);	ldx (i0)+4,b0

	loop a0,$3-1			// 32-bit path: only handle 16 LSbs
	ldc 8,i1

	sub null,b0,a0; ldx (i0)-4,b0
	stx a0,(i0)*

$3:
	j $9
	ldx (i6)-1,i1
	
$2:
	loop a0,$1-1			// 16-bit path
	ldx (i0)+2,b0

	sub null,b0,a0; ldx (i0)-2,b0
	stx a0,(i0)+4
$1:
	ldc MR_INT,mr0

$9:
	ldx (i6)-1,b0; ldy (I6),lc
	jr
	ldx (I6)-1,le;	ldy (I6),ls

#endif /* ASM */

	.end



#if 0
	/********* EVERYTHING FROM NOW ON IS PURELY DECORATIONAL!!! **********/

/*
	void Generic(void) - 2018 edition
*/
	.sect code,Generic
	.export _Generic
_Generic:
	ldx (i6)+1,null
	stx i0,(I6)+1;	sty i1,(I6)
	stx i2,(I6)+1;	sty i3,(I6)
	stx le,(I6)+1;	sty ls,(I6)
	stx lr0,(I6)+1;	sty lc,(I6)
	stx a0,(I6)+1;	sty a1,(I6)
	stx b0,(I6)+1;	sty b1,(I6)
	stx c0,(I6)+1;	sty c1,(I6)
	stx d0,(I6)+1;	sty d1,(I6)
	stx i4,(I6)

	ldx (I6)-1,i4
	ldx (I6)-1,d0;	ldy (I6),d1
	ldx (I6)-1,c0;	ldy (I6),c1
	ldx (I6)-1,b0;	ldy (I6),b1
	ldx (I6)-1,a0;	ldy (I6),a1
	ldx (I6)-1,lr0;	ldy (I6),lc
	ldx (I6)-1,le;	ldy (I6),ls
	ldx (I6)-1,i2;	ldy (I6),i3
	jr
	ldx (I6)-1,i0;	ldy (I6),i1
#endif
