summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/sound/hc55516.c
blob: 7da48819d371f4d9060d8c175b2962d5a85e2c4c (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
/*****************************************************************************

    Harris HC-55516 (and related) emulator

    Copyright Nicola Salmoria and the MAME Team

    Notes:
        * The only difference between MC3417 and MC3418 is the number
          of bits checked for the Coincidence Output.  For the MC3417,
          it is three bits, while for the MC3418 it is four.
          This is not emulated.

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

#include "sndintrf.h"
#include "streams.h"
#include "hc55516.h"
#include <math.h>


/* 4x oversampling */
#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


struct hc55516_data
{
	sound_stream *channel;
	int		clock;		/* 0 = software driven, non-0 = oscillator */
	int		active_clock_hi;

	UINT8	last_clock_state;
	UINT8	digit;
	UINT8	new_digit;
	UINT8	shiftreg;

	INT16	curr_sample;
	INT16	next_sample;

	UINT32	update_count;

	double 	filter;
	double	integrator;
};


static double charge, decay, leak;


static void hc55516_update(void *param, stream_sample_t **inputs, stream_sample_t **_buffer, int length);



static void *start_common(int sndindex, int clock, const void *config, int _active_clock_hi)
{
	struct hc55516_data *chip;

	/* allocate the chip */
	chip = auto_malloc(sizeof(*chip));
	memset(chip, 0, sizeof(*chip));

	/* compute the fixed charge, decay, and leak time constants */
	charge = pow(exp(-1), 1.0 / (FILTER_CHARGE_TC * 16000.0));
	decay = pow(exp(-1), 1.0 / (FILTER_DECAY_TC * 16000.0));
	leak = pow(exp(-1), 1.0 / (INTEGRATOR_LEAK_TC * 16000.0));

	chip->clock = clock;
	chip->active_clock_hi = _active_clock_hi;
	chip->last_clock_state = 0;

	/* create the stream */
	chip->channel = stream_create(0, 1, SAMPLE_RATE, chip, hc55516_update);

	state_save_register_item("hc55516", sndindex, chip->last_clock_state);
	state_save_register_item("hc55516", sndindex, chip->digit);
	state_save_register_item("hc55516", sndindex, chip->new_digit);
	state_save_register_item("hc55516", sndindex, chip->shiftreg);
	state_save_register_item("hc55516", sndindex, chip->curr_sample);
	state_save_register_item("hc55516", sndindex, chip->next_sample);
	state_save_register_item("hc55516", sndindex, chip->update_count);
	state_save_register_item("hc55516", sndindex, chip->filter);
	state_save_register_item("hc55516", sndindex, chip->integrator);

	/* success */
	return chip;
}


static void *hc55516_start(int sndindex, int clock, const void *config)
{
	return start_common(sndindex, clock, config, TRUE);
}


static void *mc3417_start(int sndindex, int clock, const void *config)
{
	return start_common(sndindex, clock, config, FALSE);
}


static void *mc3418_start(int sndindex, int clock, const void *config)
{
	return start_common(sndindex, clock, config, FALSE);
}



static void hc55516_reset(void *chip)
{
	((struct hc55516_data *)chip)->last_clock_state = 0;
}



INLINE int is_external_osciallator(struct hc55516_data *chip)
{
	return chip->clock != 0;
}


INLINE int is_active_clock_transition(struct hc55516_data *chip, int clock_state)
{
	return (( chip->active_clock_hi && !chip->last_clock_state &&  clock_state) ||
			(!chip->active_clock_hi &&  chip->last_clock_state && !clock_state));
}


INLINE int current_clock_state(struct hc55516_data *chip)
{
	return ((UINT64)chip->update_count * chip->clock * 2 / SAMPLE_RATE) & 0x01;
}


static void process_digit(struct hc55516_data *chip)
{
	double integrator = chip->integrator, temp;

	/* move the estimator up or down a step based on the bit */
	if (chip->digit)
	{
		chip->shiftreg = ((chip->shiftreg << 1) | 1) & 7;
		integrator += chip->filter;
	}
	else
	{
		chip->shiftreg = (chip->shiftreg << 1) & 7;
		integrator -= chip->filter;
	}

	/* simulate leakage */
	integrator *= leak;

	/* if we got all 0's or all 1's in the last n bits, bump the step up */
	if (chip->shiftreg == 0 || chip->shiftreg == 7)
	{
		chip->filter = FILTER_MAX - ((FILTER_MAX - chip->filter) * charge);
		if (chip->filter > FILTER_MAX)
			chip->filter = FILTER_MAX;
	}

	/* simulate decay */
	else
	{
		chip->filter *= decay;
		if (chip->filter < FILTER_MIN)
			chip->filter = FILTER_MIN;
	}

	/* compute the sample as a 32-bit word */
	temp = integrator * SAMPLE_GAIN;
	chip->integrator = integrator;

	/* compress the sample range to fit better in a 16-bit word */
	if (temp < 0)
		chip->next_sample = (int)(temp / (-temp * (1.0 / 32768.0) + 1.0));
	else
		chip->next_sample = (int)(temp / (temp * (1.0 / 32768.0) + 1.0));
}


static void hc55516_update(void *param, stream_sample_t **inputs, stream_sample_t **_buffer, int length)
{
	struct hc55516_data *chip = param;
	stream_sample_t *buffer = _buffer[0];
	int i;
	INT32 sample, slope;

	/* zero-length? bail */
	if (length == 0)
		return;

	if (!is_external_osciallator(chip))
	{
		/* track how many samples we've updated without a clock */
		chip->update_count += length;
		if (chip->update_count > SAMPLE_RATE / 32)
		{
			chip->update_count = SAMPLE_RATE;
			chip->next_sample = 0;
		}
	}

	/* compute the interpolation slope */
	sample = chip->curr_sample;
	slope = ((INT32)chip->next_sample - sample) / length;
	chip->curr_sample = chip->next_sample;

	if (is_external_osciallator(chip))
	{
		/* external oscillator */
		for (i = 0; i < length; i++, sample += slope)
		{
			UINT8 clock_state;

			*buffer++ = sample;

			chip->update_count++;

			clock_state = current_clock_state(chip);

			/* pull in next digit on the appropriate edge of the clock */
			if (is_active_clock_transition(chip, clock_state))
			{
				chip->digit = chip->new_digit;

				process_digit(chip);
			}

			chip->last_clock_state = clock_state;
		}
	}

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


void hc55516_clock_w(int num, int state)
{
	struct hc55516_data *chip = sndti_token(SOUND_HC55516, num);
	UINT8 clock_state = state ? TRUE : FALSE;

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

	/* speech clock changing? */
	if (is_active_clock_transition(chip, clock_state))
	{
		/* update the output buffer before changing the registers */
		stream_update(chip->channel);

		/* clear the update count */
		chip->update_count = 0;

		process_digit(chip);
	}

	/* update the clock */
	chip->last_clock_state = clock_state;
}


void hc55516_digit_w(int num, int digit)
{
	struct hc55516_data *chip = sndti_token(SOUND_HC55516, num);

	if (is_external_osciallator(chip))
	{
		stream_update(chip->channel);
		chip->new_digit = digit & 1;
	}
	else
		chip->digit = digit & 1;
}


void hc55516_clock_clear_w(int num)
{
	hc55516_clock_w(num, 0);
}


void hc55516_clock_set_w(int num)
{
	hc55516_clock_w(num, 1);
}


void hc55516_digit_clock_clear_w(int num, int digit)
{
	struct hc55516_data *chip = sndti_token(SOUND_HC55516, num);
	chip->digit = digit & 1;
	hc55516_clock_w(num, 0);
}


int hc55516_clock_state_r(int num)
{
	struct hc55516_data *chip = sndti_token(SOUND_HC55516, num);

	/* only makes sense for setups with an external oscillator */
	assert(is_external_osciallator(chip));

	stream_update(chip->channel);

	return current_clock_state(chip);
}



WRITE8_HANDLER( hc55516_0_digit_w )	{ hc55516_digit_w(0, data); }
WRITE8_HANDLER( hc55516_0_clock_w )	{ hc55516_clock_w(0, data); }
WRITE8_HANDLER( hc55516_0_clock_clear_w )	{ hc55516_clock_clear_w(0); }
WRITE8_HANDLER( hc55516_0_clock_set_w )		{ hc55516_clock_set_w(0); }
WRITE8_HANDLER( hc55516_0_digit_clock_clear_w )	{ hc55516_digit_clock_clear_w(0, data); }
READ8_HANDLER ( hc55516_0_clock_state_r )	{ return hc55516_clock_state_r(0); }

WRITE8_HANDLER( hc55516_1_digit_w ) { hc55516_digit_w(1, data); }
WRITE8_HANDLER( hc55516_1_clock_w ) { hc55516_clock_w(1, data); }
WRITE8_HANDLER( hc55516_1_clock_clear_w ) { hc55516_clock_clear_w(1); }
WRITE8_HANDLER( hc55516_1_clock_set_w )  { hc55516_clock_set_w(1); }
WRITE8_HANDLER( hc55516_1_digit_clock_clear_w ) { hc55516_digit_clock_clear_w(1, data); }
READ8_HANDLER ( hc55516_1_clock_state_r )	{ return hc55516_clock_state_r(1); }



/**************************************************************************
 * Generic get_info
 **************************************************************************/

void hc55516_get_info(void *token, UINT32 state, sndinfo *info)
{
	switch (state)
	{
		/* --- the following bits of info are returned as 64-bit signed integers --- */
		case SNDINFO_INT_ALIAS:							info->i = SOUND_HC55516;				break;

		/* --- the following bits of info are returned as pointers to data or functions --- */
		case SNDINFO_PTR_START:							info->start = hc55516_start;			break;
		case SNDINFO_PTR_RESET:							info->reset = hc55516_reset;			break;

		/* --- the following bits of info are returned as NULL-terminated strings --- */
		case SNDINFO_STR_NAME:							info->s = "HC-55516";					break;
		case SNDINFO_STR_CORE_FAMILY:					info->s = "CVSD";						break;
		case SNDINFO_STR_CORE_VERSION:					info->s = "2.0";						break;
		case SNDINFO_STR_CORE_FILE:						info->s = __FILE__;						break;
		case SNDINFO_STR_CORE_CREDITS:					info->s = "Copyright Nicola Salmoria and the MAME Team"; break;
	}
}


void mc3417_get_info(void *token, UINT32 state, sndinfo *info)
{
	switch (state)
	{
		case SNDINFO_PTR_START:							info->start = mc3417_start;				break;
		case SNDINFO_PTR_RESET:							/* chip has no reset pin */				break;
		case SNDINFO_STR_NAME:							info->s = "MC3417";						break;
		default: 										hc55516_get_info(token, state, info);	break;
	}
}


void mc3418_get_info(void *token, UINT32 state, sndinfo *info)
{
	switch (state)
	{
		case SNDINFO_PTR_START:							info->start = mc3418_start;				break;
		case SNDINFO_PTR_RESET:							/* chip has no reset pin */				break;
		case SNDINFO_STR_NAME:							info->s = "MC3418";						break;
		default: 										hc55516_get_info(token, state, info);	break;
	}
}