Debian Codegraph @

thirdparty/ymfm/src/ymfm_opq.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_opq.h"
32 #include "ymfm_fm.ipp"
33
34 #define TEMPORARY_DEBUG_PRINTS (0)
35
36 //
37 // OPQ (aka YM3806/YM3533)
38 //
39 // This chip is not officially documented as far as I know. What I have
40 // comes from Jari Kangas' work on reverse engineering the PSR70:
41 //
42 // https://github.com/JKN0/PSR70-reverse
43 //
44 // OPQ appears be bsaically a mixture of OPM and OPN.
45 //
46
47 namespace ymfm
48 {
49
50 //*********************************************************
51 // OPQ SPECIFICS
52 //*********************************************************
53
54 //-------------------------------------------------
55 // opq_registers - constructor
56 //-------------------------------------------------
57
58 opq_registers::opq_registers() :
59 m_lfo_counter(0),
60 m_lfo_am(0)
61 {
62 // create the waveforms
63 for (uint32_t index = 0; index < WAVEFORM_LENGTH; index++)
64 m_waveform[0][index] = abs_sin_attenuation(index) | (bitfield(index, 9) << 15);
65
66 uint16_t zeroval = m_waveform[0][0];
67 for (uint32_t index = 0; index < WAVEFORM_LENGTH; index++)
68 m_waveform[1][index] = bitfield(index, 9) ? zeroval : m_waveform[0][index];
69 }
70
71
72 //-------------------------------------------------
73 // reset - reset to initial state
74 //-------------------------------------------------
75
76 void opq_registers::reset()
77 {
78 std::fill_n(&m_regdata[0], REGISTERS, 0);
79
80 // enable output on both channels by default
81 m_regdata[0x10] = m_regdata[0x11] = m_regdata[0x12] = m_regdata[0x13] = 0xc0;
82 m_regdata[0x14] = m_regdata[0x15] = m_regdata[0x16] = m_regdata[0x17] = 0xc0;
83 }
84
85
86 //-------------------------------------------------
87 // save_restore - save or restore the data
88 //-------------------------------------------------
89
90 void opq_registers::save_restore(ymfm_saved_state &state)
91 {
92 state.save_restore(m_lfo_counter);
93 state.save_restore(m_lfo_am);
94 state.save_restore(m_regdata);
95 }
96
97
98 //-------------------------------------------------
99 // operator_map - return an array of operator
100 // indices for each channel; for OPM this is fixed
101 //-------------------------------------------------
102
103 void opq_registers::operator_map(operator_mapping &dest) const
104 {
105 // seems like the operators are not swizzled like they are on OPM/OPN?
106 static const operator_mapping s_fixed_map =
107 { {
108 operator_list( 0, 8, 16, 24 ), // Channel 0 operators
109 operator_list( 1, 9, 17, 25 ), // Channel 1 operators
110 operator_list( 2, 10, 18, 26 ), // Channel 2 operators
111 operator_list( 3, 11, 19, 27 ), // Channel 3 operators
112 operator_list( 4, 12, 20, 28 ), // Channel 4 operators
113 operator_list( 5, 13, 21, 29 ), // Channel 5 operators
114 operator_list( 6, 14, 22, 30 ), // Channel 6 operators
115 operator_list( 7, 15, 23, 31 ), // Channel 7 operators
116 } };
117 dest = s_fixed_map;
118 }
119
120
121 //-------------------------------------------------
122 // write - handle writes to the register array
123 //-------------------------------------------------
124
125 bool opq_registers::write(uint16_t index, uint8_t data, uint32_t &channel, uint32_t &opmask)
126 {
127 assert(index < REGISTERS);
128
129 // detune/multiple share a register based on the MSB of what is written
130 // remap the multiple values to 100-11F
131 if ((index & 0xe0) == 0x40 && bitfield(data, 7) != 0)
132 index += 0xc0;
133
134 m_regdata[index] = data;
135
136 // handle writes to the key on index
137 if (index == 0x05)
138 {
139 channel = bitfield(data, 0, 3);
140 opmask = bitfield(data, 3, 4);
141 return true;
142 }
143 return false;
144 }
145
146
147 //-------------------------------------------------
148 // clock_noise_and_lfo - clock the noise and LFO,
149 // handling clock division, depth, and waveform
150 // computations
151 //-------------------------------------------------
152
153 int32_t opq_registers::clock_noise_and_lfo()
154 {
155 // OPQ LFO is not well-understood, but the enable and rate values
156 // look a lot like OPN, so we'll crib from there as a starting point
157
158 // if LFO not enabled (not present on OPN), quick exit with 0s
159 if (!lfo_enable())
160 {
161 m_lfo_counter = 0;
162 m_lfo_am = 0;
163 return 0;
164 }
165
166 // this table is based on converting the frequencies in the applications
167 // manual to clock dividers, based on the assumption of a 7-bit LFO value
168 static uint8_t const lfo_max_count[8] = { 109, 78, 72, 68, 63, 45, 9, 6 };
169 uint32_t subcount = uint8_t(m_lfo_counter++);
170
171 // when we cross the divider count, add enough to zero it and cause an
172 // increment at bit 8; the 7-bit value lives from bits 8-14
173 if (subcount >= lfo_max_count[lfo_rate()])
174 m_lfo_counter += 0x101 - subcount;
175
176 // AM value is 7 bits, staring at bit 8; grab the low 6 directly
177 m_lfo_am = bitfield(m_lfo_counter, 8, 6);
178
179 // first half of the AM period (bit 6 == 0) is inverted
180 if (bitfield(m_lfo_counter, 8+6) == 0)
181 m_lfo_am ^= 0x3f;
182
183 // PM value is 5 bits, starting at bit 10; grab the low 3 directly
184 int32_t pm = bitfield(m_lfo_counter, 10, 3);
185
186 // PM is reflected based on bit 3
187 if (bitfield(m_lfo_counter, 10+3))
188 pm ^= 7;
189
190 // PM is negated based on bit 4
191 return bitfield(m_lfo_counter, 10+4) ? -pm : pm;
192 }
193
194
195 //-------------------------------------------------
196 // lfo_am_offset - return the AM offset from LFO
197 // for the given channel
198 //-------------------------------------------------
199
200 uint32_t opq_registers::lfo_am_offset(uint32_t choffs) const
201 {
202 // OPM maps AM quite differently from OPN
203
204 // shift value for AM sensitivity is [*, 0, 1, 2],
205 // mapping to values of [0, 23.9, 47.8, and 95.6dB]
206 uint32_t am_sensitivity = ch_lfo_am_sens(choffs);
207 if (am_sensitivity == 0)
208 return 0;
209
210 // QUESTION: see OPN note below for the dB range mapping; it applies
211 // here as well
212
213 // raw LFO AM value on OPM is 0-FF, which is already a factor of 2
214 // larger than the OPN below, putting our staring point at 2x theirs;
215 // this works out since our minimum is 2x their maximum
216 return m_lfo_am << (am_sensitivity - 1);
217 }
218
219
220 //-------------------------------------------------
221 // cache_operator_data - fill the operator cache
222 // with prefetched data
223 //-------------------------------------------------
224
225 void opq_registers::cache_operator_data(uint32_t choffs, uint32_t opoffs, opdata_cache &cache)
226 {
227 // set up the easy stuff
228 cache.waveform = &m_waveform[op_waveform(opoffs)][0];
229
230 // get frequency from the appropriate registers
231 uint32_t block_freq = cache.block_freq = (opoffs & 8) ? ch_block_freq_24(choffs) : ch_block_freq_13(choffs);
232
233 // compute the keycode: block_freq is:
234 //
235 // BBBFFFFFFFFFFFF
236 // ^^^^???
237 //
238 // keycode is not understood, so just guessing it is like OPN:
239 // the 5-bit keycode uses the top 4 bits plus a magic formula
240 // for the final bit
241 uint32_t keycode = bitfield(block_freq, 11, 4) << 1;
242
243 // lowest bit is determined by a mix of next lower FNUM bits
244 // according to this equation from the YM2608 manual:
245 //
246 // (F11 & (F10 | F9 | F8)) | (!F11 & F10 & F9 & F8)
247 //
248 // for speed, we just look it up in a 16-bit constant
249 keycode |= bitfield(0xfe80, bitfield(block_freq, 8, 4));
250
251 // detune adjustment: the detune values supported by the OPQ are
252 // a much larger range (6 bits vs 3 bits) compared to any other
253 // known FM chip; based on experiments, it seems that the extra
254 // bits provide a bigger detune range rather than finer control,
255 // so until we get true measurements just assemble a net detune
256 // value by summing smaller detunes
257 int32_t detune = int32_t(op_detune(opoffs)) - 0x20;
258 int32_t abs_detune = std::abs(detune);
259 int32_t adjust = (abs_detune / 3) * detune_adjustment(3, keycode) + detune_adjustment(abs_detune % 3, keycode);
260 cache.detune = (detune >= 0) ? adjust : -adjust;
261
262 // multiple value, as an x.1 value (0 means 0.5)
263 static const uint8_t s_multiple_map[16] = { 1,2,4,6,8,10,12,14,16,18,20,24,30,32,34,36 };
264 cache.multiple = s_multiple_map[op_multiple(opoffs)];
265
266 // phase step, or PHASE_STEP_DYNAMIC if PM is active; this depends on
267 // block_freq, detune, and multiple, so compute it after we've done those
268 if (lfo_enable() == 0 || ch_lfo_pm_sens(choffs) == 0)
269 cache.phase_step = compute_phase_step(choffs, opoffs, cache, 0);
270 else
271 cache.phase_step = opdata_cache::PHASE_STEP_DYNAMIC;
272
273 // total level, scaled by 8
274 cache.total_level = op_total_level(opoffs) << 3;
275
276 // 4-bit sustain level, but 15 means 31 so effectively 5 bits
277 cache.eg_sustain = op_sustain_level(opoffs);
278 cache.eg_sustain |= (cache.eg_sustain + 1) & 0x10;
279 cache.eg_sustain <<= 5;
280
281 // determine KSR adjustment for enevlope rates
282 uint32_t ksrval = keycode >> (op_ksr(opoffs) ^ 3);
283 cache.eg_rate[EG_ATTACK] = effective_rate(op_attack_rate(opoffs) * 2, ksrval);
284 cache.eg_rate[EG_DECAY] = effective_rate(op_decay_rate(opoffs) * 2, ksrval);
285 cache.eg_rate[EG_SUSTAIN] = effective_rate(op_sustain_rate(opoffs) * 2, ksrval);
286 cache.eg_rate[EG_RELEASE] = effective_rate(op_release_rate(opoffs) * 4 + 2, ksrval);
287 cache.eg_rate[EG_REVERB] = (ch_reverb(choffs) != 0) ? 5*4 : cache.eg_rate[EG_RELEASE];
288 cache.eg_shift = 0;
289 }
290
291
292 //-------------------------------------------------
293 // compute_phase_step - compute the phase step
294 //-------------------------------------------------
295
296 uint32_t opq_registers::compute_phase_step(uint32_t choffs, uint32_t opoffs, opdata_cache const &cache, int32_t lfo_raw_pm)
297 {
298 // OPN phase calculation has only a single detune parameter
299 // and uses FNUMs instead of keycodes
300
301 // extract frequency number (low 12 bits of block_freq)
302 uint32_t fnum = bitfield(cache.block_freq, 0, 12);
303
304 // if there's a non-zero PM sensitivity, compute the adjustment
305 uint32_t pm_sensitivity = ch_lfo_pm_sens(choffs);
306 if (pm_sensitivity != 0)
307 {
308 // apply the phase adjustment based on the upper 7 bits
309 // of FNUM and the PM depth parameters
310 fnum += opn_lfo_pm_phase_adjustment(bitfield(cache.block_freq, 5, 7), pm_sensitivity, lfo_raw_pm);
311
312 // keep fnum to 12 bits
313 fnum &= 0xfff;
314 }
315
316 // apply block shift to compute phase step
317 uint32_t block = bitfield(cache.block_freq, 12, 3);
318 uint32_t phase_step = (fnum << block) >> 2;
319
320 // apply detune based on the keycode
321 phase_step += cache.detune;
322
323 // clamp to 17 bits in case detune overflows
324 // QUESTION: is this specific to the YM2612/3438?
325 phase_step &= 0x1ffff;
326
327 // apply frequency multiplier (which is cached as an x.1 value)
328 return (phase_step * cache.multiple) >> 1;
329 }
330
331
332 //-------------------------------------------------
333 // log_keyon - log a key-on event
334 //-------------------------------------------------
335
336 std::string opq_registers::log_keyon(uint32_t choffs, uint32_t opoffs)
337 {
338 uint32_t chnum = choffs;
339 uint32_t opnum = opoffs;
340
341 char buffer[256];
342 int end = 0;
343
344 end += snprintf(&buffer[end], sizeof(buffer) - end, "%u.%02u freq=%04X dt=%+2d fb=%u alg=%X mul=%X tl=%02X ksr=%u adsr=%02X/%02X/%02X/%X sl=%X out=%c%c",
345 chnum, opnum,
346 (opoffs & 1) ? ch_block_freq_24(choffs) : ch_block_freq_13(choffs),
347 int32_t(op_detune(opoffs)) - 0x20,
348 ch_feedback(choffs),
349 ch_algorithm(choffs),
350 op_multiple(opoffs),
351 op_total_level(opoffs),
352 op_ksr(opoffs),
353 op_attack_rate(opoffs),
354 op_decay_rate(opoffs),
355 op_sustain_rate(opoffs),
356 op_release_rate(opoffs),
357 op_sustain_level(opoffs),
358 ch_output_0(choffs) ? 'L' : '-',
359 ch_output_1(choffs) ? 'R' : '-');
360
361 bool am = (lfo_enable() && op_lfo_am_enable(opoffs) && ch_lfo_am_sens(choffs) != 0);
362 if (am)
363 end += snprintf(&buffer[end], sizeof(buffer) - end, " am=%u", ch_lfo_am_sens(choffs));
364 bool pm = (lfo_enable() && ch_lfo_pm_sens(choffs) != 0);
365 if (pm)
366 end += snprintf(&buffer[end], sizeof(buffer) - end, " pm=%u", ch_lfo_pm_sens(choffs));
367 if (am || pm)
368 end += snprintf(&buffer[end], sizeof(buffer) - end, " lfo=%02X", lfo_rate());
369 if (ch_reverb(choffs))
370 end += snprintf(&buffer[end], sizeof(buffer) - end, " reverb");
371
372 return buffer;
373 }
374
375
376
377 //*********************************************************
378 // YM3806
379 //*********************************************************
380
381 //-------------------------------------------------
382 // ym3806 - constructor
383 //-------------------------------------------------
384
385 ym3806::ym3806(ymfm_interface &intf) :
386 m_fm(intf)
387 {
388 }
389
390
391 //-------------------------------------------------
392 // reset - reset the system
393 //-------------------------------------------------
394
395 void ym3806::reset()
396 {
397 // reset the engines
398 m_fm.reset();
399 }
400
401
402 //-------------------------------------------------
403 // save_restore - save or restore the data
404 //-------------------------------------------------
405
406 void ym3806::save_restore(ymfm_saved_state &state)
407 {
408 m_fm.save_restore(state);
409 }
410
411
412 //-------------------------------------------------
413 // read_status - read the status register
414 //-------------------------------------------------
415
416 uint8_t ym3806::read_status()
417 {
418 uint8_t result = m_fm.status();
419 if (m_fm.intf().ymfm_is_busy())
420 result |= fm_engine::STATUS_BUSY;
421 return result;
422 }
423
424
425 //-------------------------------------------------
426 // read - handle a read from the device
427 //-------------------------------------------------
428
429 uint8_t ym3806::read(uint32_t offset)
430 {
431 uint8_t result = 0xff;
432 switch (offset)
433 {
434 case 0: // status port
435 result = read_status();
436 break;
437
438 default: // unknown
439 debug::log_unexpected_read_write("Unexpected read from YM3806 offset %02X\n", offset);
440 break;
441 }
442 if (TEMPORARY_DEBUG_PRINTS && offset != 0) printf("Read %02X = %02X\n", offset, result);
443 return result;
444 }
445
446
447 //-------------------------------------------------
448 // write - handle a write to the register
449 // interface
450 //-------------------------------------------------
451
452 void ym3806::write(uint32_t offset, uint8_t data)
453 {
454 if (TEMPORARY_DEBUG_PRINTS && (offset != 3 || data != 0x71)) printf("Write %02X = %02X\n", offset, data);
455 // write the FM register
456 m_fm.write(offset, data);
457 }
458
459
460 //-------------------------------------------------
461 // generate - generate one sample of sound
462 //-------------------------------------------------
463
464 void ym3806::generate(output_data *output, uint32_t numsamples)
465 {
466 for (uint32_t samp = 0; samp < numsamples; samp++, output++)
467 {
468 // clock the system
469 m_fm.clock(fm_engine::ALL_CHANNELS);
470
471 // update the FM content; YM3806 is full 14-bit with no intermediate clipping
472 m_fm.output(output->clear(), 0, 32767, fm_engine::ALL_CHANNELS);
473
474 // YM3608 appears to go through a YM3012 DAC, which means we want to apply
475 // the FP truncation logic to the outputs
476 output->roundtrip_fp();
477 }
478 }
479
480 }