summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/sound/hc55516.cpp
blob: ab4497a4a934924f675e746e21bdc18e64d69378 (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
// license:BSD-3-Clause
// copyright-holders:Aaron Giles, Jonathan Gevaryahu
// thanks-to:Zonn Moore
/*****************************************************************************

    Continuously Variable Slope Demodulator standalone chip emulator:
    Harris HC-55516 (sometimes labeled HCI-55516 or HC1-55516)
    Harris HC-55532 (sometimes labeled HCI-55532 or HC1-55532) [preliminary]
    Motorola MC-3417/MC-34115
    Motorola MC-3418

    TODO:
    - see .h file
    - research HC-55536 and HC-55564 differences vs HC-55516 (better auto-zeroing,
      and removal of the encoder offset compensation DAC?)
    - /src/mame/exidy/exidy440_a.cpp has its own internal implementation of the
      MC3417 and MC3418, it should be using this file instead

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

#include "emu.h"
#include "hc55516.h"


/* fixed samplerate of 192khz */
#define SAMPLE_RATE             (48000 * 4)

#define INTEGRATOR_LEAK_TC      0.001
#define FILTER_DECAY_TC         0.004
#define FILTER_CHARGE_TC        0.004
#define FILTER_MIN              0.0416
#define FILTER_MAX              1.0954
#define SAMPLE_GAIN             (10000.0 / 32768.0)


//#####################################
//                 COMMON
//#####################################
cvsd_device_base::cvsd_device_base(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock, bool active_clock_edge, uint8_t shiftreg_mask)
	: device_t(mconfig, type, tag, owner, clock)
	, device_sound_interface(mconfig, *this)
	, m_clock_state_push_cb(*this)
	, m_digin_pull_cb(*this)
	, m_digout_push_cb(*this)
	, m_active_clock_edge(active_clock_edge)
	, m_shiftreg_mask(shiftreg_mask)
	, m_last_clock_state(false)
	, m_buffered_bit(false)
	, m_shiftreg(0)
	, m_curr_sample(0)
	, m_next_sample(0)
	, m_samples_generated(0)
{
}

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

void cvsd_device_base::device_start()
{
	/* create the stream */
	m_stream = stream_alloc(0, 1, SAMPLE_RATE);

	save_item(NAME(m_last_clock_state));
	save_item(NAME(m_buffered_bit));
	save_item(NAME(m_shiftreg));
	save_item(NAME(m_curr_sample));
	save_item(NAME(m_next_sample));
	save_item(NAME(m_samples_generated));
}

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

void cvsd_device_base::device_reset()
{
	m_last_clock_state = 0;
}

//-------------------------------------------------
//  device_clock_changed - device-specific samplerate change
//-------------------------------------------------
/*void cvsd_device_base::device_clock_changed()
{
    // do nothing.
    //m_stream->set_sample_rate(clock());
}*/

READ_LINE_MEMBER( cvsd_device_base::clock_r )
{
	// prevent debugger from changing the internal state
	if (!machine().side_effects_disabled())
		m_stream->update(); /* bring up to date first */
	return clock_state_r();
}

WRITE_LINE_MEMBER( cvsd_device_base::mclock_w )
{
	clock_w(state);
}

WRITE_LINE_MEMBER( cvsd_device_base::digin_w )
{
	digit_w(state);
}

// the following encode related functions don't do anything yet, don't call them.
/*void cvsd_device_base::audio_in_w(int16_t data)
{
    assert(0);
}*/

WRITE_LINE_MEMBER( cvsd_device_base::dec_encq_w )
{
	assert(0);
}

READ_LINE_MEMBER( cvsd_device_base::digout_r )
{
	return 0;
}

// default and stub implementations

inline bool cvsd_device_base::is_external_oscillator()
{
	return clock() != 0;
}

inline bool cvsd_device_base::is_clock_changed(bool clock_state)
{
	return ((!m_last_clock_state && clock_state) ||
			(m_last_clock_state && !clock_state));
}

inline bool cvsd_device_base::is_active_clock_transition(bool clock_state)
{
	return ((clock_state != m_last_clock_state) &&
			(clock_state == m_active_clock_edge));
}

inline bool cvsd_device_base::current_clock_state()
{
	// keep track of the clock state given its previous state and the number of samples produced
	// i.e. if we generated m_samples_generated samples, at a sample rate of SAMPLE_RATE, then are we on a
	// positive or negative level of a squarewave at clock() hz? SAMPLE_RATE may not be an integer multiple of clock()
	//uint64_t fractions_of_second = (((uint64_t)m_samples_generated)<<32) / SAMPLE_RATE; // 32.32 bits of seconds passed so far
	//uint32_t clock_edges_passed =  (fractions_of_second * clock() * 2)>>32
	//return (((((uint64_t)m_samples_generated<<32) * clock() * 2 / SAMPLE_RATE)>>32) & 0x1)?true:false;
	return (((uint64_t)m_samples_generated * clock() * 2 / SAMPLE_RATE) & 0x01)?true:false;
}

void cvsd_device_base::digit_w(int digit)
{
	m_stream->update();
	m_buffered_bit = digit ? true : false;
}

void cvsd_device_base::clock_w(int state)
{
	/* update the output buffer first */
	m_stream->update();
	bool clock_state = state ? true : false;

	/* only makes sense for setups with a software driven clock */
	assert(!is_external_oscillator());

	/* speech clock changing? */
	if (is_clock_changed(clock_state))
	{
		/* clear the update count */
		m_samples_generated = 0;
		process_bit(m_buffered_bit, clock_state);
	}

	/* update the clock */
	m_last_clock_state = clock_state;
}

int cvsd_device_base::clock_state_r()
{
	/* only makes sense for setups with an external oscillator */
	assert(is_external_oscillator());

	m_stream->update();

	return current_clock_state();
}

void cvsd_device_base::process_bit(bool bit, bool clock_state)
{
	// stub
}

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

void cvsd_device_base::sound_stream_update(sound_stream &stream, std::vector<read_stream_view> const &inputs, std::vector<write_stream_view> &outputs)
{
	// Stub, just return silence
	auto &buffer = outputs[0];

	m_samples_generated += buffer.samples();
	if (m_samples_generated >= SAMPLE_RATE)
		m_samples_generated -= SAMPLE_RATE;
	buffer.fill(0);
}


//#########################################
//                 HC55516
//#########################################
DEFINE_DEVICE_TYPE(HC55516, hc55516_device, "hc55516", "HC-55516")

hc55516_device::hc55516_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: hc55516_device(mconfig, HC55516, tag, owner, clock, 0xfc0, 6, 0xfc1, 4)
{
}

// overridable type for hc55532 etc
hc55516_device::hc55516_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock, uint32_t sylmask, int32_t sylshift, int32_t syladd, int32_t intshift)
	: cvsd_device_base(mconfig, type, tag, owner, clock, RISING, 0x7)
	, m_agc_push_cb(*this)
	, m_fzq_pull_cb(*this)
	, m_sylmask(sylmask)
	, m_sylshift(sylshift)
	, m_syladd(syladd)
	, m_intshift(intshift)
	, m_sylfilter(0)
	, m_intfilter(0)
	, m_agc(true)
	, m_buffered_fzq(true)
{
}

