git @ Cat's Eye Technologies JaC64 / master resid / WaveformGenerator.java
master

Tree @master (Download .tar.gz)

WaveformGenerator.java @masterraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
/**
 * encoding: UTF-8
 * This file is part of reSID, a MOS6581 SID emulator engine.
 * Copyright (C) 2004  Dag Lem <resid@nimrod.no>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * @author Ken Händel
 *
 */
package resid;

import static resid.SID.ANTTI_LANKILA_PATCH;
import resid.ISIDDefs.chip_model;

/**
 * A 24 bit accumulator is the basis for waveform generation. FREQ is added to
 * the lower 16 bits of the accumulator each cycle. The accumulator is set to
 * zero when TEST is set, and starts counting when TEST is cleared. The noise
 * waveform is taken from intermediate bits of a 23 bit shift register. This
 * register is clocked by bit 19 of the accumulator.
 * 
 * @author Ken Händel
 * 
 */
public class WaveformGenerator implements IWave6581, IWave8580 {

	protected WaveformGenerator sync_source = null;

	protected WaveformGenerator sync_dest = null;

	/**
	 * Tell whether the accumulator MSB was set high on this cycle.
	 */
	protected boolean msb_rising;

	protected int /* reg24 */accumulator;

	protected int /* reg24 */shift_register;

	/**
	 * Fout = (Fn*Fclk/16777216)Hz
	 */
	protected int /* reg16 */freq;

	/**
	 * PWout = (PWn/40.95)%
	 */
	protected int /* reg12 */pw;

	/**
	 * The control register right-shifted 4 bits; used for output function table
	 * lookup.
	 */
	protected int /* reg8 */waveform;

	/**
	 * The remaining control register bits.
	 */
	protected int /* reg8 */test;

	/**
	 * The remaining control register bits.
	 */
	protected int /* reg8 */ring_mod;

	/**
	 * The remaining control register bits.
	 */
	protected int /* reg8 */sync;

	// The gate bit is handled by the EnvelopeGenerator

	// Sample data for combinations of waveforms.

	int /* reg8 */wave__ST[];

	int /* reg8 */wave_P_T[];

	int /* reg8 */wave_PS_[];

	int /* reg8 */wave_PST[];

	// The gate bit is handled by the EnvelopeGenerator.

	/**
	 * Constructor.
	 */
	public WaveformGenerator() {
		sync_source = this;

		set_chip_model(chip_model.MOS6581);

		reset();
	}

	/**
	 * Set sync source.
	 * 
	 * @param source
	 *            sync source
	 */
	public void set_sync_source(WaveformGenerator source) {
		sync_source = source;
		source.sync_dest = this;
	}

	/**
	 * Set chip model.
	 * 
	 * @param model
	 *            chip model
	 */
	public void set_chip_model(chip_model model) {
		if (model == chip_model.MOS6581) {
			wave__ST = wave6581__ST;
			wave_P_T = wave6581_P_T;
			wave_PS_ = wave6581_PS_;
			wave_PST = wave6581_PST;
		} else {
			wave__ST = wave8580__ST;
			wave_P_T = wave8580_P_T;
			wave_PS_ = wave8580_PS_;
			wave_PST = wave8580_PST;
		}
	}

	/**
	 * Register functions.
	 * 
	 * @param freq_lo
	 */
	public void writeFREQ_LO(int /* reg8 */freq_lo) {
		freq = freq & 0xff00 | freq_lo & 0x00ff;
	}

	/**
	 * Register functions.
	 * 
	 * @param freq_hi
	 */
	public void writeFREQ_HI(int /* reg8 */freq_hi) {
		freq = (freq_hi << 8) & 0xff00 | freq & 0x00ff;
	}

	/**
	 * Register functions.
	 * 
	 * @param pw_lo
	 */
	public void writePW_LO(int /* reg8 */pw_lo) {
		pw = pw & 0xf00 | pw_lo & 0x0ff;
	}

	/**
	 * Register functions.
	 * 
	 * @param pw_hi
	 */
	public void writePW_HI(int /* reg8 */pw_hi) {
		pw = (pw_hi << 8) & 0xf00 | pw & 0x0ff;
	}

