aboutsummaryrefslogtreecommitdiffstats
path: root/source/stmdsp/stmdsp_code.hpp
blob: 68504599c2237b420bbe0baa1d74909332448670 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
 * @file stmdsp_code.hpp
 * @brief Source code and build scripts for stmdsp device algorithms.
 *
 * Copyright (C) 2021 Clyne Sullivan
 *
 * Distributed under the GNU GPL v3 or later. You should have received a copy of
 * the GNU General Public License along with this program.
 * If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef STMDSP_CODE_HPP
#define STMDSP_CODE_HPP

#ifdef STMDSP_WIN32
#define NEWLINE "\r\n"
#define COPY "copy"
#else
#define NEWLINE "\n"
#define COPY "cp"
#endif

namespace stmdsp {

// $0 = temp file name
// TODO try -ffunction-sections -fdata-sections -Wl,--gc-sections
static std::string makefile_text_h7 =
#ifdef STMDSP_WIN32
	"echo off" NEWLINE
#endif
    "arm-none-eabi-g++ -x c++ -Os -std=c++20 -fno-exceptions -fno-rtti "
        "-mcpu=cortex-m7 -mthumb -mfloat-abi=hard -mfpu=fpv5-d16 -mtune=cortex-m7 "
	"-nostartfiles "
        "-Wl,-Ttext-segment=0x00000000 -Wl,-zmax-page-size=512 -Wl,-eprocess_data_entry "
        "$0 -o $0.o" NEWLINE
	COPY " $0.o $0.orig.o" NEWLINE
	"arm-none-eabi-strip -s -S --strip-unneeded $0.o" NEWLINE
	"arm-none-eabi-objcopy --remove-section .ARM.attributes "
                          "--remove-section .comment "
                          "--remove-section .noinit "
                          "$0.o" NEWLINE
	"arm-none-eabi-size $0.o" NEWLINE;
static std::string makefile_text_l4 =
#ifdef STMDSP_WIN32
	"echo off" NEWLINE
#endif
    "arm-none-eabi-g++ -x c++ -Os -std=c++20 -fno-exceptions -fno-rtti "
        "-mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mtune=cortex-m4 "
        "-nostartfiles -I$1/cmsis "
        "-Wl,-Ttext-segment=0x10000000 -Wl,-zmax-page-size=512 -Wl,-eprocess_data_entry "
        "$0 -o $0.o" NEWLINE
    COPY " $0.o $0.orig.o" NEWLINE
    "arm-none-eabi-strip -s -S --strip-unneeded $0.o" NEWLINE
    "arm-none-eabi-objcopy --remove-section .ARM.attributes "
                          "--remove-section .comment "
                          "--remove-section .noinit "
                          "$0.o" NEWLINE
    "arm-none-eabi-size $0.o" NEWLINE;

// $0 = buffer size
static std::string file_header_h7 = R"cpp(
#include <cstdint>
#include <span>

using Sample = uint16_t;
using Samples = std::span<Sample, $0>;

Sample *process_data(Samples samples);
extern "C" void process_data_entry()
{
    Sample *samples;
    asm("mov %0, r0" : "=r" (samples));
    process_data(Samples(samples, $0));
}

static double PI = 3.14159265358979323846L;
__attribute__((naked))
auto sin(double x) {
asm("vmov.f64 r1, r2, d0;"
    "eor r0, r0;"
    "svc 1;"
    "vmov.f64 d0, r1, r2;"
    "bx lr");
return 0;
}
__attribute__((naked))
auto cos(double x) {
asm("vmov.f64 r1, r2, d0;"
	"mov r0, #1;"
	"svc 1;"
	"vmov.f64 d0, r1, r2;"
	"bx lr");
return 0;
}
__attribute__((naked))
auto tan(double x) {
asm("vmov.f64 r1, r2, d0;"
	"mov r0, #2;"
	"svc 1;"
	"vmov.f64 d0, r1, r2;"
	"bx lr");
return 0;
}
__attribute__((naked))
auto sqrt(double x) {
asm("vsqrt.f64 d0, d0; bx lr");
return 0;
}

auto readalt() {
Sample s;
asm("svc 3; mov %0, r0" : "=&r"(s));
return s;
}

// End stmdspgui header code

)cpp";
static std::string file_header_l4 = R"cpp(
#include <cstdint>
#include <span>

using Sample = uint16_t;
using Samples = std::span<Sample, $0>;

Sample *process_data(Samples samples);
extern "C" void process_data_entry()
{
    Sample *samples;
    asm("mov %0, r0" : "=r" (samples));
    process_data(Samples(samples, $0));
}

static float PI = 3.14159265358979L;
__attribute__((naked))
auto sin(float x) {
asm("vmov.f32 r1, s0;"
    "eor r0, r0;"
    "svc 1;"
    "vmov.f32 s0, r1;"
    "bx lr");
return 0;
}
__attribute__((naked))
auto cos(float x) {
asm("vmov.f32 r1, s0;"
	"mov r0, #1;"
	"svc 1;"
	"vmov.f32 s0, r1;"
	"bx lr");
return 0;
}
__attribute__((naked))
auto tan(float x) {
asm("vmov.f32 r1, s0;"
	"mov r0, #2;"
	"svc 1;"
	"vmov.f32 s0, r1;"
	"bx lr");
return 0;
}
__attribute__((naked))
auto sqrt(float) {
asm("vsqrt.f32 s0, s0; bx lr");
return 0;
}

auto readpot1() {
Sample s;
asm("push {r4-r11}; eor r0, r0; svc 3; mov %0, r0; pop {r4-r11}" : "=r"(s));
return s;
}
auto readpot2() {
Sample s;
asm("push {r4-r11}; mov r0, #1; svc 3; mov %0, r0; pop {r4-r11}" : "=r"(s));
return s;
}

//void puts(const char *s) {
// 's' will already be in r0.
//asm("push {r4-r6}; svc 4; pop {r4-r6}");
//}

// End stmdspgui header code

)cpp";


static std::string file_content = 
R"cpp(Sample *process_data(Samples samples)
{
    return samples.data();
}
)cpp";

} // namespace stmdsp

#endif // STMDSP_CODE_HPP