//-------------------------------------------------
//  device_start - device-specific start
//-------------------------------------------------

void hc55516_device::device_start()
{
	cvsd_device_base::device_start();
	save_item(NAME(m_sylfilter));
	save_item(NAME(m_intfilter));
	save_item(NAME(m_agc));
	save_item(NAME(m_buffered_fzq));

	/* resolve lines */
	m_agc_push_cb.resolve();
	m_fzq_pull_cb.resolve();
}

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

void hc55516_device::device_reset()
{
	cvsd_device_base::device_reset();
	// simulate /FZ having been held for a while
	m_sylfilter = 0x3f;
	m_intfilter = 0;
	m_agc = true;
	m_buffered_fzq = true; // assuming /FZ was just released and is now high/inactive
}

// device specific functions

WRITE_LINE_MEMBER( hc55516_device::fzq_w )
{
	m_buffered_fzq = state;
}

READ_LINE_MEMBER( hc55516_device::agc_r )
{
	// prevent debugger from changing the internal state
	if (!machine().side_effects_disabled())
		m_stream->update(); /* bring up to date first */
	return m_agc;
}

void hc55516_device::process_bit(bool bit, bool clock_state)
{
	bool frozen =  (  ( (m_intfilter >= 0x180) && (!bit) ) ||
		( (m_intfilter <= -0x180) && (bit) )  );

	int32_t sum;
	if (is_active_clock_transition(clock_state))
	{
		// grab the /FZ state; if the callback is present, use that, otherwise use the buffered state
		bool fzq_state = false;
		if (!m_fzq_pull_cb.isnull())
			fzq_state = m_fzq_pull_cb();
		else
			fzq_state = m_buffered_fzq;

		if (!fzq_state) // /FZ is active low, if it is active, the input bit is ignored and the inverse of the previous bit in the shifter is used instead
			bit = !(m_shiftreg&1);

		/* shift the new bit into the shift register */
		m_shiftreg = (m_shiftreg << 1) | (bit?1:0);

		/* if we got all 0's or all 1's in the last n bits... */
		if (((m_shiftreg & m_shiftreg_mask) == 0) ||
			((m_shiftreg & m_shiftreg_mask) == m_shiftreg_mask))
		{
			// coincidence is true
			if (!frozen) m_sylfilter += (((~m_sylfilter) & m_sylmask) >> m_sylshift);
		}
		else
		{
			// coincidence is false
			if (!frozen) m_sylfilter += (((~m_sylfilter) & m_sylmask) >> m_sylshift) + m_syladd;
		}
		m_sylfilter &= 0xfff;

		sum = ( ((~m_intfilter) >> m_intshift) + 1 ) & 0x3ff;
	}
	else // inactive clock transition
	{
		if (m_shiftreg&1)
		{
			sum = (  ( ~std::max(2, m_sylfilter >> 6) ) + 1  ) & 0x3ff;
		}
		else
		{
			sum = std::max(2, m_sylfilter >> 6) & 0x3ff;
		}
	}

	if (sum & 0x200)
		sum |= ~0x3ff; // sign extend

	if (!frozen)
	{
		m_intfilter += sum;
		m_intfilter &= 0x3ff;
		if (m_intfilter & 0x200) m_intfilter |= ~0x3ff; // sign extend
	}

	/* scale the result (-512 to 511) to -32768 thru 32767 */
	/*
	F E D C B A 9 8 7 6 5 4 3 2 1 0
	9 8 7 6 5 4 3 2 1 0/9 8 7 6 5 4
	*/
	m_next_sample = (  (m_intfilter << 6) | ( ((m_intfilter & 0x3ff) ^ 0x200 ) >> 4 )  );

	// update agc state
	if ( (m_intfilter >= 0x100) || (m_intfilter <= -0x100) )
		m_agc = false;
	else
		m_agc = true;

	// push agc state if a callback is present
	if (!m_agc_push_cb.isnull())
		m_agc_push_cb(m_agc);
}

