Skip to content

Commit

Permalink
GB: Fixed noise channel zombie mode
Browse files Browse the repository at this point in the history
  • Loading branch information
SourMesen committed May 19, 2023
1 parent 607d921 commit a16a4e2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
31 changes: 26 additions & 5 deletions Core/Gameboy/APU/GbNoiseChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,22 @@ void GbNoiseChannel::UpdateOutput()

void GbNoiseChannel::ClockEnvelope()
{
if(_state.EnvTimer > 0 && _state.EnvPeriod > 0) {
if(_state.EnvTimer > 0 && _state.EnvPeriod > 0 && !_state.EnvStopped) {
_state.EnvTimer--;

if(_state.EnvTimer == 0) {
if(_state.EnvRaiseVolume && _state.Volume < 0x0F) {
_state.Volume++;
} else if(!_state.EnvRaiseVolume && _state.Volume > 0) {
_state.Volume--;
} else {
_state.EnvStopped = true;
}

_state.EnvTimer = _state.EnvPeriod;

//Based on the channel_4_volume_div test, clocking the envelope updates the output immediately
UpdateOutput();

_state.EnvTimer = _state.EnvPeriod;
}
}
}
Expand Down Expand Up @@ -168,7 +170,24 @@ void GbNoiseChannel::Write(uint16_t addr, uint8_t value)
_state.Length = 64 - (value & 0x3F);
break;

case 2:
case 2: {
if(_state.EnvPeriod == 0 && !_state.EnvStopped) {
//"If the old envelope period was zero and the envelope is still doing automatic updates, volume is incremented by 1"
_state.Volume++;
} else if(!_state.EnvRaiseVolume) {
//"otherwise if the envelope was in subtract mode, volume is incremented by 2"
_state.Volume += 2;
}

bool raiseVolume = (value & 0x08) != 0;
if(raiseVolume != _state.EnvRaiseVolume) {
//"If the mode was changed (add to subtract or subtract to add), volume is set to 16 - volume."
_state.Volume = 16 - _state.Volume;
}

//"Only the low 4 bits of volume are kept after the above operations."
_state.Volume &= 0xF;

_state.EnvPeriod = value & 0x07;
_state.EnvRaiseVolume = (value & 0x08) != 0;
_state.EnvVolume = (value & 0xF0) >> 4;
Expand All @@ -177,6 +196,7 @@ void GbNoiseChannel::Write(uint16_t addr, uint8_t value)
_state.Enabled = false;
}
break;
}

case 3:
_state.PeriodShift = (value & 0xF0) >> 4;
Expand Down Expand Up @@ -230,6 +250,7 @@ void GbNoiseChannel::Write(uint16_t addr, uint8_t value)

//Volume envelope timer is reloaded with period.
_state.EnvTimer = _state.EnvPeriod;
_state.EnvStopped = false;

//Channel volume is reloaded from NRx2.
_state.Volume = _state.EnvVolume;
Expand All @@ -243,7 +264,7 @@ void GbNoiseChannel::Write(uint16_t addr, uint8_t value)

void GbNoiseChannel::Serialize(Serializer& s)
{
SV(_state.Volume); SV(_state.EnvVolume); SV(_state.EnvRaiseVolume); SV(_state.EnvPeriod); SV(_state.EnvTimer);
SV(_state.Volume); SV(_state.EnvVolume); SV(_state.EnvRaiseVolume); SV(_state.EnvPeriod); SV(_state.EnvTimer); SV(_state.EnvStopped);
SV(_state.ShiftRegister); SV(_state.PeriodShift); SV(_state.Divisor); SV(_state.ShortWidthMode);
SV(_state.Length); SV(_state.LengthEnabled); SV(_state.Enabled); SV(_state.Timer); SV(_state.Output);
}
1 change: 1 addition & 0 deletions Core/Gameboy/GbTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ struct GbNoiseState
bool EnvRaiseVolume;
uint8_t EnvPeriod;
uint8_t EnvTimer;
bool EnvStopped;

uint8_t Length;
bool LengthEnabled;
Expand Down
1 change: 1 addition & 0 deletions UI/Interop/DebugState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,7 @@ public struct GbNoiseState
[MarshalAs(UnmanagedType.I1)] public bool EnvRaiseVolume;
public byte EnvPeriod;
public byte EnvTimer;
[MarshalAs(UnmanagedType.I1)] public bool EnvStopped;

public byte Length;
[MarshalAs(UnmanagedType.I1)] public bool LengthEnabled;
Expand Down

0 comments on commit a16a4e2

Please sign in to comment.