Debian Codegraph @

src/engine/platform/sound/ymfm/ymfm_opn.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_opn.h"
32 #include "ymfm_fm.ipp"
33
34 namespace ymfm
35 {
36
37 //*********************************************************
38 // OPN/OPNA REGISTERS
39 //*********************************************************
40
41 //-------------------------------------------------
42 // opn_registers_base - constructor
43 //-------------------------------------------------
44
45 template<bool IsOpnA>
46 opn_registers_base<IsOpnA>::opn_registers_base() :
47 m_lfo_counter(0),
48 m_lfo_am(0)
49 {
50 // create the waveforms
51 for (uint32_t index = 0; index < WAVEFORM_LENGTH; index++)
52 m_waveform[0][index] = abs_sin_attenuation(index) | (bitfield(index, 9) << 15);
53 }
54
55
56 //-------------------------------------------------
57 // reset - reset to initial state
58 //-------------------------------------------------
59
60 template<bool IsOpnA>
61 void opn_registers_base<IsOpnA>::reset()
62 {
63 std::fill_n(&m_regdata[0], REGISTERS, 0);
64 if (IsOpnA)
65 {
66 // enable output on both channels by default
67 m_regdata[0xb4] = m_regdata[0xb5] = m_regdata[0xb6] = 0xc0;
68 m_regdata[0x1b4] = m_regdata[0x1b5] = m_regdata[0x1b6] = 0xc0;
69 }
70 }
71
72
73 //-------------------------------------------------
74 // save_restore - save or restore the data
75 //-------------------------------------------------
76
77 template<bool IsOpnA>
78 void opn_registers_base<IsOpnA>::save_restore(ymfm_saved_state &state)
79 {
80 if (IsOpnA)
81 {
82 state.save_restore(m_lfo_counter);
83 state.save_restore(m_lfo_am);
84 }
85 state.save_restore(m_regdata);
86 }
87
88
89 //-------------------------------------------------
90 // operator_map - return an array of operator
91 // indices for each channel; for OPN this is fixed
92 //-------------------------------------------------
93
94 template<>
95 void opn_registers_base<false>::operator_map(operator_mapping &dest) const
96 {
97 // Note that the channel index order is 0,2,1,3, so we bitswap the index.
98 //
99 // This is because the order in the map is:
100 // carrier 1, carrier 2, modulator 1, modulator 2
101 //
102 // But when wiring up the connections, the more natural order is:
103 // carrier 1, modulator 1, carrier 2, modulator 2
104 static const operator_mapping s_fixed_map =
105 { {
106 operator_list( 0, 6, 3, 9 ), // Channel 0 operators
107 operator_list( 1, 7, 4, 10 ), // Channel 1 operators
108 operator_list( 2, 8, 5, 11 ), // Channel 2 operators
109 } };
110 dest = s_fixed_map;
111 }
112
113 template<>
114 void opn_registers_base<true>::operator_map(operator_mapping &dest) const
115 {
116 // Note that the channel index order is 0,2,1,3, so we bitswap the index.
117 //
118 // This is because the order in the map is:
119 // carrier 1, carrier 2, modulator 1, modulator 2
120 //
121 // But when wiring up the connections, the more natural order is:
122 // carrier 1, modulator 1, carrier 2, modulator 2
123 static const operator_mapping s_fixed_map =
124 { {
125 operator_list( 0, 6, 3, 9 ), // Channel 0 operators
126 operator_list( 1, 7, 4, 10 ), // Channel 1 operators
127 operator_list( 2, 8, 5, 11 ), // Channel 2 operators
128 operator_list( 12, 18, 15, 21 ), // Channel 3 operators
129 operator_list( 13, 19, 16, 22 ), // Channel 4 operators
130 operator_list( 14, 20, 17, 23 ), // Channel 5 operators
131 } };
132 dest = s_fixed_map;
133 }
134
135
136 //-------------------------------------------------
137 // write - handle writes to the register array
138 //-------------------------------------------------
139
140 template<bool IsOpnA>
141 bool opn_registers_base<IsOpnA>::write(uint16_t index, uint8_t data, uint32_t &channel, uint32_t &opmask)
142 {
143 assert(index < REGISTERS);
144 if (index >= REGISTERS) return false;
145
146 // writes in the 0xa0-af/0x1a0-af region are handled as latched pairs
147 // borrow unused registers 0xb8-bf/0x1b8-bf as temporary holding locations
148 if ((index & 0xf0) == 0xa0)
149 {
150 if (bitfield(index, 0, 2) == 3)
151 return false;
152
153 uint32_t latchindex = 0xb8 | bitfield(index, 3);
154 if (IsOpnA)
155 latchindex |= index & 0x100;
156
157 // writes to the upper half just latch (only low 6 bits matter)
158 if (bitfield(index, 2))
159 m_regdata[latchindex] = data | 0x80;
160
161 // writes to the lower half only commit if the latch is there
162 else if (bitfield(m_regdata[latchindex], 7))
163 {
164 m_regdata[index] = data;
165 m_regdata[index | 4] = m_regdata[latchindex] & 0x3f;
166 m_regdata[latchindex] = 0;
167 }
168 return false;
169 }
170 else if ((index & 0xf8) == 0xb8)
171 {
172 // registers 0xb8-0xbf are used internally
173 return false;
174 }
175
176 // everything else is normal
177 m_regdata[index] = data;
178
179 // handle writes to the key on index
180 if (index == 0x28)
181 {
182 channel = bitfield(data, 0, 2);
183 if (channel == 3)
184 return false;
185 if (IsOpnA)
186 channel += bitfield(data, 2, 1) * 3;
187 opmask = bitfield(data, 4, 4);
188 return true;
189 }
190 return false;
191 }
192
193
194 //-------------------------------------------------
195 // clock_noise_and_lfo - clock the noise and LFO,
196 // handling clock division, depth, and waveform
197 // computations
198 //-------------------------------------------------
199
200 template<bool IsOpnA>
201 int32_t opn_registers_base<IsOpnA>::clock_noise_and_lfo()
202 {
203 // OPN has no noise generation
204
205 // if LFO not enabled (not present on OPN), quick exit with 0s
206 if (!IsOpnA || !lfo_enable())
207 {
208 m_lfo_counter = 0;
209
210 // special case: if LFO is disabled on OPNA, it basically just keeps the counter
211 // at 0; since position 0 gives an AM value of 0x3f, it is important to reflect
212 // that here; for example, MegaDrive Venom plays some notes with LFO globally
213 // disabled but enabling LFO on the operators, and it expects this added attenutation
214 m_lfo_am = IsOpnA ? 0x3f : 0x00;
215 return 0;
216 }
217
218 // this table is based on converting the frequencies in the applications
219 // manual to clock dividers, based on the assumption of a 7-bit LFO value
220 static uint8_t const lfo_max_count[8] = { 109, 78, 72, 68, 63, 45, 9, 6 };
221 uint32_t subcount = uint8_t(m_lfo_counter++);
222
223 // when we cross the divider count, add enough to zero it and cause an
224 // increment at bit 8; the 7-bit value lives from bits 8-14
225 if (subcount >= lfo_max_count[lfo_rate()])
226 {
227 // note: to match the published values this should be 0x100 - subcount;
228 // however, tests on the hardware and nuked bear out an off-by-one
229 // error exists that causes the max LFO rate to be faster than published
230 m_lfo_counter += 0x101 - subcount;
231 }
232
233 // AM value is 7 bits, staring at bit 8; grab the low 6 directly
234 m_lfo_am = bitfield(m_lfo_counter, 8, 6);
235
236 // first half of the AM period (bit 6 == 0) is inverted
237 if (bitfield(m_lfo_counter, 8+6) == 0)
238 m_lfo_am ^= 0x3f;
239
240 // PM value is 5 bits, starting at bit 10; grab the low 3 directly
241 int32_t pm = bitfield(m_lfo_counter, 10, 3);
242
243 // PM is reflected based on bit 3
244 if (bitfield(m_lfo_counter, 10+3))
245 pm ^= 7;
246
247 // PM is negated based on bit 4
248 return bitfield(m_lfo_counter, 10+4) ? -pm : pm;
249 }
250
251
252 //-------------------------------------------------
253 // lfo_am_offset - return the AM offset from LFO
254 // for the given channel
255 //-------------------------------------------------
256
257 template<bool IsOpnA>
258 uint32_t opn_registers_base<IsOpnA>::lfo_am_offset(uint32_t choffs) const
259 {
260 // shift value for AM sensitivity is [7, 3, 1, 0],
261 // mapping to values of [0, 1.4, 5.9, and 11.8dB]
262 uint32_t am_shift = (1 << (ch_lfo_am_sens(choffs) ^ 3)) - 1;
263
264 // QUESTION: max sensitivity should give 11.8dB range, but this value
265 // is directly added to an x.8 attenuation value, which will only give
266 // 126/256 or ~4.9dB range -- what am I missing? The calculation below
267 // matches several other emulators, including the Nuked implemenation.
268
269 // raw LFO AM value on OPN is 0-3F, scale that up by a factor of 2
270 // (giving 7 bits) before applying the final shift
271 return (m_lfo_am << 1) >> am_shift;
272 }
273
274
275 //-------------------------------------------------
276 // cache_operator_data - fill the operator cache
277 // with prefetched data
278 //-------------------------------------------------
279
280 template<bool IsOpnA>
281 void opn_registers_base<IsOpnA>::cache_operator_data(uint32_t choffs, uint32_t opoffs, opdata_cache &cache)
282 {
283 // set up the easy stuff
284 cache.waveform = &m_waveform[0][0];
285
286 // get frequency from the channel
287 uint32_t block_freq = cache.block_freq = ch_block_freq(choffs);
288
289 // if multi-frequency mode is enabled and this is channel 2,
290 // fetch one of the special frequencies
291 if (multi_freq() && choffs == 2)
292 {
293 if (opoffs == 2)
294 block_freq = cache.block_freq = multi_block_freq(1);
295 else if (opoffs == 10)
296 block_freq = cache.block_freq = multi_block_freq(2);
297 else if (opoffs == 6)
298 block_freq = cache.block_freq = multi_block_freq(0);
299 }
300
301 // compute the keycode: block_freq is:
302 //
303 // BBBFFFFFFFFFFF
304 // ^^^^???
305 //
306 // the 5-bit keycode uses the top 4 bits plus a magic formula
307 // for the final bit
308 uint32_t keycode = bitfield(block_freq, 10, 4) << 1;
309
310 // lowest bit is determined by a mix of next lower FNUM bits
311 // according to this equation from the YM2608 manual:
312 //
313 // (F11 & (F10 | F9 | F8)) | (!F11 & F10 & F9 & F8)
314 //
315 // for speed, we just look it up in a 16-bit constant
316 keycode |= bitfield(0xfe80, bitfield(block_freq, 7, 4));
317
318 // detune adjustment
319 cache.detune = detune_adjustment(op_detune(opoffs), keycode);
320
321 // multiple value, as an x.1 value (0 means 0.5)
322 cache.multiple = op_multiple(opoffs) * 2;
323 if (cache.multiple == 0)
324 cache.multiple = 1;
325
326 // phase step, or PHASE_STEP_DYNAMIC if PM is active; this depends on
327 // block_freq, detune, and multiple, so compute it after we've done those
328 if (!IsOpnA || lfo_enable() == 0 || ch_lfo_pm_sens(choffs) == 0)
329 cache.phase_step = compute_phase_step(choffs, opoffs, cache, 0);
330 else
331 cache.phase_step = opdata_cache::PHASE_STEP_DYNAMIC;
332
333 // total level, scaled by 8
334 cache.total_level = op_total_level(opoffs) << 3;
335
336 // 4-bit sustain level, but 15 means 31 so effectively 5 bits
337 cache.eg_sustain = op_sustain_level(opoffs);
338 cache.eg_sustain |= (cache.eg_sustain + 1) & 0x10;
339 cache.eg_sustain <<= 5;
340
341 // determine KSR adjustment for enevlope rates
342 uint32_t ksrval = keycode >> (op_ksr(opoffs) ^ 3);
343 cache.eg_rate[EG_ATTACK] = effective_rate(op_attack_rate(opoffs) * 2, ksrval);
344 cache.eg_rate[EG_DECAY] = effective_rate(op_decay_rate(opoffs) * 2, ksrval);
345 cache.eg_rate[EG_SUSTAIN] = effective_rate(op_sustain_rate(opoffs) * 2, ksrval);
346 cache.eg_rate[EG_RELEASE] = effective_rate(op_release_rate(opoffs) * 4 + 2, ksrval);
347 }
348
349
350 //-------------------------------------------------
351 // compute_phase_step - compute the phase step
352 //-------------------------------------------------
353
354 template<bool IsOpnA>
355 uint32_t opn_registers_base<IsOpnA>::compute_phase_step(uint32_t choffs, uint32_t opoffs, opdata_cache const &cache, int32_t lfo_raw_pm)
356 {
357 // OPN phase calculation has only a single detune parameter
358 // and uses FNUMs instead of keycodes
359
360 // extract frequency number (low 11 bits of block_freq)
361 uint32_t fnum = bitfield(cache.block_freq, 0, 11) << 1;
362
363 // if there's a non-zero PM sensitivity, compute the adjustment
364 uint32_t pm_sensitivity = ch_lfo_pm_sens(choffs);
365 if (pm_sensitivity != 0)
366 {
367 // apply the phase adjustment based on the upper 7 bits
368 // of FNUM and the PM depth parameters
369 fnum += opn_lfo_pm_phase_adjustment(bitfield(cache.block_freq, 4, 7), pm_sensitivity, lfo_raw_pm);
370
371 // keep fnum to 12 bits
372 fnum &= 0xfff;
373 }
374
375 // apply block shift to compute phase step
376 uint32_t block = bitfield(cache.block_freq, 11, 3);
377 uint32_t phase_step = (fnum << block) >> 2;
378
379 // apply detune based on the keycode
380 phase_step += cache.detune;
381
382 // clamp to 17 bits in case detune overflows
383 // QUESTION: is this specific to the YM2612/3438?
384 phase_step &= 0x1ffff;
385
386 // apply frequency multiplier (which is cached as an x.1 value)
387 return (phase_step * cache.multiple) >> 1;
388 }
389
390
391 //-------------------------------------------------
392 // log_keyon - log a key-on event
393 //-------------------------------------------------
394
395 template<bool IsOpnA>
396 std::string opn_registers_base<IsOpnA>::log_keyon(uint32_t choffs, uint32_t opoffs)
397 {
398 uint32_t chnum = (choffs & 3) + 3 * bitfield(choffs, 8);
399 uint32_t opnum = (opoffs & 15) - ((opoffs & 15) / 4) + 12 * bitfield(opoffs, 8);
400
401 uint32_t block_freq = ch_block_freq(choffs);
402 if (multi_freq() && choffs == 2)
403 {
404 if (opoffs == 2)
405 block_freq = multi_block_freq(1);
406 else if (opoffs == 10)
407 block_freq = multi_block_freq(2);
408 else if (opoffs == 6)
409 block_freq = multi_block_freq(0);
410 }
411
412 char buffer[256];
413 char *end = &buffer[0];
414
415 end += snprintf(end, 256-(end-buffer), "%u.%02u freq=%04X dt=%u fb=%u alg=%X mul=%X tl=%02X ksr=%u adsr=%02X/%02X/%02X/%X sl=%X",
416 chnum, opnum,
417 block_freq,
418 op_detune(opoffs),
419 ch_feedback(choffs),
420 ch_algorithm(choffs),
421 op_multiple(opoffs),
422 op_total_level(opoffs),
423 op_ksr(opoffs),
424 op_attack_rate(opoffs),
425 op_decay_rate(opoffs),
426 op_sustain_rate(opoffs),
427 op_release_rate(opoffs),
428 op_sustain_level(opoffs));
429
430 if (OUTPUTS > 1)
431 end += snprintf(end, 256-(end-buffer), " out=%c%c",
432 ch_output_0(choffs) ? 'L' : '-',
433 ch_output_1(choffs) ? 'R' : '-');
434 if (op_ssg_eg_enable(opoffs))
435 end += snprintf(end, 256-(end-buffer), " ssg=%X", op_ssg_eg_mode(opoffs));
436 bool am = (op_lfo_am_enable(opoffs) && ch_lfo_am_sens(choffs) != 0);
437 if (am)
438 end += snprintf(end, 256-(end-buffer), " am=%u", ch_lfo_am_sens(choffs));
439 bool pm = (ch_lfo_pm_sens(choffs) != 0);
440 if (pm)
441 end += snprintf(end, 256-(end-buffer), " pm=%u", ch_lfo_pm_sens(choffs));
442 if (am || pm)
443 end += snprintf(end, 256-(end-buffer), " lfo=%02X", lfo_rate());
444 if (multi_freq() && choffs == 2)
445 end += snprintf(end, 256-(end-buffer), " multi=1");
446
447 return buffer;
448 }
449
450
451
452 //*********************************************************
453 // SSG RESAMPLER
454 //*********************************************************
455
456 //-------------------------------------------------
457 // add_last - helper to add the last computed
458 // value to the sums, applying the given scale
459 //-------------------------------------------------
460
461 template<typename OutputType, int FirstOutput, bool MixTo1>
462 void ssg_resampler<OutputType, FirstOutput, MixTo1>::add_last(int32_t &sum0, int32_t &sum1, int32_t &sum2, int32_t scale)
463 {
464 sum0 += m_last.data[0] * scale;
465 sum1 += m_last.data[1] * scale;
466 sum2 += m_last.data[2] * scale;
467 }
468
469
470 //-------------------------------------------------
471 // clock_and_add - helper to clock a new value
472 // and then add it to the sums, applying the
473 // given scale
474 //-------------------------------------------------
475
476 template<typename OutputType, int FirstOutput, bool MixTo1>
477 void ssg_resampler<OutputType, FirstOutput, MixTo1>::clock_and_add(int32_t &sum0, int32_t &sum1, int32_t &sum2, int32_t scale)
478 {
479 m_ssg.clock();
480 m_ssg.output(m_last);
481 add_last(sum0, sum1, sum2, scale);
482 }
483
484
485 //-------------------------------------------------
486 // write_to_output - helper to write the sums to
487 // the appropriate outputs, applying the given
488 // divisor to the final result
489 //-------------------------------------------------
490
491 template<typename OutputType, int FirstOutput, bool MixTo1>
492 void ssg_resampler<OutputType, FirstOutput, MixTo1>::write_to_output(OutputType *output, int32_t sum0, int32_t sum1, int32_t sum2, int32_t divisor)
493 {
494 if (MixTo1)
495 {
496 // mixing to one, apply a 2/3 factor to prevent overflow
497 output->data[FirstOutput] = (sum0 + sum1 + sum2) * 2 / (3 * divisor);
498 }
499 else
500 {
501 // write three outputs in a row
502 output->data[FirstOutput + 0] = sum0 / divisor;
503 output->data[FirstOutput + 1] = sum1 / divisor;
504 output->data[FirstOutput + 2] = sum2 / divisor;
505 }
506
507 // track the sample index here
508 m_sampindex++;
509 }
510
511
512 //-------------------------------------------------
513 // ssg_resampler - constructor
514 //-------------------------------------------------
515
516 template<typename OutputType, int FirstOutput, bool MixTo1>
517 ssg_resampler<OutputType, FirstOutput, MixTo1>::ssg_resampler(ssg_engine &ssg) :
518 m_ssg(ssg),
519 m_sampindex(0),
520 m_resampler(&ssg_resampler::resample_nop)
521 {
522 m_last.clear();
523 }
524
525
526 //-------------------------------------------------
527 // save_restore - save or restore the data
528 //-------------------------------------------------
529
530 template<typename OutputType, int FirstOutput, bool MixTo1>
531 void ssg_resampler<OutputType, FirstOutput, MixTo1>::save_restore(ymfm_saved_state &state)
532 {
533 state.save_restore(m_sampindex);
534 state.save_restore(m_last.data);
535 }
536
537
538 //-------------------------------------------------
539 // configure - configure a new ratio
540 //-------------------------------------------------
541
542 template<typename OutputType, int FirstOutput, bool MixTo1>
543 void ssg_resampler<OutputType, FirstOutput, MixTo1>::configure(uint8_t outsamples, uint8_t srcsamples)
544 {
545 switch (outsamples * 10 + srcsamples)
546 {
547 case 4*10 + 1: /* 4:1 */ m_resampler = &ssg_resampler::resample_n_1<4>; break;
548 case 2*10 + 1: /* 2:1 */ m_resampler = &ssg_resampler::resample_n_1<2>; break;
549 case 4*10 + 3: /* 4:3 */ m_resampler = &ssg_resampler::resample_4_3; break;
550 case 1*10 + 1: /* 1:1 */ m_resampler = &ssg_resampler::resample_n_1<1>; break;
551 case 2*10 + 3: /* 2:3 */ m_resampler = &ssg_resampler::resample_2_3; break;
552 case 1*10 + 3: /* 1:3 */ m_resampler = &ssg_resampler::resample_1_n<3>; break;
553 case 2*10 + 9: /* 2:9 */ m_resampler = &ssg_resampler::resample_2_9; break;
554 case 1*10 + 6: /* 1:6 */ m_resampler = &ssg_resampler::resample_1_n<6>; break;
555 case 0*10 + 0: /* 0:0 */ m_resampler = &ssg_resampler::resample_nop; break;
556 default: assert(false); break;
557 }
558 }
559
560
561 //-------------------------------------------------
562 // resample_n_1 - resample SSG output to the
563 // target at a rate of 1 SSG sample to every
564 // n output sample
565 //-------------------------------------------------
566
567 template<typename OutputType, int FirstOutput, bool MixTo1>
568 template<int Multiplier>
569 void ssg_resampler<OutputType, FirstOutput, MixTo1>::resample_n_1(OutputType *output, uint32_t numsamples)
570 {
571 for (uint32_t samp = 0; samp < numsamples; samp++, output++)
572 {
573 if (m_sampindex % Multiplier == 0)
574 {
575 m_ssg.clock();
576 m_ssg.output(m_last);
577 }
578 write_to_output(output, m_last.data[0], m_last.data[1], m_last.data[2]);
579 }
580 }
581
582
583 //-------------------------------------------------
584 // resample_1_n - resample SSG output to the
585 // target at a rate of n SSG samples to every
586 // 1 output sample
587 //-------------------------------------------------
588
589 template<typename OutputType, int FirstOutput, bool MixTo1>
590 template<int Divisor>
591 void ssg_resampler<OutputType, FirstOutput, MixTo1>::resample_1_n(OutputType *output, uint32_t numsamples)
592 {
593 for (uint32_t samp = 0; samp < numsamples; samp++, output++)
594 {
595 int32_t sum0 = 0, sum1 = 0, sum2 = 0;
596 for (int rep = 0; rep < Divisor; rep++)
597 clock_and_add(sum0, sum1, sum2);
598 write_to_output(output, sum0, sum1, sum2, Divisor);
599 }
600 }
601
602
603 //-------------------------------------------------
604 // resample_2_9 - resample SSG output to the
605 // target at a rate of 9 SSG samples to every
606 // 2 output samples
607 //-------------------------------------------------
608
609 template<typename OutputType, int FirstOutput, bool MixTo1>
610 void ssg_resampler<OutputType, FirstOutput, MixTo1>::resample_2_9(OutputType *output, uint32_t numsamples)
611 {
612 for (uint32_t samp = 0; samp < numsamples; samp++, output++)
613 {
614 int32_t sum0 = 0, sum1 = 0, sum2 = 0;
615 if (bitfield(m_sampindex, 0) != 0)
616 add_last(sum0, sum1, sum2, 1);
617 clock_and_add(sum0, sum1, sum2, 2);
618 clock_and_add(sum0, sum1, sum2, 2);
619 clock_and_add(sum0, sum1, sum2, 2);
620 clock_and_add(sum0, sum1, sum2, 2);
621 if (bitfield(m_sampindex, 0) == 0)
622 clock_and_add(sum0, sum1, sum2, 1);
623 write_to_output(output, sum0, sum1, sum2, 9);
624 }
625 }
626
627
628 //-------------------------------------------------
629 // resample_2_3 - resample SSG output to the
630 // target at a rate of 3 SSG samples to every
631 // 2 output samples
632 //-------------------------------------------------
633
634 template<typename OutputType, int FirstOutput, bool MixTo1>
635 void ssg_resampler<OutputType, FirstOutput, MixTo1>::resample_2_3(OutputType *output, uint32_t numsamples)
636 {
637 for (uint32_t samp = 0; samp < numsamples; samp++, output++)
638 {
639 int32_t sum0 = 0, sum1 = 0, sum2 = 0;
640 if (bitfield(m_sampindex, 0) == 0)
641 {
642 clock_and_add(sum0, sum1, sum2, 2);
643 clock_and_add(sum0, sum1, sum2, 1);
644 }
645 else
646 {
647 add_last(sum0, sum1, sum2, 1);
648 clock_and_add(sum0, sum1, sum2, 2);
649 }
650 write_to_output(output, sum0, sum1, sum2, 3);
651 }
652 }
653
654
655 //-------------------------------------------------
656 // resample_4_3 - resample SSG output to the
657 // target at a rate of 3 SSG samples to every
658 // 4 output samples
659 //-------------------------------------------------
660
661 template<typename OutputType, int FirstOutput, bool MixTo1>
662 void ssg_resampler<OutputType, FirstOutput, MixTo1>::resample_4_3(OutputType *output, uint32_t numsamples)
663 {
664 for (uint32_t samp = 0; samp < numsamples; samp++, output++)
665 {
666 int32_t sum0 = 0, sum1 = 0, sum2 = 0;
667 int32_t step = bitfield(m_sampindex, 0, 2);
668 add_last(sum0, sum1, sum2, step);
669 if (step != 3)
670 clock_and_add(sum0, sum1, sum2, 3 - step);
671 write_to_output(output, sum0, sum1, sum2, 3);
672 }
673 }
674
675
676 //-------------------------------------------------
677 // resample_nop - no-op resampler
678 //-------------------------------------------------
679
680 template<typename OutputType, int FirstOutput, bool MixTo1>
681 void ssg_resampler<OutputType, FirstOutput, MixTo1>::resample_nop(OutputType *output, uint32_t numsamples)
682 {
683 // nothing to do except increment the sample index
684 m_sampindex += numsamples;
685 }
686
687
688
689 //*********************************************************
690 // YM2203
691 //*********************************************************
692
693 //-------------------------------------------------
694 // ym2203 - constructor
695 //-------------------------------------------------
696
697 ym2203::ym2203(ymfm_interface &intf) :
698 m_fidelity(OPN_FIDELITY_MAX),
699 m_address(0),
700 m_fm(intf),
701 m_ssg(intf),
702 m_ssg_resampler(m_ssg)
703 {
704 m_last_fm.clear();
705 update_prescale(m_fm.clock_prescale());
706 }
707
708
709 //-------------------------------------------------
710 // reset - reset the system
711 //-------------------------------------------------
712
713 void ym2203::reset()
714 {
715 // reset the engines
716 m_fm.reset();
717 m_ssg.reset();
718 }
719
720
721 //-------------------------------------------------
722 // save_restore - save or restore the data
723 //-------------------------------------------------
724
725 void ym2203::save_restore(ymfm_saved_state &state)
726 {
727 state.save_restore(m_address);
728 state.save_restore(m_last_fm.data);
729
730 m_fm.save_restore(state);
731 m_ssg.save_restore(state);
732 m_ssg_resampler.save_restore(state);
733
734 update_prescale(m_fm.clock_prescale());
735 }
736
737
738 //-------------------------------------------------
739 // read_status - read the status register
740 //-------------------------------------------------
741
742 uint8_t ym2203::read_status()
743 {
744 uint8_t result = m_fm.status();
745 if (m_fm.intf().ymfm_is_busy())
746 result |= fm_engine::STATUS_BUSY;
747 return result;
748 }
749
750
751 //-------------------------------------------------
752 // read_data - read the data register
753 //-------------------------------------------------
754
755 uint8_t ym2203::read_data()
756 {
757 uint8_t result = 0;
758 if (m_address < 0x10)
759 {
760 // 00-0F: Read from SSG
761 result = m_ssg.read(m_address & 0x0f);
762 }
763 return result;
764 }
765
766
767 //-------------------------------------------------
768 // read - handle a read from the device
769 //-------------------------------------------------
770
771 uint8_t ym2203::read(uint32_t offset)
772 {
773 uint8_t result = 0xff;
774 switch (offset & 1)
775 {
776 case 0: // status port
777 result = read_status();
778 break;
779
780 case 1: // data port (only SSG)
781 result = read_data();
782 break;
783 }
784 return result;
785 }
786
787
788 //-------------------------------------------------
789 // write_address - handle a write to the address
790 // register
791 //-------------------------------------------------
792
793 void ym2203::write_address(uint8_t data)
794 {
795 // just set the address
796 m_address = data;
797
798 // special case: update the prescale
799 if (m_address >= 0x2d && m_address <= 0x2f)
800 {
801 // 2D-2F: prescaler select
802 if (m_address == 0x2d)
803 update_prescale(6);
804 else if (m_address == 0x2e && m_fm.clock_prescale() == 6)
805 update_prescale(3);
806 else if (m_address == 0x2f)
807 update_prescale(2);
808 }
809 }
810
811
812 //-------------------------------------------------
813 // write - handle a write to the register
814 // interface
815 //-------------------------------------------------
816
817 void ym2203::write_data(uint8_t data)
818 {
819 if (m_address < 0x10)
820 {
821 // 00-0F: write to SSG
822 m_ssg.write(m_address & 0x0f, data);
823 }
824 else
825 {
826 // 10-FF: write to FM
827 m_fm.write(m_address, data);
828 }
829
830 // mark busy for a bit
831 m_fm.intf().ymfm_set_busy_end(32 * m_fm.clock_prescale());
832 }
833
834
835 //-------------------------------------------------
836 // write - handle a write to the register
837 // interface
838 //-------------------------------------------------
839
840 void ym2203::write(uint32_t offset, uint8_t data)
841 {
842 switch (offset & 1)
843 {
844 case 0: // address port
845 write_address(data);
846 break;
847
848 case 1: // data port
849 write_data(data);
850 break;
851 }
852 }
853
854
855 //-------------------------------------------------
856 // generate - generate one sample of sound
857 //-------------------------------------------------
858
859 void ym2203::generate(output_data *output, uint32_t numsamples)
860 {
861 // FM output is just repeated the prescale number of times; note that
862 // 0 is a special 1.5 case
863 if (m_fm_samples_per_output != 0)
864 {
865 for (uint32_t samp = 0; samp < numsamples; samp++, output++)
866 {
867 if ((m_ssg_resampler.sampindex() + samp) % m_fm_samples_per_output == 0)
868 clock_fm();
869 output->data[0] = m_last_fm.data[0];
870 }
871 }
872 else
873 {
874 for (uint32_t samp = 0; samp < numsamples; samp++, output++)
875 {
876 uint32_t step = (m_ssg_resampler.sampindex() + samp) % 3;
877 if (step == 0)
878 clock_fm();
879 output->data[0] = m_last_fm.data[0];
880 if (step == 1)
881 {
882 clock_fm();
883 output->data[0] = (output->data[0] + m_last_fm.data[0]) / 2;
884 }
885 }
886 }
887
888 // resample the SSG as configured
889 m_ssg_resampler.resample(output - numsamples, numsamples);
890 }
891
892
893 //-------------------------------------------------
894 // update_prescale - update the prescale value,
895 // recomputing derived values
896 //-------------------------------------------------
897
898 void ym2203::update_prescale(uint8_t prescale)
899 {
900 // tell the FM engine
901 m_fm.set_clock_prescale(prescale);
902 m_ssg.prescale_changed();
903
904 // Fidelity: ---- minimum ---- ---- medium ----- ---- maximum-----
905 // rate = clock/24 rate = clock/12 rate = clock/4
906 // Prescale FM rate SSG rate FM rate SSG rate FM rate SSG rate
907 // 6 3:1 2:3 6:1 4:3 18:1 4:1
908 // 3 1.5:1 1:3 3:1 2:3 9:1 2:1
909 // 2 1:1 1:6 2:1 1:3 6:1 1:1
910
911 // compute the number of FM samples per output sample, and select the
912 // resampler function
913 if (m_fidelity == OPN_FIDELITY_MIN)
914 {
915 switch (prescale)
916 {
917 default:
918 case 6: m_fm_samples_per_output = 3; m_ssg_resampler.configure(2, 3); break;
919 case 3: m_fm_samples_per_output = 0; m_ssg_resampler.configure(1, 3); break;
920 case 2: m_fm_samples_per_output = 1; m_ssg_resampler.configure(1, 6); break;
921 }
922 }
923 else if (m_fidelity == OPN_FIDELITY_MED)
924 {
925 switch (prescale)
926 {
927 default:
928 case 6: m_fm_samples_per_output = 6; m_ssg_resampler.configure(4, 3); break;
929 case 3: m_fm_samples_per_output = 3; m_ssg_resampler.configure(2, 3); break;
930 case 2: m_fm_samples_per_output = 2; m_ssg_resampler.configure(1, 3); break;
931 }
932 }
933 else
934 {
935 switch (prescale)
936 {
937 default:
938 case 6: m_fm_samples_per_output = 18; m_ssg_resampler.configure(4, 1); break;
939 case 3: m_fm_samples_per_output = 9; m_ssg_resampler.configure(2, 1); break;
940 case 2: m_fm_samples_per_output = 6; m_ssg_resampler.configure(1, 1); break;
941 }
942 }
943
944 // if overriding the SSG, override the configuration with the nop
945 // resampler to at least keep the sample index moving forward
946 if (m_ssg.overridden())
947 m_ssg_resampler.configure(0, 0);
948 }
949
950
951 //-------------------------------------------------
952 // clock_fm - clock FM state
953 //-------------------------------------------------
954
955 void ym2203::clock_fm()
956 {
957 // clock the system
958 m_fm.clock(fm_engine::ALL_CHANNELS);
959
960 // update the FM content; OPN is full 14-bit with no intermediate clipping
961 m_fm.output(m_last_fm.clear(), 0, 32767, fm_engine::ALL_CHANNELS);
962
963 // convert to 10.3 floating point value for the DAC and back
964 m_last_fm.roundtrip_fp();
965 }
966
967
968
969 //*********************************************************
970 // YM2608
971 //*********************************************************
972
973 //-------------------------------------------------
974 // ym2608 - constructor
975 //-------------------------------------------------
976
977 ym2608::ym2608(ymfm_interface &intf) :
978 m_fidelity(OPN_FIDELITY_MAX),
979 m_address(0),
980 m_irq_enable(0x1f),
981 m_flag_control(0x1c),
982 m_fm(intf),
983 m_ssg(intf),
984 m_ssg_resampler(m_ssg),
985 m_adpcm_a(intf, 0),
986 m_adpcm_b(intf)
987 {
988 m_last_fm.clear();
989 update_prescale(m_fm.clock_prescale());
990 }
991
992
993 //-------------------------------------------------
994 // reset - reset the system
995 //-------------------------------------------------
996
997 void ym2608::reset()
998 {
999 // reset the engines
1000 m_fm.reset();
1001 m_ssg.reset();
1002 m_adpcm_a.reset();
1003 m_adpcm_b.reset();
1004
1005 // configure ADPCM percussion sounds; these are present in an embedded ROM
1006 m_adpcm_a.set_start_end(0, 0x0000, 0x01bf); // bass drum
1007 m_adpcm_a.set_start_end(1, 0x01c0, 0x043f); // snare drum
1008 m_adpcm_a.set_start_end(2, 0x0440, 0x1b7f); // top cymbal
1009 m_adpcm_a.set_start_end(3, 0x1b80, 0x1cff); // high hat
1010 m_adpcm_a.set_start_end(4, 0x1d00, 0x1f7f); // tom tom
1011 m_adpcm_a.set_start_end(5, 0x1f80, 0x1fff); // rim shot
1012
1013 // initialize our special interrupt states, then read the upper status
1014 // register, which updates the IRQs
1015 m_irq_enable = 0x1f;
1016 m_flag_control = 0x1c;
1017 read_status_hi();
1018 }
1019
1020
1021 //-------------------------------------------------
1022 // save_restore - save or restore the data
1023 //-------------------------------------------------
1024
1025 void ym2608::save_restore(ymfm_saved_state &state)
1026 {
1027 state.save_restore(m_address);
1028 state.save_restore(m_irq_enable);
1029 state.save_restore(m_flag_control);
1030 state.save_restore(m_last_fm.data);
1031
1032 m_fm.save_restore(state);
1033 m_ssg.save_restore(state);
1034 m_ssg_resampler.save_restore(state);
1035 m_adpcm_a.save_restore(state);
1036 m_adpcm_b.save_restore(state);
1037 }
1038
1039
1040 //-------------------------------------------------
1041 // read_status - read the status register
1042 //-------------------------------------------------
1043
1044 uint8_t ym2608::read_status()
1045 {
1046 uint8_t result = m_fm.status() & (fm_engine::STATUS_TIMERA | fm_engine::STATUS_TIMERB);
1047 if (m_fm.intf().ymfm_is_busy())
1048 result |= fm_engine::STATUS_BUSY;
1049 return result;
1050 }
1051
1052
1053 //-------------------------------------------------
1054 // read_data - read the data register
1055 //-------------------------------------------------
1056
1057 uint8_t ym2608::read_data()
1058 {
1059 uint8_t result = 0;
1060 if (m_address < 0x10)
1061 {
1062 // 00-0F: Read from SSG
1063 result = m_ssg.read(m_address & 0x0f);
1064 }
1065 else if (m_address == 0xff)
1066 {
1067 // FF: ID code
1068 result = 1;
1069 }
1070 return result;
1071 }
1072
1073
1074 //-------------------------------------------------
1075 // read_status_hi - read the extended status
1076 // register
1077 //-------------------------------------------------
1078
1079 uint8_t ym2608::read_status_hi()
1080 {
1081 // fetch regular status
1082 uint8_t status = m_fm.status() & ~(STATUS_ADPCM_B_EOS | STATUS_ADPCM_B_BRDY | STATUS_ADPCM_B_PLAYING);
1083
1084 // fetch ADPCM-B status, and merge in the bits
1085 uint8_t adpcm_status = m_adpcm_b.status();
1086 if ((adpcm_status & adpcm_b_channel::STATUS_EOS) != 0)
1087 status |= STATUS_ADPCM_B_EOS;
1088 if ((adpcm_status & adpcm_b_channel::STATUS_BRDY) != 0)
1089 status |= STATUS_ADPCM_B_BRDY;
1090 if ((adpcm_status & adpcm_b_channel::STATUS_PLAYING) != 0)
1091 status |= STATUS_ADPCM_B_PLAYING;
1092
1093 // turn off any bits that have been requested to be masked
1094 status &= ~(m_flag_control & 0x1f);
1095
1096 // update the status so that IRQs are propagated
1097 m_fm.set_reset_status(status, ~status);
1098
1099 // merge in the busy flag
1100 if (m_fm.intf().ymfm_is_busy())
1101 status |= fm_engine::STATUS_BUSY;
1102 return status;
1103 }
1104
1105
1106 //-------------------------------------------------
1107 // read_data_hi - read the upper data register
1108 //-------------------------------------------------
1109
1110 uint8_t ym2608::read_data_hi()
1111 {
1112 uint8_t result = 0;
1113 if ((m_address & 0xff) < 0x10)
1114 {
1115 // 00-0F: Read from ADPCM-B
1116 result = m_adpcm_b.read(m_address & 0x0f);
1117 }
1118 return result;
1119 }
1120
1121
1122 //-------------------------------------------------
1123 // read - handle a read from the device
1124 //-------------------------------------------------
1125
1126 uint8_t ym2608::read(uint32_t offset)
1127 {
1128 uint8_t result = 0;
1129 switch (offset & 3)
1130 {
1131 case 0: // status port, YM2203 compatible
1132 result = read_status();
1133 break;
1134
1135 case 1: // data port (only SSG)
1136 result = read_data();
1137 break;
1138
1139 case 2: // status port, extended
1140 result = read_status_hi();
1141 break;
1142
1143 case 3: // ADPCM-B data
1144 result = read_data_hi();
1145 break;
1146 }
1147 return result;
1148 }
1149
1150
1151 //-------------------------------------------------
1152 // write_address - handle a write to the address
1153 // register
1154 //-------------------------------------------------
1155
1156 void ym2608::write_address(uint8_t data)
1157 {
1158 // just set the address
1159 m_address = data;
1160
1161 // special case: update the prescale
1162 if (m_address >= 0x2d && m_address <= 0x2f)
1163 {
1164 // 2D-2F: prescaler select
1165 if (m_address == 0x2d)
1166 update_prescale(6);
1167 else if (m_address == 0x2e && m_fm.clock_prescale() == 6)
1168 update_prescale(3);
1169 else if (m_address == 0x2f)
1170 update_prescale(2);
1171 }
1172 }
1173
1174
1175 //-------------------------------------------------
1176 // write - handle a write to the data register
1177 //-------------------------------------------------
1178
1179 void ym2608::write_data(uint8_t data)
1180 {
1181 // ignore if paired with upper address
1182 if (bitfield(m_address, 8))
1183 return;
1184
1185 if (m_address < 0x10)
1186 {
1187 // 00-0F: write to SSG
1188 m_ssg.write(m_address & 0x0f, data);
1189 }
1190 else if (m_address < 0x20)
1191 {
1192 // 10-1F: write to ADPCM-A
1193 m_adpcm_a.write(m_address & 0x0f, data);
1194 }
1195 else if (m_address == 0x29)
1196 {
1197 // 29: special IRQ mask register
1198 m_irq_enable = data;
1199 m_fm.set_irq_mask(m_irq_enable & ~m_flag_control & 0x1f);
1200 }
1201 else
1202 {
1203 // 20-28, 2A-FF: write to FM
1204 m_fm.write(m_address, data);
1205 }
1206
1207 // mark busy for a bit
1208 m_fm.intf().ymfm_set_busy_end(32 * m_fm.clock_prescale());
1209 }
1210
1211
1212 //-------------------------------------------------
1213 // write_address_hi - handle a write to the upper
1214 // address register
1215 //-------------------------------------------------
1216
1217 void ym2608::write_address_hi(uint8_t data)
1218 {
1219 // just set the address
1220 m_address = 0x100 | data;
1221 }
1222
1223
1224 //-------------------------------------------------
1225 // write_data_hi - handle a write to the upper
1226 // data register
1227 //-------------------------------------------------
1228
1229 void ym2608::write_data_hi(uint8_t data)
1230 {
1231 // ignore if paired with upper address
1232 if (!bitfield(m_address, 8))
1233 return;
1234
1235 if (m_address < 0x110)
1236 {
1237 // 100-10F: write to ADPCM-B
1238 m_adpcm_b.write(m_address & 0x0f, data);
1239 }
1240 else if (m_address == 0x110)
1241 {
1242 // 110: IRQ flag control
1243 if (bitfield(data, 7))
1244 m_fm.set_reset_status(0, 0xff);
1245 else
1246 {
1247 m_flag_control = data;
1248 m_fm.set_irq_mask(m_irq_enable & ~m_flag_control & 0x1f);
1249 }
1250 }
1251 else
1252 {
1253 // 111-1FF: write to FM
1254 m_fm.write(m_address, data);
1255 }
1256
1257 // mark busy for a bit
1258 m_fm.intf().ymfm_set_busy_end(32 * m_fm.clock_prescale());
1259 }
1260
1261
1262 //-------------------------------------------------
1263 // write - handle a write to the register
1264 // interface
1265 //-------------------------------------------------
1266
1267 void ym2608::write(uint32_t offset, uint8_t data)
1268 {
1269 switch (offset & 3)
1270 {
1271 case 0: // address port
1272 write_address(data);
1273 break;
1274
1275 case 1: // data port
1276 write_data(data);
1277 break;
1278
1279 case 2: // upper address port
1280 write_address_hi(data);
1281 break;
1282
1283 case 3: // upper data port
1284 write_data_hi(data);
1285 break;
1286 }
1287 }
1288
1289
1290 //-------------------------------------------------
1291 // generate - generate one sample of sound
1292 //-------------------------------------------------
1293
1294 void ym2608::generate(output_data *output, uint32_t numsamples)
1295 {
1296 // FM output is just repeated the prescale number of times; note that
1297 // 0 is a special 1.5 case
1298 if (m_fm_samples_per_output != 0)
1299 {
1300 for (uint32_t samp = 0; samp < numsamples; samp++, output++)
1301 {
1302 if ((m_ssg_resampler.sampindex() + samp) % m_fm_samples_per_output == 0)
1303 clock_fm_and_adpcm();
1304 output->data[0] = m_last_fm.data[0];
1305 output->data[1] = m_last_fm.data[1];
1306 }
1307 }
1308 else
1309 {
1310 for (uint32_t samp = 0; samp < numsamples; samp++, output++)
1311 {
1312 uint32_t step = (m_ssg_resampler.sampindex() + samp) % 3;
1313 if (step == 0)
1314 clock_fm_and_adpcm();
1315 output->data[0] = m_last_fm.data[0];
1316 output->data[1] = m_last_fm.data[1];
1317 if (step == 1)
1318 {
1319 clock_fm_and_adpcm();
1320 output->data[0] = (output->data[0] + m_last_fm.data[0]) / 2;
1321 output->data[1] = (output->data[1] + m_last_fm.data[1]) / 2;
1322 }
1323 }
1324 }
1325
1326 // resample the SSG as configured
1327 m_ssg_resampler.resample(output - numsamples, numsamples);
1328 }
1329
1330
1331 //-------------------------------------------------
1332 // update_prescale - update the prescale value,
1333 // recomputing derived values
1334 //-------------------------------------------------
1335
1336 void ym2608::update_prescale(uint8_t prescale)
1337 {
1338 // tell the FM engine
1339 m_fm.set_clock_prescale(prescale);
1340 m_ssg.prescale_changed();
1341
1342 // Fidelity: ---- minimum ---- ---- medium ----- ---- maximum-----
1343 // rate = clock/48 rate = clock/24 rate = clock/8
1344 // Prescale FM rate SSG rate FM rate SSG rate FM rate SSG rate
1345 // 6 3:1 2:3 6:1 4:3 18:1 4:1
1346 // 3 1.5:1 1:3 3:1 2:3 9:1 2:1
1347 // 2 1:1 1:6 2:1 1:3 6:1 1:1
1348
1349 // compute the number of FM samples per output sample, and select the
1350 // resampler function
1351 if (m_fidelity == OPN_FIDELITY_MIN)
1352 {
1353 switch (prescale)
1354 {
1355 default:
1356 case 6: m_fm_samples_per_output = 3; m_ssg_resampler.configure(2, 3); break;
1357 case 3: m_fm_samples_per_output = 0; m_ssg_resampler.configure(1, 3); break;
1358 case 2: m_fm_samples_per_output = 1; m_ssg_resampler.configure(1, 6); break;
1359 }
1360 }
1361 else if (m_fidelity == OPN_FIDELITY_MED)
1362 {
1363 switch (prescale)
1364 {
1365 default:
1366 case 6: m_fm_samples_per_output = 6; m_ssg_resampler.configure(4, 3); break;
1367 case 3: m_fm_samples_per_output = 3; m_ssg_resampler.configure(2, 3); break;
1368 case 2: m_fm_samples_per_output = 2; m_ssg_resampler.configure(1, 3); break;
1369 }
1370 }
1371 else
1372 {
1373 switch (prescale)
1374 {
1375 default:
1376 case 6: m_fm_samples_per_output = 18; m_ssg_resampler.configure(4, 1); break;
1377 case 3: m_fm_samples_per_output = 9; m_ssg_resampler.configure(2, 1); break;
1378 case 2: m_fm_samples_per_output = 6; m_ssg_resampler.configure(1, 1); break;
1379 }
1380 }
1381
1382 // if overriding the SSG, override the configuration with the nop
1383 // resampler to at least keep the sample index moving forward
1384 if (m_ssg.overridden())
1385 m_ssg_resampler.configure(0, 0);
1386 }
1387
1388
1389 //-------------------------------------------------
1390 // clock_fm_and_adpcm - clock FM and ADPCM state
1391 //-------------------------------------------------
1392
1393 void ym2608::clock_fm_and_adpcm()
1394 {
1395 // top bit of the IRQ enable flags controls 3-channel vs 6-channel mode
1396 uint32_t fmmask = bitfield(m_irq_enable, 7) ? 0x3f : 0x07;
1397
1398 // clock the system
1399 uint32_t env_counter = m_fm.clock(fm_engine::ALL_CHANNELS);
1400
1401 // clock the ADPCM-A engine on every envelope cycle
1402 // (channels 4 and 5 clock every 2 envelope clocks)
1403 if (bitfield(env_counter, 0, 2) == 0)
1404 m_adpcm_a.clock(bitfield(env_counter, 2) ? 0x0f : 0x3f);
1405
1406 // clock the ADPCM-B engine every cycle
1407 m_adpcm_b.clock();
1408
1409 // update the FM content; OPNA is 13-bit with no intermediate clipping
1410 m_fm.output(m_last_fm.clear(), 1, 32767, fmmask);
1411
1412 // mix in the ADPCM and clamp
1413 m_adpcm_a.output(m_last_fm, 0x3f);
1414 m_adpcm_b.output(m_last_fm, 1);
1415 m_last_fm.clamp16();
1416 }
1417
1418
1419 //*********************************************************
1420 // YMF288
1421 //*********************************************************
1422
1423 // YMF288 is a YM2608 with the following changes:
1424 // * ADPCM-B part removed
1425 // * prescaler removed (fixed at 6)
1426 // * CSM removed
1427 // * Low power mode added
1428 // * SSG tone frequency is altered in some way? (explicitly DC for Tp 0-7, also double volume in some cases)
1429 // * I/O ports removed
1430 // * Shorter busy times
1431 // * All registers can be read
1432
1433 //-------------------------------------------------
1434 // ymf288 - constructor
1435 //-------------------------------------------------
1436
1437 ymf288::ymf288(ymfm_interface &intf) :
1438 m_fidelity(OPN_FIDELITY_MAX),
1439 m_address(0),
1440 m_irq_enable(0x03),
1441 m_flag_control(0x03),
1442 m_fm(intf),
1443 m_ssg(intf),
1444 m_ssg_resampler(m_ssg),
1445 m_adpcm_a(intf, 0)
1446 {
1447 m_last_fm.clear();
1448 update_prescale();
1449 }
1450
1451
1452 //-------------------------------------------------
1453 // reset - reset the system
1454 //-------------------------------------------------
1455
1456 void ymf288::reset()
1457 {
1458 // reset the engines
1459 m_fm.reset();
1460 m_ssg.reset();
1461 m_adpcm_a.reset();
1462
1463 // configure ADPCM percussion sounds; these are present in an embedded ROM
1464 m_adpcm_a.set_start_end(0, 0x0000, 0x01bf); // bass drum
1465 m_adpcm_a.set_start_end(1, 0x01c0, 0x043f); // snare drum
1466 m_adpcm_a.set_start_end(2, 0x0440, 0x1b7f); // top cymbal
1467 m_adpcm_a.set_start_end(3, 0x1b80, 0x1cff); // high hat
1468 m_adpcm_a.set_start_end(4, 0x1d00, 0x1f7f); // tom tom
1469 m_adpcm_a.set_start_end(5, 0x1f80, 0x1fff); // rim shot
1470
1471 // initialize our special interrupt states, then read the upper status
1472 // register, which updates the IRQs
1473 m_irq_enable = 0x03;
1474 m_flag_control = 0x00;
1475 read_status_hi();
1476 }
1477
1478
1479 //-------------------------------------------------
1480 // save_restore - save or restore the data
1481 //-------------------------------------------------
1482
1483 void ymf288::save_restore(ymfm_saved_state &state)
1484 {
1485 state.save_restore(m_address);
1486 state.save_restore(m_irq_enable);
1487 state.save_restore(m_flag_control);
1488 state.save_restore(m_last_fm.data);
1489
1490 m_fm.save_restore(state);
1491 m_ssg.save_restore(state);
1492 m_ssg_resampler.save_restore(state);
1493 m_adpcm_a.save_restore(state);
1494 }
1495
1496
1497 //-------------------------------------------------
1498 // read_status - read the status register
1499 //-------------------------------------------------
1500
1501 uint8_t ymf288::read_status()
1502 {
1503 uint8_t result = m_fm.status() & (fm_engine::STATUS_TIMERA | fm_engine::STATUS_TIMERB);
1504 if (m_fm.intf().ymfm_is_busy())
1505 result |= fm_engine::STATUS_BUSY;
1506 return result;
1507 }
1508
1509
1510 //-------------------------------------------------
1511 // read_data - read the data register
1512 //-------------------------------------------------
1513
1514 uint8_t ymf288::read_data()
1515 {
1516 uint8_t result = 0;
1517 if (m_address < 0x0e)
1518 {
1519 // 00-0D: Read from SSG
1520 result = m_ssg.read(m_address & 0x0f);
1521 }
1522 else if (m_address < 0x10)
1523 {
1524 // 0E-0F: I/O ports not supported
1525 result = 0xff;
1526 }
1527 else if (m_address == 0xff)
1528 {
1529 // FF: ID code
1530 result = 2;
1531 }
1532 else if (ymf288_mode())
1533 {
1534 // registers are readable in YMF288 mode
1535 result = m_fm.regs().read(m_address);
1536 }
1537 return result;
1538 }
1539
1540
1541 //-------------------------------------------------
1542 // read_status_hi - read the extended status
1543 // register
1544 //-------------------------------------------------
1545
1546 uint8_t ymf288::read_status_hi()
1547 {
1548 // fetch regular status
1549 uint8_t status = m_fm.status() & (fm_engine::STATUS_TIMERA | fm_engine::STATUS_TIMERB);
1550
1551 // turn off any bits that have been requested to be masked
1552 status &= ~(m_flag_control & 0x03);
1553
1554 // update the status so that IRQs are propagated
1555 m_fm.set_reset_status(status, ~status);
1556
1557 // merge in the busy flag
1558 if (m_fm.intf().ymfm_is_busy())
1559 status |= fm_engine::STATUS_BUSY;
1560 return status;
1561 }
1562
1563
1564 //-------------------------------------------------
1565 // read - handle a read from the device
1566 //-------------------------------------------------
1567
1568 uint8_t ymf288::read(uint32_t offset)
1569 {
1570 uint8_t result = 0;
1571 switch (offset & 3)
1572 {
1573 case 0: // status port, YM2203 compatible
1574 result = read_status();
1575 break;
1576
1577 case 1: // data port
1578 result = read_data();
1579 break;
1580
1581 case 2: // status port, extended
1582 result = read_status_hi();
1583 break;
1584
1585 case 3: // unmapped
1586 debug::log_unexpected_read_write("Unexpected read from YMF288 offset %d\n", offset & 3);
1587 break;
1588 }
1589 return result;
1590 }
1591
1592
1593 //-------------------------------------------------
1594 // write_address - handle a write to the address
1595 // register
1596 //-------------------------------------------------
1597
1598 void ymf288::write_address(uint8_t data)
1599 {
1600 // just set the address
1601 m_address = data;
1602
1603 // in YMF288 mode, busy is signaled after address writes too
1604 if (ymf288_mode())
1605 m_fm.intf().ymfm_set_busy_end(16);
1606 }
1607
1608
1609 //-------------------------------------------------
1610 // write - handle a write to the data register
1611 //-------------------------------------------------
1612
1613 void ymf288::write_data(uint8_t data)
1614 {
1615 // ignore if paired with upper address
1616 if (bitfield(m_address, 8))
1617 return;
1618
1619 // wait times are shorter in YMF288 mode
1620 int busy_cycles = ymf288_mode() ? 16 : 32 * m_fm.clock_prescale();
1621 if (m_address < 0x0e)
1622 {
1623 // 00-0D: write to SSG
1624 m_ssg.write(m_address & 0x0f, data);
1625 }
1626 else if (m_address < 0x10)
1627 {
1628 // 0E-0F: I/O ports not supported
1629 }
1630 else if (m_address < 0x20)
1631 {
1632 // 10-1F: write to ADPCM-A
1633 m_adpcm_a.write(m_address & 0x0f, data);
1634 busy_cycles = 32 * m_fm.clock_prescale();
1635 }
1636 else if (m_address == 0x27)
1637 {
1638 // 27: mode register; CSM isn't supported so disable it
1639 data &= 0x7f;
1640 m_fm.write(m_address, data);
1641 }
1642 else if (m_address == 0x29)
1643 {
1644 // 29: special IRQ mask register
1645 m_irq_enable = data;
1646 m_fm.set_irq_mask(m_irq_enable & ~m_flag_control & 0x03);
1647 }
1648 else
1649 {
1650 // 20-27, 2A-FF: write to FM
1651 m_fm.write(m_address, data);
1652 }
1653
1654 // mark busy for a bit
1655 m_fm.intf().ymfm_set_busy_end(busy_cycles);
1656 }
1657
1658
1659 //-------------------------------------------------
1660 // write_address_hi - handle a write to the upper
1661 // address register
1662 //-------------------------------------------------
1663
1664 void ymf288::write_address_hi(uint8_t data)
1665 {
1666 // just set the address
1667 m_address = 0x100 | data;
1668
1669 // in YMF288 mode, busy is signaled after address writes too
1670 if (ymf288_mode())
1671 m_fm.intf().ymfm_set_busy_end(16);
1672 }
1673
1674
1675 //-------------------------------------------------
1676 // write_data_hi - handle a write to the upper
1677 // data register
1678 //-------------------------------------------------
1679
1680 void ymf288::write_data_hi(uint8_t data)
1681 {
1682 // ignore if paired with upper address
1683 if (!bitfield(m_address, 8))
1684 return;
1685
1686 // wait times are shorter in YMF288 mode
1687 int busy_cycles = ymf288_mode() ? 16 : 32 * m_fm.clock_prescale();
1688 if (m_address == 0x110)
1689 {
1690 // 110: IRQ flag control
1691 if (bitfield(data, 7))
1692 m_fm.set_reset_status(0, 0xff);
1693 else
1694 {
1695 m_flag_control = data;
1696 m_fm.set_irq_mask(m_irq_enable & ~m_flag_control & 0x03);
1697 }
1698 }
1699 else
1700 {
1701 // 100-10F,111-1FF: write to FM
1702 m_fm.write(m_address, data);
1703 }
1704
1705 // mark busy for a bit
1706 m_fm.intf().ymfm_set_busy_end(busy_cycles);
1707 }
1708
1709
1710 //-------------------------------------------------
1711 // write - handle a write to the register
1712 // interface
1713 //-------------------------------------------------
1714
1715 void ymf288::write(uint32_t offset, uint8_t data)
1716 {
1717 switch (offset & 3)
1718 {
1719 case 0: // address port
1720 write_address(data);
1721 break;
1722
1723 case 1: // data port
1724 write_data(data);
1725 break;
1726
1727 case 2: // upper address port
1728 write_address_hi(data);
1729 break;
1730
1731 case 3: // upper data port
1732 write_data_hi(data);
1733 break;
1734 }
1735 }
1736
1737
1738 //-------------------------------------------------
1739 // generate - generate one sample of sound
1740 //-------------------------------------------------
1741
1742 void ymf288::generate(output_data *output, uint32_t numsamples)
1743 {
1744 // FM output is just repeated the prescale number of times; note that
1745 // 0 is a special 1.5 case
1746 if (m_fm_samples_per_output != 0)
1747 {
1748 for (uint32_t samp = 0; samp < numsamples; samp++, output++)
1749 {
1750 if ((m_ssg_resampler.sampindex() + samp) % m_fm_samples_per_output == 0)
1751 clock_fm_and_adpcm();
1752 output->data[0] = m_last_fm.data[0];
1753 output->data[1] = m_last_fm.data[1];
1754 }
1755 }
1756 else
1757 {
1758 for (uint32_t samp = 0; samp < numsamples; samp++, output++)
1759 {
1760 uint32_t step = (m_ssg_resampler.sampindex() + samp) % 3;
1761 if (step == 0)
1762 clock_fm_and_adpcm();
1763 output->data[0] = m_last_fm.data[0];
1764 output->data[1] = m_last_fm.data[1];
1765 if (step == 1)
1766 {
1767 clock_fm_and_adpcm();
1768 output->data[0] = (output->data[0] + m_last_fm.data[0]) / 2;
1769 output->data[1] = (output->data[1] + m_last_fm.data[1]) / 2;
1770 }
1771 }
1772 }
1773
1774 // resample the SSG as configured
1775 m_ssg_resampler.resample(output - numsamples, numsamples);
1776 }
1777
1778
1779 //-------------------------------------------------
1780 // update_prescale - update the prescale value,
1781 // recomputing derived values
1782 //-------------------------------------------------
1783
1784 void ymf288::update_prescale()
1785 {
1786 // Fidelity: ---- minimum ---- ---- medium ----- ---- maximum-----
1787 // rate = clock/144 rate = clock/144 rate = clock/16
1788 // Prescale FM rate SSG rate FM rate SSG rate FM rate SSG rate
1789 // 6 1:1 2:9 1:1 2:9 9:1 2:1
1790
1791 // compute the number of FM samples per output sample, and select the
1792 // resampler function
1793 if (m_fidelity == OPN_FIDELITY_MIN || m_fidelity == OPN_FIDELITY_MED)
1794 {
1795 m_fm_samples_per_output = 1;
1796 m_ssg_resampler.configure(2, 9);
1797 }
1798 else
1799 {
1800 m_fm_samples_per_output = 9;
1801 m_ssg_resampler.configure(2, 1);
1802 }
1803
1804 // if overriding the SSG, override the configuration with the nop
1805 // resampler to at least keep the sample index moving forward
1806 if (m_ssg.overridden())
1807 m_ssg_resampler.configure(0, 0);
1808 }
1809
1810
1811 //-------------------------------------------------
1812 // clock_fm_and_adpcm - clock FM and ADPCM state
1813 //-------------------------------------------------
1814
1815 void ymf288::clock_fm_and_adpcm()
1816 {
1817 // top bit of the IRQ enable flags controls 3-channel vs 6-channel mode
1818 uint32_t fmmask = bitfield(m_irq_enable, 7) ? 0x3f : 0x07;
1819
1820 // clock the system
1821 uint32_t env_counter = m_fm.clock(fm_engine::ALL_CHANNELS);
1822
1823 // clock the ADPCM-A engine on every envelope cycle
1824 // (channels 4 and 5 clock every 2 envelope clocks)
1825 if (bitfield(env_counter, 0, 2) == 0)
1826 m_adpcm_a.clock(bitfield(env_counter, 2) ? 0x0f : 0x3f);
1827
1828 // update the FM content; OPNA is 13-bit with no intermediate clipping
1829 m_fm.output(m_last_fm.clear(), 1, 32767, fmmask);
1830
1831 // mix in the ADPCM
1832 m_adpcm_a.output(m_last_fm, 0x3f);
1833 }
1834
1835
1836
1837 //*********************************************************
1838 // YM2610
1839 //*********************************************************
1840
1841 //-------------------------------------------------
1842 // ym2610 - constructor
1843 //-------------------------------------------------
1844
1845 ym2610::ym2610(ymfm_interface &intf, uint8_t channel_mask) :
1846 m_fidelity(OPN_FIDELITY_MAX),
1847 m_address(0),
1848 m_fm_mask(channel_mask),
1849 m_eos_status(0x00),
1850 m_flag_mask(EOS_FLAGS_MASK),
1851 m_fm(intf),
1852 m_ssg(intf),
1853 m_ssg_resampler(m_ssg),
1854 m_adpcm_a(intf, 8),
1855 m_adpcm_b(intf, 8)
1856 {
1857 update_prescale();
1858 }
1859
1860
1861 //-------------------------------------------------
1862 // reset - reset the system
1863 //-------------------------------------------------
1864
1865 void ym2610::reset()
1866 {
1867 // reset the engines
1868 m_fm.reset();
1869 m_ssg.reset();
1870 m_adpcm_a.reset();
1871 m_adpcm_b.reset();
1872
1873 // initialize our special interrupt states
1874 m_eos_status = 0x00;
1875 m_flag_mask = EOS_FLAGS_MASK;
1876 }
1877
1878
1879 //-------------------------------------------------
1880 // save_restore - save or restore the data
1881 //-------------------------------------------------
1882
1883 void ym2610::save_restore(ymfm_saved_state &state)
1884 {
1885 state.save_restore(m_address);
1886 state.save_restore(m_eos_status);
1887 state.save_restore(m_flag_mask);
1888
1889 m_fm.save_restore(state);
1890 m_ssg.save_restore(state);
1891 m_ssg_resampler.save_restore(state);
1892 m_adpcm_a.save_restore(state);
1893 m_adpcm_b.save_restore(state);
1894 }
1895
1896
1897 //-------------------------------------------------
1898 // read_status - read the status register
1899 //-------------------------------------------------
1900
1901 uint8_t ym2610::read_status()
1902 {
1903 uint8_t result = m_fm.status() & (fm_engine::STATUS_TIMERA | fm_engine::STATUS_TIMERB);
1904 if (m_fm.intf().ymfm_is_busy())
1905 result |= fm_engine::STATUS_BUSY;
1906 return result;
1907 }
1908
1909
1910 //-------------------------------------------------
1911 // read_data - read the data register
1912 //-------------------------------------------------
1913
1914 uint8_t ym2610::read_data()
1915 {
1916 uint8_t result = 0;
1917 if (m_address < 0x0e)
1918 {
1919 // 00-0D: Read from SSG
1920 result = m_ssg.read(m_address & 0x0f);
1921 }
1922 else if (m_address < 0x10)
1923 {
1924 // 0E-0F: I/O ports not supported
1925 result = 0xff;
1926 }
1927 else if (m_address == 0xff)
1928 {
1929 // FF: ID code
1930 result = 1;
1931 }
1932 return result;
1933 }
1934
1935
1936 //-------------------------------------------------
1937 // read_status_hi - read the extended status
1938 // register
1939 //-------------------------------------------------
1940
1941 uint8_t ym2610::read_status_hi()
1942 {
1943 return m_eos_status & m_flag_mask;
1944 }
1945
1946
1947 //-------------------------------------------------
1948 // read_data_hi - read the upper data register
1949 //-------------------------------------------------
1950
1951 uint8_t ym2610::read_data_hi()
1952 {
1953 uint8_t result = 0;
1954 return result;
1955 }
1956
1957
1958 //-------------------------------------------------
1959 // read - handle a read from the device
1960 //-------------------------------------------------
1961
1962 uint8_t ym2610::read(uint32_t offset)
1963 {
1964 uint8_t result = 0;
1965 switch (offset & 3)
1966 {
1967 case 0: // status port, YM2203 compatible
1968 result = read_status();
1969 break;
1970
1971 case 1: // data port (only SSG)
1972 result = read_data();
1973 break;
1974
1975 case 2: // status port, extended
1976 result = read_status_hi();
1977 break;
1978
1979 case 3: // ADPCM-B data
1980 result = read_data_hi();
1981 break;
1982 }
1983 return result;
1984 }
1985
1986
1987 //-------------------------------------------------
1988 // write_address - handle a write to the address
1989 // register
1990 //-------------------------------------------------
1991
1992 void ym2610::write_address(uint8_t data)
1993 {
1994 // just set the address
1995 m_address = data;
1996 }
1997
1998
1999 //-------------------------------------------------
2000 // write - handle a write to the data register
2001 //-------------------------------------------------
2002
2003 void ym2610::write_data(uint8_t data)
2004 {
2005 // ignore if paired with upper address
2006 if (bitfield(m_address, 8))
2007 return;
2008
2009 if (m_address < 0x0e)
2010 {
2011 // 00-0D: write to SSG
2012 m_ssg.write(m_address & 0x0f, data);
2013 }
2014 else if (m_address < 0x10)
2015 {
2016 // 0E-0F: I/O ports not supported
2017 }
2018 else if (m_address < 0x1c)
2019 {
2020 // 10-1B: write to ADPCM-B
2021 // YM2610 effectively forces external mode on, and disables recording
2022 if (m_address == 0x10)
2023 data = (data | 0x20) & ~0x40;
2024 m_adpcm_b.write(m_address & 0x0f, data);
2025 }
2026 else if (m_address == 0x1c)
2027 {
2028 // 1C: EOS flag reset
2029 m_flag_mask = ~data & EOS_FLAGS_MASK;
2030 m_eos_status &= ~(data & EOS_FLAGS_MASK);
2031 }
2032 else
2033 {
2034 // 1D-FF: write to FM
2035 m_fm.write(m_address, data);
2036 }
2037
2038 // mark busy for a bit
2039 m_fm.intf().ymfm_set_busy_end(32 * m_fm.clock_prescale());
2040 }
2041
2042
2043 //-------------------------------------------------
2044 // write_address_hi - handle a write to the upper
2045 // address register
2046 //-------------------------------------------------
2047
2048 void ym2610::write_address_hi(uint8_t data)
2049 {
2050 // just set the address
2051 m_address = 0x100 | data;
2052 }
2053
2054
2055 //-------------------------------------------------
2056 // write_data_hi - handle a write to the upper
2057 // data register
2058 //-------------------------------------------------
2059
2060 void ym2610::write_data_hi(uint8_t data)
2061 {
2062 // ignore if paired with upper address
2063 if (!bitfield(m_address, 8))
2064 return;
2065
2066 if (m_address < 0x130)
2067 {
2068 // 100-12F: write to ADPCM-A
2069 m_adpcm_a.write(m_address & 0x3f, data);
2070 }
2071 else
2072 {
2073 // 130-1FF: write to FM
2074 m_fm.write(m_address, data);
2075 }
2076
2077 // mark busy for a bit
2078 m_fm.intf().ymfm_set_busy_end(32 * m_fm.clock_prescale());
2079 }
2080
2081
2082 //-------------------------------------------------
2083 // write - handle a write to the register
2084 // interface
2085 //-------------------------------------------------
2086
2087 void ym2610::write(uint32_t offset, uint8_t data)
2088 {
2089 switch (offset & 3)
2090 {
2091 case 0: // address port
2092 write_address(data);
2093 break;
2094
2095 case 1: // data port
2096 write_data(data);
2097 break;
2098
2099 case 2: // upper address port
2100 write_address_hi(data);
2101 break;
2102
2103 case 3: // upper data port
2104 write_data_hi(data);
2105 break;
2106 }
2107 }
2108
2109
2110 //-------------------------------------------------
2111 // generate - generate one sample of sound
2112 //-------------------------------------------------
2113
2114 void ym2610::generate(output_data *output, uint32_t numsamples)
2115 {
2116 // FM output is just repeated the prescale number of times
2117 for (uint32_t samp = 0; samp < numsamples; samp++, output++)
2118 {
2119 if ((m_ssg_resampler.sampindex() + samp) % m_fm_samples_per_output == 0)
2120 clock_fm_and_adpcm();
2121 output->data[0] = m_last_fm.data[0];
2122 output->data[1] = m_last_fm.data[1];
2123 }
2124
2125 // resample the SSG as configured
2126 m_ssg_resampler.resample(output - numsamples, numsamples);
2127 }
2128
2129
2130 //-------------------------------------------------
2131 // update_prescale - update the prescale value,
2132 // recomputing derived values
2133 //-------------------------------------------------
2134
2135 void ym2610::update_prescale()
2136 {
2137 // Fidelity: ---- minimum ---- ---- medium ----- ---- maximum-----
2138 // rate = clock/144 rate = clock/144 rate = clock/16
2139 // Prescale FM rate SSG rate FM rate SSG rate FM rate SSG rate
2140 // 6 1:1 2:9 1:1 2:9 9:1 2:1
2141
2142 // compute the number of FM samples per output sample, and select the
2143 // resampler function
2144 if (m_fidelity == OPN_FIDELITY_MIN || m_fidelity == OPN_FIDELITY_MED)
2145 {
2146 m_fm_samples_per_output = 1;
2147 m_ssg_resampler.configure(2, 9);
2148 }
2149 else
2150 {
2151 m_fm_samples_per_output = 9;
2152 m_ssg_resampler.configure(2, 1);
2153 }
2154
2155 // if overriding the SSG, override the configuration with the nop
2156 // resampler to at least keep the sample index moving forward
2157 if (m_ssg.overridden())
2158 m_ssg_resampler.configure(0, 0);
2159 }
2160
2161
2162 //-------------------------------------------------
2163 // clock_fm_and_adpcm - clock FM and ADPCM state
2164 //-------------------------------------------------
2165
2166 void ym2610::clock_fm_and_adpcm()
2167 {
2168 // clock the system
2169 uint32_t env_counter = m_fm.clock(m_fm_mask);
2170
2171 // clock the ADPCM-A engine on every envelope cycle
2172 if (bitfield(env_counter, 0, 2) == 0)
2173 m_eos_status |= m_adpcm_a.clock(0x3f);
2174
2175 // clock the ADPCM-B engine every cycle
2176 m_adpcm_b.clock();
2177
2178 // we track the last ADPCM-B EOS value in bit 6 (which is hidden from callers);
2179 // if it changed since the last sample, update the visible EOS state in bit 7
2180 uint8_t live_eos = ((m_adpcm_b.status() & adpcm_b_channel::STATUS_EOS) != 0) ? 0x40 : 0x00;
2181 if (((live_eos ^ m_eos_status) & 0x40) != 0)
2182 m_eos_status = (m_eos_status & ~0xc0) | live_eos | (live_eos << 1);
2183
2184 // update the FM content; OPNB is 13-bit with no intermediate clipping
2185 m_fm.output(m_last_fm.clear(), 1, 32767, m_fm_mask);
2186
2187 // mix in the ADPCM and clamp
2188 m_adpcm_a.output(m_last_fm, 0x3f);
2189 m_adpcm_b.output(m_last_fm, 1);
2190 m_last_fm.clamp16();
2191 }
2192
2193
2194
2195 //*********************************************************
2196 // YM2612
2197 //*********************************************************
2198
2199 //-------------------------------------------------
2200 // ym2612 - constructor
2201 //-------------------------------------------------
2202
2203 ym2612::ym2612(ymfm_interface &intf) :
2204 m_address(0),
2205 m_dac_data(0),
2206 m_dac_enable(0),
2207 m_fm(intf)
2208 {
2209 }
2210
2211
2212 //-------------------------------------------------
2213 // reset - reset the system
2214 //-------------------------------------------------
2215
2216 void ym2612::reset()
2217 {
2218 // reset the engines
2219 m_fm.reset();
2220 }
2221
2222
2223 //-------------------------------------------------
2224 // save_restore - save or restore the data
2225 //-------------------------------------------------
2226
2227 void ym2612::save_restore(ymfm_saved_state &state)
2228 {
2229 state.save_restore(m_address);
2230 state.save_restore(m_dac_data);
2231 state.save_restore(m_dac_enable);
2232 m_fm.save_restore(state);
2233 }
2234
2235
2236 //-------------------------------------------------
2237 // read_status - read the status register
2238 //-------------------------------------------------
2239
2240 uint8_t ym2612::read_status()
2241 {
2242 uint8_t result = m_fm.status();
2243 if (m_fm.intf().ymfm_is_busy())
2244 result |= fm_engine::STATUS_BUSY;
2245 return result;
2246 }
2247
2248
2249 //-------------------------------------------------
2250 // read - handle a read from the device
2251 //-------------------------------------------------
2252
2253 uint8_t ym2612::read(uint32_t offset)
2254 {
2255 uint8_t result = 0;
2256 switch (offset & 3)
2257 {
2258 case 0: // status port, YM2203 compatible
2259 result = read_status();
2260 break;
2261
2262 case 1: // data port (unused)
2263 case 2: // status port, extended
2264 case 3: // data port (unused)
2265 debug::log_unexpected_read_write("Unexpected read from YM2612 offset %d\n", offset & 3);
2266 break;
2267 }
2268 return result;
2269 }
2270
2271
2272 //-------------------------------------------------
2273 // write_address - handle a write to the address
2274 // register
2275 //-------------------------------------------------
2276
2277 void ym2612::write_address(uint8_t data)
2278 {
2279 // just set the address
2280 m_address = data;
2281 }
2282
2283
2284 //-------------------------------------------------
2285 // write_data - handle a write to the data
2286 // register
2287 //-------------------------------------------------
2288
2289 void ym2612::write_data(uint8_t data)
2290 {
2291 // ignore if paired with upper address
2292 if (bitfield(m_address, 8))
2293 return;
2294
2295 if (m_address == 0x2a)
2296 {
2297 // 2A: DAC data (most significant 8 bits)
2298 m_dac_data = (m_dac_data & ~0x1fe) | ((data ^ 0x80) << 1);
2299 }
2300 else if (m_address == 0x2b)
2301 {
2302 // 2B: DAC enable (bit 7)
2303 m_dac_enable = bitfield(data, 7);
2304 }
2305 else if (m_address == 0x2c)
2306 {
2307 // 2C: test/low DAC bit
2308 m_dac_data = (m_dac_data & ~1) | bitfield(data, 3);
2309 }
2310 else
2311 {
2312 // 00-29, 2D-FF: write to FM
2313 m_fm.write(m_address, data);
2314 }
2315
2316 // mark busy for a bit
2317 m_fm.intf().ymfm_set_busy_end(32 * m_fm.clock_prescale());
2318 }
2319
2320
2321 //-------------------------------------------------
2322 // write_address_hi - handle a write to the upper
2323 // address register
2324 //-------------------------------------------------
2325
2326 void ym2612::write_address_hi(uint8_t data)
2327 {
2328 // just set the address
2329 m_address = 0x100 | data;
2330 }
2331
2332
2333 //-------------------------------------------------
2334 // write_data_hi - handle a write to the upper
2335 // data register
2336 //-------------------------------------------------
2337
2338 void ym2612::write_data_hi(uint8_t data)
2339 {
2340 // ignore if paired with upper address
2341 if (!bitfield(m_address, 8))
2342 return;
2343
2344 // 100-1FF: write to FM
2345 m_fm.write(m_address, data);
2346
2347 // mark busy for a bit
2348 m_fm.intf().ymfm_set_busy_end(32 * m_fm.clock_prescale());
2349 }
2350
2351
2352 //-------------------------------------------------
2353 // write - handle a write to the register
2354 // interface
2355 //-------------------------------------------------
2356
2357 void ym2612::write(uint32_t offset, uint8_t data)
2358 {
2359 switch (offset & 3)
2360 {
2361 case 0: // address port
2362 write_address(data);
2363 break;
2364
2365 case 1: // data port
2366 write_data(data);
2367 break;
2368
2369 case 2: // upper address port
2370 write_address_hi(data);
2371 break;
2372
2373 case 3: // upper data port
2374 write_data_hi(data);
2375 break;
2376 }
2377 }
2378
2379
2380 //-------------------------------------------------
2381 // generate - generate one sample of sound
2382 //-------------------------------------------------
2383
2384 void ym2612::generate(output_data *output, uint32_t numsamples)
2385 {
2386 for (uint32_t samp = 0; samp < numsamples; samp++, output++)
2387 {
2388 // clock the system
2389 m_fm.clock(fm_engine::ALL_CHANNELS);
2390
2391 // sum individual channels to apply DAC discontinuity on each
2392 output->clear();
2393 output_data temp;
2394
2395 // first do FM-only channels; OPN2 is 9-bit with intermediate clipping
2396 int const last_fm_channel = m_dac_enable ? 5 : 6;
2397 for (int chan = 0; chan < last_fm_channel; chan++)
2398 {
2399 m_fm.output(temp.clear(), 5, 256, 1 << chan);
2400 output->data[0] += dac_discontinuity(temp.data[0]);
2401 output->data[1] += dac_discontinuity(temp.data[1]);
2402 }
2403
2404 // add in DAC
2405 if (m_dac_enable)
2406 {
2407 // DAC enabled: start with DAC value then add the first 5 channels only
2408 int32_t dacval = dac_discontinuity(int16_t(m_dac_data << 7) >> 7);
2409 output->data[0] += m_fm.regs().ch_output_0(0x102) ? dacval : dac_discontinuity(0);
2410 output->data[1] += m_fm.regs().ch_output_1(0x102) ? dacval : dac_discontinuity(0);
2411 }
2412
2413 // output is technically multiplexed rather than mixed, but that requires
2414 // a better sound mixer than we usually have, so just average over the six
2415 // channels; also apply a 64/65 factor to account for the discontinuity
2416 // adjustment above
2417 output->data[0] = (output->data[0] * 128) * 64 / (6 * 65);
2418 output->data[1] = (output->data[1] * 128) * 64 / (6 * 65);
2419 }
2420 }
2421
2422
2423 //-------------------------------------------------
2424 // generate - generate one sample of sound
2425 //-------------------------------------------------
2426
2427 void ym3438::generate(output_data *output, uint32_t numsamples)
2428 {
2429 for (uint32_t samp = 0; samp < numsamples; samp++, output++)
2430 {
2431 // clock the system
2432 m_fm.clock(fm_engine::ALL_CHANNELS);
2433
2434 // first do FM-only channels; OPN2C is 9-bit with intermediate clipping
2435 if (!m_dac_enable)
2436 {
2437 // DAC disabled: all 6 channels sum together
2438 m_fm.output(output->clear(), 5, 256, fm_engine::ALL_CHANNELS);
2439 }
2440 else
2441 {
2442 // DAC enabled: start with DAC value then add the first 5 channels only
2443 int32_t dacval = int16_t(m_dac_data << 7) >> 7;
2444 output->data[0] = m_fm.regs().ch_output_0(0x102) ? dacval : 0;
2445 output->data[1] = m_fm.regs().ch_output_1(0x102) ? dacval : 0;
2446 m_fm.output(*output, 5, 256, fm_engine::ALL_CHANNELS ^ (1 << 5));
2447 }
2448
2449 // YM3438 doesn't have the same DAC discontinuity, though its output is
2450 // multiplexed like the YM2612
2451 output->data[0] = (output->data[0] * 128) / 6;
2452 output->data[1] = (output->data[1] * 128) / 6;
2453 }
2454 }
2455
2456
2457 //-------------------------------------------------
2458 // generate - generate one sample of sound
2459 //-------------------------------------------------
2460
2461 void ymf276::generate(output_data *output, uint32_t numsamples)
2462 {
2463 for (uint32_t samp = 0; samp < numsamples; samp++, output++)
2464 {
2465 // clock the system
2466 m_fm.clock(fm_engine::ALL_CHANNELS);
2467
2468 // first do FM-only channels; OPN2L is 14-bit with intermediate clipping
2469 if (!m_dac_enable)
2470 {
2471 // DAC disabled: all 6 channels sum together
2472 m_fm.output(output->clear(), 0, 8191, fm_engine::ALL_CHANNELS);
2473 }
2474 else
2475 {
2476 // DAC enabled: start with DAC value then add the first 5 channels only
2477 int32_t dacval = int16_t(m_dac_data << 7) >> 7;
2478 output->data[0] = m_fm.regs().ch_output_0(0x102) ? dacval : 0;
2479 output->data[1] = m_fm.regs().ch_output_1(0x102) ? dacval : 0;
2480 m_fm.output(*output, 0, 8191, fm_engine::ALL_CHANNELS ^ (1 << 5));
2481 }
2482
2483 // YMF276 is properly mixed; it shifts down 1 bit before clamping
2484 output->data[0] = clamp(output->data[0] >> 1, -32768, 32767);
2485 output->data[1] = clamp(output->data[1] >> 1, -32768, 32767);
2486 }
2487 }
2488
2489 }