libsidplayfp  2.0.2
Integrator8580.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2018 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 INTEGRATOR8580_H
24 #define INTEGRATOR8580_H
25 
26 #include <stdint.h>
27 #include <cassert>
28 
29 #include "siddefs-fp.h"
30 
31 namespace reSIDfp
32 {
33 
53 {
54 private:
55  const unsigned short* opamp_rev;
56 
57  mutable int vx;
58  mutable int vc;
59 
60  unsigned short kVgt;
61  unsigned short n_dac;
62 
63  const double Vth;
64  const double denorm;
65  const double C;
66  const double k;
67  const double uCox;
68  const double vmin;
69  const double N16;
70 
71 public:
72  Integrator8580(const unsigned short* opamp_rev, double Vth, double denorm, double C, double k, double uCox, double vmin, double N16) :
73  opamp_rev(opamp_rev),
74  vx(0),
75  vc(0),
76  Vth(Vth),
77  denorm(denorm),
78  C(C),
79  k(k),
80  uCox(uCox),
81  vmin(vmin),
82  N16(N16)
83  {
84  setV(1.5);
85  }
86 
87  void setFc(double wl)
88  {
89  // Normalized current factor, 1 cycle at 1MHz.
90  // Fit in 5 bits.
91  const double tmp = denorm * (1 << 13) * (uCox / (2. * k) * wl * 1.0e-6 / C);
92  assert(tmp > -0.5 && tmp < 65535.5);
93  n_dac = static_cast<unsigned short>(tmp + 0.5);
94  }
95 
99  void setV(double v)
100  {
101  // Gate voltage is controlled by the switched capacitor voltage divider
102  // Ua = Ue * v = 4.76v 1<v<2
103  const double Vg = 4.76 * v;
104  const double Vgt = k * (Vg - Vth);
105 
106  // Vg - Vth, normalized so that translated values can be subtracted:
107  // k*Vgt - x = (k*Vgt - t) - (x - t)
108  const double tmp = N16 * (Vgt - vmin);
109  assert(tmp > -0.5 && tmp < 65535.5);
110  kVgt = static_cast<unsigned short>(tmp + 0.5);
111  }
112 
113  int solve(int vi) const;
114 };
115 
116 } // namespace reSIDfp
117 
118 #if RESID_INLINING || defined(INTEGRATOR8580_CPP)
119 
120 namespace reSIDfp
121 {
122 
123 RESID_INLINE
124 int Integrator8580::solve(int vi) const
125 {
126  // DAC voltages
127  const unsigned int Vgst = kVgt - vx;
128  const unsigned int Vgdt = (vi < kVgt) ? kVgt - vi : 0; // triode/saturation mode
129 
130  const unsigned int Vgst_2 = Vgst * Vgst;
131  const unsigned int Vgdt_2 = Vgdt * Vgdt;
132 
133  // DAC current, scaled by (1/m)*2^13*m*2^16*m*2^16*2^-15 = m*2^30
134  const int n_I_dac = n_dac * (static_cast<int>(Vgst_2 - Vgdt_2) >> 15);
135 
136  // Change in capacitor charge.
137  vc += n_I_dac;
138 
139  // vx = g(vc)
140  const int tmp = (vc >> 15) + (1 << 15);
141  assert(tmp < (1 << 16));
142  vx = opamp_rev[tmp];
143 
144  // Return vo.
145  return vx - (vc >> 14);
146 }
147 
148 } // namespace reSIDfp
149 
150 #endif
151 
152 #endif
reSIDfp::Integrator8580::setV
void setV(double v)
Definition: Integrator8580.h:99
reSIDfp::Integrator8580
Definition: Integrator8580.h:53