//-------------------------------------------------
//  sound_stream_update_legacy - handle a stream update
//-------------------------------------------------

void hc55516_device::sound_stream_update(sound_stream &stream, std::vector<read_stream_view> const &inputs, std::vector<write_stream_view> &outputs)
{
	auto &buffer = outputs[0];

/*
    if (!is_external_oscillator())
    {
        // track how many samples we've updated without a clock; if it's been more than 1/32 of a second, output silence
        m_samples_generated += buffer.samples();
        if (m_samples_generated > SAMPLE_RATE / 32)
        {
            m_samples_generated = SAMPLE_RATE;
            m_next_sample = 0;
        }
    }
*/

	if (is_external_oscillator())
	{
		/* external oscillator */
		for (int i = 0; i < buffer.samples(); i++)
		{
			buffer.put_int(i, m_next_sample, 32768);

			m_samples_generated++;

			uint8_t clock_state = current_clock_state();

			/* pull in next digit on the appropriate edge of the clock */
			if (is_clock_changed(clock_state))
			{
				process_bit(m_buffered_bit, clock_state);
			}

			m_last_clock_state = clock_state;
		}
	}

	/* software driven clock */
	else
		for (int i = 0; i < buffer.samples(); i++)
			buffer.put_int(i, m_next_sample, 32768);
}



//#########################################
//                 HC55532
//#########################################
DEFINE_DEVICE_TYPE(HC55532, hc55532_device, "hc55532", "HC-55532")

hc55532_device::hc55532_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: hc55516_device(mconfig, HC55532, tag, owner, clock, 0xf80, 7, 0xfe1, 5)
{
}

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