	/**
	 * Register functions.
	 * 
	 * @param control
	 */
	public void writeCONTROL_REG(int /* reg8 */control) {
		waveform = (control >> 4) & 0x0f;
		ring_mod = control & 0x04;
		sync = control & 0x02;

		int /* reg8 */test_next = control & 0x08;

		if (ANTTI_LANKILA_PATCH) {
			/*
			 * SounDemoN found out that test bit can be used to control the
			 * noise register. Hear the result in Bojojoing.sid.
			 */

			// testbit set. invert bit 19 and write it to bit 1
			if (test_next != 0 && test == 0) {
				accumulator = 0;
				int /* reg24 */bit19 = (shift_register >> 19) & 1;
				shift_register = (shift_register & 0x7ffffd)
						| ((bit19 ^ 1) << 1);
			}
			// Test bit cleared.
			// The accumulator starts counting, and the shift register is reset
			// to
			// the value 0x7ffff8.
			// NB! The shift register will not actually be set to this exact
			// value
			// if the
			// shift register bits have not had time to fade to zero.
			// This is not modeled.
			else if (test_next == 0 && test > 0) {
				int /* reg24 */bit0 = ((shift_register >> 22) ^ (shift_register >> 17)) & 0x1;
				shift_register <<= 1;
				shift_register &= 0x7fffff;
				shift_register |= bit0;
			}
			// clear output bits of shift register if noise and other waveforms
			// are selected simultaneously

			if (waveform > 8) {
				shift_register &= 0x7fffff ^ (1 << 22) ^ (1 << 20) ^ (1 << 16)
						^ (1 << 13) ^ (1 << 11) ^ (1 << 7) ^ (1 << 4)
						^ (1 << 2);
			}
		} else {
			// Test bit set.
			// The accumulator and the shift register are both cleared.
			// NB! The shift register is not really cleared immediately. It
			// seems
			// like the individual bits in the shift register start to fade down
			// towards zero when test is set. All bits reach zero within
			// approximately $2000 - $4000 cycles.
			// This is not modeled. There should fortunately be little audible
			// output from this peculiar behavior.
			if (test_next != 0) {
				accumulator = 0;
				shift_register = 0;
			}
			// Test bit cleared.
			// The accumulator starts counting, and the shift register is reset
			// to
			// the value 0x7ffff8.
			// NB! The shift register will not actually be set to this exact
			// value
			// if the
			// shift register bits have not had time to fade to zero.
			// This is not modeled.
			else if (test != 0) {
				shift_register = 0x7ffff8;
			}
		}
		test = test_next;

		// The gate bit is handled by the EnvelopeGenerator.
	}

	public int /* reg8 */readOSC() {
		return output() >> 4;
	}

	/**
	 * SID reset.
	 */
	public void reset() {
		accumulator = 0;
		if (ANTTI_LANKILA_PATCH)
			shift_register = 0x7ffffc;
		else
			shift_register = 0x7ffff8;
		freq = 0;
		pw = 0;

		test = 0;
		ring_mod = 0;
		sync = 0;

		msb_rising = false;
	}

	// ----------------------------------------------------------------------------
	// Inline functions.
	// The following functions are defined inline because they are called every
	// time a sample is calculated.
	// ----------------------------------------------------------------------------

	/**
	 * SID clocking - 1 cycle.
	 */
	public void clock() {
		// No operation if test bit is set.
		if (test != 0) {
			return;
		}

		int /* reg24 */accumulator_prev = accumulator;

		// Calculate new accumulator value;
		accumulator += freq;
		accumulator &= 0xffffff;

		// Check whether the MSB is set high. This is used for synchronization.
		msb_rising = !((accumulator_prev & 0x800000) != 0)
				&& ((accumulator & 0x800000) != 0);

		// Shift noise register once for each time accumulator bit 19 is set
		// high.
		if (!((accumulator_prev & 0x080000) != 0)
				&& ((accumulator & 0x080000) != 0)) {
			int /* reg24 */bit0 = ((shift_register >> 22) ^ (shift_register >> 17)) & 0x1;
			shift_register <<= 1;
			shift_register &= 0x7fffff;
			shift_register |= bit0;
		}
	}

