| 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_pcm.h" |
| 32 |
#include "ymfm_fm.h" |
| 33 |
#include "ymfm_fm.ipp" |
| 34 |
|
| 35 |
namespace ymfm |
| 36 |
{ |
| 37 |
|
| 38 |
//********************************************************* |
| 39 |
// PCM REGISTERS |
| 40 |
//********************************************************* |
| 41 |
|
| 42 |
//------------------------------------------------- |
| 43 |
// reset - reset the register state |
| 44 |
//------------------------------------------------- |
| 45 |
|
| 46 |
void pcm_registers::reset() |
| 47 |
{ |
| 48 |
std::fill_n(&m_regdata[0], REGISTERS, 0); |
| 49 |
m_regdata[0xf8] = 0x1b; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
//------------------------------------------------- |
| 54 |
// save_restore - save or restore the data |
| 55 |
//------------------------------------------------- |
| 56 |
|
| 57 |
void pcm_registers::save_restore(ymfm_saved_state &state) |
| 58 |
{ |
| 59 |
state.save_restore(m_regdata); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
//------------------------------------------------- |
| 64 |
// cache_channel_data - update the cache with |
| 65 |
// data from the registers |
| 66 |
//------------------------------------------------- |
| 67 |
|
| 68 |
void pcm_registers::cache_channel_data(uint32_t choffs, pcm_cache &cache) |
| 69 |
{ |
| 70 |
// compute step from octave and fnumber; the math here implies |
| 71 |
// a .18 fraction but .16 should be perfectly fine |
| 72 |
int32_t octave = int8_t(ch_octave(choffs) << 4) >> 4; |
| 73 |
uint32_t fnum = ch_fnumber(choffs); |
| 74 |
cache.step = ((0x400 | fnum) << (octave + 7)) >> 2; |
| 75 |
|
| 76 |
// total level is computed as a .10 value for interpolation |
| 77 |
cache.total_level = ch_total_level(choffs) << 10; |
| 78 |
|
| 79 |
// compute panning values in terms of envelope attenuation |
| 80 |
int32_t panpot = int8_t(ch_panpot(choffs) << 4) >> 4; |
| 81 |
if (panpot >= 0) |
| 82 |
{ |
| 83 |
cache.pan_left = (panpot == 7) ? 0x3ff : 0x20 * panpot; |
| 84 |
cache.pan_right = 0; |
| 85 |
} |
| 86 |
else if (panpot >= -7) |
| 87 |
{ |
| 88 |
cache.pan_left = 0; |
| 89 |
cache.pan_right = (panpot == -7) ? 0x3ff : -0x20 * panpot; |
| 90 |
} |
| 91 |
else |
| 92 |
cache.pan_left = cache.pan_right = 0x3ff; |
| 93 |
|
| 94 |
// determine the LFO stepping value; this how much to add to a running |
| 95 |
// x.18 value for the LFO; steps were derived from frequencies in the |
| 96 |
// manual and come out very close with these values |
| 97 |
static const uint8_t s_lfo_steps[8] = { 1, 12, 19, 25, 31, 35, 37, 42 }; |
| 98 |
cache.lfo_step = s_lfo_steps[ch_lfo_speed(choffs)]; |
| 99 |
|
| 100 |
// AM LFO depth values, derived from the manual; note each has at most |
| 101 |
// 2 bits to make the "multiply" easy in hardware |
| 102 |
static const uint8_t s_am_depth[8] = { 0, 0x14, 0x20, 0x28, 0x30, 0x40, 0x50, 0x80 }; |
| 103 |
cache.am_depth = s_am_depth[ch_am_depth(choffs)]; |
| 104 |
|
| 105 |
// PM LFO depth values; these are converted from the manual's cents values |
| 106 |
// into f-numbers; the computations come out quite cleanly so pretty sure |
| 107 |
// these are correct |
| 108 |
static const uint8_t s_pm_depth[8] = { 0, 2, 3, 4, 6, 12, 24, 48 }; |
| 109 |
cache.pm_depth = s_pm_depth[ch_vibrato(choffs)]; |
| 110 |
|
| 111 |
// 4-bit sustain level, but 15 means 31 so effectively 5 bits |
| 112 |
cache.eg_sustain = ch_sustain_level(choffs); |
| 113 |
cache.eg_sustain |= (cache.eg_sustain + 1) & 0x10; |
| 114 |
cache.eg_sustain <<= 5; |
| 115 |
|
| 116 |
// compute the key scaling correction factor; 15 means don't do any correction |
| 117 |
int32_t correction = ch_rate_correction(choffs); |
| 118 |
if (correction == 15) |
| 119 |
correction = 0; |
| 120 |
else |
| 121 |
correction = (octave + correction) * 2 + bitfield(fnum, 9); |
| 122 |
|
| 123 |
// compute the envelope generator rates |
| 124 |
cache.eg_rate[EG_ATTACK] = effective_rate(ch_attack_rate(choffs), correction); |
| 125 |
cache.eg_rate[EG_DECAY] = effective_rate(ch_decay_rate(choffs), correction); |
| 126 |
cache.eg_rate[EG_SUSTAIN] = effective_rate(ch_sustain_rate(choffs), correction); |
| 127 |
cache.eg_rate[EG_RELEASE] = effective_rate(ch_release_rate(choffs), correction); |
| 128 |
cache.eg_rate[EG_REVERB] = 5; |
| 129 |
|
| 130 |
// if damping is on, override some things; essentially decay at a hardcoded |
| 131 |
// rate of 48 until -12db (0x80), then at maximum rate for the rest |
| 132 |
if (ch_damp(choffs) != 0) |
| 133 |
{ |
| 134 |
cache.eg_rate[EG_DECAY] = 48; |
| 135 |
cache.eg_rate[EG_SUSTAIN] = 63; |
| 136 |
cache.eg_rate[EG_RELEASE] = 63; |
| 137 |
cache.eg_sustain = 0x80; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
//------------------------------------------------- |
| 143 |
// effective_rate - return the effective rate, |
| 144 |
// clamping and applying corrections as needed |
| 145 |
//------------------------------------------------- |
| 146 |
|
| 147 |
uint32_t pcm_registers::effective_rate(uint32_t raw, uint32_t correction) |
| 148 |
{ |
| 149 |
// raw rates of 0 and 15 just pin to min/max |
| 150 |
if (raw == 0) |
| 151 |
return 0; |
| 152 |
if (raw == 15) |
| 153 |
return 63; |
| 154 |
|
| 155 |
// otherwise add the correction and clamp to range |
| 156 |
return clamp(raw * 4 + correction, 0, 63); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
//********************************************************* |
| 162 |
// PCM CHANNEL |
| 163 |
//********************************************************* |
| 164 |
|
| 165 |
//------------------------------------------------- |
| 166 |
// pcm_channel - constructor |
| 167 |
//------------------------------------------------- |
| 168 |
|
| 169 |
pcm_channel::pcm_channel(pcm_engine &owner, uint32_t choffs) : |
| 170 |
m_choffs(choffs), |
| 171 |
m_baseaddr(0), |
| 172 |
m_endpos(0), |
| 173 |
m_looppos(0), |
| 174 |
m_curpos(0), |
| 175 |
m_nextpos(0), |
| 176 |
m_lfo_counter(0), |
| 177 |
m_eg_state(EG_RELEASE), |
| 178 |
m_env_attenuation(0x3ff), |
| 179 |
m_total_level(0x7f << 10), |
| 180 |
m_format(0), |
| 181 |
m_key_state(0), |
| 182 |
m_regs(owner.regs()), |
| 183 |
m_owner(owner) |
| 184 |
{ |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
//------------------------------------------------- |
| 189 |
// reset - reset the channel state |
| 190 |
//------------------------------------------------- |
| 191 |
|
| 192 |
void pcm_channel::reset() |
| 193 |
{ |
| 194 |
m_baseaddr = 0; |
| 195 |
m_endpos = 0; |
| 196 |
m_looppos = 0; |
| 197 |
m_curpos = 0; |
| 198 |
m_nextpos = 0; |
| 199 |
m_lfo_counter = 0; |
| 200 |
m_eg_state = EG_RELEASE; |
| 201 |
m_env_attenuation = 0x3ff; |
| 202 |
m_total_level = 0x7f << 10; |
| 203 |
m_format = 0; |
| 204 |
m_key_state = 0; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
//------------------------------------------------- |
| 209 |
// save_restore - save or restore the data |
| 210 |
//------------------------------------------------- |
| 211 |
|
| 212 |
void pcm_channel::save_restore(ymfm_saved_state &state) |
| 213 |
{ |
| 214 |
state.save_restore(m_baseaddr); |
| 215 |
state.save_restore(m_endpos); |
| 216 |
state.save_restore(m_looppos); |
| 217 |
state.save_restore(m_curpos); |
| 218 |
state.save_restore(m_nextpos); |
| 219 |
state.save_restore(m_lfo_counter); |
| 220 |
state.save_restore(m_eg_state); |
| 221 |
state.save_restore(m_env_attenuation); |
| 222 |
state.save_restore(m_total_level); |
| 223 |
state.save_restore(m_format); |
| 224 |
state.save_restore(m_key_state); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
//------------------------------------------------- |
| 229 |
// prepare - prepare for clocking |
| 230 |
//------------------------------------------------- |
| 231 |
|
| 232 |
bool pcm_channel::prepare() |
| 233 |
{ |
| 234 |
// cache the data |
| 235 |
m_regs.cache_channel_data(m_choffs, m_cache); |
| 236 |
|
| 237 |
// clock the key state |
| 238 |
if ((m_key_state & KEY_PENDING) != 0) |
| 239 |
{ |
| 240 |
uint8_t oldstate = m_key_state; |
| 241 |
m_key_state = (m_key_state >> 1) & KEY_ON; |
| 242 |
if (((oldstate ^ m_key_state) & KEY_ON) != 0) |
| 243 |
{ |
| 244 |
if ((m_key_state & KEY_ON) != 0) |
| 245 |
start_attack(); |
| 246 |
else |
| 247 |
start_release(); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
// set the total level directly if not interpolating |
| 252 |
if (m_regs.ch_level_direct(m_choffs)) |
| 253 |
m_total_level = m_cache.total_level; |
| 254 |
|
| 255 |
// we're active until we're quiet after the release |
| 256 |
return (m_eg_state < EG_RELEASE || m_env_attenuation < EG_QUIET); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
//------------------------------------------------- |
| 261 |
// clock - master clocking function |
| 262 |
//------------------------------------------------- |
| 263 |
|
| 264 |
void pcm_channel::clock(uint32_t env_counter) |
| 265 |
{ |
| 266 |
// clock the LFO, which is an x.18 value incremented based on the |
| 267 |
// LFO speed value |
| 268 |
m_lfo_counter += m_cache.lfo_step; |
| 269 |
|
| 270 |
// clock the envelope |
| 271 |
clock_envelope(env_counter); |
| 272 |
|
| 273 |
// determine the step after applying vibrato |
| 274 |
uint32_t step = m_cache.step; |
| 275 |
if (m_cache.pm_depth != 0) |
| 276 |
{ |
| 277 |
// shift the LFO by 1/4 cycle for PM so that it starts at 0 |
| 278 |
uint32_t lfo_shifted = m_lfo_counter + (1 << 16); |
| 279 |
int32_t lfo_value = bitfield(lfo_shifted, 10, 7); |
| 280 |
if (bitfield(lfo_shifted, 17) != 0) |
| 281 |
lfo_value ^= 0x7f; |
| 282 |
lfo_value -= 0x40; |
| 283 |
step += (lfo_value * int32_t(m_cache.pm_depth)) >> 7; |
| 284 |
} |
| 285 |
|
| 286 |
// advance the sample step and loop as needed |
| 287 |
m_curpos = m_nextpos; |
| 288 |
m_nextpos = m_curpos + step; |
| 289 |
if (m_nextpos >= m_endpos) |
| 290 |
m_nextpos += m_looppos - m_endpos; |
| 291 |
|
| 292 |
// interpolate total level if needed |
| 293 |
if (m_total_level != m_cache.total_level) |
| 294 |
{ |
| 295 |
// max->min volume takes 156.4ms, or pretty close to 19/1024 per 44.1kHz sample |
| 296 |
// min->max volume is half that, so advance by 38/1024 per sample |
| 297 |
if (m_total_level < m_cache.total_level) |
| 298 |
m_total_level = std::min<int32_t>(m_total_level + 19, m_cache.total_level); |
| 299 |
else |
| 300 |
m_total_level = std::max<int32_t>(m_total_level - 38, m_cache.total_level); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
//------------------------------------------------- |
| 306 |
// output - return the computed output value, with |
| 307 |
// panning applied |
| 308 |
//------------------------------------------------- |
| 309 |
|
| 310 |
void pcm_channel::output(output_data &output) const |
| 311 |
{ |
| 312 |
// early out if the envelope is effectively off |
| 313 |
uint32_t envelope = m_env_attenuation; |
| 314 |
if (envelope > EG_QUIET) |
| 315 |
return; |
| 316 |
|
| 317 |
// add in LFO AM modulation |
| 318 |
if (m_cache.am_depth != 0) |
| 319 |
{ |
| 320 |
uint32_t lfo_value = bitfield(m_lfo_counter, 10, 7); |
| 321 |
if (bitfield(m_lfo_counter, 17) != 0) |
| 322 |
lfo_value ^= 0x7f; |
| 323 |
envelope += (lfo_value * m_cache.am_depth) >> 7; |
| 324 |
} |
| 325 |
|
| 326 |
// add in the current interpolated total level value, which is a .10 |
| 327 |
// value shifted left by 2 |
| 328 |
envelope += m_total_level >> 8; |
| 329 |
|
| 330 |
// add in panning effect and clamp |
| 331 |
uint32_t lenv = std::min<uint32_t>(envelope + m_cache.pan_left, 0x3ff); |
| 332 |
uint32_t renv = std::min<uint32_t>(envelope + m_cache.pan_right, 0x3ff); |
| 333 |
|
| 334 |
// convert to volume as a .11 fraction |
| 335 |
int32_t lvol = attenuation_to_volume(lenv << 2); |
| 336 |
int32_t rvol = attenuation_to_volume(renv << 2); |
| 337 |
|
| 338 |
// fetch current sample and add |
| 339 |
int16_t sample = fetch_sample(); |
| 340 |
uint32_t outnum = m_regs.ch_output_channel(m_choffs) * 2; |
| 341 |
output.data[outnum + 0] += (lvol * sample) >> 15; |
| 342 |
output.data[outnum + 1] += (rvol * sample) >> 15; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
//------------------------------------------------- |
| 347 |
// keyonoff - signal key on/off |
| 348 |
//------------------------------------------------- |
| 349 |
|
| 350 |
void pcm_channel::keyonoff(bool on) |
| 351 |
{ |
| 352 |
// mark the key state as pending |
| 353 |
m_key_state |= KEY_PENDING | (on ? KEY_PENDING_ON : 0); |
| 354 |
|
| 355 |
// don't log masked channels |
| 356 |
if ((m_key_state & (KEY_PENDING_ON | KEY_ON)) == KEY_PENDING_ON && ((debug::GLOBAL_PCM_CHANNEL_MASK >> m_choffs) & 1) != 0) |
| 357 |
{ |
| 358 |
debug::log_keyon("KeyOn PCM-%02d: num=%3d oct=%2d fnum=%03X level=%02X%c ADSR=%X/%X/%X/%X SL=%X", |
| 359 |
m_choffs, |
| 360 |
m_regs.ch_wave_table_num(m_choffs), |
| 361 |
int8_t(m_regs.ch_octave(m_choffs) << 4) >> 4, |
| 362 |
m_regs.ch_fnumber(m_choffs), |
| 363 |
m_regs.ch_total_level(m_choffs), |
| 364 |
m_regs.ch_level_direct(m_choffs) ? '!' : '/', |
| 365 |
m_regs.ch_attack_rate(m_choffs), |
| 366 |
m_regs.ch_decay_rate(m_choffs), |
| 367 |
m_regs.ch_sustain_rate(m_choffs), |
| 368 |
m_regs.ch_release_rate(m_choffs), |
| 369 |
m_regs.ch_sustain_level(m_choffs)); |
| 370 |
|
| 371 |
if (m_regs.ch_rate_correction(m_choffs) != 15) |
| 372 |
debug::log_keyon(" RC=%X", m_regs.ch_rate_correction(m_choffs)); |
| 373 |
|
| 374 |
if (m_regs.ch_pseudo_reverb(m_choffs) != 0) |
| 375 |
debug::log_keyon(" %s", "REV"); |
| 376 |
if (m_regs.ch_damp(m_choffs) != 0) |
| 377 |
debug::log_keyon(" %s", "DAMP"); |
| 378 |
|
| 379 |
if (m_regs.ch_vibrato(m_choffs) != 0 || m_regs.ch_am_depth(m_choffs) != 0) |
| 380 |
{ |
| 381 |
if (m_regs.ch_vibrato(m_choffs) != 0) |
| 382 |
debug::log_keyon(" VIB=%d", m_regs.ch_vibrato(m_choffs)); |
| 383 |
if (m_regs.ch_am_depth(m_choffs) != 0) |
| 384 |
debug::log_keyon(" AM=%d", m_regs.ch_am_depth(m_choffs)); |
| 385 |
debug::log_keyon(" LFO=%d", m_regs.ch_lfo_speed(m_choffs)); |
| 386 |
} |
| 387 |
debug::log_keyon("%s", "\n"); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
//------------------------------------------------- |
| 393 |
// load_wavetable - load a wavetable by fetching |
| 394 |
// its data from external memory |
| 395 |
//------------------------------------------------- |
| 396 |
|
| 397 |
void pcm_channel::load_wavetable() |
| 398 |
{ |
| 399 |
// determine the address of the wave table header |
| 400 |
uint32_t wavnum = m_regs.ch_wave_table_num(m_choffs); |
| 401 |
uint32_t wavheader = 12 * wavnum; |
| 402 |
|
| 403 |
// above 384 it may be in a different bank |
| 404 |
if (wavnum >= 384) |
| 405 |
{ |
| 406 |
uint32_t bank = m_regs.wave_table_header(); |
| 407 |
if (bank != 0) |
| 408 |
wavheader = 512*1024 * bank + (wavnum - 384) * 12; |
| 409 |
} |
| 410 |
|
| 411 |
// fetch the 22-bit base address and 2-bit format |
| 412 |
uint8_t byte = read_pcm(wavheader + 0); |
| 413 |
m_format = bitfield(byte, 6, 2); |
| 414 |
m_baseaddr = bitfield(byte, 0, 6) << 16; |
| 415 |
m_baseaddr |= read_pcm(wavheader + 1) << 8; |
| 416 |
m_baseaddr |= read_pcm(wavheader + 2) << 0; |
| 417 |
|
| 418 |
// fetch the 16-bit loop position |
| 419 |
m_looppos = read_pcm(wavheader + 3) << 8; |
| 420 |
m_looppos |= read_pcm(wavheader + 4); |
| 421 |
m_looppos <<= 16; |
| 422 |
|
| 423 |
// fetch the 16-bit end position, which is stored as a negative value |
| 424 |
// for some reason that is unclear |
| 425 |
m_endpos = read_pcm(wavheader + 5) << 8; |
| 426 |
m_endpos |= read_pcm(wavheader + 6); |
| 427 |
m_endpos = -int32_t(m_endpos) << 16; |
| 428 |
|
| 429 |
// remaining data values set registers |
| 430 |
m_owner.write(0x80 + m_choffs, read_pcm(wavheader + 7)); |
| 431 |
m_owner.write(0x98 + m_choffs, read_pcm(wavheader + 8)); |
| 432 |
m_owner.write(0xb0 + m_choffs, read_pcm(wavheader + 9)); |
| 433 |
m_owner.write(0xc8 + m_choffs, read_pcm(wavheader + 10)); |
| 434 |
m_owner.write(0xe0 + m_choffs, read_pcm(wavheader + 11)); |
| 435 |
|
| 436 |
// reset the envelope so we don't continue playing mid-sample from previous key ons |
| 437 |
m_env_attenuation = 0x3ff; |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
//------------------------------------------------- |
| 442 |
// read_pcm - read a byte from the external PCM |
| 443 |
// memory interface |
| 444 |
//------------------------------------------------- |
| 445 |
|
| 446 |
uint8_t pcm_channel::read_pcm(uint32_t address) const |
| 447 |
{ |
| 448 |
return m_owner.intf().ymfm_external_read(ACCESS_PCM, address); |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
//------------------------------------------------- |
| 453 |
// start_attack - start the attack phase |
| 454 |
//------------------------------------------------- |
| 455 |
|
| 456 |
void pcm_channel::start_attack() |
| 457 |
{ |
| 458 |
// don't change anything if already in attack state |
| 459 |
if (m_eg_state == EG_ATTACK) |
| 460 |
return; |
| 461 |
m_eg_state = EG_ATTACK; |
| 462 |
|
| 463 |
// reset the LFO if requested |
| 464 |
if (m_regs.ch_lfo_reset(m_choffs)) |
| 465 |
m_lfo_counter = 0; |
| 466 |
|
| 467 |
// if the attack rate == 63 then immediately go to max attenuation |
| 468 |
if (m_cache.eg_rate[EG_ATTACK] == 63) |
| 469 |
m_env_attenuation = 0; |
| 470 |
|
| 471 |
// reset the positions |
| 472 |
m_curpos = m_nextpos = 0; |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
//------------------------------------------------- |
| 477 |
// start_release - start the release phase |
| 478 |
//------------------------------------------------- |
| 479 |
|
| 480 |
void pcm_channel::start_release() |
| 481 |
{ |
| 482 |
// don't change anything if already in release or reverb state |
| 483 |
if (m_eg_state >= EG_RELEASE) |
| 484 |
return; |
| 485 |
m_eg_state = EG_RELEASE; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
//------------------------------------------------- |
| 490 |
// clock_envelope - clock the envelope generator |
| 491 |
//------------------------------------------------- |
| 492 |
|
| 493 |
void pcm_channel::clock_envelope(uint32_t env_counter) |
| 494 |
{ |
| 495 |
// handle attack->decay transitions |
| 496 |
if (m_eg_state == EG_ATTACK && m_env_attenuation == 0) |
| 497 |
m_eg_state = EG_DECAY; |
| 498 |
|
| 499 |
// handle decay->sustain transitions |
| 500 |
if (m_eg_state == EG_DECAY && m_env_attenuation >= m_cache.eg_sustain) |
| 501 |
m_eg_state = EG_SUSTAIN; |
| 502 |
|
| 503 |
// fetch the appropriate 6-bit rate value from the cache |
| 504 |
uint32_t rate = m_cache.eg_rate[m_eg_state]; |
| 505 |
|
| 506 |
// compute the rate shift value; this is the shift needed to |
| 507 |
// apply to the env_counter such that it becomes a 5.11 fixed |
| 508 |
// point number |
| 509 |
uint32_t rate_shift = rate >> 2; |
| 510 |
env_counter <<= rate_shift; |
| 511 |
|
| 512 |
// see if the fractional part is 0; if not, it's not time to clock |
| 513 |
if (bitfield(env_counter, 0, 11) != 0) |
| 514 |
return; |
| 515 |
|
| 516 |
// determine the increment based on the non-fractional part of env_counter |
| 517 |
uint32_t relevant_bits = bitfield(env_counter, (rate_shift <= 11) ? 11 : rate_shift, 3); |
| 518 |
uint32_t increment = attenuation_increment(rate, relevant_bits); |
| 519 |
|
| 520 |
// attack is the only one that increases |
| 521 |
if (m_eg_state == EG_ATTACK) |
| 522 |
m_env_attenuation += (~m_env_attenuation * increment) >> 4; |
| 523 |
|
| 524 |
// all other cases are similar |
| 525 |
else |
| 526 |
{ |
| 527 |
// apply the increment |
| 528 |
m_env_attenuation += increment; |
| 529 |
|
| 530 |
// clamp the final attenuation |
| 531 |
if (m_env_attenuation >= 0x400) |
| 532 |
m_env_attenuation = 0x3ff; |
| 533 |
|
| 534 |
// transition to reverb at -18dB if enabled |
| 535 |
if (m_env_attenuation >= 0xc0 && m_eg_state < EG_REVERB && m_regs.ch_pseudo_reverb(m_choffs)) |
| 536 |
m_eg_state = EG_REVERB; |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
//------------------------------------------------- |
| 542 |
// fetch_sample - fetch a sample at the current |
| 543 |
// position |
| 544 |
//------------------------------------------------- |
| 545 |
|
| 546 |
int16_t pcm_channel::fetch_sample() const |
| 547 |
{ |
| 548 |
uint32_t addr = m_baseaddr; |
| 549 |
uint32_t pos = m_curpos >> 16; |
| 550 |
|
| 551 |
// 8-bit PCM: shift up by 8 |
| 552 |
if (m_format == 0) |
| 553 |
return read_pcm(addr + pos) << 8; |
| 554 |
|
| 555 |
// 16-bit PCM: assemble from 2 halves |
| 556 |
if (m_format == 2) |
| 557 |
{ |
| 558 |
addr += pos * 2; |
| 559 |
return (read_pcm(addr) << 8) | read_pcm(addr + 1); |
| 560 |
} |
| 561 |
|
| 562 |
// 12-bit PCM: assemble out of half of 3 bytes |
| 563 |
addr += (pos / 2) * 3; |
| 564 |
if ((pos & 1) == 0) |
| 565 |
return (read_pcm(addr + 0) << 8) | ((read_pcm(addr + 1) << 4) & 0xf0); |
| 566 |
else |
| 567 |
return (read_pcm(addr + 2) << 8) | ((read_pcm(addr + 1) << 0) & 0xf0); |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
//********************************************************* |
| 573 |
// PCM ENGINE |
| 574 |
//********************************************************* |
| 575 |
|
| 576 |
//------------------------------------------------- |
| 577 |
// pcm_engine - constructor |
| 578 |
//------------------------------------------------- |
| 579 |
|
| 580 |
pcm_engine::pcm_engine(ymfm_interface &intf) : |
| 581 |
m_intf(intf), |
| 582 |
m_env_counter(0), |
| 583 |
m_modified_channels(ALL_CHANNELS), |
| 584 |
m_active_channels(ALL_CHANNELS) |
| 585 |
{ |
| 586 |
// create the channels |
| 587 |
for (int chnum = 0; chnum < CHANNELS; chnum++) |
| 588 |
m_channel[chnum] = std::make_unique<pcm_channel>(*this, chnum); |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
//------------------------------------------------- |
| 593 |
// reset - reset the engine state |
| 594 |
//------------------------------------------------- |
| 595 |
|
| 596 |
void pcm_engine::reset() |
| 597 |
{ |
| 598 |
// reset register state |
| 599 |
m_regs.reset(); |
| 600 |
|
| 601 |
// reset each channel |
| 602 |
for (auto &chan : m_channel) |
| 603 |
chan->reset(); |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
//------------------------------------------------- |
| 608 |
// save_restore - save or restore the data |
| 609 |
//------------------------------------------------- |
| 610 |
|
| 611 |
void pcm_engine::save_restore(ymfm_saved_state &state) |
| 612 |
{ |
| 613 |
// save our data |
| 614 |
state.save_restore(m_env_counter); |
| 615 |
|
| 616 |
// save channel state |
| 617 |
for (int chnum = 0; chnum < CHANNELS; chnum++) |
| 618 |
m_channel[chnum]->save_restore(state); |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
//------------------------------------------------- |
| 623 |
// clock - master clocking function |
| 624 |
//------------------------------------------------- |
| 625 |
|
| 626 |
void pcm_engine::clock(uint32_t chanmask) |
| 627 |
{ |
| 628 |
// if something was modified, prepare |
| 629 |
// also prepare every 4k samples to catch ending notes |
| 630 |
if (m_modified_channels != 0 || m_prepare_count++ >= 4096) |
| 631 |
{ |
| 632 |
// call each channel to prepare |
| 633 |
m_active_channels = 0; |
| 634 |
for (int chnum = 0; chnum < CHANNELS; chnum++) |
| 635 |
if (bitfield(chanmask, chnum)) |
| 636 |
if (m_channel[chnum]->prepare()) |
| 637 |
m_active_channels |= 1 << chnum; |
| 638 |
|
| 639 |
// reset the modified channels and prepare count |
| 640 |
m_modified_channels = m_prepare_count = 0; |
| 641 |
} |
| 642 |
|
| 643 |
// increment the envelope counter; the envelope generator |
| 644 |
// only clocks every other sample in order to make the PCM |
| 645 |
// envelopes line up with the FM envelopes (after taking into |
| 646 |
// account the different FM sampling rate) |
| 647 |
m_env_counter++; |
| 648 |
|
| 649 |
// now update the state of all the channels and operators |
| 650 |
for (int chnum = 0; chnum < CHANNELS; chnum++) |
| 651 |
if (bitfield(chanmask, chnum)) |
| 652 |
m_channel[chnum]->clock(m_env_counter >> 1); |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
//------------------------------------------------- |
| 657 |
// update - master update function |
| 658 |
//------------------------------------------------- |
| 659 |
|
| 660 |
void pcm_engine::output(output_data &output, uint32_t chanmask) |
| 661 |
{ |
| 662 |
// mask out some channels for debug purposes |
| 663 |
chanmask &= debug::GLOBAL_PCM_CHANNEL_MASK; |
| 664 |
|
| 665 |
// compute the output of each channel |
| 666 |
for (int chnum = 0; chnum < CHANNELS; chnum++) |
| 667 |
if (bitfield(chanmask, chnum)) |
| 668 |
m_channel[chnum]->output(output); |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
//------------------------------------------------- |
| 673 |
// read - handle reads from the PCM registers |
| 674 |
//------------------------------------------------- |
| 675 |
|
| 676 |
uint8_t pcm_engine::read(uint32_t regnum) |
| 677 |
{ |
| 678 |
// handle reads from the data register |
| 679 |
if (regnum == 0x06 && m_regs.memory_access_mode() != 0) |
| 680 |
return m_intf.ymfm_external_read(ACCESS_PCM, m_regs.memory_address_autoinc()); |
| 681 |
|
| 682 |
return m_regs.read(regnum); |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
//------------------------------------------------- |
| 687 |
// write - handle writes to the PCM registers |
| 688 |
//------------------------------------------------- |
| 689 |
|
| 690 |
void pcm_engine::write(uint32_t regnum, uint8_t data) |
| 691 |
{ |
| 692 |
// handle reads to the data register |
| 693 |
if (regnum == 0x06 && m_regs.memory_access_mode() != 0) |
| 694 |
{ |
| 695 |
m_intf.ymfm_external_write(ACCESS_PCM, m_regs.memory_address_autoinc(), data); |
| 696 |
return; |
| 697 |
} |
| 698 |
|
| 699 |
// for now just mark all channels as modified |
| 700 |
m_modified_channels = ALL_CHANNELS; |
| 701 |
|
| 702 |
// most writes are passive, consumed only when needed |
| 703 |
m_regs.write(regnum, data); |
| 704 |
|
| 705 |
// however, process keyons immediately |
| 706 |
if (regnum >= 0x68 && regnum <= 0x7f) |
| 707 |
m_channel[regnum - 0x68]->keyonoff(bitfield(data, 7)); |
| 708 |
|
| 709 |
// and also wavetable writes |
| 710 |
else if (regnum >= 0x08 && regnum <= 0x1f) |
| 711 |
m_channel[regnum - 0x08]->load_wavetable(); |
| 712 |
} |
| 713 |
|
| 714 |
} |