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