void hc55532_device::device_reset()
{
	cvsd_device_base::device_reset();
	// simulate /FZ having been held for a while
	m_sylfilter = 0x7f;
	m_intfilter = 0;
	m_agc = true;
	m_buffered_fzq = true; // assuming /FZ was just released and is now high/inactive
}



//##########################################
//                 MC3417
//##########################################
DEFINE_DEVICE_TYPE(MC3417, mc3417_device, "mc3417", "MC3417")

mc3417_device::mc3417_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: mc3417_device(mconfig, MC3417, tag, owner, clock, 0x7)
{
}

// overridable type for mc3418 etc
mc3417_device::mc3417_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock, uint8_t shiftreg_mask)
	: cvsd_device_base(mconfig, type, tag, owner, clock, FALLING, shiftreg_mask)
	, m_charge(pow(exp(-1.0), 1.0 / (FILTER_CHARGE_TC * 16000.0)))
	, m_decay(pow(exp(-1.0), 1.0 / (FILTER_DECAY_TC * 16000.0)))
	, m_leak(pow(exp(-1.0), 1.0 / (INTEGRATOR_LEAK_TC * 16000.0)))
	, m_sylfilter_d(0.0)
	, m_intfilter_d(0.0)
{
}

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

void mc3417_device::device_start()
{
	cvsd_device_base::device_start();
	save_item(NAME(m_sylfilter_d));
	save_item(NAME(m_intfilter_d));
}

void mc3417_device::process_bit(bool bit, bool clock_state)
{
	if (is_active_clock_transition(clock_state))
	{

		/* shift the new bit into the shift register */
		m_shiftreg = (m_shiftreg << 1) | (bit?1:0);

		/* move the estimator up or down a step based on the bit */
		if (!bit)
			m_intfilter_d += m_sylfilter_d;
		else
			m_intfilter_d -= m_sylfilter_d;

		/* simulate leakage */
		m_intfilter_d *= m_leak;

		/* if we got all 0's or all 1's in the last n bits, bump the step up */
		if (((m_shiftreg & m_shiftreg_mask) == 0) ||
			((m_shiftreg & m_shiftreg_mask) == m_shiftreg_mask))
		{
			// coincidence is true
			m_sylfilter_d = FILTER_MAX - ((FILTER_MAX - m_sylfilter_d) * m_charge);

			if (m_sylfilter_d > FILTER_MAX)
				m_sylfilter_d = FILTER_MAX;
		}
		else
		{
			m_sylfilter_d *= m_decay;

			if (m_sylfilter_d < FILTER_MIN)
				m_sylfilter_d = FILTER_MIN;
		}

		/* compute the sample as a 32-bit word */
		m_next_sample = m_intfilter_d * SAMPLE_GAIN;
	}
}

void mc3417_device::sound_stream_update(sound_stream &stream, std::vector<read_stream_view> const &inputs, std::vector<write_stream_view> &outputs)
{
	auto &buffer = outputs[0];

	if (!is_external_oscillator())
	{
		/* track how many samples we've updated without a clock; if it's been more than 1/32 of a second, output silence */
		m_samples_generated += buffer.samples();
		if (m_samples_generated > SAMPLE_RATE / 32)
		{
			m_samples_generated = SAMPLE_RATE;
			m_next_sample = 0;
		}
	}

	/* compute the interpolation slope */
	stream_buffer::sample_t sample = m_curr_sample;
	stream_buffer::sample_t slope = (m_next_sample - sample) / buffer.samples();
	m_curr_sample = m_next_sample;

	if (is_external_oscillator())
	{
		/* external oscillator */
		for (int i = 0; i < buffer.samples(); i++, sample += slope)
		{
			buffer.put(i, sample);

			m_samples_generated++;

			uint8_t clock_state = current_clock_state();

			/* pull in next digit on the appropriate edge of the clock */
			if (is_clock_changed(clock_state))
			{
				process_bit(m_buffered_bit, clock_state);
			}

			m_last_clock_state = clock_state;
		}
	}

	/* software driven clock */
	else
		for (int i = 0; i < buffer.samples(); i++, sample += slope)
			buffer.put(i, sample);
}



//##########################################
//                 MC3418
//##########################################
DEFINE_DEVICE_TYPE(MC3418, mc3418_device, "mc3418", "MC3418")

mc3418_device::mc3418_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: mc3417_device(mconfig, MC3418, tag, owner, clock, 0xf)
{
}