libsidplayfp 1.8.8
Integrator.h
1/*
2 * This file is part of libsidplayfp, a SID player engine.
3 *
4 * Copyright 2011-2013 Leandro Nini <drfiemost@users.sourceforge.net>
5 * Copyright 2007-2010 Antti Lankila
6 * Copyright 2004, 2010 Dag Lem <resid@nimrod.no>
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 INTEGRATOR_H
24#define INTEGRATOR_H
25
26#include <stdint.h>
27#include <cassert>
28
29#include "siddefs-fp.h"
30
31namespace reSIDfp
32{
33
154{
155private:
156 const unsigned short* vcr_kVg;
157 const unsigned short* vcr_n_Ids_term;
158 const unsigned short* opamp_rev;
159
160 unsigned int Vddt_Vw_2;
161 int vx;
162 int vc;
163
164 const unsigned short kVddt;
165 const unsigned short n_snake;
166
167public:
168 Integrator(const unsigned short* vcr_kVg, const unsigned short* vcr_n_Ids_term,
169 const unsigned short* opamp_rev, unsigned short kVddt, unsigned short n_snake) :
170 vcr_kVg(vcr_kVg),
171 vcr_n_Ids_term(vcr_n_Ids_term),
172 opamp_rev(opamp_rev),
173 Vddt_Vw_2(0),
174 vx(0),
175 vc(0),
176 kVddt(kVddt),
177 n_snake(n_snake) {}
178
179 void setVw(unsigned short Vw) { Vddt_Vw_2 = (kVddt - Vw) * (kVddt - Vw) >> 1; }
180
181 int solve(int vi);
182};
183
184} // namespace reSIDfp
185
186#if RESID_INLINING || defined(INTEGRATOR_CPP)
187
188namespace reSIDfp
189{
190
191RESID_INLINE
192int Integrator::solve(int vi)
193{
194 // Check that transistor is actually in triode mode
195 // VDS < VGS - Vth
196 assert(vi < kVddt);
197
198 // "Snake" voltages for triode mode calculation.
199 const unsigned int Vgst = kVddt - vx;
200 const unsigned int Vgdt = kVddt - vi;
201
202 const unsigned int Vgst_2 = Vgst * Vgst;
203 const unsigned int Vgdt_2 = Vgdt * Vgdt;
204
205 // "Snake" current, scaled by (1/m)*2^13*m*2^16*m*2^16*2^-15 = m*2^30
206 const int n_I_snake = n_snake * (static_cast<int>(Vgst_2 - Vgdt_2) >> 15);
207
208 // VCR gate voltage. // Scaled by m*2^16
209 // Vg = Vddt - sqrt(((Vddt - Vw)^2 + Vgdt^2)/2)
210 const int kVg = static_cast<int>(vcr_kVg[(Vddt_Vw_2 + (Vgdt_2 >> 1)) >> 16]);
211
212 // VCR voltages for EKV model table lookup.
213 int Vgs = kVg - vx;
214 if (Vgs < 0) Vgs = 0;
215 assert(Vgs < (1 << 16));
216 int Vgd = kVg - vi;
217 if (Vgd < 0) Vgd = 0;
218 assert(Vgd < (1 << 16));
219
220 // VCR current, scaled by m*2^15*2^15 = m*2^30
221 const int n_I_vcr = static_cast<int>(vcr_n_Ids_term[Vgs] - vcr_n_Ids_term[Vgd]) << 15;
222
223 // Change in capacitor charge.
224 vc += n_I_snake + n_I_vcr;
225
226 // vx = g(vc)
227 const int tmp = (vc >> 15) + (1 << 15);
228 assert(tmp < (1 << 16));
229 vx = opamp_rev[tmp];
230
231 // Return vo.
232 return vx - (vc >> 14);
233}
234
235} // namespace reSIDfp
236
237#endif
238
239#endif
Definition: Integrator.h:154