Skip to content

Commit

Permalink
CDROM: Prevent games which spam Reset from getting wedged
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Jul 13, 2020
1 parent 9a2f222 commit 6ed6746
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/core/cdrom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ void CDROM::RemoveMedia(bool force /* = false */)
m_drive_event->Deactivate();

// The console sends an interrupt when the shell is opened regardless of whether a command was executing.
if (HasPendingAsyncInterrupt())
ClearAsyncInterrupt();
SendAsyncErrorResponse(STAT_ERROR, 0x08);

// Begin spin-down timer, we can't swap the new disc in immediately for some games (e.g. Metal Gear Solid).
Expand Down Expand Up @@ -1184,7 +1186,7 @@ void CDROM::ExecuteCommand()
UpdatePositionWhileSeeking();

m_drive_state = DriveState::Resetting;
m_drive_event->SetIntervalAndSchedule(BASE_RESET_TICKS + GetTicksForSeek(0));
m_drive_event->SetIntervalAndSchedule(BASE_RESET_TICKS + ((m_current_lba != 0) ? GetTicksForSeek(0) : 0));
m_seek_start_lba = m_current_lba;
m_seek_end_lba = 0;

Expand Down Expand Up @@ -1631,13 +1633,22 @@ void CDROM::UpdatePositionWhileSeeking()
CDImage::LBA current_lba;
if (m_seek_end_lba > m_seek_start_lba)
{
current_lba = m_seek_start_lba +
static_cast<CDImage::LBA>(static_cast<float>(m_seek_end_lba - m_seek_start_lba) * completed_frac);
current_lba =
m_seek_start_lba +
std::max<CDImage::LBA>(
static_cast<CDImage::LBA>(static_cast<float>(m_seek_end_lba - m_seek_start_lba) * completed_frac), 1);
}
else if (m_seek_end_lba < m_seek_start_lba)
{
current_lba =
m_seek_start_lba -
std::max<CDImage::LBA>(
static_cast<CDImage::LBA>(static_cast<float>(m_seek_start_lba - m_seek_end_lba) * completed_frac), 1);
}
else
{
current_lba = m_seek_start_lba -
static_cast<CDImage::LBA>(static_cast<float>(m_seek_start_lba - m_seek_end_lba) * completed_frac);
// strange seek...
return;
}

Log_DevPrintf("Update position while seeking from %u to %u - %u (%.2f)", m_seek_start_lba, m_seek_end_lba,
Expand Down

0 comments on commit 6ed6746

Please sign in to comment.