libsidplayfp  1.8.7
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 
31 namespace reSIDfp
32 {
33 
154 {
155 private:
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 
167 public:
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 
188 namespace reSIDfp
189 {
190 
191 RESID_INLINE
192 int Integrator::solve(int vi)
193 {
194  // "Snake" voltages for triode mode calculation.
195  const unsigned int Vgst = kVddt - vx;
196  const unsigned int Vgdt = kVddt - vi;
197 
198  const unsigned int Vgst_2 = Vgst * Vgst;
199  const unsigned int Vgdt_2 = Vgdt * Vgdt;
200 
201  // "Snake" current, scaled by (1/m)*2^13*m*2^16*m*2^16*2^-15 = m*2^30
202  const int n_I_snake = n_snake * (static_cast<int>(Vgst_2 - Vgdt_2) >> 15);
203 
204  // VCR gate voltage. // Scaled by m*2^16
205  // Vg = Vddt - sqrt(((Vddt - Vw)^2 + Vgdt^2)/2)
206  const int kVg = static_cast<int>(vcr_kVg[(Vddt_Vw_2 + (Vgdt_2 >> 1)) >> 16]);
207 
208  // VCR voltages for EKV model table lookup.
209  int Vgs = kVg - vx;
210  if (Vgs < 0) Vgs = 0;
211  assert(Vgs < (1 << 16));
212  int Vgd = kVg - vi;
213  if (Vgd < 0) Vgd = 0;
214  assert(Vgd < (1 << 16));
215 
216  // VCR current, scaled by m*2^15*2^15 = m*2^30
217  const int n_I_vcr = static_cast<int>(vcr_n_Ids_term[Vgs] - vcr_n_Ids_term[Vgd]) << 15;
218 
219  // Change in capacitor charge.
220  vc += n_I_snake + n_I_vcr;
221 
222  // vx = g(vc)
223  const int tmp = (vc >> 15) + (1 << 15);
224  assert(tmp < (1 << 16));
225  vx = opamp_rev[tmp];
226 
227  // Return vo.
228  return vx - (vc >> 14);
229 }
230 
231 } // namespace reSIDfp
232 
233 #endif
234 
235 #endif
Definition: Dac.cpp:25
Definition: Integrator.h:153