	/**
	 * SID clocking - delta_t cycles.
	 */
	public void clock(int/* cycle_count */delta_t) {
		// No operation if test bit is set.
		if (test != 0) {
			return;
		}

		int /* reg24 */accumulator_prev = accumulator;

		// Calculate new accumulator value;
		int /* reg24 */delta_accumulator = delta_t * freq;
		accumulator += delta_accumulator;
		accumulator &= 0xffffff;

		// Check whether the MSB is set high. This is used for synchronization.
		msb_rising = !((accumulator_prev & 0x800000) != 0)
				&& ((accumulator & 0x800000) != 0);

		// Shift noise register once for each time accumulator bit 19 is set
		// high.
		// Bit 19 is set high each time 2^20 (0x100000) is added to the
		// accumulator.
		int /* reg24 */shift_period = 0x100000;

		while (delta_accumulator != 0) {
			if (delta_accumulator < shift_period) {
				shift_period = delta_accumulator;
				// Determine whether bit 19 is set on the last period.
				// NB! Requires two's complement integer.
				if (shift_period <= 0x080000) {
					// Check for flip from 0 to 1.
					if ((((accumulator - shift_period) & 0x080000) != 0)
							|| !((accumulator & 0x080000) != 0)) {
						break;
					}
				} else {
					// Check for flip from 0 (to 1 or via 1 to 0) or from 1 via
					// 0 to 1.
					if ((((accumulator - shift_period) & 0x080000) != 0)
							&& !((accumulator & 0x080000) != 0)) {
						break;
					}
				}
			}

			// Shift the noise/random register.
			// NB! The shift is actually delayed 2 cycles, this is not modeled.
			int /* reg24 */bit0 = ((shift_register >> 22) ^ (shift_register >> 17)) & 0x1;
			shift_register <<= 1;
			shift_register &= 0x7fffff;
			shift_register |= bit0;

			delta_accumulator -= shift_period;
		}
	}

	/**
	 * Synchronize oscillators. This must be done after all the oscillators have
	 * been clock()'ed since the oscillators operate in parallel. Note that the
	 * oscillators must be clocked exactly on the cycle when the MSB is set high
	 * for hard sync to operate correctly. See SID.clock().
	 */
	public void synchronize() {
		// A special case occurs when a sync source is synced itself on the same
		// cycle as when its MSB is set high. In this case the destination will
		// not be synced. This has been verified by sampling OSC3.
		if (msb_rising && (sync_dest.sync != 0)
				&& !((sync != 0) && sync_source.msb_rising)) {
			sync_dest.accumulator = 0;
		}
	}

	// ----------------------------------------------------------------------------
	// 16 possible combinations of waveforms.
	// Output functions.
	// NB! The output from SID 8580 is delayed one cycle compared to SID 6581,
	// this is not modeled.
	// ----------------------------------------------------------------------------

	/**
	 * No waveform: Zero output.
	 * 
	 * @return zero
	 */
	protected int /* reg12 */output____() {
		return 0x000;
	}

	/**
	 * Triangle: The upper 12 bits of the accumulator are used. The MSB is used
	 * to create the falling edge of the triangle by inverting the lower 11
	 * bits. The MSB is thrown away and the lower 11 bits are left-shifted (half
	 * the resolution, full amplitude). Ring modulation substitutes the MSB with
	 * MSB EOR sync_source MSB.
	 * 
	 * @return triangle
	 */
	protected int /* reg12 */output___T() {
		int /* reg24 */msb = ((ring_mod != 0) ? accumulator
				^ sync_source.accumulator : accumulator) & 0x800000;
		return (((msb != 0) ? ~accumulator : accumulator) >> 11) & 0xfff;
	}

	/**
	 * Sawtooth: The output is identical to the upper 12 bits of the
	 * accumulator.
	 * 
	 * @return sawtooth
	 */
	protected int /* reg12 */output__S_() {
		return accumulator >> 12;
	}

	/**
	 * Pulse: The upper 12 bits of the accumulator are used. These bits are
	 * compared to the pulse width register by a 12 bit digital comparator;
	 * output is either all one or all zero bits.
	 * <P>
	 * NB! The output is actually delayed one cycle after the compare. This is
	 * not modeled.
	 * <P>
	 * The test bit, when set to one, holds the pulse waveform output at 0xfff
	 * regardless of the pulse width setting.
	 * 
	 * @return
	 */
	protected int /* reg12 */output_P__() {
		return ((test != 0) || (accumulator >> 12) >= pw) ? 0xfff : 0x000;
	}

