Debian Codegraph @

src/engine/platform/sound/c64_fp/SID.cpp

raw
1 /*
2 * This file is part of libsidplayfp, a SID player engine.
3 *
4 * Copyright 2011-2016 Leandro Nini <[email protected]>
5 * Copyright 2007-2010 Antti Lankila
6 * Copyright 2004 Dag Lem <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22
23 #define SID_CPP
24
25 #include "SID.h"
26
27 #include <limits>
28
29 #include "array.h"
30 #include "Dac.h"
31 #include "Filter6581.h"
32 #include "Filter8580.h"
33 #include "Potentiometer.h"
34 #include "WaveformCalculator.h"
35 #include "resample/TwoPassSincResampler.h"
36 #include "resample/ZeroOrderResampler.h"
37
38 namespace reSIDfp
39 {
40
41 const unsigned int ENV_DAC_BITS = 8;
42 const unsigned int OSC_DAC_BITS = 12;
43
44 /**
45 * The waveform D/A converter introduces a DC offset in the signal
46 * to the envelope multiplying D/A converter. The "zero" level of
47 * the waveform D/A converter can be found as follows:
48 *
49 * Measure the "zero" voltage of voice 3 on the SID audio output
50 * pin, routing only voice 3 to the mixer ($d417 = $0b, $d418 =
51 * $0f, all other registers zeroed).
52 *
53 * Then set the sustain level for voice 3 to maximum and search for
54 * the waveform output value yielding the same voltage as found
55 * above. This is done by trying out different waveform output
56 * values until the correct value is found, e.g. with the following
57 * program:
58 *
59 * lda #$08
60 * sta $d412
61 * lda #$0b
62 * sta $d417
63 * lda #$0f
64 * sta $d418
65 * lda #$f0
66 * sta $d414
67 * lda #$21
68 * sta $d412
69 * lda #$01
70 * sta $d40e
71 *
72 * ldx #$00
73 * lda #$38 ; Tweak this to find the "zero" level
74 *l cmp $d41b
75 * bne l
76 * stx $d40e ; Stop frequency counter - freeze waveform output
77 * brk
78 *
79 * The waveform output range is 0x000 to 0xfff, so the "zero"
80 * level should ideally have been 0x800. In the measured chip, the
81 * waveform output "zero" level was found to be 0x380 (i.e. $d41b
82 * = 0x38) at an audio output voltage of 5.94V.
83 *
84 * With knowledge of the mixer op-amp characteristics, further estimates
85 * of waveform voltages can be obtained by sampling the EXT IN pin.
86 * From EXT IN samples, the corresponding waveform output can be found by
87 * using the model for the mixer.
88 *
89 * Such measurements have been done on a chip marked MOS 6581R4AR
90 * 0687 14, and the following results have been obtained:
91 * * The full range of one voice is approximately 1.5V.
92 * * The "zero" level rides at approximately 5.0V.
93 *
94 *
95 * zero-x did the measuring on the 8580 (https://sourceforge.net/p/vice-emu/bugs/1036/#c5b3):
96 * When it sits on basic from powerup it's at 4.72
97 * Run 1.prg and check the output pin level.
98 * Then run 2.prg andadjust it until the output level is the same...
99 * 0x94-0xA8 gives me the same 4.72 1.prg shows.
100 * On another 8580 it's 0x90-0x9C
101 * Third chip 0x94-0xA8
102 * Fourth chip 0x90-0xA4
103 * On the 8580 that plays digis the output is 4.66 and 0x93 is the only value to reach that.
104 * To me that seems as regular 8580s have somewhat wide 0-level range,
105 * whereas that digi-compatible 8580 has it very narrow.
106 * On my 6581R4AR has 0x3A as the only value giving the same output level as 1.prg
107 */
108 //@{
109 unsigned int constexpr OFFSET_6581 = 0x380;
110 unsigned int constexpr OFFSET_8580 = 0x9c0;
111 //@}
112
113 /**
114 * Bus value stays alive for some time after each operation.
115 * Values differs between chip models, the timings used here
116 * are taken from VICE [1].
117 * See also the discussion "How do I reliably detect 6581/8580 sid?" on CSDb [2].
118 *
119 * Results from real C64 (testprogs/SID/bitfade/delayfrq0.prg):
120 *
121 * (new SID) (250469/8580R5) (250469/8580R5)
122 * delayfrq0 ~7a000 ~108000
123 *
124 * (old SID) (250407/6581)
125 * delayfrq0 ~01d00
126 *
127 * [1]: http://sourceforge.net/p/vice-emu/patches/99/
128 * [2]: http://noname.c64.org/csdb/forums/?roomid=11&topicid=29025&showallposts=1
129 */
130 //@{
131 int constexpr BUS_TTL_6581 = 0x01d00;
132 int constexpr BUS_TTL_8580 = 0xa2000;
133 //@}
134
135 SID::SID() :
136 filter6581(new Filter6581()),
137 filter8580(new Filter8580()),
138 externalFilter(new ExternalFilter()),
139 resampler(nullptr),
140 potX(new Potentiometer()),
141 potY(new Potentiometer())
142 {
143 voice[0].reset(new Voice());
144 voice[1].reset(new Voice());
145 voice[2].reset(new Voice());
146
147 muted[0] = muted[1] = muted[2] = false;
148
149 reset();
150 setChipModel(MOS8580);
151 }
152
153 SID::~SID()
154 {
155 // Needed to delete auto_ptr with complete type
156 }
157
158 void SID::setFilter6581Curve(double filterCurve)
159 {
160 filter6581->setFilterCurve(filterCurve);
161 }
162
163 void SID::setFilter8580Curve(double filterCurve)
164 {
165 filter8580->setFilterCurve(filterCurve);
166 }
167
168 void SID::enableFilter(bool enable)
169 {
170 filter6581->enable(enable);
171 filter8580->enable(enable);
172 }
173
174 void SID::voiceSync(bool sync)
175 {
176 if (sync)
177 {
178 // Synchronize the 3 waveform generators.
179 for (int i = 0; i < 3; i++)
180 {
181 voice[i]->wave()->synchronize(voice[(i + 1) % 3]->wave(), voice[(i + 2) % 3]->wave());
182 }
183 }
184
185 // Calculate the time to next voice sync
186 nextVoiceSync = std::numeric_limits<int>::max();
187
188 for (int i = 0; i < 3; i++)
189 {
190 WaveformGenerator* const wave = voice[i]->wave();
191 const unsigned int freq = wave->readFreq();
192
193 if (wave->readTest() || freq == 0 || !voice[(i + 1) % 3]->wave()->readSync())
194 {
195 continue;
196 }
197
198 const unsigned int accumulator = wave->readAccumulator();
199 const unsigned int thisVoiceSync = ((0x7fffff - accumulator) & 0xffffff) / freq + 1;
200
201 if (thisVoiceSync < nextVoiceSync)
202 {
203 nextVoiceSync = thisVoiceSync;
204 }
205 }
206 }
207
208 void SID::setChipModel(ChipModel model)
209 {
210 switch (model)
211 {
212 case MOS6581:
213 filter = filter6581.get();
214 modelTTL = BUS_TTL_6581;
215 break;
216
217 case MOS8580:
218 filter = filter8580.get();
219 modelTTL = BUS_TTL_8580;
220 break;
221
222 default:
223 throw SIDError("Unknown chip type");
224 }
225
226 this->model = model;
227
228 // calculate waveform-related tables
229 matrix_t* tables = WaveformCalculator::getInstance()->buildTable(model);
230
231 // calculate envelope DAC table
232 {
233 Dac dacBuilder(ENV_DAC_BITS);
234 dacBuilder.kinkedDac(model);
235
236 for (unsigned int i = 0; i < (1 << ENV_DAC_BITS); i++)
237 {
238 envDAC[i] = static_cast<float>(dacBuilder.getOutput(i));
239 }
240 }
241
242 // calculate oscillator DAC table
243 const bool is6581 = model == MOS6581;
244
245 {
246 Dac dacBuilder(OSC_DAC_BITS);
247 dacBuilder.kinkedDac(model);
248
249 const double offset = dacBuilder.getOutput(is6581 ? OFFSET_6581 : OFFSET_8580);
250
251 for (unsigned int i = 0; i < (1 << OSC_DAC_BITS); i++)
252 {
253 const double dacValue = dacBuilder.getOutput(i);
254 oscDAC[i] = static_cast<float>(dacValue - offset);
255 }
256 }
257
258 // set voice tables
259 for (int i = 0; i < 3; i++)
260 {
261 voice[i]->setEnvDAC(envDAC);
262 voice[i]->setWavDAC(oscDAC);
263 voice[i]->wave()->setModel(is6581);
264 voice[i]->wave()->setWaveformModels(tables);
265 }
266 }
267
268 void SID::reset()
269 {
270 for (int i = 0; i < 3; i++)
271 {
272 voice[i]->reset();
273 }
274
275 filter6581->reset();
276 filter8580->reset();
277 externalFilter->reset();
278
279 if (resampler.get())
280 {
281 resampler->reset();
282 }
283
284 busValue = 0;
285 busValueTtl = 0;
286 voiceSync(false);
287 }
288
289 void SID::input(int value)
290 {
291 filter6581->input(value);
292 filter8580->input(value);
293 }
294
295 unsigned char SID::read(int offset)
296 {
297 switch (offset)
298 {
299 case 0x19: // X value of paddle
300 busValue = potX->readPOT();
301 busValueTtl = modelTTL;
302 break;
303
304 case 0x1a: // Y value of paddle
305 busValue = potY->readPOT();
306 busValueTtl = modelTTL;
307 break;
308
309 case 0x1b: // Voice #3 waveform output
310 busValue = voice[2]->wave()->readOSC();
311 busValueTtl = modelTTL;
312 break;
313
314 case 0x1c: // Voice #3 ADSR output
315 busValue = voice[2]->envelope()->readENV();
316 busValueTtl = modelTTL;
317 break;
318
319 default:
320 // Reading from a write-only or non-existing register
321 // makes the bus discharge faster.
322 // Emulate this by halving the residual TTL.
323 busValueTtl /= 2;
324 break;
325 }
326
327 return busValue;
328 }
329
330 void SID::write(int offset, unsigned char value)
331 {
332 busValue = value;
333 busValueTtl = modelTTL;
334
335 switch (offset)
336 {
337 case 0x00: // Voice #1 frequency (Low-byte)
338 voice[0]->wave()->writeFREQ_LO(value);
339 break;
340
341 case 0x01: // Voice #1 frequency (High-byte)
342 voice[0]->wave()->writeFREQ_HI(value);
343 break;
344
345 case 0x02: // Voice #1 pulse width (Low-byte)
346 voice[0]->wave()->writePW_LO(value);
347 break;
348
349 case 0x03: // Voice #1 pulse width (bits #8-#15)
350 voice[0]->wave()->writePW_HI(value);
351 break;
352
353 case 0x04: // Voice #1 control register
354 voice[0]->writeCONTROL_REG(value);
355 break;
356
357 case 0x05: // Voice #1 Attack and Decay length
358 voice[0]->envelope()->writeATTACK_DECAY(value);
359 break;
360
361 case 0x06: // Voice #1 Sustain volume and Release length
362 voice[0]->envelope()->writeSUSTAIN_RELEASE(value);
363 break;
364
365 case 0x07: // Voice #2 frequency (Low-byte)
366 voice[1]->wave()->writeFREQ_LO(value);
367 break;
368
369 case 0x08: // Voice #2 frequency (High-byte)
370 voice[1]->wave()->writeFREQ_HI(value);
371 break;
372
373 case 0x09: // Voice #2 pulse width (Low-byte)
374 voice[1]->wave()->writePW_LO(value);
375 break;
376
377 case 0x0a: // Voice #2 pulse width (bits #8-#15)
378 voice[1]->wave()->writePW_HI(value);
379 break;
380
381 case 0x0b: // Voice #2 control register
382 voice[1]->writeCONTROL_REG(value);
383 break;
384
385 case 0x0c: // Voice #2 Attack and Decay length
386 voice[1]->envelope()->writeATTACK_DECAY(value);
387 break;
388
389 case 0x0d: // Voice #2 Sustain volume and Release length
390 voice[1]->envelope()->writeSUSTAIN_RELEASE(value);
391 break;
392
393 case 0x0e: // Voice #3 frequency (Low-byte)
394 voice[2]->wave()->writeFREQ_LO(value);
395 break;
396
397 case 0x0f: // Voice #3 frequency (High-byte)
398 voice[2]->wave()->writeFREQ_HI(value);
399 break;
400
401 case 0x10: // Voice #3 pulse width (Low-byte)
402 voice[2]->wave()->writePW_LO(value);
403 break;
404
405 case 0x11: // Voice #3 pulse width (bits #8-#15)
406 voice[2]->wave()->writePW_HI(value);
407 break;
408
409 case 0x12: // Voice #3 control register
410 voice[2]->writeCONTROL_REG(value);
411 break;
412
413 case 0x13: // Voice #3 Attack and Decay length
414 voice[2]->envelope()->writeATTACK_DECAY(value);
415 break;
416
417 case 0x14: // Voice #3 Sustain volume and Release length
418 voice[2]->envelope()->writeSUSTAIN_RELEASE(value);
419 break;
420
421 case 0x15: // Filter cut off frequency (bits #0-#2)
422 filter6581->writeFC_LO(value);
423 filter8580->writeFC_LO(value);
424 break;
425
426 case 0x16: // Filter cut off frequency (bits #3-#10)
427 filter6581->writeFC_HI(value);
428 filter8580->writeFC_HI(value);
429 break;
430
431 case 0x17: // Filter control
432 filter6581->writeRES_FILT(value);
433 filter8580->writeRES_FILT(value);
434 break;
435
436 case 0x18: // Volume and filter modes
437 filter6581->writeMODE_VOL(value);
438 filter8580->writeMODE_VOL(value);
439 break;
440
441 default:
442 break;
443 }
444
445 // Update voicesync just in case.
446 voiceSync(false);
447 }
448
449 void SID::setSamplingParameters(double clockFrequency, SamplingMethod method, double samplingFrequency, double highestAccurateFrequency)
450 {
451 externalFilter->setClockFrequency(clockFrequency);
452
453 switch (method)
454 {
455 case DECIMATE:
456 resampler.reset(new ZeroOrderResampler(clockFrequency, samplingFrequency));
457 break;
458
459 case RESAMPLE:
460 resampler.reset(TwoPassSincResampler::create(clockFrequency, samplingFrequency, highestAccurateFrequency));
461 break;
462
463 default:
464 throw SIDError("Unknown sampling method");
465 }
466 }
467
468 void SID::clockSilent(unsigned int cycles)
469 {
470 ageBusValue(cycles);
471
472 while (cycles != 0)
473 {
474 int delta_t = std::min(nextVoiceSync, cycles);
475
476 if (delta_t > 0)
477 {
478 for (int i = 0; i < delta_t; i++)
479 {
480 // clock waveform generators (can affect OSC3)
481 voice[0]->wave()->clock();
482 voice[1]->wave()->clock();
483 voice[2]->wave()->clock();
484
485 voice[0]->wave()->output(voice[2]->wave());
486 voice[1]->wave()->output(voice[0]->wave());
487 voice[2]->wave()->output(voice[1]->wave());
488
489 // clock ENV3 only
490 voice[2]->envelope()->clock();
491 }
492
493 cycles -= delta_t;
494 nextVoiceSync -= delta_t;
495 }
496
497 if (nextVoiceSync == 0)
498 {
499 voiceSync(true);
500 }
501 }
502 }
503
504 } // namespace reSIDfp