2014-04-06 13:11:19 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
|
|
|
* Copyright (C) 2002-2014 David Quintana [gigaherz]
|
|
|
|
*
|
|
|
|
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU Lesser General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-04-24 21:37:39 +00:00
|
|
|
#include "CDVD.h"
|
2016-10-25 00:32:51 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <condition_variable>
|
2016-11-11 17:51:19 +00:00
|
|
|
#include <limits>
|
2016-11-17 20:07:31 +00:00
|
|
|
#include <queue>
|
2016-10-25 00:51:36 +00:00
|
|
|
#include <thread>
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-11-17 20:07:31 +00:00
|
|
|
struct CacheRequest
|
|
|
|
{
|
|
|
|
u32 lsn;
|
|
|
|
s32 mode;
|
|
|
|
};
|
|
|
|
|
2016-11-11 17:51:19 +00:00
|
|
|
struct SectorInfo
|
|
|
|
{
|
|
|
|
u32 lsn;
|
|
|
|
s32 mode;
|
|
|
|
u8 data[2352 * 16]; // we will read in blocks of 16 sectors
|
|
|
|
};
|
|
|
|
|
2016-09-19 17:24:54 +00:00
|
|
|
const s32 prefetch_max_blocks = 16;
|
|
|
|
s32 prefetch_mode = 0;
|
|
|
|
s32 prefetch_last_lba = 0;
|
|
|
|
s32 prefetch_last_mode = 0;
|
|
|
|
s32 prefetch_left = 0;
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-10-25 00:51:36 +00:00
|
|
|
static std::thread s_thread;
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-10-25 00:32:51 +00:00
|
|
|
static std::mutex s_notify_lock;
|
|
|
|
static std::condition_variable s_notify_cv;
|
|
|
|
static std::mutex s_request_lock;
|
2016-11-17 20:07:31 +00:00
|
|
|
static std::queue<CacheRequest> s_request_queue;
|
2016-10-24 23:08:09 +00:00
|
|
|
static std::mutex s_cache_lock;
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-10-25 00:32:51 +00:00
|
|
|
static std::atomic<bool> cdvd_is_open;
|
|
|
|
|
2014-05-26 12:28:56 +00:00
|
|
|
//bits: 12 would use 1<<12 entries, or 4096*16 sectors ~ 128MB
|
|
|
|
#define CACHE_SIZE 12
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-11-11 17:51:19 +00:00
|
|
|
const u32 CacheSize = 1U << CACHE_SIZE;
|
2010-04-24 21:37:39 +00:00
|
|
|
SectorInfo Cache[CacheSize];
|
|
|
|
|
2016-11-11 17:51:19 +00:00
|
|
|
u32 cdvdSectorHash(u32 lsn, s32 mode)
|
2010-04-24 21:37:39 +00:00
|
|
|
{
|
2016-09-19 17:24:54 +00:00
|
|
|
u32 t = 0;
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-09-19 17:24:54 +00:00
|
|
|
int i = 32;
|
2016-11-11 17:51:19 +00:00
|
|
|
u32 m = CacheSize - 1;
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-09-19 17:24:54 +00:00
|
|
|
while (i >= 0) {
|
|
|
|
t ^= lsn & m;
|
|
|
|
lsn >>= CACHE_SIZE;
|
|
|
|
i -= CACHE_SIZE;
|
|
|
|
}
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-09-19 17:24:54 +00:00
|
|
|
return (t ^ mode) & m;
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 17:51:19 +00:00
|
|
|
void cdvdCacheUpdate(u32 lsn, s32 mode, u8 *data)
|
2010-04-24 21:37:39 +00:00
|
|
|
{
|
2016-10-24 23:08:09 +00:00
|
|
|
std::lock_guard<std::mutex> guard(s_cache_lock);
|
2016-09-19 17:24:54 +00:00
|
|
|
u32 entry = cdvdSectorHash(lsn, mode);
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-09-19 17:24:54 +00:00
|
|
|
memcpy(Cache[entry].data, data, 2352 * 16);
|
|
|
|
Cache[entry].lsn = lsn;
|
|
|
|
Cache[entry].mode = mode;
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 20:50:36 +00:00
|
|
|
bool cdvdCacheCheck(u32 lsn, s32 mode)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> guard(s_cache_lock);
|
|
|
|
u32 entry = cdvdSectorHash(lsn, mode);
|
|
|
|
|
|
|
|
return Cache[entry].lsn == lsn && Cache[entry].mode == mode;
|
|
|
|
}
|
|
|
|
|
2016-11-11 17:51:19 +00:00
|
|
|
bool cdvdCacheFetch(u32 lsn, s32 mode, u8 *data)
|
2010-04-24 21:37:39 +00:00
|
|
|
{
|
2016-10-24 23:08:09 +00:00
|
|
|
std::lock_guard<std::mutex> guard(s_cache_lock);
|
2016-09-19 17:24:54 +00:00
|
|
|
u32 entry = cdvdSectorHash(lsn, mode);
|
|
|
|
|
|
|
|
if ((Cache[entry].lsn == lsn) &&
|
|
|
|
(Cache[entry].mode == mode)) {
|
|
|
|
memcpy(data, Cache[entry].data, 2352 * 16);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//printf("NOT IN CACHE\n");
|
|
|
|
return false;
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cdvdCacheReset()
|
|
|
|
{
|
2016-10-24 23:08:09 +00:00
|
|
|
std::lock_guard<std::mutex> guard(s_cache_lock);
|
2016-11-11 17:51:19 +00:00
|
|
|
for (u32 i = 0; i < CacheSize; i++) {
|
|
|
|
Cache[i].lsn = std::numeric_limits<u32>::max();
|
2016-09-19 17:24:54 +00:00
|
|
|
Cache[i].mode = -1;
|
|
|
|
}
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 20:42:31 +00:00
|
|
|
bool cdvdReadBlockOfSectors(u32 sector, s32 mode, u8 *data)
|
|
|
|
{
|
|
|
|
u32 count = std::min(16U, src->GetSectorCount() - sector);
|
|
|
|
|
|
|
|
// TODO: Is it really necessary to retry 3 times if it fails?
|
|
|
|
for (int tries = 0; tries < 4; ++tries) {
|
|
|
|
if (mode == CDVD_MODE_2048) {
|
|
|
|
if (src->ReadSectors2048(sector, count, data))
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if (src->ReadSectors2352(sector, count, data))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-24 21:37:39 +00:00
|
|
|
void cdvdCallNewDiscCB()
|
|
|
|
{
|
2016-09-19 17:24:54 +00:00
|
|
|
weAreInNewDiskCB = true;
|
|
|
|
newDiscCB();
|
|
|
|
weAreInNewDiskCB = false;
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cdvdUpdateDiscStatus()
|
|
|
|
{
|
2016-10-22 23:53:41 +00:00
|
|
|
bool ready = src->DiscReady();
|
2016-09-19 17:24:54 +00:00
|
|
|
|
2016-10-22 23:53:41 +00:00
|
|
|
if (!ready) {
|
2016-09-19 17:24:54 +00:00
|
|
|
if (!disc_has_changed) {
|
|
|
|
disc_has_changed = true;
|
|
|
|
curDiskType = CDVD_TYPE_NODISC;
|
|
|
|
curTrayStatus = CDVD_TRAY_OPEN;
|
|
|
|
cdvdCallNewDiscCB();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (disc_has_changed) {
|
|
|
|
curDiskType = CDVD_TYPE_NODISC;
|
|
|
|
curTrayStatus = CDVD_TRAY_CLOSE;
|
|
|
|
|
|
|
|
disc_has_changed = false;
|
|
|
|
cdvdRefreshData();
|
2016-11-17 20:07:31 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> request_guard(s_request_lock);
|
|
|
|
s_request_queue = std::queue<CacheRequest>();
|
|
|
|
}
|
|
|
|
|
2016-09-19 17:24:54 +00:00
|
|
|
cdvdCallNewDiscCB();
|
|
|
|
}
|
|
|
|
}
|
2016-10-22 23:53:41 +00:00
|
|
|
return !ready;
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 00:51:36 +00:00
|
|
|
void cdvdThread()
|
2010-04-24 21:37:39 +00:00
|
|
|
{
|
2016-11-17 20:07:31 +00:00
|
|
|
u8 buffer[2352 * 16];
|
|
|
|
|
2016-09-19 17:24:54 +00:00
|
|
|
printf(" * CDVD: IO thread started...\n");
|
2016-10-25 00:32:51 +00:00
|
|
|
std::unique_lock<std::mutex> guard(s_notify_lock);
|
2016-09-19 17:24:54 +00:00
|
|
|
|
|
|
|
while (cdvd_is_open) {
|
|
|
|
if (cdvdUpdateDiscStatus()) {
|
|
|
|
// Need to sleep some to avoid an aggressive spin that sucks the cpu dry.
|
2016-10-25 00:51:36 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
2016-09-19 17:24:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-25 00:32:51 +00:00
|
|
|
s_notify_cv.wait_for(guard, std::chrono::milliseconds(prefetch_left ? 1 : 250));
|
2016-09-19 17:24:54 +00:00
|
|
|
|
|
|
|
// check again to make sure we're not done here...
|
|
|
|
if (!cdvd_is_open)
|
|
|
|
break;
|
|
|
|
|
2016-11-17 20:07:31 +00:00
|
|
|
bool handling_request = false;
|
|
|
|
CacheRequest request;
|
2016-09-19 17:24:54 +00:00
|
|
|
|
2016-11-17 20:07:31 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> request_guard(s_request_lock);
|
|
|
|
if (!s_request_queue.empty()) {
|
|
|
|
request = s_request_queue.front();
|
|
|
|
s_request_queue.pop();
|
|
|
|
handling_request = true;
|
|
|
|
} else {
|
|
|
|
request.lsn = prefetch_last_lba;
|
|
|
|
request.mode = prefetch_last_mode;
|
|
|
|
}
|
2016-09-19 17:24:54 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 20:07:31 +00:00
|
|
|
if (handling_request || prefetch_left) {
|
|
|
|
if (!cdvdCacheCheck(request.lsn, request.mode))
|
|
|
|
if (cdvdReadBlockOfSectors(request.lsn, request.mode, buffer))
|
|
|
|
cdvdCacheUpdate(request.lsn, request.mode, buffer);
|
2016-09-19 17:24:54 +00:00
|
|
|
|
2016-11-17 20:07:31 +00:00
|
|
|
if (handling_request) {
|
|
|
|
prefetch_last_lba = request.lsn;
|
|
|
|
prefetch_last_mode = request.mode;
|
2016-09-19 17:24:54 +00:00
|
|
|
|
|
|
|
prefetch_left = prefetch_max_blocks;
|
|
|
|
} else {
|
|
|
|
prefetch_last_lba += 16;
|
|
|
|
prefetch_left--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf(" * CDVD: IO thread finished.\n");
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 00:51:36 +00:00
|
|
|
bool cdvdStartThread()
|
2010-04-24 21:37:39 +00:00
|
|
|
{
|
2016-09-19 17:24:54 +00:00
|
|
|
cdvd_is_open = true;
|
2016-10-25 00:51:36 +00:00
|
|
|
try {
|
|
|
|
s_thread = std::thread(cdvdThread);
|
|
|
|
} catch (std::system_error &) {
|
|
|
|
cdvd_is_open = false;
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-09-19 17:24:54 +00:00
|
|
|
cdvdCacheReset();
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-10-25 00:51:36 +00:00
|
|
|
return true;
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cdvdStopThread()
|
|
|
|
{
|
2016-09-19 17:24:54 +00:00
|
|
|
cdvd_is_open = false;
|
2016-10-25 00:32:51 +00:00
|
|
|
s_notify_cv.notify_one();
|
2016-10-25 00:51:36 +00:00
|
|
|
s_thread.join();
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s32 cdvdRequestSector(u32 sector, s32 mode)
|
|
|
|
{
|
2016-10-17 19:15:57 +00:00
|
|
|
if (sector >= src->GetSectorCount())
|
2016-09-19 17:24:54 +00:00
|
|
|
return -1;
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-10-16 19:39:56 +00:00
|
|
|
sector &= ~15; //align to 16-sector block
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-11-17 20:07:31 +00:00
|
|
|
if (cdvdCacheCheck(sector, mode))
|
2016-09-19 17:24:54 +00:00
|
|
|
return 0;
|
2016-11-17 20:07:31 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> guard(s_request_lock);
|
|
|
|
s_request_queue.push({sector, mode});
|
2016-09-19 17:24:54 +00:00
|
|
|
}
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-10-25 00:32:51 +00:00
|
|
|
s_notify_cv.notify_one();
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-09-19 17:24:54 +00:00
|
|
|
return 0;
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 17:51:19 +00:00
|
|
|
u8 *cdvdGetSector(u32 sector, s32 mode)
|
2010-04-24 21:37:39 +00:00
|
|
|
{
|
2016-11-16 22:02:00 +00:00
|
|
|
static u8 buffer[2352 * 16];
|
|
|
|
u32 sector_block = sector & ~15;
|
|
|
|
|
|
|
|
if (!cdvdCacheFetch(sector_block, mode, buffer))
|
|
|
|
if (cdvdReadBlockOfSectors(sector_block, mode, buffer))
|
|
|
|
cdvdCacheUpdate(sector_block, mode, buffer);
|
2016-09-19 17:24:54 +00:00
|
|
|
|
|
|
|
if (mode == CDVD_MODE_2048) {
|
2016-11-16 22:02:00 +00:00
|
|
|
u32 offset = 2048 * (sector - sector_block);
|
|
|
|
return buffer + offset;
|
2016-09-19 17:24:54 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 22:02:00 +00:00
|
|
|
u32 offset = 2352 * (sector - sector_block);
|
|
|
|
u8 *data = buffer + offset;
|
2016-09-19 17:24:54 +00:00
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case CDVD_MODE_2328:
|
|
|
|
return data + 24;
|
|
|
|
case CDVD_MODE_2340:
|
|
|
|
return data + 12;
|
|
|
|
}
|
|
|
|
return data;
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 17:51:19 +00:00
|
|
|
s32 cdvdDirectReadSector(u32 first, s32 mode, u8 *buffer)
|
2010-04-24 21:37:39 +00:00
|
|
|
{
|
2016-11-11 17:51:19 +00:00
|
|
|
static u8 data[16 * 2352];
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-11-11 17:51:19 +00:00
|
|
|
if (first >= src->GetSectorCount())
|
2016-09-19 17:24:54 +00:00
|
|
|
return -1;
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-11-11 17:51:19 +00:00
|
|
|
u32 sector = first & (~15); //align to 16-sector block
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-09-19 17:24:54 +00:00
|
|
|
if (!cdvdCacheFetch(sector, mode, data)) {
|
2016-11-16 20:42:31 +00:00
|
|
|
if (cdvdReadBlockOfSectors(sector, mode, data))
|
|
|
|
cdvdCacheUpdate(sector, mode, data);
|
2016-09-19 17:24:54 +00:00
|
|
|
}
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-09-19 17:24:54 +00:00
|
|
|
if (mode == CDVD_MODE_2048) {
|
2016-11-11 17:51:19 +00:00
|
|
|
u32 offset = 2048 * (first - sector);
|
2016-09-19 17:24:54 +00:00
|
|
|
memcpy(buffer, data + offset, 2048);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2016-11-11 17:51:19 +00:00
|
|
|
u32 offset = 2352 * (first - sector);
|
|
|
|
u8 *bfr = data + offset;
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-09-19 17:24:54 +00:00
|
|
|
switch (mode) {
|
|
|
|
case CDVD_MODE_2328:
|
|
|
|
memcpy(buffer, bfr + 24, 2328);
|
|
|
|
return 0;
|
|
|
|
case CDVD_MODE_2340:
|
|
|
|
memcpy(buffer, bfr + 12, 2340);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
memcpy(buffer, bfr + 12, 2352);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s32 cdvdGetMediaType()
|
|
|
|
{
|
2016-09-19 17:24:54 +00:00
|
|
|
return src->GetMediaType();
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s32 cdvdRefreshData()
|
|
|
|
{
|
2016-10-26 16:31:26 +00:00
|
|
|
const char *diskTypeName = "Unknown";
|
2016-09-19 17:24:54 +00:00
|
|
|
|
|
|
|
//read TOC from device
|
|
|
|
cdvdParseTOC();
|
|
|
|
|
|
|
|
if ((etrack == 0) || (strack > etrack)) {
|
|
|
|
curDiskType = CDVD_TYPE_NODISC;
|
|
|
|
} else {
|
|
|
|
s32 mt = cdvdGetMediaType();
|
|
|
|
|
|
|
|
if (mt < 0)
|
|
|
|
curDiskType = CDVD_TYPE_DETCTCD;
|
|
|
|
else if (mt == 0)
|
|
|
|
curDiskType = CDVD_TYPE_DETCTDVDS;
|
|
|
|
else
|
|
|
|
curDiskType = CDVD_TYPE_DETCTDVDD;
|
|
|
|
}
|
|
|
|
|
|
|
|
curTrayStatus = CDVD_TRAY_CLOSE;
|
|
|
|
|
|
|
|
switch (curDiskType) {
|
|
|
|
case CDVD_TYPE_DETCTDVDD:
|
|
|
|
diskTypeName = "Double-Layer DVD";
|
|
|
|
break;
|
|
|
|
case CDVD_TYPE_DETCTDVDS:
|
|
|
|
diskTypeName = "Single-Layer DVD";
|
|
|
|
break;
|
|
|
|
case CDVD_TYPE_DETCTCD:
|
|
|
|
diskTypeName = "CD-ROM";
|
|
|
|
break;
|
|
|
|
case CDVD_TYPE_NODISC:
|
|
|
|
diskTypeName = "No Disc";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(" * CDVD: Disk Type: %s\n", diskTypeName);
|
|
|
|
|
|
|
|
cdvdCacheReset();
|
|
|
|
|
|
|
|
return 0;
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|