From 6ed67468d2f1ade8b7cdff82fe8a878724859526 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Tue, 14 Jul 2020 01:26:37 +1000 Subject: [PATCH] CDROM: Prevent games which spam Reset from getting wedged --- src/core/cdrom.cpp | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/core/cdrom.cpp b/src/core/cdrom.cpp index 3cb025446..d4c03127e 100644 --- a/src/core/cdrom.cpp +++ b/src/core/cdrom.cpp @@ -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). @@ -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; @@ -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(static_cast(m_seek_end_lba - m_seek_start_lba) * completed_frac); + current_lba = + m_seek_start_lba + + std::max( + static_cast(static_cast(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( + static_cast(static_cast(m_seek_start_lba - m_seek_end_lba) * completed_frac), 1); } else { - current_lba = m_seek_start_lba - - static_cast(static_cast(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,