Debian Codegraph @

src/engine/platform/sound/ymfm/ymfm_opm.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_opm.h"
32 #include "ymfm_fm.ipp"
33
34 namespace ymfm
35 {
36
37 //*********************************************************
38 // OPM REGISTERS
39 //*********************************************************
40
41 //-------------------------------------------------
42 // opm_registers - constructor
43 //-------------------------------------------------
44
45 opm_registers::opm_registers() :
46 m_lfo_counter(0),
47 m_noise_lfsr(1),
48 m_noise_counter(0),
49 m_noise_state(0),
50 m_noise_lfo(0),
51 m_lfo_am(0)
52 {
53 // create the waveforms
54 for (uint32_t index = 0; index < WAVEFORM_LENGTH; index++)
55 m_waveform[0][index] = abs_sin_attenuation(index) | (bitfield(index, 9) << 15);
56
57 // create the LFO waveforms; AM in the low 8 bits, PM in the upper 8
58 // waveforms are adjusted to match the pictures in the application manual
59 for (uint32_t index = 0; index < LFO_WAVEFORM_LENGTH; index++)
60 {
61 // waveform 0 is a sawtooth
62 uint8_t am = index ^ 0xff;
63 int8_t pm = int8_t(index);
64 m_lfo_waveform[0][index] = am | (pm << 8);
65
66 // waveform 1 is a square wave
67 am = bitfield(index, 7) ? 0 : 0xff;
68 pm = int8_t(am ^ 0x80);
69 m_lfo_waveform[1][index] = am | (pm << 8);
70
71 // waveform 2 is a triangle wave
72 am = bitfield(index, 7) ? (index << 1) : ((index ^ 0xff) << 1);
73 pm = int8_t(bitfield(index, 6) ? am : ~am);
74 m_lfo_waveform[2][index] = am | (pm << 8);
75
76 // waveform 3 is noise; it is filled in dynamically
77 m_lfo_waveform[3][index] = 0;
78 }
79 }
80
81
82 //-------------------------------------------------
83 // reset - reset to initial state
84 //-------------------------------------------------
85
86 void opm_registers::reset()
87 {
88 std::fill_n(&m_regdata[0], REGISTERS, 0);
89
90 m_lfo_counter = 0;
91
92 // enable output on both channels by default
93 m_regdata[0x20] = m_regdata[0x21] = m_regdata[0x22] = m_regdata[0x23] = 0xc0;
94 m_regdata[0x24] = m_regdata[0x25] = m_regdata[0x26] = m_regdata[0x27] = 0xc0;
95 }
96
97
98 //-------------------------------------------------
99 // save_restore - save or restore the data
100 //-------------------------------------------------
101
102 void opm_registers::save_restore(ymfm_saved_state &state)
103 {
104 state.save_restore(m_lfo_counter);
105 state.save_restore(m_lfo_am);
106 state.save_restore(m_noise_lfsr);
107 state.save_restore(m_noise_counter);
108 state.save_restore(m_noise_state);
109 state.save_restore(m_noise_lfo);
110 state.save_restore(m_regdata);
111 }
112
113
114 //-------------------------------------------------
115 // operator_map - return an array of operator
116 // indices for each channel; for OPM this is fixed
117 //-------------------------------------------------
118
119 void opm_registers::operator_map(operator_mapping &dest) const
120 {
121 // Note that the channel index order is 0,2,1,3, so we bitswap the index.
122 //
123 // This is because the order in the map is:
124 // carrier 1, carrier 2, modulator 1, modulator 2
125 //
126 // But when wiring up the connections, the more natural order is:
127 // carrier 1, modulator 1, carrier 2, modulator 2
128 static const operator_mapping s_fixed_map =
129 { {
130 operator_list( 0, 16, 8, 24 ), // Channel 0 operators
131 operator_list( 1, 17, 9, 25 ), // Channel 1 operators
132 operator_list( 2, 18, 10, 26 ), // Channel 2 operators
133 operator_list( 3, 19, 11, 27 ), // Channel 3 operators
134 operator_list( 4, 20, 12, 28 ), // Channel 4 operators
135 operator_list( 5, 21, 13, 29 ), // Channel 5 operators
136 operator_list( 6, 22, 14, 30 ), // Channel 6 operators
137 operator_list( 7, 23, 15, 31 ), // Channel 7 operators
138 } };
139 dest = s_fixed_map;
140 }
141
142
143 //-------------------------------------------------
144 // write - handle writes to the register array
145 //-------------------------------------------------
146
147 bool opm_registers::write(uint16_t index, uint8_t data, uint32_t &channel, uint32_t &opmask)
148 {
149 assert(index < REGISTERS);
150
151 // LFO AM/PM depth are written to the same register (0x19);
152 // redirect the PM depth to an unused neighbor (0x1a)
153 if (index == 0x19)
154 m_regdata[index + bitfield(data, 7)] = data;
155 else if (index != 0x1a)
156 m_regdata[index] = data;
157
158 // handle writes to the key on index
159 if (index == 0x08)
160 {
161 channel = bitfield(data, 0, 3);
162 opmask = bitfield(data, 3, 4);
163 return true;
164 }
165 return false;
166 }
167
168
169 //-------------------------------------------------
170 // clock_noise_and_lfo - clock the noise and LFO,
171 // handling clock division, depth, and waveform
172 // computations
173 //-------------------------------------------------
174
175 int32_t opm_registers::clock_noise_and_lfo()
176 {
177 // base noise frequency is measured at 2x 1/2 FM frequency; this
178 // means each tick counts as two steps against the noise counter
179 uint32_t freq = noise_frequency() ^ 0x1f;
180 for (int rep = 0; rep < 2; rep++)
181 {
182 // evidence seems to suggest the LFSR is clocked continually and just
183 // sampled at the noise frequency for output purposes; note that the
184 // low 8 bits are the most recent 8 bits of history while bits 8-24
185 // contain the 17 bit LFSR state
186 m_noise_lfsr <<= 1;
187 m_noise_lfsr |= bitfield(m_noise_lfsr, 17) ^ bitfield(m_noise_lfsr, 14) ^ 1;
188
189 // compare against the frequency and latch when we exceed it
190 if (m_noise_counter++ >= freq)
191 {
192 m_noise_counter = 0;
193 m_noise_state = bitfield(m_noise_lfsr, 17);
194 }
195 }
196
197 // treat the rate as a 4.4 floating-point step value with implied
198 // leading 1; this matches exactly the frequencies in the application
199 // manual, though it might not be implemented exactly this way on chip
200 // note from tildearrow:
201 // - in fact it doesn't. the strings in Scherzo Di Notte totally go out
202 // tune after a bit (and this doesn't happen in Nuked-OPM).
203 uint32_t rate = lfo_rate();
204 if (rate != 0) {
205 m_lfo_counter += (0x10 | bitfield(rate, 0, 4)) << bitfield(rate, 4, 4);
206 }
207
208 // bit 1 of the test register is officially undocumented but has been
209 // discovered to hold the LFO in reset while active
210 if (lfo_reset())
211 m_lfo_counter = 0;
212
213 // now pull out the non-fractional LFO value
214 uint32_t lfo = bitfield(m_lfo_counter, 22, 8);
215
216 // fill in the noise entry 1 ahead of our current position; this
217 // ensures the current value remains stable for a full LFO clock
218 // and effectively latches the running value when the LFO advances
219 uint32_t lfo_noise = bitfield(m_noise_lfsr, 17, 8);
220 m_lfo_waveform[3][(lfo + 1) & 0xff] = lfo_noise | (lfo_noise << 8);
221
222 // fetch the AM/PM values based on the waveform; AM is unsigned and
223 // encoded in the low 8 bits, while PM signed and encoded in the upper
224 // 8 bits
225 int32_t ampm = m_lfo_waveform[lfo_waveform()][lfo];
226
227 // apply depth to the AM value and store for later
228 m_lfo_am = ((ampm & 0xff) * lfo_am_depth()) >> 7;
229
230 // apply depth to the PM value and return it
231 return ((ampm >> 8) * int32_t(lfo_pm_depth())) >> 7;
232 }
233
234
235 //-------------------------------------------------
236 // lfo_am_offset - return the AM offset from LFO
237 // for the given channel
238 //-------------------------------------------------
239
240 uint32_t opm_registers::lfo_am_offset(uint32_t choffs) const
241 {
242 // OPM maps AM quite differently from OPN
243
244 // shift value for AM sensitivity is [*, 0, 1, 2],
245 // mapping to values of [0, 23.9, 47.8, and 95.6dB]
246 uint32_t am_sensitivity = ch_lfo_am_sens(choffs);
247 if (am_sensitivity == 0)
248 return 0;
249
250 // QUESTION: see OPN note below for the dB range mapping; it applies
251 // here as well
252
253 // raw LFO AM value on OPM is 0-FF, which is already a factor of 2
254 // larger than the OPN below, putting our staring point at 2x theirs;
255 // this works out since our minimum is 2x their maximum
256 return m_lfo_am << (am_sensitivity - 1);
257 }
258
259
260 //-------------------------------------------------
261 // cache_operator_data - fill the operator cache
262 // with prefetched data
263 //-------------------------------------------------
264
265 void opm_registers::cache_operator_data(uint32_t choffs, uint32_t opoffs, opdata_cache &cache)
266 {
267 // set up the easy stuff
268 cache.waveform = &m_waveform[0][0];
269
270 // get frequency from the channel
271 uint32_t block_freq = cache.block_freq = ch_block_freq(choffs);
272
273 // compute the keycode: block_freq is:
274 //
275 // BBBCCCCFFFFFF
276 // ^^^^^
277 //
278 // the 5-bit keycode is just the top 5 bits (block + top 2 bits
279 // of the key code)
280 uint32_t keycode = bitfield(block_freq, 8, 5);
281
282 // detune adjustment
283 cache.detune = detune_adjustment(op_detune(opoffs), keycode);
284
285 // multiple value, as an x.1 value (0 means 0.5)
286 cache.multiple = op_multiple(opoffs) * 2;
287 if (cache.multiple == 0)
288 cache.multiple = 1;
289
290 // phase step, or PHASE_STEP_DYNAMIC if PM is active; this depends on
291 // block_freq, detune, and multiple, so compute it after we've done those
292 if (lfo_pm_depth() == 0 || ch_lfo_pm_sens(choffs) == 0)
293 cache.phase_step = compute_phase_step(choffs, opoffs, cache, 0);
294 else
295 cache.phase_step = opdata_cache::PHASE_STEP_DYNAMIC;
296
297 // total level, scaled by 8
298 cache.total_level = op_total_level(opoffs) << 3;
299
300 // 4-bit sustain level, but 15 means 31 so effectively 5 bits
301 cache.eg_sustain = op_sustain_level(opoffs);
302 cache.eg_sustain |= (cache.eg_sustain + 1) & 0x10;
303 cache.eg_sustain <<= 5;
304
305 // determine KSR adjustment for enevlope rates
306 uint32_t ksrval = keycode >> (op_ksr(opoffs) ^ 3);
307 cache.eg_rate[EG_ATTACK] = effective_rate(op_attack_rate(opoffs) * 2, ksrval);
308 cache.eg_rate[EG_DECAY] = effective_rate(op_decay_rate(opoffs) * 2, ksrval);
309 cache.eg_rate[EG_SUSTAIN] = effective_rate(op_sustain_rate(opoffs) * 2, ksrval);
310 cache.eg_rate[EG_RELEASE] = effective_rate(op_release_rate(opoffs) * 4 + 2, ksrval);
311 }
312
313
314 //-------------------------------------------------
315 // compute_phase_step - compute the phase step
316 //-------------------------------------------------
317
318 uint32_t opm_registers::compute_phase_step(uint32_t choffs, uint32_t opoffs, opdata_cache const &cache, int32_t lfo_raw_pm)
319 {
320 // OPM logic is rather unique here, due to extra detune
321 // and the use of key codes (not to be confused with keycode)
322
323 // start with coarse detune delta; table uses cents value from
324 // manual, converted into 1/64ths
325 static const int16_t s_detune2_delta[4] = { 0, (600*64+50)/100, (781*64+50)/100, (950*64+50)/100 };
326 int32_t delta = s_detune2_delta[op_detune2(opoffs)];
327
328 // add in the PM delta
329 uint32_t pm_sensitivity = ch_lfo_pm_sens(choffs);
330 if (pm_sensitivity != 0)
331 {
332 // raw PM value is -127..128 which is +/- 200 cents
333 // manual gives these magnitudes in cents:
334 // 0, +/-5, +/-10, +/-20, +/-50, +/-100, +/-400, +/-700
335 // this roughly corresponds to shifting the 200-cent value:
336 // 0 >> 5, >> 4, >> 3, >> 2, >> 1, << 1, << 2
337 if (pm_sensitivity < 6)
338 delta += lfo_raw_pm >> (6 - pm_sensitivity);
339 else
340 delta += lfo_raw_pm << (pm_sensitivity - 5);
341 }
342
343 // apply delta and convert to a frequency number
344 uint32_t phase_step = opm_key_code_to_phase_step(cache.block_freq, delta);
345
346 // apply detune based on the keycode
347 phase_step += cache.detune;
348
349 // apply frequency multiplier (which is cached as an x.1 value)
350 return (phase_step * cache.multiple) >> 1;
351 }
352
353
354 //-------------------------------------------------
355 // log_keyon - log a key-on event
356 //-------------------------------------------------
357
358 std::string opm_registers::log_keyon(uint32_t choffs, uint32_t opoffs)
359 {
360 uint32_t chnum = choffs;
361 uint32_t opnum = opoffs;
362
363 char buffer[256];
364 char *end = &buffer[0];
365
366 end += snprintf(end, 256-(end-buffer), "%u.%02u freq=%04X dt2=%u dt=%u fb=%u alg=%X mul=%X tl=%02X ksr=%u adsr=%02X/%02X/%02X/%X sl=%X out=%c%c",
367 chnum, opnum,
368 ch_block_freq(choffs),
369 op_detune2(opoffs),
370 op_detune(opoffs),
371 ch_feedback(choffs),
372 ch_algorithm(choffs),
373 op_multiple(opoffs),
374 op_total_level(opoffs),
375 op_ksr(opoffs),
376 op_attack_rate(opoffs),
377 op_decay_rate(opoffs),
378 op_sustain_rate(opoffs),
379 op_release_rate(opoffs),
380 op_sustain_level(opoffs),
381 ch_output_0(choffs) ? 'L' : '-',
382 ch_output_1(choffs) ? 'R' : '-');
383
384 bool am = (lfo_am_depth() != 0 && ch_lfo_am_sens(choffs) != 0 && op_lfo_am_enable(opoffs) != 0);
385 if (am)
386 end += snprintf(end, 256-(end-buffer), " am=%u/%02X", ch_lfo_am_sens(choffs), lfo_am_depth());
387 bool pm = (lfo_pm_depth() != 0 && ch_lfo_pm_sens(choffs) != 0);
388 if (pm)
389 end += snprintf(end, 256-(end-buffer), " pm=%u/%02X", ch_lfo_pm_sens(choffs), lfo_pm_depth());
390 if (am || pm)
391 end += snprintf(end, 256-(end-buffer), " lfo=%02X/%c", lfo_rate(), "WQTN"[lfo_waveform()]);
392 if (noise_enable() && opoffs == 31)
393 end += snprintf(end, 256-(end-buffer), " noise=1");
394
395 return buffer;
396 }
397
398
399
400 //*********************************************************
401 // YM2151
402 //*********************************************************
403
404 //-------------------------------------------------
405 // ym2151 - constructor
406 //-------------------------------------------------
407
408 ym2151::ym2151(ymfm_interface &intf, opm_variant variant) :
409 m_variant(variant),
410 m_address(0),
411 m_fm(intf)
412 {
413 }
414
415
416 //-------------------------------------------------
417 // reset - reset the system
418 //-------------------------------------------------
419
420 void ym2151::reset()
421 {
422 // reset the engines
423 m_fm.reset();
424 }
425
426
427 //-------------------------------------------------
428 // save_restore - save or restore the data
429 //-------------------------------------------------
430
431 void ym2151::save_restore(ymfm_saved_state &state)
432 {
433 m_fm.save_restore(state);
434 state.save_restore(m_address);
435 }
436
437
438 //-------------------------------------------------
439 // read_status - read the status register
440 //-------------------------------------------------
441
442 uint8_t ym2151::read_status()
443 {
444 uint8_t result = m_fm.status();
445 if (m_fm.intf().ymfm_is_busy())
446 result |= fm_engine::STATUS_BUSY;
447 return result;
448 }
449
450
451 //-------------------------------------------------
452 // read - handle a read from the device
453 //-------------------------------------------------
454
455 uint8_t ym2151::read(uint32_t offset)
456 {
457 uint8_t result = 0xff;
458 switch (offset & 1)
459 {
460 case 0: // data port (unused)
461 debug::log_unexpected_read_write("Unexpected read from YM2151 offset %d\n", offset & 3);
462 break;
463
464 case 1: // status port, YM2203 compatible
465 result = read_status();
466 break;
467 }
468 return result;
469 }
470
471
472 //-------------------------------------------------
473 // write_address - handle a write to the address
474 // register
475 //-------------------------------------------------
476
477 void ym2151::write_address(uint8_t data)
478 {
479 // just set the address
480 m_address = data;
481 }
482
483
484 //-------------------------------------------------
485 // write - handle a write to the register
486 // interface
487 //-------------------------------------------------
488
489 void ym2151::write_data(uint8_t data)
490 {
491 // write the FM register
492 m_fm.write(m_address, data);
493
494 // special cases
495 if (m_address == 0x1b)
496 {
497 // writes to register 0x1B send the upper 2 bits to the output lines
498 m_fm.intf().ymfm_external_write(ACCESS_IO, 0, data >> 6);
499 }
500
501 // mark busy for a bit
502 m_fm.intf().ymfm_set_busy_end(32 * m_fm.clock_prescale());
503 }
504
505
506 //-------------------------------------------------
507 // write - handle a write to the register
508 // interface
509 //-------------------------------------------------
510
511 void ym2151::write(uint32_t offset, uint8_t data)
512 {
513 switch (offset & 1)
514 {
515 case 0: // address port
516 write_address(data);
517 break;
518
519 case 1: // data port
520 write_data(data);
521 break;
522 }
523 }
524
525
526 //-------------------------------------------------
527 // generate - generate one sample of sound
528 //-------------------------------------------------
529
530 void ym2151::generate(output_data *output, uint32_t numsamples)
531 {
532 for (uint32_t samp = 0; samp < numsamples; samp++, output++)
533 {
534 // clock the system
535 m_fm.clock(fm_engine::ALL_CHANNELS);
536
537 // update the FM content; OPM is full 14-bit with no intermediate clipping
538 m_fm.output(output->clear(), 0, 32767, fm_engine::ALL_CHANNELS);
539
540 // YM2151 uses an external DAC (YM3012) with mantissa/exponent format
541 // convert to 10.3 floating point value and back to simulate truncation
542 output->roundtrip_fp();
543 }
544 }
545
546 }