Debian Codegraph @

src/engine/platform/sound/ymz280b.cpp

raw
1 // license:BSD-3-Clause
2 // copyright-holders:Aaron Giles
3 /*
4
5 Yamaha YMZ280B driver
6 by Aaron Giles
7
8 YMZ280B 8-Channel PCMD8 PCM/ADPCM Decoder
9
10 Features as listed in LSI-4MZ280B3 data sheet:
11 Voice data stored in external memory can be played back simultaneously for up to eight voices
12 Voice data format can be selected from 4-bit ADPCM, 8-bit PCM and 16-bit PCM
13 Control of voice data external memory
14 Up to 16M bytes of ROM or SRAM (x 8 bits, access time 150ms max) can be connected
15 Continuous access is possible
16 Loop playback between selective addresses is possible
17 Voice data playback frequency control
18 4-bit ADPCM ................ 0.172 to 44.1kHz in 256 steps
19 8-bit PCM, 16-bit PCM ...... 0.172 to 88.2kHz in 512 steps
20 256 steps total level and 16 steps panpot can be set
21 Voice signal is output in stereo 16-bit 2's complement MSB-first format
22
23 TODO:
24 - Is memory handling 100% correct? At the moment, Konami firebeat.c is the only
25 hardware currently emulated that uses external handlers.
26 It also happens to be the only one using 16-bit PCM.
27
28 Some other drivers (eg. bishi.cpp, bfm_sc4/5.cpp) also use ROM readback.
29
30 */
31
32 #include "ymz280b.h"
33 #include <assert.h>
34 #include <stdio.h>
35 #include <string.h>
36
37 #define MAX_SAMPLE_CHUNK 10000
38
39 static constexpr unsigned FRAC_BITS = 8;
40 static constexpr s32 FRAC_ONE = 1 << FRAC_BITS;
41
42 /* step size index shift table */
43 static constexpr int index_scale[8] = { 0x0e6, 0x0e6, 0x0e6, 0x0e6, 0x133, 0x199, 0x200, 0x266 };
44
45 /* lookup table for the precomputed difference */
46 static int diff_lookup[16];
47
48
49 void ymz280b_device::update_step(struct YMZ280BVoice *voice)
50 {
51 int frequency;
52
53 /* compute the frequency */
54 if (voice->mode == 1)
55 frequency = voice->fnum & 0x0ff;
56 else
57 frequency = voice->fnum & 0x1ff;
58 voice->output_step = frequency + 1; // ((fnum + 1) * (input clock / 384)) / 256
59 }
60
61
62 void ymz280b_device::update_volumes(struct YMZ280BVoice *voice)
63 {
64 if (voice->pan == 8)
65 {
66 voice->output_left = voice->level;
67 voice->output_right = voice->level;
68 }
69 else if (voice->pan < 8)
70 {
71 voice->output_left = voice->level;
72
73 /* pan 1 is hard-left, what's pan 0? for now assume same as pan 1 */
74 voice->output_right = (voice->pan == 0) ? 0 : voice->level * (voice->pan - 1) / 7;
75 }
76 else
77 {
78 voice->output_left = voice->level * (15 - voice->pan) / 7;
79 voice->output_right = voice->level;
80 }
81 }
82
83 /**********************************************************************************************
84
85 compute_tables -- compute the difference tables
86
87 ***********************************************************************************************/
88
89 static void compute_tables()
90 {
91 /* loop over all nibbles and compute the difference */
92 for (int nib = 0; nib < 16; nib++)
93 {
94 int value = (nib & 0x07) * 2 + 1;
95 diff_lookup[nib] = (nib & 0x08) ? -value : value;
96 }
97 }
98
99
100
101 /**********************************************************************************************
102
103 generate_adpcm -- general ADPCM decoding routine
104
105 ***********************************************************************************************/
106
107 int ymz280b_device::generate_adpcm(struct YMZ280BVoice *voice, s16 *buffer, int samples)
108 {
109 u32 position = voice->position;
110 int signal = voice->signal;
111 int step = voice->step;
112 int val;
113
114 /* two cases: first cases is non-looping */
115 if (!voice->looping)
116 {
117 /* loop while we still have samples to generate */
118 while (samples)
119 {
120 /* compute the new amplitude and update the current step */
121 val = m_ext_mem[position / 2] >> ((~position & 1) << 2);
122 signal += (step * diff_lookup[val & 15]) / 8;
123
124 /* clamp to the maximum */
125 if (signal > 32767)
126 signal = 32767;
127 else if (signal < -32768)
128 signal = -32768;
129
130 /* adjust the step size and clamp */
131 step = (step * index_scale[val & 7]) >> 8;
132 if (step > 0x6000)
133 step = 0x6000;
134 else if (step < 0x7f)
135 step = 0x7f;
136
137 /* output to the buffer, scaling by the volume */
138 *buffer++ = signal;
139 samples--;
140
141 /* next! */
142 position++;
143 if (position >= voice->stop)
144 {
145 voice->ended = true;
146 break;
147 }
148 }
149 }
150
151 /* second case: looping */
152 else
153 {
154 /* loop while we still have samples to generate */
155 while (samples)
156 {
157 /* compute the new amplitude and update the current step */
158 val = m_ext_mem[position / 2] >> ((~position & 1) << 2);
159 signal += (step * diff_lookup[val & 15]) / 8;
160
161 /* clamp to the maximum */
162 if (signal > 32767)
163 signal = 32767;
164 else if (signal < -32768)
165 signal = -32768;
166
167 /* adjust the step size and clamp */
168 step = (step * index_scale[val & 7]) >> 8;
169 if (step > 0x6000)
170 step = 0x6000;
171 else if (step < 0x7f)
172 step = 0x7f;
173
174 /* output to the buffer, scaling by the volume */
175 *buffer++ = signal;
176 samples--;
177
178 /* next! */
179 position++;
180 if (position == voice->loop_start && voice->loop_count == 0)
181 {
182 voice->loop_signal = signal;
183 voice->loop_step = step;
184 }
185 if (position >= voice->loop_end)
186 {
187 if (voice->keyon)
188 {
189 position = voice->loop_start;
190 signal = voice->loop_signal;
191 step = voice->loop_step;
192 voice->loop_count++;
193 }
194 }
195 if (position >= voice->stop)
196 {
197 voice->ended = true;
198 break;
199 }
200 }
201 }
202
203 /* update the parameters */
204 voice->position = position;
205 voice->signal = signal;
206 voice->step = step;
207
208 return samples;
209 }
210
211
212
213 /**********************************************************************************************
214
215 generate_pcm8 -- general 8-bit PCM decoding routine
216
217 ***********************************************************************************************/
218
219 int ymz280b_device::generate_pcm8(struct YMZ280BVoice *voice, s16 *buffer, int samples)
220 {
221 u32 position = voice->position;
222 int val;
223
224 /* two cases: first cases is non-looping */
225 if (!voice->looping)
226 {
227 /* loop while we still have samples to generate */
228 while (samples)
229 {
230 /* fetch the current value */
231 val = m_ext_mem[position / 2];
232
233 /* output to the buffer, scaling by the volume */
234 *buffer++ = (s8)val * 256;
235 samples--;
236
237 /* next! */
238 position += 2;
239 if (position >= voice->stop)
240 {
241 voice->ended = true;
242 break;
243 }
244 }
245 }
246
247 /* second case: looping */
248 else
249 {
250 /* loop while we still have samples to generate */
251 while (samples)
252 {
253 /* fetch the current value */
254 val = m_ext_mem[position / 2];
255
256 /* output to the buffer, scaling by the volume */
257 *buffer++ = (s8)val * 256;
258 samples--;
259
260 /* next! */
261 position += 2;
262 if (position >= voice->loop_end)
263 {
264 if (voice->keyon)
265 position = voice->loop_start;
266 }
267 if (position >= voice->stop)
268 {
269 voice->ended = true;
270 break;
271 }
272 }
273 }
274
275 /* update the parameters */
276 voice->position = position;
277
278 return samples;
279 }
280
281
282
283 /**********************************************************************************************
284
285 generate_pcm16 -- general 16-bit PCM decoding routine
286
287 ***********************************************************************************************/
288
289 // according to this core, it should be little-endian.
290 // but it's big-endian in VGMPlay...
291 int ymz280b_device::generate_pcm16(struct YMZ280BVoice *voice, s16 *buffer, int samples)
292 {
293 u32 position = voice->position;
294 int val;
295
296 /* two cases: first cases is non-looping */
297 if (!voice->looping)
298 {
299 /* loop while we still have samples to generate */
300 while (samples)
301 {
302 /* fetch the current value */
303 val = (s16)((m_ext_mem[position / 2 + 0] << 8) + m_ext_mem[position / 2 + 1]);
304
305 /* output to the buffer, scaling by the volume */
306 *buffer++ = val;
307 samples--;
308
309 /* next! */
310 position += 4;
311 if (position >= voice->stop)
312 {
313 voice->ended = true;
314 break;
315 }
316 }
317 }
318
319 /* second case: looping */
320 else
321 {
322 /* loop while we still have samples to generate */
323 while (samples)
324 {
325 /* fetch the current value */
326 val = (s16)((m_ext_mem[position / 2 + 0] << 8) + m_ext_mem[position / 2 + 1]);
327
328 /* output to the buffer, scaling by the volume */
329 *buffer++ = val;
330 samples--;
331
332 /* next! */
333 position += 4;
334 if (position >= voice->loop_end)
335 {
336 if (voice->keyon)
337 position = voice->loop_start;
338 }
339 if (position >= voice->stop)
340 {
341 voice->ended = true;
342 break;
343 }
344 }
345 }
346
347 /* update the parameters */
348 voice->position = position;
349
350 return samples;
351 }
352
353
354
355
356 //-------------------------------------------------
357 // sound_stream_update - handle a stream update
358 //-------------------------------------------------
359
360 void ymz280b_device::sound_stream_update(s16 **outputs, int samples)
361 {
362 int v;
363
364 /* loop over voices */
365 for (v = 0; v < 8; v++)
366 {
367 struct YMZ280BVoice *voice = &m_voice[v];
368 s16 prev = voice->last_sample;
369 s16 curr = voice->curr_sample;
370 s16 *curr_data = m_scratch.get();
371 s16 *ldest = outputs[v*2];
372 s16 *rdest = outputs[v*2+1];
373 s32 sampindex = 0;
374 u32 new_samples, samples_left;
375 u32 final_pos;
376 int remaining = samples;
377 int lvol = voice->output_left;
378 int rvol = voice->output_right;
379
380 /* quick out if we're not playing and we're at 0 */
381 if (!voice->playing && curr == 0 && prev == 0)
382 {
383 memset(ldest, 0, samples * sizeof(s16));
384 memset(rdest, 0, samples * sizeof(s16));
385 /* make sure next sound plays immediately */
386 voice->output_pos = FRAC_ONE;
387 continue;
388 }
389
390 /* finish off the current sample */
391 /* interpolate */
392 while (remaining > 0 && voice->output_pos < FRAC_ONE)
393 {
394 s32 interp_sample = ((s32(prev) * (FRAC_ONE - voice->output_pos)) + (s32(curr) * voice->output_pos)) >> FRAC_BITS;
395 ldest[sampindex] = (s16)(interp_sample * lvol / 256);
396 rdest[sampindex] = (s16)(interp_sample * rvol / 256);
397 sampindex++;
398 voice->output_pos += voice->output_step;
399 remaining--;
400 }
401
402 /* if we're over, continue; otherwise, we're done */
403 if (voice->output_pos >= FRAC_ONE)
404 voice->output_pos -= FRAC_ONE;
405 else
406 continue;
407
408 /* compute how many new samples we need */
409 final_pos = voice->output_pos + remaining * voice->output_step;
410 new_samples = (final_pos + FRAC_ONE) >> FRAC_BITS;
411 if (new_samples > MAX_SAMPLE_CHUNK)
412 new_samples = MAX_SAMPLE_CHUNK;
413 samples_left = new_samples;
414
415 /* generate them into our buffer */
416 switch (voice->playing << 7 | voice->mode)
417 {
418 case 0x81: samples_left = generate_adpcm(voice, m_scratch.get(), new_samples); break;
419 case 0x82: samples_left = generate_pcm8(voice, m_scratch.get(), new_samples); break;
420 case 0x83: samples_left = generate_pcm16(voice, m_scratch.get(), new_samples); break;
421 default: samples_left = 0; memset(m_scratch.get(), 0, new_samples * sizeof(m_scratch[0])); break;
422 }
423
424 if (samples_left || voice->ended)
425 {
426 voice->ended = false;
427
428 /* if there are leftovers, ramp back to 0 */
429 int base = new_samples - samples_left;
430 int t = (base == 0) ? curr : m_scratch[base - 1];
431 for (u32 i = 0; i < samples_left; i++)
432 {
433 if (t < 0) t = -((-t * 15) >> 4);
434 else if (t > 0) t = (t * 15) >> 4;
435 m_scratch[base + i] = t;
436 }
437
438 /* if we hit the end and IRQs are enabled, signal it */
439 if (base != 0)
440 {
441 voice->playing = 0;
442
443 /* set update_irq_state_timer. IRQ is signaled on next CPU execution. */
444 voice->irq_schedule = 1;
445 }
446 }
447
448 /* advance forward one sample */
449 prev = curr;
450 curr = *curr_data++;
451
452 /* then sample-rate convert with linear interpolation */
453 while (remaining > 0)
454 {
455 /* interpolate */
456 while (remaining > 0 && voice->output_pos < FRAC_ONE)
457 {
458 int interp_sample = ((s32(prev) * (FRAC_ONE - voice->output_pos)) + (s32(curr) * voice->output_pos)) >> FRAC_BITS;
459 ldest[sampindex] = (s16)(interp_sample * lvol / 256);
460 rdest[sampindex] = (s16)(interp_sample * rvol / 256);
461 sampindex++;
462 voice->output_pos += voice->output_step;
463 remaining--;
464 }
465
466 /* if we're over, grab the next samples */
467 if (voice->output_pos >= FRAC_ONE)
468 {
469 voice->output_pos -= FRAC_ONE;
470 prev = curr;
471 curr = *curr_data++;
472 }
473 }
474
475 /* remember the last samples */
476 voice->last_sample = prev;
477 voice->curr_sample = curr;
478 }
479 }
480
481
482
483 //-------------------------------------------------
484 // device_start - device-specific startup
485 //-------------------------------------------------
486
487 void ymz280b_device::device_start(u8 *ext_mem)
488 {
489 m_ext_mem = ext_mem;
490
491 /* compute ADPCM tables */
492 compute_tables();
493
494 /* allocate memory */
495 assert(MAX_SAMPLE_CHUNK < 0x10000);
496 m_scratch = std::make_unique<s16[]>(MAX_SAMPLE_CHUNK);
497
498 for (auto & elem : m_voice)
499 {
500 update_step(&elem);
501 }
502 }
503
504 //-------------------------------------------------
505 // device_reset - device-specific reset
506 //-------------------------------------------------
507
508 void ymz280b_device::device_reset()
509 {
510 /* initial clear registers */
511 for (int i = 0xff; i >= 0; i--)
512 {
513 m_current_register = i;
514 write_to_register(0);
515 }
516
517 m_current_register = 0;
518 m_status_register = 0;
519 m_ext_mem_address = 0;
520
521 /* clear other voice parameters */
522 for (auto &elem : m_voice)
523 {
524 struct YMZ280BVoice *voice = &elem;
525
526 voice->curr_sample = 0;
527 voice->last_sample = 0;
528 voice->output_pos = FRAC_ONE;
529 voice->playing = 0;
530 }
531 }
532
533 /**********************************************************************************************
534
535 write_to_register -- handle a write to the current register
536
537 ***********************************************************************************************/
538
539 void ymz280b_device::write_to_register(int data)
540 {
541 struct YMZ280BVoice *voice;
542 int i;
543
544 /* lower registers follow a pattern */
545 if (m_current_register < 0x80)
546 {
547 voice = &m_voice[(m_current_register >> 2) & 7];
548
549 switch (m_current_register & 0xe3)
550 {
551 case 0x00: /* pitch low 8 bits */
552 voice->fnum = (voice->fnum & 0x100) | (data & 0xff);
553 update_step(voice);
554 break;
555
556 case 0x01: /* pitch upper 1 bit, loop, key on, mode */
557 voice->fnum = (voice->fnum & 0xff) | ((data & 0x01) << 8);
558 voice->looping = (data & 0x10) >> 4;
559 if ((data & 0x60) == 0) data &= 0x7f; /* ignore mode setting and set to same state as KON=0 */
560 else voice->mode = (data & 0x60) >> 5;
561 if (!voice->keyon && (data & 0x80) && m_keyon_enable)
562 {
563 voice->playing = 1;
564 voice->position = voice->start;
565 voice->signal = voice->loop_signal = 0;
566 voice->step = voice->loop_step = 0x7f;
567 voice->loop_count = 0;
568
569 /* if update_irq_state_timer is set, cancel it. */
570 voice->irq_schedule = 0;
571 }
572 else if (voice->keyon && !(data & 0x80))
573 {
574 voice->playing = 0;
575
576 /* if update_irq_state_timer is set, cancel it. */
577 voice->irq_schedule = 0;
578 }
579 voice->keyon = (data & 0x80) >> 7;
580 update_step(voice);
581 break;
582
583 case 0x02: /* total level */
584 voice->level = data;
585 update_volumes(voice);
586 break;
587
588 case 0x03: /* pan */
589 voice->pan = data & 0x0f;
590 update_volumes(voice);
591 break;
592
593 case 0x20: /* start address high */
594 voice->start = (voice->start & (0x00ffff << 1)) | (data << 17);
595 break;
596
597 case 0x21: /* loop start address high */
598 voice->loop_start = (voice->loop_start & (0x00ffff << 1)) | (data << 17);
599 break;
600
601 case 0x22: /* loop end address high */
602 voice->loop_end = (voice->loop_end & (0x00ffff << 1)) | (data << 17);
603 break;
604
605 case 0x23: /* stop address high */
606 voice->stop = (voice->stop & (0x00ffff << 1)) | (data << 17);
607 break;
608
609 case 0x40: /* start address middle */
610 voice->start = (voice->start & (0xff00ff << 1)) | (data << 9);
611 break;
612
613 case 0x41: /* loop start address middle */
614 voice->loop_start = (voice->loop_start & (0xff00ff << 1)) | (data << 9);
615 break;
616
617 case 0x42: /* loop end address middle */
618 voice->loop_end = (voice->loop_end & (0xff00ff << 1)) | (data << 9);
619 break;
620
621 case 0x43: /* stop address middle */
622 voice->stop = (voice->stop & (0xff00ff << 1)) | (data << 9);
623 break;
624
625 case 0x60: /* start address low */
626 voice->start = (voice->start & (0xffff00 << 1)) | (data << 1);
627 break;
628
629 case 0x61: /* loop start address low */
630 voice->loop_start = (voice->loop_start & (0xffff00 << 1)) | (data << 1);
631 break;
632
633 case 0x62: /* loop end address low */
634 voice->loop_end = (voice->loop_end & (0xffff00 << 1)) | (data << 1);
635 break;
636
637 case 0x63: /* stop address low */
638 voice->stop = (voice->stop & (0xffff00 << 1)) | (data << 1);
639 break;
640
641 default:
642 if (data != 0)
643 printf("YMZ280B: unknown register write %02X = %02X\n", m_current_register, data);
644 break;
645 }
646 }
647
648 /* upper registers are special */
649 else
650 {
651 switch (m_current_register)
652 {
653 /* DSP related (not implemented yet) */
654 case 0x80: // d0-2: DSP Rch, d3: enable Rch (0: yes, 1: no), d4-6: DSP Lch, d7: enable Lch (0: yes, 1: no)
655 case 0x81: // d0: enable control of $82 (0: yes, 1: no)
656 case 0x82: // DSP data
657 //printf("YMZ280B: DSP register write %02X = %02X\n", m_current_register, data);
658 break;
659
660 case 0x84: /* ROM readback / RAM write (high) */
661 m_ext_mem_address_hi = data << 16;
662 break;
663
664 case 0x85: /* ROM readback / RAM write (middle) */
665 m_ext_mem_address_mid = data << 8;
666 break;
667
668 case 0x86: /* ROM readback / RAM write (low) -> update latch */
669 m_ext_mem_address = m_ext_mem_address_hi | m_ext_mem_address_mid | data;
670 if (m_ext_mem_enable)
671 m_ext_readlatch = m_ext_mem[m_ext_mem_address];
672 break;
673
674 case 0x87: /* RAM write */
675 if (m_ext_mem_enable)
676 {
677 m_ext_mem[m_ext_mem_address] = data;
678 m_ext_mem_address = (m_ext_mem_address + 1) & 0xffffff;
679 }
680 break;
681
682 case 0xfe: /* IRQ mask */
683 m_irq_mask = data;
684 break;
685
686 case 0xff: /* IRQ enable, test, etc */
687 m_ext_mem_enable = (data & 0x40) >> 6;
688 m_irq_enable = (data & 0x10) >> 4;
689
690 if (m_keyon_enable && !(data & 0x80))
691 {
692 for (i = 0; i < 8; i++)
693 {
694 m_voice[i].playing = 0;
695
696 /* if update_irq_state_timer is set, cancel it. */
697 m_voice[i].irq_schedule = 0;
698 }
699 }
700 else if (!m_keyon_enable && (data & 0x80))
701 {
702 for (i = 0; i < 8; i++)
703 {
704 if (m_voice[i].keyon && m_voice[i].looping)
705 m_voice[i].playing = 1;
706 }
707 }
708 m_keyon_enable = (data & 0x80) >> 7;
709 break;
710
711 default:
712 if (data != 0)
713 printf("YMZ280B: unknown register write %02X = %02X\n", m_current_register, data);
714 break;
715 }
716 }
717 }
718
719
720
721 /**********************************************************************************************
722
723 compute_status -- determine the status bits
724
725 ***********************************************************************************************/
726
727 int ymz280b_device::compute_status()
728 {
729 u8 result;
730
731 result = m_status_register;
732
733 /* clear the IRQ state */
734 m_status_register = 0;
735 return result;
736 }
737
738
739
740 /**********************************************************************************************
741
742 read/write -- handle external accesses
743
744 ***********************************************************************************************/
745
746 u8 ymz280b_device::read(offs_t offset)
747 {
748 if ((offset & 1) == 0)
749 {
750 if (!m_ext_mem_enable)
751 return 0xff;
752
753 /* read from external memory */
754 u8 ret = m_ext_readlatch;
755 m_ext_readlatch = m_ext_mem[m_ext_mem_address];
756 m_ext_mem_address = (m_ext_mem_address + 1) & 0xffffff;
757 return ret;
758 }
759 else
760 return compute_status();
761 }
762
763
764 void ymz280b_device::write(offs_t offset, u8 data)
765 {
766 if ((offset & 1) == 0)
767 m_current_register = data;
768 else
769 {
770 write_to_register(data);
771 }
772 }
773
774 ymz280b_device::ymz280b_device()
775 : m_current_register(0)
776 , m_status_register(0)
777 , m_irq_mask(0)
778 , m_irq_enable(0)
779 , m_keyon_enable(0)
780 , m_ext_mem_enable(0)
781 , m_ext_readlatch(0)
782 , m_ext_mem_address_hi(0)
783 , m_ext_mem_address_mid(0)
784 , m_ext_mem_address(0)
785 {
786 memset(m_voice, 0, sizeof(m_voice));
787 }