1 /************************************************************
2
3 NEC uPD7759 ADPCM Speech Processor
4 by: Juergen Buchmueller, Mike Balfour and Howie Cohen
5
6
7 Description:
8 The uPD7759 is a speech processing LSI that, with an external
9 ROM, utilizes ADPCM to produce speech. The uPD7759 can
10 directly address up to 1Mbits of external data ROM, or the
11 host CPU can control the speech data transfer. Three sample
12 frequencies are selectable - 5, 6, or 8 kHz. The external
13 ROM can store a maximum of 256 different messages and up to
14 50 seconds of speech.
15
16 The uPD7759 should always be hooked up to a 640 kHz clock.
17
18 TODO:
19 1) find bugs
20 2) fix bugs
21 3) Bankswitching and frequency selection may not be 100%
22
23 NOTES:
24
25 There are 2 types of upd7759 sound roms, master and slave.
26
27 A master rom has a header at the beginning of the rom
28 for example : 15 5A A5 69 55 (this is the POW header)
29
30 -the 1st byte (15) is the number of samples stored in the rom
31 (actually the number of samples minus 1 - NS)
32 -the next 4 bytes seems standard in every upd7759 rom used in
33 master mode (5A A5 69 55)
34 -after that there is table of (sample offsets)/2 we use this to
35 calculate the sample table on the fly. Then the samples start,
36 each sample has a short header with sample rate info in it.
37 A master rom can have up to 256 samples , and there should be
38 only one rom per upd7759.
39
40 a slave rom has no header... but each sample starts with
41 FF 00 00 00 00 10 (followed by a few other bytes that are
42 usually the same but not always)
43
44 Clock rates:
45 in master mode the clock rate should always be 64000000
46 sample frequencies are selectable - 5, 6, or 8 kHz. This
47 info is coded in the uPD7759 roms, it selects the sample
48 frequency for you.
49
50 slave mode is still some what of a mystery. Everything
51 we know about slave mode (and 1/2 of everything in master
52 mode) is from guesswork. As far as we know the clock rate
53 is the same for all samples in slave mode on a per game basis
54 so valid clock rates are: 40000000, 48000000, 64000000
55
56 Differances between master/slave mode.
57 (very basic explanation based on my understanding)
58
59 Master mode: the sound cpu sends a sample number to the upd7759
60 it then sends a trigger command to it, and the upd7759 plays the
61 sample directly from the rom (like an adpcm chip)
62
63 Slave mode: the sound cpu sends data to the upd7759 to select
64 the bank for the sound data, each bank is 0x4000 in size, and
65 the sample offset. The sound cpu then sends the data for the sample
66 a byte(?) at a time (dac / cvsd like) which the upd7759 plays till
67 it reaches the header of the next sample (FF 00 00 00 00)
68
69 Changes:
70 05/99 HJB
71 Tried to figure better index_shift and diff_lookutp tables and
72 also adjusted sample value range. It seems the 4 bits of the
73 ADPCM data are signed (0x0f == -1)! Also a signal and step
74 width fall off seems to be closer to the real thing.
75 Reduced work load by adding a wrap around buffer for slave
76 mode data that is stuffed by the sound CPU.
77 Finally removed (now obsolete) 8 bit sample support.
78
79 *************************************************************/
80
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <math.h>
84
85 #include "driver.h"
86 #include "upd7759.h"
87 #include "streams.h"
88
89 #define VERBOSE 0
90
91 #if VERBOSE
92 #define LOG(n,x) if((n)>=VERBOSE) logerror x
93 #else
94 #define LOG(n,x)
95 #endif
96
97 /* number of samples stuffed into the rom */
98 static unsigned char numsam;
99
100 /* playback rate for the streams interface */
101 /* BASE_CLOCK or a multiple (if oversampling is active) */
102 static int emulation_rate;
103
104 static int base_rate;
105 /* define the output rate */
106 #define CLOCK_DIVIDER 80
107
108 #define OVERSAMPLING 0 /* 1 use oversampling, 0 don't */
109
110 /* signal fall off factor */
111 #define FALL_OFF(n) ((n)-(((n)+7)/8))
112
113 #define SIGNAL_BITS 15 /* signal range */
114 #define SIGNAL_MAX (0x7fff >> (15-SIGNAL_BITS))
115 #define SIGNAL_MIN -SIGNAL_MAX
116
117 #define STEP_MAX 32
118 #define STEP_MIN 0
119
120 #define DATA_MAX 512
121
122 struct UPD7759sample
123 {
124 unsigned int offset; /* offset in that region */
125 unsigned int length; /* length of the sample */
126 unsigned int freq; /* play back freq of sample */
127 };
128
129
130 /* struct describing a single playing ADPCM voice */
131 struct UPD7759voice
132 {
133 int playing; /* 1 if we are actively playing */
134 unsigned char *base; /* pointer to the base memory location */
135 int mask; /* mask to keep us within the buffer */
136 int sample; /* current sample number (sample data in slave mode) */
137 int freq; /* current sample playback freq */
138 int count; /* total samples to play */
139 int signal; /* current ADPCM signal */
140 #if OVERSAMPLING
141 int old_signal; /* last ADPCM signal */
142 #endif
143 int step; /* current ADPCM step */
144 int counter; /* sample counter */
145 void *timer; /* timer used in slave mode */
146 int data[DATA_MAX]; /* data array used in slave mode */
147 unsigned head; /* head of data array used in slave mode */
148 unsigned tail; /* tail of data array used in slave mode */
149 unsigned available;
150 };
151
152 /* global pointer to the current interface */
153 static const struct UPD7759_interface *upd7759_intf;
154
155 /* array of ADPCM voices */
156 static struct UPD7759voice updadpcm[MAX_UPD7759];
157
158 #if OVERSAMPLING
159 /* oversampling factor, ie. playback_rate / BASE_CLOCK */
160 static int oversampling;
161 #endif
162 /* array of channels returned by streams.c */
163 static int channel[MAX_UPD7759];
164
165 /* stores the current sample number */
166 static int sampnum[MAX_UPD7759];
167
168 /* step size index shift table */
169 #define INDEX_SHIFT_MAX 16
170 static int index_shift[INDEX_SHIFT_MAX] = {
171 0, 1, 2, 3, 6, 7, 10, 15,
172 0, 15, 10, 7, 6, 3, 2, 1
173 };
174
175 /* lookup table for the precomputed difference */
176 static int diff_lookup[(STEP_MAX+1)*16];
177
178 static void UPD7759_update (int chip, INT16 *buffer, int left);
179
180 /*
181 * Compute the difference table
182 */
183
ComputeTables(void)184 static void ComputeTables (void)
185 {
186 /* nibble to bit map */
187 static int nbl2bit[16][4] = {
188 { 1, 0, 0, 0}, { 1, 0, 0, 1}, { 1, 0, 1, 0}, { 1, 0, 1, 1},
189 { 1, 1, 0, 0}, { 1, 1, 0, 1}, { 1, 1, 1, 0}, { 1, 1, 1, 1},
190 {-1, 0, 0, 0}, {-1, 0, 0, 1}, {-1, 0, 1, 0}, {-1, 0, 1, 1},
191 {-1, 1, 0, 0}, {-1, 1, 0, 1}, {-1, 1, 1, 0}, {-1, 1, 1, 1},
192 };
193 int step, nib;
194
195 /* loop over all possible steps */
196 for (step = 0; step <= STEP_MAX; step++)
197 {
198 /* compute the step value */
199 int stepval = 6 * (step+1) * (step+1);
200 LOG(1,("step %2d:", step));
201 /* loop over all nibbles and compute the difference */
202 for (nib = 0; nib < 16; nib++)
203 {
204 diff_lookup[step*16 + nib] = nbl2bit[nib][0] *
205 (stepval * nbl2bit[nib][1] +
206 stepval/2 * nbl2bit[nib][2] +
207 stepval/4 * nbl2bit[nib][3] +
208 stepval/8);
209 LOG(1,(" %+6d", diff_lookup[step*16 + nib]));
210 }
211 LOG(1,("\n"));
212 }
213 }
214
215
216
find_sample(int num,int sample_num,struct UPD7759sample * sample)217 static int find_sample(int num, int sample_num,struct UPD7759sample *sample)
218 {
219 int j;
220 int nextoff = 0;
221 unsigned char *memrom;
222 unsigned char *header; /* upd7759 has a 4 byte what we assume is an identifier (bytes 1-4)*/
223 unsigned char *data;
224
225
226 memrom = memory_region(upd7759_intf->region[num]);
227
228 numsam = (unsigned int)memrom[0]; /* get number of samples from sound rom */
229 header = &(memrom[1]);
230
231 if (memcmp (header, "\x5A\xA5\x69\x55",4) == 0)
232 {
233 LOG(1,("uPD7759 header verified\n"));
234 }
235 else
236 {
237 LOG(1,("uPD7759 header verification failed\n"));
238 }
239
240 LOG(1,("Number of samples in UPD7759 rom = %d\n",numsam));
241
242 /* move the header pointer to the start of the sample offsets */
243 header = &(memrom[5]);
244
245
246 if (sample_num > numsam) return 0; /* sample out of range */
247
248
249 nextoff = 2 * sample_num;
250 sample->offset = ((((unsigned int)(header[nextoff]))<<8)+(header[nextoff+1]))*2;
251 data = &memory_region(upd7759_intf->region[num])[sample->offset];
252 /* guesswork, probably wrong */
253 j = 0;
254 if (!data[j]) j++;
255 if ((data[j] & 0xf0) != 0x50) j++;
256
257 // Added and Modified by Takahiro Nogi. 1999/10/28
258 #if 0 // original
259 switch (data[j])
260 {
261 case 0x53: sample->freq = 8000; break;
262 case 0x59: sample->freq = 6000; break;
263 case 0x5f: sample->freq = 5000; break;
264 default:
265 sample->freq = 5000;
266 }
267 #else // modified by Takahiro Nogi. 1999/10/28
268 switch (data[j] & 0x1f)
269 {
270 case 0x13: sample->freq = 8000; break;
271 case 0x19: sample->freq = 6000; break;
272 case 0x1f: sample->freq = 5000; break;
273 default: // ???
274 sample->freq = 5000;
275 }
276 #endif
277
278 if (sample_num == numsam)
279 {
280 sample->length = 0x20000 - sample->offset;
281 }
282 else
283 sample->length = ((((unsigned int)(header[nextoff+2]))<<8)+(header[nextoff+3]))*2 -
284 ((((unsigned int)(header[nextoff]))<<8)+(header[nextoff+1]))*2;
285
286 data = &memory_region(upd7759_intf->region[num])[sample->offset];
287 /*
288 logerror("play sample %3d, offset $%06x, length %5d, freq = %4d [data $%02x $%02x $%02x]\n",
289 sample_num,
290 sample->offset,
291 sample->length,
292 sample->freq,
293 data[0],data[1],data[2]);
294 */
295 return 1;
296 }
297
298
299 /*
300 * Start emulation of several ADPCM output streams
301 */
302
303
UPD7759_sh_start(const struct MachineSound * msound)304 int UPD7759_sh_start (const struct MachineSound *msound)
305 {
306 int i;
307 const struct UPD7759_interface *intf = (const struct UPD7759_interface *)msound->sound_interface;
308
309 if( Machine->sample_rate == 0 )
310 return 0;
311
312 /* compute the difference tables */
313 ComputeTables ();
314
315 /* copy the interface pointer to a global */
316 upd7759_intf = intf;
317 base_rate = intf->clock_rate / CLOCK_DIVIDER;
318
319 #if OVERSAMPLING
320 oversampling = (Machine->sample_rate / base_rate);
321 if (!oversampling) oversampling = 1;
322 emulation_rate = base_rate * oversampling;
323 #else
324 emulation_rate = base_rate;
325 #endif
326
327
328 memset(updadpcm,0,sizeof(updadpcm));
329 for (i = 0; i < intf->num; i++)
330 {
331 char name[20];
332
333 updadpcm[i].mask = 0xffffffff;
334 updadpcm[i].signal = 0;
335 updadpcm[i].step = 0;
336 updadpcm[i].counter = emulation_rate / 2;
337
338 sprintf(name,"uPD7759 #%d",i);
339
340 channel[i] = stream_init(name,intf->volume[i],emulation_rate,i,UPD7759_update);
341 }
342 return 0;
343 }
344
345
346 /*
347 * Stop emulation of several UPD7759 output streams
348 */
349
UPD7759_sh_stop(void)350 void UPD7759_sh_stop (void)
351 {
352 }
353
354
355 /*
356 * Update emulation of an uPD7759 output stream
357 */
UPD7759_update(int chip,INT16 * buffer,int left)358 static void UPD7759_update (int chip, INT16 *buffer, int left)
359 {
360 struct UPD7759voice *voice = &updadpcm[chip];
361 int i;
362
363 /* see if there's actually any need to generate samples */
364 LOG(3,("UPD7759_update %d (%d)\n", left, voice->available));
365
366 if (left > 0)
367 {
368 /* if this voice is active */
369 if (voice->playing)
370 {
371 voice->available -= left;
372 if( upd7759_intf->mode == UPD7759_SLAVE_MODE )
373 {
374 while( left-- > 0 )
375 {
376 *buffer++ = voice->data[voice->tail];
377 #if OVERSAMPLE
378 if( (voice->counter++ % OVERSAMPLE) == 0 )
379 #endif
380 voice->tail = (voice->tail + 1) % DATA_MAX;
381 }
382 }
383 else
384 {
385 unsigned char *base = voice->base;
386 int val;
387 #if OVERSAMPLING
388 int i, delta;
389 #endif
390
391 while( left > 0 )
392 {
393 /* compute the new amplitude and update the current voice->step */
394 val = base[(voice->sample / 2) & voice->mask] >> (((voice->sample & 1) << 2) ^ 4);
395 voice->step = FALL_OFF(voice->step) + index_shift[val & (INDEX_SHIFT_MAX-1)];
396 if (voice->step > STEP_MAX) voice->step = STEP_MAX;
397 else if (voice->step < STEP_MIN) voice->step = STEP_MIN;
398 voice->signal = FALL_OFF(voice->signal) + diff_lookup[voice->step * 16 + (val & 15)];
399 if (voice->signal > SIGNAL_MAX) voice->signal = SIGNAL_MAX;
400 else if (voice->signal < SIGNAL_MIN) voice->signal = SIGNAL_MIN;
401 #if OVERSAMPLING
402 i = 0;
403 delta = voice->signal - voice->old_signal;
404 while (voice->counter > 0 && left > 0)
405 {
406 *sample++ = voice->old_signal + delta * i / oversampling;
407 if (++i == oversampling) i = 0;
408 voice->counter -= voice->freq;
409 left--;
410 }
411 voice->old_signal = voice->signal;
412 #else
413 while (voice->counter > 0 && left > 0)
414 {
415 *buffer++ = voice->signal;
416 voice->counter -= voice->freq;
417 left--;
418 }
419 #endif
420 voice->counter += emulation_rate;
421
422 /* next! */
423 if( ++voice->sample > voice->count )
424 {
425 while (left-- > 0)
426 {
427 *buffer++ = voice->signal;
428 voice->signal = FALL_OFF(voice->signal);
429 }
430 voice->playing = 0;
431 break;
432 }
433 }
434 }
435 }
436 else
437 {
438 /* voice is not playing */
439 for (i = 0; i < left; i++)
440 *buffer++ = voice->signal;
441 }
442 }
443 }
444
445 /************************************************************
446 UPD7759_message_w
447
448 Store the inputs to I0-I7 externally to the uPD7759.
449
450 I0-I7 input the message number of the message to be
451 reproduced. The inputs are latched at the rising edge of the
452 !ST input. Unused pins should be grounded.
453
454 In slave mode it seems like the ADPCM data is stuffed
455 here from an external source (eg. Z80 NMI code).
456 *************************************************************/
457
UPD7759_message_w(int num,int data)458 void UPD7759_message_w (int num, int data)
459 {
460 struct UPD7759voice *voice = updadpcm + num;
461
462 /* bail if we're not playing anything */
463 if (Machine->sample_rate == 0)
464 return;
465
466 /* range check the numbers */
467 if( num >= upd7759_intf->num )
468 {
469 LOG(1,("error: UPD7759_SNDSELECT() called with channel = %d, but only %d channels allocated\n", num, upd7759_intf->num));
470 return;
471 }
472
473 if (upd7759_intf->mode == UPD7759_SLAVE_MODE)
474 {
475 int offset = -1;
476
477 //LOG(1,("upd7759_message_w $%02x\n", data));
478 //logerror("upd7759_message_w $%2x\n",data);
479
480 switch (data) {
481
482 case 0x00: /* roms 0x10000 & 0x20000 in size */
483 case 0x38: offset = 0x10000; break; /* roms 0x8000 in size */
484
485 case 0x01: /* roms 0x10000 & 0x20000 in size */
486 case 0x39: offset = 0x14000; break; /* roms 0x8000 in size */
487
488 case 0x02: /* roms 0x10000 & 0x20000 in size */
489 case 0x34: offset = 0x18000; break; /* roms 0x8000 in size */
490
491 case 0x03: /* roms 0x10000 & 0x20000 in size */
492 case 0x35: offset = 0x1c000; break; /* roms 0x8000 in size */
493
494 case 0x04: /* roms 0x10000 & 0x20000 in size */
495 case 0x2c: offset = 0x20000; break; /* roms 0x8000 in size */
496
497 case 0x05: /* roms 0x10000 & 0x20000 in size */
498 case 0x2d: offset = 0x24000; break; /* roms 0x8000 in size */
499
500 case 0x06: /* roms 0x10000 & 0x20000 in size */
501 case 0x1c: offset = 0x28000; break; /* roms 0x8000 in size in size */
502
503 case 0x07: /* roms 0x10000 & 0x20000 in size */
504 case 0x1d: offset = 0x2c000; break; /* roms 0x8000 in size */
505
506 case 0x08: offset = 0x30000; break; /* roms 0x10000 & 0x20000 in size */
507 case 0x09: offset = 0x34000; break; /* roms 0x10000 & 0x20000 in size */
508 case 0x0a: offset = 0x38000; break; /* roms 0x10000 & 0x20000 in size */
509 case 0x0b: offset = 0x3c000; break; /* roms 0x10000 & 0x20000 in size */
510 case 0x0c: offset = 0x40000; break; /* roms 0x10000 & 0x20000 in size */
511 case 0x0d: offset = 0x44000; break; /* roms 0x10000 & 0x20000 in size */
512 case 0x0e: offset = 0x48000; break; /* roms 0x10000 & 0x20000 in size */
513 case 0x0f: offset = 0x4c000; break; /* roms 0x10000 & 0x20000 in size */
514
515 default:
516
517 //LOG(1,("upd7759_message_w unhandled $%02x\n", data));
518 //logerror("upd7759_message_w unhandled $%02x\n", data);
519 if ((data & 0xc0) == 0xc0)
520 {
521 if (voice->timer)
522 {
523 timer_remove(voice->timer);
524 voice->timer = 0;
525 }
526 voice->playing = 0;
527 }
528 }
529 if (offset > 0)
530 {
531 voice->base = &memory_region(upd7759_intf->region[num])[offset];
532 //LOG(1,("upd7759_message_w set base $%08x\n", offset));
533 //logerror("upd7759_message_w set base $%08x\n", offset);
534 }
535 }
536 else
537 {
538
539 LOG(1,("uPD7759 calling sample : %d\n", data));
540 sampnum[num] = data;
541
542 }
543 }
544
545 /************************************************************
546 UPD7759_dac
547
548 Called by the timer interrupt at twice the sample rate.
549 The first time the external irq callback is called, the
550 second time the ADPCM msb is converted and the resulting
551 signal is sent to the DAC.
552 ************************************************************/
UPD7759_dac(int num)553 static void UPD7759_dac(int num)
554 {
555 static int dac_msb = 0;
556 struct UPD7759voice *voice = updadpcm + num;
557
558 dac_msb ^= 1;
559 if( dac_msb )
560 {
561 LOG(3,("UPD7759_dac: $%x ", voice->sample & 15));
562 /* convert lower nibble */
563 voice->step = FALL_OFF(voice->step) + index_shift[voice->sample & (INDEX_SHIFT_MAX-1)];
564 if (voice->step > STEP_MAX) voice->step = STEP_MAX;
565 else if (voice->step < STEP_MIN) voice->step = STEP_MIN;
566 voice->signal = FALL_OFF(voice->signal) + diff_lookup[voice->step * 16 + (voice->sample & 15)];
567 if (voice->signal > SIGNAL_MAX) voice->signal = SIGNAL_MAX;
568 else if (voice->signal < SIGNAL_MIN) voice->signal = SIGNAL_MIN;
569 LOG(3,("step: %3d signal: %+5d\n", voice->step, voice->signal));
570 voice->head = (voice->head + 1) % DATA_MAX;
571 voice->data[voice->head] = voice->signal;
572 voice->available++;
573 }
574 else
575 {
576 if( upd7759_intf->irqcallback[num] )
577 (*upd7759_intf->irqcallback[num])(num);
578 }
579 }
580
581 /************************************************************
582 UPD7759_start_w
583
584 !ST pin:
585 Setting the !ST input low while !CS is low will start
586 speech reproduction of the message in the speech ROM locations
587 addressed by the contents of I0-I7. If the device is in
588 standby mode, standby mode will be released.
589 NOTE: While !BUSY is low, another !ST will not be accepted.
590 *************************************************************/
591
UPD7759_start_w(int num,int data)592 void UPD7759_start_w (int num, int data)
593 {
594 struct UPD7759voice *voice = updadpcm + num;
595
596 /* bail if we're not playing anything */
597 if (Machine->sample_rate == 0)
598 return;
599
600 /* range check the numbers */
601 if( num >= upd7759_intf->num )
602 {
603 LOG(1,("error: UPD7759_play_stop() called with channel = %d, but only %d channels allocated\n", num, upd7759_intf->num));
604 return;
605 }
606
607 /* handle the slave mode */
608 if (upd7759_intf->mode == UPD7759_SLAVE_MODE)
609 {
610 if (voice->playing)
611 {
612 /* if the chip is busy this should be the ADPCM data */
613 data &= 0xff; /* be sure to use 8 bits value only */
614 LOG(3,("UPD7759_data_w: $%x ", (data >> 4) & 15));
615
616 /* detect end of a sample by inspection of the last 5 bytes */
617 /* FF 00 00 00 00 is the start of the next sample */
618 if( voice->count > 5 && voice->sample == 0xff && data == 0x00 )
619 {
620 /* remove an old timer */
621 if (voice->timer)
622 {
623 timer_remove(voice->timer);
624 voice->timer = 0;
625 }
626 /* stop playing this sample */
627 voice->playing = 0;
628 return;
629 }
630
631 /* save the data written in voice->sample */
632 voice->sample = data;
633 voice->count++;
634
635 /* conversion of the ADPCM data to a new signal value */
636 voice->step = FALL_OFF(voice->step) + index_shift[(voice->sample >> 4) & (INDEX_SHIFT_MAX-1)];
637 if (voice->step > STEP_MAX) voice->step = STEP_MAX;
638 else if (voice->step < STEP_MIN) voice->step = STEP_MIN;
639 voice->signal = FALL_OFF(voice->signal) + diff_lookup[voice->step * 16 + ((voice->sample >> 4) & 15)];
640 if (voice->signal > SIGNAL_MAX) voice->signal = SIGNAL_MAX;
641 else if (voice->signal < SIGNAL_MIN) voice->signal = SIGNAL_MIN;
642 LOG(3,("step: %3d signal: %+5d\n", voice->step, voice->signal));
643 voice->head = (voice->head + 1) % DATA_MAX;
644 voice->data[voice->head] = voice->signal;
645 voice->available++;
646 }
647 else
648 {
649 LOG(2,("UPD7759_start_w: $%02x\n", data));
650 /* remove an old timer */
651 if (voice->timer)
652 {
653 timer_remove(voice->timer);
654 voice->timer = 0;
655 }
656 /* bring the chip in sync with the CPU */
657 stream_update(channel[num], 0);
658 /* start a new timer */
659 voice->timer = timer_pulse( TIME_IN_HZ(base_rate), num, UPD7759_dac );
660 voice->signal = 0;
661 voice->step = 0; /* reset the step width */
662 voice->count = 0; /* reset count for the detection of an sample ending */
663 voice->playing = 1; /* this voice is now playing */
664 voice->tail = 0;
665 voice->head = 0;
666 voice->available = 0;
667 }
668 }
669 else
670 {
671 struct UPD7759sample sample;
672
673 /* if !ST is high, do nothing */ /* EHC - 13/08/99 */
674 if (data > 0)
675 return;
676
677 /* bail if the chip is busy */
678 if (voice->playing)
679 return;
680
681 LOG(2,("UPD7759_start_w: %d\n", data));
682
683 /* find a match */
684 if (find_sample(num,sampnum[num],&sample))
685 {
686 /* update the voice */
687 stream_update(channel[num], 0);
688 voice->freq = sample.freq;
689 /* set up the voice to play this sample */
690 voice->playing = 1;
691 voice->base = &memory_region(upd7759_intf->region[num])[sample.offset];
692 voice->sample = 0;
693 /* sample length needs to be doubled (counting nibbles) */
694 voice->count = sample.length * 2;
695
696 /* also reset the chip parameters */
697 voice->step = 0;
698 voice->counter = emulation_rate / 2;
699
700 return;
701 }
702
703 LOG(1,("warning: UPD7759_playing_w() called with invalid number = %08x\n",data));
704 }
705 }
706
707 /************************************************************
708 UPD7759_data_r
709
710 External read data from the UPD7759 memory region based
711 on voice->base. Used in slave mode to retrieve data to
712 stuff into UPD7759_message_w.
713 *************************************************************/
714
UPD7759_data_r(int num,int offs)715 int UPD7759_data_r(int num, int offs)
716 {
717 struct UPD7759voice *voice = updadpcm + num;
718
719 /* If there's no sample rate, do nothing */
720 if (Machine->sample_rate == 0)
721 return 0x00;
722
723 /* range check the numbers */
724 if( num >= upd7759_intf->num )
725 {
726 LOG(1,("error: UPD7759_data_r() called with channel = %d, but only %d channels allocated\n", num, upd7759_intf->num));
727 return 0x00;
728 }
729
730 if ( voice->base == NULL )
731 {
732 LOG(1,("error: UPD7759_data_r() called with channel = %d, but updadpcm[%d].base == NULL\n", num, num));
733 return 0x00;
734 }
735
736 #if VERBOSE
737 if (!(offs&0xff)) LOG(1, ("UPD7759#%d sample offset = $%04x\n", num, offs));
738 #endif
739
740 return voice->base[offs];
741 }
742
743 /************************************************************
744 UPD7759_busy_r
745
746 !BUSY pin:
747 !BUSY outputs the status of the uPD7759. It goes low during
748 speech decode and output operations. When !ST is received,
749 !BUSY goes low. While !BUSY is low, another !ST will not be
750 accepted. In standby mode, !BUSY becomes high impedance. This
751 is an active low output.
752 *************************************************************/
753
UPD7759_busy_r(int num)754 int UPD7759_busy_r (int num)
755 {
756 struct UPD7759voice *voice = updadpcm + num;
757
758 /* If there's no sample rate, return not busy */
759 if ( Machine->sample_rate == 0 )
760 return 1;
761
762 /* range check the numbers */
763 if( num >= upd7759_intf->num )
764 {
765 LOG(1,("error: UPD7759_busy_r() called with channel = %d, but only %d channels allocated\n", num, upd7759_intf->num));
766 return 1;
767 }
768
769 /* bring the chip in sync with the CPU */
770 #ifndef MAME_FASTSOUND
771 stream_update(channel[num], 0);
772 #else
773 {
774 extern int fast_sound;
775 if (!fast_sound) stream_update(channel[num], 0);
776 }
777 #endif
778 if ( voice->playing == 0 )
779 {
780 LOG(1,("uPD7759 not busy\n"));
781 return 1;
782 }
783 else
784 {
785 LOG(1,("uPD7759 busy\n"));
786 return 0;
787 }
788
789 return 1;
790 }
791
792 /************************************************************
793 UPD7759_reset_w
794
795 !RESET pin:
796 The !RESET input initialized the chip. Use !RESET following
797 power-up to abort speech reproduction or to release standby
798 mode. !RESET must remain low at least 12 oscillator clocks.
799 At power-up or when recovering from standby mode, !RESET
800 must remain low at least 12 more clocks after clock
801 oscillation stabilizes.
802 *************************************************************/
803
UPD7759_reset_w(int num,int data)804 void UPD7759_reset_w (int num, int data)
805 {
806 struct UPD7759voice *voice = updadpcm + num;
807
808 /* If there's no sample rate, do nothing */
809 if( Machine->sample_rate == 0 )
810 return;
811
812 /* range check the numbers */
813 if( num >= upd7759_intf->num )
814 {
815 LOG(1,("error: UPD7759_reset_w() called with channel = %d, but only %d channels allocated\n", num, upd7759_intf->num));
816 return;
817 }
818
819 /* if !RESET is high, do nothing */
820 if (data > 0)
821 return;
822
823 /* mark the uPD7759 as NOT PLAYING */
824 /* (Note: do we need to do anything else?) */
825 voice->playing = 0;
826 }
827
828
829 /* helper functions to be used as memory read handler function pointers */
WRITE_HANDLER(UPD7759_0_message_w)830 WRITE_HANDLER( UPD7759_0_message_w ) { UPD7759_message_w(0,data); }
WRITE_HANDLER(UPD7759_0_start_w)831 WRITE_HANDLER( UPD7759_0_start_w ) { UPD7759_start_w(0,data); }
READ_HANDLER(UPD7759_0_busy_r)832 READ_HANDLER( UPD7759_0_busy_r ) { return UPD7759_busy_r(0); }
READ_HANDLER(UPD7759_0_data_r)833 READ_HANDLER( UPD7759_0_data_r ) { return UPD7759_data_r(0,offset); }
READ_HANDLER(UPD7759_1_data_r)834 READ_HANDLER( UPD7759_1_data_r ) { return UPD7759_data_r(1,offset); }
835