Debian Codegraph @

src/engine/platform/sound/sid2/wave.h

raw
1 // ---------------------------------------------------------------------------
2 // This file is part of reSID, a MOS6581_2 SID2 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
21 #include "siddefs.h"
22
23 // ----------------------------------------------------------------------------
24 // A 24 bit accumulator is the basis for waveform generation. FREQ is added to
25 // the lower 16 bits of the accumulator each cycle.
26 // The accumulator is set to zero when TEST is set, and starts counting
27 // when TEST is cleared.
28 // The noise waveform is taken from intermediate bits of a 23 bit shift
29 // register. This register is clocked by bit 19 of the accumulator.
30 // ----------------------------------------------------------------------------
31 class WaveformGenerator2
32 {
33 public:
34 WaveformGenerator2();
35
36 void set_sync_source(WaveformGenerator2*);
37 void set_chip_model(chip_model2 model);
38
39 RESID_INLINE void clock();
40 RESID_INLINE void synchronize();
41 void reset();
42
43 void writeFREQ_LO(reg8);
44 void writeFREQ_HI(reg8);
45 void writeFREQ_HIGHEST(reg8);
46 void writePW_LO(reg8);
47 void writePW_HI(reg8);
48 void writeCONTROL_REG(reg8);
49 void writeNOISE_MODE(reg8);
50 void writeMIX_MODE(reg8);
51 reg8 readOSC();
52
53 // 12-bit waveform output.
54 RESID_INLINE reg12 output();
55
56 protected:
57 const WaveformGenerator2* sync_source;
58 WaveformGenerator2* sync_dest;
59
60 // Tell whether the accumulator MSB was set high on this cycle.
61 bool msb_rising;
62
63 reg24 accumulator;
64 reg24 shift_register;
65
66 // Fout = (Fn*Fclk/16777216)Hz
67 reg16 freq;
68 // PWout = (PWn/40.95)%
69 reg12 pw;
70
71 // The control register right-shifted 4 bits; used for output function
72 // table lookup.
73 reg8 waveform;
74
75 reg8 mix_mode;
76 reg8 noise_mode;
77
78 // The remaining control register bits.
79 reg8 test;
80 reg8 ring_mod;
81 reg8 sync;
82 // The gate bit is handled by the EnvelopeGenerator2.
83
84 // 16 possible combinations of waveforms.
85 RESID_INLINE reg12 output____();
86 RESID_INLINE reg12 output___T();
87 RESID_INLINE reg12 output__S_();
88 RESID_INLINE reg12 output__ST();
89 RESID_INLINE reg12 output_P__();
90 RESID_INLINE reg12 output_P_T();
91 RESID_INLINE reg12 output_PS_();
92 RESID_INLINE reg12 output_PST();
93 RESID_INLINE reg12 outputN___();
94 RESID_INLINE reg12 outputN__T();
95 RESID_INLINE reg12 outputN_S_();
96 RESID_INLINE reg12 outputN_ST();
97 RESID_INLINE reg12 outputNP__();
98 RESID_INLINE reg12 outputNP_T();
99 RESID_INLINE reg12 outputNPS_();
100 RESID_INLINE reg12 outputNPST();
101
102 // Sample data for combinations of waveforms.
103 static reg8 wave8580__ST[];
104 static reg8 wave8580_P_T[];
105 static reg8 wave8580_PS_[];
106 static reg8 wave8580_PST[];
107
108 reg8* wave__ST;
109 reg8* wave_P_T;
110 reg8* wave_PS_;
111 reg8* wave_PST;
112
113 friend class Voice2;
114 friend class SID2;
115 };
116
117
118 // ----------------------------------------------------------------------------
119 // Inline functions.
120 // The following functions are defined inline because they are called every
121 // time a sample is calculated.
122 // ----------------------------------------------------------------------------
123
124 #if RESID_INLINING || defined(__WAVE_CC__)
125
126 // ----------------------------------------------------------------------------
127 // SID2 clocking - 1 cycle.
128 // ----------------------------------------------------------------------------
129 RESID_INLINE
130 void WaveformGenerator2::clock()
131 {
132 // No operation if test bit is set.
133 if (test) {
134 return;
135 }
136
137 reg24 accumulator_prev = accumulator;
138
139 // Calculate new accumulator value;
140 accumulator += freq;
141 accumulator &= 0xffffff;
142
143 // Check whether the MSB is set high. This is used for synchronization.
144 msb_rising = !(accumulator_prev & 0x800000) && (accumulator & 0x800000);
145
146 // Shift noise register once for each time accumulator bit 19 is set high.
147 if (!(accumulator_prev & 0x080000) && (accumulator & 0x080000)) {
148
149 switch(noise_mode)
150 {
151 case 0:
152 {
153 reg24 bit0 = ((shift_register >> 22) ^ (shift_register >> 17)) & 0x1;
154 shift_register <<= 1;
155 shift_register &= 0x7fffff;
156 shift_register |= bit0;
157 break;
158 }
159
160 case 1:
161 {
162 reg24 bit0 = ((shift_register >> 15) ^ (shift_register >> 7)) & 0x1;
163 shift_register <<= 1;
164 shift_register &= 0x7fffff;
165 shift_register |= bit0;
166 break;
167 }
168
169 case 2:
170 {
171 reg24 bit0 = ((shift_register >> 2) ^ (shift_register >> 4)) & 0x1;
172 shift_register <<= 1;
173 shift_register &= 0x7fffff;
174 shift_register |= bit0;
175 break;
176 }
177
178 case 3:
179 {
180 reg24 bit0 = ((shift_register >> 5) ^ (shift_register >> 4)) & 0x1;
181 shift_register <<= 1;
182 shift_register &= 0x7fffff;
183 shift_register |= bit0;
184 break;
185 }
186 default: break;
187 }
188 }
189 }
190
191
192 // ----------------------------------------------------------------------------
193 // Synchronize oscillators.
194 // This must be done after all the oscillators have been clock()'ed since the
195 // oscillators operate in parallel.
196 // Note that the oscillators must be clocked exactly on the cycle when the
197 // MSB is set high for hard sync to operate correctly. See SID2::clock().
198 // ----------------------------------------------------------------------------
199 RESID_INLINE
200 void WaveformGenerator2::synchronize()
201 {
202 // A special case occurs when a sync source is synced itself on the same
203 // cycle as when its MSB is set high. In this case the destination will
204 // not be synced. This has been verified by sampling OSC3.
205 if (msb_rising && sync_dest->sync && !(sync && sync_source->msb_rising)) {
206 sync_dest->accumulator = 0;
207 sync_dest->shift_register = 0x7ffff8;
208 }
209 }
210
211
212 // ----------------------------------------------------------------------------
213 // Output functions.
214 // NB! The output from SID2 8580 is delayed one cycle compared to SID2 6581,
215 // this is not modeled.
216 // ----------------------------------------------------------------------------
217
218 // No waveform:
219 // Zero output.
220 //
221 RESID_INLINE
222 reg12 WaveformGenerator2::output____()
223 {
224 return 0x000;
225 }
226
227 // Triangle:
228 // The upper 12 bits of the accumulator are used.
229 // The MSB is used to create the falling edge of the triangle by inverting
230 // the lower 11 bits. The MSB is thrown away and the lower 11 bits are
231 // left-shifted (half the resolution, full amplitude).
232 // Ring modulation substitutes the MSB with MSB EOR sync_source MSB.
233 //
234 RESID_INLINE
235 reg12 WaveformGenerator2::output___T()
236 {
237 reg24 msb = (ring_mod ? accumulator ^ sync_source->accumulator : accumulator)
238 & 0x800000;
239 return ((msb ? ~accumulator : accumulator) >> 11) & 0xfff;
240 }
241
242 // Sawtooth:
243 // The output is identical to the upper 12 bits of the accumulator.
244 //
245 RESID_INLINE
246 reg12 WaveformGenerator2::output__S_()
247 {
248 return accumulator >> 12;
249 }
250
251 // Pulse:
252 // The upper 12 bits of the accumulator are used.
253 // These bits are compared to the pulse width register by a 12 bit digital
254 // comparator; output is either all one or all zero bits.
255 // NB! The output is actually delayed one cycle after the compare.
256 // This is not modeled.
257 //
258 // The test bit, when set to one, holds the pulse waveform output at 0xfff
259 // regardless of the pulse width setting.
260 //
261 RESID_INLINE
262 reg12 WaveformGenerator2::output_P__()
263 {
264 return (test || (accumulator >> 12) >= pw) ? 0xfff : 0x000;
265 }
266
267 // Noise:
268 // The noise output is taken from intermediate bits of a 23-bit shift register
269 // which is clocked by bit 19 of the accumulator.
270 // NB! The output is actually delayed 2 cycles after bit 19 is set high.
271 // This is not modeled.
272 //
273 // Operation: Calculate EOR result, shift register, set bit 0 = result.
274 //
275 // ----------------------->---------------------
276 // | |
277 // ----EOR---- |
278 // | | |
279 // 2 2 2 1 1 1 1 1 1 1 1 1 1 |
280 // 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 <---
281 // | | | | | | | |
282 // OSC3 bits : 7 6 5 4 3 2 1 0
283 //
284 // Since waveform output is 12 bits the output is left-shifted 4 times.
285 //
286 RESID_INLINE
287 reg12 WaveformGenerator2::outputN___()
288 {
289 return
290 ((shift_register & 0x400000) >> 11) |
291 ((shift_register & 0x100000) >> 10) |
292 ((shift_register & 0x010000) >> 7) |
293 ((shift_register & 0x002000) >> 5) |
294 ((shift_register & 0x000800) >> 4) |
295 ((shift_register & 0x000080) >> 1) |
296 ((shift_register & 0x000010) << 1) |
297 ((shift_register & 0x000004) << 2);
298 }
299
300 // Combined waveforms:
301 // By combining waveforms, the bits of each waveform are effectively short
302 // circuited. A zero bit in one waveform will result in a zero output bit
303 // (thus the infamous claim that the waveforms are AND'ed).
304 // However, a zero bit in one waveform will also affect the neighboring bits
305 // in the output. The reason for this has not been determined.
306 //
307 // Example:
308 //
309 // 1 1
310 // Bit # 1 0 9 8 7 6 5 4 3 2 1 0
311 // -----------------------
312 // Sawtooth 0 0 0 1 1 1 1 1 1 0 0 0
313 //
314 // Triangle 0 0 1 1 1 1 1 1 0 0 0 0
315 //
316 // AND 0 0 0 1 1 1 1 1 0 0 0 0
317 //
318 // Output 0 0 0 0 1 1 1 0 0 0 0 0
319 //
320 //
321 // This behavior would be quite difficult to model exactly, since the SID2
322 // in this case does not act as a digital state machine. Tests show that minor
323 // (1 bit) differences can actually occur in the output from otherwise
324 // identical samples from OSC3 when waveforms are combined. To further
325 // complicate the situation the output changes slightly with time (more
326 // neighboring bits are successively set) when the 12-bit waveform
327 // registers are kept unchanged.
328 //
329 // It is probably possible to come up with a valid model for the
330 // behavior, however this would be far too slow for practical use since it
331 // would have to be based on the mutual influence of individual bits.
332 //
333 // The output is instead approximated by using the upper bits of the
334 // accumulator as an index to look up the combined output in a table
335 // containing actual combined waveform samples from OSC3.
336 // These samples are 8 bit, so 4 bits of waveform resolution is lost.
337 // All OSC3 samples are taken with FREQ=0x1000, adding a 1 to the upper 12
338 // bits of the accumulator each cycle for a sample period of 4096 cycles.
339 //
340 // Sawtooth+Triangle:
341 // The sawtooth output is used to look up an OSC3 sample.
342 //
343 // Pulse+Triangle:
344 // The triangle output is right-shifted and used to look up an OSC3 sample.
345 // The sample is output if the pulse output is on.
346 // The reason for using the triangle output as the index is to handle ring
347 // modulation. Only the first half of the sample is used, which should be OK
348 // since the triangle waveform has half the resolution of the accumulator.
349 //
350 // Pulse+Sawtooth:
351 // The sawtooth output is used to look up an OSC3 sample.
352 // The sample is output if the pulse output is on.
353 //
354 // Pulse+Sawtooth+Triangle:
355 // The sawtooth output is used to look up an OSC3 sample.
356 // The sample is output if the pulse output is on.
357 //
358 RESID_INLINE
359 reg12 WaveformGenerator2::output__ST()
360 {
361 switch(mix_mode)
362 {
363 case 0: return wave__ST[output__S_()] << 4; break;
364 case 1: return (output__S_() & output___T()); break;
365 case 2: return (output__S_() | output___T()); break;
366 case 3: return (output__S_() ^ output___T()); break;
367 default: return 0; break;
368 }
369 }
370
371 RESID_INLINE
372 reg12 WaveformGenerator2::output_P_T()
373 {
374 switch(mix_mode)
375 {
376 case 0: return (wave_P_T[output___T() >> 1] << 4) & output_P__(); break;
377 case 1: return (output___T() & output_P__()); break;
378 case 2: return (output___T() | output_P__()); break;
379 case 3: return (output___T() ^ output_P__()); break;
380 default: return 0; break;
381 }
382 }
383
384 RESID_INLINE
385 reg12 WaveformGenerator2::output_PS_()
386 {
387 switch(mix_mode)
388 {
389 case 0: return (wave_PS_[output__S_()] << 4) & output_P__(); break;
390 case 1: return (output__S_() & output_P__()); break;
391 case 2: return (output__S_() | output_P__()); break;
392 case 3: return (output__S_() ^ output_P__()); break;
393 default: return 0; break;
394 }
395 }
396
397 RESID_INLINE
398 reg12 WaveformGenerator2::output_PST()
399 {
400 switch(mix_mode)
401 {
402 case 0: return (wave_PST[output__S_()] << 4) & output_P__(); break;
403 case 1: return (output__S_() & output_P__() & output___T()); break;
404 case 2: return (output__S_() | output_P__() | output___T()); break;
405 case 3: return (output__S_() ^ output_P__() ^ output___T()); break;
406 default: return 0; break;
407 }
408 }
409
410 // Combined waveforms including noise:
411 // All waveform combinations including noise output zero after a few cycles.
412 // NB! The effects of such combinations are not fully explored. It is claimed
413 // that the shift register may be filled with zeroes and locked up, which
414 // seems to be true.
415 // We have not attempted to model this behavior, suffice to say that
416 // there is very little audible output from waveform combinations including
417 // noise. We hope that nobody is actually using it.
418 //
419 RESID_INLINE
420 reg12 WaveformGenerator2::outputN__T()
421 {
422 switch(mix_mode)
423 {
424 case 0: return 0; break;
425 case 1: return (output___T() & outputN___()); break;
426 case 2: return (output___T() | outputN___()); break;
427 case 3: return (output___T() ^ outputN___()); break;
428 default: return 0; break;
429 }
430 }
431
432 RESID_INLINE
433 reg12 WaveformGenerator2::outputN_S_()
434 {
435 switch(mix_mode)
436 {
437 case 0: return 0; break;
438 case 1: return (output__S_() & outputN___()); break;
439 case 2: return (output__S_() | outputN___()); break;
440 case 3: return (output__S_() ^ outputN___()); break;
441 default: return 0; break;
442 }
443 }
444
445 RESID_INLINE
446 reg12 WaveformGenerator2::outputN_ST()
447 {
448 switch(mix_mode)
449 {
450 case 0: return 0; break;
451 case 1: return (output__S_() & outputN___() & output___T()); break;
452 case 2: return (output__S_() | outputN___() | output___T()); break;
453 case 3: return (output__S_() ^ outputN___() ^ output___T()); break;
454 default: return 0; break;
455 }
456 }
457
458 RESID_INLINE
459 reg12 WaveformGenerator2::outputNP__()
460 {
461 switch(mix_mode)
462 {
463 case 0: return 0; break;
464 case 1: return (output_P__() & outputN___()); break;
465 case 2: return (output_P__() | outputN___()); break;
466 case 3: return (output_P__() ^ outputN___()); break;
467 default: return 0; break;
468 }
469 }
470
471 RESID_INLINE
472 reg12 WaveformGenerator2::outputNP_T()
473 {
474 switch(mix_mode)
475 {
476 case 0: return 0; break;
477 case 1: return (output_P__() & outputN___() & output___T()); break;
478 case 2: return (output_P__() | outputN___() | output___T()); break;
479 case 3: return (output_P__() ^ outputN___() ^ output___T()); break;
480 default: return 0; break;
481 }
482 }
483
484 RESID_INLINE
485 reg12 WaveformGenerator2::outputNPS_()
486 {
487 switch(mix_mode)
488 {
489 case 0: return 0; break;
490 case 1: return (output_P__() & outputN___() & output__S_()); break;
491 case 2: return (output_P__() | outputN___() | output__S_()); break;
492 case 3: return (output_P__() ^ outputN___() ^ output__S_()); break;
493 default: return 0; break;
494 }
495 }
496
497 RESID_INLINE
498 reg12 WaveformGenerator2::outputNPST()
499 {
500 switch(mix_mode)
501 {
502 case 0: return 0; break;
503 case 1: return (output_P__() & outputN___() & output__S_() & output___T()); break;
504 case 2: return (output_P__() | outputN___() | output__S_() | output___T()); break;
505 case 3: return (output_P__() ^ outputN___() ^ output__S_() ^ output___T()); break;
506 default: return 0; break;
507 }
508 }
509
510 // ----------------------------------------------------------------------------
511 // Select one of 16 possible combinations of waveforms.
512 // ----------------------------------------------------------------------------
513 RESID_INLINE
514 reg12 WaveformGenerator2::output()
515 {
516 // It may seem cleaner to use an array of member functions to return
517 // waveform output; however a switch with inline functions is faster.
518
519 switch (waveform) {
520 default:
521 case 0x0:
522 return output____();
523 case 0x1:
524 return output___T();
525 case 0x2:
526 return output__S_();
527 case 0x3:
528 return output__ST();
529 case 0x4:
530 return output_P__();
531 case 0x5:
532 return output_P_T();
533 case 0x6:
534 return output_PS_();
535 case 0x7:
536 return output_PST();
537 case 0x8:
538 return outputN___();
539 case 0x9:
540 return outputN__T();
541 case 0xa:
542 return outputN_S_();
543 case 0xb:
544 return outputN_ST();
545 case 0xc:
546 return outputNP__();
547 case 0xd:
548 return outputNP_T();
549 case 0xe:
550 return outputNPS_();
551 case 0xf:
552 return outputNPST();
553 }
554 }
555
556 #endif // RESID_INLINING || defined(__WAVE_CC__)
557