libsidplayfp  2.0.3
Integrator.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2020 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  // Make sure Vgst>0 so we're not in subthreshold mode
195  assert(vx < kVddt);
196 
197  // Check that transistor is actually in triode mode
198  // Vds < Vgs - Vth
199  assert(vi < kVddt);
200 
201  // "Snake" voltages for triode mode calculation.
202  const unsigned int Vgst = kVddt - vx;
203  const unsigned int Vgdt = kVddt - vi;
204 
205  const unsigned int Vgst_2 = Vgst * Vgst;
206  const unsigned int Vgdt_2 = Vgdt * Vgdt;
207 
208  // "Snake" current, scaled by (1/m)*2^13*m*2^16*m*2^16*2^-15 = m*2^30
209  const int n_I_snake = n_snake * (static_cast<int>(Vgst_2 - Vgdt_2) >> 15);
210 
211  // VCR gate voltage. // Scaled by m*2^16
212  // Vg = Vddt - sqrt(((Vddt - Vw)^2 + Vgdt^2)/2)
213  const int kVg = static_cast<int>(vcr_kVg[(Vddt_Vw_2 + (Vgdt_2 >> 1)) >> 16]);
214 
215  // VCR voltages for EKV model table lookup.
216  int Vgs = kVg - vx;
217  if (Vgs < 0) Vgs = 0;
218  assert(Vgs < (1 << 16));
219  int Vgd = kVg - vi;
220  if (Vgd < 0) Vgd = 0;
221  assert(Vgd < (1 << 16));
222 
223  // VCR current, scaled by m*2^15*2^15 = m*2^30
224  const unsigned int If = static_cast<unsigned int>(vcr_n_Ids_term[Vgs]) << 15;
225  const unsigned int Ir = static_cast<unsigned int>(vcr_n_Ids_term[Vgd]) << 15;
226  const int n_I_vcr = If - Ir;
227 
228  // Change in capacitor charge.
229  vc += n_I_snake + n_I_vcr;
230 
231  // vx = g(vc)
232  const int tmp = (vc >> 15) + (1 << 15);
233  assert(tmp < (1 << 16));
234  vx = opamp_rev[tmp];
235 
236  // Return vo.
237  return vx - (vc >> 14);
238 }
239 
240 } // namespace reSIDfp
241 
242 #endif
243 
244 #endif
reSIDfp::Integrator
Definition: Integrator.h:153