Debian Codegraph @

src/resid-fp/wavefp.cpp

raw symbols SCIP
1 // ---------------------------------------------------------------------------cxx . . $ `<file>/src/resid-fp/wavefp.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 #include "wavefp.h"cxx . . $ `<file>/src/resid-fp/wavefp.h`/
21 #include "sidfp.h"cxx . . $ `<file>/src/resid-fp/sidfp.h`/
22
23 typedef structcxx . . $ $anonymous_type_2aa0b9d6a3fa803e_0# {
24 float biascxx . . $ $anonymous_type_2aa0b9d6a3fa803e_0#bias.;
25 float pulsestrengthcxx . . $ $anonymous_type_2aa0b9d6a3fa803e_0#pulsestrength.;
26 float topbitcxx . . $ $anonymous_type_2aa0b9d6a3fa803e_0#topbit.;
27 float distancecxx . . $ $anonymous_type_2aa0b9d6a3fa803e_0#distance.;
28 float stmixcxx . . $ $anonymous_type_2aa0b9d6a3fa803e_0#stmix.;
29 } waveformconfig_tcxx . . $ waveformconfig_t#;
30
31 const float sharpnesscxx . . $ sharpness. = 512.f;
32 const waveformconfig_tcxx . . $ waveformconfig_t# wfconfigcxx . . $ wfconfig.[2][5] = { {
33 /* kevtris chip G (6581) */
34 { 0.880815f, 0.f, 0.f, 0.3279614f, 0.5999545f }, // error 1795
35 { 0.8924618f, 2.014781f, 1.003332f, 0.02992322f, 0.0f }, // error 11610
36 { 0.8646501f, 1.712586f, 1.137704f, 0.02845423f, 0.f }, // error 21307
37 { 0.9527834f, 1.794777f, 0.f, 0.09806272f, 0.7752482f }, // error 196
38 { 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, },
39 }, {
40 /* kevtris chip V (8580) */
41 { 0.9781665f, 0.f, 0.9899469f, 8.087667f, 0.8226412f }, // error 5546
42 { 0.9097769f, 2.039997f, 0.9584096f, 0.1765447f, 0.f }, // error 18763
43 { 0.9231212f, 2.084788f, 0.9493895f, 0.1712518f, 0.f }, // error 17103
44 { 0.9845552f, 1.415612f, 0.9703883f, 3.68829f, 0.8265008f }, // error 3319
45 { 0.5f, 0.0f, 1.0f, 0.0f, 0.0f },
46 }
47 };
48
49 /* render output from bitstate */
50 static float make_samplecxx . . $ make_sample(b845eb43615009fa).(float *o) {
51 float out = 0;
52 for (int i = 0; i < 12; i ++) {
53 out += o[i] * daccxx . . $ dac.[i];
54 }
55 return out;
56 }
57
58 /* generate tables for waveforms 1 .. 7 */
59 void WaveformGeneratorFPcxx . . $ WaveformGeneratorFP#::rebuild_wftablecxx . . $ WaveformGeneratorFP#rebuild_wftable(49f6e7a06ebc5aa8).() {
60 float o[12];
61 reg8cxx . . $ reg8# oldwf = waveformcxx . . $ WaveformGeneratorFP#waveform.;
62 reg32cxx . . $ reg32# oldacc = accumulatorcxx . . $ WaveformGeneratorFP#accumulator.;
63 reg12cxx . . $ reg12# oldpw = pwcxx . . $ WaveformGeneratorFP#pw.;
64
65 for (waveformcxx . . $ WaveformGeneratorFP#waveform. = 1; waveformcxx . . $ WaveformGeneratorFP#waveform. < 8; waveformcxx . . $ WaveformGeneratorFP#waveform. ++) {
66 for (accumulatorcxx . . $ WaveformGeneratorFP#accumulator. = 0; accumulatorcxx . . $ WaveformGeneratorFP#accumulator. < (1<<24); accumulatorcxx . . $ WaveformGeneratorFP#accumulator. += (1<<12)) {
67 /* generate pulse-low variants. Also,
68 * when waveform < 4, pw doesn't matter. */
69 pwcxx . . $ WaveformGeneratorFP#pw. = 0x1000; /* pulse always low */
70 calculate_waveform_samplecxx . . $ WaveformGeneratorFP#calculate_waveform_sample(e6881f009d13a851).(o);
71 wftablecxx . . $ wftable.[waveformcxx . . $ WaveformGeneratorFP#waveform. - 1][accumulatorcxx . . $ WaveformGeneratorFP#accumulator. >> 12] = make_samplecxx . . $ make_sample(b845eb43615009fa).(o) + wave_zerocxx . . $ WaveformGeneratorFP#wave_zero.;
72 /* Add pulse-high variants after pulse-low state variants */
73 if (waveformcxx . . $ WaveformGeneratorFP#waveform. >= 4) {
74 pwcxx . . $ WaveformGeneratorFP#pw. = 0x000; /* pulse always high */
75 calculate_waveform_samplecxx . . $ WaveformGeneratorFP#calculate_waveform_sample(e6881f009d13a851).(o);
76 wftablecxx . . $ wftable.[waveformcxx . . $ WaveformGeneratorFP#waveform. + 3][accumulatorcxx . . $ WaveformGeneratorFP#accumulator. >> 12] = make_samplecxx . . $ make_sample(b845eb43615009fa).(o) + wave_zerocxx . . $ WaveformGeneratorFP#wave_zero.;
77 }
78 }
79 }
80
81 waveformcxx . . $ WaveformGeneratorFP#waveform. = oldwf;
82 accumulatorcxx . . $ WaveformGeneratorFP#accumulator. = oldacc;
83 pwcxx . . $ WaveformGeneratorFP#pw. = oldpw;
84 }
85
86 /* explode reg12 to a floating point bit array */
87 static void populatecxx . . $ populate(93dac818ae82d741).(reg12cxx . . $ reg12# v, float o[12])
88 {
89 int j = 1;
90 for (int i = 0; i < 12; i ++) {
91 o[i] = v & j ? 1.f : 0.f;
92 j <<= 1;
93 }
94 }
95
96 /* waveform values valid are 1 .. 7 */
97 void WaveformGeneratorFPcxx . . $ WaveformGeneratorFP#::calculate_waveform_samplecxx . . $ WaveformGeneratorFP#calculate_waveform_sample(e6881f009d13a851).(float o[12])
98 {
99 int i;
100
101 /* P */
102 if (waveformcxx . . $ WaveformGeneratorFP#waveform. == 4) {
103 populatecxx . . $ populate(93dac818ae82d741).((accumulatorcxx . . $ WaveformGeneratorFP#accumulator. >> 12) >= pwcxx . . $ WaveformGeneratorFP#pw. ? 0xfff : 0x000, o);
104 return;
105 }
106
107 const waveformconfig_tcxx . . $ waveformconfig_t# config = wfconfigcxx . . $ wfconfig.[
108 modelcxx . . $ WaveformGeneratorFP#model. == MOS6581cxx . . $ MOS6581. ? 0 : 1
109 ][
110 waveformcxx . . $ WaveformGeneratorFP#waveform. == 3 ? 0 :
111 waveformcxx . . $ WaveformGeneratorFP#waveform. == 5 ? 1 :
112 waveformcxx . . $ WaveformGeneratorFP#waveform. == 6 ? 2 :
113 waveformcxx . . $ WaveformGeneratorFP#waveform. == 7 ? 3 :
114 4
115 ];
116
117 /* S with strong top bit for 6581 */
118 populatecxx . . $ populate(93dac818ae82d741).(accumulatorcxx . . $ WaveformGeneratorFP#accumulator. >> 12, o);
119
120 /* convert to T */
121 if ((waveformcxx . . $ WaveformGeneratorFP#waveform. & 3) == 1) {
122 bool top = (accumulatorcxx . . $ WaveformGeneratorFP#accumulator. & 0x800000) != 0;
123 for (i = 11; i > 0; i --) {
124 if (top) {
125 o[i] = 1.0f - o[i-1];
126 } else {
127 o[i] = o[i-1];
128 }
129 }
130 o[0] = 0;
131 }
132
133 /* convert to ST */
134 if ((waveformcxx . . $ WaveformGeneratorFP#waveform. & 3) == 3) {
135 /* bottom bit is grounded via T waveform selector */
136 o[0] *= config.stmixcxx . . $ $anonymous_type_2aa0b9d6a3fa803e_0#stmix.;
137 for (i = 1; i < 12; i ++) {
138 o[i] = o[i - 1] * (1.f - config.stmixcxx . . $ $anonymous_type_2aa0b9d6a3fa803e_0#stmix.) + o[i] * config.stmixcxx . . $ $anonymous_type_2aa0b9d6a3fa803e_0#stmix.;
139 }
140 }
141 o[11] *= config.topbitcxx . . $ $anonymous_type_2aa0b9d6a3fa803e_0#topbit.;
142
143 /* ST, P* waveform? */
144 if (waveformcxx . . $ WaveformGeneratorFP#waveform. == 3 || waveformcxx . . $ WaveformGeneratorFP#waveform. > 4) {
145 float distancetable[12 * 2 + 1];
146 for (i = 0; i <= 12; i ++) {
147 distancetable[12+i] = distancetable[12-i] = 1.f / (1.f + i * i * config.distancecxx . . $ $anonymous_type_2aa0b9d6a3fa803e_0#distance.);
148 }
149
150 float pulse = (accumulatorcxx . . $ WaveformGeneratorFP#accumulator. >> 12) >= pwcxx . . $ WaveformGeneratorFP#pw. ? 1.f : -1.f;
151 pulse *= config.pulsestrengthcxx . . $ $anonymous_type_2aa0b9d6a3fa803e_0#pulsestrength.;
152
153 float tmp[12];
154 for (i = 0; i < 12; i ++) {
155 float avg = 0;
156 float n = 0;
157 for (int j = 0; j < 12; j ++) {
158 float weight = distancetable[i - j + 12];
159 avg += o[j] * weight;
160 n += weight;
161 }
162 /* pulse control bit */
163 if (waveformcxx . . $ WaveformGeneratorFP#waveform. > 4) {
164 float weight = distancetable[i - 12 + 12];
165 avg += pulse * weight;
166 n += weight;
167 }
168
169 tmp[i] = (o[i] + avg / n) * 0.5f;
170 }
171
172 for (i = 0; i < 12; i ++) {
173 o[i] = tmp[i];
174 }
175 }
176
177 /* use the environment around bias value to set/clear dac bit. The
178 * relationship is nonlinear because that seems to sound a bit better. */
179 for (i = 0; i < 12; i ++) {
180 o[i] = (o[i] - config.biascxx . . $ $anonymous_type_2aa0b9d6a3fa803e_0#bias.) * sharpnesscxx . . $ sharpness.;
181
182 o[i] += 0.5f;
183 if (o[i] > 1.f) {
184 o[i] = 1.f;
185 }
186 if (o[i] < 0.f) {
187 o[i] = 0.;
188 }
189 }
190 }
191
192 void WaveformGeneratorFPcxx . . $ WaveformGeneratorFP#::set_nonlinearitycxx . . $ WaveformGeneratorFP#set_nonlinearity(9364e6e7bff7660).(float nl)
193 {
194 for (int i = 0; i < 12; i ++) {
195 daccxx . . $ dac.[i] = SIDFPcxx . . $ SIDFP#::kinked_daccxx . . $ SIDFP#kinked_dac(7555465bc4331e8c).((1 << i), nl, 12);
196 }
197 }
198
199 // ----------------------------------------------------------------------------
200 // Constructor.
201 // ----------------------------------------------------------------------------
202 WaveformGeneratorFPcxx . . $ WaveformGeneratorFP#::WaveformGeneratorFPcxx . . $ WaveformGeneratorFP#WaveformGeneratorFP(49f6e7a06ebc5aa8).()
203 {
204 set_chip_modelcxx . . $ WaveformGeneratorFP#set_chip_model(cea143c94d31b99f).(MOS6581cxx . . $ MOS6581.);
205 resetcxx . . $ WaveformGeneratorFP#reset(49f6e7a06ebc5aa8).();
206 }
207
208 // ----------------------------------------------------------------------------
209 // Set chip model.
210 // ----------------------------------------------------------------------------
211 void WaveformGeneratorFPcxx . . $ WaveformGeneratorFP#::set_chip_modelcxx . . $ WaveformGeneratorFP#set_chip_model(cea143c94d31b99f).(chip_modelcxx . . $ chip_model# model)
212 {
213 this->modelcxx . . $ WaveformGeneratorFP#model. = model;
214 wave_zerocxx . . $ WaveformGeneratorFP#wave_zero. = static_cast<float>(model == MOS6581cxx . . $ MOS6581. ? -0x380 : -0x800);
215 }
216
217
218 // ----------------------------------------------------------------------------
219 // Register functions.
220 // ----------------------------------------------------------------------------
221 void WaveformGeneratorFPcxx . . $ WaveformGeneratorFP#::writeFREQ_LOcxx . . $ WaveformGeneratorFP#writeFREQ_LO(cb2f890c2fdad230).(reg8cxx . . $ reg8# freq_lo)
222 {
223 freqcxx . . $ WaveformGeneratorFP#freq. = (freqcxx . . $ WaveformGeneratorFP#freq. & 0xff00) | (freq_lo & 0xff);
224 }
225
226 void WaveformGeneratorFPcxx . . $ WaveformGeneratorFP#::writeFREQ_HIcxx . . $ WaveformGeneratorFP#writeFREQ_HI(cb2f890c2fdad230).(reg8cxx . . $ reg8# freq_hi)
227 {
228 freqcxx . . $ WaveformGeneratorFP#freq. = ((freq_hi << 8) & 0xff00) | (freqcxx . . $ WaveformGeneratorFP#freq. & 0xff);
229 }
230
231 void WaveformGeneratorFPcxx . . $ WaveformGeneratorFP#::writePW_LOcxx . . $ WaveformGeneratorFP#writePW_LO(cb2f890c2fdad230).(reg8cxx . . $ reg8# pw_lo)
232 {
233 pwcxx . . $ WaveformGeneratorFP#pw. = (pwcxx . . $ WaveformGeneratorFP#pw. & 0xf00) | (pw_lo & 0x0ff);
234 }
235
236 void WaveformGeneratorFPcxx . . $ WaveformGeneratorFP#::writePW_HIcxx . . $ WaveformGeneratorFP#writePW_HI(cb2f890c2fdad230).(reg8cxx . . $ reg8# pw_hi)
237 {
238 pwcxx . . $ WaveformGeneratorFP#pw. = ((pw_hi << 8) & 0xf00) | (pwcxx . . $ WaveformGeneratorFP#pw. & 0x0ff);
239 }
240
241 void WaveformGeneratorFPcxx . . $ WaveformGeneratorFP#::writeCONTROL_REGcxx . . $ WaveformGeneratorFP#writeCONTROL_REG(9dc1848052cb5733).(WaveformGeneratorFPcxx . . $ WaveformGeneratorFP#& source, reg8cxx . . $ reg8# control)
242 {
243 /* when selecting the 0 waveform, the previous output is held for
244 * a time in the DAC MOSFET gates. We keep on holding forever, though... */
245 reg4cxx . . $ reg4# waveform_next = (control >> 4) & 0x0f;
246 if (waveform_next == 0 && waveformcxx . . $ WaveformGeneratorFP#waveform. >= 1 && waveformcxx . . $ WaveformGeneratorFP#waveform. <= 7) {
247 previouscxx . . $ WaveformGeneratorFP#previous. = readOSCcxx . . $ WaveformGeneratorFP#readOSC(ee47d820ae90fdc).(source);
248 previous_daccxx . . $ WaveformGeneratorFP#previous_dac. = outputcxx . . $ WaveformGeneratorFP#output(d83fafc7bbddfa49).(source);
249 }
250
251 waveformcxx . . $ WaveformGeneratorFP#waveform. = waveform_next;
252 ring_modcxx . . $ WaveformGeneratorFP#ring_mod. = (control & 0x04) != 0;
253 synccxx . . $ WaveformGeneratorFP#sync. = (control & 0x02) != 0;
254 bool test_next = (control & 0x08) != 0;
255
256 // Test bit rising? Invert bit 19 and write it to bit 1.
257 if (test_next && !testcxx . . $ WaveformGeneratorFP#test.) {
258 accumulatorcxx . . $ WaveformGeneratorFP#accumulator. = 0;
259 reg24cxx . . $ reg24# bit19 = (shift_registercxx . . $ WaveformGeneratorFP#shift_register. >> 18) & 2;
260 shift_registercxx . . $ WaveformGeneratorFP#shift_register. = (shift_registercxx . . $ WaveformGeneratorFP#shift_register. & 0x7ffffd) | (bit19^2);
261 noise_overwrite_delaycxx . . $ WaveformGeneratorFP#noise_overwrite_delay. = 200000; /* 200 ms, probably too generous? */
262 } else {
263 if (! test_next) {
264 // Test bit falling? clock noise once,
265 // otherwise just emulate noise's combined waveforms.
266 clock_noisecxx . . $ WaveformGeneratorFP#clock_noise(b45ddcbc8423892c).(testcxx . . $ WaveformGeneratorFP#test.);
267 }
268 }
269
270 testcxx . . $ WaveformGeneratorFP#test. = test_next;
271 }
272
273 reg8cxx . . $ reg8# WaveformGeneratorFPcxx . . $ WaveformGeneratorFP#::readOSCcxx . . $ WaveformGeneratorFP#readOSC(ee47d820ae90fdc).(WaveformGeneratorFPcxx . . $ WaveformGeneratorFP#& source)
274 {
275 float o[12];
276
277 if (waveformcxx . . $ WaveformGeneratorFP#waveform. == 0 || waveformcxx . . $ WaveformGeneratorFP#waveform. > 7) {
278 return previouscxx . . $ WaveformGeneratorFP#previous.;
279 }
280
281 /* Include effects of the test bit & ring mod */
282 reg12cxx . . $ reg12# oldpw = pwcxx . . $ WaveformGeneratorFP#pw.;
283 if (testcxx . . $ WaveformGeneratorFP#test.) {
284 pwcxx . . $ WaveformGeneratorFP#pw. = 0;
285 }
286 reg24cxx . . $ reg24# oldaccumulator = accumulatorcxx . . $ WaveformGeneratorFP#accumulator.;
287 accumulatorcxx . . $ WaveformGeneratorFP#accumulator. ^= (waveformcxx . . $ WaveformGeneratorFP#waveform. & 3) == 1 && ring_modcxx . . $ WaveformGeneratorFP#ring_mod. && (source.accumulatorcxx . . $ WaveformGeneratorFP#accumulator. & 0x800000) ? 0x800000 : 0;
288 calculate_waveform_samplecxx . . $ WaveformGeneratorFP#calculate_waveform_sample(e6881f009d13a851).(o);
289 pwcxx . . $ WaveformGeneratorFP#pw. = oldpw;
290 accumulatorcxx . . $ WaveformGeneratorFP#accumulator. = oldaccumulator;
291
292 reg8cxx . . $ reg8# out = 0;
293 reg8cxx . . $ reg8# bit = 1;
294 for (int i = 4; i < 12; i ++) {
295 if (o[i] > 0.5f) {
296 out |= bit;
297 }
298 bit <<= 1;
299 }
300 return out;
301 }
302
303 void WaveformGeneratorFPcxx . . $ WaveformGeneratorFP#::clock_noisecxx . . $ WaveformGeneratorFP#clock_noise(b45ddcbc8423892c).(const bool clock)
304 {
305 if (clock) {
306 reg24cxx . . $ reg24# bit0 = ((shift_registercxx . . $ WaveformGeneratorFP#shift_register. >> 22) ^ (shift_registercxx . . $ WaveformGeneratorFP#shift_register. >> 17)) & 0x1;
307 shift_registercxx . . $ WaveformGeneratorFP#shift_register. <<= 1;
308 shift_registercxx . . $ WaveformGeneratorFP#shift_register. |= bit0;
309 }
310
311 // clear output bits of shift register if noise and other waveforms
312 // are selected simultaneously
313 if (waveformcxx . . $ WaveformGeneratorFP#waveform. > 8) {
314 shift_registercxx . . $ WaveformGeneratorFP#shift_register. &= 0x7fffff^(1<<22)^(1<<20)^(1<<16)^(1<<13)^(1<<11)^(1<<7)^(1<<4)^(1<<2);
315 }
316
317 if (waveformcxx . . $ WaveformGeneratorFP#waveform. >= 8) {
318 previouscxx . . $ WaveformGeneratorFP#previous. = outputN___cxx . . $ WaveformGeneratorFP#outputN___(db52760e79bf9054).() >> 4;
319 previous_daccxx . . $ WaveformGeneratorFP#previous_dac. = wave_zerocxx . . $ WaveformGeneratorFP#wave_zero.;
320 for (int i = 0; i < 8; i ++) {
321 if (previouscxx . . $ WaveformGeneratorFP#previous. & (1 << i)) {
322 previous_daccxx . . $ WaveformGeneratorFP#previous_dac. += daccxx . . $ dac.[i+4];
323 }
324 }
325 }
326 }
327
328 reg12cxx . . $ reg12# WaveformGeneratorFPcxx . . $ WaveformGeneratorFP#::outputN___cxx . . $ WaveformGeneratorFP#outputN___(db52760e79bf9054).()
329 {
330 return
331 ((shift_registercxx . . $ WaveformGeneratorFP#shift_register. & 0x400000) >> 11) |
332 ((shift_registercxx . . $ WaveformGeneratorFP#shift_register. & 0x100000) >> 10) |
333 ((shift_registercxx . . $ WaveformGeneratorFP#shift_register. & 0x010000) >> 7) |
334 ((shift_registercxx . . $ WaveformGeneratorFP#shift_register. & 0x002000) >> 5) |
335 ((shift_registercxx . . $ WaveformGeneratorFP#shift_register. & 0x000800) >> 4) |
336 ((shift_registercxx . . $ WaveformGeneratorFP#shift_register. & 0x000080) >> 1) |
337 ((shift_registercxx . . $ WaveformGeneratorFP#shift_register. & 0x000010) << 1) |
338 ((shift_registercxx . . $ WaveformGeneratorFP#shift_register. & 0x000004) << 2);
339 }
340
341 // ----------------------------------------------------------------------------
342 // SID reset.
343 // ----------------------------------------------------------------------------
344 void WaveformGeneratorFPcxx . . $ WaveformGeneratorFP#::resetcxx . . $ WaveformGeneratorFP#reset(49f6e7a06ebc5aa8).()
345 {
346 accumulatorcxx . . $ WaveformGeneratorFP#accumulator. = 0;
347 previouscxx . . $ WaveformGeneratorFP#previous. = 0;
348 previous_daccxx . . $ WaveformGeneratorFP#previous_dac. = 0;
349 shift_registercxx . . $ WaveformGeneratorFP#shift_register. = 0x7ffffc;
350 freqcxx . . $ WaveformGeneratorFP#freq. = 0;
351 pwcxx . . $ WaveformGeneratorFP#pw. = 0;
352 testcxx . . $ WaveformGeneratorFP#test. = 0;
353 waveformcxx . . $ WaveformGeneratorFP#waveform. = 0;
354 writeCONTROL_REGcxx . . $ WaveformGeneratorFP#writeCONTROL_REG(9dc1848052cb5733).(*this, 0);
355 msb_risingcxx . . $ WaveformGeneratorFP#msb_rising. = false;
356 }