Merge pull request #665 from QuarkTheAwesome/libretro-merge-be
authorLibretroAdmin <105389611+LibretroAdmin@users.noreply.github.com>
Sat, 11 Jun 2022 01:08:36 +0000 (02:08 +0100)
committerGitHub <noreply@github.com>
Sat, 11 Jun 2022 01:08:36 +0000 (02:08 +0100)
Fix PCSX core and dfxvideo for big-endian systems

1  2 
libpcsxcore/cdriso.c
libpcsxcore/misc.c
libpcsxcore/r3000a.h

diff --combined libpcsxcore/cdriso.c
@@@ -685,6 -685,7 +685,7 @@@ static int handlepbp(const char *isofil
        off_t psisoimg_offs, cdimg_base;
        unsigned int t, cd_length;
        unsigned int offsettab[8];
+       unsigned int psar_offs, index_entry_size, index_entry_offset;
        const char *ext = NULL;
        int i, ret;
  
                goto fail_io;
        }
  
-       ret = fseeko(cdHandle, pbp_hdr.psar_offs, SEEK_SET);
+       psar_offs = SWAP32(pbp_hdr.psar_offs);
+       ret = fseeko(cdHandle, psar_offs, SEEK_SET);
        if (ret != 0) {
-               SysPrintf("failed to seek to %x\n", pbp_hdr.psar_offs);
+               SysPrintf("failed to seek to %x\n", psar_offs);
                goto fail_io;
        }
  
-       psisoimg_offs = pbp_hdr.psar_offs;
+       psisoimg_offs = psar_offs;
        if (fread(psar_sig, 1, sizeof(psar_sig), cdHandle) != sizeof(psar_sig))
                goto fail_io;
        psar_sig[10] = 0;
        if (strcmp(psar_sig, "PSTITLEIMG") == 0) {
                // multidisk image?
-               ret = fseeko(cdHandle, pbp_hdr.psar_offs + 0x200, SEEK_SET);
+               ret = fseeko(cdHandle, psar_offs + 0x200, SEEK_SET);
                if (ret != 0) {
-                       SysPrintf("failed to seek to %x\n", pbp_hdr.psar_offs + 0x200);
+                       SysPrintf("failed to seek to %x\n", psar_offs + 0x200);
                        goto fail_io;
                }
  
                if (cdrIsoMultidiskSelect >= cdrIsoMultidiskCount)
                        cdrIsoMultidiskSelect = 0;
  
-               psisoimg_offs += offsettab[cdrIsoMultidiskSelect];
+               psisoimg_offs += SWAP32(offsettab[cdrIsoMultidiskSelect]);
  
                ret = fseeko(cdHandle, psisoimg_offs, SEEK_SET);
                if (ret != 0) {
                        goto fail_index;
                }
  
-               if (index_entry.size == 0)
+               index_entry_size = SWAP32(index_entry.size);
+               index_entry_offset = SWAP32(index_entry.offset);
+               if (index_entry_size == 0)
                        break;
  
-               compr_img->index_table[i] = cdimg_base + index_entry.offset;
+               compr_img->index_table[i] = cdimg_base + index_entry_offset;
        }
-       compr_img->index_table[i] = cdimg_base + index_entry.offset + index_entry.size;
+       compr_img->index_table[i] = cdimg_base + index_entry_offset + index_entry_size;
  
        return 0;
  
@@@ -936,9 -942,6 +942,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;
     cddaBigEndian = TRUE;
  
        numtracks = 0;
 -      int frame_offset = 0;
 -      int file_offset = 0;
        memset(ti, 0, sizeof(ti));
  
     while (1)
                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++;
        }
  
diff --combined libpcsxcore/misc.c
@@@ -28,7 -28,6 +28,7 @@@
  #include "gpu.h"
  #include "ppf.h"
  #include "database.h"
 +#include "lightrec/plugin.h"
  #include <zlib.h>
  
  char CdromId[10] = "";
@@@ -59,14 -58,7 +59,7 @@@ struct iso_directory_record 
  void mmssdd( char *b, char *p )
  {
        int m, s, d;
- #if defined(__arm__)
-       unsigned char *u = (void *)b;
-       int block = (u[3] << 24) | (u[2] << 16) | (u[1] << 8) | u[0];
- #elif defined(__BIGENDIAN__)
-       int block = (b[0] & 0xff) | ((b[1] & 0xff) << 8) | ((b[2] & 0xff) << 16) | (b[3] << 24);
- #else
-       int block = *((int*)b);
- #endif
+       int block = SWAP32(*((uint32_t*) b));
  
        block += 150;
        m = block / 4500;                       // minutes
@@@ -604,6 -596,15 +597,6 @@@ static const char PcsxHeader[32] = "STv
  // If you make changes to the savestate version, please increment the value below.
  static const u32 SaveVersion = 0x8b410006;
  
 -static int drc_is_lightrec(void)
 -{
 -#if defined(LIGHTREC)
 -      return 1;
 -#else
 -      return 0;
 -#endif
 -}
 -
  int SaveState(const char *file) {
        void *f;
        GPUFreeze_t *gpufP;
@@@ -716,7 -717,7 +709,7 @@@ int LoadState(const char *file) 
        GPU_freeze(0, gpufP);
        free(gpufP);
        if (HW_GPU_STATUS == 0)
-               HW_GPU_STATUS = GPU_readStatus();
+               HW_GPU_STATUS = SWAP32(GPU_readStatus());
  
        // spu
        SaveFuncs.read(f, &Size, 4);
diff --combined libpcsxcore/r3000a.h
@@@ -51,7 -51,7 +51,7 @@@ extern R3000Acpu psxInt
  extern R3000Acpu psxRec;
  
  typedef union {
- #if defined(__BIGENDIAN__)
+ #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
        struct { u8 h3, h2, h, l; } b;
        struct { s8 h3, h2, h, l; } sb;
        struct { u16 h, l; } w;
@@@ -204,6 -204,9 +204,6 @@@ extern psxRegisters psxRegs
  extern u32 event_cycles[PSXINT_COUNT];
  extern u32 next_interupt;
  
 -void lightrec_plugin_prepare_save_state(void);
 -void lightrec_plugin_prepare_load_state(void);
 -
  void new_dyna_before_save(void);
  void new_dyna_after_save(void);
  void new_dyna_freeze(void *f, int mode);
        } \
  }
  
- #if defined(__BIGENDIAN__)
+ #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  
  #define _i32(x) *(s32 *)&x
  #define _u32(x) x