libsidplayfp  1.0.3
FilterModelConfig.h
00001 /*
00002  * This file is part of libsidplayfp, a SID player engine.
00003  *
00004  * Copyright 2011-2013 Leandro Nini <drfiemost@users.sourceforge.net>
00005  * Copyright 2007-2010 Antti Lankila
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020  */
00021 
00022 #ifndef FILTERMODELCONFIG_H
00023 #define FILTERMODELCONFIG_H
00024 
00025 #include <memory>
00026 
00027 #define OPAMP_SIZE 22
00028 #define DAC_SIZE 11
00029 
00030 namespace reSIDfp
00031 {
00032 
00033 class Integrator;
00034 
00037 class FilterModelConfig
00038 {
00039 private:
00040     static std::auto_ptr<FilterModelConfig> instance;
00041     // This allows access to the private constructor
00042     friend class std::auto_ptr<FilterModelConfig>;
00043 
00044     static const double opamp_voltage[OPAMP_SIZE][2];
00045 
00046     const double voice_voltage_range;
00047     const double voice_DC_voltage;
00048 
00049     // Capacitor value.
00050     const double C;
00051 
00052     // Transistor parameters.
00053     const double Vdd;
00054     const double Vth;                   // Threshold voltage
00055     const double uCox_vcr;                      // 1/2*u*Cox
00056     const double WL_vcr;                        // W/L for VCR
00057     const double uCox_snake;            // 1/2*u*Cox
00058     const double WL_snake;                      // W/L for "snake"
00059 
00060     // DAC parameters.
00061     const double dac_zero;
00062     const double dac_scale;
00063     const double dac_2R_div_R;
00064     const bool dac_term;
00065 
00066     /* Derived stuff */
00067     const double vmin, norm;
00068     double opamp_working_point;
00069     unsigned short* mixer[8];
00070     unsigned short* summer[7];
00071     unsigned short* gain[16];
00072     double dac[DAC_SIZE];
00073     unsigned short vcr_Vg[1 << 16];
00074     unsigned short vcr_n_Ids_term[1 << 16];
00075     int opamp_rev[1 << 16];
00076 
00077     double evaluateTransistor(double Vw, double vi, double vx);
00078 
00079     FilterModelConfig();
00080     ~FilterModelConfig();
00081 
00082 public:
00083     static FilterModelConfig* getInstance();
00084 
00085     double getDacZero(double adjustment) const { return dac_zero - (adjustment - 0.5) * 2.; }
00086 
00087     int getVO_T16() const { return (int)(norm * ((1L << 16) - 1) * vmin); }
00088 
00089     int getVoiceScaleS14() const { return (int)((norm * ((1L << 14) - 1)) * voice_voltage_range); }
00090 
00091     int getVoiceDC() const { return (int)((norm * ((1L << 16) - 1)) * (voice_DC_voltage - vmin)); }
00092 
00093     unsigned short** getGain() { return gain; }
00094 
00095     unsigned short** getSummer() { return summer; }
00096 
00097     unsigned short** getMixer() { return mixer; }
00098 
00107     unsigned int* getDAC(double dac_zero) const;
00108 
00109     Integrator* buildIntegrator();
00110 
00124     double estimateFrequency(double dac_zero, int fc);
00125 };
00126 
00127 } // namespace reSIDfp
00128 
00129 #endif