Skip to content

Commit

Permalink
Tidying up random sound functions to hopefully better understand the …
Browse files Browse the repository at this point in the history
…slowdown
  • Loading branch information
davidwhitney committed Apr 16, 2020
1 parent 9a599c5 commit 6c409dd
Show file tree
Hide file tree
Showing 16 changed files with 428 additions and 564 deletions.
6 changes: 3 additions & 3 deletions CoreBoy.Test.Unit/Sound/LengthCounterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ and b
*/
private void shouldBeOn()
{
if (lengthCounter.IsEnabled())
if (lengthCounter.Enabled)
{
Assert.AreNotEqual(0, lengthCounter.GetValue());
Assert.AreNotEqual(0, lengthCounter.Length);
}
}

Expand All @@ -276,7 +276,7 @@ private void shouldBeAlmostOff()

private void shouldBeOff()
{
Assert.True(lengthCounter.IsEnabled() && lengthCounter.GetValue() == 0);
Assert.True(lengthCounter.Enabled && lengthCounter.Length == 0);
}

private void delay(int cpuCycles)
Expand Down
14 changes: 7 additions & 7 deletions CoreBoy.Test.Unit/Sound/LengthTriggerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public void test05()
begin();
wchn(1, -1);
wchn(4, 0x40);
Assert.True(lengthCounter.IsEnabled());
Assert.AreEqual(0, lengthCounter.GetValue());
Assert.True(lengthCounter.Enabled);
Assert.AreEqual(0, lengthCounter.Length);
}

/*
Expand Down Expand Up @@ -252,11 +252,11 @@ public void test12()
wchn(4, 0x40);
wchn(4, 0xc0);
delayApu(maxlen - 3);
Assert.True(lengthCounter.IsEnabled());
Assert.AreNotEqual(0, lengthCounter.GetValue());
Assert.True(lengthCounter.Enabled);
Assert.AreNotEqual(0, lengthCounter.Length);
delayApu(1);
Assert.True(lengthCounter.IsEnabled());
Assert.AreEqual(0, lengthCounter.GetValue());
Assert.True(lengthCounter.Enabled);
Assert.AreEqual(0, lengthCounter.Length);
}

/*
Expand Down Expand Up @@ -301,7 +301,7 @@ private void endNoDelay(int remainingLength)

private void endPassive(int remainingLength)
{
Assert.AreEqual(remainingLength, lengthCounter.GetValue());
Assert.AreEqual(remainingLength, lengthCounter.Length);
}

}
Expand Down
2 changes: 1 addition & 1 deletion CoreBoy/Gameboy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Gameboy(
Cartridge rom,
IDisplay display,
IController controller,
SoundOutput soundOutput,
ISoundOutput soundOutput,
SerialEndpoint serialEndpoint)
{
_display = display;
Expand Down
176 changes: 0 additions & 176 deletions CoreBoy/sound/AbstractSoundMode.cs

This file was deleted.

57 changes: 27 additions & 30 deletions CoreBoy/sound/FrequencySweep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ namespace CoreBoy.sound
{
public class FrequencySweep
{

private static readonly int Divider = Gameboy.TicksPerSec / 128;

// sweep parameters
Expand Down Expand Up @@ -52,10 +51,7 @@ public void SetNr10(int value)
}
}

public void SetNr13(int value)
{
_nr13 = value;
}
public void SetNr13(int value) => _nr13 = value;

public void SetNr14(int value)
{
Expand All @@ -71,35 +67,36 @@ public void SetNr14(int value)

public void Tick()
{
if (++_i == Divider)
{
_i = 0;
if (!_counterEnabled)
{
return;
}

if (--_timer == 0)
{
_timer = _period == 0 ? 8 : _period;
if (_period != 0)
{
int newFreq = Calculate();
if (!_overflow && _shift != 0)
{
_shadowFreq = newFreq;
_nr13 = _shadowFreq & 0xff;
_nr14 = (_shadowFreq & 0x700) >> 8;
Calculate();
}
}
}
}
_i++;

if (_i != Divider) return;

_i = 0;

if (!_counterEnabled) return;

_timer--;

if (_timer != 0) return;

_timer = _period == 0 ? 8 : _period;

if (_period == 0) return;

var newFreq = Calculate();

if (_overflow || _shift == 0) return;

_shadowFreq = newFreq;
_nr13 = _shadowFreq & 0xff;
_nr14 = (_shadowFreq & 0x700) >> 8;

Calculate();
}

private int Calculate()
{
int freq = _shadowFreq >> _shift;
var freq = _shadowFreq >> _shift;
if (_negate)
{
freq = _shadowFreq - freq;
Expand Down
Loading

0 comments on commit 6c409dd

Please sign in to comment.