1 //  ---------------------------------------------------------------------------
2 //  This file is part of reSID, a MOS6581 SID emulator engine.
3 //  Copyright (C) 2010  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 #define RESID_SID_CC
21 
22 #ifdef _M_ARM
23 #undef _ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE
24 #define _ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE 1
25 #endif
26 
27 #include "sid.h"
28 #include <math.h>
29 #ifdef __CELLOS_LV2__
30 #include "PS3_include.h"
31 #endif
32 
33 #ifndef round
34 #define round(x) (x>=0.0?floor(x+0.5):ceil(x-0.5))
35 #endif
36 
37 namespace reSID
38 {
39 
40 // ----------------------------------------------------------------------------
41 // Constructor.
42 // ----------------------------------------------------------------------------
SID()43 SID::SID()
44 {
45   // Initialize pointers.
46   sample = 0;
47   fir = 0;
48   fir_N = 0;
49   fir_RES = 0;
50   fir_beta = 0;
51   fir_f_cycles_per_sample = 0;
52   fir_filter_scale = 0;
53 
54   sid_model = MOS6581;
55   voice[0].set_sync_source(&voice[2]);
56   voice[1].set_sync_source(&voice[0]);
57   voice[2].set_sync_source(&voice[1]);
58 
59   set_sampling_parameters(985248, SAMPLE_FAST, 44100);
60 
61   bus_value = 0;
62   bus_value_ttl = 0;
63   write_pipeline = 0;
64 
65   databus_ttl = 0;
66 }
67 
68 
69 // ----------------------------------------------------------------------------
70 // Destructor.
71 // ----------------------------------------------------------------------------
~SID()72 SID::~SID()
73 {
74   delete[] sample;
75   delete[] fir;
76 }
77 
78 
79 // ----------------------------------------------------------------------------
80 // Set chip model.
81 // ----------------------------------------------------------------------------
set_chip_model(chip_model model)82 void SID::set_chip_model(chip_model model)
83 {
84   sid_model = model;
85 
86   /*
87     results from real C64 (testprogs/SID/bitfade/delayfrq0.prg):
88 
89     (new SID) (250469/8580R5) (250469/8580R5)
90     delayfrq0    ~7a000        ~108000
91 
92     (old SID) (250407/6581)
93     delayfrq0    ~01d00
94 
95    */
96   databus_ttl = sid_model == MOS8580 ? 0xa2000 : 0x1d00;
97 
98   for (int i = 0; i < 3; i++) {
99     voice[i].set_chip_model(model);
100   }
101 
102   filter.set_chip_model(model);
103 }
104 
105 
106 // ----------------------------------------------------------------------------
107 // SID reset.
108 // ----------------------------------------------------------------------------
reset()109 void SID::reset()
110 {
111   for (int i = 0; i < 3; i++) {
112     voice[i].reset();
113   }
114   filter.reset();
115   extfilt.reset();
116 
117   bus_value = 0;
118   bus_value_ttl = 0;
119 }
120 
121 
122 // ----------------------------------------------------------------------------
123 // Write 16-bit sample to audio input.
124 // Note that to mix in an external audio signal, the signal should be
125 // resampled to 1MHz first to avoid sampling noise.
126 // ----------------------------------------------------------------------------
input(short sample)127 void SID::input(short sample)
128 {
129   // The input can be used to simulate the MOS8580 "digi boost" hardware hack.
130   filter.input(sample);
131 }
132 
133 
134 // ----------------------------------------------------------------------------
135 // Read registers.
136 //
137 // Reading a write only register returns the last byte written to any SID
138 // register. The individual bits in this value start to fade down towards
139 // zero after a few cycles. All bits reach zero within approximately
140 // $2000 - $4000 cycles.
141 // It has been claimed that this fading happens in an orderly fashion, however
142 // sampling of write only registers reveals that this is not the case.
143 // NB! This is not correctly modeled.
144 // The actual use of write only registers has largely been made in the belief
145 // that all SID registers are readable. To support this belief the read
146 // would have to be done immediately after a write to the same register
147 // (remember that an intermediate write to another register would yield that
148 // value instead). With this in mind we return the last value written to
149 // any SID register for $4000 cycles without modeling the bit fading.
150 // ----------------------------------------------------------------------------
read(reg8 offset)151 reg8 SID::read(reg8 offset)
152 {
153   switch (offset) {
154   case 0x19:
155     bus_value = potx.readPOT();
156     bus_value_ttl = databus_ttl;
157     break;
158   case 0x1a:
159     bus_value = poty.readPOT();
160     bus_value_ttl = databus_ttl;
161     break;
162   case 0x1b:
163     bus_value = voice[2].wave.readOSC();
164     bus_value_ttl = databus_ttl;
165     break;
166   case 0x1c:
167     bus_value = voice[2].envelope.readENV();
168     bus_value_ttl = databus_ttl;
169     break;
170   }
171   return bus_value;
172 }
173 
174 
175 // ----------------------------------------------------------------------------
176 // Write registers.
177 // Writes are one cycle delayed on the MOS8580. This is only modeled for
178 // single cycle clocking.
179 // ----------------------------------------------------------------------------
write(reg8 offset,reg8 value)180 void SID::write(reg8 offset, reg8 value)
181 {
182   write_address = offset;
183   bus_value = value;
184   bus_value_ttl = databus_ttl;
185 
186   if (unlikely(sampling == SAMPLE_FAST) && (sid_model == MOS8580)) {
187     // Fake one cycle pipeline delay on the MOS8580
188     // when using non cycle accurate emulation.
189     // This will make the SID detection method work.
190     write_pipeline = 1;
191   }
192   else {
193     write();
194   }
195 }
196 
197 
198 // ----------------------------------------------------------------------------
199 // Write registers.
200 // ----------------------------------------------------------------------------
write()201 void SID::write()
202 {
203   switch (write_address) {
204   case 0x00:
205     voice[0].wave.writeFREQ_LO(bus_value);
206     break;
207   case 0x01:
208     voice[0].wave.writeFREQ_HI(bus_value);
209     break;
210   case 0x02:
211     voice[0].wave.writePW_LO(bus_value);
212     break;
213   case 0x03:
214     voice[0].wave.writePW_HI(bus_value);
215     break;
216   case 0x04:
217     voice[0].writeCONTROL_REG(bus_value);
218     break;
219   case 0x05:
220     voice[0].envelope.writeATTACK_DECAY(bus_value);
221     break;
222   case 0x06:
223     voice[0].envelope.writeSUSTAIN_RELEASE(bus_value);
224     break;
225   case 0x07:
226     voice[1].wave.writeFREQ_LO(bus_value);
227     break;
228   case 0x08:
229     voice[1].wave.writeFREQ_HI(bus_value);
230     break;
231   case 0x09:
232     voice[1].wave.writePW_LO(bus_value);
233     break;
234   case 0x0a:
235     voice[1].wave.writePW_HI(bus_value);
236     break;
237   case 0x0b:
238     voice[1].writeCONTROL_REG(bus_value);
239     break;
240   case 0x0c:
241     voice[1].envelope.writeATTACK_DECAY(bus_value);
242     break;
243   case 0x0d:
244     voice[1].envelope.writeSUSTAIN_RELEASE(bus_value);
245     break;
246   case 0x0e:
247     voice[2].wave.writeFREQ_LO(bus_value);
248     break;
249   case 0x0f:
250     voice[2].wave.writeFREQ_HI(bus_value);
251     break;
252   case 0x10:
253     voice[2].wave.writePW_LO(bus_value);
254     break;
255   case 0x11:
256     voice[2].wave.writePW_HI(bus_value);
257     break;
258   case 0x12:
259     voice[2].writeCONTROL_REG(bus_value);
260     break;
261   case 0x13:
262     voice[2].envelope.writeATTACK_DECAY(bus_value);
263     break;
264   case 0x14:
265     voice[2].envelope.writeSUSTAIN_RELEASE(bus_value);
266     break;
267   case 0x15:
268     filter.writeFC_LO(bus_value);
269     break;
270   case 0x16:
271     filter.writeFC_HI(bus_value);
272     break;
273   case 0x17:
274     filter.writeRES_FILT(bus_value);
275     break;
276   case 0x18:
277     filter.writeMODE_VOL(bus_value);
278     break;
279   default:
280     break;
281   }
282 
283   // Tell clock() that the pipeline is empty.
284   write_pipeline = 0;
285 }
286 
287 
288 // ----------------------------------------------------------------------------
289 // Constructor.
290 // ----------------------------------------------------------------------------
State()291 SID::State::State()
292 {
293   int i;
294 
295   for (i = 0; i < 0x20; i++) {
296     sid_register[i] = 0;
297   }
298 
299   bus_value = 0;
300   bus_value_ttl = 0;
301   write_pipeline = 0;
302   write_address = 0;
303   voice_mask = 0xff;
304 
305   for (i = 0; i < 3; i++) {
306     accumulator[i] = 0;
307     shift_register[i] = 0x7fffff;
308     shift_register_reset[i] = 0;
309     shift_pipeline[i] = 0;
310     pulse_output[i] = 0;
311     floating_output_ttl[i] = 0;
312 
313     rate_counter[i] = 0;
314     rate_counter_period[i] = 9;
315     exponential_counter[i] = 0;
316     exponential_counter_period[i] = 1;
317     envelope_counter[i] = 0;
318     envelope_state[i] = EnvelopeGenerator::RELEASE;
319     hold_zero[i] = true;
320     envelope_pipeline[i] = 0;
321   }
322 }
323 
324 
325 // ----------------------------------------------------------------------------
326 // Read state.
327 // ----------------------------------------------------------------------------
read_state()328 SID::State SID::read_state()
329 {
330   State state;
331   int i, j;
332 
333   for (i = 0, j = 0; i < 3; i++, j += 7) {
334     WaveformGenerator& wave = voice[i].wave;
335     EnvelopeGenerator& envelope = voice[i].envelope;
336     state.sid_register[j + 0] = wave.freq & 0xff;
337     state.sid_register[j + 1] = wave.freq >> 8;
338     state.sid_register[j + 2] = wave.pw & 0xff;
339     state.sid_register[j + 3] = wave.pw >> 8;
340     state.sid_register[j + 4] =
341       (wave.waveform << 4)
342       | (wave.test ? 0x08 : 0)
343       | (wave.ring_mod ? 0x04 : 0)
344       | (wave.sync ? 0x02 : 0)
345       | (envelope.gate ? 0x01 : 0);
346     state.sid_register[j + 5] = (envelope.attack << 4) | envelope.decay;
347     state.sid_register[j + 6] = (envelope.sustain << 4) | envelope.release;
348   }
349 
350   state.sid_register[j++] = filter.fc & 0x007;
351   state.sid_register[j++] = filter.fc >> 3;
352   state.sid_register[j++] = (filter.res << 4) | filter.filt;
353   state.sid_register[j++] = filter.mode | filter.vol;
354 
355   // These registers are superfluous, but are included for completeness.
356   for (; j < 0x1d; j++) {
357     state.sid_register[j] = read(j);
358   }
359   for (; j < 0x20; j++) {
360     state.sid_register[j] = 0;
361   }
362 
363   state.bus_value = bus_value;
364   state.bus_value_ttl = bus_value_ttl;
365   state.write_pipeline = write_pipeline;
366   state.write_address = write_address;
367   state.voice_mask = filter.voice_mask;
368 
369   for (i = 0; i < 3; i++) {
370     state.accumulator[i] = voice[i].wave.accumulator;
371     state.shift_register[i] = voice[i].wave.shift_register;
372     state.shift_register_reset[i] = voice[i].wave.shift_register_reset;
373     state.shift_pipeline[i] = voice[i].wave.shift_pipeline;
374     state.pulse_output[i] = voice[i].wave.pulse_output;
375     state.floating_output_ttl[i] = voice[i].wave.floating_output_ttl;
376 
377     state.rate_counter[i] = voice[i].envelope.rate_counter;
378     state.rate_counter_period[i] = voice[i].envelope.rate_period;
379     state.exponential_counter[i] = voice[i].envelope.exponential_counter;
380     state.exponential_counter_period[i] = voice[i].envelope.exponential_counter_period;
381     state.envelope_counter[i] = voice[i].envelope.envelope_counter;
382     state.envelope_state[i] = voice[i].envelope.state;
383     state.hold_zero[i] = voice[i].envelope.hold_zero;
384     state.envelope_pipeline[i] = voice[i].envelope.envelope_pipeline;
385   }
386 
387   return state;
388 }
389 
390 
391 // ----------------------------------------------------------------------------
392 // Write state.
393 // ----------------------------------------------------------------------------
write_state(const State & state)394 void SID::write_state(const State& state)
395 {
396   int i;
397 
398   for (i = 0; i <= 0x18; i++) {
399     write(i, state.sid_register[i]);
400   }
401 
402   bus_value = state.bus_value;
403   bus_value_ttl = state.bus_value_ttl;
404   write_pipeline = state.write_pipeline;
405   write_address = state.write_address;
406   filter.set_voice_mask(state.voice_mask);
407 
408   for (i = 0; i < 3; i++) {
409     voice[i].wave.accumulator = state.accumulator[i];
410     voice[i].wave.shift_register = state.shift_register[i];
411     voice[i].wave.shift_register_reset = state.shift_register_reset[i];
412     voice[i].wave.shift_pipeline = state.shift_pipeline[i];
413     voice[i].wave.pulse_output = state.pulse_output[i];
414     voice[i].wave.floating_output_ttl = state.floating_output_ttl[i];
415 
416     voice[i].envelope.rate_counter = state.rate_counter[i];
417     voice[i].envelope.rate_period = state.rate_counter_period[i];
418     voice[i].envelope.exponential_counter = state.exponential_counter[i];
419     voice[i].envelope.exponential_counter_period = state.exponential_counter_period[i];
420     voice[i].envelope.envelope_counter = state.envelope_counter[i];
421     voice[i].envelope.state = state.envelope_state[i];
422     voice[i].envelope.hold_zero = state.hold_zero[i];
423     voice[i].envelope.envelope_pipeline = state.envelope_pipeline[i];
424   }
425 }
426 
427 
428 // ----------------------------------------------------------------------------
429 // Mask for voices routed into the filter / audio output stage.
430 // Used to physically connect/disconnect EXT IN, and for test purposed
431 // (voice muting).
432 // ----------------------------------------------------------------------------
set_voice_mask(reg4 mask)433 void SID::set_voice_mask(reg4 mask)
434 {
435   filter.set_voice_mask(mask);
436 }
437 
438 
439 // ----------------------------------------------------------------------------
440 // Enable filter.
441 // ----------------------------------------------------------------------------
enable_filter(bool enable)442 void SID::enable_filter(bool enable)
443 {
444   filter.enable_filter(enable);
445 }
446 
447 
448 // ----------------------------------------------------------------------------
449 // Adjust the DAC bias parameter of the filter.
450 // This gives user variable control of the exact CF -> center frequency
451 // mapping used by the filter.
452 // The setting is currently only effective for 6581.
453 // ----------------------------------------------------------------------------
adjust_filter_bias(double dac_bias)454 void SID::adjust_filter_bias(double dac_bias) {
455   filter.adjust_filter_bias(dac_bias);
456 }
457 
458 
459 // ----------------------------------------------------------------------------
460 // Enable external filter.
461 // ----------------------------------------------------------------------------
enable_external_filter(bool enable)462 void SID::enable_external_filter(bool enable)
463 {
464   extfilt.enable_filter(enable);
465 }
466 
467 
468 // ----------------------------------------------------------------------------
469 // I0() computes the 0th order modified Bessel function of the first kind.
470 // This function is originally from resample-1.5/filterkit.c by J. O. Smith.
471 // ----------------------------------------------------------------------------
I0(double x)472 double SID::I0(double x)
473 {
474   // Max error acceptable in I0.
475   const double I0e = 1e-6;
476 
477   double sum, u, halfx, temp;
478   int n;
479 
480   sum = u = n = 1;
481   halfx = x/2.0;
482 
483   do {
484     temp = halfx/n++;
485     u *= temp*temp;
486     sum += u;
487   } while (u >= I0e*sum);
488 
489   return sum;
490 }
491 
492 
493 // ----------------------------------------------------------------------------
494 // Setting of SID sampling parameters.
495 //
496 // Use a clock freqency of 985248Hz for PAL C64, 1022730Hz for NTSC C64.
497 // The default end of passband frequency is pass_freq = 0.9*sample_freq/2
498 // for sample frequencies up to ~ 44.1kHz, and 20kHz for higher sample
499 // frequencies.
500 //
501 // For resampling, the ratio between the clock frequency and the sample
502 // frequency is limited as follows:
503 //   125*clock_freq/sample_freq < 16384
504 // E.g. provided a clock frequency of ~ 1MHz, the sample frequency can not
505 // be set lower than ~ 8kHz. A lower sample frequency would make the
506 // resampling code overfill its 16k sample ring buffer.
507 //
508 // The end of passband frequency is also limited:
509 //   pass_freq <= 0.9*sample_freq/2
510 
511 // E.g. for a 44.1kHz sampling rate the end of passband frequency is limited
512 // to slightly below 20kHz. This constraint ensures that the FIR table is
513 // not overfilled.
514 // ----------------------------------------------------------------------------
set_sampling_parameters(double clock_freq,sampling_method method,double sample_freq,double pass_freq,double filter_scale)515 bool SID::set_sampling_parameters(double clock_freq, sampling_method method,
516                         double sample_freq, double pass_freq, double filter_scale)
517 {
518   // Check resampling constraints.
519   if (method == SAMPLE_RESAMPLE || method == SAMPLE_RESAMPLE_FASTMEM)
520   {
521     // Check whether the sample ring buffer would overfill.
522     if (FIR_N*clock_freq/sample_freq >= RINGSIZE) {
523       return false;
524     }
525 
526     // The default passband limit is 0.9*sample_freq/2 for sample
527     // frequencies below ~ 44.1kHz, and 20kHz for higher sample frequencies.
528     if (pass_freq < 0) {
529       pass_freq = 20000;
530       if (2*pass_freq/sample_freq >= 0.9) {
531         pass_freq = 0.9*sample_freq/2;
532       }
533     }
534     // Check whether the FIR table would overfill.
535     else if (pass_freq > 0.9*sample_freq/2) {
536       return false;
537     }
538 
539     // The filter scaling is only included to avoid clipping, so keep
540     // it sane.
541     if (filter_scale < 0.9 || filter_scale > 1.0) {
542       return false;
543     }
544   }
545 
546   clock_frequency = clock_freq;
547   sampling = method;
548 
549   cycles_per_sample =
550     cycle_count(clock_freq/sample_freq*(1 << FIXP_SHIFT) + 0.5);
551 
552   sample_offset = 0;
553   sample_prev = 0;
554   sample_now = 0;
555 
556   // FIR initialization is only necessary for resampling.
557   if (method != SAMPLE_RESAMPLE && method != SAMPLE_RESAMPLE_FASTMEM)
558   {
559     delete[] sample;
560     delete[] fir;
561     sample = 0;
562     fir = 0;
563     return true;
564   }
565 
566   // Allocate sample buffer.
567   if (!sample) {
568     sample = new short[RINGSIZE*2];
569   }
570   // Clear sample buffer.
571   for (int j = 0; j < RINGSIZE*2; j++) {
572     sample[j] = 0;
573   }
574   sample_index = 0;
575 
576   const double pi = 3.1415926535897932385;
577 
578   // 16 bits -> -96dB stopband attenuation.
579   const double A = -20*log10(1.0/(1 << 16));
580   // A fraction of the bandwidth is allocated to the transition band,
581   double dw = (1 - 2*pass_freq/sample_freq)*pi*2;
582   // The cutoff frequency is midway through the transition band (nyquist)
583   double wc = pi;
584 
585   // For calculation of beta and N see the reference for the kaiserord
586   // function in the MATLAB Signal Processing Toolbox:
587   // http://www.mathworks.com/access/helpdesk/help/toolbox/signal/kaiserord.html
588   const double beta = 0.1102*(A - 8.7);
589   const double I0beta = I0(beta);
590 
591   // The filter order will maximally be 124 with the current constraints.
592   // N >= (96.33 - 7.95)/(2.285*0.1*pi) -> N >= 123
593   // The filter order is equal to the number of zero crossings, i.e.
594   // it should be an even number (sinc is symmetric about x = 0).
595   int N = int((A - 7.95)/(2.285*dw) + 0.5);
596   N += N & 1;
597 
598   double f_samples_per_cycle = sample_freq/clock_freq;
599   double f_cycles_per_sample = clock_freq/sample_freq;
600 
601   // The filter length is equal to the filter order + 1.
602   // The filter length must be an odd number (sinc is symmetric about x = 0).
603   int fir_N_new = int(N*f_cycles_per_sample) + 1;
604   fir_N_new |= 1;
605 
606   // We clamp the filter table resolution to 2^n, making the fixed point
607   // sample_offset a whole multiple of the filter table resolution.
608   int res = method == SAMPLE_RESAMPLE ?
609     FIR_RES : FIR_RES_FASTMEM;
610   int n = (int)ceil(log(res/f_cycles_per_sample)/log(2.0f));
611   int fir_RES_new = 1 << n;
612 
613   /* Determine if we need to recalculate table, or whether we can reuse earlier cached copy.
614    * This pays off on slow hardware such as current Android devices.
615    */
616   if (fir && fir_RES_new == fir_RES && fir_N_new == fir_N && beta == fir_beta && f_cycles_per_sample == fir_f_cycles_per_sample && fir_filter_scale == filter_scale) {
617       return true;
618   }
619   fir_RES = fir_RES_new;
620   fir_N = fir_N_new;
621   fir_beta = beta;
622   fir_f_cycles_per_sample = f_cycles_per_sample;
623   fir_filter_scale = filter_scale;
624 
625   // Allocate memory for FIR tables.
626   delete[] fir;
627   fir = new short[fir_N*fir_RES];
628 
629   // Calculate fir_RES FIR tables for linear interpolation.
630   for (int i = 0; i < fir_RES; i++) {
631     int fir_offset = i*fir_N + fir_N/2;
632     double j_offset = double(i)/fir_RES;
633     // Calculate FIR table. This is the sinc function, weighted by the
634     // Kaiser window.
635     for (int j = -fir_N/2; j <= fir_N/2; j++) {
636       double jx = j - j_offset;
637       double wt = wc*jx/f_cycles_per_sample;
638       double temp = jx/(fir_N/2);
639       double Kaiser = fabs(temp) <= 1 ? I0(beta*sqrt(1 - temp*temp))/I0beta : 0;
640       double sincwt = fabs(wt) >= 1e-6 ? sin(wt)/wt : 1;
641       double val = (1 << FIR_SHIFT)*filter_scale*f_samples_per_cycle*wc/pi*sincwt*Kaiser;
642       fir[fir_offset + j] = (short)round(val);
643     }
644   }
645 
646   return true;
647 }
648 
649 
650 // ----------------------------------------------------------------------------
651 // Adjustment of SID sampling frequency.
652 //
653 // In some applications, e.g. a C64 emulator, it can be desirable to
654 // synchronize sound with a timer source. This is supported by adjustment of
655 // the SID sampling frequency.
656 //
657 // NB! Adjustment of the sampling frequency may lead to noticeable shifts in
658 // frequency, and should only be used for interactive applications. Note also
659 // that any adjustment of the sampling frequency will change the
660 // characteristics of the resampling filter, since the filter is not rebuilt.
661 // ----------------------------------------------------------------------------
adjust_sampling_frequency(double sample_freq)662 void SID::adjust_sampling_frequency(double sample_freq)
663 {
664   cycles_per_sample =
665     cycle_count(clock_frequency/sample_freq*(1 << FIXP_SHIFT) + 0.5);
666 }
667 
668 
669 // ----------------------------------------------------------------------------
670 // SID clocking - delta_t cycles.
671 // ----------------------------------------------------------------------------
clock(cycle_count delta_t)672 void SID::clock(cycle_count delta_t)
673 {
674   int i;
675 
676   // Pipelined writes on the MOS8580.
677   if (unlikely(write_pipeline) && likely(delta_t > 0)) {
678     // Step one cycle by a recursive call to ourselves.
679     write_pipeline = 0;
680     clock(1);
681     write();
682     delta_t -= 1;
683   }
684 
685   if (unlikely(delta_t <= 0)) {
686     return;
687   }
688 
689   // Age bus value.
690   bus_value_ttl -= delta_t;
691   if (unlikely(bus_value_ttl <= 0)) {
692     bus_value = 0;
693     bus_value_ttl = 0;
694   }
695 
696   // Clock amplitude modulators.
697   for (i = 0; i < 3; i++) {
698     voice[i].envelope.clock(delta_t);
699   }
700 
701   // Clock and synchronize oscillators.
702   // Loop until we reach the current cycle.
703   cycle_count delta_t_osc = delta_t;
704   while (delta_t_osc) {
705     cycle_count delta_t_min = delta_t_osc;
706 
707     // Find minimum number of cycles to an oscillator accumulator MSB toggle.
708     // We have to clock on each MSB on / MSB off for hard sync to operate
709     // correctly.
710     for (i = 0; i < 3; i++) {
711       WaveformGenerator& wave = voice[i].wave;
712 
713       // It is only necessary to clock on the MSB of an oscillator that is
714       // a sync source and has freq != 0.
715       if (likely(!(wave.sync_dest->sync && wave.freq))) {
716         continue;
717       }
718 
719       reg16 freq = wave.freq;
720       reg24 accumulator = wave.accumulator;
721 
722       // Clock on MSB off if MSB is on, clock on MSB on if MSB is off.
723       reg24 delta_accumulator =
724         (accumulator & 0x800000 ? 0x1000000 : 0x800000) - accumulator;
725 
726       cycle_count delta_t_next = delta_accumulator/freq;
727       if (likely(delta_accumulator%freq)) {
728         ++delta_t_next;
729       }
730 
731       if (unlikely(delta_t_next < delta_t_min)) {
732         delta_t_min = delta_t_next;
733       }
734     }
735 
736     // Clock oscillators.
737     for (i = 0; i < 3; i++) {
738       voice[i].wave.clock(delta_t_min);
739     }
740 
741     // Synchronize oscillators.
742     for (i = 0; i < 3; i++) {
743       voice[i].wave.synchronize();
744     }
745 
746     delta_t_osc -= delta_t_min;
747   }
748 
749   // Calculate waveform output.
750   for (i = 0; i < 3; i++) {
751     voice[i].wave.set_waveform_output(delta_t);
752   }
753 
754   // Clock filter.
755   filter.clock(delta_t, voice[0].output(), voice[1].output(), voice[2].output());
756 
757   // Clock external filter.
758   extfilt.clock(delta_t, filter.output());
759 }
760 
761 
762 // ----------------------------------------------------------------------------
763 // SID clocking with audio sampling.
764 // Fixed point arithmetics are used.
765 //
766 // The example below shows how to clock the SID a specified amount of cycles
767 // while producing audio output:
768 //
769 // while (delta_t) {
770 //   bufindex += sid.clock(delta_t, buf + bufindex, buflength - bufindex);
771 //   write(dsp, buf, bufindex*2);
772 //   bufindex = 0;
773 // }
774 //
775 // ----------------------------------------------------------------------------
clock(cycle_count & delta_t,short * buf,int n,int interleave)776 int SID::clock(cycle_count& delta_t, short* buf, int n, int interleave)
777 {
778   switch (sampling) {
779   default:
780   case SAMPLE_FAST:
781     return clock_fast(delta_t, buf, n, interleave);
782   case SAMPLE_INTERPOLATE:
783     return clock_interpolate(delta_t, buf, n, interleave);
784   case SAMPLE_RESAMPLE:
785     return clock_resample(delta_t, buf, n, interleave);
786   case SAMPLE_RESAMPLE_FASTMEM:
787     return clock_resample_fastmem(delta_t, buf, n, interleave);
788   }
789 }
790 
791 
792 // ----------------------------------------------------------------------------
793 // SID clocking with audio sampling - delta clocking picking nearest sample.
794 // ----------------------------------------------------------------------------
clock_fast(cycle_count & delta_t,short * buf,int n,int interleave)795 int SID::clock_fast(cycle_count& delta_t, short* buf, int n, int interleave)
796 {
797   int s;
798 
799   for (s = 0; s < n; s++) {
800     cycle_count next_sample_offset = sample_offset + cycles_per_sample + (1 << (FIXP_SHIFT - 1));
801     cycle_count delta_t_sample = next_sample_offset >> FIXP_SHIFT;
802 
803     if (delta_t_sample > delta_t) {
804       delta_t_sample = delta_t;
805     }
806 
807     clock(delta_t_sample);
808 
809     if ((delta_t -= delta_t_sample) == 0) {
810       sample_offset -= delta_t_sample << FIXP_SHIFT;
811       break;
812     }
813 
814     sample_offset = (next_sample_offset & FIXP_MASK) - (1 << (FIXP_SHIFT - 1));
815     buf[s*interleave] = output();
816   }
817 
818   return s;
819 }
820 
821 
822 // ----------------------------------------------------------------------------
823 // SID clocking with audio sampling - cycle based with linear sample
824 // interpolation.
825 //
826 // Here the chip is clocked every cycle. This yields higher quality
827 // sound since the samples are linearly interpolated, and since the
828 // external filter attenuates frequencies above 16kHz, thus reducing
829 // sampling noise.
830 // ----------------------------------------------------------------------------
clock_interpolate(cycle_count & delta_t,short * buf,int n,int interleave)831 int SID::clock_interpolate(cycle_count& delta_t, short* buf, int n, int interleave)
832 {
833   int s;
834 
835   for (s = 0; s < n; s++) {
836     cycle_count next_sample_offset = sample_offset + cycles_per_sample;
837     cycle_count delta_t_sample = next_sample_offset >> FIXP_SHIFT;
838 
839     if (delta_t_sample > delta_t) {
840       delta_t_sample = delta_t;
841     }
842 
843     for (int i = delta_t_sample; i > 0; i--) {
844       clock();
845       if (unlikely(i <= 2)) {
846         sample_prev = sample_now;
847         sample_now = output();
848       }
849     }
850 
851     if ((delta_t -= delta_t_sample) == 0) {
852       sample_offset -= delta_t_sample << FIXP_SHIFT;
853       break;
854     }
855 
856     sample_offset = next_sample_offset & FIXP_MASK;
857 
858     buf[s*interleave] =
859       sample_prev + (sample_offset*(sample_now - sample_prev) >> FIXP_SHIFT);
860   }
861 
862   return s;
863 }
864 
865 
866 // ----------------------------------------------------------------------------
867 // SID clocking with audio sampling - cycle based with audio resampling.
868 //
869 // This is the theoretically correct (and computationally intensive) audio
870 // sample generation. The samples are generated by resampling to the specified
871 // sampling frequency. The work rate is inversely proportional to the
872 // percentage of the bandwidth allocated to the filter transition band.
873 //
874 // This implementation is based on the paper "A Flexible Sampling-Rate
875 // Conversion Method", by J. O. Smith and P. Gosset, or rather on the
876 // expanded tutorial on the "Digital Audio Resampling Home Page":
877 // http://www-ccrma.stanford.edu/~jos/resample/
878 //
879 // By building shifted FIR tables with samples according to the
880 // sampling frequency, the implementation below dramatically reduces the
881 // computational effort in the filter convolutions, without any loss
882 // of accuracy. The filter convolutions are also vectorizable on
883 // current hardware.
884 //
885 // Further possible optimizations are:
886 // * An equiripple filter design could yield a lower filter order, see
887 //   http://www.mwrf.com/Articles/ArticleID/7229/7229.html
888 // * The Convolution Theorem could be used to bring the complexity of
889 //   convolution down from O(n*n) to O(n*log(n)) using the Fast Fourier
890 //   Transform, see http://en.wikipedia.org/wiki/Convolution_theorem
891 // * Simply resampling in two steps can also yield computational
892 //   savings, since the transition band will be wider in the first step
893 //   and the required filter order is thus lower in this step.
894 //   Laurent Ganier has found the optimal intermediate sampling frequency
895 //   to be (via derivation of sum of two steps):
896 //     2 * pass_freq + sqrt [ 2 * pass_freq * orig_sample_freq
897 //       * (dest_sample_freq - 2 * pass_freq) / dest_sample_freq ]
898 //
899 // NB! the result of right shifting negative numbers is really
900 // implementation dependent in the C++ standard.
901 // ----------------------------------------------------------------------------
clock_resample(cycle_count & delta_t,short * buf,int n,int interleave)902 int SID::clock_resample(cycle_count& delta_t, short* buf, int n, int interleave)
903 {
904   int s;
905 
906   for (s = 0; s < n; s++) {
907     cycle_count next_sample_offset = sample_offset + cycles_per_sample;
908     cycle_count delta_t_sample = next_sample_offset >> FIXP_SHIFT;
909 
910     if (delta_t_sample > delta_t) {
911       delta_t_sample = delta_t;
912     }
913 
914     for (int i = 0; i < delta_t_sample; i++) {
915       clock();
916       sample[sample_index] = sample[sample_index + RINGSIZE] = output();
917       ++sample_index &= RINGMASK;
918     }
919 
920     if ((delta_t -= delta_t_sample) == 0) {
921       sample_offset -= delta_t_sample << FIXP_SHIFT;
922       break;
923     }
924 
925     sample_offset = next_sample_offset & FIXP_MASK;
926 
927     int fir_offset = sample_offset*fir_RES >> FIXP_SHIFT;
928     int fir_offset_rmd = sample_offset*fir_RES & FIXP_MASK;
929     short* fir_start = fir + fir_offset*fir_N;
930     short* sample_start = sample + sample_index - fir_N - 1 + RINGSIZE;
931 
932     // Convolution with filter impulse response.
933     int v1 = 0;
934     for (int j = 0; j < fir_N; j++) {
935       v1 += sample_start[j]*fir_start[j];
936     }
937 
938     // Use next FIR table, wrap around to first FIR table using
939     // next sample.
940     if (unlikely(++fir_offset == fir_RES)) {
941       fir_offset = 0;
942       ++sample_start;
943     }
944     fir_start = fir + fir_offset*fir_N;
945 
946     // Convolution with filter impulse response.
947     int v2 = 0;
948     for (int k = 0; k < fir_N; k++) {
949       v2 += sample_start[k]*fir_start[k];
950     }
951 
952     // Linear interpolation.
953     // fir_offset_rmd is equal for all samples, it can thus be factorized out:
954     // sum(v1 + rmd*(v2 - v1)) = sum(v1) + rmd*(sum(v2) - sum(v1))
955     int v = v1 + int((unsigned(fir_offset_rmd)*unsigned(v2 - v1)) >> FIXP_SHIFT);
956 
957     v >>= FIR_SHIFT;
958 
959     // Saturated arithmetics to guard against 16 bit sample overflow.
960     const int half = 1 << 15;
961     if (v >= half) {
962       v = half - 1;
963     }
964     else if (v < -half) {
965       v = -half;
966     }
967 
968     buf[s*interleave] = v;
969   }
970 
971   return s;
972 }
973 
974 
975 // ----------------------------------------------------------------------------
976 // SID clocking with audio sampling - cycle based with audio resampling.
977 // ----------------------------------------------------------------------------
clock_resample_fastmem(cycle_count & delta_t,short * buf,int n,int interleave)978 int SID::clock_resample_fastmem(cycle_count& delta_t, short* buf, int n, int interleave)
979 {
980   int s;
981 
982   for (s = 0; s < n; s++) {
983     cycle_count next_sample_offset = sample_offset + cycles_per_sample;
984     cycle_count delta_t_sample = next_sample_offset >> FIXP_SHIFT;
985 
986     if (delta_t_sample > delta_t) {
987       delta_t_sample = delta_t;
988     }
989 
990     for (int i = 0; i < delta_t_sample; i++) {
991       clock();
992       sample[sample_index] = sample[sample_index + RINGSIZE] = output();
993       ++sample_index &= RINGMASK;
994     }
995 
996     if ((delta_t -= delta_t_sample) == 0) {
997       sample_offset -= delta_t_sample << FIXP_SHIFT;
998       break;
999     }
1000 
1001     sample_offset = next_sample_offset & FIXP_MASK;
1002 
1003     int fir_offset = sample_offset*fir_RES >> FIXP_SHIFT;
1004     short* fir_start = fir + fir_offset*fir_N;
1005     short* sample_start = sample + sample_index - fir_N + RINGSIZE;
1006 
1007     // Convolution with filter impulse response.
1008     int v = 0;
1009     for (int j = 0; j < fir_N; j++) {
1010       v += sample_start[j]*fir_start[j];
1011     }
1012 
1013     v >>= FIR_SHIFT;
1014 
1015     // Saturated arithmetics to guard against 16 bit sample overflow.
1016     const int half = 1 << 15;
1017     if (v >= half) {
1018       v = half - 1;
1019     }
1020     else if (v < -half) {
1021       v = -half;
1022     }
1023 
1024     buf[s*interleave] = v;
1025   }
1026 
1027   return s;
1028 }
1029 
1030 } // namespace reSID
1031