Debian Codegraph @

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