	/**
	 * Noise: The noise output is taken from intermediate bits of a 23-bit shift
	 * register which is clocked by bit 19 of the accumulator. NB! The output is
	 * actually delayed 2 cycles after bit 19 is set high. This is not modeled.
	 * <P>
	 * Operation: Calculate EOR result, shift register, set bit 0 = result.
	 * 
	 * <pre>
	 *                         ------------------------&gt;--------------------
	 *                         |                                            |
	 *                    ----EOR----                                       |
	 *                    |         |                                       |
	 *                    2 2 2 1 1 1 1 1 1 1 1 1 1                         |
	 *  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 &lt;---
	 *                    |   |       |     |   |       |     |   |
	 *  OSC3 bits  :      7   6       5     4   3       2     1   0
	 * </pre>
	 * 
	 * Since waveform output is 12 bits the output is left-shifted 4 times.
	 */
	protected int /* reg12 */outputN___() {
		return ((shift_register & 0x400000) >> 11)
				| ((shift_register & 0x100000) >> 10)
				| ((shift_register & 0x010000) >> 7)
				| ((shift_register & 0x002000) >> 5)
				| ((shift_register & 0x000800) >> 4)
				| ((shift_register & 0x000080) >> 1)
				| ((shift_register & 0x000010) << 1)
				| ((shift_register & 0x000004) << 2);
	}

	/**
	 * Combined waveforms: By combining waveforms, the bits of each waveform are
	 * effectively short circuited. A zero bit in one waveform will result in a
	 * zero output bit (thus the infamous claim that the waveforms are AND'ed).
	 * However, a zero bit in one waveform will also affect the neighboring bits
	 * in the output. The reason for this has not been determined.
	 * <P>
	 * Example:
	 * 
	 * <pre>
	 *  
	 *              1 1
	 *  Bit #       1 0 9 8 7 6 5 4 3 2 1 0
	 *              -----------------------
	 *  Sawtooth    0 0 0 1 1 1 1 1 1 0 0 0
	 * 
	 *  Triangle    0 0 1 1 1 1 1 1 0 0 0 0
	 * 
	 *  AND         0 0 0 1 1 1 1 1 0 0 0 0
	 * 
	 *  Output      0 0 0 0 1 1 1 0 0 0 0 0
	 * </pre>
	 * 
	 * This behavior would be quite difficult to model exactly, since the SID in
	 * this case does not act as a digital state machine. Tests show that minor
	 * (1 bit) differences can actually occur in the output from otherwise
	 * identical samples from OSC3 when waveforms are combined. To further
	 * complicate the situation the output changes slightly with time (more
	 * neighboring bits are successively set) when the 12-bit waveform registers
	 * are kept unchanged.
	 * <P>
	 * It is probably possible to come up with a valid model for the behavior,
	 * however this would be far too slow for practical use since it would have
	 * to be based on the mutual influence of individual bits.
	 * <P>
	 * The output is instead approximated by using the upper bits of the
	 * accumulator as an index to look up the combined output in a table
	 * containing actual combined waveform samples from OSC3. These samples are
	 * 8 bit, so 4 bits of waveform resolution is lost. All OSC3 samples are
	 * taken with FREQ=0x1000, adding a 1 to the upper 12 bits of the
	 * accumulator each cycle for a sample period of 4096 cycles.
	 * <P>
	 * Sawtooth+Triangle: The sawtooth output is used to look up an OSC3 sample.
	 * <P>
	 * Pulse+Triangle: The triangle output is right-shifted and used to look up
	 * an OSC3 sample. The sample is output if the pulse output is on. The
	 * reason for using the triangle output as the index is to handle ring
	 * modulation. Only the first half of the sample is used, which should be OK
	 * since the triangle waveform has half the resolution of the accumulator.
	 * <P>
	 * Pulse+Sawtooth: The sawtooth output is used to look up an OSC3 sample.
	 * The sample is output if the pulse output is on.
	 * <P>
	 * Pulse+Sawtooth+Triangle: The sawtooth output is used to look up an OSC3
	 * sample. The sample is output if the pulse output is on.
	 */
	protected int /* reg12 */output__ST() {
		return wave__ST[output__S_()] << 4;
	}

