Debian Codegraph @

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