1 //  ---------------------------------------------------------------------------
2 //  This file is part of reSID, a MOS6581 SID emulator engine.
3 //  Copyright (C) 2004  Dag Lem <resid@nimrod.no>
4 //
5 //  This program is free software; you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation; either version 2 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 //  ---------------------------------------------------------------------------
19 
20 #ifndef __SID_H__
21 #define __SID_H__
22 
23 #include "siddefs.h"
24 #include "voice.h"
25 #include "filter.h"
26 #include "extfilt.h"
27 #include "pot.h"
28 
29 RESID_NAMESPACE_START
30 
31 class SID
32 {
33 public:
34   SID();
35   ~SID();
36 
37   void set_chip_model(chip_model model);
38   void enable_filter(bool enable);
39   void enable_external_filter(bool enable);
40   bool set_sampling_parameters(double clock_freq, sampling_method method,
41 			       double sample_freq, double pass_freq = -1,
42 			       double filter_scale = 0.97);
43   void adjust_sampling_frequency(double sample_freq);
44 
45   void fc_default(const fc_point*& points, int& count);
46   PointPlotter<sound_sample> fc_plotter();
47 
48   void clock();
49   void clock(cycle_count delta_t);
50   int clock(cycle_count& delta_t, short* buf, int n, int interleave = 1);
51   void reset();
52 
53   // Read/write registers.
54   reg8 read(reg8 offset);
55   void write(reg8 offset, reg8 value);
56   void mute(reg8 channel, bool enable);
57 
58   // Read/write state.
59   class State
60   {
61   public:
62     State();
63 
64     char sid_register[0x20];
65 
66     reg8 bus_value;
67     cycle_count bus_value_ttl;
68 
69     reg24 accumulator[3];
70     reg24 shift_register[3];
71     reg16 rate_counter[3];
72     reg16 rate_counter_period[3];
73     reg16 exponential_counter[3];
74     reg16 exponential_counter_period[3];
75     reg8 envelope_counter[3];
76     EnvelopeGenerator::State envelope_state[3];
77     bool hold_zero[3];
78   };
79 
80   State read_state();
81   void write_state(const State& state);
82 
83   // 16-bit input (EXT IN).
84   void input(int sample);
85 
86   // 16-bit output (AUDIO OUT).
87   int output();
88   // n-bit output.
89   int output(int bits);
90 
91 protected:
92   static double I0(double x);
93   RESID_INLINE int clock_fast(cycle_count& delta_t, short* buf, int n,
94 			      int interleave);
95   RESID_INLINE int clock_interpolate(cycle_count& delta_t, short* buf, int n,
96 				     int interleave);
97   RESID_INLINE int clock_resample_interpolate(cycle_count& delta_t, short* buf,
98 					      int n, int interleave);
99   RESID_INLINE int clock_resample_fast(cycle_count& delta_t, short* buf,
100 				       int n, int interleave);
101 
102   Voice voice[3];
103   Filter filter;
104   ExternalFilter extfilt;
105   Potentiometer potx;
106   Potentiometer poty;
107 
108   reg8 bus_value;
109   cycle_count bus_value_ttl;
110 
111   double clock_frequency;
112 
113   // External audio input.
114   int ext_in;
115 
116   // Resampling constants.
117   static const int FIR_N;
118   static const int FIR_RES_INTERPOLATE;
119   static const int FIR_RES_FAST;
120   static const int FIR_SHIFT;
121   static const int RINGSIZE;
122 
123   // Fixpoint constants.
124   static const int FIXP_SHIFT;
125   static const int FIXP_MASK;
126 
127   // Sampling variables.
128   sampling_method sampling;
129   cycle_count cycles_per_sample;
130   cycle_count sample_offset;
131   int sample_index;
132   short sample_prev;
133   int fir_N;
134   int fir_RES;
135 
136   // Ring buffer with overflow for contiguous storage of RINGSIZE samples.
137   short* sample;
138 
139   // FIR_RES filter tables (FIR_N*FIR_RES).
140   short* fir;
141 };
142 
143 RESID_NAMESPACE_STOP
144 
145 #endif // not __SID_H__
146