Debian Codegraph @

src/resid/filter.cpp

raw symbols SCIP
1 // ---------------------------------------------------------------------------cxx . . $ `<file>/src/resid/filter.cpp`/
2 // This file is part of reSID, a MOS6581 SID emulator engine.
3 // Copyright (C) 2004 Dag Lem <[email protected]>
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 #define __FILTER_CC__cxx . . $ `src/resid/filter.cpp:20:9`!
21 #include "filter.h"cxx . . $ `<file>/src/resid/filter.h`/
22
23 // Maximum cutoff frequency is specified as
24 // FCmax = 2.6e-5/C = 2.6e-5/2200e-12 = 11818.
25 //
26 // Measurements indicate a cutoff frequency range of approximately
27 // 220Hz - 18kHz on a MOS6581 fitted with 470pF capacitors. The function
28 // mapping FC to cutoff frequency has the shape of the tanh function, with
29 // a discontinuity at FCHI = 0x80.
30 // In contrast, the MOS8580 almost perfectly corresponds with the
31 // specification of a linear mapping from 30Hz to 12kHz.
32 //
33 // The mappings have been measured by feeding the SID with an external
34 // signal since the chip itself is incapable of generating waveforms of
35 // higher fundamental frequency than 4kHz. It is best to use the bandpass
36 // output at full resonance to pick out the cutoff frequency at any given
37 // FC setting.
38 //
39 // The mapping function is specified with spline interpolation points and
40 // the function values are retrieved via table lookup.
41 //
42 // NB! Cutoff frequency characteristics may vary, we have modeled two
43 // particular Commodore 64s.
44
45 fc_pointcxx . . $ fc_point# Filtercxx . . $ Filter#::f0_points_6581cxx . . $ Filter#f0_points_6581.[] =
46 {
47 // FC f FCHI FCLO
48 // ----------------------------
49 { 0, 220 }, // 0x00 - repeated end point
50 { 0, 220 }, // 0x00
51 { 128, 230 }, // 0x10
52 { 256, 250 }, // 0x20
53 { 384, 300 }, // 0x30
54 { 512, 420 }, // 0x40
55 { 640, 780 }, // 0x50
56 { 768, 1600 }, // 0x60
57 { 832, 2300 }, // 0x68
58 { 896, 3200 }, // 0x70
59 { 960, 4300 }, // 0x78
60 { 992, 5000 }, // 0x7c
61 { 1008, 5400 }, // 0x7e
62 { 1016, 5700 }, // 0x7f
63 { 1023, 6000 }, // 0x7f 0x07
64 { 1023, 6000 }, // 0x7f 0x07 - discontinuity
65 { 1024, 4600 }, // 0x80 -
66 { 1024, 4600 }, // 0x80
67 { 1032, 4800 }, // 0x81
68 { 1056, 5300 }, // 0x84
69 { 1088, 6000 }, // 0x88
70 { 1120, 6600 }, // 0x8c
71 { 1152, 7200 }, // 0x90
72 { 1280, 9500 }, // 0xa0
73 { 1408, 12000 }, // 0xb0
74 { 1536, 14500 }, // 0xc0
75 { 1664, 16000 }, // 0xd0
76 { 1792, 17100 }, // 0xe0
77 { 1920, 17700 }, // 0xf0
78 { 2047, 18000 }, // 0xff 0x07
79 { 2047, 18000 } // 0xff 0x07 - repeated end point
80 };
81
82 fc_pointcxx . . $ fc_point# Filtercxx . . $ Filter#::f0_points_8580cxx . . $ Filter#f0_points_8580.[] =
83 {
84 // FC f FCHI FCLO
85 // ----------------------------
86 { 0, 0 }, // 0x00 - repeated end point
87 { 0, 0 }, // 0x00
88 { 128, 800 }, // 0x10
89 { 256, 1600 }, // 0x20
90 { 384, 2500 }, // 0x30
91 { 512, 3300 }, // 0x40
92 { 640, 4100 }, // 0x50
93 { 768, 4800 }, // 0x60
94 { 896, 5600 }, // 0x70
95 { 1024, 6500 }, // 0x80
96 { 1152, 7500 }, // 0x90
97 { 1280, 8400 }, // 0xa0
98 { 1408, 9200 }, // 0xb0
99 { 1536, 9800 }, // 0xc0
100 { 1664, 10500 }, // 0xd0
101 { 1792, 11000 }, // 0xe0
102 { 1920, 11700 }, // 0xf0
103 { 2047, 12500 }, // 0xff 0x07
104 { 2047, 12500 } // 0xff 0x07 - repeated end point
105 };
106
107
108 // ----------------------------------------------------------------------------
109 // Constructor.
110 // ----------------------------------------------------------------------------
111 Filtercxx . . $ Filter#::Filtercxx . . $ Filter#Filter(49f6e7a06ebc5aa8).()
112 {
113 fccxx . . $ Filter#fc. = 0;
114
115 rescxx . . $ Filter#res. = 0;
116
117 filtcxx . . $ Filter#filt. = 0;
118
119 voice3offcxx . . $ Filter#voice3off. = 0;
120
121 hp_bp_lpcxx . . $ Filter#hp_bp_lp. = 0;
122
123 volcxx . . $ Filter#vol. = 0;
124
125 // State of filter.
126 Vhpcxx . . $ Filter#Vhp. = 0;
127 Vbpcxx . . $ Filter#Vbp. = 0;
128 Vlpcxx . . $ Filter#Vlp. = 0;
129 Vnfcxx . . $ Filter#Vnf. = 0;
130
131 enable_filtercxx . . $ Filter#enable_filter(b45ddcbc8423892c).(true);
132
133 // Create mappings from FC to cutoff frequency.
134 interpolatecxx . . $ interpolate(9b4d3232a0b85fb9).(f0_points_6581cxx . . $ Filter#f0_points_6581., f0_points_6581cxx . . $ Filter#f0_points_6581.
135 + sizeof(f0_points_6581cxx . . $ Filter#f0_points_6581.)/sizeof(*f0_points_6581cxx . . $ Filter#f0_points_6581.) - 1,
136 PointPlottercxx . . $ PointPlotter#<sound_samplecxx . . $ sound_sample#>(f0_6581cxx . . $ Filter#f0_6581.), 1.0);
137 interpolatecxx . . $ interpolate(9b4d3232a0b85fb9).(f0_points_8580cxx . . $ Filter#f0_points_8580., f0_points_8580cxx . . $ Filter#f0_points_8580.
138 + sizeof(f0_points_8580cxx . . $ Filter#f0_points_8580.)/sizeof(*f0_points_8580cxx . . $ Filter#f0_points_8580.) - 1,
139 PointPlottercxx . . $ PointPlotter#<sound_samplecxx . . $ sound_sample#>(f0_8580cxx . . $ Filter#f0_8580.), 1.0);
140
141 set_chip_modelcxx . . $ Filter#set_chip_model(cea143c94d31b99f).(MOS6581cxx . . $ MOS6581.);
142 }
143
144
145 // ----------------------------------------------------------------------------
146 // Enable filter.
147 // ----------------------------------------------------------------------------
148 void Filtercxx . . $ Filter#::enable_filtercxx . . $ Filter#enable_filter(b45ddcbc8423892c).(bool enable)
149 {
150 enabledcxx . . $ Filter#enabled. = enable;
151 }
152
153
154 // ----------------------------------------------------------------------------
155 // Set chip model.
156 // ----------------------------------------------------------------------------
157 void Filtercxx . . $ Filter#::set_chip_modelcxx . . $ Filter#set_chip_model(cea143c94d31b99f).(chip_modelcxx . . $ chip_model# model)
158 {
159 if (model == MOS6581cxx . . $ MOS6581.) {
160 // The mixer has a small input DC offset. This is found as follows:
161 //
162 // The "zero" output level of the mixer measured on the SID audio
163 // output pin is 5.50V at zero volume, and 5.44 at full
164 // volume. This yields a DC offset of (5.44V - 5.50V) = -0.06V.
165 //
166 // The DC offset is thus -0.06V/1.05V ~ -1/18 of the dynamic range
167 // of one voice. See voice.cc for measurement of the dynamic
168 // range.
169
170 mixer_DCcxx . . $ Filter#mixer_DC. = -0xfff*0xff/18 >> 7;
171
172 f0cxx . . $ Filter#f0. = f0_6581cxx . . $ Filter#f0_6581.;
173 f0_pointscxx . . $ Filter#f0_points. = f0_points_6581cxx . . $ Filter#f0_points_6581.;
174 f0_countcxx . . $ Filter#f0_count. = sizeof(f0_points_6581cxx . . $ Filter#f0_points_6581.)/sizeof(*f0_points_6581cxx . . $ Filter#f0_points_6581.);
175 }
176 else {
177 // No DC offsets in the MOS8580.
178 mixer_DCcxx . . $ Filter#mixer_DC. = 0;
179
180 f0cxx . . $ Filter#f0. = f0_8580cxx . . $ Filter#f0_8580.;
181 f0_pointscxx . . $ Filter#f0_points. = f0_points_8580cxx . . $ Filter#f0_points_8580.;
182 f0_countcxx . . $ Filter#f0_count. = sizeof(f0_points_8580cxx . . $ Filter#f0_points_8580.)/sizeof(*f0_points_8580cxx . . $ Filter#f0_points_8580.);
183 }
184
185 set_w0cxx . . $ Filter#set_w0(49f6e7a06ebc5aa8).();
186 set_Qcxx . . $ Filter#set_Q(49f6e7a06ebc5aa8).();
187 }
188
189
190 // ----------------------------------------------------------------------------
191 // SID reset.
192 // ----------------------------------------------------------------------------
193 void Filtercxx . . $ Filter#::resetcxx . . $ Filter#reset(49f6e7a06ebc5aa8).()
194 {
195 fccxx . . $ Filter#fc. = 0;
196
197 rescxx . . $ Filter#res. = 0;
198
199 filtcxx . . $ Filter#filt. = 0;
200
201 voice3offcxx . . $ Filter#voice3off. = 0;
202
203 hp_bp_lpcxx . . $ Filter#hp_bp_lp. = 0;
204
205 volcxx . . $ Filter#vol. = 0;
206
207 // State of filter.
208 Vhpcxx . . $ Filter#Vhp. = 0;
209 Vbpcxx . . $ Filter#Vbp. = 0;
210 Vlpcxx . . $ Filter#Vlp. = 0;
211 Vnfcxx . . $ Filter#Vnf. = 0;
212
213 set_w0cxx . . $ Filter#set_w0(49f6e7a06ebc5aa8).();
214 set_Qcxx . . $ Filter#set_Q(49f6e7a06ebc5aa8).();
215 }
216
217
218 // ----------------------------------------------------------------------------
219 // Register functions.
220 // ----------------------------------------------------------------------------
221 void Filtercxx . . $ Filter#::writeFC_LOcxx . . $ Filter#writeFC_LO(cb2f890c2fdad230).(reg8cxx . . $ reg8# fc_lo)
222 {
223 fccxx . . $ Filter#fc. = fccxx . . $ Filter#fc. & 0x7f8 | fc_lo & 0x007;
224 set_w0cxx . . $ Filter#set_w0(49f6e7a06ebc5aa8).();
225 }
226
227 void Filtercxx . . $ Filter#::writeFC_HIcxx . . $ Filter#writeFC_HI(cb2f890c2fdad230).(reg8cxx . . $ reg8# fc_hi)
228 {
229 fccxx . . $ Filter#fc. = (fc_hi << 3) & 0x7f8 | fccxx . . $ Filter#fc. & 0x007;
230 set_w0cxx . . $ Filter#set_w0(49f6e7a06ebc5aa8).();
231 }
232
233 void Filtercxx . . $ Filter#::writeRES_FILTcxx . . $ Filter#writeRES_FILT(cb2f890c2fdad230).(reg8cxx . . $ reg8# res_filt)
234 {
235 rescxx . . $ Filter#res. = (res_filt >> 4) & 0x0f;
236 set_Qcxx . . $ Filter#set_Q(49f6e7a06ebc5aa8).();
237
238 filtcxx . . $ Filter#filt. = res_filt & 0x0f;
239 }
240
241 void Filtercxx . . $ Filter#::writeMODE_VOLcxx . . $ Filter#writeMODE_VOL(cb2f890c2fdad230).(reg8cxx . . $ reg8# mode_vol)
242 {
243 voice3offcxx . . $ Filter#voice3off. = mode_vol & 0x80;
244
245 hp_bp_lpcxx . . $ Filter#hp_bp_lp. = (mode_vol >> 4) & 0x07;
246
247 volcxx . . $ Filter#vol. = mode_vol & 0x0f;
248 }
249
250 // Set filter cutoff frequency.
251 void Filtercxx . . $ Filter#::set_w0cxx . . $ Filter#set_w0(49f6e7a06ebc5aa8).()
252 {
253 const double pi = 3.1415926535897932385;
254
255 // Multiply with 1.048576 to facilitate division by 1 000 000 by right-
256 // shifting 20 times (2 ^ 20 = 1048576).
257 w0cxx . . $ Filter#w0. = static_cast<sound_samplecxx . . $ sound_sample#>(2*pi*f0cxx . . $ Filter#f0.[fccxx . . $ Filter#fc.]*1.048576);
258
259 // Limit f0 to 16kHz to keep 1 cycle filter stable.
260 const sound_samplecxx . . $ sound_sample# w0_max_1 = static_cast<sound_samplecxx . . $ sound_sample#>(2*pi*16000*1.048576);
261 w0_ceil_1cxx . . $ Filter#w0_ceil_1. = w0cxx . . $ Filter#w0. <= w0_max_1 ? w0cxx . . $ Filter#w0. : w0_max_1;
262
263 // Limit f0 to 4kHz to keep delta_t cycle filter stable.
264 const sound_samplecxx . . $ sound_sample# w0_max_dt = static_cast<sound_samplecxx . . $ sound_sample#>(2*pi*4000*1.048576);
265 w0_ceil_dtcxx . . $ Filter#w0_ceil_dt. = w0cxx . . $ Filter#w0. <= w0_max_dt ? w0cxx . . $ Filter#w0. : w0_max_dt;
266 }
267
268 // Set filter resonance.
269 void Filtercxx . . $ Filter#::set_Qcxx . . $ Filter#set_Q(49f6e7a06ebc5aa8).()
270 {
271 // Q is controlled linearly by res. Q has approximate range [0.707, 1.7].
272 // As resonance is increased, the filter must be clocked more often to keep
273 // stable.
274
275 // The coefficient 1024 is dispensed of later by right-shifting 10 times
276 // (2 ^ 10 = 1024).
277 _1024_div_Qcxx . . $ Filter#_1024_div_Q. = static_cast<sound_samplecxx . . $ sound_sample#>(1024.0/(0.707 + 1.0*rescxx . . $ Filter#res./0x0f));
278 }
279
280 // ----------------------------------------------------------------------------
281 // Spline functions.
282 // ----------------------------------------------------------------------------
283
284 // ----------------------------------------------------------------------------
285 // Return the array of spline interpolation points used to map the FC register
286 // to filter cutoff frequency.
287 // ----------------------------------------------------------------------------
288 void Filtercxx . . $ Filter#::fc_defaultcxx . . $ Filter#fc_default(c5cb538b22a2051f).(const fc_pointcxx . . $ fc_point#*& points, int& count)
289 {
290 points = f0_pointscxx . . $ Filter#f0_points.;
291 count = f0_countcxx . . $ Filter#f0_count.;
292 }
293
294 // ----------------------------------------------------------------------------
295 // Given an array of interpolation points p with n points, the following
296 // statement will specify a new FC mapping:
297 // interpolate(p, p + n - 1, filter.fc_plotter(), 1.0);
298 // Note that the x range of the interpolation points *must* be [0, 2047],
299 // and that additional end points *must* be present since the end points
300 // are not interpolated.
301 // ----------------------------------------------------------------------------
302 PointPlottercxx . . $ PointPlotter#<sound_samplecxx . . $ sound_sample#> Filtercxx . . $ Filter#::fc_plottercxx . . $ Filter#fc_plotter(d92c3fe0c353d99b).()
303 {
304 return PointPlottercxx . . $ PointPlotter#<sound_samplecxx . . $ sound_sample#>(f0cxx . . $ Filter#f0.);
305 }