Debian Codegraph @

src/engine/platform/sound/ymfm/ymfm_pcm.cpp

raw
1 // BSD 3-Clause License
2 //
3 // Copyright (c) 2021, Aaron Giles
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this
10 // list of conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice,
13 // this list of conditions and the following disclaimer in the documentation
14 // and/or other materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 #include "ymfm_pcm.h"
32 #include "ymfm_fm.h"
33 #include "ymfm_fm.ipp"
34
35 namespace ymfm
36 {
37
38 //*********************************************************
39 // PCM REGISTERS
40 //*********************************************************
41
42 //-------------------------------------------------
43 // reset - reset the register state
44 //-------------------------------------------------
45
46 void pcm_registers::reset()
47 {
48 std::fill_n(&m_regdata[0], REGISTERS, 0);
49 m_regdata[0xf8] = 0x1b;
50 }
51
52
53 //-------------------------------------------------
54 // save_restore - save or restore the data
55 //-------------------------------------------------
56
57 void pcm_registers::save_restore(ymfm_saved_state &state)
58 {
59 state.save_restore(m_regdata);
60 }
61
62
63 //-------------------------------------------------
64 // cache_channel_data - update the cache with
65 // data from the registers
66 //-------------------------------------------------
67
68 void pcm_registers::cache_channel_data(uint32_t choffs, pcm_cache &cache)
69 {
70 // compute step from octave and fnumber; the math here implies
71 // a .18 fraction but .16 should be perfectly fine
72 int32_t octave = int8_t(ch_octave(choffs) << 4) >> 4;
73 uint32_t fnum = ch_fnumber(choffs);
74 cache.step = ((0x400 | fnum) << (octave + 7)) >> 2;
75
76 // total level is computed as a .10 value for interpolation
77 cache.total_level = ch_total_level(choffs) << 10;
78
79 // compute panning values in terms of envelope attenuation
80 int32_t panpot = int8_t(ch_panpot(choffs) << 4) >> 4;
81 if (panpot >= 0)
82 {
83 cache.pan_left = (panpot == 7) ? 0x3ff : 0x20 * panpot;
84 cache.pan_right = 0;
85 }
86 else if (panpot >= -7)
87 {
88 cache.pan_left = 0;
89 cache.pan_right = (panpot == -7) ? 0x3ff : -0x20 * panpot;
90 }
91 else
92 cache.pan_left = cache.pan_right = 0x3ff;
93
94 // determine the LFO stepping value; this how much to add to a running
95 // x.18 value for the LFO; steps were derived from frequencies in the
96 // manual and come out very close with these values
97 static const uint8_t s_lfo_steps[8] = { 1, 12, 19, 25, 31, 35, 37, 42 };
98 cache.lfo_step = s_lfo_steps[ch_lfo_speed(choffs)];
99
100 // AM LFO depth values, derived from the manual; note each has at most
101 // 2 bits to make the "multiply" easy in hardware
102 static const uint8_t s_am_depth[8] = { 0, 0x14, 0x20, 0x28, 0x30, 0x40, 0x50, 0x80 };
103 cache.am_depth = s_am_depth[ch_am_depth(choffs)];
104
105 // PM LFO depth values; these are converted from the manual's cents values
106 // into f-numbers; the computations come out quite cleanly so pretty sure
107 // these are correct
108 static const uint8_t s_pm_depth[8] = { 0, 2, 3, 4, 6, 12, 24, 48 };
109 cache.pm_depth = s_pm_depth[ch_vibrato(choffs)];
110
111 // 4-bit sustain level, but 15 means 31 so effectively 5 bits
112 cache.eg_sustain = ch_sustain_level(choffs);
113 cache.eg_sustain |= (cache.eg_sustain + 1) & 0x10;
114 cache.eg_sustain <<= 5;
115
116 // compute the key scaling correction factor; 15 means don't do any correction
117 int32_t correction = ch_rate_correction(choffs);
118 if (correction == 15)
119 correction = 0;
120 else
121 correction = (octave + correction) * 2 + bitfield(fnum, 9);
122
123 // compute the envelope generator rates
124 cache.eg_rate[EG_ATTACK] = effective_rate(ch_attack_rate(choffs), correction);
125 cache.eg_rate[EG_DECAY] = effective_rate(ch_decay_rate(choffs), correction);
126 cache.eg_rate[EG_SUSTAIN] = effective_rate(ch_sustain_rate(choffs), correction);
127 cache.eg_rate[EG_RELEASE] = effective_rate(ch_release_rate(choffs), correction);
128 cache.eg_rate[EG_REVERB] = 5;
129
130 // if damping is on, override some things; essentially decay at a hardcoded
131 // rate of 48 until -12db (0x80), then at maximum rate for the rest
132 if (ch_damp(choffs) != 0)
133 {
134 cache.eg_rate[EG_DECAY] = 48;
135 cache.eg_rate[EG_SUSTAIN] = 63;
136 cache.eg_rate[EG_RELEASE] = 63;
137 cache.eg_sustain = 0x80;
138 }
139 }
140
141
142 //-------------------------------------------------
143 // effective_rate - return the effective rate,
144 // clamping and applying corrections as needed
145 //-------------------------------------------------
146
147 uint32_t pcm_registers::effective_rate(uint32_t raw, uint32_t correction)
148 {
149 // raw rates of 0 and 15 just pin to min/max
150 if (raw == 0)
151 return 0;
152 if (raw == 15)
153 return 63;
154
155 // otherwise add the correction and clamp to range
156 return clamp(raw * 4 + correction, 0, 63);
157 }
158
159
160
161 //*********************************************************
162 // PCM CHANNEL
163 //*********************************************************
164
165 //-------------------------------------------------
166 // pcm_channel - constructor
167 //-------------------------------------------------
168
169 pcm_channel::pcm_channel(pcm_engine &owner, uint32_t choffs) :
170 m_choffs(choffs),
171 m_baseaddr(0),
172 m_endpos(0),
173 m_looppos(0),
174 m_curpos(0),
175 m_nextpos(0),
176 m_lfo_counter(0),
177 m_eg_state(EG_RELEASE),
178 m_env_attenuation(0x3ff),
179 m_total_level(0x7f << 10),
180 m_format(0),
181 m_key_state(0),
182 m_regs(owner.regs()),
183 m_owner(owner)
184 {
185 }
186
187
188 //-------------------------------------------------
189 // reset - reset the channel state
190 //-------------------------------------------------
191
192 void pcm_channel::reset()
193 {
194 m_baseaddr = 0;
195 m_endpos = 0;
196 m_looppos = 0;
197 m_curpos = 0;
198 m_nextpos = 0;
199 m_lfo_counter = 0;
200 m_eg_state = EG_RELEASE;
201 m_env_attenuation = 0x3ff;
202 m_total_level = 0x7f << 10;
203 m_format = 0;
204 m_key_state = 0;
205 }
206
207
208 //-------------------------------------------------
209 // save_restore - save or restore the data
210 //-------------------------------------------------
211
212 void pcm_channel::save_restore(ymfm_saved_state &state)
213 {
214 state.save_restore(m_baseaddr);
215 state.save_restore(m_endpos);
216 state.save_restore(m_looppos);
217 state.save_restore(m_curpos);
218 state.save_restore(m_nextpos);
219 state.save_restore(m_lfo_counter);
220 state.save_restore(m_eg_state);
221 state.save_restore(m_env_attenuation);
222 state.save_restore(m_total_level);
223 state.save_restore(m_format);
224 state.save_restore(m_key_state);
225 }
226
227
228 //-------------------------------------------------
229 // prepare - prepare for clocking
230 //-------------------------------------------------
231
232 bool pcm_channel::prepare()
233 {
234 // cache the data
235 m_regs.cache_channel_data(m_choffs, m_cache);
236
237 // clock the key state
238 if ((m_key_state & KEY_PENDING) != 0)
239 {
240 uint8_t oldstate = m_key_state;
241 m_key_state = (m_key_state >> 1) & KEY_ON;
242 if (((oldstate ^ m_key_state) & KEY_ON) != 0)
243 {
244 if ((m_key_state & KEY_ON) != 0)
245 start_attack();
246 else
247 start_release();
248 }
249 }
250
251 // set the total level directly if not interpolating
252 if (m_regs.ch_level_direct(m_choffs))
253 m_total_level = m_cache.total_level;
254
255 // we're active until we're quiet after the release
256 return (m_eg_state < EG_RELEASE || m_env_attenuation < EG_QUIET);
257 }
258
259
260 //-------------------------------------------------
261 // clock - master clocking function
262 //-------------------------------------------------
263
264 void pcm_channel::clock(uint32_t env_counter)
265 {
266 // clock the LFO, which is an x.18 value incremented based on the
267 // LFO speed value
268 m_lfo_counter += m_cache.lfo_step;
269
270 // clock the envelope
271 clock_envelope(env_counter);
272
273 // determine the step after applying vibrato
274 uint32_t step = m_cache.step;
275 if (m_cache.pm_depth != 0)
276 {
277 // shift the LFO by 1/4 cycle for PM so that it starts at 0
278 uint32_t lfo_shifted = m_lfo_counter + (1 << 16);
279 int32_t lfo_value = bitfield(lfo_shifted, 10, 7);
280 if (bitfield(lfo_shifted, 17) != 0)
281 lfo_value ^= 0x7f;
282 lfo_value -= 0x40;
283 step += (lfo_value * int32_t(m_cache.pm_depth)) >> 7;
284 }
285
286 // advance the sample step and loop as needed
287 m_curpos = m_nextpos;
288 m_nextpos = m_curpos + step;
289 if (m_nextpos >= m_endpos)
290 m_nextpos += m_looppos - m_endpos;
291
292 // interpolate total level if needed
293 if (m_total_level != m_cache.total_level)
294 {
295 // max->min volume takes 156.4ms, or pretty close to 19/1024 per 44.1kHz sample
296 // min->max volume is half that, so advance by 38/1024 per sample
297 if (m_total_level < m_cache.total_level)
298 m_total_level = std::min<int32_t>(m_total_level + 19, m_cache.total_level);
299 else
300 m_total_level = std::max<int32_t>(m_total_level - 38, m_cache.total_level);
301 }
302 }
303
304
305 //-------------------------------------------------
306 // output - return the computed output value, with
307 // panning applied
308 //-------------------------------------------------
309
310 void pcm_channel::output(output_data &output) const
311 {
312 m_output[0] = m_output[1] = m_output[2] = m_output[3] = 0;
313 // early out if the envelope is effectively off
314 uint32_t envelope = m_env_attenuation;
315 if (envelope > EG_QUIET)
316 return;
317
318 // add in LFO AM modulation
319 if (m_cache.am_depth != 0)
320 {
321 uint32_t lfo_value = bitfield(m_lfo_counter, 10, 7);
322 if (bitfield(m_lfo_counter, 17) != 0)
323 lfo_value ^= 0x7f;
324 envelope += (lfo_value * m_cache.am_depth) >> 7;
325 }
326
327 // add in the current interpolated total level value, which is a .10
328 // value shifted left by 2
329 envelope += m_total_level >> 8;
330
331 // add in panning effect and clamp
332 uint32_t lenv = std::min<uint32_t>(envelope + m_cache.pan_left, 0x3ff);
333 uint32_t renv = std::min<uint32_t>(envelope + m_cache.pan_right, 0x3ff);
334
335 // convert to volume as a .11 fraction
336 int32_t lvol = attenuation_to_volume(lenv << 2);
337 int32_t rvol = attenuation_to_volume(renv << 2);
338
339 // fetch current sample and add
340 int16_t sample = fetch_sample();
341 int32_t outl = (lvol * sample) >> 15;
342 int32_t outr = (rvol * sample) >> 15;
343 uint32_t outnum = m_regs.ch_output_channel(m_choffs) * 2;
344 output.data[outnum + 0] += outl;
345 output.data[outnum + 1] += outr;
346 m_output[outnum + 0] = outl;
347 m_output[outnum + 1] = outr;
348 }
349
350
351 //-------------------------------------------------
352 // keyonoff - signal key on/off
353 //-------------------------------------------------
354
355 void pcm_channel::keyonoff(bool on)
356 {
357 // mark the key state as pending
358 m_key_state |= KEY_PENDING | (on ? KEY_PENDING_ON : 0);
359
360 // don't log masked channels
361 if ((m_key_state & (KEY_PENDING_ON | KEY_ON)) == KEY_PENDING_ON && ((debug::GLOBAL_PCM_CHANNEL_MASK >> m_choffs) & 1) != 0)
362 {
363 debug::log_keyon("KeyOn PCM-%02d: num=%3d oct=%2d fnum=%03X level=%02X%c ADSR=%X/%X/%X/%X SL=%X",
364 m_choffs,
365 m_regs.ch_wave_table_num(m_choffs),
366 int8_t(m_regs.ch_octave(m_choffs) << 4) >> 4,
367 m_regs.ch_fnumber(m_choffs),
368 m_regs.ch_total_level(m_choffs),
369 m_regs.ch_level_direct(m_choffs) ? '!' : '/',
370 m_regs.ch_attack_rate(m_choffs),
371 m_regs.ch_decay_rate(m_choffs),
372 m_regs.ch_sustain_rate(m_choffs),
373 m_regs.ch_release_rate(m_choffs),
374 m_regs.ch_sustain_level(m_choffs));
375
376 if (m_regs.ch_rate_correction(m_choffs) != 15)
377 debug::log_keyon(" RC=%X", m_regs.ch_rate_correction(m_choffs));
378
379 if (m_regs.ch_pseudo_reverb(m_choffs) != 0)
380 debug::log_keyon(" %s", "REV");
381 if (m_regs.ch_damp(m_choffs) != 0)
382 debug::log_keyon(" %s", "DAMP");
383
384 if (m_regs.ch_vibrato(m_choffs) != 0 || m_regs.ch_am_depth(m_choffs) != 0)
385 {
386 if (m_regs.ch_vibrato(m_choffs) != 0)
387 debug::log_keyon(" VIB=%d", m_regs.ch_vibrato(m_choffs));
388 if (m_regs.ch_am_depth(m_choffs) != 0)
389 debug::log_keyon(" AM=%d", m_regs.ch_am_depth(m_choffs));
390 debug::log_keyon(" LFO=%d", m_regs.ch_lfo_speed(m_choffs));
391 }
392 debug::log_keyon("%s", "\n");
393 }
394 }
395
396
397 //-------------------------------------------------
398 // load_wavetable - load a wavetable by fetching
399 // its data from external memory
400 //-------------------------------------------------
401
402 void pcm_channel::load_wavetable()
403 {
404 // determine the address of the wave table header
405 uint32_t wavnum = m_regs.ch_wave_table_num(m_choffs);
406 uint32_t wavheader = 12 * wavnum;
407
408 // above 384 it may be in a different bank
409 if (wavnum >= 384)
410 {
411 uint32_t bank = m_regs.wave_table_header();
412 if (bank != 0)
413 wavheader = 512*1024 * bank + (wavnum - 384) * 12;
414 }
415
416 // fetch the 22-bit base address and 2-bit format
417 uint8_t byte = read_pcm(wavheader + 0);
418 m_format = bitfield(byte, 6, 2);
419 m_baseaddr = bitfield(byte, 0, 6) << 16;
420 m_baseaddr |= read_pcm(wavheader + 1) << 8;
421 m_baseaddr |= read_pcm(wavheader + 2) << 0;
422
423 // fetch the 16-bit loop position
424 m_looppos = read_pcm(wavheader + 3) << 8;
425 m_looppos |= read_pcm(wavheader + 4);
426 m_looppos <<= 16;
427
428 // fetch the 16-bit end position, which is stored as a negative value
429 // for some reason that is unclear
430 m_endpos = read_pcm(wavheader + 5) << 8;
431 m_endpos |= read_pcm(wavheader + 6);
432 m_endpos = -int32_t(m_endpos) << 16;
433
434 // remaining data values set registers
435 m_owner.write(0x80 + m_choffs, read_pcm(wavheader + 7));
436 m_owner.write(0x98 + m_choffs, read_pcm(wavheader + 8));
437 m_owner.write(0xb0 + m_choffs, read_pcm(wavheader + 9));
438 m_owner.write(0xc8 + m_choffs, read_pcm(wavheader + 10));
439 m_owner.write(0xe0 + m_choffs, read_pcm(wavheader + 11));
440
441 // reset the envelope so we don't continue playing mid-sample from previous key ons
442 m_env_attenuation = 0x3ff;
443 }
444
445
446 //-------------------------------------------------
447 // read_pcm - read a byte from the external PCM
448 // memory interface
449 //-------------------------------------------------
450
451 uint8_t pcm_channel::read_pcm(uint32_t address) const
452 {
453 return m_owner.intf().ymfm_external_read(ACCESS_PCM, address);
454 }
455
456
457 //-------------------------------------------------
458 // start_attack - start the attack phase
459 //-------------------------------------------------
460
461 void pcm_channel::start_attack()
462 {
463 // don't change anything if already in attack state
464 if (m_eg_state == EG_ATTACK)
465 return;
466 m_eg_state = EG_ATTACK;
467
468 // reset the LFO if requested
469 if (m_regs.ch_lfo_reset(m_choffs))
470 m_lfo_counter = 0;
471
472 // if the attack rate == 63 then immediately go to max attenuation
473 if (m_cache.eg_rate[EG_ATTACK] == 63)
474 m_env_attenuation = 0;
475
476 // reset the positions
477 m_curpos = m_nextpos = 0;
478 }
479
480
481 //-------------------------------------------------
482 // start_release - start the release phase
483 //-------------------------------------------------
484
485 void pcm_channel::start_release()
486 {
487 // don't change anything if already in release or reverb state
488 if (m_eg_state >= EG_RELEASE)
489 return;
490 m_eg_state = EG_RELEASE;
491 }
492
493
494 //-------------------------------------------------
495 // clock_envelope - clock the envelope generator
496 //-------------------------------------------------
497
498 void pcm_channel::clock_envelope(uint32_t env_counter)
499 {
500 // handle attack->decay transitions
501 if (m_eg_state == EG_ATTACK && m_env_attenuation == 0)
502 m_eg_state = EG_DECAY;
503
504 // handle decay->sustain transitions
505 if (m_eg_state == EG_DECAY && m_env_attenuation >= m_cache.eg_sustain)
506 m_eg_state = EG_SUSTAIN;
507
508 // fetch the appropriate 6-bit rate value from the cache
509 uint32_t rate = m_cache.eg_rate[m_eg_state];
510
511 // compute the rate shift value; this is the shift needed to
512 // apply to the env_counter such that it becomes a 5.11 fixed
513 // point number
514 uint32_t rate_shift = rate >> 2;
515 env_counter <<= rate_shift;
516
517 // see if the fractional part is 0; if not, it's not time to clock
518 if (bitfield(env_counter, 0, 11) != 0)
519 return;
520
521 // determine the increment based on the non-fractional part of env_counter
522 uint32_t relevant_bits = bitfield(env_counter, (rate_shift <= 11) ? 11 : rate_shift, 3);
523 uint32_t increment = attenuation_increment(rate, relevant_bits);
524
525 // attack is the only one that increases
526 if (m_eg_state == EG_ATTACK)
527 m_env_attenuation += (~m_env_attenuation * increment) >> 4;
528
529 // all other cases are similar
530 else
531 {
532 // apply the increment
533 m_env_attenuation += increment;
534
535 // clamp the final attenuation
536 if (m_env_attenuation >= 0x400)
537 m_env_attenuation = 0x3ff;
538
539 // transition to reverb at -18dB if enabled
540 if (m_env_attenuation >= 0xc0 && m_eg_state < EG_REVERB && m_regs.ch_pseudo_reverb(m_choffs))
541 m_eg_state = EG_REVERB;
542 }
543 }
544
545
546 //-------------------------------------------------
547 // fetch_sample - fetch a sample at the current
548 // position
549 //-------------------------------------------------
550
551 int16_t pcm_channel::fetch_sample() const
552 {
553 uint32_t addr = m_baseaddr;
554 uint32_t pos = m_curpos >> 16;
555
556 // 8-bit PCM: shift up by 8
557 if (m_format == 0)
558 return read_pcm(addr + pos) << 8;
559
560 // 16-bit PCM: assemble from 2 halves
561 if (m_format == 2)
562 {
563 addr += pos * 2;
564 return (read_pcm(addr) << 8) | read_pcm(addr + 1);
565 }
566
567 // 12-bit PCM: assemble out of half of 3 bytes
568 addr += (pos / 2) * 3;
569 if ((pos & 1) == 0)
570 return (read_pcm(addr + 0) << 8) | ((read_pcm(addr + 1) << 4) & 0xf0);
571 else
572 return (read_pcm(addr + 2) << 8) | ((read_pcm(addr + 1) << 0) & 0xf0);
573 }
574
575
576
577 //*********************************************************
578 // PCM ENGINE
579 //*********************************************************
580
581 //-------------------------------------------------
582 // pcm_engine - constructor
583 //-------------------------------------------------
584
585 pcm_engine::pcm_engine(ymfm_interface &intf) :
586 m_intf(intf),
587 m_env_counter(0),
588 m_modified_channels(ALL_CHANNELS),
589 m_active_channels(ALL_CHANNELS)
590 {
591 // create the channels
592 for (int chnum = 0; chnum < CHANNELS; chnum++)
593 m_channel[chnum] = std::make_unique<pcm_channel>(*this, chnum);
594 }
595
596
597 //-------------------------------------------------
598 // reset - reset the engine state
599 //-------------------------------------------------
600
601 void pcm_engine::reset()
602 {
603 // reset register state
604 m_regs.reset();
605
606 // reset each channel
607 for (auto &chan : m_channel)
608 chan->reset();
609 }
610
611
612 //-------------------------------------------------
613 // save_restore - save or restore the data
614 //-------------------------------------------------
615
616 void pcm_engine::save_restore(ymfm_saved_state &state)
617 {
618 // save our data
619 state.save_restore(m_env_counter);
620
621 // save channel state
622 for (int chnum = 0; chnum < CHANNELS; chnum++)
623 m_channel[chnum]->save_restore(state);
624 }
625
626
627 //-------------------------------------------------
628 // clock - master clocking function
629 //-------------------------------------------------
630
631 void pcm_engine::clock(uint32_t chanmask)
632 {
633 // if something was modified, prepare
634 // also prepare every 4k samples to catch ending notes
635 if (m_modified_channels != 0 || m_prepare_count++ >= 4096)
636 {
637 // call each channel to prepare
638 m_active_channels = 0;
639 for (int chnum = 0; chnum < CHANNELS; chnum++)
640 if (bitfield(chanmask, chnum))
641 if (m_channel[chnum]->prepare())
642 m_active_channels |= 1 << chnum;
643
644 // reset the modified channels and prepare count
645 m_modified_channels = m_prepare_count = 0;
646 }
647
648 // increment the envelope counter; the envelope generator
649 // only clocks every other sample in order to make the PCM
650 // envelopes line up with the FM envelopes (after taking into
651 // account the different FM sampling rate)
652 m_env_counter++;
653
654 // now update the state of all the channels and operators
655 for (int chnum = 0; chnum < CHANNELS; chnum++)
656 if (bitfield(chanmask, chnum))
657 m_channel[chnum]->clock(m_env_counter >> 1);
658 }
659
660
661 //-------------------------------------------------
662 // update - master update function
663 //-------------------------------------------------
664
665 void pcm_engine::output(output_data &output, uint32_t chanmask)
666 {
667 // mask out some channels for debug purposes
668 chanmask &= debug::GLOBAL_PCM_CHANNEL_MASK;
669
670 // compute the output of each channel
671 for (int chnum = 0; chnum < CHANNELS; chnum++)
672 if (bitfield(chanmask, chnum))
673 m_channel[chnum]->output(output);
674 }
675
676
677 //-------------------------------------------------
678 // read - handle reads from the PCM registers
679 //-------------------------------------------------
680
681 uint8_t pcm_engine::read(uint32_t regnum)
682 {
683 // handle reads from the data register
684 if (regnum == 0x06 && m_regs.memory_access_mode() != 0)
685 return m_intf.ymfm_external_read(ACCESS_PCM, m_regs.memory_address_autoinc());
686
687 return m_regs.read(regnum);
688 }
689
690
691 //-------------------------------------------------
692 // write - handle writes to the PCM registers
693 //-------------------------------------------------
694
695 void pcm_engine::write(uint32_t regnum, uint8_t data)
696 {
697 // handle reads to the data register
698 if (regnum == 0x06 && m_regs.memory_access_mode() != 0)
699 {
700 m_intf.ymfm_external_write(ACCESS_PCM, m_regs.memory_address_autoinc(), data);
701 return;
702 }
703
704 // for now just mark all channels as modified
705 m_modified_channels = ALL_CHANNELS;
706
707 // most writes are passive, consumed only when needed
708 m_regs.write(regnum, data);
709
710 // however, process keyons immediately
711 if (regnum >= 0x68 && regnum <= 0x7f)
712 m_channel[regnum - 0x68]->keyonoff(bitfield(data, 7));
713
714 // and also wavetable writes
715 else if (regnum >= 0x08 && regnum <= 0x1f)
716 m_channel[regnum - 0x08]->load_wavetable();
717 }
718
719 }