Debian Codegraph @

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

raw
1 /*
2 * This file is part of libsidplayfp, a SID player engine.
3 *
4 * Copyright 2011-2022 Leandro Nini <[email protected]>
5 * Copyright 2007-2010 Antti Lankila
6 * Copyright 2004, 2010 Dag Lem <[email protected]>
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 INTEGRATOR6581_H
24 #define INTEGRATOR6581_H
25
26 #include "FilterModelConfig6581.h"
27
28 #include <stdint.h>
29 #include <cassert>
30
31 // uncomment to enable use of the slope factor
32 // in the EKV model
33 // actually produces worse results, needs investigation
34 //#define SLOPE_FACTOR
35
36 #ifdef SLOPE_FACTOR
37 # include <cmath>
38 #endif
39
40 #include "siddefs-fp.h"
41
42 namespace reSIDfp
43 {
44
45 /**
46 * Find output voltage in inverting integrator SID op-amp circuits, using a
47 * single fixpoint iteration step.
48 *
49 * A circuit diagram of a MOS 6581 integrator is shown below.
50 *
51 * +---C---+
52 * | |
53 * vi --o--Rw--o-o--[A>--o-- vo
54 * | | vx
55 * +--Rs--+
56 *
57 * From Kirchoff's current law it follows that
58 *
59 * IRw + IRs + ICr = 0
60 *
61 * Using the formula for current through a capacitor, i = C*dv/dt, we get
62 *
63 * IRw + IRs + C*(vc - vc0)/dt = 0
64 * dt/C*(IRw + IRs) + vc - vc0 = 0
65 * vc = vc0 - n*(IRw(vi,vx) + IRs(vi,vx))
66 *
67 * which may be rewritten as the following iterative fixpoint function:
68 *
69 * vc = vc0 - n*(IRw(vi,g(vc)) + IRs(vi,g(vc)))
70 *
71 * To accurately calculate the currents through Rs and Rw, we need to use
72 * transistor models. Rs has a gate voltage of Vdd = 12V, and can be
73 * assumed to always be in triode mode. For Rw, the situation is rather
74 * more complex, as it turns out that this transistor will operate in
75 * both subthreshold, triode, and saturation modes.
76 *
77 * The Shichman-Hodges transistor model routinely used in textbooks may
78 * be written as follows:
79 *
80 * Ids = 0 , Vgst < 0 (subthreshold mode)
81 * Ids = K*W/L*(2*Vgst - Vds)*Vds , Vgst >= 0, Vds < Vgst (triode mode)
82 * Ids = K*W/L*Vgst^2 , Vgst >= 0, Vds >= Vgst (saturation mode)
83 *
84 * where
85 * K = u*Cox/2 (transconductance coefficient)
86 * W/L = ratio between substrate width and length
87 * Vgst = Vg - Vs - Vt (overdrive voltage)
88 *
89 * This transistor model is also called the quadratic model.
90 *
91 * Note that the equation for the triode mode can be reformulated as
92 * independent terms depending on Vgs and Vgd, respectively, by the
93 * following substitution:
94 *
95 * Vds = Vgst - (Vgst - Vds) = Vgst - Vgdt
96 *
97 * Ids = K*W/L*(2*Vgst - Vds)*Vds
98 * = K*W/L*(2*Vgst - (Vgst - Vgdt)*(Vgst - Vgdt)
99 * = K*W/L*(Vgst + Vgdt)*(Vgst - Vgdt)
100 * = K*W/L*(Vgst^2 - Vgdt^2)
101 *
102 * This turns out to be a general equation which covers both the triode
103 * and saturation modes (where the second term is 0 in saturation mode).
104 * The equation is also symmetrical, i.e. it can calculate negative
105 * currents without any change of parameters (since the terms for drain
106 * and source are identical except for the sign).
107 *
108 * FIXME: Subthreshold as function of Vgs, Vgd.
109 *
110 * Ids = I0*W/L*e^(Vgst/(Ut/k)) , Vgst < 0 (subthreshold mode)
111 *
112 * where
113 * I0 = (2 * uCox * Ut^2) / k
114 *
115 * The remaining problem with the textbook model is that the transition
116 * from subthreshold the triode/saturation is not continuous.
117 *
118 * Realizing that the subthreshold and triode/saturation modes may both
119 * be defined by independent (and equal) terms of Vgs and Vds,
120 * respectively, the corresponding terms can be blended into (equal)
121 * continuous functions suitable for table lookup.
122 *
123 * The EKV model (Enz, Krummenacher and Vittoz) essentially performs this
124 * blending using an elegant mathematical formulation:
125 *
126 * Ids = Is * (if - ir)
127 * Is = ((2 * u*Cox * Ut^2)/k) * W/L
128 * if = ln^2(1 + e^((k*(Vg - Vt) - Vs)/(2*Ut))
129 * ir = ln^2(1 + e^((k*(Vg - Vt) - Vd)/(2*Ut))
130 *
131 * For our purposes, the EKV model preserves two important properties
132 * discussed above:
133 *
134 * - It consists of two independent terms, which can be represented by
135 * the same lookup table.
136 * - It is symmetrical, i.e. it calculates current in both directions,
137 * facilitating a branch-free implementation.
138 *
139 * Rw in the circuit diagram above is a VCR (voltage controlled resistor),
140 * as shown in the circuit diagram below.
141 *
142 *
143 * Vdd
144 * |
145 * Vdd _|_
146 * | +---+ +---- Vw
147 * _|_ |
148 * +--+ +---o Vg
149 * | __|__
150 * | ----- Rw
151 * | | |
152 * vi -----o------+ +-------- vo
153 *
154 *
155 * In order to calculalate the current through the VCR, its gate voltage
156 * must be determined.
157 *
158 * Assuming triode mode and applying Kirchoff's current law, we get the
159 * following equation for Vg:
160 *
161 * u*Cox/2*W/L*((nVddt - Vg)^2 - (nVddt - vi)^2 + (nVddt - Vg)^2 - (nVddt - Vw)^2) = 0
162 * 2*(nVddt - Vg)^2 - (nVddt - vi)^2 - (nVddt - Vw)^2 = 0
163 * (nVddt - Vg) = sqrt(((nVddt - vi)^2 + (nVddt - Vw)^2)/2)
164 *
165 * Vg = nVddt - sqrt(((nVddt - vi)^2 + (nVddt - Vw)^2)/2)
166 */
167 class Integrator6581
168 {
169 private:
170 unsigned int nVddt_Vw_2;
171 mutable int vx;
172 mutable int vc;
173
174 #ifdef SLOPE_FACTOR
175 // Slope factor n = 1/k
176 // where k is the gate coupling coefficient
177 // k = Cox/(Cox+Cdep) ~ 0.7 (depends on gate voltage)
178 mutable double n;
179 #endif
180 const unsigned short nVddt;
181 const unsigned short nVt;
182 const unsigned short nVmin;
183 const unsigned short nSnake;
184
185 const FilterModelConfig6581* fmc;
186
187 public:
188 Integrator6581(const FilterModelConfig6581* fmc,
189 double WL_snake) :
190 nVddt_Vw_2(0),
191 vx(0),
192 vc(0),
193 #ifdef SLOPE_FACTOR
194 n(1.4),
195 #endif
196 nVddt(fmc->getNormalizedValue(fmc->getVddt())),
197 nVt(fmc->getNormalizedValue(fmc->getVth())),
198 nVmin(fmc->getNVmin()),
199 nSnake(fmc->getNormalizedCurrentFactor(WL_snake)),
200 fmc(fmc) {}
201
202 void setVw(unsigned short Vw) { nVddt_Vw_2 = ((nVddt - Vw) * (nVddt - Vw)) >> 1; }
203
204 int solve(int vi) const;
205 };
206
207 } // namespace reSIDfp
208
209 #if RESID_INLINING || defined(INTEGRATOR_CPP)
210
211 namespace reSIDfp
212 {
213
214 RESID_INLINE
215 int Integrator6581::solve(int vi) const
216 {
217 // Make sure Vgst>0 so we're not in subthreshold mode
218 assert(vx < nVddt);
219
220 // Check that transistor is actually in triode mode
221 // Vds < Vgs - Vth
222 assert(vi < nVddt);
223
224 // "Snake" voltages for triode mode calculation.
225 const unsigned int Vgst = nVddt - vx;
226 const unsigned int Vgdt = nVddt - vi;
227
228 const unsigned int Vgst_2 = Vgst * Vgst;
229 const unsigned int Vgdt_2 = Vgdt * Vgdt;
230
231 // "Snake" current, scaled by (1/m)*2^13*m*2^16*m*2^16*2^-15 = m*2^30
232 const int n_I_snake = nSnake * (static_cast<int>(Vgst_2 - Vgdt_2) >> 15);
233
234 // VCR gate voltage. // Scaled by m*2^16
235 // Vg = Vddt - sqrt(((Vddt - Vw)^2 + Vgdt^2)/2)
236 const int nVg = static_cast<int>(fmc->getVcr_nVg((nVddt_Vw_2 + (Vgdt_2 >> 1)) >> 16));
237 #ifdef SLOPE_FACTOR
238 const double nVp = static_cast<double>(nVg - nVt) / n; // Pinch-off voltage
239 const int kVg = static_cast<int>(nVp + 0.5) - nVmin;
240 #else
241 const int kVg = (nVg - nVt) - nVmin;
242 #endif
243
244 // VCR voltages for EKV model table lookup.
245 const int kVgt_Vs = (vx < kVg) ? kVg - vx : 0;
246 assert(kVgt_Vs < (1 << 16));
247 const int kVgt_Vd = (vi < kVg) ? kVg - vi : 0;
248 assert(kVgt_Vd < (1 << 16));
249
250 // VCR current, scaled by m*2^15*2^15 = m*2^30
251 const unsigned int If = static_cast<unsigned int>(fmc->getVcr_n_Ids_term(kVgt_Vs)) << 15;
252 const unsigned int Ir = static_cast<unsigned int>(fmc->getVcr_n_Ids_term(kVgt_Vd)) << 15;
253 #ifdef SLOPE_FACTOR
254 const double iVcr = static_cast<double>(If - Ir);
255 const int n_I_vcr = static_cast<int>((iVcr * n) + 0.5);
256 #else
257 const int n_I_vcr = If - Ir;
258 #endif
259
260 #ifdef SLOPE_FACTOR
261 // estimate new slope factor based on gate voltage
262 const double gamma = 1.0; // body effect factor
263 const double phi = 0.8; // bulk Fermi potential
264 const double Vp = nVp / fmc->getN16();
265 n = 1. + (gamma / (2. * sqrt(Vp + phi + 4. * fmc->getUt())));
266 assert((n > 1.2) && (n < 1.8));
267 #endif
268
269 // Change in capacitor charge.
270 vc += n_I_snake + n_I_vcr;
271
272 // vx = g(vc)
273 const int tmp = (vc >> 15) + (1 << 15);
274 assert(tmp < (1 << 16));
275 vx = fmc->getOpampRev(tmp);
276
277 // Return vo.
278 return vx - (vc >> 14);
279 }
280
281 } // namespace reSIDfp
282
283 #endif
284
285 #endif