summaryrefslogtreecommitdiffstats
path: root/firmware/asm/m68k/pcm-mixer.c
blob: d8318fffaf9f84c76c4ab6e0117df7b987627859 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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();
    }     
}