Skip to content

Commit 5e7fd5a

Browse files
committed
CDROM: Fix integer overflow in seek timing calculation
1 parent 1947080 commit 5e7fd5a

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/core/cdrom.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -818,13 +818,21 @@ TickCount CDROM::GetTicksForSeek(CDImage::LBA new_lba)
818818
const u32 lba_diff = static_cast<u32>((new_lba > current_lba) ? (new_lba - current_lba) : (current_lba - new_lba));
819819

820820
// Formula from Mednafen.
821-
TickCount ticks = std::max<TickCount>(20000, lba_diff * MASTER_CLOCK * 1000 / (72 * 60 * 75) / 1000);
821+
TickCount ticks = std::max<TickCount>(
822+
20000, static_cast<u32>(
823+
((static_cast<u64>(lba_diff) * static_cast<u64>(MASTER_CLOCK) * static_cast<u64>(1000)) / (72 * 60 * 75)) /
824+
1000));
822825
if (!m_secondary_status.motor_on)
823826
ticks += MASTER_CLOCK;
824827
if (lba_diff >= 2550)
825828
ticks += static_cast<TickCount>(u64(MASTER_CLOCK) * 300 / 1000);
826829
else if (m_drive_state == DriveState::Idle) // paused
827-
ticks += 1237952 << (BoolToUInt8(!m_mode.double_speed));
830+
{
831+
// When paused, the CDC seems to keep reading the disc until it hits the position it's set to, then skip 10-15
832+
// sectors back (depending on how far into the disc it is). We'll be generous and use 4 sectors, since on average
833+
// it's probably closer.
834+
ticks += GetTicksForRead() * 4u;
835+
}
828836

829837
if (m_mode.double_speed != m_current_double_speed)
830838
{
@@ -836,10 +844,6 @@ TickCount CDROM::GetTicksForSeek(CDImage::LBA new_lba)
836844
ticks += static_cast<u32>(static_cast<double>(MASTER_CLOCK) * 0.1);
837845
}
838846

839-
// it's unlikely that the drive would seek to exactly the correct position, so simulate this by adding the time
840-
// required to read a few sectors
841-
ticks += GetTicksForRead() * 4u;
842-
843847
Log_DevPrintf("Seek time for %u LBAs: %d", lba_diff, ticks);
844848
return ticks;
845849
}

0 commit comments

Comments
 (0)