Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue #5734 (FreeBoy Division by zero) #6053

Merged
merged 5 commits into from
Jul 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions plugins/FreeBoy/FreeBoy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,30 +358,34 @@ void FreeBoyInstrument::playNote(NotePlayHandle* nph, sampleFrame* workingBuffer

if (tfp == 0)
{
//PRNG Frequency = (1048576 Hz / (ratio + 1)) / 2 ^ (shiftclockfreq + 1)
char sopt = 0;
char ropt = 1;
float fopt = 524288.0 / (ropt * std::pow(2.0, sopt + 1.0));
float f;
// Initialize noise channel...
// PRNG Frequency = (1048576 Hz / (ratio + 1)) / 2 ^ (shiftclockfreq + 1)
// When div_ratio = 0 the ratio should be 0.5. Since s = 0 is the only case where r = 0 gives
// a unique frequency, we can start by guessing s = r = 0 here and then skip r = 0 in the loop.
char clock_freq = 0;
char div_ratio = 0;
float closest_freq = 524288.0 / (0.5 * std::pow(2.0, clock_freq + 1.0));
// This nested for loop iterates over all possible combinations of clock frequency and dividing
// ratio and chooses the combination whose resulting frequency is closest to the note frequency
for (char s = 0; s < 16; ++s)
{
for (char r = 0; r < 8; ++r)
for (char r = 1; r < 8; ++r)
{
f = 524288.0 / (r * std::pow(2.0, s + 1.0));
if (std::fabs(freq - fopt) > std::fabs(freq - f))
float f = 524288.0 / (r * std::pow(2.0, s + 1.0));
if (std::fabs(freq - closest_freq) > std::fabs(freq - f))
{
fopt = f;
ropt = r;
sopt = s;
closest_freq = f;
div_ratio = r;
clock_freq = s;
}
}
}

data = sopt;
data = clock_freq;
data = data << 1;
data += m_ch4ShiftRegWidthModel.value();
data = data << 3;
data += ropt;
data += div_ratio;
papu->writeRegister(0xff22, data);

//channel 4 init
Expand Down