mirror of
https://github.com/libretro/beetle-pce-fast-libretro.git
synced 2024-11-23 07:50:03 +00:00
Slim down cdromif - no need for multithreading (slows down if any) -
take out superfluous exception throwing
This commit is contained in:
parent
04a4a089f9
commit
56ab33cd55
@ -65,375 +65,6 @@ CDIF_Message::~CDIF_Message()
|
||||
|
||||
}
|
||||
|
||||
#ifdef WANT_CDIF_MT
|
||||
CDIF_Queue::CDIF_Queue()
|
||||
{
|
||||
ze_mutex = MDFND_CreateMutex();
|
||||
}
|
||||
|
||||
CDIF_Queue::~CDIF_Queue()
|
||||
{
|
||||
MDFND_DestroyMutex(ze_mutex);
|
||||
}
|
||||
|
||||
// Returns FALSE if message not read, TRUE if it was read. Will always return TRUE if "blocking" is set.
|
||||
// Will throw MDFN_Error if the read message code is CDIF_MSG_FATAL_ERROR
|
||||
bool CDIF_Queue::Read(CDIF_Message *message, bool blocking)
|
||||
{
|
||||
TryAgain:
|
||||
|
||||
MDFND_LockMutex(ze_mutex);
|
||||
|
||||
if(ze_queue.size() > 0)
|
||||
{
|
||||
*message = ze_queue.front();
|
||||
ze_queue.pop();
|
||||
MDFND_UnlockMutex(ze_mutex);
|
||||
|
||||
if(message->message == CDIF_MSG_FATAL_ERROR)
|
||||
throw MDFN_Error(0, "%s", message->str_message.c_str());
|
||||
|
||||
return(TRUE);
|
||||
}
|
||||
else if(blocking)
|
||||
{
|
||||
MDFND_UnlockMutex(ze_mutex);
|
||||
MDFND_Sleep(1);
|
||||
goto TryAgain;
|
||||
}
|
||||
else
|
||||
{
|
||||
MDFND_UnlockMutex(ze_mutex);
|
||||
return(FALSE);
|
||||
}
|
||||
}
|
||||
|
||||
void CDIF_Queue::Write(const CDIF_Message &message)
|
||||
{
|
||||
MDFND_LockMutex(ze_mutex);
|
||||
|
||||
ze_queue.push(message);
|
||||
|
||||
MDFND_UnlockMutex(ze_mutex);
|
||||
}
|
||||
|
||||
void CDIF_MT::RT_EjectDisc(bool eject_status, bool skip_actual_eject)
|
||||
{
|
||||
int32 old_de = DiscEjected;
|
||||
|
||||
DiscEjected = eject_status;
|
||||
|
||||
if(old_de != DiscEjected)
|
||||
{
|
||||
if(!skip_actual_eject)
|
||||
disc_cdaccess->Eject(eject_status);
|
||||
|
||||
if(!eject_status) // Re-read the TOC
|
||||
{
|
||||
disc_cdaccess->Read_TOC(&disc_toc);
|
||||
|
||||
if(disc_toc.first_track < 1 || disc_toc.last_track > 99 || disc_toc.first_track > disc_toc.last_track)
|
||||
{
|
||||
throw(MDFN_Error(0, _("TOC first(%d)/last(%d) track numbers bad."), disc_toc.first_track, disc_toc.last_track));
|
||||
}
|
||||
}
|
||||
|
||||
SBWritePos = 0;
|
||||
ra_lba = 0;
|
||||
ra_count = 0;
|
||||
last_read_lba = ~0U;
|
||||
memset(SectorBuffers, 0, SBSize * sizeof(CDIF_Sector_Buffer));
|
||||
}
|
||||
}
|
||||
|
||||
struct RTS_Args
|
||||
{
|
||||
CDIF_MT *cdif_ptr;
|
||||
const char *device_name;
|
||||
};
|
||||
|
||||
static int ReadThreadStart_C(void *v_arg)
|
||||
{
|
||||
RTS_Args *args = (RTS_Args *)v_arg;
|
||||
|
||||
//zero 07-feb-2012 - fix dynamic stack buffer
|
||||
if(args->device_name)
|
||||
{
|
||||
char *device_name = new char[strlen(args->device_name) + 1];
|
||||
|
||||
strcpy(device_name, args->device_name);
|
||||
|
||||
int ret = args->cdif_ptr->ReadThreadStart(device_name);
|
||||
delete[] device_name;
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
return args->cdif_ptr->ReadThreadStart(NULL);
|
||||
}
|
||||
|
||||
int CDIF_MT::ReadThreadStart(const char *device_name)
|
||||
{
|
||||
bool Running = TRUE;
|
||||
|
||||
DiscEjected = true;
|
||||
SBWritePos = 0;
|
||||
ra_lba = 0;
|
||||
ra_count = 0;
|
||||
last_read_lba = ~0U;
|
||||
|
||||
try
|
||||
{
|
||||
disc_cdaccess = cdaccess_open(device_name ? device_name : NULL, false);
|
||||
RT_EjectDisc(false, true);
|
||||
}
|
||||
catch(std::exception &e)
|
||||
{
|
||||
EmuThreadQueue.Write(CDIF_Message(CDIF_MSG_FATAL_ERROR, std::string(e.what())));
|
||||
return(0);
|
||||
}
|
||||
|
||||
is_phys_cache = disc_cdaccess->Is_Physical();
|
||||
|
||||
EmuThreadQueue.Write(CDIF_Message(CDIF_MSG_DONE));
|
||||
|
||||
while(Running)
|
||||
{
|
||||
CDIF_Message msg;
|
||||
|
||||
// Only do a blocking-wait for a message if we don't have any sectors to read-ahead.
|
||||
// MDFN_DispMessage("%d %d %d\n", last_read_lba, ra_lba, ra_count);
|
||||
if(ReadThreadQueue.Read(&msg, ra_count ? FALSE : TRUE))
|
||||
{
|
||||
switch(msg.message)
|
||||
{
|
||||
case CDIF_MSG_DIEDIEDIE:
|
||||
Running = FALSE;
|
||||
break;
|
||||
|
||||
case CDIF_MSG_EJECT:
|
||||
try
|
||||
{
|
||||
RT_EjectDisc(msg.args[0]);
|
||||
EmuThreadQueue.Write(CDIF_Message(CDIF_MSG_DONE));
|
||||
}
|
||||
catch(std::exception &e)
|
||||
{
|
||||
EmuThreadQueue.Write(CDIF_Message(CDIF_MSG_FATAL_ERROR, std::string(e.what())));
|
||||
}
|
||||
break;
|
||||
|
||||
case CDIF_MSG_READ_SECTOR:
|
||||
{
|
||||
static const int max_ra = 16;
|
||||
static const int initial_ra = 1;
|
||||
static const int speedmult_ra = 2;
|
||||
uint32 new_lba = msg.args[0];
|
||||
|
||||
assert((unsigned int)max_ra < (SBSize / 4));
|
||||
|
||||
if(last_read_lba != ~0U && new_lba == (last_read_lba + 1))
|
||||
{
|
||||
int how_far_ahead = ra_lba - new_lba;
|
||||
|
||||
if(how_far_ahead <= max_ra)
|
||||
ra_count = std::min(speedmult_ra, 1 + max_ra - how_far_ahead);
|
||||
else
|
||||
ra_count++;
|
||||
}
|
||||
else if(new_lba != last_read_lba)
|
||||
{
|
||||
ra_lba = new_lba;
|
||||
ra_count = initial_ra;
|
||||
}
|
||||
|
||||
last_read_lba = new_lba;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Don't read >= the "end" of the disc, silly snake. Slither.
|
||||
if(ra_count && ra_lba == disc_toc.tracks[100].lba)
|
||||
{
|
||||
ra_count = 0;
|
||||
//printf("Ephemeral scarabs: %d!\n", ra_lba);
|
||||
}
|
||||
|
||||
if(ra_count)
|
||||
{
|
||||
uint8 tmpbuf[2352 + 96];
|
||||
bool error_condition = false;
|
||||
|
||||
try
|
||||
{
|
||||
disc_cdaccess->Read_Raw_Sector(tmpbuf, ra_lba);
|
||||
}
|
||||
catch(std::exception &e)
|
||||
{
|
||||
MDFN_PrintError(_("Sector %u read error: %s"), ra_lba, e.what());
|
||||
memset(tmpbuf, 0, sizeof(tmpbuf));
|
||||
error_condition = true;
|
||||
}
|
||||
|
||||
MDFND_LockMutex(SBMutex);
|
||||
|
||||
SectorBuffers[SBWritePos].lba = ra_lba;
|
||||
memcpy(SectorBuffers[SBWritePos].data, tmpbuf, 2352 + 96);
|
||||
SectorBuffers[SBWritePos].valid = TRUE;
|
||||
SectorBuffers[SBWritePos].error = error_condition;
|
||||
SBWritePos = (SBWritePos + 1) % SBSize;
|
||||
|
||||
MDFND_UnlockMutex(SBMutex);
|
||||
|
||||
ra_lba++;
|
||||
ra_count--;
|
||||
}
|
||||
}
|
||||
|
||||
if(disc_cdaccess)
|
||||
{
|
||||
delete disc_cdaccess;
|
||||
disc_cdaccess = NULL;
|
||||
}
|
||||
|
||||
return(1);
|
||||
}
|
||||
|
||||
CDIF_MT::CDIF_MT(const char *device_name) : CDReadThread(NULL), SBMutex(NULL)
|
||||
{
|
||||
try
|
||||
{
|
||||
CDIF_Message msg;
|
||||
RTS_Args s;
|
||||
|
||||
SBMutex = MDFND_CreateMutex();
|
||||
UnrecoverableError = false;
|
||||
|
||||
s.cdif_ptr = this;
|
||||
s.device_name = device_name;
|
||||
|
||||
CDReadThread = MDFND_CreateThread(ReadThreadStart_C, &s);
|
||||
EmuThreadQueue.Read(&msg);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
if(CDReadThread)
|
||||
{
|
||||
MDFND_WaitThread(CDReadThread, NULL);
|
||||
CDReadThread = NULL;
|
||||
}
|
||||
|
||||
if(SBMutex)
|
||||
{
|
||||
MDFND_DestroyMutex(SBMutex);
|
||||
SBMutex = NULL;
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
CDIF_MT::~CDIF_MT()
|
||||
{
|
||||
bool thread_murdered_with_kitchen_knife = false;
|
||||
|
||||
try
|
||||
{
|
||||
ReadThreadQueue.Write(CDIF_Message(CDIF_MSG_DIEDIEDIE));
|
||||
}
|
||||
catch(std::exception &e)
|
||||
{
|
||||
MDFND_PrintError(e.what());
|
||||
MDFND_KillThread(CDReadThread);
|
||||
thread_murdered_with_kitchen_knife = true;
|
||||
}
|
||||
|
||||
if(!thread_murdered_with_kitchen_knife)
|
||||
MDFND_WaitThread(CDReadThread, NULL);
|
||||
|
||||
if(SBMutex)
|
||||
{
|
||||
MDFND_DestroyMutex(SBMutex);
|
||||
SBMutex = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool CDIF_MT::ReadRawSector(uint8 *buf, uint32 lba)
|
||||
{
|
||||
bool found = FALSE;
|
||||
bool error_condition = false;
|
||||
|
||||
if(UnrecoverableError)
|
||||
{
|
||||
memset(buf, 0, 2352 + 96);
|
||||
return(false);
|
||||
}
|
||||
|
||||
// This shouldn't happen, the emulated-system-specific CDROM emulation code should make sure the emulated program doesn't try
|
||||
// to read past the last "real" sector of the disc.
|
||||
if(lba >= disc_toc.tracks[100].lba)
|
||||
{
|
||||
printf("Attempt to read LBA %d, >= LBA %d\n", lba, disc_toc.tracks[100].lba);
|
||||
return(FALSE);
|
||||
}
|
||||
|
||||
ReadThreadQueue.Write(CDIF_Message(CDIF_MSG_READ_SECTOR, lba));
|
||||
|
||||
do
|
||||
{
|
||||
MDFND_LockMutex(SBMutex);
|
||||
|
||||
for(int i = 0; i < SBSize; i++)
|
||||
{
|
||||
if(SectorBuffers[i].valid && SectorBuffers[i].lba == lba)
|
||||
{
|
||||
error_condition = SectorBuffers[i].error;
|
||||
memcpy(buf, SectorBuffers[i].data, 2352 + 96);
|
||||
found = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
MDFND_UnlockMutex(SBMutex);
|
||||
|
||||
if(!found)
|
||||
MDFND_Sleep(1);
|
||||
} while(!found);
|
||||
|
||||
return(!error_condition);
|
||||
}
|
||||
|
||||
void CDIF_MT::HintReadSector(uint32 lba)
|
||||
{
|
||||
if(UnrecoverableError)
|
||||
return;
|
||||
|
||||
ReadThreadQueue.Write(CDIF_Message(CDIF_MSG_READ_SECTOR, lba));
|
||||
}
|
||||
|
||||
bool CDIF_MT::Eject(bool eject_status)
|
||||
{
|
||||
if(UnrecoverableError)
|
||||
return(false);
|
||||
|
||||
try
|
||||
{
|
||||
CDIF_Message msg;
|
||||
|
||||
ReadThreadQueue.Write(CDIF_Message(CDIF_MSG_EJECT, eject_status));
|
||||
EmuThreadQueue.Read(&msg);
|
||||
}
|
||||
catch(std::exception &e)
|
||||
{
|
||||
MDFN_PrintError(_("Error on eject/insert attempt: %s"), e.what());
|
||||
return(false);
|
||||
}
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
#endif //WANT_CDIF_MT
|
||||
|
||||
bool CDIF::ValidateRawSector(uint8 *buf)
|
||||
{
|
||||
int mode = buf[12 + 3];
|
||||
@ -647,14 +278,7 @@ void CDIF_Stream_Thing::unmap(void)
|
||||
uint64 CDIF_Stream_Thing::read(void *data, uint64 count, bool error_on_eos)
|
||||
{
|
||||
if(count > (((uint64)sector_count * 2048) - position))
|
||||
{
|
||||
if(error_on_eos)
|
||||
{
|
||||
throw MDFN_Error(0, "EOF");
|
||||
}
|
||||
|
||||
count = ((uint64)sector_count * 2048) - position;
|
||||
}
|
||||
|
||||
if(!count)
|
||||
return(0);
|
||||
@ -663,12 +287,7 @@ uint64 CDIF_Stream_Thing::read(void *data, uint64 count, bool error_on_eos)
|
||||
{
|
||||
uint8 buf[2048];
|
||||
|
||||
if(!cdintf->ReadSector(buf, start_lba + (rp / 2048), 1))
|
||||
{
|
||||
throw MDFN_Error(ErrnoHolder(EIO));
|
||||
}
|
||||
|
||||
//::printf("Meow: %08llx -- %08llx\n", count, (rp - position) + std::min<uint64>(2048 - (rp & 2047), count - (rp - position)));
|
||||
cdintf->ReadSector(buf, start_lba + (rp / 2048), 1);
|
||||
memcpy((uint8*)data + (rp - position), buf + (rp & 2047), std::min<uint64>(2048 - (rp & 2047), count - (rp - position)));
|
||||
}
|
||||
|
||||
|
@ -101,22 +101,6 @@ class CDIF_Message
|
||||
std::string str_message;
|
||||
};
|
||||
|
||||
class CDIF_Queue
|
||||
{
|
||||
public:
|
||||
|
||||
CDIF_Queue();
|
||||
~CDIF_Queue();
|
||||
|
||||
bool Read(CDIF_Message *message, bool blocking = TRUE);
|
||||
|
||||
void Write(const CDIF_Message &message);
|
||||
|
||||
private:
|
||||
std::queue<CDIF_Message> ze_queue;
|
||||
MDFN_Mutex *ze_mutex;
|
||||
};
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
@ -126,57 +110,6 @@ typedef struct
|
||||
uint8 data[2352 + 96];
|
||||
} CDIF_Sector_Buffer;
|
||||
|
||||
#ifdef WANT_CDIF_MT
|
||||
|
||||
// TODO: prohibit copy constructor
|
||||
class CDIF_MT : public CDIF
|
||||
{
|
||||
public:
|
||||
|
||||
CDIF_MT(const char *device_name);
|
||||
virtual ~CDIF_MT();
|
||||
|
||||
virtual void HintReadSector(uint32 lba);
|
||||
virtual bool ReadRawSector(uint8 *buf, uint32 lba);
|
||||
|
||||
// Return true if operation succeeded or it was a NOP(either due to not being implemented, or the current status matches eject_status).
|
||||
// Returns false on failure(usually drive error of some kind; not completely fatal, can try again).
|
||||
virtual bool Eject(bool eject_status);
|
||||
|
||||
// FIXME: Semi-private:
|
||||
int ReadThreadStart(const char *device_name);
|
||||
|
||||
private:
|
||||
|
||||
MDFN_Thread *CDReadThread;
|
||||
|
||||
// Queue for messages to the read thread.
|
||||
CDIF_Queue ReadThreadQueue;
|
||||
|
||||
// Queue for messages to the emu thread.
|
||||
CDIF_Queue EmuThreadQueue;
|
||||
|
||||
|
||||
enum { SBSize = 256 };
|
||||
CDIF_Sector_Buffer SectorBuffers[SBSize];
|
||||
|
||||
uint32 SBWritePos;
|
||||
|
||||
MDFN_Mutex *SBMutex;
|
||||
|
||||
|
||||
//
|
||||
// Read-thread-only:
|
||||
//
|
||||
void RT_EjectDisc(bool eject_status, bool skip_actual_eject = false);
|
||||
|
||||
uint32 ra_lba;
|
||||
int ra_count;
|
||||
uint32 last_read_lba;
|
||||
};
|
||||
#endif //WANT_CDIF_MT
|
||||
|
||||
|
||||
// TODO: prohibit copy constructor
|
||||
class CDIF_ST : public CDIF
|
||||
{
|
||||
|
@ -24,21 +24,6 @@ void MDFN_DebugPrintReal(const char *file, const int line, const char *format, .
|
||||
#define gettext_noop(format, ...) (format)
|
||||
#define MDFN_DebugPrint(format, ...) MDFN_DebugPrintReal(__FILE__, __LINE__, format, ## __VA_ARGS__)
|
||||
|
||||
|
||||
class MDFNException
|
||||
{
|
||||
public:
|
||||
|
||||
MDFNException();
|
||||
~MDFNException();
|
||||
|
||||
char TheMessage[1024];
|
||||
|
||||
void AddPre(const char *format, ...);
|
||||
void AddPost(const char *format, ...);
|
||||
};
|
||||
|
||||
|
||||
void MDFN_LoadGameCheats(FILE *override);
|
||||
void MDFN_FlushGameCheats(int nosave);
|
||||
void MDFN_DoSimpleCommand(int cmd);
|
||||
|
Loading…
Reference in New Issue
Block a user