	/**
	 * Combined waveforms: By combining waveforms, the bits of each waveform are
	 * effectively short circuited. A zero bit in one waveform will result in a
	 * zero output bit (thus the infamous claim that the waveforms are AND'ed).
	 * However, a zero bit in one waveform will also affect the neighboring bits
	 * in the output. The reason for this has not been determined.
	 * <P>
	 * Example:
	 * 
	 * <pre>
	 *  
	 *              1 1
	 *  Bit #       1 0 9 8 7 6 5 4 3 2 1 0
	 *              -----------------------
	 *  Sawtooth    0 0 0 1 1 1 1 1 1 0 0 0
	 * 
	 *  Triangle    0 0 1 1 1 1 1 1 0 0 0 0
	 * 
	 *  AND         0 0 0 1 1 1 1 1 0 0 0 0
	 * 
	 *  Output      0 0 0 0 1 1 1 0 0 0 0 0
	 * </pre>
	 * 
	 * This behavior would be quite difficult to model exactly, since the SID in
	 * this case does not act as a digital state machine. Tests show that minor
	 * (1 bit) differences can actually occur in the output from otherwise
	 * identical samples from OSC3 when waveforms are combined. To further
	 * complicate the situation the output changes slightly with time (more
	 * neighboring bits are successively set) when the 12-bit waveform registers
	 * are kept unchanged.
	 * <P>
	 * It is probably possible to come up with a valid model for the behavior,
	 * however this would be far too slow for practical use since it would have
	 * to be based on the mutual influence of individual bits.
	 * <P>
	 * The output is instead approximated by using the upper bits of the
	 * accumulator as an index to look up the combined output in a table
	 * containing actual combined waveform samples from OSC3. These samples are
	 * 8 bit, so 4 bits of waveform resolution is lost. All OSC3 samples are
	 * taken with FREQ=0x1000, adding a 1 to the upper 12 bits of the
	 * accumulator each cycle for a sample period of 4096 cycles.
	 * <P>
	 * Sawtooth+Triangle: The sawtooth output is used to look up an OSC3 sample.
	 * <P>
	 * Pulse+Triangle: The triangle output is right-shifted and used to look up
	 * an OSC3 sample. The sample is output if the pulse output is on. The
	 * reason for using the triangle output as the index is to handle ring
	 * modulation. Only the first half of the sample is used, which should be OK
	 * since the triangle waveform has half the resolution of the accumulator.
	 * <P>
	 * Pulse+Sawtooth: The sawtooth output is used to look up an OSC3 sample.
	 * The sample is output if the pulse output is on.
	 * <P>
	 * Pulse+Sawtooth+Triangle: The sawtooth output is used to look up an OSC3
	 * sample. The sample is output if the pulse output is on.
	 */
	protected int /* reg12 */output_P_T() {
		return (wave_P_T[output___T() >> 1] << 4) & output_P__();
	}

	/**
	 * Combined waveforms: By combining waveforms, the bits of each waveform are
	 * effectively short circuited. A zero bit in one waveform will result in a
	 * zero output bit (thus the infamous claim that the waveforms are AND'ed).
	 * However, a zero bit in one waveform will also affect the neighboring bits
	 * in the output. The reason for this has not been determined.
	 * <P>
	 * Example:
	 * 
	 * <pre>
	 *  
	 *              1 1
	 *  Bit #       1 0 9 8 7 6 5 4 3 2 1 0
	 *              -----------------------
	 *  Sawtooth    0 0 0 1 1 1 1 1 1 0 0 0
	 * 
	 *  Triangle    0 0 1 1 1 1 1 1 0 0 0 0
	 * 
	 *  AND         0 0 0 1 1 1 1 1 0 0 0 0
	 * 
	 *  Output      0 0 0 0 1 1 1 0 0 0 0 0
	 * </pre>
	 * 
	 * This behavior would be quite difficult to model exactly, since the SID in
	 * this case does not act as a digital state machine. Tests show that minor
	 * (1 bit) differences can actually occur in the output from otherwise
	 * identical samples from OSC3 when waveforms are combined. To further
	 * complicate the situation the output changes slightly with time (more
	 * neighboring bits are successively set) when the 12-bit waveform registers
	 * are kept unchanged.
	 * <P>
	 * It is probably possible to come up with a valid model for the behavior,
	 * however this would be far too slow for practical use since it would have
	 * to be based on the mutual influence of individual bits.
	 * <P>
	 * The output is instead approximated by using the upper bits of the
	 * accumulator as an index to look up the combined output in a table
	 * containing actual combined waveform samples from OSC3. These samples are
	 * 8 bit, so 4 bits of waveform resolution is lost. All OSC3 samples are
	 * taken with FREQ=0x1000, adding a 1 to the upper 12 bits of the
	 * accumulator each cycle for a sample period of 4096 cycles.
	 * <P>
	 * Sawtooth+Triangle: The sawtooth output is used to look up an OSC3 sample.
	 * <P>
	 * Pulse+Triangle: The triangle output is right-shifted and used to look up
	 * an OSC3 sample. The sample is output if the pulse output is on. The
	 * reason for using the triangle output as the index is to handle ring
	 * modulation. Only the first half of the sample is used, which should be OK
	 * since the triangle waveform has half the resolution of the accumulator.
	 * <P>
	 * Pulse+Sawtooth: The sawtooth output is used to look up an OSC3 sample.
	 * The sample is output if the pulse output is on.
	 * <P>
	 * Pulse+Sawtooth+Triangle: The sawtooth output is used to look up an OSC3
	 * sample. The sample is output if the pulse output is on.
	 */
	protected int /* reg12 */output_PS_() {
		return (wave_PS_[output__S_()] << 4) & output_P__();
	}

