summaryrefslogtreecommitdiffstats
path: root/apps/plugins/sdl/progs/quake/snd_mix_arm.S
blob: 15733d89122f1d75970ba0b1f8393bc8a1579b62 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2019 Franklin Wei
 *
 * 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.
 *
 ***************************************************************************/

        /** Sound mixing code for ARM. **/
        /* Takes 8-bit mono audio and outputs stereo 16-bit samples.
         * stereo volumes are passed as arguments.
         *
         * Bear with me. This is my first ARM assembly, ever.
        */

        .text
        .align  2
        .global SND_PaintChannelFrom8
        .type   SND_PaintChannelFrom8, %function

#if defined(__ARM_ARCH_5TEJ__)
SND_PaintChannelFrom8:
        // r0: int true_lvol
        // r1: int true_rvol
        // r2: char *sfx
        // r3: int count

        stmfd sp!, {r4, r5, r6, r7, r8, sl}

        ldr ip, =paintbuffer
        ldr ip, [ip]

        mov r0, r0, asl #16 // pre-scale both volumes by 2^16
        mov r1, r1, asl #16

        sub r3, r3, #1 // we'll count backwards
        // sl = 0xffff0000
        ldrh sl, =0xffff

.loop:
        ldrsb r4, [r2, r3] // load *sfx[i] -> r4

        // keep endianness in mind here
        // buffer looks like [left_0, left_1, right_0, right_1] in memory
        // but it is loaded as [right1, right0, left1, left0] to registers
        ldr r8, [ip, r3, lsl #2] // load paintbuffer[0:1] = RIGHTCHANNEL:LEFTCHANNEL

        // handle high half (right channel) first
        mul r5, r4, r1 // SCALEDRIGHT = SFXI * (true_rvol << 16) -- bottom half is zero

        // r7 holds right channel in high half (dirty bottom half)
        qadd r7, r5, r8 // RIGHTCHANORIG = SCALEDRIGHT + RIGHTCHANORIG (high half)

        bic r7, r7, sl // zero bottom bits of r7

        // trash r5, r6 and handle left channel
        mul r5, r4, r0 // SCALEDLEFT = SFXI * (true_rvol << 16)

        mov r8, r8, lsl #16 // extract original left channel from paintbuffer

        // r8 holds left channel in high half with zero bottom half
        qadd r8, r5, r8

        // combine the two 16-bit samples in r7 as 32-bit [left:right]
        // (use lsr to not sign-extend the lower half)
        orr r7, r7, r8, lsr #16

        str r7, [ip, r3, lsl #2] // write 32-bit to paintbuffer
        subs r3, r3, #1
        bgt .loop // must use instead of bne because of the corner case count=1



        ldmfd sp!, {r4, r5, r6, r7, r8, sl}

        bx lr

#elif defined(__ARM_ARCH_6__) // ARMv6 with QADD16 (disabled)
SND_PaintChannelFrom8:
        // r0: int true_lvol
        // r1: int true_rvol
        // r2: char *sfx
        // r3: int count

        stmfd sp!, {r4, r5, r6, r7}

        ldr ip, =paintbuffer
        ldr ip, [ip]
        sub r3, r3, #1 // we'll count backwards
.loop:
        ldrsb r4, [r2, r3] // load *sfx[i] -> r4

        // keep endianness in mind here
        // buffer looks like [left_0, left_1, right_0, right_1] in memory
        // but it is loaded as [right1, right0, left1, left0] to registers
        ldr r7, [ip, r3, lsl #2] // load paintbuffer[0:1] = RIGHTCHANNEL:LEFTCHANNEL

        // handle high half (right channel) first
        mul r5, r4, r1 // SCALEDRIGHT = SFXI * true_rvol
        mul r6, r4, r0 // SCALEDLEFT = SFXI * true_rvol

        orr r6, r6, r5, lsl #16

        qadd16 r6, r6, r7

        str r6, [ip, r3, lsl #2] // write 32-bit to paintbuffer

        subs r3, r3, #1
        bne .loop

        ldmfd sp!, {r4, r5, r6, r7}

        bx lr
#else
#error ARMv5/6 only
#endif