libresidfp 1.3.0
FilterModelConfig.h
1/*
2 * This file is part of libsidplayfp, a SID player engine.
3 *
4 * Copyright 2011-2024 Leandro Nini <drfiemost@users.sourceforge.net>
5 * Copyright 2007-2010 Antti Lankila
6 * Copyright 2004,2010 Dag Lem
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22
23#ifndef FILTERMODELCONFIG_H
24#define FILTERMODELCONFIG_H
25
26#include <algorithm>
27#include <random>
28#include <cassert>
29#include <climits>
30
31#include "OpAmp.h"
32#include "Spline.h"
33
34#include "siddefs-fp.h"
35
36namespace reSIDfp
37{
38
40{
41public:
42 // The highpass summer has 2 - 6 inputs (bandpass, lowpass, and 0 - 4 voices).
43 template<int i>
45 {
46 static constexpr int value = summer_offset<i - 1>::value + ((2 + i - 1) << 16);
47 };
48
49 // The mixer has 0 - 7 inputs (0 - 4 voices and 0 - 3 filter outputs).
50 template<int i>
52 {
53 static constexpr int value = mixer_offset<i - 1>::value + ((i - 1) << 16);
54 };
55
56protected:
57 static inline uint16_t to_uint16(double x)
58 {
59 return static_cast<uint16_t>(x);
60 }
61
62protected:
64 const double C;
65
67
68
69 static constexpr double k = 1.380649e-23; // Boltzmann Constant
70 static constexpr double q = 1.602176634e-19; // charge of an electron
71 static constexpr double temp = 27; // temperature in °C
72 static constexpr double Ut = (k * (temp + 273.15)) / q;
73
74 const double Vdd;
75 const double Vth;
76 const double Vddt;
77 double uCox;
79
80 // Derived stuff
81 const double vmin, vmax;
82 const double denorm, norm;
83
85 const double N16;
86
87 const double voice_voltage_range;
88
91
93
94 uint16_t* mixer; //-V730_NOINIT this is initialized in the derived class constructor
95 uint16_t* summer; //-V730_NOINIT this is initialized in the derived class constructor
96 uint16_t* volume; //-V730_NOINIT this is initialized in the derived class constructor
97 uint16_t* resonance; //-V730_NOINIT this is initialized in the derived class constructor
99
101 uint16_t opamp_rev[1 << 16]; //-V730_NOINIT this is initialized in the derived class constructor
102
103private:
104 FilterModelConfig(const FilterModelConfig&) = delete;
105 FilterModelConfig& operator= (const FilterModelConfig&) = delete;
106
107 inline double getVoiceVoltage(float value, uint8_t env) const
108 {
109 return value * voice_voltage_range + getVoiceDC(env);
110 }
111
112protected:
123 double vvr,
124 double c,
125 double vdd,
126 double vth,
127 double ucox,
128 const Spline::Point *opamp_voltage,
129 int opamp_size
130 );
131
133
134 void calcCurrFactorCoeff();
135
136 virtual double getVoiceDC(uint8_t env) const = 0;
137
147 inline void buildSummerTable(const OpAmp& opampModel)
148 {
149 const double r_N16 = 1. / N16;
150
151 int idx = 0;
152 for (int i = 0; i < 5; i++)
153 {
154 const int idiv = 2 + i; // 2 - 6 input "resistors".
155 const int size = idiv << 16;
156 const double n = idiv;
157 const double r_idiv = 1. / idiv;
158 opampModel.reset();
159
160 for (int vi = 0; vi < size; vi++)
161 {
162 const double vin = vmin + vi * r_N16 * r_idiv; /* vmin .. vmax */
163 summer[idx++] = getNormalizedValue(opampModel.solve(n, vin));
164 }
165 }
166 }
167
176 inline void buildMixerTable(const OpAmp& opampModel, double nRatio)
177 {
178 const double r_N16 = 1. / N16;
179
180 int idx = 0;
181 for (int i = 0; i < 8; i++)
182 {
183 const int idiv = (i == 0) ? 1 : i;
184 const int size = (i == 0) ? 1 : i << 16;
185 const double n = i * nRatio;
186 const double r_idiv = 1. / idiv;
187 opampModel.reset();
188
189 for (int vi = 0; vi < size; vi++)
190 {
191 const double vin = vmin + vi * r_N16 * r_idiv; /* vmin .. vmax */
192 mixer[idx++] = getNormalizedValue(opampModel.solve(n, vin));
193 }
194 }
195 }
196
204 inline void buildVolumeTable(const OpAmp& opampModel, double nDivisor)
205 {
206 const double r_N16 = 1. / N16;
207
208 int idx = 0;
209 for (int n8 = 0; n8 < 16; n8++)
210 {
211 constexpr int size = 1 << 16;
212 const double n = n8 / nDivisor;
213 opampModel.reset();
214
215 for (int vi = 0; vi < size; vi++)
216 {
217 const double vin = vmin + vi * r_N16; /* vmin .. vmax */
218 volume[idx++] = getNormalizedValue(opampModel.solve(n, vin));
219 }
220 }
221 }
222
230 inline void buildResonanceTable(const OpAmp& opampModel, const double resonance_n[16])
231 {
232 const double r_N16 = 1. / N16;
233
234 int idx = 0;
235 for (int n8 = 0; n8 < 16; n8++)
236 {
237 constexpr int size = 1 << 16;
238 opampModel.reset();
239
240 for (int vi = 0; vi < size; vi++)
241 {
242 const double vin = vmin + vi * r_N16; /* vmin .. vmax */
243 resonance[idx++] = getNormalizedValue(opampModel.solve(resonance_n[n8], vin));
244 }
245 }
246 }
247
248public:
249 uint16_t* getVolume() const { return volume; }
250 uint16_t* getResonance() const { return resonance; }
251 uint16_t* getSummer() const { return summer; }
252 uint16_t* getMixer() const { return mixer; }
253
254 inline uint16_t getOpampRev(int i) const { return opamp_rev[i]; }
255 inline double getVddt() const { return Vddt; }
256 inline double getVth() const { return Vth; }
257
258 // helper functions
259
260 inline uint16_t getNormalizedValue(double value) const
261 {
262 return to_uint16(N16 * (value - vmin));
263 }
264
265 template<int N>
266 inline uint16_t getNormalizedCurrentFactor(double wl) const
267 {
268 return to_uint16((1 << N) * currFactorCoeff * wl);
269 }
270
271 inline uint16_t getNVmin() const
272 {
273 return to_uint16(N16 * vmin);
274 }
275
276 inline int32_t getNormalizedVoice(float value, uint8_t env) const
277 {
278 return static_cast<int32_t>(getNormalizedValue(getVoiceVoltage(value, env)));
279 }
280};
281
282template<>
284{
285 static constexpr int value = 0;
286};
287
288template<>
290{
291 static constexpr int value = 1;
292};
293
294template<>
296{
297 static constexpr int value = 0;
298};
299
300} // namespace reSIDfp
301
302#endif
Definition FilterModelConfig.h:40
void buildVolumeTable(const OpAmp &opampModel, double nDivisor)
Definition FilterModelConfig.h:204
const double Vdd
Positive supply voltage.
Definition FilterModelConfig.h:74
uint16_t * mixer
Lookup tables for gain and summer op-amps in output stage / filter.
Definition FilterModelConfig.h:94
void buildResonanceTable(const OpAmp &opampModel, const double resonance_n[16])
Definition FilterModelConfig.h:230
double uCox
Transconductance coefficient: u*Cox.
Definition FilterModelConfig.h:77
const double Vddt
Vdd - Vth.
Definition FilterModelConfig.h:76
static constexpr double k
Transistor parameters.
Definition FilterModelConfig.h:69
double currFactorCoeff
Current factor coefficient for op-amp integrators.
Definition FilterModelConfig.h:90
const double C
Capacitor value.
Definition FilterModelConfig.h:64
const double Vth
Threshold voltage.
Definition FilterModelConfig.h:75
void buildMixerTable(const OpAmp &opampModel, double nRatio)
Definition FilterModelConfig.h:176
const double N16
Fixed point scaling for 16 bit op-amp output.
Definition FilterModelConfig.h:85
uint16_t opamp_rev[1<< 16]
Reverse op-amp transfer function.
Definition FilterModelConfig.h:101
void buildSummerTable(const OpAmp &opampModel)
Definition FilterModelConfig.h:147
Definition OpAmp.h:71
double solve(double n, double vi) const
Definition OpAmp.cpp:33
void reset() const
Definition OpAmp.h:102
Definition FilterModelConfig.h:52
Definition FilterModelConfig.h:45