1 /*
2 * This file is part of libsidplayfp, a SID player engine.
3 *
4 * Copyright 2011-2016 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 #include "Dac.h"
24
25 namespace reSIDfp
26 {
27
Dac(unsigned int bits)28 Dac::Dac(unsigned int bits) :
29 dac(new double[bits]),
30 dacLength(bits)
31 {}
32
~Dac()33 Dac::~Dac()
34 {
35 delete [] dac;
36 }
37
getOutput(unsigned int input) const38 double Dac::getOutput(unsigned int input) const
39 {
40 double dacValue = 0.;
41
42 for (unsigned int i = 0; i < dacLength; i++)
43 {
44 if ((input & (1 << i)) != 0)
45 {
46 dacValue += dac[i];
47 }
48 }
49
50 return dacValue;
51 }
52
kinkedDac(ChipModel chipModel)53 void Dac::kinkedDac(ChipModel chipModel)
54 {
55 const double R_INFINITY = 1e6;
56
57 // Non-linearity parameter, 8580 DACs are perfectly linear
58 const double _2R_div_R = chipModel == MOS6581 ? 2.20 : 2.00;
59
60 // 6581 DACs are not terminated by a 2R resistor
61 const bool term = chipModel == MOS8580;
62
63 // Calculate voltage contribution by each individual bit in the R-2R ladder.
64 for (unsigned int set_bit = 0; set_bit < dacLength; set_bit++)
65 {
66 double Vn = 1.; // Normalized bit voltage.
67 double R = 1.; // Normalized R
68 const double _2R = _2R_div_R * R; // 2R
69 double Rn = term ? // Rn = 2R for correct termination,
70 _2R : R_INFINITY; // INFINITY for missing termination.
71
72 unsigned int bit;
73
74 // Calculate DAC "tail" resistance by repeated parallel substitution.
75 for (bit = 0; bit < set_bit; bit++)
76 {
77 Rn = (Rn == R_INFINITY) ?
78 R + _2R :
79 R + (_2R * Rn) / (_2R + Rn); // R + 2R || Rn
80 }
81
82 // Source transformation for bit voltage.
83 if (Rn == R_INFINITY)
84 {
85 Rn = _2R;
86 }
87 else
88 {
89 Rn = (_2R * Rn) / (_2R + Rn); // 2R || Rn
90 Vn = Vn * Rn / _2R;
91 }
92
93 // Calculate DAC output voltage by repeated source transformation from
94 // the "tail".
95
96 for (++bit; bit < dacLength; bit++)
97 {
98 Rn += R;
99 const double I = Vn / Rn;
100 Rn = (_2R * Rn) / (_2R + Rn); // 2R || Rn
101 Vn = Rn * I;
102 }
103
104 dac[set_bit] = Vn;
105 }
106
107 // Normalize to integerish behavior
108 double Vsum = 0.;
109
110 for (unsigned int i = 0; i < dacLength; i++)
111 {
112 Vsum += dac[i];
113 }
114
115 Vsum /= 1 << dacLength;
116
117 for (unsigned int i = 0; i < dacLength; i++)
118 {
119 dac[i] /= Vsum;
120 }
121 }
122
123 } // namespace reSIDfp
124