	/**
	 * Combined waveforms: By combining waveforms, the bits of each waveform are
	 * effectively short circuited. A zero bit in one waveform will result in a
	 * zero output bit (thus the infamous claim that the waveforms are AND'ed).
	 * However, a zero bit in one waveform will also affect the neighboring bits
	 * in the output. The reason for this has not been determined.
	 * <P>
	 * Example:
	 * 
	 * <pre>
	 *  
	 *              1 1
	 *  Bit #       1 0 9 8 7 6 5 4 3 2 1 0
	 *              -----------------------
	 *  Sawtooth    0 0 0 1 1 1 1 1 1 0 0 0
	 * 
	 *  Triangle    0 0 1 1 1 1 1 1 0 0 0 0
	 * 
	 *  AND         0 0 0 1 1 1 1 1 0 0 0 0
	 * 
	 *  Output      0 0 0 0 1 1 1 0 0 0 0 0
	 * </pre>
	 * 
	 * This behavior would be quite difficult to model exactly, since the SID in
	 * this case does not act as a digital state machine. Tests show that minor
	 * (1 bit) differences can actually occur in the output from otherwise
	 * identical samples from OSC3 when waveforms are combined. To further
	 * complicate the situation the output changes slightly with time (more
	 * neighboring bits are successively set) when the 12-bit waveform registers
	 * are kept unchanged.
	 * <P>
	 * It is probably possible to come up with a valid model for the behavior,
	 * however this would be far too slow for practical use since it would have
	 * to be based on the mutual influence of individual bits.
	 * <P>
	 * The output is instead approximated by using the upper bits of the
	 * accumulator as an index to look up the combined output in a table
	 * containing actual combined waveform samples from OSC3. These samples are
	 * 8 bit, so 4 bits of waveform resolution is lost. All OSC3 samples are
	 * taken with FREQ=0x1000, adding a 1 to the upper 12 bits of the
	 * accumulator each cycle for a sample period of 4096 cycles.
	 * <P>
	 * Sawtooth+Triangle: The sawtooth output is used to look up an OSC3 sample.
	 * <P>
	 * Pulse+Triangle: The triangle output is right-shifted and used to look up
	 * an OSC3 sample. The sample is output if the pulse output is on. The
	 * reason for using the triangle output as the index is to handle ring
	 * modulation. Only the first half of the sample is used, which should be OK
	 * since the triangle waveform has half the resolution of the accumulator.
	 * <P>
	 * Pulse+Sawtooth: The sawtooth output is used to look up an OSC3 sample.
	 * The sample is output if the pulse output is on.
	 * <P>
	 * Pulse+Sawtooth+Triangle: The sawtooth output is used to look up an OSC3
	 * sample. The sample is output if the pulse output is on.
	 */
	protected int /* reg12 */output_PST() {
		return (wave_PST[output__S_()] << 4) & output_P__();
	}

	/**
	 * Combined waveforms including noise: All waveform combinations including
	 * noise output zero after a few cycles.
	 * <P>
	 * NB! The effects of such combinations are not fully explored. It is
	 * claimed that the shift register may be filled with zeroes and locked up,
	 * which seems to be true.
	 * <P>
	 * We have not attempted to model this behavior, suffice to say that there
	 * is very little audible output from waveform combinations including noise.
	 * We hope that nobody is actually using it.
	 */
	protected int /* reg12 */outputN__T() {
		return 0;
	}

