update ymfm

This commit is contained in:
tildearrow 2022-12-14 17:07:46 -05:00
parent 2a068ca046
commit 7732031404
10 changed files with 237 additions and 97 deletions

View File

@ -40,6 +40,7 @@
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <memory>
#include <string>
@ -325,6 +326,86 @@ struct ymfm_output
};
// ======================> ymfm_wavfile
// this class is a debugging helper that accumulates data and writes it to wav files
template<int Channels>
class ymfm_wavfile
{
public:
// construction
ymfm_wavfile(uint32_t samplerate = 44100) :
m_samplerate(samplerate)
{
}
// configuration
ymfm_wavfile &set_index(uint32_t index) { m_index = index; return *this; }
ymfm_wavfile &set_samplerate(uint32_t samplerate) { m_samplerate = samplerate; return *this; }
// destruction
~ymfm_wavfile()
{
if (!m_buffer.empty())
{
// create file
char name[20];
sprintf(name, "wavlog-%02d.wav", m_index);
FILE *out = fopen(name, "wb");
// make the wav file header
uint8_t header[44];
memcpy(&header[0], "RIFF", 4);
*(uint32_t *)&header[4] = m_buffer.size() * 2 + 44 - 8;
memcpy(&header[8], "WAVE", 4);
memcpy(&header[12], "fmt ", 4);
*(uint32_t *)&header[16] = 16;
*(uint16_t *)&header[20] = 1;
*(uint16_t *)&header[22] = Channels;
*(uint32_t *)&header[24] = m_samplerate;
*(uint32_t *)&header[28] = m_samplerate * 2 * Channels;
*(uint16_t *)&header[32] = 2 * Channels;
*(uint16_t *)&header[34] = 16;
memcpy(&header[36], "data", 4);
*(uint32_t *)&header[40] = m_buffer.size() * 2 + 44 - 44;
// write header then data
fwrite(&header[0], 1, sizeof(header), out);
fwrite(&m_buffer[0], 2, m_buffer.size(), out);
fclose(out);
}
}
// add data to the file
template<int Outputs>
void add(ymfm_output<Outputs> output)
{
int16_t sum[Channels] = { 0 };
for (int index = 0; index < Outputs; index++)
sum[index % Channels] += output.data[index];
for (int index = 0; index < Channels; index++)
m_buffer.push_back(sum[index]);
}
// add data to the file, using a reference
template<int Outputs>
void add(ymfm_output<Outputs> output, ymfm_output<Outputs> const &ref)
{
int16_t sum[Channels] = { 0 };
for (int index = 0; index < Outputs; index++)
sum[index % Channels] += output.data[index] - ref.data[index];
for (int index = 0; index < Channels; index++)
m_buffer.push_back(sum[index]);
}
private:
// internal state
uint32_t m_index;
uint32_t m_samplerate;
std::vector<int16_t> m_buffer;
};
// ======================> ymfm_saved_state
// this class contains a managed vector of bytes that is used to save and

View File

