switch over to libretro input code
[pcsx_rearmed.git] / libpcsxcore / cdriso.c
index a755a23..081a8f2 100644 (file)
@@ -30,8 +30,7 @@
 #include <process.h>
 #include <windows.h>
 #define strcasecmp _stricmp
-#define usleep(x) Sleep((x) / 1000)
-#else
+#elif P_HAVE_PTHREAD
 #include <pthread.h>
 #include <sys/time.h>
 #include <unistd.h>
@@ -83,11 +82,12 @@ static struct {
 
 #ifdef HAVE_CHD
 static struct {
-       unsigned char (*buffer)[CD_FRAMESIZE_RAW + SUB_FRAMESIZE];
+       unsigned char *buffer;
        chd_file* chd;
        const chd_header* header;
        unsigned int sectors_per_hunk;
-       unsigned int current_hunk;
+       unsigned int current_hunk[2];
+       unsigned int current_buffer;
        unsigned int sector_in_hunk;
 } *chd_img;
 #endif
@@ -109,7 +109,7 @@ struct trackinfo {
        char start[3];          // MSF-format
        char length[3];         // MSF-format
        FILE *handle;           // for multi-track images CDDA
-       unsigned int start_offset; // byte offset from start of above file
+       unsigned int start_offset; // byte offset from start of above file (chd: sector offset)
 };
 
 #define MAXTRACKS 100 /* How many tracks can a CD hold? */
@@ -952,12 +952,13 @@ static int handlechd(const char *isofile) {
 
        chd_img->header = chd_get_header(chd_img->chd);
 
-       chd_img->buffer = malloc(chd_img->header->hunkbytes);
+       chd_img->buffer = malloc(chd_img->header->hunkbytes * 2);
        if (chd_img->buffer == NULL)
                goto fail_io;
 
        chd_img->sectors_per_hunk = chd_img->header->hunkbytes / (CD_FRAMESIZE_RAW + SUB_FRAMESIZE);
-       chd_img->current_hunk = (unsigned int)-1;
+       chd_img->current_hunk[0] = (unsigned int)-1;
+       chd_img->current_hunk[1] = (unsigned int)-1;
 
        cddaBigEndian = TRUE;
 
@@ -1216,6 +1217,13 @@ finish:
 }
 
 #ifdef HAVE_CHD
+static unsigned char *chd_get_sector(unsigned int current_buffer, unsigned int sector_in_hunk)
+{
+       return chd_img->buffer
+               + current_buffer * chd_img->header->hunkbytes
+               + sector_in_hunk * (CD_FRAMESIZE_RAW + SUB_FRAMESIZE);
+}
+
 static int cdread_chd(FILE *f, unsigned int base, void *dest, int sector)
 {
        int hunk;
@@ -1225,35 +1233,48 @@ static int cdread_chd(FILE *f, unsigned int base, void *dest, int sector)
        hunk = sector / chd_img->sectors_per_hunk;
        chd_img->sector_in_hunk = sector % chd_img->sectors_per_hunk;
 
-       if (hunk != chd_img->current_hunk)
+       if (hunk == chd_img->current_hunk[0])
+               chd_img->current_buffer = 0;
+       else if (hunk == chd_img->current_hunk[1])
+               chd_img->current_buffer = 1;
+       else
        {
-               chd_read(chd_img->chd, hunk, chd_img->buffer);
-               chd_img->current_hunk = hunk;
+               chd_read(chd_img->chd, hunk, chd_img->buffer +
+                       chd_img->current_buffer * chd_img->header->hunkbytes);
+               chd_img->current_hunk[chd_img->current_buffer] = hunk;
        }
 
        if (dest != cdbuffer) // copy avoid HACK
-               memcpy(dest, chd_img->buffer[chd_img->sector_in_hunk],
+               memcpy(dest, chd_get_sector(chd_img->current_buffer, chd_img->sector_in_hunk),
                        CD_FRAMESIZE_RAW);
        return CD_FRAMESIZE_RAW;
 }
 
 static int cdread_sub_chd(FILE *f, int sector)
 {
+       unsigned int sector_in_hunk;
+       unsigned int buffer;
        int hunk;
 
        if (!subChanMixed)
                return -1;
 
        hunk = sector / chd_img->sectors_per_hunk;
-       chd_img->sector_in_hunk = sector % chd_img->sectors_per_hunk;
+       sector_in_hunk = sector % chd_img->sectors_per_hunk;
 
-       if (hunk != chd_img->current_hunk)
+       if (hunk == chd_img->current_hunk[0])
+               buffer = 0;
+       else if (hunk == chd_img->current_hunk[1])
+               buffer = 1;
+       else
        {
-               chd_read(chd_img->chd, hunk, chd_img->buffer);
-               chd_img->current_hunk = hunk;
+               buffer = chd_img->current_buffer ^ 1;
+               chd_read(chd_img->chd, hunk, chd_img->buffer +
+                       buffer * chd_img->header->hunkbytes);
+               chd_img->current_hunk[buffer] = hunk;
        }
 
-       memcpy(subbuffer, chd_img->buffer[chd_img->sector_in_hunk] + CD_FRAMESIZE_RAW, SUB_FRAMESIZE);
+       memcpy(subbuffer, chd_get_sector(buffer, sector_in_hunk) + CD_FRAMESIZE_RAW, SUB_FRAMESIZE);
        return SUB_FRAMESIZE;
 }
 #endif
@@ -1279,7 +1300,7 @@ static unsigned char * CALLBACK ISOgetBuffer_compr(void) {
 
 #ifdef HAVE_CHD
 static unsigned char * CALLBACK ISOgetBuffer_chd(void) {
-       return chd_img->buffer[chd_img->sector_in_hunk] + 12;
+       return chd_get_sector(chd_img->current_buffer, chd_img->sector_in_hunk) + 12;
 }
 #endif
 
@@ -1305,7 +1326,6 @@ static long CALLBACK ISOopen(void) {
        char alt_bin_filename[MAXPATHLEN];
        const char *bin_filename;
        char image_str[1024];
-       int is_chd = 0;
 
        if (cdHandle != NULL) {
                return 0; // it's already open
@@ -1360,7 +1380,6 @@ static long CALLBACK ISOopen(void) {
                CDR_getBuffer = ISOgetBuffer_chd;
                cdimg_read_func = cdread_chd;
                cdimg_read_sub_func = cdread_sub_chd;
-               is_chd = 1;
        }
 #endif
 
@@ -1401,14 +1420,11 @@ static long CALLBACK ISOopen(void) {
        }
 
        // guess whether it is mode1/2048
-       if (ftello(cdHandle) % 2048 == 0) {
+       if (cdimg_read_func == cdread_normal && ftello(cdHandle) % 2048 == 0) {
                unsigned int modeTest = 0;
                fseek(cdHandle, 0, SEEK_SET);
                if (!fread(&modeTest, sizeof(modeTest), 1, cdHandle)) {
-#ifndef NDEBUG
                        SysPrintf(_("File IO error in <%s:%s>.\n"), __FILE__, __func__);
-#endif
-                       return -1;
                }
                if (SWAP32(modeTest) != 0xffffff00) {
                        strcat(image_str, "[2048]");
@@ -1421,7 +1437,7 @@ static long CALLBACK ISOopen(void) {
 
        PrintTracks();
 
-       if (subChanMixed && !is_chd) {
+       if (subChanMixed && cdimg_read_func == cdread_normal) {
                cdimg_read_func = cdread_sub_mixed;
                cdimg_read_sub_func = cdread_sub_sub_mixed;
        }