1 /**********************************************************************************************
2  *
3  *   streaming ADPCM driver
4  *   by Aaron Giles
5  *
6  *   Library to transcode from an ADPCM source to raw PCM.
7  *   Written by Buffoni Mirko in 08/06/97
8  *   References: various sources and documents.
9  *
10  *	 HJB 08/31/98
11  *	 modified to use an automatically selected oversampling factor
12  *	 for the current Machine->sample_rate
13  *
14  *   Mish 21/7/99
15  *   Updated to allow multiple OKI chips with different sample rates
16  *
17  *   R. Belmont 31/10/2003
18  *   Updated to allow a driver to use both MSM6295s and "raw" ADPCM voices (gcpinbal)
19  *   Also added some error trapping for MAME_DEBUG builds
20  *
21  **********************************************************************************************/
22 
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <math.h>
27 
28 #include "driver.h"
29 #include "state.h"
30 #include "adpcm.h"
31 
32 #define MAX_SAMPLE_CHUNK	10000
33 
34 #define FRAC_BITS			14
35 #define FRAC_ONE			(1 << FRAC_BITS)
36 #define FRAC_MASK			(FRAC_ONE - 1)
37 
38 
39 /* struct describing a single playing ADPCM voice */
40 struct ADPCMVoice
41 {
42 	int stream;				/* which stream are we playing on? */
43 	UINT8 playing;			/* 1 if we are actively playing */
44 
45 	UINT8 *region_base;		/* pointer to the base of the region */
46 	UINT8 *base;			/* pointer to the base memory location */
47 	UINT32 sample;			/* current sample number */
48 	UINT32 count;			/* total samples to play */
49 
50 	UINT32 signal;			/* current ADPCM signal */
51 	UINT32 step;			/* current ADPCM step */
52 	UINT32 volume;			/* output volume */
53 
54 	INT16 last_sample;		/* last sample output */
55 	INT16 curr_sample;		/* current sample target */
56 	UINT32 source_step;		/* step value for frequency conversion */
57 	UINT32 source_pos;		/* current fractional position */
58 };
59 /* total ADPCM voices */
60 static UINT8 num_voices = 0;
61 
62 /* number of MSM6295 voices */
63 static UINT8 msm_voices = 0;
64 
65 /* array of ADPCM voices */
66 static struct ADPCMVoice adpcm[MAX_ADPCM];
67 
68 /* step size index shift table */
69 static int index_shift[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
70 
71 /* lookup table for the precomputed difference */
72 static int diff_lookup[49*16];
73 
74 /* volume lookup table */
75 static UINT32 volume_table[16];
76 
77 
78 
79 /**********************************************************************************************
80 
81      compute_tables -- compute the difference tables
82 
83 ***********************************************************************************************/
84 
compute_tables(void)85 static void compute_tables(void)
86 {
87 	/* nibble to bit map */
88 	static int nbl2bit[16][4] =
89 	{
90 		{ 1, 0, 0, 0}, { 1, 0, 0, 1}, { 1, 0, 1, 0}, { 1, 0, 1, 1},
91 		{ 1, 1, 0, 0}, { 1, 1, 0, 1}, { 1, 1, 1, 0}, { 1, 1, 1, 1},
92 		{-1, 0, 0, 0}, {-1, 0, 0, 1}, {-1, 0, 1, 0}, {-1, 0, 1, 1},
93 		{-1, 1, 0, 0}, {-1, 1, 0, 1}, {-1, 1, 1, 0}, {-1, 1, 1, 1}
94 	};
95 
96 	int step, nib;
97 
98 	/* loop over all possible steps */
99 	for (step = 0; step <= 48; step++)
100 	{
101 		/* compute the step value */
102 		int stepval = floor(16.0 * pow(11.0 / 10.0, (double)step));
103 
104 		/* loop over all nibbles and compute the difference */
105 		for (nib = 0; nib < 16; nib++)
106 		{
107 			diff_lookup[step*16 + nib] = nbl2bit[nib][0] *
108 				(stepval   * nbl2bit[nib][1] +
109 				 stepval/2 * nbl2bit[nib][2] +
110 				 stepval/4 * nbl2bit[nib][3] +
111 				 stepval/8);
112 		}
113 	}
114 
115 	/* generate the OKI6295 volume table */
116 	for (step = 0; step < 16; step++)
117 	{
118 		double out = 256.0;
119 		int vol = step;
120 
121 		/* 3dB per step */
122 		while (vol-- > 0)
123 			out /= 1.412537545;	/* = 10 ^ (3/20) = 3dB */
124 		volume_table[step] = (UINT32)out;
125 	}
126 }
127 
128 
129 
130 /**********************************************************************************************
131 
132      generate_adpcm -- general ADPCM decoding routine
133 
134 ***********************************************************************************************/
135 
generate_adpcm(struct ADPCMVoice * voice,INT16 * buffer,int samples)136 static void generate_adpcm(struct ADPCMVoice *voice, INT16 *buffer, int samples)
137 {
138 	/* if this voice is active */
139 	if (voice->playing)
140 	{
141 		UINT8 *base = voice->base;
142 		int sample = voice->sample;
143 		int signal = voice->signal;
144 		int count = voice->count;
145 		int step = voice->step;
146 		int val;
147 
148 		/* loop while we still have samples to generate */
149 		while (samples)
150 		{
151 			/* compute the new amplitude and update the current step */
152 			val = base[sample / 2] >> (((sample & 1) << 2) ^ 4);
153 			signal += diff_lookup[step * 16 + (val & 15)];
154 
155 			/* clamp to the maximum */
156 			if (signal > 2047)
157 				signal = 2047;
158 			else if (signal < -2048)
159 				signal = -2048;
160 
161 			/* adjust the step size and clamp */
162 			step += index_shift[val & 7];
163 			if (step > 48)
164 				step = 48;
165 			else if (step < 0)
166 				step = 0;
167 
168 			/* output to the buffer, scaling by the volume */
169 			*buffer++ = signal * voice->volume / 16;
170 			samples--;
171 
172 			/* next! */
173 			if (++sample >= count)
174 			{
175 				voice->playing = 0;
176 				break;
177 			}
178 		}
179 
180 		/* update the parameters */
181 		voice->sample = sample;
182 		voice->signal = signal;
183 		voice->step = step;
184 	}
185 
186 	/* fill the rest with silence */
187 	while (samples--)
188 		*buffer++ = 0;
189 }
190 
191 
192 
193 /**********************************************************************************************
194 
195      adpcm_update -- update the sound chip so that it is in sync with CPU execution
196 
197 ***********************************************************************************************/
198 
adpcm_update(int num,INT16 * buffer,int length)199 static void adpcm_update(int num, INT16 *buffer, int length)
200 {
201 	struct ADPCMVoice *voice = &adpcm[num];
202 	INT16 sample_data[MAX_SAMPLE_CHUNK], *curr_data = sample_data;
203 	INT16 prev = voice->last_sample, curr = voice->curr_sample;
204 	UINT32 final_pos;
205 	UINT32 new_samples;
206 
207 	/* finish off the current sample */
208 	if (voice->source_pos > 0)
209 	{
210 		/* interpolate */
211 		while (length > 0 && voice->source_pos < FRAC_ONE)
212 		{
213 			*buffer++ = (((INT32)prev * (FRAC_ONE - voice->source_pos)) + ((INT32)curr * voice->source_pos)) >> FRAC_BITS;
214 			voice->source_pos += voice->source_step;
215 			length--;
216 		}
217 
218 		/* if we're over, continue; otherwise, we're done */
219 		if (voice->source_pos >= FRAC_ONE)
220 			voice->source_pos -= FRAC_ONE;
221 		else
222 			return;
223 	}
224 
225 	/* compute how many new samples we need */
226 	final_pos = voice->source_pos + length * voice->source_step;
227 	new_samples = (final_pos + FRAC_ONE - 1) >> FRAC_BITS;
228 	if (new_samples > MAX_SAMPLE_CHUNK)
229 		new_samples = MAX_SAMPLE_CHUNK;
230 
231 	/* generate them into our buffer */
232 	generate_adpcm(voice, sample_data, new_samples);
233 	prev = curr;
234 	curr = *curr_data++;
235 
236 	/* then sample-rate convert with linear interpolation */
237 	while (length > 0)
238 	{
239 		/* interpolate */
240 		while (length > 0 && voice->source_pos < FRAC_ONE)
241 		{
242 			*buffer++ = (((INT32)prev * (FRAC_ONE - voice->source_pos)) + ((INT32)curr * voice->source_pos)) >> FRAC_BITS;
243 			voice->source_pos += voice->source_step;
244 			length--;
245 		}
246 
247 		/* if we're over, grab the next samples */
248 		if (voice->source_pos >= FRAC_ONE)
249 		{
250 			voice->source_pos -= FRAC_ONE;
251 			prev = curr;
252 			curr = *curr_data++;
253 		}
254 	}
255 
256 	/* remember the last samples */
257 	voice->last_sample = prev;
258 	voice->curr_sample = curr;
259 }
260 
261 
262 
263 /**********************************************************************************************
264 
265      state save support for MAME
266 
267 ***********************************************************************************************/
268 
269 static UINT32 voice_base_offset[MAX_ADPCM]; /*we cannot save the pointer - this is a workaround*/
adpcm_state_save_base_store(void)270 static void adpcm_state_save_base_store (void)
271 {
272 	int i;
273 	struct ADPCMVoice *voice;
274 
275 	for (i=0; i<num_voices; i++)
276 	{
277 		voice = &adpcm[i];
278 		voice_base_offset[i] = voice->base - voice->region_base;
279 	}
280 }
281 
adpcm_state_save_base_refresh(void)282 static void adpcm_state_save_base_refresh (void)
283 {
284 	int i;
285 	struct ADPCMVoice *voice;
286 
287 	for (i=0; i<num_voices; i++)
288 	{
289 		voice = &adpcm[i];
290 		voice->base = &voice->region_base[ voice_base_offset[i] ];
291 	}
292 }
293 
adpcm_state_save_register(void)294 static void adpcm_state_save_register( void )
295 {
296 	int i;
297 	char buf[20];
298 	struct ADPCMVoice *voice;
299 
300 
301 	sprintf(buf,"ADPCM");
302 
303 	for (i=msm_voices; i<num_voices; i++)
304 	{
305 		voice = &adpcm[i];
306 
307 		state_save_register_UINT8  (buf, i, "playing", &voice->playing, 1);
308 		state_save_register_UINT32 (buf, i, "base_offset" , &voice_base_offset[i],  1);
309 		state_save_register_UINT32 (buf, i, "sample" , &voice->sample,  1);
310 		state_save_register_UINT32 (buf, i, "count"  , &voice->count,   1);
311 		state_save_register_UINT32 (buf, i, "signal" , &voice->signal,  1);
312 		state_save_register_UINT32 (buf, i, "step"   , &voice->step,    1);
313 		state_save_register_UINT32 (buf, i, "volume" , &voice->volume,  1);
314 
315 		state_save_register_INT16  (buf, i, "last_sample", &voice->last_sample, 1);
316 		state_save_register_INT16  (buf, i, "curr_sample", &voice->curr_sample, 1);
317 		state_save_register_UINT32 (buf, i, "source_step", &voice->source_step, 1);
318 		state_save_register_UINT32 (buf, i, "source_pos" , &voice->source_pos,  1);
319 	}
320 
321 	if (msm_voices == 0)
322 	{
323 		state_save_register_func_presave(adpcm_state_save_base_store);
324 		state_save_register_func_postload(adpcm_state_save_base_refresh);
325 	}
326 }
327 
328 /**********************************************************************************************
329 
330      ADPCM_sh_start -- start emulation of several ADPCM output streams
331 
332 ***********************************************************************************************/
333 
ADPCM_sh_start(const struct MachineSound * msound)334 int ADPCM_sh_start(const struct MachineSound *msound)
335 {
336 	const struct ADPCMinterface *intf = msound->sound_interface;
337 	char stream_name[40];
338 	int i;
339 
340 	/* reset the ADPCM system */
341 	if (msm_voices > 0)
342 	{
343 		/* system has already been initalized by the MSM6295, do a smaller portion */
344 		num_voices += intf->num;
345 
346 		if (num_voices > MAX_ADPCM)
347 		{
348 			log_cb(RETRO_LOG_DEBUG, LOGPRE "ERROR: too many ADPCM voices: %d vs. MAX_ADPCM %d\n", num_voices, MAX_ADPCM);
349 		#ifdef MAME_DEBUG
350 			exit(-1);
351 		#endif
352     }
353 		for (i = msm_voices; i < num_voices; i++)
354 		{
355 			/* generate the name and create the stream */
356 			sprintf(stream_name, "%s #%d", sound_name(msound), i-msm_voices);
357 			adpcm[i].stream = stream_init(stream_name, intf->mixing_level[i-msm_voices], Machine->sample_rate, i, adpcm_update);
358 			if (adpcm[i].stream == -1)
359 				return 1;
360 
361 			/* initialize the rest of the structure */
362 			adpcm[i].region_base = memory_region(intf->region);
363 			adpcm[i].volume = 255;
364 			adpcm[i].signal = -2;
365 			if (Machine->sample_rate)
366 				adpcm[i].source_step = (UINT32)((double)intf->frequency * (double)FRAC_ONE / (double)Machine->sample_rate);
367 		}
368 	}
369 	else
370 	{
371 		num_voices = intf->num;
372 		if (num_voices > MAX_ADPCM)
373 		{
374 			log_cb(RETRO_LOG_DEBUG, LOGPRE "ERROR: too many ADPCM voices: %d vs. MAX_ADPCM %d\n", num_voices, MAX_ADPCM);
375 		#ifdef MAME_DEBUG
376 			exit(-1);
377 		#endif
378     }
379 		compute_tables();
380 
381 		/* initialize the voices */
382 		memset(adpcm, 0, sizeof(adpcm));
383 		for (i = 0; i < num_voices; i++)
384 		{
385 			/* generate the name and create the stream */
386 			sprintf(stream_name, "%s #%d", sound_name(msound), i);
387 			adpcm[i].stream = stream_init(stream_name, intf->mixing_level[i], Machine->sample_rate, i, adpcm_update);
388 			if (adpcm[i].stream == -1)
389 				return 1;
390 
391 			/* initialize the rest of the structure */
392 			adpcm[i].region_base = memory_region(intf->region);
393 			adpcm[i].volume = 255;
394 			adpcm[i].signal = -2;
395 			if (Machine->sample_rate)
396 				adpcm[i].source_step = (UINT32)((double)intf->frequency * (double)FRAC_ONE / (double)Machine->sample_rate);
397 		}
398 	}
399 
400 	adpcm_state_save_register();
401 
402 	/* success */
403 	return 0;
404 }
405 
406 
407 
408 /**********************************************************************************************
409 
410      ADPCM_sh_stop -- stop emulation of several ADPCM output streams
411 
412 ***********************************************************************************************/
413 
ADPCM_sh_stop(void)414 void ADPCM_sh_stop(void)
415 {
416 	num_voices = 0;
417 	msm_voices = 0;
418 }
419 
420 
421 
422 /**********************************************************************************************
423 
424      ADPCM_sh_update -- update ADPCM streams
425 
426 ***********************************************************************************************/
427 
ADPCM_sh_update(void)428 void ADPCM_sh_update(void)
429 {
430 }
431 
432 
433 
434 /**********************************************************************************************
435 
436      ADPCM_play -- play data from a specific offset for a specific length
437 
438 ***********************************************************************************************/
439 
ADPCM_play(int num,int offset,int length)440 void ADPCM_play(int num, int offset, int length)
441 {
442 	struct ADPCMVoice *voice = &adpcm[num+msm_voices];
443 
444 	/* bail if we're not playing anything */
445 	if (Machine->sample_rate == 0)
446 		return;
447 
448 	/* range check the numbers */
449 	if ((num+msm_voices) >= num_voices)
450 	{
451 		log_cb(RETRO_LOG_DEBUG, LOGPRE "error: ADPCM_trigger() called with channel = %d, but only %d channels allocated\n", num, num_voices);
452 		return;
453 	}
454 
455 	/* update the ADPCM voice */
456 	stream_update(voice->stream, 0);
457 
458 	/* set up the voice to play this sample */
459 	voice->playing = 1;
460 	voice->base = &voice->region_base[offset];
461 	voice->sample = 0;
462 	voice->count = length;
463 
464 	/* also reset the ADPCM parameters */
465 	voice->signal = -2;
466 	voice->step = 0;
467 }
468 
469 
470 
471 /**********************************************************************************************
472 
473      ADPCM_play -- stop playback on an ADPCM data channel
474 
475 ***********************************************************************************************/
476 
ADPCM_stop(int num)477 void ADPCM_stop(int num)
478 {
479 	struct ADPCMVoice *voice = &adpcm[num+msm_voices];
480 
481 	/* bail if we're not playing anything */
482 	if (Machine->sample_rate == 0)
483 		return;
484 
485 	/* range check the numbers */
486 	if ((num+msm_voices) >= num_voices)
487 	{
488 		log_cb(RETRO_LOG_DEBUG, LOGPRE "error: ADPCM_stop() called with channel = %d, but only %d channels allocated\n", num, num_voices);
489 		return;
490 	}
491 
492 	/* update the ADPCM voice */
493 	stream_update(voice->stream, 0);
494 
495 	/* stop playback */
496 	voice->playing = 0;
497 }
498 
499 
500 
501 /**********************************************************************************************
502 
503      ADPCM_setvol -- change volume on an ADPCM data channel
504 
505 ***********************************************************************************************/
506 
ADPCM_setvol(int num,int vol)507 void ADPCM_setvol(int num, int vol)
508 {
509 	struct ADPCMVoice *voice = &adpcm[num+msm_voices];
510 
511 	/* bail if we're not playing anything */
512 	if (Machine->sample_rate == 0)
513 		return;
514 
515 	/* range check the numbers */
516 	if ((num+msm_voices) >= num_voices)
517 	{
518 		log_cb(RETRO_LOG_DEBUG, LOGPRE "error: ADPCM_setvol() called with channel = %d, but only %d channels allocated\n", num, num_voices);
519 		return;
520 	}
521 
522 	/* update the ADPCM voice */
523 	stream_update(voice->stream, 0);
524 	voice->volume = vol;
525 }
526 
527 
528 
529 /**********************************************************************************************
530 
531      ADPCM_playing -- returns true if an ADPCM data channel is still playing
532 
533 ***********************************************************************************************/
534 
ADPCM_playing(int num)535 int ADPCM_playing(int num)
536 {
537 	struct ADPCMVoice *voice = &adpcm[num+msm_voices];
538 
539 	/* bail if we're not playing anything */
540 	if (Machine->sample_rate == 0)
541 		return 0;
542 
543 	/* range check the numbers */
544 	if ((num+msm_voices) >= num_voices)
545 	{
546 		log_cb(RETRO_LOG_DEBUG, LOGPRE "error: ADPCM_playing() called with channel = %d, but only %d channels allocated\n", num, num_voices);
547 		return 0;
548 	}
549 
550 	/* update the ADPCM voice */
551 	stream_update(voice->stream, 0);
552 	return voice->playing;
553 }
554 
555 
556 
557 /**********************************************************************************************
558  *
559  *	OKIM 6295 ADPCM chip:
560  *
561  *	Command bytes are sent:
562  *
563  *		1xxx xxxx = start of 2-byte command sequence, xxxxxxx is the sample number to trigger
564  *		abcd vvvv = second half of command; one of the abcd bits is set to indicate which voice
565  *		            the v bits seem to be volumed
566  *
567  *		0abc d000 = stop playing; one or more of the abcd bits is set to indicate which voice(s)
568  *
569  *	Status is read:
570  *
571  *		???? abcd = one bit per voice, set to 0 if nothing is playing, or 1 if it is active
572  *
573 ***********************************************************************************************/
574 
575 #define OKIM6295_VOICES		4
576 
577 static INT32 okim6295_command[MAX_OKIM6295];
578 static INT32 okim6295_base[MAX_OKIM6295][OKIM6295_VOICES];
579 
580 
581 /**********************************************************************************************
582 
583      state save support for MAME
584 
585 ***********************************************************************************************/
586 
okim6295_state_save_register(void)587 static void okim6295_state_save_register(void)
588 {
589 	int i,j;
590 	int chips;
591 	char buf[20];
592 	char buf2[20];
593 
594 	adpcm_state_save_register();
595 	sprintf(buf,"OKIM6295");
596 
597 	chips = num_voices / OKIM6295_VOICES;
598 	for (i = 0; i < chips; i++)
599 	{
600 		state_save_register_INT32  (buf, i, "command", &okim6295_command[i], 1);
601 		for (j = 0; j < OKIM6295_VOICES; j++)
602 		{
603 			sprintf(buf2,"base_voice_%1i",j);
604 			state_save_register_INT32  (buf, i, buf2, &okim6295_base[i][j], 1);
605 		}
606 	}
607 }
608 
609 
610 
611 /**********************************************************************************************
612 
613      OKIM6295_sh_start -- start emulation of an OKIM6295-compatible chip
614 
615 ***********************************************************************************************/
616 
OKIM6295_sh_start(const struct MachineSound * msound)617 int OKIM6295_sh_start(const struct MachineSound *msound)
618 {
619 	const struct OKIM6295interface *intf = msound->sound_interface;
620 	char stream_name[40];
621 	int i;
622 
623 	/* to share with "raw" ADPCM voices, we must be initialized first */
624 	if (num_voices > 0)
625 	{
626 		log_cb(RETRO_LOG_DEBUG, LOGPRE "ERROR: MSM6295s must appear in MDRV_ADD_SOUND list before ADPCMs\n");
627 		#ifdef MAME_DEBUG
628 			exit(-1);
629 		#endif
630   }
631 
632 	/* reset the ADPCM system */
633 	num_voices = intf->num * OKIM6295_VOICES;
634 	msm_voices = 0;
635 	#ifdef MAME_DEBUG
636 	if (num_voices > MAX_ADPCM)
637 	{
638 		log_cb(RETRO_LOG_DEBUG, LOGPRE "ERROR: too many ADPCM voices: %d vs. MAX_ADPCM %d\n", num_voices, MAX_ADPCM);
639 		#ifdef MAME_DEBUG
640 			exit(-1);
641 		#endif
642   }
643 	#endif
644 	compute_tables();
645 
646 	/* initialize the voices */
647 	memset(adpcm, 0, sizeof(adpcm));
648 	for (i = 0; i < num_voices; i++)
649 	{
650 		int chip = i / OKIM6295_VOICES;
651 		int voice = i % OKIM6295_VOICES;
652 
653 		/* reset the OKI-specific parameters */
654 		okim6295_command[chip] = -1;
655 		okim6295_base[chip][voice] = 0;
656 
657 		/* generate the name and create the stream */
658 		sprintf(stream_name, "%s #%d (voice %d)", sound_name(msound), chip, voice);
659 		adpcm[i].stream = stream_init(stream_name, intf->mixing_level[chip], Machine->sample_rate, i, adpcm_update);
660 		if (adpcm[i].stream == -1)
661 			return 1;
662 
663 		/* initialize the rest of the structure */
664 		adpcm[i].region_base = memory_region(intf->region[chip]);
665 		adpcm[i].volume = 255;
666 		adpcm[i].signal = -2;
667 		if (Machine->sample_rate)
668 			adpcm[i].source_step = (UINT32)((double)intf->frequency[chip] * (double)FRAC_ONE / (double)Machine->sample_rate);
669 	}
670 
671 	okim6295_state_save_register();
672 	msm_voices = num_voices;
673 
674 	/* success */
675 	return 0;
676 }
677 
678 
679 
680 /**********************************************************************************************
681 
682      OKIM6295_sh_stop -- stop emulation of an OKIM6295-compatible chip
683 
684 ***********************************************************************************************/
685 
OKIM6295_sh_stop(void)686 void OKIM6295_sh_stop(void)
687 {
688 	num_voices = 0;
689 	msm_voices = 0;
690 }
691 
692 
693 
694 /**********************************************************************************************
695 
696      OKIM6295_sh_update -- update emulation of an OKIM6295-compatible chip
697 
698 ***********************************************************************************************/
699 
OKIM6295_sh_update(void)700 void OKIM6295_sh_update(void)
701 {
702 }
703 
704 
705 
706 /**********************************************************************************************
707 
708      OKIM6295_set_bank_base -- set the base of the bank for a given voice on a given chip
709 
710 ***********************************************************************************************/
711 
OKIM6295_set_bank_base(int which,int base)712 void OKIM6295_set_bank_base(int which, int base)
713 {
714 	int channel;
715 
716 	for (channel = 0; channel < OKIM6295_VOICES; channel++)
717 	{
718 		struct ADPCMVoice *voice = &adpcm[which * OKIM6295_VOICES + channel];
719 
720 		/* update the stream and set the new base */
721 		stream_update(voice->stream, 0);
722 		okim6295_base[which][channel] = base;
723 	}
724 }
725 
726 
727 
728 /**********************************************************************************************
729 
730      OKIM6295_set_frequency -- dynamically adjusts the frequency of a given ADPCM voice
731 
732 ***********************************************************************************************/
733 
OKIM6295_set_frequency(int which,int frequency)734 void OKIM6295_set_frequency(int which, int frequency)
735 {
736 	int channel;
737 
738 	for (channel = 0; channel < OKIM6295_VOICES; channel++)
739 	{
740 		struct ADPCMVoice *voice = &adpcm[which * OKIM6295_VOICES + channel];
741 
742 		/* update the stream and set the new base */
743 		stream_update(voice->stream, 0);
744 		if (Machine->sample_rate)
745 			voice->source_step = (UINT32)((double)frequency * (double)FRAC_ONE / (double)Machine->sample_rate);
746 	}
747 }
748 
749 
750 /**********************************************************************************************
751 
752      OKIM6295_status_r -- read the status port of an OKIM6295-compatible chip
753 
754 ***********************************************************************************************/
755 
OKIM6295_status_r(int num)756 static int OKIM6295_status_r(int num)
757 {
758 	int i, result;
759 
760 	/* range check the numbers */
761 	if (num >= num_voices / OKIM6295_VOICES)
762 	{
763 		log_cb(RETRO_LOG_DEBUG, LOGPRE "error: OKIM6295_status_r() called with chip = %d, but only %d chips allocated\n",num, num_voices / OKIM6295_VOICES);
764 		return 0xff;
765 	}
766 
767 	result = 0xf0;	/* naname expects bits 4-7 to be 1 */
768 	/* set the bit to 1 if something is playing on a given channel */
769 	for (i = 0; i < OKIM6295_VOICES; i++)
770 	{
771 		struct ADPCMVoice *voice = &adpcm[num * OKIM6295_VOICES + i];
772 
773 		/* update the stream */
774 		stream_update(voice->stream, 0);
775 
776 		/* set the bit if it's playing */
777 		if (voice->playing)
778 			result |= 1 << i;
779 	}
780 
781 	return result;
782 }
783 
784 
785 
786 /**********************************************************************************************
787 
788      OKIM6295_data_w -- write to the data port of an OKIM6295-compatible chip
789 
790 ***********************************************************************************************/
791 
OKIM6295_data_w(int num,int data)792 static void OKIM6295_data_w(int num, int data)
793 {
794 	/* range check the numbers */
795 	if (num >= num_voices / OKIM6295_VOICES)
796 	{
797 		log_cb(RETRO_LOG_DEBUG, LOGPRE "error: OKIM6295_data_w() called with chip = %d, but only %d chips allocated\n", num, num_voices / OKIM6295_VOICES);
798 		return;
799 	}
800 
801 	/* if a command is pending, process the second half */
802 	if (okim6295_command[num] != -1)
803 	{
804 		int temp = data >> 4, i, start, stop;
805 		unsigned char *base;
806 
807 		/* determine which voice(s) (voice is set by a 1 bit in the upper 4 bits of the second byte) */
808 		for (i = 0; i < OKIM6295_VOICES; i++, temp >>= 1)
809 		{
810 			if (temp & 1)
811 			{
812 				struct ADPCMVoice *voice = &adpcm[num * OKIM6295_VOICES + i];
813 
814 				/* update the stream */
815 				stream_update(voice->stream, 0);
816 
817 				if (Machine->sample_rate == 0) return;
818 
819 				/* determine the start/stop positions */
820 				base = &voice->region_base[ okim6295_base[num][i] + okim6295_command[num] * 8];
821 				start = ((base[0] << 16) + (base[1] << 8) + base[2]) & 0x3ffff;
822 				stop  = ((base[3] << 16) + (base[4] << 8) + base[5]) & 0x3ffff;
823 
824 				/* set up the voice to play this sample */
825 				if (start < stop)
826 				{
827 					if (!voice->playing) /* fixes Got-cha and Steel Force */
828 					{
829 						voice->playing = 1;
830 						voice->base = &voice->region_base[okim6295_base[num][i] + start];
831 						voice->sample = 0;
832 						voice->count = 2 * (stop - start + 1);
833 
834 						/* also reset the ADPCM parameters */
835 						voice->signal = -2;
836 						voice->step = 0;
837 						voice->volume = volume_table[data & 0x0f];
838 					}
839 					else
840 					{
841 						log_cb(RETRO_LOG_DEBUG, LOGPRE "OKIM6295:%d requested to play sample %02x on non-stopped voice\n",num,okim6295_command[num]);
842 					}
843 				}
844 				/* invalid samples go here */
845 				else
846 				{
847 					log_cb(RETRO_LOG_DEBUG, LOGPRE "OKIM6295:%d requested to play invalid sample %02x\n",num,okim6295_command[num]);
848 					voice->playing = 0;
849 				}
850 			}
851 		}
852 
853 		/* reset the command */
854 		okim6295_command[num] = -1;
855 	}
856 
857 	/* if this is the start of a command, remember the sample number for next time */
858 	else if (data & 0x80)
859 	{
860 		okim6295_command[num] = data & 0x7f;
861 	}
862 
863 	/* otherwise, see if this is a silence command */
864 	else
865 	{
866 		int temp = data >> 3, i;
867 
868 		/* determine which voice(s) (voice is set by a 1 bit in bits 3-6 of the command */
869 		for (i = 0; i < 4; i++, temp >>= 1)
870 		{
871 			if (temp & 1)
872 			{
873 				struct ADPCMVoice *voice = &adpcm[num * OKIM6295_VOICES + i];
874 
875 				/* update the stream, then turn it off */
876 				stream_update(voice->stream, 0);
877 				voice->playing = 0;
878 			}
879 		}
880 	}
881 }
882 
883 
884 
885 /**********************************************************************************************
886 
887      OKIM6295_status_0_r -- generic status read functions
888      OKIM6295_status_1_r
889 
890 ***********************************************************************************************/
891 
READ_HANDLER(OKIM6295_status_0_r)892 READ_HANDLER( OKIM6295_status_0_r )
893 {
894 	return OKIM6295_status_r(0);
895 }
896 
READ_HANDLER(OKIM6295_status_1_r)897 READ_HANDLER( OKIM6295_status_1_r )
898 {
899 	return OKIM6295_status_r(1);
900 }
901 
READ_HANDLER(OKIM6295_status_2_r)902 READ_HANDLER( OKIM6295_status_2_r )
903 {
904 	return OKIM6295_status_r(2);
905 }
906 
READ16_HANDLER(OKIM6295_status_0_lsb_r)907 READ16_HANDLER( OKIM6295_status_0_lsb_r )
908 {
909 	return OKIM6295_status_r(0);
910 }
911 
READ16_HANDLER(OKIM6295_status_1_lsb_r)912 READ16_HANDLER( OKIM6295_status_1_lsb_r )
913 {
914 	return OKIM6295_status_r(1);
915 }
916 
READ16_HANDLER(OKIM6295_status_2_lsb_r)917 READ16_HANDLER( OKIM6295_status_2_lsb_r )
918 {
919 	return OKIM6295_status_r(2);
920 }
921 
READ16_HANDLER(OKIM6295_status_0_msb_r)922 READ16_HANDLER( OKIM6295_status_0_msb_r )
923 {
924 	return OKIM6295_status_r(0) << 8;
925 }
926 
READ16_HANDLER(OKIM6295_status_1_msb_r)927 READ16_HANDLER( OKIM6295_status_1_msb_r )
928 {
929 	return OKIM6295_status_r(1) << 8;
930 }
931 
READ16_HANDLER(OKIM6295_status_2_msb_r)932 READ16_HANDLER( OKIM6295_status_2_msb_r )
933 {
934 	return OKIM6295_status_r(2) << 8;
935 }
936 
937 
938 
939 /**********************************************************************************************
940 
941      OKIM6295_data_0_w -- generic data write functions
942      OKIM6295_data_1_w
943 
944 ***********************************************************************************************/
945 
WRITE_HANDLER(OKIM6295_data_0_w)946 WRITE_HANDLER( OKIM6295_data_0_w )
947 {
948 	OKIM6295_data_w(0, data);
949 }
950 
WRITE_HANDLER(OKIM6295_data_1_w)951 WRITE_HANDLER( OKIM6295_data_1_w )
952 {
953 	OKIM6295_data_w(1, data);
954 }
955 
WRITE_HANDLER(OKIM6295_data_2_w)956 WRITE_HANDLER( OKIM6295_data_2_w )
957 {
958 	OKIM6295_data_w(2, data);
959 }
960 
WRITE16_HANDLER(OKIM6295_data_0_lsb_w)961 WRITE16_HANDLER( OKIM6295_data_0_lsb_w )
962 {
963 	if (ACCESSING_LSB)
964 		OKIM6295_data_w(0, data & 0xff);
965 }
966 
WRITE16_HANDLER(OKIM6295_data_1_lsb_w)967 WRITE16_HANDLER( OKIM6295_data_1_lsb_w )
968 {
969 	if (ACCESSING_LSB)
970 		OKIM6295_data_w(1, data & 0xff);
971 }
972 
WRITE16_HANDLER(OKIM6295_data_2_lsb_w)973 WRITE16_HANDLER( OKIM6295_data_2_lsb_w )
974 {
975 	if (ACCESSING_LSB)
976 		OKIM6295_data_w(2, data & 0xff);
977 }
978 
WRITE16_HANDLER(OKIM6295_data_0_msb_w)979 WRITE16_HANDLER( OKIM6295_data_0_msb_w )
980 {
981 	if (ACCESSING_MSB)
982 		OKIM6295_data_w(0, data >> 8);
983 }
984 
WRITE16_HANDLER(OKIM6295_data_1_msb_w)985 WRITE16_HANDLER( OKIM6295_data_1_msb_w )
986 {
987 	if (ACCESSING_MSB)
988 		OKIM6295_data_w(1, data >> 8);
989 }
990 
WRITE16_HANDLER(OKIM6295_data_2_msb_w)991 WRITE16_HANDLER( OKIM6295_data_2_msb_w )
992 {
993 	if (ACCESSING_MSB)
994 		OKIM6295_data_w(2, data >> 8);
995 }
996