Debian Codegraph @

src/engine/platform/sound/ymfm/ymfm_opz.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_opz.h"
32 #include "ymfm_fm.ipp"
33
34 #define TEMPORARY_DEBUG_PRINTS (0)
35
36 //
37 // OPZ (aka YM2414)
38 //
39 // This chip is not officially documented as far as I know. What I have
40 // comes from this site:
41 //
42 // http://sr4.sakura.ne.jp/fmsound/opz.html
43 //
44 // and from reading the TX81Z operator manual, which describes how a number
45 // of these new features work.
46 //
47 // OPZ appears be bsaically OPM with a bunch of extra features.
48 //
49 // For starters, there are two LFO generators. I have presumed that they
50 // operate identically since identical parameters are offered for each. I
51 // have also presumed the effects are additive between them. The LFOs on
52 // the OPZ have an extra "sync" option which apparently causes the LFO to
53 // reset whenever a key on is received.
54 //
55 // At the channel level, there is an additional 8-bit volume control. This
56 // might work as an addition to total level, or some other way. Completely
57 // unknown, and unimplemented.
58 //
59 // At the operator level, there are a number of extra features. First, there
60 // are 8 different waveforms to choose from. These are different than the
61 // waveforms introduced in the OPL2 and later chips.
62 //
63 // Second, there is an additional "reverb" stage added to the envelope
64 // generator, which kicks in when the envelope reaches -18dB. It specifies
65 // a slower decay rate to produce a sort of faux reverb effect.
66 //
67 // The envelope generator also supports a 2-bit shift value, which can be
68 // used to reduce the effect of the envelope attenuation.
69 //
70 // OPZ supports a "fixed frequency" mode for each operator, with a 3-bit
71 // range and 4-bit frequency value, plus a 1-bit enable. Not sure how that
72 // works at all, so it's not implemented.
73 // note by tildearrow:
74 // - I have verified behavior of this mode against real hardware.
75 // after applying a small fix on the existing early implementation, it matches hardware.
76 // this means fixed frequency is fully implemented and working.
77 //
78 // There are also several mystery fields in the operators which I have no
79 // clue about: "fine" (4 bits), "eg_shift" (2 bits), and "rev" (3 bits).
80 // eg_shift is some kind of envelope generator effect, but how it works is
81 // unknown.
82 // note by tildearrow:
83 // - behavior of "fine" is now confirmed and matches hardware.
84 //
85 // Also, according to the site above, the panning controls are changed from
86 // OPM, with a "mono" bit and only one control bit for the right channel.
87 // Current implementation is just a guess.
88 //
89 // additional modifications by tildearrow for Furnace
90 //
91
92 namespace ymfm
93 {
94
95 //*********************************************************
96 // OPZ REGISTERS
97 //*********************************************************
98
99 //-------------------------------------------------
100 // opz_registers - constructor
101 //-------------------------------------------------
102
103 opz_registers::opz_registers() :
104 m_lfo_counter{ 0, 0 },
105 m_noise_lfsr(1),
106 m_noise_counter(0),
107 m_noise_state(0),
108 m_noise_lfo(0),
109 m_lfo_am{ 0, 0 }
110 {
111 // create the waveforms
112 for (uint32_t index = 0; index < WAVEFORM_LENGTH; index++)
113 m_waveform[0][index] = abs_sin_attenuation(index) | (bitfield(index, 9) << 15);
114
115 // we only have the diagrams to judge from, but suspecting waveform 1 (and
116 // derived waveforms) are sin^2, based on OPX description of similar wave-
117 // forms; since our sin table is logarithmic, this ends up just being
118 // 2*existing value
119 uint16_t zeroval = m_waveform[0][0];
120 for (uint32_t index = 0; index < WAVEFORM_LENGTH; index++)
121 m_waveform[1][index] = std::min<uint16_t>(2 * (m_waveform[0][index] & 0x7fff), zeroval) | (bitfield(index, 9) << 15);
122
123 // remaining waveforms are just derivations of the 2 main ones
124 for (uint32_t index = 0; index < WAVEFORM_LENGTH; index++)
125 {
126 m_waveform[2][index] = bitfield(index, 9) ? zeroval : m_waveform[0][index];
127 m_waveform[3][index] = bitfield(index, 9) ? zeroval : m_waveform[1][index];
128 m_waveform[4][index] = bitfield(index, 9) ? zeroval : m_waveform[0][index * 2];
129 m_waveform[5][index] = bitfield(index, 9) ? zeroval : m_waveform[1][index * 2];
130 m_waveform[6][index] = bitfield(index, 9) ? zeroval : m_waveform[0][(index * 2) & 0x1ff];
131 m_waveform[7][index] = bitfield(index, 9) ? zeroval : m_waveform[1][(index * 2) & 0x1ff];
132 }
133
134 // create the LFO waveforms; AM in the low 8 bits, PM in the upper 8
135 // waveforms are adjusted to match the pictures in the application manual
136 for (uint32_t index = 0; index < LFO_WAVEFORM_LENGTH; index++)
137 {
138 // waveform 0 is a sawtooth
139 uint8_t am = index ^ 0xff;
140 int8_t pm = int8_t(index);
141 m_lfo_waveform[0][index] = am | (pm << 8);
142
143 // waveform 1 is a square wave
144 am = bitfield(index, 7) ? 0 : 0xff;
145 pm = int8_t(am ^ 0x80);
146 m_lfo_waveform[1][index] = am | (pm << 8);
147
148 // waveform 2 is a triangle wave
149 am = bitfield(index, 7) ? (index << 1) : ((index ^ 0xff) << 1);
150 pm = int8_t(bitfield(index, 6) ? am : ~am);
151 m_lfo_waveform[2][index] = am | (pm << 8);
152
153 // waveform 3 is noise; it is filled in dynamically
154 }
155 }
156
157
158 //-------------------------------------------------
159 // reset - reset to initial state
160 //-------------------------------------------------
161
162 void opz_registers::reset()
163 {
164 std::fill_n(&m_regdata[0], REGISTERS, 0);
165
166 // enable output on both channels by default
167 m_regdata[0x30] = m_regdata[0x31] = m_regdata[0x32] = m_regdata[0x33] = 0x01;
168 m_regdata[0x34] = m_regdata[0x35] = m_regdata[0x36] = m_regdata[0x37] = 0x01;
169 }
170
171
172 //-------------------------------------------------
173 // save_restore - save or restore the data
174 //-------------------------------------------------
175
176 void opz_registers::save_restore(ymfm_saved_state &state)
177 {
178 state.save_restore(m_lfo_counter);
179 state.save_restore(m_lfo_am);
180 state.save_restore(m_noise_lfsr);
181 state.save_restore(m_noise_counter);
182 state.save_restore(m_noise_state);
183 state.save_restore(m_noise_lfo);
184 state.save_restore(m_regdata);
185 state.save_restore(m_phase_substep);
186 }
187
188
189 //-------------------------------------------------
190 // operator_map - return an array of operator
191 // indices for each channel; for OPZ this is fixed
192 //-------------------------------------------------
193
194 void opz_registers::operator_map(operator_mapping &dest) const
195 {
196 // Note that the channel index order is 0,2,1,3, so we bitswap the index.
197 //
198 // This is because the order in the map is:
199 // carrier 1, carrier 2, modulator 1, modulator 2
200 //
201 // But when wiring up the connections, the more natural order is:
202 // carrier 1, modulator 1, carrier 2, modulator 2
203 static const operator_mapping s_fixed_map =
204 { {
205 operator_list( 0, 16, 8, 24 ), // Channel 0 operators
206 operator_list( 1, 17, 9, 25 ), // Channel 1 operators
207 operator_list( 2, 18, 10, 26 ), // Channel 2 operators
208 operator_list( 3, 19, 11, 27 ), // Channel 3 operators
209 operator_list( 4, 20, 12, 28 ), // Channel 4 operators
210 operator_list( 5, 21, 13, 29 ), // Channel 5 operators
211 operator_list( 6, 22, 14, 30 ), // Channel 6 operators
212 operator_list( 7, 23, 15, 31 ), // Channel 7 operators
213 } };
214 dest = s_fixed_map;
215 }
216
217
218 //-------------------------------------------------
219 // write - handle writes to the register array
220 //-------------------------------------------------
221
222 bool opz_registers::write(uint16_t index, uint8_t data, uint32_t &channel, uint32_t &opmask)
223 {
224 assert(index < REGISTERS);
225
226 // special mappings:
227 // 0x16 -> 0x188 if bit 7 is set
228 // 0x19 -> 0x189 if bit 7 is set
229 // 0x38..0x3F -> 0x180..0x187 if bit 7 is set
230 // 0x40..0x5F -> 0x100..0x11F if bit 7 is set
231 // 0xC0..0xDF -> 0x120..0x13F if bit 5 is set
232 if (index == 0x17 && bitfield(data, 7) != 0)
233 m_regdata[0x188] = data;
234 else if (index == 0x19 && bitfield(data, 7) != 0)
235 m_regdata[0x189] = data;
236 else if ((index & 0xf8) == 0x38 && bitfield(data, 7) != 0)
237 m_regdata[0x180 + (index & 7)] = data;
238 else if ((index & 0xe0) == 0x40 && bitfield(data, 7) != 0)
239 m_regdata[0x100 + (index & 0x1f)] = data;
240 else if ((index & 0xe0) == 0xc0 && bitfield(data, 5) != 0)
241 m_regdata[0x120 + (index & 0x1f)] = data;
242 else if (index < 0x100)
243 m_regdata[index] = data;
244
245 // preset writes restore some values from a preset memory; not sure
246 // how this really works but the TX81Z will overwrite the sustain level/
247 // release rate register and the envelope shift/reverb rate register to
248 // dampen sound, then write the preset number to register 8 to restore them
249 if (index == 0x08)
250 {
251 int chan = bitfield(data, 0, 3);
252 if (TEMPORARY_DEBUG_PRINTS)
253 printf("Loading preset %d\n", chan);
254 m_regdata[0xe0 + chan + 0] = m_regdata[0x140 + chan + 0];
255 m_regdata[0xe0 + chan + 8] = m_regdata[0x140 + chan + 8];
256 m_regdata[0xe0 + chan + 16] = m_regdata[0x140 + chan + 16];
257 m_regdata[0xe0 + chan + 24] = m_regdata[0x140 + chan + 24];
258 m_regdata[0x120 + chan + 0] = m_regdata[0x160 + chan + 0];
259 m_regdata[0x120 + chan + 8] = m_regdata[0x160 + chan + 8];
260 m_regdata[0x120 + chan + 16] = m_regdata[0x160 + chan + 16];
261 m_regdata[0x120 + chan + 24] = m_regdata[0x160 + chan + 24];
262 }
263
264 // store the presets under some unknown condition; the pattern of writes
265 // when setting a new preset is:
266 //
267 // 08 (0-7), 80-9F, A0-BF, C0-DF, C0-DF (alt), 20-27, 40-5F, 40-5F (alt),
268 // C0-DF (alt -- again?), 38-3F, 1B, 18, E0-FF
269 //
270 // So it writes 0-7 to 08 to either reset all presets or to indicate
271 // that we're going to be loading them. Immediately after all the writes
272 // above, the very next write will be temporary values to blow away the
273 // values loaded into E0-FF, so somehow it also knows that anything after
274 // that point is not part of the preset.
275 //
276 // For now, try using the 40-5F (alt) writes as flags that presets are
277 // being loaded until the E0-FF writes happen.
278 bool is_setting_preset = (bitfield(m_regdata[0x100 + (index & 0x1f)], 7) != 0);
279 if (is_setting_preset)
280 {
281 //printf("ISP\n");
282 if ((index & 0xe0) == 0xe0)
283 {
284 m_regdata[0x140 + (index & 0x1f)] = data;
285 m_regdata[0x100 + (index & 0x1f)] &= 0x7f;
286 }
287 else if ((index & 0xe0) == 0xc0 && bitfield(data, 5) != 0)
288 m_regdata[0x160 + (index & 0x1f)] = data;
289 }
290
291 // handle writes to the key on index
292
293 // note from tildearrow:
294 // - are you kidding? I have to write to this "load preset" register before keying on?
295 // another note from tildearrow:
296 // - see https://github.com/110-kenichi/ymfm/blob/main/src/ymfm_opz.cpp
297 // - is 0x08 the actual key on register just like OPM?
298 // - if so then what's bit 5?
299 if ((index & 0xf8) == 0x20 /*&& bitfield(index, 0, 3) == bitfield(m_regdata[0x08], 0, 3)*/)
300 {
301 channel = bitfield(index, 0, 3);
302 opmask = ch_key_on(channel) ? 0xf : 0;
303 //printf("%d opmask is %d\n",opmask,channel);
304
305 // according to the TX81Z manual, the sync option causes the LFOs
306 // to reset at each note on
307 if (opmask != 0)
308 {
309 if (lfo_sync())
310 m_lfo_counter[0] = 0;
311 if (lfo2_sync())
312 m_lfo_counter[1] = 0;
313 }
314 return true;
315 }
316 return false;
317 }
318
319
320 //-------------------------------------------------
321 // clock_noise_and_lfo - clock the noise and LFO,
322 // handling clock division, depth, and waveform
323 // computations
324 //-------------------------------------------------
325
326 int32_t opz_registers::clock_noise_and_lfo()
327 {
328 // base noise frequency is measured at 2x 1/2 FM frequency; this
329 // means each tick counts as two steps against the noise counter
330 uint32_t freq = noise_frequency() ^ 0x1f;
331 for (int rep = 0; rep < 2; rep++)
332 {
333 // evidence seems to suggest the LFSR is clocked continually and just
334 // sampled at the noise frequency for output purposes; note that the
335 // low 8 bits are the most recent 8 bits of history while bits 8-24
336 // contain the 17 bit LFSR state
337 m_noise_lfsr <<= 1;
338 m_noise_lfsr |= bitfield(m_noise_lfsr, 17) ^ bitfield(m_noise_lfsr, 14) ^ 1;
339
340 // compare against the frequency and latch when we exceed it
341 if (m_noise_counter++ >= freq)
342 {
343 m_noise_counter = 0;
344 m_noise_state = bitfield(m_noise_lfsr, 17);
345 }
346 }
347
348 // treat the rate as a 4.4 floating-point step value with implied
349 // leading 1; this matches exactly the frequencies in the application
350 // manual, though it might not be implemented exactly this way on chip
351 uint32_t rate0 = lfo_rate();
352 uint32_t rate1 = lfo2_rate();
353 if (rate0 != 0) {
354 m_lfo_counter[0] += (0x10 | bitfield(rate0, 0, 4)) << bitfield(rate0, 4, 4);
355 }
356 if (rate1 != 0) {
357 m_lfo_counter[1] += (0x10 | bitfield(rate1, 0, 4)) << bitfield(rate1, 4, 4);
358 }
359 uint32_t lfo0 = bitfield(m_lfo_counter[0], 22, 8);
360 uint32_t lfo1 = bitfield(m_lfo_counter[1], 22, 8);
361
362 // fill in the noise entry 1 ahead of our current position; this
363 // ensures the current value remains stable for a full LFO clock
364 // and effectively latches the running value when the LFO advances
365 uint32_t lfo_noise = bitfield(m_noise_lfsr, 17, 8);
366 m_lfo_waveform[3][(lfo0 + 1) & 0xff] = lfo_noise | (lfo_noise << 8);
367 m_lfo_waveform[3][(lfo1 + 1) & 0xff] = lfo_noise | (lfo_noise << 8);
368
369 // fetch the AM/PM values based on the waveform; AM is unsigned and
370 // encoded in the low 8 bits, while PM signed and encoded in the upper
371 // 8 bits
372 int32_t ampm0 = m_lfo_waveform[lfo_waveform()][lfo0];
373 int32_t ampm1 = m_lfo_waveform[lfo2_waveform()][lfo1];
374
375 // apply depth to the AM values and store for later
376 m_lfo_am[0] = ((ampm0 & 0xff) * lfo_am_depth()) >> 7;
377 m_lfo_am[1] = ((ampm1 & 0xff) * lfo2_am_depth()) >> 7;
378
379 // apply depth to the PM values and return them combined into two
380 int32_t pm0 = ((ampm0 >> 8) * int32_t(lfo_pm_depth())) >> 7;
381 int32_t pm1 = ((ampm1 >> 8) * int32_t(lfo2_pm_depth())) >> 7;
382 return (pm0 & 0xff) | (pm1 << 8);
383 }
384
385
386 //-------------------------------------------------
387 // lfo_am_offset - return the AM offset from LFO
388 // for the given channel
389 //-------------------------------------------------
390
391 uint32_t opz_registers::lfo_am_offset(uint32_t choffs) const
392 {
393 // not sure how this works for real, but just adding the two
394 // AM LFOs together
395 uint32_t result = 0;
396
397 // shift value for AM sensitivity is [*, 0, 1, 2],
398 // mapping to values of [0, 23.9, 47.8, and 95.6dB]
399 uint32_t am_sensitivity = ch_lfo_am_sens(choffs);
400 if (am_sensitivity != 0)
401 result = m_lfo_am[0] << (am_sensitivity - 1);
402
403 // QUESTION: see OPN note below for the dB range mapping; it applies
404 // here as well
405
406 // raw LFO AM value on OPZ is 0-FF, which is already a factor of 2
407 // larger than the OPN below, putting our staring point at 2x theirs;
408 // this works out since our minimum is 2x their maximum
409 uint32_t am_sensitivity2 = ch_lfo2_am_sens(choffs);
410 if (am_sensitivity2 != 0)
411 result += m_lfo_am[1] << (am_sensitivity2 - 1);
412
413 return result;
414 }
415
416
417 //-------------------------------------------------
418 // cache_operator_data - fill the operator cache
419 // with prefetched data
420 //-------------------------------------------------
421
422 void opz_registers::cache_operator_data(uint32_t choffs, uint32_t opoffs, opdata_cache &cache)
423 {
424 // TODO: what is op_rev()?
425
426 // set up the easy stuff
427 cache.waveform = &m_waveform[op_waveform(opoffs)][0];
428
429 // get frequency from the channel
430 uint32_t block_freq = cache.block_freq = ch_block_freq(choffs);
431
432 // compute the keycode: block_freq is:
433 //
434 // BBBCCCCFFFFFF
435 // ^^^^^
436 //
437 // the 5-bit keycode is just the top 5 bits (block + top 2 bits
438 // of the key code)
439 uint32_t keycode = bitfield(block_freq, 8, 5);
440
441 // detune adjustment
442 cache.detune = detune_adjustment(op_detune(opoffs), keycode);
443
444 // multiple value, as an x.4 value (0 means 0.5)
445 // the "fine" control provides the fractional bits
446 cache.multiple = op_multiple(opoffs) << 4;
447 if (cache.multiple == 0)
448 cache.multiple = 0x08;
449 cache.multiple |= op_fine(opoffs);
450
451 // phase step, or PHASE_STEP_DYNAMIC if PM is active; this depends on
452 // block_freq, detune, and multiple, so compute it after we've done those;
453 // note that fix frequency mode is also treated as dynamic
454 if (!op_fix_mode(opoffs) && (lfo_pm_depth() == 0 || ch_lfo_pm_sens(choffs) == 0) && (lfo2_pm_depth() == 0 || ch_lfo2_pm_sens(choffs) == 0))
455 cache.phase_step = compute_phase_step(choffs, opoffs, cache, 0);
456 else
457 cache.phase_step = opdata_cache::PHASE_STEP_DYNAMIC;
458
459 // total level, scaled by 8
460 // TODO: how does ch_volume() fit into this?
461 cache.total_level = op_total_level(opoffs) << 3;
462
463 // 4-bit sustain level, but 15 means 31 so effectively 5 bits
464 cache.eg_sustain = op_sustain_level(opoffs);
465 cache.eg_sustain |= (cache.eg_sustain + 1) & 0x10;
466 cache.eg_sustain <<= 5;
467
468 // determine KSR adjustment for enevlope rates
469 uint32_t ksrval = keycode >> (op_ksr(opoffs) ^ 3);
470 cache.eg_rate[EG_ATTACK] = effective_rate(op_attack_rate(opoffs) * 2, ksrval);
471 cache.eg_rate[EG_DECAY] = effective_rate(op_decay_rate(opoffs) * 2, ksrval);
472 cache.eg_rate[EG_SUSTAIN] = effective_rate(op_sustain_rate(opoffs) * 2, ksrval);
473 cache.eg_rate[EG_RELEASE] = effective_rate(op_release_rate(opoffs) * 4 + 2, ksrval);
474 cache.eg_rate[EG_REVERB] = cache.eg_rate[EG_RELEASE];
475 uint32_t reverb = op_reverb_rate(opoffs);
476 if (reverb != 0)
477 cache.eg_rate[EG_REVERB] = std::min<uint32_t>(effective_rate(reverb * 4 + 2, ksrval), cache.eg_rate[EG_REVERB]);
478
479 // set the envelope shift; TX81Z manual says operator 1 (actually operator 4) shift is fixed at "off"
480 cache.eg_shift = ((opoffs & 0x18) == 0x18) ? 0 : op_eg_shift(opoffs);
481 }
482
483
484 //-------------------------------------------------
485 // compute_phase_step - compute the phase step
486 //-------------------------------------------------
487
488 uint32_t opz_registers::compute_phase_step(uint32_t choffs, uint32_t opoffs, opdata_cache const &cache, int32_t lfo_raw_pm)
489 {
490 // OPZ has a fixed frequency mode; it is unclear whether the
491 // detune and multiple parameters affect things
492
493 uint32_t phase_step;
494 if (op_fix_mode(opoffs))
495 {
496 // the baseline frequency in hz comes from the fix frequency and fine
497 // registers, which can specify values 8-255Hz in 1Hz increments; that
498 // value is then shifted up by the 3-bit range
499 uint32_t freq = op_fix_frequency(opoffs) << 4;
500 if (freq == 0)
501 freq = 8;
502 freq |= op_fine(opoffs);
503 freq <<= op_fix_range(opoffs);
504
505 // there is not enough resolution in the plain phase step to track the
506 // full range of frequencies, so we keep a per-operator sub step with an
507 // additional 12 bits of resolution; this calculation gives us, for
508 // example, a frequency of 8.0009Hz when 8Hz is requested
509 uint32_t substep = m_phase_substep[opoffs];
510 substep += 75 * 1024 * freq;
511 phase_step = substep >> 12;
512 m_phase_substep[opoffs] = substep & 0xfff;
513
514 // detune/multiple occupy the same space as fix_range/fix_frequency so
515 // don't apply them in addition
516 return phase_step;
517 }
518 else
519 {
520 // start with coarse detune delta; table uses cents value from
521 // manual, converted into 1/64ths
522 static const int16_t s_detune2_delta[4] = { 0, (600*64+50)/100, (781*64+50)/100, (950*64+50)/100 };
523 int32_t delta = s_detune2_delta[op_detune2(opoffs)];
524
525 // add in the PM deltas
526 uint32_t pm_sensitivity = ch_lfo_pm_sens(choffs);
527 if (pm_sensitivity != 0)
528 {
529 // raw PM value is -127..128 which is +/- 200 cents
530 // manual gives these magnitudes in cents:
531 // 0, +/-5, +/-10, +/-20, +/-50, +/-100, +/-400, +/-700
532 // this roughly corresponds to shifting the 200-cent value:
533 // 0 >> 5, >> 4, >> 3, >> 2, >> 1, << 1, << 2
534 if (pm_sensitivity < 6)
535 delta += int8_t(lfo_raw_pm) >> (6 - pm_sensitivity);
536 else
537 delta += int8_t(lfo_raw_pm) << (pm_sensitivity - 5);
538 }
539 uint32_t pm_sensitivity2 = ch_lfo2_pm_sens(choffs);
540 if (pm_sensitivity2 != 0)
541 {
542 // raw PM value is -127..128 which is +/- 200 cents
543 // manual gives these magnitudes in cents:
544 // 0, +/-5, +/-10, +/-20, +/-50, +/-100, +/-400, +/-700
545 // this roughly corresponds to shifting the 200-cent value:
546 // 0 >> 5, >> 4, >> 3, >> 2, >> 1, << 1, << 2
547 if (pm_sensitivity2 < 6)
548 delta += int8_t(lfo_raw_pm >> 8) >> (6 - pm_sensitivity2);
549 else
550 delta += int8_t(lfo_raw_pm >> 8) << (pm_sensitivity2 - 5);
551 }
552
553 // apply delta and convert to a frequency number; this translation is
554 // the same as OPM so just re-use that helper
555 phase_step = opm_key_code_to_phase_step(cache.block_freq, delta);
556
557 // apply detune based on the keycode
558 phase_step += cache.detune;
559
560 // apply frequency multiplier (which is cached as an x.4 value)
561 return (phase_step * cache.multiple) >> 4;
562 }
563 }
564
565
566 //-------------------------------------------------
567 // log_keyon - log a key-on event
568 //-------------------------------------------------
569
570 std::string opz_registers::log_keyon(uint32_t choffs, uint32_t opoffs)
571 {
572 uint32_t chnum = choffs;
573 uint32_t opnum = opoffs;
574
575 char buffer[256];
576 char *end = &buffer[0];
577
578 end += snprintf(end, 256-(end-buffer), "%u.%02u", chnum, opnum);
579
580 if (op_fix_mode(opoffs))
581 end += snprintf(end, 256-(end-buffer), " fixfreq=%X fine=%X shift=%X", op_fix_frequency(opoffs), op_fine(opoffs), op_fix_range(opoffs));
582 else
583 end += snprintf(end, 256-(end-buffer), " freq=%04X dt2=%u fine=%X", ch_block_freq(choffs), op_detune2(opoffs), op_fine(opoffs));
584
585 end += snprintf(end, 256-(end-buffer), " dt=%u fb=%u alg=%X mul=%X tl=%02X ksr=%u adsr=%02X/%02X/%02X/%X sl=%X out=%c%c",
586 op_detune(opoffs),
587 ch_feedback(choffs),
588 ch_algorithm(choffs),
589 op_multiple(opoffs),
590 op_total_level(opoffs),
591 op_ksr(opoffs),
592 op_attack_rate(opoffs),
593 op_decay_rate(opoffs),
594 op_sustain_rate(opoffs),
595 op_release_rate(opoffs),
596 op_sustain_level(opoffs),
597 ch_output_0(choffs) ? 'L' : '-',
598 ch_output_1(choffs) ? 'R' : '-');
599
600 if (op_eg_shift(opoffs) != 0)
601 end += snprintf(end, 256-(end-buffer), " egshift=%u", op_eg_shift(opoffs));
602
603 bool am = (lfo_am_depth() != 0 && ch_lfo_am_sens(choffs) != 0 && op_lfo_am_enable(opoffs) != 0);
604 if (am)
605 end += snprintf(end, 256-(end-buffer), " am=%u/%02X", ch_lfo_am_sens(choffs), lfo_am_depth());
606 bool pm = (lfo_pm_depth() != 0 && ch_lfo_pm_sens(choffs) != 0);
607 if (pm)
608 end += snprintf(end, 256-(end-buffer), " pm=%u/%02X", ch_lfo_pm_sens(choffs), lfo_pm_depth());
609 if (am || pm)
610 end += snprintf(end, 256-(end-buffer), " lfo=%02X/%c", lfo_rate(), "WQTN"[lfo_waveform()]);
611
612 bool am2 = (lfo2_am_depth() != 0 && ch_lfo2_am_sens(choffs) != 0 && op_lfo_am_enable(opoffs) != 0);
613 if (am2)
614 end += snprintf(end, 256-(end-buffer), " am2=%u/%02X", ch_lfo2_am_sens(choffs), lfo2_am_depth());
615 bool pm2 = (lfo2_pm_depth() != 0 && ch_lfo2_pm_sens(choffs) != 0);
616 if (pm2)
617 end += snprintf(end, 256-(end-buffer), " pm2=%u/%02X", ch_lfo2_pm_sens(choffs), lfo2_pm_depth());
618 if (am2 || pm2)
619 end += snprintf(end, 256-(end-buffer), " lfo2=%02X/%c", lfo2_rate(), "WQTN"[lfo2_waveform()]);
620
621 if (op_reverb_rate(opoffs) != 0)
622 end += snprintf(end, 256-(end-buffer), " rev=%u", op_reverb_rate(opoffs));
623 if (op_waveform(opoffs) != 0)
624 end += snprintf(end, 256-(end-buffer), " wf=%u", op_waveform(opoffs));
625 if (noise_enable() && opoffs == 31)
626 end += snprintf(end, 256-(end-buffer), " noise=1");
627
628 return buffer;
629 }
630
631
632
633 //*********************************************************
634 // YM2414
635 //*********************************************************
636
637 //-------------------------------------------------
638 // ym2414 - constructor
639 //-------------------------------------------------
640
641 ym2414::ym2414(ymfm_interface &intf) :
642 m_address(0),
643 m_fm(intf)
644 {
645 }
646
647
648 //-------------------------------------------------
649 // reset - reset the system
650 //-------------------------------------------------
651
652 void ym2414::reset()
653 {
654 // reset the engines
655 m_fm.reset();
656 }
657
658
659 //-------------------------------------------------
660 // save_restore - save or restore the data
661 //-------------------------------------------------
662
663 void ym2414::save_restore(ymfm_saved_state &state)
664 {
665 m_fm.save_restore(state);
666 state.save_restore(m_address);
667 }
668
669
670 //-------------------------------------------------
671 // read_status - read the status register
672 //-------------------------------------------------
673
674 uint8_t ym2414::read_status()
675 {
676 uint8_t result = m_fm.status();
677 if (m_fm.intf().ymfm_is_busy())
678 result |= fm_engine::STATUS_BUSY;
679 return result;
680 }
681
682
683 //-------------------------------------------------
684 // read - handle a read from the device
685 //-------------------------------------------------
686
687 uint8_t ym2414::read(uint32_t offset)
688 {
689 uint8_t result = 0xff;
690 switch (offset & 1)
691 {
692 case 0: // data port (unused)
693 debug::log_unexpected_read_write("Unexpected read from YM2414 offset %d\n", offset & 3);
694 break;
695
696 case 1: // status port, YM2203 compatible
697 result = read_status();
698 break;
699 }
700 return result;
701 }
702
703
704 //-------------------------------------------------
705 // write_address - handle a write to the address
706 // register
707 //-------------------------------------------------
708
709 void ym2414::write_address(uint8_t data)
710 {
711 // just set the address
712 m_address = data;
713 }
714
715
716 //-------------------------------------------------
717 // write - handle a write to the register
718 // interface
719 //-------------------------------------------------
720
721 void ym2414::write_data(uint8_t data)
722 {
723 // write the FM register
724 m_fm.write(m_address, data);
725 if (TEMPORARY_DEBUG_PRINTS)
726 {
727 switch (m_address & 0xe0)
728 {
729 case 0x00:
730 printf("CTL %02X = %02X\n", m_address, data);
731 break;
732
733 case 0x20:
734 switch (m_address & 0xf8)
735 {
736 case 0x20: printf("R/FBL/ALG %d = %02X\n", m_address & 7, data); break;
737 case 0x28: printf("KC %d = %02X\n", m_address & 7, data); break;
738 case 0x30: printf("KF/M %d = %02X\n", m_address & 7, data); break;
739 case 0x38: printf("PMS/AMS %d = %02X\n", m_address & 7, data); break;
740 }
741 break;
742
743 case 0x40:
744 if (bitfield(data, 7) == 0)
745 printf("DT1/MUL %d.%d = %02X\n", m_address & 7, (m_address >> 3) & 3, data);
746 else
747 printf("OW/FINE %d.%d = %02X\n", m_address & 7, (m_address >> 3) & 3, data);
748 break;
749
750 case 0x60:
751 printf("TL %d.%d = %02X\n", m_address & 7, (m_address >> 3) & 3, data);
752 break;
753
754 case 0x80:
755 printf("KRS/FIX/AR %d.%d = %02X\n", m_address & 7, (m_address >> 3) & 3, data);
756 break;
757
758 case 0xa0:
759 printf("A/D1R %d.%d = %02X\n", m_address & 7, (m_address >> 3) & 3, data);
760 break;
761
762 case 0xc0:
763 if (bitfield(data, 5) == 0)
764 printf("DT2/D2R %d.%d = %02X\n", m_address & 7, (m_address >> 3) & 3, data);
765 else
766 printf("EGS/REV %d.%d = %02X\n", m_address & 7, (m_address >> 3) & 3, data);
767 break;
768
769 case 0xe0:
770 printf("D1L/RR %d.%d = %02X\n", m_address & 7, (m_address >> 3) & 3, data);
771 break;
772 }
773 }
774
775 // special cases
776 if (m_address == 0x1b)
777 {
778 // writes to register 0x1B send the upper 2 bits to the output lines
779 m_fm.intf().ymfm_external_write(ACCESS_IO, 0, data >> 6);
780 }
781
782 // mark busy for a bit
783 m_fm.intf().ymfm_set_busy_end(32 * m_fm.clock_prescale());
784 }
785
786
787 //-------------------------------------------------
788 // write - handle a write to the register
789 // interface
790 //-------------------------------------------------
791
792 void ym2414::write(uint32_t offset, uint8_t data)
793 {
794 switch (offset & 1)
795 {
796 case 0: // address port
797 write_address(data);
798 break;
799
800 case 1: // data port
801 write_data(data);
802 break;
803 }
804 }
805
806
807 //-------------------------------------------------
808 // generate - generate one sample of sound
809 //-------------------------------------------------
810
811 void ym2414::generate(output_data *output, uint32_t numsamples)
812 {
813 for (uint32_t samp = 0; samp < numsamples; samp++, output++)
814 {
815 // clock the system
816 m_fm.clock(fm_engine::ALL_CHANNELS);
817
818 // update the FM content; YM2414 is full 14-bit with no intermediate clipping
819 m_fm.output(output->clear(), 0, 32767, fm_engine::ALL_CHANNELS);
820
821 // unsure about YM2414 outputs; assume it is like YM2151
822 output->roundtrip_fp();
823 }
824 }
825
826 }