libsidplayfp 2.7.1
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 <cassert>
28
29#include "OpAmp.h"
30#include "Spline.h"
31
32#include "sidcxx11.h"
33
34namespace reSIDfp
35{
36
37class Integrator;
38
40{
41protected:
43 const double C;
44
46
47 const double Vdd;
48 const double Vth;
49 const double Ut;
50 double uCox;
51 const double Vddt;
53
54 // Derived stuff
55 const double vmin, vmax;
56 const double denorm, norm;
57
59 const double N16;
60
63
64 const double voice_voltage_range;
65 const double voice_DC_voltage;
66
68
69 unsigned short* mixer[8]; //-V730_NOINIT this is initialized in the derived class constructor
70 unsigned short* summer[5]; //-V730_NOINIT this is initialized in the derived class constructor
71 unsigned short* volume[16]; //-V730_NOINIT this is initialized in the derived class constructor
72 unsigned short* resonance[16]; //-V730_NOINIT this is initialized in the derived class constructor
74
76 unsigned short opamp_rev[1 << 16]; //-V730_NOINIT this is initialized in the derived class constructor
77
78private:
80 FilterModelConfig& operator= (const FilterModelConfig&) DELETE;
81
82 inline double getVoiceVoltage(float value) const
83 {
84 return value * voice_voltage_range + voice_DC_voltage;
85 }
86
87protected:
99 double vvr,
100 double vdv,
101 double c,
102 double vdd,
103 double vth,
104 double ucox,
105 const Spline::Point *opamp_voltage,
106 int opamp_size
107 );
108
110
111 void setUCox(double new_uCox);
112
122 inline void buildSummerTable(const OpAmp& opampModel)
123 {
124 for (int i = 0; i < 5; i++)
125 {
126 const int idiv = 2 + i; // 2 - 6 input "resistors".
127 const int size = idiv << 16;
128 const double n = idiv;
129 opampModel.reset();
130 summer[i] = new unsigned short[size];
131
132 for (int vi = 0; vi < size; vi++)
133 {
134 const double vin = vmin + vi / N16 / idiv; /* vmin .. vmax */
135 summer[i][vi] = getNormalizedValue(opampModel.solve(n, vin));
136 }
137 }
138 }
139
148 inline void buildMixerTable(const OpAmp& opampModel, double nRatio)
149 {
150 for (int i = 0; i < 8; i++)
151 {
152 const int idiv = (i == 0) ? 1 : i;
153 const int size = (i == 0) ? 1 : i << 16;
154 const double n = i * nRatio;
155 opampModel.reset();
156 mixer[i] = new unsigned short[size];
157
158 for (int vi = 0; vi < size; vi++)
159 {
160 const double vin = vmin + vi / N16 / idiv; /* vmin .. vmax */
161 mixer[i][vi] = getNormalizedValue(opampModel.solve(n, vin));
162 }
163 }
164 }
165
173 inline void buildVolumeTable(const OpAmp& opampModel, double nDivisor)
174 {
175 for (int n8 = 0; n8 < 16; n8++)
176 {
177 const int size = 1 << 16;
178 const double n = n8 / nDivisor;
179 opampModel.reset();
180 volume[n8] = new unsigned short[size];
181
182 for (int vi = 0; vi < size; vi++)
183 {
184 const double vin = vmin + vi / N16; /* vmin .. vmax */
185 volume[n8][vi] = getNormalizedValue(opampModel.solve(n, vin));
186 }
187 }
188 }
189
197 inline void buildResonanceTable(const OpAmp& opampModel, const double resonance_n[16])
198 {
199 for (int n8 = 0; n8 < 16; n8++)
200 {
201 const int size = 1 << 16;
202 opampModel.reset();
203 resonance[n8] = new unsigned short[size];
204
205 for (int vi = 0; vi < size; vi++)
206 {
207 const double vin = vmin + vi / N16; /* vmin .. vmax */
208 resonance[n8][vi] = getNormalizedValue(opampModel.solve(resonance_n[n8], vin));
209 }
210 }
211 }
212
213public:
214 unsigned short** getVolume() { return volume; }
215 unsigned short** getResonance() { return resonance; }
216 unsigned short** getSummer() { return summer; }
217 unsigned short** getMixer() { return mixer; }
218
219 virtual Integrator* buildIntegrator() = 0;
220
221 inline unsigned short getOpampRev(int i) const { return opamp_rev[i]; }
222 inline double getVddt() const { return Vddt; }
223 inline double getVth() const { return Vth; }
224
225 // helper functions
226 inline unsigned short getNormalizedValue(double value) const
227 {
228 const double tmp = N16 * (value - vmin);
229 assert(tmp > -0.5 && tmp < 65535.5);
230 return static_cast<unsigned short>(tmp + 0.5);
231 }
232
233 inline unsigned short getNormalizedCurrentFactor(double wl) const
234 {
235 const double tmp = (1 << 13) * currFactorCoeff * wl;
236 assert(tmp > -0.5 && tmp < 65535.5);
237 return static_cast<unsigned short>(tmp + 0.5);
238 }
239
240 inline unsigned short getNVmin() const
241 {
242 const double tmp = N16 * vmin;
243 assert(tmp > -0.5 && tmp < 65535.5);
244 return static_cast<unsigned short>(tmp + 0.5);
245 }
246
247 inline int getNormalizedVoice(float value) const
248 {
249 return static_cast<int>(getNormalizedValue(getVoiceVoltage(value)));
250 }
251};
252
253} // namespace reSIDfp
254
255#endif
Definition FilterModelConfig.h:40
void buildVolumeTable(const OpAmp &opampModel, double nDivisor)
Definition FilterModelConfig.h:173
const double Vdd
Transistor parameters.
Definition FilterModelConfig.h:47
unsigned short * mixer[8]
Lookup tables for gain and summer op-amps in output stage / filter.
Definition FilterModelConfig.h:69
const double Ut
Thermal voltage: Ut = kT/q = 8.61734315e-5*T ~ 26mV.
Definition FilterModelConfig.h:49
void buildResonanceTable(const OpAmp &opampModel, const double resonance_n[16])
Definition FilterModelConfig.h:197
double uCox
Transconductance coefficient: u*Cox.
Definition FilterModelConfig.h:50
const double Vddt
Vdd - Vth.
Definition FilterModelConfig.h:51
double currFactorCoeff
Current factor coefficient for op-amp integrators.
Definition FilterModelConfig.h:62
const double C
Capacitor value.
Definition FilterModelConfig.h:43
unsigned short opamp_rev[1<< 16]
Reverse op-amp transfer function.
Definition FilterModelConfig.h:76
const double Vth
Threshold voltage.
Definition FilterModelConfig.h:48
void buildMixerTable(const OpAmp &opampModel, double nRatio)
Definition FilterModelConfig.h:148
const double N16
Fixed point scaling for 16 bit op-amp output.
Definition FilterModelConfig.h:59
void buildSummerTable(const OpAmp &opampModel)
Definition FilterModelConfig.h:122
Definition OpAmp.h:72
double solve(double n, double vi) const
Definition OpAmp.cpp:33
void reset() const
Definition OpAmp.h:104
Definition Spline.h:42