libsidplayfp  2.0.2
wave.h
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 #ifndef RESID_WAVE_H
21 #define RESID_WAVE_H
22 
23 #include "resid-config.h"
24 
25 namespace reSID
26 {
27 
28 // ----------------------------------------------------------------------------
29 // A 24 bit accumulator is the basis for waveform generation. FREQ is added to
30 // the lower 16 bits of the accumulator each cycle.
31 // The accumulator is set to zero when TEST is set, and starts counting
32 // when TEST is cleared.
33 // The noise waveform is taken from intermediate bits of a 23 bit shift
34 // register. This register is clocked by bit 19 of the accumulator.
35 // ----------------------------------------------------------------------------
37 {
38 public:
40 
41  void set_sync_source(WaveformGenerator*);
42  void set_chip_model(chip_model model);
43 
44  void clock();
45  void clock(cycle_count delta_t);
46  void synchronize();
47  void reset();
48 
49  void writeFREQ_LO(reg8);
50  void writeFREQ_HI(reg8);
51  void writePW_LO(reg8);
52  void writePW_HI(reg8);
53  void writeCONTROL_REG(reg8);
54  reg8 readOSC();
55 
56  // 12-bit waveform output.
57  short output();
58 
59  // Calculate and set waveform output value.
60  void set_waveform_output();
61  void set_waveform_output(cycle_count delta_t);
62 
63 protected:
64  void clock_shift_register();
65  void write_shift_register();
66  void reset_shift_register();
67  void set_noise_output();
68 
69  const WaveformGenerator* sync_source;
70  WaveformGenerator* sync_dest;
71 
72  reg24 accumulator;
73 
74  // Tell whether the accumulator MSB was set high on this cycle.
75  bool msb_rising;
76 
77  // Fout = (Fn*Fclk/16777216)Hz
78  // reg16 freq;
79  reg24 freq;
80  // PWout = (PWn/40.95)%
81  reg12 pw;
82 
83  reg24 shift_register;
84 
85  // Remaining time to fully reset shift register.
86  cycle_count shift_register_reset;
87  // Emulation of pipeline causing bit 19 to clock the shift register.
88  cycle_count shift_pipeline;
89 
90  // Helper variables for waveform table lookup.
91  reg24 ring_msb_mask;
92  unsigned short no_noise;
93  unsigned short noise_output;
94  unsigned short no_noise_or_noise_output;
95  unsigned short no_pulse;
96  unsigned short pulse_output;
97 
98  // The control register right-shifted 4 bits; used for waveform table lookup.
99  reg8 waveform;
100 
101  // 8580 tri/saw pipeline
102  reg12 tri_saw_pipeline;
103  reg12 osc3;
104 
105  // The remaining control register bits.
106  reg8 test;
107  reg8 ring_mod;
108  reg8 sync;
109  // The gate bit is handled by the EnvelopeGenerator.
110 
111  // DAC input.
112  reg12 waveform_output;
113  // Fading time for floating DAC input (waveform 0).
114  cycle_count floating_output_ttl;
115 
116  chip_model sid_model;
117 
118  // Sample data for waveforms, not including noise.
119  unsigned short* wave;
120  static unsigned short model_wave[2][8][1 << 12];
121  // DAC lookup tables.
122  static unsigned short model_dac[2][1 << 12];
123 
124 friend class Voice;
125 friend class SID;
126 };
127 
128 
129 // ----------------------------------------------------------------------------
130 // Inline functions.
131 // The following functions are defined inline because they are called every
132 // time a sample is calculated.
133 // ----------------------------------------------------------------------------
134 
135 #if RESID_INLINING || defined(RESID_WAVE_CC)
136 
137 // ----------------------------------------------------------------------------
138 // SID clocking - 1 cycle.
139 // ----------------------------------------------------------------------------
140 RESID_INLINE
141 void WaveformGenerator::clock()
142 {
143  if (unlikely(test)) {
144  // Count down time to fully reset shift register.
145  if (unlikely(shift_register_reset) && unlikely(!--shift_register_reset)) {
146  reset_shift_register();
147  }
148 
149  // The test bit sets pulse high.
150  pulse_output = 0xfff;
151  }
152  else {
153  // Calculate new accumulator value;
154  reg24 accumulator_next = (accumulator + freq) & 0xffffff;
155  reg24 accumulator_bits_set = ~accumulator & accumulator_next;
156  accumulator = accumulator_next;
157 
158  // Check whether the MSB is set high. This is used for synchronization.
159  msb_rising = (accumulator_bits_set & 0x800000) ? true : false;
160 
161  // Shift noise register once for each time accumulator bit 19 is set high.
162  // The shift is delayed 2 cycles.
163  if (unlikely(accumulator_bits_set & 0x080000)) {
164  // Pipeline: Detect rising bit, shift phase 1, shift phase 2.
165  shift_pipeline = 2;
166  }
167  else if (unlikely(shift_pipeline) && !--shift_pipeline) {
168  clock_shift_register();
169  }
170  }
171 }
172 
173 // ----------------------------------------------------------------------------
174 // SID clocking - delta_t cycles.
175 // ----------------------------------------------------------------------------
176 RESID_INLINE
177 void WaveformGenerator::clock(cycle_count delta_t)
178 {
179  if (unlikely(test)) {
180  // Count down time to fully reset shift register.
181  if (shift_register_reset) {
182  shift_register_reset -= delta_t;
183  if (unlikely(shift_register_reset <= 0)) {
184  reset_shift_register();
185  }
186  }
187 
188  // The test bit sets pulse high.
189  pulse_output = 0xfff;
190  }
191  else {
192  // Calculate new accumulator value;
193  reg24 delta_accumulator = delta_t*freq;
194  reg24 accumulator_next = (accumulator + delta_accumulator) & 0xffffff;
195  reg24 accumulator_bits_set = ~accumulator & accumulator_next;
196  accumulator = accumulator_next;
197 
198  // Check whether the MSB is set high. This is used for synchronization.
199  msb_rising = (accumulator_bits_set & 0x800000) ? true : false;
200 
201  // NB! Any pipelined shift register clocking from single cycle clocking
202  // will be lost. It is not worth the trouble to flush the pipeline here.
203 
204  // Shift noise register once for each time accumulator bit 19 is set high.
205  // Bit 19 is set high each time 2^20 (0x100000) is added to the accumulator.
206  reg24 shift_period = 0x100000;
207 
208  while (delta_accumulator) {
209  if (likely(delta_accumulator < shift_period)) {
210  shift_period = delta_accumulator;
211  // Determine whether bit 19 is set on the last period.
212  // NB! Requires two's complement integer.
213  if (likely(shift_period <= 0x080000)) {
214  // Check for flip from 0 to 1.
215  if (((accumulator - shift_period) & 0x080000) || !(accumulator & 0x080000))
216  {
217  break;
218  }
219  }
220  else {
221  // Check for flip from 0 (to 1 or via 1 to 0) or from 1 via 0 to 1.
222  if (((accumulator - shift_period) & 0x080000) && !(accumulator & 0x080000))
223  {
224  break;
225  }
226  }
227  }
228 
229  // Shift the noise/random register.
230  // NB! The two-cycle pipeline delay is only modeled for 1 cycle clocking.
231  clock_shift_register();
232 
233  delta_accumulator -= shift_period;
234  }
235 
236  // Calculate pulse high/low.
237  // NB! The one-cycle pipeline delay is only modeled for 1 cycle clocking.
238  pulse_output = (accumulator >> 12) >= pw ? 0xfff : 0x000;
239  }
240 }
241 
242 
243 // ----------------------------------------------------------------------------
244 // Synchronize oscillators.
245 // This must be done after all the oscillators have been clock()'ed since the
246 // oscillators operate in parallel.
247 // Note that the oscillators must be clocked exactly on the cycle when the
248 // MSB is set high for hard sync to operate correctly. See SID::clock().
249 // ----------------------------------------------------------------------------
250 RESID_INLINE
251 void WaveformGenerator::synchronize()
252 {
253  // A special case occurs when a sync source is synced itself on the same
254  // cycle as when its MSB is set high. In this case the destination will
255  // not be synced. This has been verified by sampling OSC3.
256  if (unlikely(msb_rising) && sync_dest->sync && !(sync && sync_source->msb_rising)) {
257  sync_dest->accumulator = 0;
258  }
259 }
260 
261 
262 // ----------------------------------------------------------------------------
263 // Waveform output.
264 // The output from SID 8580 is delayed one cycle compared to SID 6581;
265 // this is only modeled for single cycle clocking (see sid.cc).
266 // ----------------------------------------------------------------------------
267 
268 // No waveform:
269 // When no waveform is selected, the DAC input is floating.
270 //
271 
272 // Triangle:
273 // The upper 12 bits of the accumulator are used.
274 // The MSB is used to create the falling edge of the triangle by inverting
275 // the lower 11 bits. The MSB is thrown away and the lower 11 bits are
276 // left-shifted (half the resolution, full amplitude).
277 // Ring modulation substitutes the MSB with MSB EOR NOT sync_source MSB.
278 //
279 
280 // Sawtooth:
281 // The output is identical to the upper 12 bits of the accumulator.
282 //
283 
284 // Pulse:
285 // The upper 12 bits of the accumulator are used.
286 // These bits are compared to the pulse width register by a 12 bit digital
287 // comparator; output is either all one or all zero bits.
288 // The pulse setting is delayed one cycle after the compare; this is only
289 // modeled for single cycle clocking.
290 //
291 // The test bit, when set to one, holds the pulse waveform output at 0xfff
292 // regardless of the pulse width setting.
293 //
294 
295 // Noise:
296 // The noise output is taken from intermediate bits of a 23-bit shift register
297 // which is clocked by bit 19 of the accumulator.
298 // The shift is delayed 2 cycles after bit 19 is set high; this is only
299 // modeled for single cycle clocking.
300 //
301 // Operation: Calculate EOR result, shift register, set bit 0 = result.
302 //
303 // reset -------------------------------------------
304 // | | |
305 // test--OR-->EOR<-- |
306 // | | |
307 // 2 2 2 1 1 1 1 1 1 1 1 1 1 |
308 // Register bits: 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 <---
309 // | | | | | | | |
310 // Waveform bits: 1 1 9 8 7 6 5 4
311 // 1 0
312 //
313 // The low 4 waveform bits are zero (grounded).
314 //
315 
316 RESID_INLINE void WaveformGenerator::clock_shift_register()
317 {
318  // bit0 = (bit22 | test) ^ bit17
319  reg24 bit0 = ((shift_register >> 22) ^ (shift_register >> 17)) & 0x1;
320  shift_register = ((shift_register << 1) | bit0) & 0x7fffff;
321 
322  // New noise waveform output.
323  set_noise_output();
324 }
325 
326 RESID_INLINE void WaveformGenerator::write_shift_register()
327 {
328  // Write changes to the shift register output caused by combined waveforms
329  // back into the shift register.
330  // A bit once set to zero cannot be changed, hence the and'ing.
331  // FIXME: Write test program to check the effect of 1 bits and whether
332  // neighboring bits are affected.
333 
334  shift_register &=
335  ~((1<<20)|(1<<18)|(1<<14)|(1<<11)|(1<<9)|(1<<5)|(1<<2)|(1<<0)) |
336  ((waveform_output & 0x800) << 9) | // Bit 11 -> bit 20
337  ((waveform_output & 0x400) << 8) | // Bit 10 -> bit 18
338  ((waveform_output & 0x200) << 5) | // Bit 9 -> bit 14
339  ((waveform_output & 0x100) << 3) | // Bit 8 -> bit 11
340  ((waveform_output & 0x080) << 2) | // Bit 7 -> bit 9
341  ((waveform_output & 0x040) >> 1) | // Bit 6 -> bit 5
342  ((waveform_output & 0x020) >> 3) | // Bit 5 -> bit 2
343  ((waveform_output & 0x010) >> 4); // Bit 4 -> bit 0
344 
345  noise_output &= waveform_output;
346  no_noise_or_noise_output = no_noise | noise_output;
347 }
348 
349 RESID_INLINE void WaveformGenerator::reset_shift_register()
350 {
351  shift_register = 0x7fffff;
352  shift_register_reset = 0;
353 
354  // New noise waveform output.
355  set_noise_output();
356 }
357 
358 RESID_INLINE void WaveformGenerator::set_noise_output()
359 {
360  noise_output =
361  ((shift_register & 0x100000) >> 9) |
362  ((shift_register & 0x040000) >> 8) |
363  ((shift_register & 0x004000) >> 5) |
364  ((shift_register & 0x000800) >> 3) |
365  ((shift_register & 0x000200) >> 2) |
366  ((shift_register & 0x000020) << 1) |
367  ((shift_register & 0x000004) << 3) |
368  ((shift_register & 0x000001) << 4);
369 
370  no_noise_or_noise_output = no_noise | noise_output;
371 }
372 
373 // Combined waveforms:
374 // By combining waveforms, the bits of each waveform are effectively short
375 // circuited. A zero bit in one waveform will result in a zero output bit
376 // (thus the infamous claim that the waveforms are AND'ed).
377 // However, a zero bit in one waveform may also affect the neighboring bits
378 // in the output.
379 //
380 // Example:
381 //
382 // 1 1
383 // Bit # 1 0 9 8 7 6 5 4 3 2 1 0
384 // -----------------------
385 // Sawtooth 0 0 0 1 1 1 1 1 1 0 0 0
386 //
387 // Triangle 0 0 1 1 1 1 1 1 0 0 0 0
388 //
389 // AND 0 0 0 1 1 1 1 1 0 0 0 0
390 //
391 // Output 0 0 0 0 1 1 1 0 0 0 0 0
392 //
393 //
394 // Re-vectorized die photographs reveal the mechanism behind this behavior.
395 // Each waveform selector bit acts as a switch, which directly connects
396 // internal outputs into the waveform DAC inputs as follows:
397 //
398 // * Noise outputs the shift register bits to DAC inputs as described above.
399 // Each output is also used as input to the next bit when the shift register
400 // is shifted.
401 // * Pulse connects a single line to all DAC inputs. The line is connected to
402 // either 5V (pulse on) or 0V (pulse off) at bit 11, and ends at bit 0.
403 // * Triangle connects the upper 11 bits of the (MSB EOR'ed) accumulator to the
404 // DAC inputs, so that DAC bit 0 = 0, DAC bit n = accumulator bit n - 1.
405 // * Sawtooth connects the upper 12 bits of the accumulator to the DAC inputs,
406 // so that DAC bit n = accumulator bit n. Sawtooth blocks out the MSB from
407 // the EOR used to generate the triangle waveform.
408 //
409 // We can thus draw the following conclusions:
410 //
411 // * The shift register may be written to by combined waveforms.
412 // * The pulse waveform interconnects all bits in combined waveforms via the
413 // pulse line.
414 // * The combination of triangle and sawtooth interconnects neighboring bits
415 // of the sawtooth waveform.
416 //
417 // This behavior would be quite difficult to model exactly, since the short
418 // circuits are not binary, but are subject to analog effects. Tests show that
419 // minor (1 bit) differences can actually occur in the output from otherwise
420 // identical samples from OSC3 when waveforms are combined. To further
421 // complicate the situation the output changes slightly with time (more
422 // neighboring bits are successively set) when the 12-bit waveform
423 // registers are kept unchanged.
424 //
425 // The output is instead approximated by using the upper bits of the
426 // accumulator as an index to look up the combined output in a table
427 // containing actual combined waveform samples from OSC3.
428 // These samples are 8 bit, so 4 bits of waveform resolution is lost.
429 // All OSC3 samples are taken with FREQ=0x1000, adding a 1 to the upper 12
430 // bits of the accumulator each cycle for a sample period of 4096 cycles.
431 //
432 // Sawtooth+Triangle:
433 // The accumulator is used to look up an OSC3 sample.
434 //
435 // Pulse+Triangle:
436 // The accumulator is used to look up an OSC3 sample. When ring modulation is
437 // selected, the accumulator MSB is substituted with MSB EOR NOT sync_source MSB.
438 //
439 // Pulse+Sawtooth:
440 // The accumulator is used to look up an OSC3 sample.
441 // The sample is output if the pulse output is on.
442 //
443 // Pulse+Sawtooth+Triangle:
444 // The accumulator is used to look up an OSC3 sample.
445 // The sample is output if the pulse output is on.
446 //
447 // Combined waveforms including noise:
448 // All waveform combinations including noise output zero after a few cycles,
449 // since the waveform bits are and'ed into the shift register via the shift
450 // register outputs.
451 
452 RESID_INLINE
453 void WaveformGenerator::set_waveform_output()
454 {
455  // Set output value.
456  if (likely(waveform)) {
457  // The bit masks no_pulse and no_noise are used to achieve branch-free
458  // calculation of the output value.
459  int ix = (accumulator ^ (~sync_source->accumulator & ring_msb_mask)) >> 12;
460 
461  waveform_output = wave[ix] & (no_pulse | pulse_output) & no_noise_or_noise_output;
462 
463  // Triangle/Sawtooth output is delayed half cycle on 8580.
464  // This will appear as a one cycle delay on OSC3 as it is
465  // latched in the first phase of the clock.
466  if ((waveform & 3) && (sid_model == MOS8580))
467  {
468  osc3 = tri_saw_pipeline & (no_pulse | pulse_output) & no_noise_or_noise_output;
469  tri_saw_pipeline = wave[ix];
470  }
471  else
472  {
473  osc3 = waveform_output;
474  }
475 
476  if ((waveform & 0x2) && unlikely(waveform & 0xd) && (sid_model == MOS6581)) {
477  // In the 6581 the top bit of the accumulator may be driven low by combined waveforms
478  // when the sawtooth is selected
479  accumulator &= (waveform_output << 12) | 0x7fffff;
480  }
481 
482  if (unlikely(waveform > 0x8) && likely(!test) && likely(shift_pipeline != 1)) {
483  // Combined waveforms write to the shift register.
484  write_shift_register();
485  }
486  }
487  else {
488  // Age floating DAC input.
489  if (likely(floating_output_ttl) && unlikely(!--floating_output_ttl)) {
490  waveform_output = 0;
491  }
492  }
493 
494  // The pulse level is defined as (accumulator >> 12) >= pw ? 0xfff : 0x000.
495  // The expression -((accumulator >> 12) >= pw) & 0xfff yields the same
496  // results without any branching (and thus without any pipeline stalls).
497  // NB! This expression relies on that the result of a boolean expression
498  // is either 0 or 1, and furthermore requires two's complement integer.
499  // A few more cycles may be saved by storing the pulse width left shifted
500  // 12 bits, and dropping the and with 0xfff (this is valid since pulse is
501  // used as a bit mask on 12 bit values), yielding the expression
502  // -(accumulator >= pw24). However this only results in negligible savings.
503 
504  // The result of the pulse width compare is delayed one cycle.
505  // Push next pulse level into pulse level pipeline.
506  pulse_output = -((accumulator >> 12) >= pw) & 0xfff;
507 }
508 
509 RESID_INLINE
510 void WaveformGenerator::set_waveform_output(cycle_count delta_t)
511 {
512  // Set output value.
513  if (likely(waveform)) {
514  // The bit masks no_pulse and no_noise are used to achieve branch-free
515  // calculation of the output value.
516  int ix = (accumulator ^ (~sync_source->accumulator & ring_msb_mask)) >> 12;
517  waveform_output =
518  wave[ix] & (no_pulse | pulse_output) & no_noise_or_noise_output;
519  // Triangle/Sawtooth output delay for the 8580 is not modeled
520  osc3 = waveform_output;
521 
522  if ((waveform & 0x2) && unlikely(waveform & 0xd) && (sid_model == MOS6581)) {
523  accumulator &= (waveform_output << 12) | 0x7fffff;
524  }
525 
526  if (unlikely(waveform > 0x8) && likely(!test)) {
527  // Combined waveforms write to the shift register.
528  // NB! Since cycles are skipped in delta_t clocking, writes will be
529  // missed. Single cycle clocking must be used for 100% correct operation.
530  write_shift_register();
531  }
532  }
533  else {
534  if (likely(floating_output_ttl)) {
535  // Age floating D/A output.
536  floating_output_ttl -= delta_t;
537  if (unlikely(floating_output_ttl <= 0)) {
538  floating_output_ttl = 0;
539  waveform_output = 0;
540  }
541  }
542  }
543 }
544 
545 
546 // ----------------------------------------------------------------------------
547 // Waveform output (12 bits).
548 // ----------------------------------------------------------------------------
549 
550 // The digital waveform output is converted to an analog signal by a 12-bit
551 // DAC. Re-vectorized die photographs reveal that the DAC is an R-2R ladder
552 // built up as follows:
553 //
554 // 12V 11 10 9 8 7 6 5 4 3 2 1 0 GND
555 // Strange | | | | | | | | | | | | | | Missing
556 // part 2R 2R 2R 2R 2R 2R 2R 2R 2R 2R 2R 2R 2R 2R term.
557 // (bias) | | | | | | | | | | | | | |
558 // --R- --R---R---R---R---R---R---R---R---R---R---R-- ---
559 // | _____
560 // __|__ __|__ |
561 // ----- ===== |
562 // | | | | |
563 // 12V --- ----- ------- GND
564 // |
565 // wout
566 //
567 // Bit on: 5V
568 // Bit off: 0V (GND)
569 //
570 // As is the case with all MOS 6581 DACs, the termination to (virtual) ground
571 // at bit 0 is missing. The MOS 8580 has correct termination, and has also
572 // done away with the bias part on the left hand side of the figure above.
573 //
574 
575 RESID_INLINE
576 short WaveformGenerator::output()
577 {
578  // DAC imperfections are emulated by using waveform_output as an index
579  // into a DAC lookup table. readOSC() uses waveform_output directly.
580  return model_dac[sid_model][waveform_output];
581 }
582 
583 #endif // RESID_INLINING || defined(RESID_WAVE_CC)
584 
585 } // namespace reSID
586 
587 #endif // not RESID_WAVE_H
reSID::SID
Definition: sid.h:39
reSID::Voice
Definition: voice.h:31
reSID::WaveformGenerator
Definition: wave.h:37