cdrom: report read errors correctly
[pcsx_rearmed.git] / libpcsxcore / cdriso.c
index 974bafe..e46901c 100644 (file)
@@ -70,16 +70,13 @@ static unsigned char subbuffer[SUB_FRAMESIZE];
 
 static boolean playing = FALSE;
 static boolean cddaBigEndian = FALSE;
-// cdda sectors in toc, byte offset in file
-static unsigned int cdda_cur_sector;
-static unsigned int cdda_file_offset;
 /* Frame offset into CD image where pregap data would be found if it was there.
  * If a game seeks there we must *not* return subchannel data since it's
  * not in the CD image, so that cdrom code can fake subchannel data instead.
  * XXX: there could be multiple pregaps but PSX dumps only have one? */
 static unsigned int pregapOffset;
 
-#define cddaCurPos cdda_cur_sector
+static unsigned int cddaCurPos;
 
 // compressed image stuff
 static struct {
@@ -942,6 +939,9 @@ fail_io:
 
 #ifdef HAVE_CHD
 static int handlechd(const char *isofile) {
+       int frame_offset = 150;
+       int file_offset = 0;
+
        chd_img = calloc(1, sizeof(*chd_img));
        if (chd_img == NULL)
                goto fail_io;
@@ -964,8 +964,6 @@ static int handlechd(const char *isofile) {
    cddaBigEndian = TRUE;
 
        numtracks = 0;
-       int frame_offset = 0;
-       int file_offset = 0;
        memset(ti, 0, sizeof(ti));
 
    while (1)
@@ -993,25 +991,23 @@ static int handlechd(const char *isofile) {
                SysPrintf("chd: %s\n", meta);
 
                if (md.track == 1) {
-                       md.pregap = 150;
                        if (!strncmp(md.subtype, "RW", 2)) {
                                subChanMixed = TRUE;
                                if (!strcmp(md.subtype, "RW_RAW"))
                                        subChanRaw = TRUE;
                        }
                }
-               else
-                       sec2msf(msf2sec(ti[md.track-1].length) + md.pregap, ti[md.track-1].length);
 
                ti[md.track].type = !strncmp(md.type, "AUDIO", 5) ? CDDA : DATA;
 
                sec2msf(frame_offset + md.pregap, ti[md.track].start);
                sec2msf(md.frames, ti[md.track].length);
 
-               ti[md.track].start_offset = file_offset;
+               ti[md.track].start_offset = file_offset + md.pregap;
 
-               frame_offset += md.pregap + md.frames + md.postgap;
-               file_offset += md.frames + md.postgap;
+               // XXX: what about postgap?
+               frame_offset += md.frames;
+               file_offset += md.frames;
                numtracks++;
        }
 
@@ -1424,6 +1420,7 @@ static int cdread_chd(FILE *f, unsigned int base, void *dest, int sector)
        return CD_FRAMESIZE_RAW;
 }
 #endif
+
 static int cdread_2048(FILE *f, unsigned int base, void *dest, int sector)
 {
        int ret;
@@ -1436,7 +1433,7 @@ static int cdread_2048(FILE *f, unsigned int base, void *dest, int sector)
        sec2msf(sector + 2 * 75, (char *)&cdbuffer[12]);
        cdbuffer[12 + 3] = 1;
 
-       return ret;
+       return 12*2 + ret;
 }
 
 #ifndef _WIN32
@@ -1653,8 +1650,6 @@ static long CALLBACK ISOopen(void) {
        if (numtracks > 1 && ti[1].handle == NULL) {
                ti[1].handle = fopen(bin_filename, "rb");
        }
-       cdda_cur_sector = 0;
-       cdda_file_offset = 0;
 
   if (Config.AsyncCD) {
     readThreadStart();
@@ -1806,14 +1801,14 @@ static boolean CALLBACK ISOreadTrack(unsigned char *time) {
        }
 
        ret = cdimg_read_func(cdHandle, 0, cdbuffer, sector);
-       if (ret < 0)
+       if (ret < 12*2 + 2048)
                return 0;
 
        if (subHandle != NULL) {
                fseek(subHandle, sector * SUB_FRAMESIZE, SEEK_SET);
                if (fread(subbuffer, 1, SUB_FRAMESIZE, subHandle) != SUB_FRAMESIZE)
                        /* Faulty subchannel data shouldn't cause a read failure */
-                       return 0;
+                       return 1;
 
                if (subChanRaw) DecodeRawSubData();
        }
@@ -1824,7 +1819,7 @@ static boolean CALLBACK ISOreadTrack(unsigned char *time) {
 // plays cdda audio
 // sector: byte 0 - minute; byte 1 - second; byte 2 - frame
 // does NOT uses bcd format
-static long CALLBACK ISOplay(void) {
+static long CALLBACK ISOplay(unsigned char *time) {
        playing = TRUE;
        return 0;
 }