Debian Codegraph @

src/engine/platform/sound/c64_fp/OpAmp.h

raw
1 /*
2 * This file is part of libsidplayfp, a SID player engine.
3 *
4 * Copyright 2011-2015 Leandro Nini <[email protected]>
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 OPAMP_H
24 #define OPAMP_H
25
26 #include <memory>
27 #include <vector>
28
29 #include "Spline.h"
30
31 #include "sidcxx11.h"
32
33 namespace reSIDfp
34 {
35
36 /**
37 * Find output voltage in inverting gain and inverting summer SID op-amp
38 * circuits, using a combination of Newton-Raphson and bisection.
39 *
40 * +---R2--+
41 * | |
42 * vi ---R1--o--[A>--o-- vo
43 * vx
44 *
45 * From Kirchoff's current law it follows that
46 *
47 * IR1f + IR2r = 0
48 *
49 * Substituting the triode mode transistor model K*W/L*(Vgst^2 - Vgdt^2)
50 * for the currents, we get:
51 *
52 * n*((Vddt - vx)^2 - (Vddt - vi)^2) + (Vddt - vx)^2 - (Vddt - vo)^2 = 0
53 *
54 * Our root function f can thus be written as:
55 *
56 * f = (n + 1)*(Vddt - vx)^2 - n*(Vddt - vi)^2 - (Vddt - vo)^2 = 0
57 *
58 * Using substitution constants
59 *
60 * a = n + 1
61 * b = Vddt
62 * c = n*(Vddt - vi)^2
63 *
64 * the equations for the root function and its derivative can be written as:
65 *
66 * f = a*(b - vx)^2 - c - (b - vo)^2
67 * df = 2*((b - vo)*dvo - a*(b - vx))
68 */
69 class OpAmp
70 {
71 private:
72 /// Current root position (cached as guess to speed up next iteration)
73 mutable double x;
74
75 const double Vddt;
76 const double vmin;
77 const double vmax;
78
79 std::unique_ptr<Spline> const opamp;
80
81 public:
82 /**
83 * Opamp input -> output voltage conversion
84 *
85 * @param opamp opamp mapping table as pairs of points (in -> out)
86 * @param opamplength length of the opamp array
87 * @param kVddt transistor dt parameter (in volts)
88 */
89 OpAmp(const std::vector<Spline::Point> &opamp, double Vddt) :
90 x(0.),
91 Vddt(Vddt),
92 vmin(opamp.front().x),
93 vmax(opamp.back().x),
94 opamp(new Spline(opamp)) {}
95
96 void reset() const
97 {
98 x = vmin;
99 }
100
101 /**
102 * Solve the opamp equation for input vi in loading context n
103 *
104 * @param n the ratio of input/output loading
105 * @param vi input
106 * @return vo
107 */
108 double solve(double n, double vi) const;
109 };
110
111 } // namespace reSIDfp
112
113 #endif