@ -465,48 +465,60 @@ void adpcm_b_channel::clock()
if (position < 0x10000)
return;
// if playing from RAM/ROM, check the end address and process
if (m_regs.external())
// if we're about to process nibble 0, fetch sample
if (m_curnibble == 0)
{
// wrap at the limit address
if (at_limit())
m_curaddress = 0;
// handle the sample end, either repeating or stopping
if (at_end())
{
// if repeating, go back to the start
if (m_regs.repeat())
load_start();
// otherwise, done; set the EOS bit and return
else
{
m_accumulator = 0;
m_prev_accum = 0;
m_status = (m_status & ~STATUS_PLAYING) | STATUS_EOS;
debug::log_keyon("%s\n", "ADPCM EOS");
return;
}
}
// if we're about to process nibble 0, fetch and increment
if (m_curnibble == 0)
{
m_curbyte = m_owner.intf().ymfm_external_read(ACCESS_ADPCM_B, m_curaddress++);
m_curaddress &= 0xffffff;
}
// playing from RAM/ROM
if (m_regs.external())
m_curbyte = m_owner.intf().ymfm_external_read(ACCESS_ADPCM_B, m_curaddress);
}
// extract the nibble from our current byte
uint8_t data = uint8_t(m_curbyte << (4 * m_curnibble)) >> 4;
m_curnibble ^= 1;
// if CPU-driven and we just processed the last nibble, copy the next byte and request more
if (m_curnibble == 0 && !m_regs.external())
// we just processed the last nibble
if (m_curnibble == 0)
{
m_curbyte = m_regs.cpudata();
m_status |= STATUS_BRDY;
// if playing from RAM/ROM, check the end/limit address or advance
if (m_regs.external())
{
// handle the sample end, either repeating or stopping
if (at_end())
{
// if repeating, go back to the start
if (m_regs.repeat())
load_start();
// otherwise, done; set the EOS bit
else
{
m_accumulator = 0;
m_prev_accum = 0;
m_status = (m_status & ~STATUS_PLAYING) | STATUS_EOS;
debug::log_keyon("%s\n", "ADPCM EOS");
return;
}
}
// wrap at the limit address
else if (at_limit())
m_curaddress = 0;
// otherwise, advance the current address
else
{
m_curaddress++;
m_curaddress &= 0xffffff;
}
}
// if CPU-driven, copy the next byte and request more
else
{
m_curbyte = m_regs.cpudata();
m_status |= STATUS_BRDY;
}
}
// remember previous value for interpolation
@ -574,18 +586,27 @@ uint8_t adpcm_b_channel::read(uint32_t regnum)
m_dummy_read--;
}
// did we hit the end? if so, signal EOS
if (at_end())
{
m_status = STATUS_EOS | STATUS_BRDY;
debug::log_keyon("%s\n", "ADPCM EOS");
}
// otherwise, write the data and signal ready
// read the data
else
{
// read from outside of the chip
result = m_owner.intf().ymfm_external_read(ACCESS_ADPCM_B, m_curaddress++);
m_status = STATUS_BRDY;
// did we hit the end? if so, signal EOS
if (at_end())
{
m_status = STATUS_EOS | STATUS_BRDY;
debug::log_keyon("%s\n", "ADPCM EOS");
}
else
{
// signal ready
m_status = STATUS_BRDY;
}
// wrap at the limit address
if (at_limit())
m_curaddress = 0;
}
}
return result;

View File

@ -351,11 +351,11 @@ private:
// load the start address
void load_start();
// limit checker
bool at_limit() const { return (m_curaddress >> address_shift()) >= m_regs.limit(); }
// limit checker; stops at the last byte of the chunk described by address_shift()
bool at_limit() const { return (m_curaddress == (((m_regs.limit() + 1) << address_shift()) - 1)); }
// end checker
bool at_end() const { return (m_curaddress >> address_shift()) > m_regs.end(); }
// end checker; stops at the last byte of the chunk described by address_shift()
bool at_end() const { return (m_curaddress == (((m_regs.end() + 1) << address_shift()) - 1)); }
// internal state
uint32_t const m_address_shift; // address bits shift-left

View File

@ -33,6 +33,8 @@
#pragma once
#define YMFM_DEBUG_LOG_WAVFILES (0)
namespace ymfm
{
@ -162,8 +164,8 @@ template<class RegisterType> class fm_engine_base;
template<class RegisterType>
class fm_operator
{
// "quiet" value, used to optimize when we can skip doing working
static constexpr uint32_t EG_QUIET = 0x200;
// "quiet" value, used to optimize when we can skip doing work
static constexpr uint32_t EG_QUIET = 0x380;
public:
// constructor
@ -206,6 +208,7 @@ public:
// simple getters for debugging
envelope_state debug_eg_state() const { return m_env_state; }
uint16_t debug_eg_attenuation() const { return m_env_attenuation; }
uint8_t debug_ssg_inverted() const { return m_ssg_inverted; }
opdata_cache &debug_cache() { return m_cache; }
private:
@ -406,7 +409,14 @@ public:
void set_clock_prescale(uint32_t prescale) { m_clock_prescale = prescale; }
// compute sample rate
uint32_t sample_rate(uint32_t baseclock) const { return baseclock / (m_clock_prescale * OPERATORS); }
uint32_t sample_rate(uint32_t baseclock) const
{
#if (YMFM_DEBUG_LOG_WAVFILES)
for (uint32_t chnum = 0; chnum < CHANNELS; chnum++)
m_wavfile[chnum].set_samplerate(baseclock / (m_clock_prescale * OPERATORS));
#endif
return baseclock / (m_clock_prescale * OPERATORS);
}
// return the owning device
ymfm_interface &intf() const { return m_intf; }
@ -453,6 +463,9 @@ protected:
RegisterType m_regs; // register accessor
std::unique_ptr<fm_channel<RegisterType>> m_channel[CHANNELS]; // channel pointers
std::unique_ptr<fm_operator<RegisterType>> m_operator[OPERATORS]; // operator pointers
#if (YMFM_DEBUG_LOG_WAVFILES)
mutable ymfm_wavfile<1> m_wavfile[CHANNELS]; // for debugging
#endif
};
}

