summaryrefslogtreecommitdiffstats
path: root/firmware/asm
diff options
context:
space:
mode:
authorThomas Martitz <kugel@rockbox.org>2012-01-06 06:26:48 +0100
committerThomas Martitz <kugel@rockbox.org>2012-01-22 18:46:45 +0100
commit3c17f28eca86ff3ee9e7cef6c4d5198c27b7c03c (patch)
tree672ee79463fc3c20c186f6d34ebd88cbe430c662 /firmware/asm
parent991ae1e39553172a7dd6cd8c634aebfce892e261 (diff)
downloadrockbox-3c17f28eca86ff3ee9e7cef6c4d5198c27b7c03c.tar.gz
rockbox-3c17f28eca86ff3ee9e7cef6c4d5198c27b7c03c.tar.bz2
rockbox-3c17f28eca86ff3ee9e7cef6c4d5198c27b7c03c.zip
Move pcm_mixer helper routines to firmware/asm.
Diffstat (limited to 'firmware/asm')
-rw-r--r--firmware/asm/arm/pcm-mixer-armv4.c182
-rw-r--r--firmware/asm/arm/pcm-mixer-armv5.c106
-rw-r--r--firmware/asm/arm/pcm-mixer-armv6.c118
-rw-r--r--firmware/asm/arm/pcm-mixer.c7
-rw-r--r--firmware/asm/generic/pcm-mixer.c100
-rw-r--r--firmware/asm/m68k/pcm-mixer.c134
-rw-r--r--firmware/asm/pcm-mixer.c108
7 files changed, 755 insertions, 0 deletions
diff --git a/firmware/asm/arm/pcm-mixer-armv4.c b/firmware/asm/arm/pcm-mixer-armv4.c
new file mode 100644
index 0000000000..4818544d7b
--- /dev/null
+++ b/firmware/asm/arm/pcm-mixer-armv4.c
@@ -0,0 +1,182 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2011 by Michael Sevakis
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+
+#define MIXER_OPTIMIZED_WRITE_SAMPLES
+#define MIXER_OPTIMIZED_MIX_SAMPLES
+
+/* Mix channels' samples and apply gain factors */
+static FORCE_INLINE void mix_samples(void *out,
+ void *src0,
+ int32_t src0_amp,
+ void *src1,
+ int32_t src1_amp,
+ size_t size)
+{
+ if (src0_amp == MIX_AMP_UNITY && src1_amp == MIX_AMP_UNITY)
+ {
+ /* Both are unity amplitude */
+ int32_t l0, l1, h0, h1;
+ asm volatile (
+ "1: \n"
+ "ldrsh %4, [%1], #2 \n"
+ "ldrsh %5, [%2], #2 \n"
+ "ldrsh %6, [%1], #2 \n"
+ "ldrsh %7, [%2], #2 \n"
+ "add %4, %4, %5 \n"
+ "add %6, %6, %7 \n"
+ "mov %5, %4, asr #15 \n"
+ "teq %5, %5, asr #31 \n"
+ "eorne %4, %8, %4, asr #31 \n"
+ "mov %7, %6, asr #15 \n"
+ "teq %7, %7, asr #31 \n"
+ "eorne %6, %8, %6, asr #31 \n"
+ "subs %3, %3, #4 \n"
+ "and %4, %4, %8, lsr #16 \n"
+ "orr %6, %4, %6, lsl #16 \n"
+ "str %6, [%0], #4 \n"
+ "bhi 1b \n"
+ : "+r"(out), "+r"(src0), "+r"(src1), "+r"(size),
+ "=&r"(l0), "=&r"(l1), "=&r"(h0), "=&r"(h1)
+ : "r"(0xffff7fff));
+ }
+ else if (src0_amp != MIX_AMP_UNITY && src1_amp != MIX_AMP_UNITY)
+ {
+ /* Neither are unity amplitude */
+ int32_t l0, l1, h0, h1;
+ asm volatile (
+ "1: \n"
+ "ldrsh %4, [%1], #2 \n"
+ "ldrsh %5, [%2], #2 \n"
+ "ldrsh %6, [%1], #2 \n"
+ "ldrsh %7, [%2], #2 \n"
+ "mul %4, %8, %4 \n"
+ "mul %5, %9, %5 \n"
+ "mul %6, %8, %6 \n"
+ "mul %7, %9, %7 \n"
+ "mov %4, %4, asr #16 \n"
+ "add %4, %4, %5, asr #16 \n"
+ "mov %6, %6, asr #16 \n"
+ "add %6, %6, %7, asr #16 \n"
+ "mov %5, %4, asr #15 \n"
+ "teq %5, %5, asr #31 \n"
+ "eorne %4, %10, %4, asr #31 \n"
+ "mov %7, %6, asr #15 \n"
+ "teq %7, %7, asr #31 \n"
+ "eorne %6, %10, %6, asr #31 \n"
+ "subs %3, %3, #4 \n"
+ "and %4, %4, %10, lsr #16 \n"
+ "orr %6, %4, %6, lsl #16 \n"
+ "str %6, [%0], #4 \n"
+ "bhi 1b \n"
+ : "+r"(out), "+r"(src0), "+r"(src1), "+r"(size),
+ "=&r"(l0), "=&r"(l1), "=&r"(h0), "=&r"(h1)
+ : "r"(src0_amp), "r"(src1_amp), "r"(0xffff7fff));
+ }
+ else
+ {
+ /* One is unity amplitude */
+ if (src0_amp != MIX_AMP_UNITY)
+ {
+ /* Keep unity in src0, amp0 */
+ int16_t *src_tmp = src0;
+ src0 = src1;
+ src1 = src_tmp;
+ src1_amp = src0_amp;
+ src0_amp = MIX_AMP_UNITY;
+ }
+
+ int32_t l0, l1, h0, h1;
+ asm volatile (
+ "1: \n"
+ "ldrsh %4, [%1], #2 \n"
+ "ldrsh %5, [%2], #2 \n"
+ "ldrsh %6, [%1], #2 \n"
+ "ldrsh %7, [%2], #2 \n"
+ "mul %5, %8, %5 \n"
+ "mul %7, %8, %7 \n"
+ "add %4, %4, %5, asr #16 \n"
+ "add %6, %6, %7, asr #16 \n"
+ "mov %5, %4, asr #15 \n"
+ "teq %5, %5, asr #31 \n"
+ "eorne %4, %9, %4, asr #31 \n"
+ "mov %7, %6, asr #15 \n"
+ "teq %7, %7, asr #31 \n"
+ "eorne %6, %9, %6, asr #31 \n"
+ "subs %3, %3, #4 \n"
+ "and %4, %4, %9, lsr #16 \n"
+ "orr %6, %4, %6, lsl #16 \n"
+ "str %6, [%0], #4 \n"
+ "bhi 1b \n"
+ : "+r"(out), "+r"(src0), "+r"(src1), "+r"(size),
+ "=&r"(l0), "=&r"(l1), "=&r"(h0), "=&r"(h1)
+ : "r"(src1_amp), "r"(0xffff7fff));
+ }
+}
+
+/* Write channel's samples and apply gain factor */
+static FORCE_INLINE void write_samples(void *out,
+ void *src,
+ int32_t amp,
+ size_t size)
+{
+ if (LIKELY(amp == MIX_AMP_UNITY))
+ {
+ /* Channel is unity amplitude */
+ asm volatile (
+ "ands r1, %2, #0x1f \n"
+ "beq 2f \n"
+ "1: \n"
+ "ldr r0, [%1], #4 \n"
+ "subs r1, r1, #4 \n"
+ "str r0, [%0], #4 \n"
+ "bne 1b \n"
+ "bics %2, %2, #0x1f \n"
+ "beq 3f \n"
+ "2: \n"
+ "ldmia %1!, { r0-r7 } \n"
+ "subs %2, %2, #32 \n"
+ "stmia %0!, { r0-r7 } \n"
+ "bhi 2b \n"
+ "3: \n"
+ : "+r"(out), "+r"(src), "+r"(size)
+ :
+ : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7");
+ }
+ else
+ {
+ /* Channel needs amplitude cut */
+ uint32_t l, h;
+ asm volatile (
+ "1: \n"
+ "ldrsh %3, [%1], #2 \n"
+ "ldrsh %4, [%1], #2 \n"
+ "subs %2, %2, #4 \n"
+ "mul %3, %5, %3 \n"
+ "mul %4, %5, %4 \n"
+ "and %4, %4, %6, lsl #16 \n"
+ "orr %4, %4, %3, lsr #16 \n"
+ "str %4, [%0], #4 \n"
+ "bhi 1b \n"
+ : "+r"(out), "+r"(src), "+r"(size),
+ "=&r"(l), "=&r"(h)
+ : "r"(amp), "r"(0xffffffffu));
+ }
+}
diff --git a/firmware/asm/arm/pcm-mixer-armv5.c b/firmware/asm/arm/pcm-mixer-armv5.c
new file mode 100644
index 0000000000..64f2c86f52
--- /dev/null
+++ b/firmware/asm/arm/pcm-mixer-armv5.c
@@ -0,0 +1,106 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2011 by Michael Sevakis
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+
+#define MIXER_OPTIMIZED_WRITE_SAMPLES
+#define MIXER_OPTIMIZED_MIX_SAMPLES
+
+/* Mix channels' samples and apply gain factors */
+static FORCE_INLINE void mix_samples(void *out,
+ void *src0,
+ int32_t src0_amp,
+ void *src1,
+ int32_t src1_amp,
+ size_t size)
+{
+ int32_t s0, s1, tmp;
+ asm volatile (
+ "1: \n"
+ "ldr %4, [%1], #4 \n"
+ "ldr %5, [%2], #4 \n"
+ "smulwb %6, %7, %4 \n"
+ "smulwt %4, %7, %4 \n"
+ "smlawb %6, %8, %5, %6 \n"
+ "smlawt %4, %8, %5, %4 \n"
+ "mov %5, %6, asr #15 \n"
+ "teq %5, %5, asr #31 \n"
+ "eorne %6, %9, %6, asr #31 \n"
+ "mov %5, %4, asr #15 \n"
+ "teq %5, %5, asr #31 \n"
+ "eorne %4, %9, %4, asr #31 \n"
+ "subs %3, %3, #4 \n"
+ "and %6, %6, %9, lsr #16 \n"
+ "orr %6, %6, %4, lsl #16 \n"
+ "str %6, [%0], #4 \n"
+ "bhi 1b \n"
+ : "+r"(out), "+r"(src0), "+r"(src1), "+r"(size),
+ "=&r"(s0), "=&r"(s1), "=&r"(tmp)
+ : "r"(src0_amp), "r"(src1_amp), "r"(0xffff7fff));
+}
+
+/* Write channel's samples and apply gain factor */
+static FORCE_INLINE void write_samples(void *out,
+ void *src,
+ int32_t amp,
+ size_t size)
+{
+ if (LIKELY(amp == MIX_AMP_UNITY))
+ {
+ /* Channel is unity amplitude */
+ asm volatile (
+ "ands r1, %2, #0x1f \n"
+ "beq 2f \n"
+ "1: \n"
+ "ldr r0, [%1], #4 \n"
+ "subs r1, r1, #4 \n"
+ "str r0, [%0], #4 \n"
+ "bne 1b \n"
+ "bics %2, %2, #0x1f \n"
+ "beq 3f \n"
+ "2: \n"
+ "ldmia %1!, { r0-r7 } \n"
+ "subs %2, %2, #32 \n"
+ "stmia %0!, { r0-r7 } \n"
+ "bhi 2b \n"
+ "3: \n"
+ : "+r"(out), "+r"(src), "+r"(size)
+ :
+ : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7");
+ }
+ else
+ {
+ /* Channel needs amplitude cut */
+ uint32_t l, h;
+ asm volatile (
+ "1: \n"
+ "ldr %3, [%1], #4 \n"
+ "subs %2, %2, #4 \n"
+ "smulwt %4, %5, %3 \n"
+ "smulwb %3, %5, %3 \n"
+ "mov %4, %4, lsl #16 \n"
+ "mov %3, %3, lsl #16 \n"
+ "orr %4, %4, %3, lsr #16 \n"
+ "str %4, [%0], #4 \n"
+ "bhi 1b \n"
+ : "+r"(out), "+r"(src), "+r"(size),
+ "=&r"(l), "=&r"(h)
+ : "r"(amp));
+ }
+}
diff --git a/firmware/asm/arm/pcm-mixer-armv6.c b/firmware/asm/arm/pcm-mixer-armv6.c
new file mode 100644
index 0000000000..94eecd0f90
--- /dev/null
+++ b/firmware/asm/arm/pcm-mixer-armv6.c
@@ -0,0 +1,118 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2011 by Michael Sevakis
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+#define MIXER_OPTIMIZED_MIX_SAMPLES
+#define MIXER_OPTIMIZED_WRITE_SAMPLES
+
+/* Mix channels' samples and apply gain factors */
+static FORCE_INLINE void mix_samples(void *out,
+ void *src0,
+ int32_t src0_amp,
+ void *src1,
+ int32_t src1_amp,
+ size_t size)
+{
+ uint32_t s0, s1;
+
+ if (src0_amp == MIX_AMP_UNITY && src1_amp == MIX_AMP_UNITY)
+ {
+ /* Both are unity amplitude */
+ asm volatile (
+ "1: \n"
+ "ldr %4, [%1], #4 \n"
+ "ldr %5, [%2], #4 \n"
+ "subs %3, %3, #4 \n"
+ "qadd16 %5, %5, %4 \n"
+ "str %5, [%0], #4 \n"
+ "bhi 1b \n"
+ : "+r"(out), "+r"(src0), "+r"(src1), "+r"(size),
+ "=&r"(s0), "=&r"(s1));
+ }
+ else
+ {
+ /* One or neither are unity amplitude */
+ uint32_t tmp;
+ asm volatile (
+ "1: \n"
+ "ldr %4, [%1], #4 \n"
+ "ldr %5, [%2], #4 \n"
+ "subs %3, %3, #4 \n"
+ "smulwb %6, %7, %4 \n"
+ "smulwt %4, %7, %4 \n"
+ "smlawb %6, %8, %5, %6 \n"
+ "smlawt %4, %8, %5, %4 \n"
+ "ssat %6, #16, %6 \n"
+ "ssat %4, #16, %4 \n"
+ "pkhbt %6, %6, %4, asl #16 \n"
+ "str %6, [%0], #4 \n"
+ "bhi 1b \n"
+ : "+r"(out), "+r"(src0), "+r"(src1), "+r"(size),
+ "=&r"(s0), "=&r"(s1), "=&r"(tmp)
+ : "r"(src0_amp), "r"(src1_amp));
+ }
+}
+
+/* Write channel's samples and apply gain factor */
+static FORCE_INLINE void write_samples(void *out,
+ void *src,
+ int32_t amp,
+ size_t size)
+{
+ if (LIKELY(amp == MIX_AMP_UNITY))
+ {
+ /* Channel is unity amplitude */
+ asm volatile (
+ "ands r1, %2, #0x1f \n"
+ "beq 2f \n"
+ "1: \n"
+ "ldr r0, [%1], #4 \n"
+ "subs r1, r1, #4 \n"
+ "str r0, [%0], #4 \n"
+ "bne 1b \n"
+ "bics %2, %2, #0x1f \n"
+ "beq 3f \n"
+ "2: \n"
+ "ldmia %1!, { r0-r7 } \n"
+ "subs %2, %2, #32 \n"
+ "stmia %0!, { r0-r7 } \n"
+ "bhi 2b \n"
+ "3: \n"
+ : "+r"(out), "+r"(src), "+r"(size)
+ :
+ : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7");
+ }
+ else
+ {
+ /* Channel needs amplitude cut */
+ uint32_t s, tmp;
+ asm volatile(
+ "1: \n"
+ "ldr %3, [%1], #4 \n"
+ "subs %2, %2, #4 \n"
+ "smulwt %4, %5, %3 \n"
+ "smulwb %3, %5, %3 \n"
+ "pkhbt %4, %3, %4, asl #16 \n"
+ "str %4, [%0], #4 \n"
+ "bhi 1b \n"
+ : "+r"(out), "+r"(src), "+r"(size),
+ "=&r"(s), "=&r"(tmp)
+ : "r"(amp));
+ }
+}
diff --git a/firmware/asm/arm/pcm-mixer.c b/firmware/asm/arm/pcm-mixer.c
new file mode 100644
index 0000000000..16c9c3575d
--- /dev/null
+++ b/firmware/asm/arm/pcm-mixer.c
@@ -0,0 +1,7 @@
+#if ARM_ARCH >= 6
+ #include "pcm-mixer-armv6.c"
+#elif ARM_ARCH >= 5
+ #include "pcm-mixer-armv5.c"
+#elif ARM_ARCH >= 4
+ #include "pcm-mixer-armv4.c"
+#endif
diff --git a/firmware/asm/generic/pcm-mixer.c b/firmware/asm/generic/pcm-mixer.c
new file mode 100644
index 0000000000..93841be70d
--- /dev/null
+++ b/firmware/asm/generic/pcm-mixer.c
@@ -0,0 +1,100 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2011 by Michael Sevakis
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+
+#include "dsp-util.h" /* for clip_sample_16 */
+/* Mix channels' samples and apply gain factors */
+static FORCE_INLINE void mix_samples(uint32_t *out,
+ int16_t *src0,
+ int32_t src0_amp,
+ int16_t *src1,
+ int32_t src1_amp,
+ size_t size)
+{
+ if (src0_amp == MIX_AMP_UNITY && src1_amp == MIX_AMP_UNITY)
+ {
+ /* Both are unity amplitude */
+ do
+ {
+ int32_t l = *src0++ + *src1++;
+ int32_t h = *src0++ + *src1++;
+ *out++ = (uint16_t)clip_sample_16(l) | (clip_sample_16(h) << 16);
+ }
+ while ((size -= 4) > 0);
+ }
+ else if (src0_amp != MIX_AMP_UNITY && src1_amp != MIX_AMP_UNITY)
+ {
+ /* Neither are unity amplitude */
+ do
+ {
+ int32_t l = (*src0++ * src0_amp >> 16) + (*src1++ * src1_amp >> 16);
+ int32_t h = (*src0++ * src0_amp >> 16) + (*src1++ * src1_amp >> 16);
+ *out++ = (uint16_t)clip_sample_16(l) | (clip_sample_16(h) << 16);
+ }
+ while ((size -= 4) > 0);
+ }
+ else
+ {
+ /* One is unity amplitude */
+ if (src0_amp != MIX_AMP_UNITY)
+ {
+ /* Keep unity in src0, amp0 */
+ int16_t *src_tmp = src0;
+ src0 = src1;
+ src1 = src_tmp;
+ src1_amp = src0_amp;
+ src0_amp = MIX_AMP_UNITY;
+ }
+
+ do
+ {
+ int32_t l = *src0++ + (*src1++ * src1_amp >> 16);
+ int32_t h = *src0++ + (*src1++ * src1_amp >> 16);
+ *out++ = (uint16_t)clip_sample_16(l) | (clip_sample_16(h) << 16);
+ }
+ while ((size -= 4) > 0);
+ }
+}
+
+/* Write channel's samples and apply gain factor */
+static FORCE_INLINE void write_samples(uint32_t *out,
+ int16_t *src,
+ int32_t amp,
+ size_t size)
+{
+ if (LIKELY(amp == MIX_AMP_UNITY))
+ {
+ /* Channel is unity amplitude */
+ memcpy(out, src, size);
+ }
+ else
+ {
+ /* Channel needs amplitude cut */
+ do
+ {
+ int32_t l = *src++ * amp >> 16;
+ int32_t h = *src++ * amp & 0xffff0000;
+ *out++ = (uint16_t)l | h;
+ }
+ while ((size -= 4) > 0);
+ }
+}
+
+#endif
diff --git a/firmware/asm/m68k/pcm-mixer.c b/firmware/asm/m68k/pcm-mixer.c
new file mode 100644
index 0000000000..d8318fffaf
--- /dev/null
+++ b/firmware/asm/m68k/pcm-mixer.c
@@ -0,0 +1,134 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2011 by Michael Sevakis
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+
+#define MIXER_OPTIMIZED_MIX_SAMPLES
+#define MIXER_OPTIMIZED_WRITE_SAMPLES
+static struct emac_context
+{
+ unsigned long r[4];
+} emac_context IBSS_ATTR;
+
+/* Save emac context affected in ISR */
+static FORCE_INLINE void save_emac_context(void)
+{
+ asm volatile (
+ "move.l %%macsr, %%d0 \n"
+ "move.l %%accext01, %%d1 \n"
+ "movclr.l %%acc0, %%a0 \n"
+ "movclr.l %%acc1, %%a1 \n"
+ "movem.l %%d0-%%d1/%%a0-%%a1, (%0) \n"
+ :
+ : "a"(&emac_context)
+ : "d0", "d1", "a0", "a1");
+}
+
+/* Restore emac context affected in ISR */
+static FORCE_INLINE void restore_emac_context(void)
+{
+ asm volatile (
+ "movem.l (%0), %%d0-%%d1/%%a0-%%a1 \n"
+ "move.l %%a1, %%acc1 \n"
+ "move.l %%a0, %%acc0 \n"
+ "move.l %%d1, %%accext01 \n"
+ "move.l %%d0, %%macsr \n"
+ :
+ : "a"(&emac_context)
+ : "d0", "d1", "a0", "a1");
+}
+
+/* Mix channels' samples and apply gain factors */
+static FORCE_INLINE void mix_samples(void *out,
+ void *src0,
+ int32_t src0_amp,
+ void *src1,
+ int32_t src1_amp,
+ size_t size)
+{
+ uint32_t s0, s1, s2, s3;
+ save_emac_context();
+ coldfire_set_macsr(EMAC_ROUND | EMAC_SATURATE);
+
+ asm volatile (
+ "move.l (%1)+, %5 \n"
+ "1: \n"
+ "movea.w %5, %4 \n"
+ "asr.l %10, %5 \n"
+ "mac.l %4, %8, %%acc0 \n"
+ "mac.l %5, %8, (%2)+, %5, %%acc1 \n"
+ "movea.w %5, %4 \n"
+ "asr.l %10, %5 \n"
+ "mac.l %4, %9, %%acc0 \n"
+ "mac.l %5, %9, (%1)+, %5, %%acc1 \n"
+ "movclr.l %%acc0, %6 \n"
+ "movclr.l %%acc1, %7 \n"
+ "swap.w %6 \n"
+ "move.w %6, %7 \n"
+ "move.l %7, (%0)+ \n"
+ "subq.l #4, %3 \n"
+ "bhi.b 1b \n"
+ : "+a"(out), "+a"(src0), "+a"(src1), "+d"(size),
+ "=&a"(s0), "=&d"(s1), "=&d"(s2), "=&d"(s3)
+ : "r"(src0_amp), "r"(src1_amp), "d"(16)
+ );
+
+ restore_emac_context();
+}
+
+/* Write channel's samples and apply gain factor */
+static FORCE_INLINE void write_samples(void *out,
+ void *src,
+ int32_t amp,
+ size_t size)
+{
+ if (LIKELY(amp == MIX_AMP_UNITY))
+ {
+ /* Channel is unity amplitude */
+ memcpy(out, src, size);
+ }
+ else
+ {
+ /* Channel needs amplitude cut */
+ uint32_t s0, s1, s2, s3;
+ save_emac_context();
+ coldfire_set_macsr(EMAC_ROUND | EMAC_SATURATE);
+
+ asm volatile (
+ "move.l (%1)+, %4 \n"
+ "1: \n"
+ "movea.w %4, %3 \n"
+ "asr.l %8, %4 \n"
+ "mac.l %3, %7, %%acc0 \n"
+ "mac.l %4, %7, (%1)+, %4, %%acc1 \n"
+ "movclr.l %%acc0, %5 \n"
+ "movclr.l %%acc1, %6 \n"
+ "swap.w %5 \n"
+ "move.w %5, %6 \n"
+ "move.l %6, (%0)+ \n"
+ "subq.l #4, %2 \n"
+ "bhi.b 1b \n"
+ : "+a"(out), "+a"(src), "+d"(size),
+ "=&a"(s0), "=&d"(s1), "=&d"(s2), "=&d"(s3)
+ : "r"(amp), "d"(16)
+ );
+
+ restore_emac_context();
+ }
+}
diff --git a/firmware/asm/pcm-mixer.c b/firmware/asm/pcm-mixer.c
new file mode 100644
index 0000000000..369e830427
--- /dev/null
+++ b/firmware/asm/pcm-mixer.c
@@ -0,0 +1,108 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2011 by Michael Sevakis
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+
+#if defined(CPU_ARM)
+ #include "arm/pcm-mixer.c"
+#elif defined(CPU_COLDFIRE)
+ #include "m68k/pcm-mixer.c"
+#else
+
+/* generic pcm-mixer.c */
+#include "dsp-util.h" /* for clip_sample_16 */
+/* Mix channels' samples and apply gain factors */
+static FORCE_INLINE void mix_samples(uint32_t *out,
+ int16_t *src0,
+ int32_t src0_amp,
+ int16_t *src1,
+ int32_t src1_amp,
+ size_t size)
+{
+ if (src0_amp == MIX_AMP_UNITY && src1_amp == MIX_AMP_UNITY)
+ {
+ /* Both are unity amplitude */
+ do
+ {
+ int32_t l = *src0++ + *src1++;
+ int32_t h = *src0++ + *src1++;
+ *out++ = (uint16_t)clip_sample_16(l) | (clip_sample_16(h) << 16);
+ }
+ while ((size -= 4) > 0);
+ }
+ else if (src0_amp != MIX_AMP_UNITY && src1_amp != MIX_AMP_UNITY)
+ {
+ /* Neither are unity amplitude */
+ do
+ {
+ int32_t l = (*src0++ * src0_amp >> 16) + (*src1++ * src1_amp >> 16);
+ int32_t h = (*src0++ * src0_amp >> 16) + (*src1++ * src1_amp >> 16);
+ *out++ = (uint16_t)clip_sample_16(l) | (clip_sample_16(h) << 16);
+ }
+ while ((size -= 4) > 0);
+ }
+ else
+ {
+ /* One is unity amplitude */
+ if (src0_amp != MIX_AMP_UNITY)
+ {
+ /* Keep unity in src0, amp0 */
+ int16_t *src_tmp = src0;
+ src0 = src1;
+ src1 = src_tmp;
+ src1_amp = src0_amp;
+ src0_amp = MIX_AMP_UNITY;
+ }
+
+ do
+ {
+ int32_t l = *src0++ + (*src1++ * src1_amp >> 16);
+ int32_t h = *src0++ + (*src1++ * src1_amp >> 16);
+ *out++ = (uint16_t)clip_sample_16(l) | (clip_sample_16(h) << 16);
+ }
+ while ((size -= 4) > 0);
+ }
+}
+
+/* Write channel's samples and apply gain factor */
+static FORCE_INLINE void write_samples(uint32_t *out,
+ int16_t *src,
+ int32_t amp,
+ size_t size)
+{
+ if (LIKELY(amp == MIX_AMP_UNITY))
+ {
+ /* Channel is unity amplitude */
+ memcpy(out, src, size);
+ }
+ else
+ {
+ /* Channel needs amplitude cut */
+ do
+ {
+ int32_t l = *src++ * amp >> 16;
+ int32_t h = *src++ * amp & 0xffff0000;
+ *out++ = (uint16_t)l | h;
+ }
+ while ((size -= 4) > 0);
+ }
+}
+
+
+#endif