DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/hardware/reSID/sid.h
00001 //  ---------------------------------------------------------------------------
00002 //  This file is part of reSID, a MOS6581 SID emulator engine.
00003 //  Copyright (C) 2004  Dag Lem <resid@nimrod.no>
00004 //
00005 //  This program is free software; you can redistribute it and/or modify
00006 //  it under the terms of the GNU General Public License as published by
00007 //  the Free Software Foundation; either version 2 of the License, or
00008 //  (at your option) any later version.
00009 //
00010 //  This program is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //  GNU General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU General Public License along
00016 //  with this program; if not, write to the Free Software Foundation, Inc.,
00017 //  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 //  ---------------------------------------------------------------------------
00019 
00020 #ifndef __SID_H__
00021 #define __SID_H__
00022 
00023 #include "siddefs.h"
00024 #include "voice.h"
00025 #include "filter.h"
00026 #include "extfilt.h"
00027 #include "pot.h"
00028 
00029 class SID2
00030 {
00031 public:
00032   SID2();
00033   ~SID2();
00034 
00035   void set_chip_model(chip_model model);
00036   void enable_filter(bool enable);
00037   void enable_external_filter(bool enable);
00038   bool set_sampling_parameters(double clock_freq, sampling_method method,
00039                                double sample_freq, double pass_freq /*= -1*/,
00040                                double filter_scale /*= 0.97*/);
00041   void adjust_sampling_frequency(double sample_freq);
00042 
00043   void fc_default(const fc_point*& points, int& count);
00044   PointPlotter<sound_sample> fc_plotter();
00045 
00046   void clock();
00047   void clock(cycle_count delta_t);
00048   int clock(cycle_count& delta_t, short* buf, int n, int interleave = 1);
00049   void reset();
00050   
00051   // Read/write registers.
00052   reg8 read(reg8 offset);
00053   void write(reg8 offset, reg8 value);
00054 
00055   // Read/write state.
00056   class State
00057   {
00058   public:
00059     State();
00060 
00061     char sid_register[0x20] = {};
00062 
00063     reg8 bus_value;
00064     cycle_count bus_value_ttl;
00065 
00066     reg24 accumulator[3] = {};
00067     reg24 shift_register[3] = {};
00068     reg16 rate_counter[3] = {};
00069     reg16 rate_counter_period[3] = {};
00070     reg16 exponential_counter[3] = {};
00071     reg16 exponential_counter_period[3] = {};
00072     reg8 envelope_counter[3] = {};
00073     EnvelopeGenerator::State envelope_state[3] = {};
00074     bool hold_zero[3] = {};
00075         };
00076     
00077   State read_state();
00078   void write_state(const State& state);
00079 
00080   // 16-bit input (EXT IN).
00081   void input(int sample);
00082 
00083   // 16-bit output (AUDIO OUT).
00084   int output();
00085   // n-bit output.
00086   int output(int bits);
00087 
00088         void SaveState( std::ostream& stream );
00089         void LoadState( std::istream& stream );
00090 
00091 protected:
00092   static double I0(double x);
00093   RESID_INLINE int clock_fast(cycle_count& delta_t, short* buf, int n,
00094                               int interleave);
00095   RESID_INLINE int clock_interpolate(cycle_count& delta_t, short* buf, int n,
00096                                      int interleave);
00097   RESID_INLINE int clock_resample_interpolate(cycle_count& delta_t, short* buf,
00098                                               int n, int interleave);
00099   RESID_INLINE int clock_resample_fast(cycle_count& delta_t, short* buf,
00100                                        int n, int interleave);
00101 
00102   Voice voice[3];
00103   Filter filter;
00104   ExternalFilter extfilt;
00105   Potentiometer potx;
00106   Potentiometer poty;
00107 
00108   reg8 bus_value;
00109   cycle_count bus_value_ttl;
00110 
00111   double clock_frequency;
00112 
00113   // External audio input.
00114   int ext_in;
00115 
00116   // Resampling constants.
00117   // The error in interpolated lookup is bounded by 1.234/L^2,
00118   // while the error in non-interpolated lookup is bounded by
00119   // 0.7854/L + 0.4113/L^2, see
00120   // http://www-ccrma.stanford.edu/~jos/resample/Choice_Table_Size.html
00121   // For a resolution of 16 bits this yields L >= 285 and L >= 51473,
00122   // respectively.
00123   static const int FIR_N = 125;
00124 #ifdef __APPLE__
00125 #define FIR_RES_INTERPOLATE 285
00126 #define FIR_RES_FAST 51473
00127 #else
00128   static const int FIR_RES_INTERPOLATE = 285;
00129   static const int FIR_RES_FAST = 51473;
00130 #endif
00131   static const int FIR_SHIFT = 15;
00132   static const int RINGSIZE = 16384;
00133 
00134   // Fixpoint constants (16.16 bits).
00135   static const int FIXP_SHIFT = 16;
00136   static const int FIXP_MASK = 0xffff;
00137 
00138   // Sampling variables.
00139   sampling_method sampling;
00140   cycle_count cycles_per_sample;
00141   cycle_count sample_offset;
00142   int sample_index;
00143   short sample_prev;
00144   int fir_N;
00145   int fir_RES;
00146 
00147   // Ring buffer with overflow for contiguous storage of RINGSIZE samples.
00148   short* sample;
00149 
00150   // FIR_RES filter tables (FIR_N*FIR_RES).
00151   short* fir;
00152 };
00153 
00154 #endif // not __SID_H__