View File

@ -448,6 +448,8 @@ void fm_operator<RegisterType>::clock(uint32_t env_counter, int32_t lfo_raw_pm)
// clock the SSG-EG state (OPN/OPNA)
if (m_regs.op_ssg_eg_enable(m_opoffs))
clock_ssg_eg_state();
else
m_ssg_inverted = false;
// clock the envelope if on an envelope cycle; env_counter is a x.2 value
if (bitfield(env_counter, 0, 2) == 0)
@ -470,15 +472,6 @@ int32_t fm_operator<RegisterType>::compute_volume(uint32_t phase, uint32_t am_of
// the low 10 bits of phase represents a full 2*PI period over
// the full sin wave
#if 0
// temporary envelope logging
if (m_choffs == 0)
{
printf(" %c@%02X:%03X", "PADSRV"[m_env_state], m_cache.eg_rate[m_env_state], envelope_attenuation(am_offset));
if (m_opoffs == 0x18) printf("\n");
}
#endif
// early out if the envelope is effectively off
if (m_env_attenuation > EG_QUIET && m_cache.eg_shift == 0)
return 0;
@ -896,6 +889,23 @@ void fm_channel<RegisterType>::clock(uint32_t env_counter, int32_t lfo_raw_pm)
for (uint32_t opnum = 0; opnum < array_size(m_op); opnum++)
if (m_op[opnum] != nullptr)
m_op[opnum]->clock(env_counter, lfo_raw_pm);
/*
useful temporary code for envelope debugging
if (m_choffs == 0x101)
{
for (uint32_t opnum = 0; opnum < array_size(m_op); opnum++)
{
auto &op = *m_op[((opnum & 1) << 1) | ((opnum >> 1) & 1)];
printf(" %c%03X%c%c ",
"PADSRV"[op.debug_eg_state()],
op.debug_eg_attenuation(),
op.debug_ssg_inverted() ? '-' : '+',
m_regs.op_ssg_eg_enable(op.opoffs()) ? '0' + m_regs.op_ssg_eg_mode(op.opoffs()) : ' ');
}
printf(" -- ");
}
*/
}
@ -943,7 +953,8 @@ void fm_channel<RegisterType>::output_2op(output_data &output, uint32_t rshift,
}
else
{
result = op1value + (m_op[1]->compute_volume(m_op[1]->phase(), am_offset) >> rshift);
result = (RegisterType::MODULATOR_DELAY ? m_feedback[1] : op1value) >> rshift;
result += m_op[1]->compute_volume(m_op[1]->phase(), am_offset) >> rshift;
int32_t clipmin = -clipmax - 1;
result = clamp(result, clipmin, clipmax);
}
@ -1180,6 +1191,7 @@ fm_engine_base<RegisterType>::fm_engine_base(ymfm_interface &intf) :
m_irq_mask(STATUS_TIMERA | STATUS_TIMERB),
m_irq_state(0),
m_timer_running{0,0},
m_total_clocks(0),
m_active_channels(ALL_CHANNELS),
m_modified_channels(ALL_CHANNELS),
m_prepare_count(0)
@ -1195,6 +1207,11 @@ fm_engine_base<RegisterType>::fm_engine_base(ymfm_interface &intf) :
for (uint32_t opnum = 0; opnum < OPERATORS; opnum++)
m_operator[opnum] = std::make_unique<fm_operator<RegisterType>>(*this, RegisterType::operator_offset(opnum));
#if (YMFM_DEBUG_LOG_WAVFILES)
for (uint32_t chnum = 0; chnum < CHANNELS; chnum++)
m_wavfile[chnum].set_index(chnum);
#endif
// do the initial operator assignment
assign_operators();
}
@ -1305,24 +1322,6 @@ uint32_t fm_engine_base<RegisterType>::clock(uint32_t chanmask)
if (bitfield(chanmask, chnum))
m_channel[chnum]->clock(m_env_counter, lfo_raw_pm);
#if 0
//Temporary debugging...
static double curtime = 0;
//for (uint32_t chnum = 0; chnum < CHANNELS; chnum++)
uint32_t chnum = 4;
{
printf("t=%.4f ch%d: ", curtime, chnum);
for (uint32_t opnum = 0; opnum < 4; opnum++)
{
auto op = debug_channel(chnum)->debug_operator(opnum);
auto eg_state = op->debug_eg_state();
printf(" %c%03X[%02X]%c ", "PADSRV"[eg_state], op.debug_eg_attenuation(), op.debug_cache().eg_rate[eg_state], m_regs.op_ssg_eg_enable(op.opoffs()) ? '*' : ' ');
}
printf(" -- ");
}
curtime += 1.0 / double(sample_rate(7670454));
#endif
// return the envelope counter as it is used to clock ADPCM-A
return m_env_counter;
}
@ -1340,7 +1339,8 @@ void fm_engine_base<RegisterType>::output(output_data &output, uint32_t rshift,
chanmask &= debug::GLOBAL_FM_CHANNEL_MASK;
// mask out inactive channels
chanmask &= m_active_channels;
if (!YMFM_DEBUG_LOG_WAVFILES)
chanmask &= m_active_channels;
// handle the rhythm case, where some of the operators are dedicated
// to percussion (this is an OPL-specific feature)
@ -1358,6 +1358,9 @@ void fm_engine_base<RegisterType>::output(output_data &output, uint32_t rshift,
for (uint32_t chnum = 0; chnum < CHANNELS; chnum++)
if (bitfield(chanmask, chnum))
{
#if (YMFM_DEBUG_LOG_WAVFILES)
auto reference = output;
#endif
if (chnum == 6)
m_channel[chnum]->output_rhythm_ch6(output, rshift, clipmax);
else if (chnum == 7)
@ -1368,6 +1371,9 @@ void fm_engine_base<RegisterType>::output(output_data &output, uint32_t rshift,
m_channel[chnum]->output_4op(output, rshift, clipmax);
else
m_channel[chnum]->output_2op(output, rshift, clipmax);
#if (YMFM_DEBUG_LOG_WAVFILES)
m_wavfile[chnum].add(output, reference);
#endif
}
}
else
@ -1376,10 +1382,16 @@ void fm_engine_base<RegisterType>::output(output_data &output, uint32_t rshift,
for (uint32_t chnum = 0; chnum < CHANNELS; chnum++)
if (bitfield(chanmask, chnum))
{
#if (YMFM_DEBUG_LOG_WAVFILES)
auto reference = output;
#endif
if (m_channel[chnum]->is4op())
m_channel[chnum]->output_4op(output, rshift, clipmax);
else
m_channel[chnum]->output_2op(output, rshift, clipmax);
#if (YMFM_DEBUG_LOG_WAVFILES)
m_wavfile[chnum].add(output, reference);
#endif
}
}
}
@ -1508,7 +1520,10 @@ void fm_engine_base<RegisterType>::engine_timer_expired(uint32_t tnum)
if (tnum == 0 && m_regs.csm())
for (uint32_t chnum = 0; chnum < CHANNELS; chnum++)
if (bitfield(RegisterType::CSM_TRIGGER_MASK, chnum))
{
m_channel[chnum]->keyonoff(1, KEYON_CSM, chnum);
m_modified_channels |= 1 << chnum;
}
// reset
m_timer_running[tnum] = false;

View File

@ -174,7 +174,7 @@ public:
// system-wide registers
uint32_t test() const { return byte(0x01, 0, 8); }
uint32_t lfo_reset() const { return byte(0x01, 1, 1); }
uint32_t noise_frequency() const { return byte(0x0f, 0, 5); }
uint32_t noise_frequency() const { return byte(0x0f, 0, 5) ^ 0x1f; }
uint32_t noise_enable() const { return byte(0x0f, 7, 1); }
uint32_t timer_a_value() const { return word(0x10, 0, 8, 0x11, 0, 2); }
uint32_t timer_b_value() const { return byte(0x12, 0, 8); }

View File

@ -146,7 +146,10 @@ bool opn_registers_base<IsOpnA>::write(uint16_t index, uint8_t data, uint32_t &c
// borrow unused registers 0xb8-bf/0x1b8-bf as temporary holding locations
if ((index & 0xf0) == 0xa0)
{
uint32_t latchindex = 0xb8 | (bitfield(index, 3) << 2) | bitfield(index, 0, 2);
if (bitfield(index, 0, 2) == 3)
return false;
uint32_t latchindex = 0xb8 | bitfield(index, 3);
if (IsOpnA)
latchindex |= index & 0x100;
@ -157,9 +160,16 @@ bool opn_registers_base<IsOpnA>::write(uint16_t index, uint8_t data, uint32_t &c
// writes to the lower half only commit if the latch is there
else if (bitfield(m_regdata[latchindex], 7))
{
m_regdata[index] = data;
m_regdata[index | 4] = m_regdata[latchindex] & 0x3f;
m_regdata[latchindex] = 0;
}
return false;
}
else if ((index & 0xf8) == 0xb8)
{
// registers 0xb8-0xbf are used internally
return false;
}
// everything else is normal
@ -195,7 +205,12 @@ int32_t opn_registers_base<IsOpnA>::clock_noise_and_lfo()
if (!IsOpnA || !lfo_enable())
{
m_lfo_counter = 0;
m_lfo_am = 0;
// special case: if LFO is disabled on OPNA, it basically just keeps the counter
// at 0; since position 0 gives an AM value of 0x3f, it is important to reflect
// that here; for example, MegaDrive Venom plays some notes with LFO globally
// disabled but enabling LFO on the operators, and it expects this added attenutation
m_lfo_am = IsOpnA ? 0x3f : 0x00;
return 0;
}
@ -417,10 +432,10 @@ std::string opn_registers_base<IsOpnA>::log_keyon(uint32_t choffs, uint32_t opof
ch_output_1(choffs) ? 'R' : '-');
if (op_ssg_eg_enable(opoffs))
end += sprintf(end, " ssg=%X", op_ssg_eg_mode(opoffs));
bool am = (lfo_enable() && op_lfo_am_enable(opoffs) && ch_lfo_am_sens(choffs) != 0);
bool am = (op_lfo_am_enable(opoffs) && ch_lfo_am_sens(choffs) != 0);
if (am)
end += sprintf(end, " am=%u", ch_lfo_am_sens(choffs));
bool pm = (lfo_enable() && ch_lfo_pm_sens(choffs) != 0);
bool pm = (ch_lfo_pm_sens(choffs) != 0);
if (pm)
end += sprintf(end, " pm=%u", ch_lfo_pm_sens(choffs));
if (am || pm)
@ -1094,7 +1109,7 @@ uint8_t ym2608::read_status_hi()
uint8_t ym2608::read_data_hi()
{
uint8_t result = 0;
if (m_address < 0x10)
if ((m_address & 0xff) < 0x10)
{
// 00-0F: Read from ADPCM-B
result = m_adpcm_b.read(m_address & 0x0f);

View File

@ -784,7 +784,7 @@ public:
protected:
// simulate the DAC discontinuity
int32_t dac_discontinuity(int32_t value) const { return (value < 0) ? (value - 2) : (value + 3); }
int32_t dac_discontinuity(int32_t value) const { return (value < 0) ? (value - 3) : (value + 4); }
// internal state
uint16_t m_address; // address register

View File

@ -201,19 +201,14 @@ void ssg_engine::output(output_data &output)
for (int chan = 0; chan < 3; chan++)
{
// noise depends on the noise state, which is the LSB of m_noise_state
uint32_t noise_on = m_regs.ch_noise_enable(chan) & m_noise_state;
uint32_t noise_on = m_regs.ch_noise_enable_n(chan) | m_noise_state;
// tone depends on the current tone state
uint32_t tone_on = m_regs.ch_tone_enable(chan) & m_tone_state[chan];
uint32_t tone_on = m_regs.ch_tone_enable_n(chan) | m_tone_state[chan];
// if envelope is enabled but tone and noise aren't, use the envelope
// volume
// if neither tone nor noise enabled, return 0
uint32_t volume;
if (m_regs.ch_envelope_enable(chan) && !m_regs.ch_noise_enable(chan) && !m_regs.ch_tone_enable(chan))
volume = envelope_volume;
// if neither tone nor noise enabled, return 0
else if ((noise_on | tone_on) == 0)
if ((noise_on & tone_on) == 0)
volume = 0;
// if the envelope is enabled, use its amplitude

View File

@ -130,8 +130,8 @@ public:
uint32_t io_b_data() const { return m_regdata[0x0f]; }
// per-channel registers
uint32_t ch_noise_enable(uint32_t choffs) const { return bitfield(~m_regdata[0x07], 3 + choffs); }
uint32_t ch_tone_enable(uint32_t choffs) const { return bitfield(~m_regdata[0x07], 0 + choffs); }
uint32_t ch_noise_enable_n(uint32_t choffs) const { return bitfield(m_regdata[0x07], 3 + choffs); }
uint32_t ch_tone_enable_n(uint32_t choffs) const { return bitfield(m_regdata[0x07], 0 + choffs); }
uint32_t ch_tone_period(uint32_t choffs) const { return m_regdata[0x00 + 2 * choffs] | (bitfield(m_regdata[0x01 + 2 * choffs], 0, 4) << 8); }
uint32_t ch_envelope_enable(uint32_t choffs) const { return bitfield(m_regdata[0x08 + choffs], 4); }
uint32_t ch_amplitude(uint32_t choffs) const { return bitfield(m_regdata[0x08 + choffs], 0, 4); }