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

    Harris HC-55516 (and related) emulator

    Copyright Nicola Salmoria and the MAME Team

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

#include "emu.h"
#include "streams.h"
#include "hc55516.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


typedef struct _hc55516_state hc55516_state;
struct _hc55516_state
{
	sound_stream *channel;
	int		clock;		/* 0 = software driven, non-0 = oscillator */
	int		active_clock_hi;
	UINT8   shiftreg_mask;

	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 STREAM_UPDATE( hc55516_update );



INLINE hc55516_state *get_safe_token(running_device *device)
{
	assert(device != NULL);
	assert(device->type() == SOUND_HC55516 ||
		   device->type() == SOUND_MC3417 ||
		   device->type() == SOUND_MC3418);
	return (hc55516_state *)downcast<legacy_device_base *>(device)->token();
}


static void start_common(running_device *device, UINT8 _shiftreg_mask, int _active_clock_hi)
{
	hc55516_state *chip = get_safe_token(device);

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

	chip->clock = device->clock();
	chip->shiftreg_mask = _shiftreg_mask;
	chip->active_clock_hi = _active_clock_hi;
	chip->last_clock_state = 0;

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

	state_save_register_device_item(device, 0, chip->last_clock_state);
	state_save_register_device_item(device, 0, chip->digit);
	state_save_register_device_item(device, 0, chip->new_digit);
	state_save_register_device_item(device, 0, chip->shiftreg);
	state_save_register_device_item(device, 0, chip->curr_sample);
	state_save_register_device_item(device, 0, chip->next_sample);
	state_save_register_device_item(device, 0, chip->update_count);
	state_save_register_device_item(device, 0, chip->filter);
	state_save_register_device_item(device, 0, chip->integrator);
}


static DEVICE_START( hc55516 )
{
	start_common(device, 0x07, TRUE);
}


static DEVICE_START( mc3417 )
{
	start_common(device, 0x07, FALSE);
}


static DEVICE_START( mc3418 )
{
	start_common(device, 0x0f, FALSE);
}



static DEVICE_RESET( hc55516 )
{
	hc55516_state *chip = get_safe_token(device);
	chip->last_clock_state = 0;
}



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


INLINE int is_active_clock_transition(hc55516_state *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(hc55516_state *chip)
{
	return ((UINT64)chip->update_count * chip->clock * 2 / SAMPLE_RATE) & 0x01;
}


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

	/* shift the bit into the shift register */
	chip->shiftreg = (chip->shiftreg << 1) | chip->digit;

	/* move the estimator up or down a step based on the bit */
	if (chip->digit)
		integrator += chip->filter;
	else
		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 & chip->shiftreg_mask) == 0) ||
		((chip->shiftreg & chip->shiftreg_mask) == chip->shiftreg_mask))
	{
		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 STREAM_UPDATE( hc55516_update )
{
	hc55516_state *chip = (hc55516_state *)param;
	stream_sample_t *buffer = outputs[0];
	int i;
	INT32 sample, slope;

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

	if (!is_external_osciallator(chip))
	{
		/* track how many samples we've updated without a clock */
		chip->update_count += samples;
		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) / samples;
	chip->curr_sample = chip->next_sample;

	if (is_external_osciallator(chip))
	{
		/* external oscillator */
		for (i = 0; i < samples; 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 < samples; i++, sample += slope)
			*buffer++ = sample;
}


void hc55516_clock_w(running_device *device, int state)
{
	hc55516_state *chip = get_safe_token(device);
	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(running_device *device, int digit)
{
	hc55516_state *chip = get_safe_token(device);

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


int hc55516_clock_state_r(running_device *device)
{
	hc55516_state *chip = get_safe_token(device);

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

	stream_update(chip->channel);

	return current_clock_state(chip);
}



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

DEVICE_GET_INFO( hc55516 )
{
	switch (state)
	{
		/* --- the following bits of info are returned as 64-bit signed integers --- */
		case DEVINFO_INT_TOKEN_BYTES:					info->i = sizeof(hc55516_state);			break;

		/* --- the following bits of info are returned as pointers to data or functions --- */
		case DEVINFO_FCT_START:							info->start = DEVICE_START_NAME( hc55516 );	break;
		case DEVINFO_FCT_RESET:							info->reset = DEVICE_RESET_NAME( hc55516 );	break;

		/* --- the following bits of info are returned as NULL-terminated strings --- */
		case DEVINFO_STR_NAME:							strcpy(info->s, "HC-55516");				break;
		case DEVINFO_STR_FAMILY:					strcpy(info->s, "CVSD");					break;
		case DEVINFO_STR_VERSION:					strcpy(info->s, "2.1");						break;
		case DEVINFO_STR_SOURCE_FILE:						strcpy(info->s, __FILE__);					break;
		case DEVINFO_STR_CREDITS:					strcpy(info->s, "Copyright Nicola Salmoria and the MAME Team"); break;
	}
}


DEVICE_GET_INFO( mc3417 )
{
	switch (state)
	{
		case DEVINFO_FCT_START:							info->start = DEVICE_START_NAME( mc3417 );		break;
		case DEVINFO_FCT_RESET:							/* chip has no reset pin */					break;
		case DEVINFO_STR_NAME:							strcpy(info->s, "MC3417");					break;
		default:										DEVICE_GET_INFO_CALL(hc55516);					break;
	}
}


DEVICE_GET_INFO( mc3418 )
{
	switch (state)
	{
		case DEVINFO_FCT_START:							info->start = DEVICE_START_NAME( mc3418 );		break;
		case DEVINFO_FCT_RESET:							/* chip has no reset pin */					break;
		case DEVINFO_STR_NAME:							strcpy(info->s, "MC3418");					break;
		default:										DEVICE_GET_INFO_CALL(hc55516);					break;
	}
}