summaryrefslogtreecommitdiffstatshomepage
path: root/src/mess/audio/upd1771.c
blob: ad07d815a985f38388dab44a5b62f0774800244b (plain) (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
/**********************************************************************

    NEC uPD1771-017 as used in the Epoch Super Cassette Vision (SCV)

    Made using recording/analysis on a Yeno (PAL Super Cassete Vision)
    by plgDavid

    Full markings on my 2 specimens are
    "NEC JAPAN 8431K9 D1771C 017" (31st week of 1984, mask rom #017)

    I've since (October 2012) got a Grandstand Firefox F-7 Handheld game
    (AKA Epoch GalagaX6/Epoch Astro Thunder 7/Tandy Astro Thunder),
    (http://www.handheldmuseum.com/Grandstand/Firefox.htm)
    which includes a
    "NEC JAPAN 8319K9 D1771C 011" (19th week of 1983, mask rom #011)
    Thanks to user 'Blanka' from Dragonslairfans for the nice catch!
    (http://www.dragonslairfans.com/smfor/index.php?topic=3061.0)

    Since the chip generates tones using ROM wavetables,
    it is perfectly possible to generate other sounds with different rom code and data.

    Most upd17XXX devices are typically 4bit NEC MCUs, however based on information
    in in "Electronic Speech Synthesis" by Geoff Bristow (ISBN 0-07-007912-9, pages 148-152)
    the upd1770/1771 is not one of these 4-bit ones.

    The uPD1770/uPD1771 SSM is a 16-bit-wide rom/ram mcu with 8kb (4kw) of rom code,
    64 bytes of ram (16x16bit words addressable as 16 or 2x8 bits each, the
    remaining 32 bytes acting as an 8-level stack), 182 instructions, a complex
    noise and tone internal interrupt system, external interrupts,
    and two 8-bit ports with multiple modes allowing for chips to operate as master
    or slave devices.
    SSM stands for "Sound Synthesis Microcomputer".

    People who I *THINK* worked on the uPD1771 and what part I think they worked on:
    Toshio Oura - Project Lead(?), VSRSSS/TSRSSS speech synthesis engine (on upd1776C), master/slave i/o controls, author of bristow article and primary author of the IEEE article
    Hatsuhide Igarashi - Clock oscillator and pad layout, coauthor on the IEEE article, other IEEE stuff
    Tomoaki Isozaki - ? (senior NEC engineer?), coauthor on the IEEE article
    Sachiyuki Toufuku - ?, coauthor on the IEEE article
    Tojiro Mukawa - IGFETs and the DAC
    M. Sakai ? - digital filtering for VSRSSS? (IEEE 4131979, 1169295)
    M. Endo ? - digital design system or speech synthesis? (IEEE 4069656, another? person: IEEE 150330, 225838)
    H. Aoyama ? - logic design system used to assemble/lay out the chip? (IEEE 1585393)
    I. Fujitaka ? (no IEEE)
    Eiji Sugimoto - cpu design? 1156033 1155824
    F. Tsukuda ? (no IEEE)
    N. Miyake ? switched capacitor stuff? (IEEE nnnnnn)


    The uPD1771 internal workings are described to some extent by the Bristow book
    and the IEEE article "A Single-Chip Sound Synthesis Microcomputer" which complements the book
    and are covered by at least four US patents:
    4184152 - on IGFET-based DAC stuff
    4488061 - on the IGFET-based drive circuit part of the DAC.
    4408094 - covers the 3 pin DAC with the volume control/vref pin. Not all that interesting,
              except it might describe to some extent how the (9->5bit?) PWM works in the text.
    4470113 - covers the multiplexed PB0/1/2/3 pins and their use as /CS /WR /RD and ALE
              note as I have marked the pins below I assume the final pins connected
              to /CS /WR /RD and /ALE are PB7,6,5,4 but this is just a guess of mine:
              The actual order may well match the patent.
    4577343 - covers the VSRSSS implementation as discussed in the Bristow book.
              This patent has an internal diagram of the workings of the chips and
              a limited description of how many registers etc it has.
    4805508 - on the operation of the tone divider register and correction for accurate period when
              the tone interrupt frequency is not perfectly divisible from the clock.
    These next two may not be specific to the 1771 or even related at all!
    4321562 - on a self-adjusting circuit for internal coupling to the clock crystal inputs.
              This may be a generic NEC invention and probably isn't limited to the upd1771.
    4656491 - on a new method of distributing resistors and transistors on anti-ESD pin buffers
              This may be a generic NEC invention and probably isn't limited to the upd1771.


    Based on the 4577343 patent mostly, plus the bristow and IEEE article:
    * these are the registers:
    8bits:
     AH, AL (forming the 16-bit A' accumulator),
     B, C (a pair of general purpose registers),
    4bits (may be technically part of ALU):
     H -> points to one of the 16 words of ram
    1bit:
     L -> selector of left or right half of the ram word
    ?bits:
     D (having to do with the DAC)
     N (3 bits? having to do with the pseudorandom noise interrupt, namely setting the clock divider ratio for the PRNG clock vs cpu clock)
     MODE (5 or more bits? enabling/disabling/acking the noise interrupt, and the tone interrupts (there are four!))
     SP (the stack pointer, probably 5 bits, points to the stack ram; may encompass H and L as above!)
     FLO: unsure. quite possibly 'flag overflow' used for branching. there likely exists other flags as well...
     ODF: 'output data flag?', selects which half of a selected ram word is output to the dac not really sure of this?


    Mask roms known:
    uPD1776C: mentioned in the bristow book, implements VSRSSS speech concatenation
              (see US Patent 4577343 which is a patent on this VSRSSS implementation)
    uPD1771C-006: used in NEC APC for sound as the "MPU"
            -011: used on Firefox F-4 handheld
            -015: unknown, known to exist from part scalper sites only.
            -017: used on Epoch Super Cassete Vision for sound; This audio driver HLEs that part only.

     Used pinout in the SCV:

     NC           1        28        NC
     NC           2        27        NC
     NC           3        26        ACK
    !WR           4        25        D7
    !CS           5        24        D6
     RESET        6        23        D5
     NC           7        22        D4
     VCC          8        21        D3
     6Mhz XIN     9        20        D2
     6Mhz XOUT   10        19        D1
     AUDOUT      11        18        D0
     NC          12        17        GND
     AUDOUT(inv) 13        16        VCC
     GND         14        15        ? tied to pin 16 (VCC) through a resistor (pullup?)

     Pinout based on guesses and information in "Electronic Speech Synthesis" by Geoff Bristow
     (ISBN 0-07-007912-9, pages 148-152), and the data on page 233 of the Nec APC technical manual at
     http://bitsavers.trailing-edge.com/pdf/nec/APC/819-000100-1003_APC_System_Reference_Guide_Apr83.pdf
     [x] is unsure:
     PB3          1        28        PB2
     PB4(/ALE)    2        27        PB1
     PB5(/RD)     3        26        PB0
     PB6(/WR)     4        25        D7(PA7)
     PB7(/CS)     5        24        D6(PA6)
     /RESET       6        23        D5(PA5)
     [TEST?]      7        22        D4(PA4)
     VCC          8        21        D3(PA3)
     XI(CLK)      9        20        D2(PA2)
     XO          10        19        D1(PA1)
     D/A OUT +   11        18        D0(PA0)
     [D/A VREF]  12        17        CH2
     D/A OUT -   13        16        /EXTINT
     GND         14        15        CH1 tied to pin 16 (VCC) through a resistor, on APC to VCC thru a 12k resistor and thru a 10uf cap to gnd

    CH1 and CH2 are some sort of mode selects?

    In the SCV (info from plgDavid):
    pin  5 is tied to the !SCPU pin on the Epoch TV chip pin 29 (0x3600 writes)
    pin  6 is tied to the   PC3 pin of the upD7801 CPU
    pin 26 is tied to the  INT1 pin of the upD7801 (CPU pin 12),

    1,2,3,28,27 dont generate any digital signals
    6 seems to be lowered 2.5 ms before an audio write
    7  is always low.
    12 is always high

    (NOTE: the photomicrograph in the bristow book makes it fairly clear due to
    pad thicknessess that the real VCC is pin 8 and the real GND is pin 14.
    The function of pin 7 is unknown.

    Pins 11 and 13 go to a special circuit, which according to kevtris's analysis
    of my schematics, consist of a balanced output (not unlike XLR cables),
    which are then combined together then sent to the RF box.
    (The bristow book explains that there are two DAC pins and one DAC
    VREF/volume pin. The dac+ and dac- are pins 11 and 13, and based on the
    photomicrograph it looks like dac vref is probably pin 12)

    HLE:
    All writes are made through address 0x3600 on the upD7801
    Instead of using register=value, this chip require sending multiple
    bytes for each command, one after the other.

**********************************************************************/

#include "emu.h"
#include "audio/upd1771.h"


#define LOG 0

/*
  Each of the 8 waveforms have been sampled at 192kHz using period 0xFF,
  filtered, and each of the 32 levels have been calculated with averages on around 10 samples
  (removing the transition samples) then quantized to int8_t's.
  We are not clear on the exact DAC details yet, especially with regards to volume changes.

  External AC coupling is assumed in the use of this DAC, so we will center the 8bit data using a signed container
*/
const char WAVEFORMS[8][32]={
{ -5,   -5,  -5,-117,-116, -53, -10, 127, 120, 108,  97, -121,-121,-121,  -4,  -4,  -4,  -4,  -4,  -4,  -4,  -4,  -4,  -4,-119,-119,-118,  -2,  -2,  -2,  -2,  -2},
{  6,  -21,  -1, -41,  -1,  25, -35, -35,  -1, -16,  34,   29, -37, -30, -33, -20,  38, -15,  50, -20, -20, -15,   7, -20,  77, -15, -37,  69,  93, -21, -38, -37},
{ -11,  -4, -11,  51,  -9, -11, -11,  84,  87,-112,  44,  102, -86,-112,  35, 103, -12,  51, -10, -12, -12,  -9, -12,  13, -11, -44,  25, 103, -12,  -5, -90,-101},
{  40,  98,  31,  98,  -1,  13,  58,   3, -18,  45,  -5,  -13,  -5, -13,  -5, -13,  -5, -13,  -5, -13, -10, -15,-121,   5, -17,  45,-128,   8, -16, -12, -16,  -9},
{ -53,-101,-121,-128,-113, -77, -34,   5,  26,  63,  97,  117, 119, 119, 115,  99,  54,  13, -13, -11,  -2,   3,  31,  52,  62,  74,  60,  51,  38,  22,   8, -14},
{ -86,-128, -60,   3,  65, 101, 119,  44,  37,  41,  51,   53,  55,  58,  58,  29, -12,  74,  82,  77,  59, 113,  52,  21,  24,  34,  39,  45,  48,  48,  48, -13},
{ -15, -18, -46, -67, -95,-111,-117,-124,-128,-123,-116, -105, -89, -72, -50, -21,   2,  16,  46,  76,  95, 111, 118, 119, 119, 119, 117, 110,  97,  75,  47,  18},
{ -84,-121,-128,-105, -51,   7,  38,  66,  93,  97,  93,   88,  89,  96, 102, 111, 116, 118, 118, 119, 118, 118, 117, 117, 118, 118, 117, 117, 117, 115,  85, -14}
};


#define NOISE_SIZE 255


static unsigned char noise_tbl[]=
{
	0x1c,0x86,0x8a,0x8f,0x98,0xa1,0xad,0xbe,0xd9,0x8a,0x66,0x4d,0x40,0x33,0x2b,0x23,
	0x1e,0x8a,0x90,0x97,0xa4,0xae,0xb8,0xd6,0xec,0xe9,0x69,0x4a,0x3e,0x34,0x2d,0x27,
	0x24,0x24,0x89,0x8e,0x93,0x9c,0xa5,0xb0,0xc1,0xdd,0x40,0x36,0x30,0x29,0x27,0x24,
	0x8b,0x90,0x96,0x9e,0xa7,0xb3,0xc4,0xe1,0x25,0x21,0x8a,0x8f,0x93,0x9d,0xa5,0xb2,
	0xc2,0xdd,0xdd,0x98,0xa2,0xaf,0xbf,0xd8,0xfd,0x65,0x4a,0x3c,0x31,0x2b,0x24,0x22,
	0x1e,0x87,0x8c,0x91,0x9a,0xa3,0xaf,0xc0,0xdb,0xbe,0xd9,0x8c,0x66,0x4d,0x40,0x34,
	0x2c,0x24,0x1f,0x88,0x90,0x9a,0xa4,0xb2,0xc2,0xda,0xff,0x67,0x4d,0x3d,0x34,0x2d,
	0x26,0x24,0x20,0x89,0x8e,0x93,0x9c,0xa5,0xb1,0xc2,0xde,0xc1,0xda,0xff,0x67,0x4d,
	0x3d,0x33,0x2d,0x26,0x24,0x20,0x89,0x8e,0x93,0x9c,0xa5,0xb1,0xc2,0xdd,0xa3,0xb0,
	0xc0,0xd9,0xfe,0x66,0x4b,0x3c,0x32,0x2b,0x24,0x23,0x1e,0x88,0x8d,0x92,0x9b,0xa4,
	0xb0,0xc1,0xdc,0xad,0xbe,0xda,0x22,0x20,0x1c,0x85,0x8a,0x8f,0x98,0xa1,0xad,0xbe,
	0xda,0x20,0x1b,0x85,0x8d,0x97,0xa1,0xaf,0xbf,0xd8,0xfd,0x64,0x49,0x3a,0x30,0x2a,
	0x23,0x21,0x1d,0x86,0x8b,0x91,0x9a,0xa2,0xae,0xc0,0xdb,0x33,0x2b,0x24,0x1f,0x88,
	0x90,0x9a,0xa4,0xb2,0xc2,0xda,0xff,0x67,0x4c,0x3e,0x33,0x2d,0x25,0x24,0x1f,0x89,
	0x8e,0x93,0x9c,0xa5,0xb1,0xc2,0xde,0x85,0x8e,0x98,0xa2,0xb0,0xc0,0xd9,0xfe,0x64,
	0x4b,0x3b,0x31,0x2a,0x23,0x22,0x1e,0x88,0x8c,0x91,0x9b,0xa3,0xaf,0xc1,0xdc,0xdc
};



#define STATE_SILENCE 0
#define STATE_NOISE   1
#define STATE_TONE    2
#define STATE_ADPCM   3


const device_type UPD1771C = &device_creator<upd1771c_device>;


upd1771c_device::upd1771c_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
				: device_t(mconfig, UPD1771C, "NEC uPD1771C 017", tag, owner, clock),
					device_sound_interface(mconfig, *this)
{
}


//-------------------------------------------------
//  device_config_complete - perform any
//  operations now that the configuration is
//  complete
//-------------------------------------------------

void upd1771c_device::device_config_complete()
{
	// inherit a copy of the static data
	const upd1771_interface *intf = reinterpret_cast<const upd1771_interface *>(static_config());
	if (intf != NULL)
		*static_cast<upd1771_interface *>(this) = *intf;

	// or initialize to defaults if none provided
	else
	{
		memset(&m_ack_callback, 0, sizeof(m_ack_callback));
	}
}

//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void upd1771c_device::device_start()
{
	/* resolve callbacks */
	m_ack_out_func.resolve(m_ack_callback, *this);

	m_timer = machine().scheduler().timer_alloc(timer_expired_delegate(FUNC(upd1771c_device::ack_callback),this));

	m_channel = machine().sound().stream_alloc(*this, 0, 1, clock() / 4, this);

	save_item(NAME(m_packet));
	save_item(NAME(m_index));
	save_item(NAME(m_expected_bytes));
	save_item(NAME(m_state));
	save_item(NAME(m_pc3));
	save_item(NAME(m_t_timbre));
	save_item(NAME(m_t_offset));
	save_item(NAME(m_t_period));
	save_item(NAME(m_t_volume));
	save_item(NAME(m_t_tpos));
	save_item(NAME(m_t_ppos));
	save_item(NAME(m_nw_timbre));
	save_item(NAME(m_nw_volume));
	save_item(NAME(m_nw_period));
	save_item(NAME(m_nw_tpos));
	save_item(NAME(m_nw_ppos));
	save_item(NAME(m_n_value));
	save_item(NAME(m_n_volume));
	save_item(NAME(m_n_period));
	save_item(NAME(m_n_ppos));
}

//-------------------------------------------------
//  device_reset - device-specific reset
//-------------------------------------------------

void upd1771c_device::device_reset()
{
	m_index = 0;
	m_expected_bytes = 0;
	m_pc3 = 0;
}



READ8_MEMBER( upd1771c_device::read )
{
	return 0x80; // TODO
}

/*
*************TONE*****************
Tone consists of a wavetable playback mechanism.
Each wavetable is a looping period of 32 samples but can be played with an offset from any point in the table
effectively shrinking the sample loop, thus allowing different pitch "macros ranges" to be played.
This method is rather crude because the spectrum of the sound get heavily altered...
unless that was the intent.

Tone Write (4 bytes):

Byte0: 0x02

Byte1: 0bTTTOOOOO
  MSB 3 bits of Timbre (8 wavetables)
  LSB 5 bits offset in the table.

Byte2: 0bPPPPPPPP
  8bits of clock divider/period
  Anything under <= 0x20 give the same value

Byte3: 0b???VVVVV
   MSB 3 bits unknown
   LSB 5 bits of "Volume"

   Note: volume is not a volume in a normal sense but some kind
   of bit cropping/rounding.
*/


/*
*************NOISE*****************
Noise consists on 4 different components
A weird Wavetable LFSR (for lack of a better term),
and three independent (but equal) low frequency
50/50 pulse wavs.

The 4 components are mixed in a mysterious way,
a weird ORing with volume having a huge effect.

Byte0: 0x01

Byte1: 0bTTTOOOOO
  MSB 3 bits of LFSR Timbre (8 wavetables)
  LSB 5 bits ?????????

Byte2: 0bPPPPPPPP
  8bits of clock divider/period

Byte3: 0b???VVVVV
   MSB 3 bits unknown
   LSB 5 bits of "Volume"


Byte4: 0bPPPPPPPP  Low Freq0 period(if not 0 this periodically resets the  Wavetable LFSR)
Byte5: 0bPPPPPPPP  Low Freq1 period(if not 0 this periodically resets the  Wavetable LFSR)
Byte6: 0bPPPPPPPP  Low Freq2 period(if not 0 this periodically resets the  Wavetable LFSR)

Byte7: 0b???VVVVV  Low Freq0 volume
Byte8: 0b???VVVVV  Low Freq1 volume
Byte9: 0b???VVVVV  Low Freq2 volume
*/

WRITE8_MEMBER( upd1771c_device::write )
{
	//if (LOG)
	//  logerror( "upd1771_w: received byte 0x%02x\n", data );

	m_ack_out_func(0);

	if (m_index < MAX_PACKET_SIZE)
		m_packet[m_index++] = data;
	else
	{
		logerror("upd1771_w: received byte 0x%02x overload!\n", data);
		return;
	}

	switch (m_packet[0])
	{
		case 0:
			m_state = STATE_SILENCE;
			m_index = 0;
			//logerror( "upd1771_w: ----------------silence  state reset\n");
			break;

		case 1:
			if (m_index == 10)
			{
				m_state = STATE_NOISE;
				m_index = 0;

				m_nw_timbre = (m_packet[1] & 0xe0) >> 5;
				m_nw_period = ((UINT32)m_packet[2] + 1) << 7;
				m_nw_volume = m_packet[3] & 0x1f;

				//very long clocked periods.. used for engine drones
				m_n_period[0] = (((UINT32)m_packet[4]) + 1) << 7;
				m_n_period[1] = (((UINT32)m_packet[5]) + 1) << 7;
				m_n_period[2] = (((UINT32)m_packet[6]) + 1) << 7;

				m_n_volume[0] = m_packet[7] & 0x1f;
				m_n_volume[1] = m_packet[8] & 0x1f;
				m_n_volume[2] = m_packet[9] & 0x1f;

				//logerror( "upd1771_w: ----------------noise state reset\n");
			}
			else
				m_timer->adjust(attotime::from_ticks(512, clock()));
			break;

		case 2:
			if (m_index == 4)
			{
				//logerror( "upd1771_w: ----------------tone  state reset\n");
				m_t_timbre = (m_packet[1] & 0xe0) >> 5;
				m_t_offset = (m_packet[1] & 0x1f);
				m_t_period = m_packet[2];
				//smaller periods dont all equal to 0x20
				if (m_t_period < 0x20)
					m_t_period = 0x20;

				m_t_volume =  m_packet[3] & 0x1f;
				m_state = STATE_TONE;
				m_index = 0;
			}
			else
				m_timer->adjust(attotime::from_ticks(512, clock()));
			break;

		case 0x1f:
			//6Khz(ish) DIGI playback

			//end capture
			if (m_index >= 2 && m_packet[m_index - 2] == 0xfe && m_packet[m_index - 1] == 0x00)
			{
				//TODO play capture!
				m_index = 0;
				m_packet[0] = 0;
				m_state = STATE_ADPCM;
			}
			else
				m_timer->adjust(attotime::from_ticks(512, clock()));
			break;

		//garbage: wipe stack
		default:
			m_state = STATE_SILENCE;
			m_index = 0;
			break;
	}
}


WRITE_LINE_MEMBER( upd1771c_device::pcm_write )
{
	//RESET upon HIGH
	if (state != m_pc3)
	{
		logerror("upd1771_pc3 change!: state = %d\n", state);
		m_index = 0;
		m_packet[0] = 0;
	}

	m_pc3 = state;
}


TIMER_CALLBACK_MEMBER( upd1771c_device::ack_callback )
{
	m_ack_out_func(1);
}


//-------------------------------------------------
//  sound_stream_update - handle a stream update
//-------------------------------------------------

void upd1771c_device::sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
{
	stream_sample_t *buffer = outputs[0];

	switch (m_state)
	{
		case STATE_TONE:
			//logerror( "upd1771_STATE_TONE samps:%d %d %d %d %d %d\n",(int)samples,
			//    (int)m_t_timbre,(int)m_t_offset,(int)m_t_volume,(int)m_t_period,(int)m_t_tpos);

			while (--samples >= 0)
			{
				*buffer++ = (WAVEFORMS[m_t_timbre][m_t_tpos]) * m_t_volume * 2;

				m_t_ppos++;
				if (m_t_ppos >= m_t_period)
				{
					m_t_tpos++;
					if (m_t_tpos == 32)
						m_t_tpos = m_t_offset;

					m_t_ppos = 0;
				}
			}
			break;

		case STATE_NOISE:
			while (--samples >= 0)
			{
				*buffer = 0;

				//"wavetable-LFSR" component
				int wlfsr_val = ((int)noise_tbl[m_nw_tpos]) - 127;//data too wide

				m_nw_ppos++;
				if (m_nw_ppos >= m_nw_period)
				{
					m_nw_tpos++;
					if (m_nw_tpos == NOISE_SIZE)
						m_nw_tpos = 0;
					m_nw_ppos = 0;
				}

				//mix in each of the noise's 3 pulse components
				char res[3];
				for (int i = 0; i < 3; ++i)
				{
					res[i] = m_n_value[i] * 127;
					m_n_ppos[i]++;
					if (m_n_ppos[i] >= m_n_period[i])
					{
						m_n_ppos[i] = 0;
						m_n_value[i] = !m_n_value[i];
					}
				}
				//not quite, but close.
				*buffer+= (
							(wlfsr_val * m_nw_volume) |
							(res[0] * m_n_volume[0]) |
							(res[1] * m_n_volume[1]) |
							(res[2] * m_n_volume[2])
							) ;

				buffer++;
			}
			break;

		default:
			//fill buffer with silence
			while (--samples >= 0)
			{
				*buffer++ = 0;
			}
			break;
	}
}