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

    sndintrf.c

    Core sound interface functions and definitions.

    Copyright Nicola Salmoria and the MAME Team.
    Visit http://mamedev.org for licensing and usage restrictions.

****************************************************************************

    Still to do:
        * fix drivers that used to use ADPCM
        * many cores do their own resampling; they should stop
        * many cores mix to a separate buffer; no longer necessary

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

#include "driver.h"



/***************************************************************************
    DEBUGGING
***************************************************************************/

#define VERBOSE			(0)

#define VPRINTF(x) do { if (VERBOSE) mame_printf_debug x; } while (0)



/***************************************************************************
    TYPE DEFINITIONS
***************************************************************************/

typedef struct _sndintrf_data sndintrf_data;
struct _sndintrf_data
{
	sound_type		sndtype; 		/* type index of this sound chip */
	sound_type		aliastype;		/* aliased type index of this sound chip */
	device_config *	device;			/* dummy device for now */
	int				index; 			/* index of this sound chip */
};



/***************************************************************************
    VALIDATION MACROS
***************************************************************************/

#define VERIFY_SNDNUM(name) \
	assert_always(sndnum >= 0 && sndnum < totalsnd, #name "() called with invalid sound num!")

#define VERIFY_SNDTI(name) \
	int sndnum = sndti_to_sndnum(sndtype, sndindex); \
	assert_always(sndindex >= 0 && sndindex < totalsnd && sndnum != -1, #name "() called with invalid (type,index) pair!")

#define VERIFY_SNDTYPE(name) \



/***************************************************************************
    GLOBAL VARIABLES
***************************************************************************/

static sndintrf_data sound[MAX_SOUND];
static int totalsnd;



/***************************************************************************
    INITIALIZATION
***************************************************************************/

/*-------------------------------------------------
    sndintrf_init - discover all linked sound
    systems and build a matrix for sound
    (type,index) pairs for the current machine
-------------------------------------------------*/

void sndintrf_init(running_machine *machine)
{
	/* zap the sound data structures */
	memset(sound, 0, sizeof(sound));
	totalsnd = 0;
}


/*-------------------------------------------------
    sndintrf_init_sound - initialize data for a
    particular sndnum
-------------------------------------------------*/

static DEVICE_GET_INFO( sndclass )
{
	sndintrf_data *snddata = device->inline_config;
	(*snddata->sndtype)(device, state, (sndinfo *)info);
}

int sndintrf_init_sound(running_machine *machine, int sndnum, const char *tag, sound_type sndtype, int clock, const void *config)
{
	snd_start_func start;
	sndintrf_data *info = &sound[sndnum];
	int tokenbytes;
	int index;
	int sndidx;

	info->device = auto_malloc(sizeof(*info->device) + strlen(tag));
	memset(info->device, 0, sizeof(*info->device) + strlen(tag));
	info->device->type = DEVICE_GET_INFO_NAME(sndclass);
	info->device->machine = machine;
	strcpy(info->device->tag, tag);
	info->device->static_config = config;
	info->device->region = memory_region(info->device->machine, info->device->tag);
	info->device->regionbytes = memory_region_length(info->device->machine, info->device->tag);

	/* hack: stash the info pointer in the inline_config */
	info->device->inline_config = info;

	/* fill in the type and interface */
	info->sndtype = sndtype;
	info->aliastype = (sound_type)sndtype_get_info_fct(sndtype, SNDINFO_FCT_ALIAS);
	if (info->aliastype == 0)
		info->aliastype = sndtype;
	info->device->clock = clock;

	/* compute the index */
	for (index = 0, sndidx = 0; sndidx < totalsnd; sndidx++)
		if (sound[sndidx].aliastype == info->aliastype)
			index++;
	info->index = index;
	totalsnd++;
	
	/* allocate the token */
	tokenbytes = sndtype_get_info_int(sndtype, DEVINFO_INT_TOKEN_BYTES);
	assert(tokenbytes != 0);
	info->device->token = auto_malloc(tokenbytes);
	memset(info->device->token, 0, tokenbytes);

	/* start the chip, tagging all its streams */
	start = (snd_start_func)sndtype_get_info_fct(sndtype, SNDINFO_PTR_START);
	(*start)(info->device, clock);
	VPRINTF(("  token = %p\n", info->device->token));

	return 0;
}


/*-------------------------------------------------
    sndintrf_exit_sound - tear down data for a
    particular sndnum
-------------------------------------------------*/

void sndintrf_exit_sound(int sndnum)
{
	sndinfo info;

	info.stop = NULL;
	(*sound[sndnum].sndtype)(NULL, SNDINFO_PTR_STOP, &info);

	/* stop the chip */
	if (info.stop)
		(*info.stop)(sound[sndnum].device);
}



/***************************************************************************
    HELPERS
***************************************************************************/

/*-------------------------------------------------
    sndti_exists - return TRUE if a (type,index)
    pair describes an existing chip
-------------------------------------------------*/

int sndti_exists(sound_type type, int index)
{
	return (sndti_to_sndnum(type, index) != -1);
}


/*-------------------------------------------------
    sndti_to_sndnum - map a (type,index) pair to
    a sound number
-------------------------------------------------*/

int sndti_to_sndnum(sound_type type, int index)
{
	int sndnum;
	for (sndnum = 0; sndnum < totalsnd; sndnum++)
		if (sound[sndnum].aliastype == type)
			if (index-- == 0)
				return sndnum;
	return -1;
}


/*-------------------------------------------------
    sndnum_to_sndti - map a sound number to a
    (type,index) pair
-------------------------------------------------*/

sound_type sndnum_to_sndti(int sndnum, int *index)
{
	if (index != NULL)
		*index = sound[sndnum].index;
	return sound[sndnum].aliastype;
}


/*-------------------------------------------------
    sndtype_count - count the number of a
    given type
-------------------------------------------------*/

int sndtype_count(sound_type sndtype)
{
	int index;
	int count = 0;

	for (index = 0; index < totalsnd; index++)
		if (sound[index].sndtype == sndtype)
			count++;

	return count;
}



/***************************************************************************
    CHIP INTERFACES BY INDEX
***************************************************************************/

/*-------------------------------------------------
    Get info accessors
-------------------------------------------------*/

INT64 sndnum_get_info_int(int sndnum, UINT32 state)
{
	sndinfo info;

	VERIFY_SNDNUM(sndnum_get_info_int);
	info.i = 0;
	(*sound[sndnum].sndtype)(sound[sndnum].device, state, &info);
	return info.i;
}

void *sndnum_get_info_ptr(int sndnum, UINT32 state)
{
	sndinfo info;

	VERIFY_SNDNUM(sndnum_get_info_ptr);
	info.p = NULL;
	(*sound[sndnum].sndtype)(sound[sndnum].device, state, &info);
	return info.p;
}

genf *sndnum_get_info_fct(int sndnum, UINT32 state)
{
	sndinfo info;

	VERIFY_SNDNUM(sndnum_get_info_fct);
	info.f = NULL;
	(*sound[sndnum].sndtype)(sound[sndnum].device, state, &info);
	return info.f;
}

const char *sndnum_get_info_string(int sndnum, UINT32 state)
{
	extern char *get_temp_string_buffer(void);
	sndinfo info;

	VERIFY_SNDNUM(sndnum_get_info_string);
	info.s = get_temp_string_buffer();
	(*sound[sndnum].sndtype)(sound[sndnum].device, state, &info);
	return info.s;
}


/*-------------------------------------------------
    Misc accessors
-------------------------------------------------*/

void sndnum_reset(int sndnum)
{
	sndinfo info;

	VERIFY_SNDNUM(sndnum_reset);
	info.reset = NULL;
	(*sound[sndnum].sndtype)(NULL, SNDINFO_PTR_RESET, &info);
	if (info.reset)
		(info.reset)(sound[sndnum].device);
}

int sndnum_clock(int sndnum)
{
	VERIFY_SNDNUM(sndnum_clock);
	return sound[sndnum].device->clock;
}

void *sndnum_token(int sndnum)
{
	VERIFY_SNDNUM(sndnum_token);
	return sound[sndnum].device->token;
}



/***************************************************************************
    CHIP INTERFACES BY (TYPE,INDEX) PAIR
***************************************************************************/

/*-------------------------------------------------
    Get info accessors
-------------------------------------------------*/

INT64 sndti_get_info_int(sound_type sndtype, int sndindex, UINT32 state)
{
	sndinfo info;

	VERIFY_SNDTI(sndti_get_info_int);
	info.i = 0;
	(*sound[sndnum].sndtype)(sound[sndnum].device, state, &info);
	return info.i;
}

void *sndti_get_info_ptr(sound_type sndtype, int sndindex, UINT32 state)
{
	sndinfo info;

	VERIFY_SNDTI(sndti_get_info_ptr);
	info.p = NULL;
	(*sound[sndnum].sndtype)(sound[sndnum].device, state, &info);
	return info.p;
}

genf *sndti_get_info_fct(sound_type sndtype, int sndindex, UINT32 state)
{
	sndinfo info;

	VERIFY_SNDTI(sndti_get_info_fct);
	info.f = NULL;
	(*sound[sndnum].sndtype)(sound[sndnum].device, state, &info);
	return info.f;
}

const char *sndti_get_info_string(sound_type sndtype, int sndindex, UINT32 state)
{
	extern char *get_temp_string_buffer(void);
	sndinfo info;

	VERIFY_SNDTI(sndti_get_info_string);
	info.s = get_temp_string_buffer();
	(*sound[sndnum].sndtype)(sound[sndnum].device, state, &info);
	return info.s;
}


/*-------------------------------------------------
    Misc accessors
-------------------------------------------------*/

void sndti_reset(sound_type sndtype, int sndindex)
{
	sndinfo info;

	VERIFY_SNDTI(sndti_reset);
	info.reset = NULL;
	(*sound[sndnum].sndtype)(NULL, SNDINFO_PTR_RESET, &info);

	if (info.reset)
		(*info.reset)(sound[sndnum].device);
}

int sndti_clock(sound_type sndtype, int sndindex)
{
	VERIFY_SNDTI(sndti_clock);
	return sound[sndnum].device->clock;
}

void *sndti_token(sound_type sndtype, int sndindex)
{
	VERIFY_SNDTI(sndti_token);
	return sound[sndnum].device->token;
}



/***************************************************************************
    CHIP INTERFACES BY TYPE
***************************************************************************/

/*-------------------------------------------------
    Get info accessors
-------------------------------------------------*/

INT64 sndtype_get_info_int(sound_type sndtype, UINT32 state)
{
	snd_get_info_func get_info = sndtype;
	sndinfo info;

	VERIFY_SNDTYPE(sndtype_get_info_int);
	info.i = 0;
	(*get_info)(NULL, state, &info);
	return info.i;
}

void *sndtype_get_info_ptr(sound_type sndtype, UINT32 state)
{
	snd_get_info_func get_info = sndtype;
	sndinfo info;

	VERIFY_SNDTYPE(sndtype_get_info_ptr);
	info.p = NULL;
	(*get_info)(NULL, state, &info);
	return info.p;
}

genf *sndtype_get_info_fct(sound_type sndtype, UINT32 state)
{
	snd_get_info_func get_info = sndtype;
	sndinfo info;

	VERIFY_SNDTYPE(sndtype_get_info_fct);
	info.f = NULL;
	(*get_info)(NULL, state, &info);
	return info.f;
}

const char *sndtype_get_info_string(sound_type sndtype, UINT32 state)
{
	extern char *get_temp_string_buffer(void);
	snd_get_info_func get_info = sndtype;
	sndinfo info;

	VERIFY_SNDTYPE(sndtype_get_info_string);
	info.s = get_temp_string_buffer();
	(*get_info)(NULL, state, &info);
	return info.s;
}