	/**
	 * Combined waveforms including noise: All waveform combinations including
	 * noise output zero after a few cycles.
	 * <P>
	 * NB! The effects of such combinations are not fully explored. It is
	 * claimed that the shift register may be filled with zeroes and locked up,
	 * which seems to be true.
	 * <P>
	 * We have not attempted to model this behavior, suffice to say that there
	 * is very little audible output from waveform combinations including noise.
	 * We hope that nobody is actually using it.
	 */
	protected int /* reg12 */outputN_S_() {
		return 0;
	}

	/**
	 * Combined waveforms including noise: All waveform combinations including
	 * noise output zero after a few cycles.
	 * <P>
	 * NB! The effects of such combinations are not fully explored. It is
	 * claimed that the shift register may be filled with zeroes and locked up,
	 * which seems to be true.
	 * <P>
	 * We have not attempted to model this behavior, suffice to say that there
	 * is very little audible output from waveform combinations including noise.
	 * We hope that nobody is actually using it.
	 */
	protected int /* reg12 */outputN_ST() {
		return 0;
	}

	/**
	 * Combined waveforms including noise: All waveform combinations including
	 * noise output zero after a few cycles.
	 * <P>
	 * NB! The effects of such combinations are not fully explored. It is
	 * claimed that the shift register may be filled with zeroes and locked up,
	 * which seems to be true.
	 * <P>
	 * We have not attempted to model this behavior, suffice to say that there
	 * is very little audible output from waveform combinations including noise.
	 * We hope that nobody is actually using it.
	 */
	protected int /* reg12 */outputNP__() {
		return 0;
	}

	/**
	 * Combined waveforms including noise: All waveform combinations including
	 * noise output zero after a few cycles.
	 * <P>
	 * NB! The effects of such combinations are not fully explored. It is
	 * claimed that the shift register may be filled with zeroes and locked up,
	 * which seems to be true.
	 * <P>
	 * We have not attempted to model this behavior, suffice to say that there
	 * is very little audible output from waveform combinations including noise.
	 * We hope that nobody is actually using it.
	 */
	protected int /* reg12 */outputNP_T() {
		return 0;
	}

	/**
	 * Combined waveforms including noise: All waveform combinations including
	 * noise output zero after a few cycles.
	 * <P>
	 * NB! The effects of such combinations are not fully explored. It is
	 * claimed that the shift register may be filled with zeroes and locked up,
	 * which seems to be true.
	 * <P>
	 * We have not attempted to model this behavior, suffice to say that there
	 * is very little audible output from waveform combinations including noise.
	 * We hope that nobody is actually using it.
	 */
	protected int /* reg12 */outputNPS_() {
		return 0;
	}

	/**
	 * Combined waveforms including noise: All waveform combinations including
	 * noise output zero after a few cycles.
	 * <P>
	 * NB! The effects of such combinations are not fully explored. It is
	 * claimed that the shift register may be filled with zeroes and locked up,
	 * which seems to be true.
	 * <P>
	 * We have not attempted to model this behavior, suffice to say that there
	 * is very little audible output from waveform combinations including noise.
	 * We hope that nobody is actually using it.
	 */
	protected int /* reg12 */outputNPST() {
		return 0;
	}

	/**
	 * 12-bit waveform output. Select one of 16 possible combinations of
	 * waveforms.
	 * 
	 * @return
	 */
	public int /* reg12 */output() {
		// It may seem cleaner to use an array of member functions to return
		// waveform output; however a switch with inline functions is faster.

		switch (waveform) {
		default:
		case 0x0:
			return output____();
		case 0x1:
			return output___T();
		case 0x2:
			return output__S_();
		case 0x3:
			return output__ST();
		case 0x4:
			return output_P__();
		case 0x5:
			return output_P_T();
		case 0x6:
			return output_PS_();
		case 0x7:
			return output_PST();
		case 0x8:
			return outputN___();
		case 0x9:
			return outputN__T();
		case 0xa:
			return outputN_S_();
		case 0xb:
			return outputN_ST();
		case 0xc:
			return outputNP__();
		case 0xd:
			return outputNP_T();
		case 0xe:
			return outputNPS_();
		case 0xf:
			return outputNPST();
		}
	}

	// ----------------------------------------------------------------------------
	// END Inline functions.
	// ----------------------------------------------------------------------------

}