summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/sound/upd933.cpp
blob: 6167ec2a3c1722899f1d3b0ba0a93a4a2caddf0e (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
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
// license:BSD-3-Clause
// copyright-holders:Devin Acker

/***************************************************************************
    NEC/Casio uPD933 "Phase Distortion" synthesis chip
***************************************************************************/

#include "emu.h"
#include "upd933.h"

#include <algorithm>
#include <climits>
#include <cmath>

DEFINE_DEVICE_TYPE(UPD933, upd933_device, "upd933", "NEC uPD933")

upd933_device::upd933_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: device_t(mconfig, UPD933, tag, owner, clock)
	, device_sound_interface(mconfig, *this)
	, m_irq_cb(*this)
{
}

/**************************************************************************/
void upd933_device::device_start()
{
	m_stream = stream_alloc(0, 1, clock() / CLOCKS_PER_SAMPLE);

	m_irq_timer = timer_alloc(FUNC(upd933_device::timer_tick), this);

	for (int i = 0; i < 0x800; i++)
		m_cosine[i] = 0xfff * (1 - cos(2.0 * M_PI * i / 0x7ff)) / 2;

	for (int i = 0; i < 0x80; i++)
	{
		// A4 is note 62, 442 Hz
		const double freq = 442.0 * pow(2, (i - 62) / 12.0);
		m_pitch[i] = (1 << PITCH_SHIFT) * (freq * 0x800 / 40000);
	}

	for (int i = 0; i < 0x200; i++)
		m_pitch_fine[i] = (1 << PITCH_FINE_SHIFT) * (pow(2, (double)i / (12.0 * 0x200)) - 1);

	// logarithmic volume curve, also scales 12-bit waveform to 13-bit range
	// (also allows pitch modulation to cover the same spectrum with no extra scaling)
	for (int i = 1; i < 0x200; i++)
		m_volume[i] = pow(2 << VOLUME_SHIFT, (double)i / 0x1ff);
	m_volume[0] = 0;

	m_cs = m_id = 1;

	save_item(NAME(m_irq_pending));
	save_item(NAME(m_irq_state));
	save_item(NAME(m_cs));
	save_item(NAME(m_id));
	save_item(NAME(m_sound_data));
	save_item(NAME(m_sound_data_pos));
	save_item(NAME(m_sound_regs));
	save_item(NAME(m_sample_count));
	save_item(NAME(m_last_sample));

	save_item(STRUCT_MEMBER(m_voice, m_wave));
	save_item(STRUCT_MEMBER(m_voice, m_window));
	save_item(STRUCT_MEMBER(m_voice, m_ring_mod));
	save_item(STRUCT_MEMBER(m_voice, m_pitch_mod));
	save_item(STRUCT_MEMBER(m_voice, m_mute_other));
	save_item(STRUCT_MEMBER(m_voice, m_pitch));
	save_item(STRUCT_MEMBER(m_voice, m_position));
	save_item(STRUCT_MEMBER(m_voice, m_pitch_step));
	save_item(STRUCT_MEMBER(m_voice, m_dcw_limit));
	save_item(STRUCT_MEMBER(m_voice, m_pm_level));

	save_item(STRUCT_MEMBER(m_dca, m_direction));
	save_item(STRUCT_MEMBER(m_dca, m_sustain));
	save_item(STRUCT_MEMBER(m_dca, m_irq));
	save_item(STRUCT_MEMBER(m_dca, m_rate));
	save_item(STRUCT_MEMBER(m_dca, m_target));
	save_item(STRUCT_MEMBER(m_dca, m_current));

	save_item(STRUCT_MEMBER(m_dcw, m_direction));
	save_item(STRUCT_MEMBER(m_dcw, m_sustain));
	save_item(STRUCT_MEMBER(m_dcw, m_irq));
	save_item(STRUCT_MEMBER(m_dcw, m_rate));
	save_item(STRUCT_MEMBER(m_dcw, m_target));
	save_item(STRUCT_MEMBER(m_dcw, m_current));

	save_item(STRUCT_MEMBER(m_dco, m_direction));
	save_item(STRUCT_MEMBER(m_dco, m_sustain));
	save_item(STRUCT_MEMBER(m_dco, m_irq));
	save_item(STRUCT_MEMBER(m_dco, m_rate));
	save_item(STRUCT_MEMBER(m_dco, m_target));
	save_item(STRUCT_MEMBER(m_dco, m_current));
}

/**************************************************************************/
TIMER_CALLBACK_MEMBER(upd933_device::timer_tick)
{
	m_irq_pending = 1;
	update_irq();
}

/**************************************************************************/
void upd933_device::device_reset()
{
	m_irq_pending = m_irq_state = 0;

	m_sound_data[0] = m_sound_data[1] = 0;
	m_sound_data_pos = 0;
	std::fill(m_sound_regs.begin(), m_sound_regs.end(), 0);

	std::fill(m_voice.begin(), m_voice.end(), voice_t());
	std::fill(m_dca.begin(), m_dca.end(), env_t());
	std::fill(m_dco.begin(), m_dco.end(), env_t());
	std::fill(m_dcw.begin(), m_dcw.end(), env_t());

	m_sample_count = 0;
	m_last_sample = 0;

	m_irq_timer->adjust(attotime::never);
	m_irq_cb(0);
}

/**************************************************************************/
void upd933_device::device_clock_changed()
{
	m_stream->set_sample_rate(clock() / CLOCKS_PER_SAMPLE);
}

/**************************************************************************/
int upd933_device::rq_r()
{
	if (!machine().side_effects_disabled())
		m_stream->update();

	return m_irq_state;
}

/**************************************************************************/
void upd933_device::cs_w(int state)
{
	m_stream->update();

	if (!m_cs && state)
		update_pending_irq();
	m_cs = state;
	update_irq();
}

/**************************************************************************/
void upd933_device::id_w(int state)
{
	m_stream->update();

	m_id = state;
	update_irq();
}

/**************************************************************************/
u8 upd933_device::irq_data()
{
	// TODO: do these have the correct priority?
	for (int i = 0; i < 8; i++)
	{
		if (m_dco[i].m_irq)
		{
			if (!machine().side_effects_disabled())
				m_dco[i].m_irq = false;
			return 4 | (i << 3);
		}
	}
	for (int i = 0; i < 8; i++)
	{
		if (m_dcw[i].m_irq)
		{
			if (!machine().side_effects_disabled())
				m_dcw[i].m_irq = false;
			return 2 | (i << 2);
		}
	}
	for (int i = 0; i < 8; i++)
	{
		if (m_dca[i].m_irq)
		{
			if (!machine().side_effects_disabled())
				m_dca[i].m_irq = false;
			return 1 | (i << 1);
		}
	}
	return 0;
}

/**************************************************************************/
void upd933_device::update_pending_irq()
{
	m_irq_pending = 0;
	bool env_active = false;
	unsigned new_time = UINT_MAX;

	for (int i = 0; i < 8; i++)
	{
		env_active |= (m_dca[i].calc_timeout(new_time)
				   |   m_dco[i].calc_timeout(new_time)
				   |   m_dcw[i].calc_timeout(new_time));
	}

	if (env_active)
		m_irq_timer->adjust(clocks_to_attotime((u64)new_time * CLOCKS_PER_SAMPLE));
	else
		m_irq_timer->adjust(attotime::never);
}

/**************************************************************************/
void upd933_device::update_irq()
{
	u8 const irq_state = m_cs & m_id & m_irq_pending;
	if (irq_state != m_irq_state)
	{
		m_irq_state = irq_state;
		m_irq_cb(m_irq_state);
	}
}

/**************************************************************************/
u8 upd933_device::read()
{
	if (!machine().side_effects_disabled())
		m_stream->update();

	return m_cs ? 0xff : irq_data();
}

/**************************************************************************/
void upd933_device::write(u8 data)
{
	if (m_cs) return;

	if (m_sound_data_pos >= 2)
	{
		m_stream->update();

		bool ok = true;
		const u8 reg = m_sound_data[0];
		const u16 value = m_sound_regs[reg] = (m_sound_data[1] << 8) | data;

		// the low 3 bits of the register number determine which voice is controlled by per-voice registers...
		const int vnum = reg & 7;
		voice_t &voice = m_voice[vnum];
		// ...except for registers 68-6f, which control waveform for voice 'n', but modulation for voice 'n-2'
		// (even though those two voices don't actually modulate each other...)
		voice_t &mod_voice = m_voice[(vnum + 6) & 7];

		m_sound_data_pos = 0;
		switch (reg >> 3)
		{
		case 0x0: // 00-07: DCA step (volume envelope)
			/*
			msb      lsb
			n-------          - direction (0 = up, 1 = down)
			-nnnnnnn          - rate
			         n------- - sustain flag
			         -nnnnnnn - level
			*/
		{
			env_t &dca = m_dca[vnum];
			dca.m_direction = BIT(value, 15);
			dca.m_rate      = env_rate(BIT(value, 8, 7));
			dca.m_sustain   = BIT(value, 7);
			dca.m_target    = BIT(value, 0, 7) << (ENV_DCA_SHIFT + 2);
			dca.m_irq = false;
		}
			break;

		case 0x2: // 10-17: DCO step (pitch envelope)
			/*
			msb      lsb
			n-------          - direction (0 = up, 1 = down)
			-nnnnnnn          - rate
			         n------- - sustain flag
			         -n------ - level units (1 = 2-semitone intervals, 0 = 6.25-cent intervals)
			         --nnnnnn - level
			*/
		{
			env_t &dco = m_dco[vnum];
			dco.m_direction = BIT(value, 15);
			dco.m_rate      = env_rate(BIT(value, 8, 7));
			dco.m_sustain   = BIT(value, 7);
			dco.m_target    = BIT(value, 0, 6) << (ENV_DCO_SHIFT + 5);
			if (BIT(value, 6))
				dco.m_target <<= 5;
			dco.m_irq = false;
		}
			break;

		case 0x4: // 20-27: DCW step (waveform envelope)
			// same bits as DCA step
		{
			env_t &dcw = m_dcw[vnum];
			dcw.m_direction = BIT(value, 15);
			dcw.m_rate      = env_rate(BIT(value, 8, 7));
			dcw.m_sustain   = BIT(value, 7);
			dcw.m_target    = BIT(value, 0, 7) << (ENV_DCW_SHIFT + 3);
			dcw.m_irq = false;
		}
			break;

		case 0xc: // 60-67: pitch (in semitones, as 7.9 fixed point)
			voice.m_pitch = value;
			update_pitch_step(vnum);
			break;

		case 0xd: // 68-6f: waveform
			/*
			msb      lsb
			nnn-----          - first waveform
			---nnn--          - second waveform
			------n-          - enable second
			-------n nn------ - window function
			         --n----- - ring modulation enable
			         ---n---- - pitch modulation enable
			         ----n--- - pitch modulation source (0 = other voice, 1 = noise)
			         -----n-- - output (0 = normal, 1 = mute previous voice)
			*/
			voice.m_wave[0]         = BIT(value, 13, 3);
			if (BIT(value, 9))
				voice.m_wave[1]     = BIT(value, 10, 3);
			else
				voice.m_wave[1]     = voice.m_wave[0];
			voice.m_window          = BIT(value, 6, 3);
			if (!BIT(vnum, 0))
			{
				// see earlier comment - these bits actually control a different voice
				mod_voice.m_ring_mod    = BIT(value, 5);
				mod_voice.m_pitch_mod   = BIT(value, 3, 2);
				mod_voice.m_mute_other  = BIT(value, 2);
			}
			break;

		case 0x13: // 98-9f: phase counter
			/*
			cz101 sets these to zero when starting a note to reset the oscillator.
			cz1 writes 0x0000, 0x0080, 0x0100, or 0x0180 for up to four voices of a tone instead
			*/
			voice.m_position = value << (PITCH_SHIFT - 4);
			break;

		case 0x17: // b8-bb: pitch modulator (probably - cz1 sets to zero when disabling noise)
			if (vnum < 4)
				m_voice[vnum << 1].m_pm_level = (s16)value;
			else
				ok = false;
			break;

		default:
			ok = false;
			break;
		}

		if (!ok)
			logerror("%s: unknown sound reg write: %02x %04x\n", machine().describe_context(), reg, value);
	}
	else
	{
		m_sound_data[m_sound_data_pos++] = data;
	}
}

/**************************************************************************/
u32 upd933_device::env_rate(u8 data) const
{
	return (8 | (data & 7)) << (data >> 3);
}

/**************************************************************************/
void upd933_device::sound_stream_update(sound_stream &stream, std::vector<read_stream_view> const &inputs, std::vector<write_stream_view> &outputs)
{
	for (int i = 0; i < outputs[0].samples(); i++)
	{
		s32 sample = 0;

		/*
		Voices need to be processed in a certain order for modulation to work correctly,
		i.e. to match each odd-numbered voice ("line 1") with the corresponding even-numbered one ("line 2").
		*/
		static const int voice_map[] = {5, 0, 7, 2, 1, 4, 3, 6};
		for (int j : voice_map)
			sample += update(j);

		outputs[0].put_int_clamp(i, sample, 1 << 15);
		m_sample_count++;
	}
}

/**************************************************************************/
s16 upd933_device::update(int vnum)
{
	voice_t &voice = m_voice[vnum];
	s16 sample = 0;

	const u16 pos = BIT(voice.m_position, PITCH_SHIFT, 11);
	const u8 wave = BIT(voice.m_position, PITCH_SHIFT + 11);

	const u16 dcw = std::min(u16(m_dcw[vnum].m_current >> ENV_DCW_SHIFT), voice.m_dcw_limit);
	const u16 pivot = 0x400 - dcw;
	u16 phase = 0;
	u16 window = 0;

	//
	// apply transfer function
	//
	switch (voice.m_wave[wave] & 7)
	{
	case 0: // sawtooth - rises from [0, pivot) and falls from [pivot, 800)
		if (pos < pivot)
			phase = pos * 0x400 / pivot;
		else
			phase = 0x400 + (pos - pivot) * 0x400 / (0x800 - pivot);
		break;

	case 1: // square - rises from [0, pivot), stays high from [pivot, 400), then inverts
		if ((pos & 0x3ff) < pivot)
			phase = (pos & 0x3ff) * 0x400 / pivot;
		else
			phase = 0x3ff;

		phase |= (pos & 0x400);
		break;

	case 2: // pulse - rises & falls from [0, pivot*2), then stays low
		if (pos < pivot * 2)
			phase = pos * 0x800 / (pivot * 2);
		else
			phase = 0x7ff;
		break;

	case 3: // silent (undocumented)
		break;

	case 4: // double sine - rises & falls from [0, pivot), then again from [pivot, 800)
		if (pos < pivot)
			phase = pos * 0x800 / pivot;
		else
			phase = (pos - pivot) * 0x800 / (0x800 - pivot);
		break;

	case 5: // saw pulse - rises from [0, 400), falls from [400, 400+pivot), then stays low
		if (pos < 0x400)
			phase = pos;
		else if (pos < (pivot + 0x400))
			phase = 0x400 + (pos & 0x3ff) * 0x400 / pivot;
		else
			phase = 0x7ff;
		break;

	case 6: // resonance
		// this is a special case that just multiplies the frequency by the DCW level...
		phase = pos + ((pos * dcw) >> 6);
		// ...and hardsyncs to the fundamental frequency
		phase &= 0x7ff;
		break;

	case 7: // double pulse (undocumented) - same as regular pulse but double frequency
		if ((pos & 0x3ff) < pivot)
			phase = (pos & 0x3ff) * 0x400 / pivot;
		else
			phase = 0x7ff;
		break;
	}

	//
	// apply window function
	//
	switch (voice.m_window & 7)
	{
	case 0: // none
		break;

	case 1: // sawtooth - falls from [0, 800)
		window = pos;
		break;

	case 2: // triangle - rises from [0, 400), falls from [400, 800)
		window = (pos & 0x3ff) * 2;
		if (pos < 0x400)
			window ^= 0x7fe;
		break;

	case 3: // trapezoid - falls from [400, 800)
		if (pos >= 0x400)
			window = (pos & 0x3ff) * 2;
		break;

	case 4: // pulse (undocumented) - falls from [0, 400)
		if (pos < 0x400)
			window = pos * 2;
		else
			window = 0x7ff;
		break;

	default: // double saw (undocumented) - rises from [0, 400) and [400, 800)
		window = (0x3ff ^ (pos & 0x3ff)) * 2;
		break;
	}

	sample = m_cosine[phase];
	if (window)
		sample = ((s32)sample * (0x800 - window)) / 0x800;

	// center sample around zero, apply volume and ring mod
	const u16 volume = m_dca[vnum].m_current >> ENV_DCA_SHIFT;
	sample = ((s32)sample * m_volume[volume]) >> VOLUME_SHIFT;
	sample -= m_volume[volume] / 2;

	if (voice.m_ring_mod)
		sample = ((s32)sample * m_last_sample) / 0x1000;

	// 'mute' actually negates the other voice in a modulating pair
	if (voice.m_mute_other)
		sample -= m_last_sample;

	//
	// update envelopes and pitch modulation, recalculate DCO pitch step if needed
	//
	const u32 old_dco = m_dco[vnum].m_current;
	const s16 old_pm = voice.m_pm_level;

	m_dca[vnum].update();
	m_dcw[vnum].update();
	m_dco[vnum].update();

	// pitch/noise modulation latches a new pitch multiplier every 8 samples
	if (!(m_sample_count & 7))
	{
		switch (voice.m_pitch_mod & 3)
		{
		default:
			voice.m_pm_level = 0;
			break;

		case 2:
			// pitch modulated by other voice (normally unused, up to about +/- 7.5 semitones)
			voice.m_pm_level = m_last_sample;
			break;

		case 3:
			// pitch modulated by noise (0 or 32 semitones above base pitch)
			voice.m_pm_level = machine().rand() & (32 << NOTE_SHIFT);
			break;
		}
	}

	if ((old_dco ^ m_dco[vnum].m_current) >> ENV_DCO_SHIFT
		|| old_pm != voice.m_pm_level)
		update_pitch_step(vnum);

	voice.m_position += voice.m_pitch_step;

	m_last_sample = sample;
	return sample;
}

/**************************************************************************/
void upd933_device::env_t::update()
{
	if (m_current != m_target)
	{
		if (!m_direction) // increasing
		{
			if (m_current > m_target
				|| m_target - m_current <= m_rate)
				m_current = m_target;
			else
				m_current += m_rate;
		}
		else // decreasing
		{
			if (m_current < m_target
				|| m_current - m_target <= m_rate)
				m_current = m_target;
			else
				m_current -= m_rate;
		}
	}

	if (!m_sustain && (m_current == m_target))
		m_irq = m_sustain = true; // set sustain too to make sure this only causes an interrupt once
}

/**************************************************************************/
bool upd933_device::env_t::calc_timeout(unsigned &samples)
{
	if (m_irq)
	{
		samples = 0;
	}
	else if (m_sustain || !m_rate)
	{
		return false;
	}
	else
	{
		const unsigned remaining = m_direction ? (m_current - m_target) : (m_target - m_current);
		unsigned new_time = remaining / m_rate;
		if (remaining % m_rate)
			new_time++;
		if (new_time < samples)
			samples = new_time;
	}

	return true;
}

/**************************************************************************/
void upd933_device::update_pitch_step(int vnum)
{
	voice_t &voice = m_voice[vnum];
	const s32 pitch = s32(voice.m_pitch + (m_dco[vnum].m_current >> ENV_DCO_SHIFT)) + voice.m_pm_level;
	u32 step = 0;

	if (pitch > 0 && pitch < (1 << 16))
	{
		const u8 note = pitch >> NOTE_SHIFT;
		const u16 fine = pitch & ((1 << NOTE_SHIFT) - 1);
		step = m_pitch[note];
		if (fine)
			step += (step >> PITCH_FINE_SHIFT) * m_pitch_fine[fine];
	}

	voice.m_pitch_step = step;

	/*
	The effective DCW envelope value is limited for higher pitch values.
	This allows e.g. narrow pulse waves to remain correctly audible
	and also prevents aliasing noise for extremely high pitch values.
	*/
	voice.m_dcw_limit = 0x400 - std::min(0x400U, (step >> (PITCH_SHIFT - 2)));
}