1 /*! \file resid-dtv/sid.h */
2 
3 //  ---------------------------------------------------------------------------
4 //  This file is part of reSID, a MOS6581 SID emulator engine.
5 //  Copyright (C) 2004  Dag Lem <resid@nimrod.no>
6 //
7 //  This program is free software; you can redistribute it and/or modify
8 //  it under the terms of the GNU General Public License as published by
9 //  the Free Software Foundation; either version 2 of the License, or
10 //  (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 //  ---------------------------------------------------------------------------
21 // C64 DTV modifications written by
22 //   Daniel Kahlin <daniel@kahlin.net>
23 // Copyright (C) 2007  Daniel Kahlin <daniel@kahlin.net>
24 
25 #ifndef VICE__SID_H__
26 #define VICE__SID_H__
27 
28 #include "residdtv-config.h"
29 
30 #include "bittrain.h"
31 #include "voice.h"
32 #include "filter.h"
33 #include "extfilt.h"
34 
35 namespace reSID_dtv
36 {
37 
38 class SID
39 {
40 public:
41   SID();
42   ~SID();
43 
44   /* Some hacks to keep DTV looking like regular ReSID engine -- hopefully
45    * removed at some point. */
46   void set_chip_model(chip_model ignored);
47   void set_voice_mask(reg4 mask);
48   void enable_filter(bool enable);
49   void input(short input);
50 
51   void enable_external_filter(bool enable);
52   void adjust_filter_bias(double bias);
53   bool set_sampling_parameters(double clock_freq, sampling_method method,
54 			       double sample_freq, double pass_freq = 20000,
55 			       double filter_scale = 0.97);
56   void adjust_sampling_frequency(double sample_freq);
57 
58   void clock();
59   void clock(cycle_count delta_t);
60   int clock(cycle_count& delta_t, short* buf, int n, int interleave = 1);
61   void reset();
62 
63   // Read/write registers.
64   reg8 read(reg8 offset);
65   void write(reg8 offset, reg8 value);
66 
67   // Read/write state.
68   class State
69   {
70   public:
71     State();
72 
73     char sid_register[0x20];
74 
75     reg8 bus_value;
76 
77     reg24 accumulator[3];
78     reg24 shift_register[3];
79     reg16 rate_counter[3];
80     reg16 rate_counter_period[3];
81     reg16 exponential_counter[3];
82     reg16 exponential_counter_period[3];
83     reg8 envelope_counter[3];
84     EnvelopeGenerator::State envelope_state[3];
85     bool hold_zero[3];
86   };
87 
88   State read_state();
89   void write_state(const State& state);
90 
91   // 16-bit output (AUDIO OUT).
92   int output();
93 
94 protected:
95   static double I0(double x);
96   RESID_INLINE int clock_interpolate(cycle_count& delta_t, short* buf, int n,
97 				     int interleave);
98   RESID_INLINE int clock_resample_interpolate(cycle_count& delta_t, short* buf,
99 					      int n, int interleave);
100   RESID_INLINE int clock_resample_fast(cycle_count& delta_t, short* buf,
101 				       int n, int interleave);
102 
103   Voice voice[3];
104   Filter filter;
105   ExternalFilter extfilt;
106 
107   reg8 bus_value;
108 
109   // Resampling constants.
110   // The error in interpolated lookup is bounded by 1.234/L^2,
111   // while the error in non-interpolated lookup is bounded by
112   // 0.7854/L + 0.4113/L^2, see
113   // http://www-ccrma.stanford.edu/~jos/resample/Choice_Table_Size.html
114   // For a resolution of 16 bits this yields L >= 285 and L >= 51473,
115   // respectively.
116   enum { FIR_N = 125 };
117   enum { FIR_RES_INTERPOLATE = 285 };
118   enum { FIR_RES_FAST = 51473 };
119   enum { FIR_SHIFT = 15 };
120   enum { RINGSIZE = 4096 };
121 
122   // Fixpoint constants (16.16 bits).
123   enum { FIXP_SHIFT = 16 };
124   enum { FIXP_MASK = 0xffff };
125 
126   // for DTV volume bittrain emulation
127   unsigned int master_volume;
128 
129   // Sampling variables.
130   sampling_method sampling;
131   cycle_count cycles_per_sample;
132   cycle_count sample_offset;
133   int sample_index;
134   int sample_prev;
135   int fir_N;
136   int fir_RES;
137 
138   // Ring buffer with overflow for contiguous storage of RINGSIZE samples.
139   short* sample;
140 
141   // FIR_RES filter tables (FIR_N*FIR_RES).
142   short* fir;
143 };
144 
145 } // namespace reSID_dtv
146 
147 #endif // not __SID_H__
148