Make sure hardware registers are manipulated as little-endian
authorPaul Cercueil <paul@crapouillou.net>
Fri, 13 Sep 2019 21:01:57 +0000 (23:01 +0200)
committerAsh Logan <ash@heyquark.com>
Mon, 6 Jun 2022 13:04:52 +0000 (23:04 +1000)
The hardware registers should be represented in little-endian format.
Therefore, on big-endian systems the values need to be byte-swapped.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
libpcsxcore/gpu.h
libpcsxcore/misc.c
libpcsxcore/psxcounters.c
libpcsxcore/psxdma.c
libpcsxcore/psxhw.c

index 98a22a0..c924b67 100644 (file)
@@ -38,8 +38,8 @@
 #define PSXGPU_TIMING_BITS (PSXGPU_LCF | PSXGPU_nBUSY)
 
 #define gpuSyncPluginSR() { \
-       HW_GPU_STATUS &= PSXGPU_TIMING_BITS; \
-       HW_GPU_STATUS |= GPU_readStatus() & ~PSXGPU_TIMING_BITS; \
+       HW_GPU_STATUS &= SWAP32(PSXGPU_TIMING_BITS); \
+       HW_GPU_STATUS |= SWAP32(GPU_readStatus() & ~PSXGPU_TIMING_BITS); \
 }
 
 #endif /* __GPU_H__ */
index f820868..85402fc 100644 (file)
@@ -724,7 +724,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);
index 1788db6..80fd8a4 100644 (file)
@@ -327,7 +327,7 @@ void psxRcntUpdate()
         // VSync irq.
         if( hSyncCount == VBlankStart )
         {
-            HW_GPU_STATUS &= ~PSXGPU_LCF;
+            HW_GPU_STATUS &= SWAP32(~PSXGPU_LCF);
             GPU_vBlank( 1, 0 );
             setIrq( 0x01 );
 
@@ -347,9 +347,9 @@ void psxRcntUpdate()
             frame_counter++;
 
             gpuSyncPluginSR();
-            if( (HW_GPU_STATUS & PSXGPU_ILACE_BITS) == PSXGPU_ILACE_BITS )
-                HW_GPU_STATUS |= frame_counter << 31;
-            GPU_vBlank( 0, HW_GPU_STATUS >> 31 );
+            if ((HW_GPU_STATUS & SWAP32(PSXGPU_ILACE_BITS)) == SWAP32(PSXGPU_ILACE_BITS))
+                HW_GPU_STATUS |= SWAP32(frame_counter << 31);
+            GPU_vBlank(0, SWAP32(HW_GPU_STATUS) >> 31);
         }
 
         // Schedule next call, in hsyncs
index 0480ce6..c888ecd 100644 (file)
@@ -184,7 +184,7 @@ void psxDma2(u32 madr, u32 bcr, u32 chcr) { // GPU
                        size = GPU_dmaChain((u32 *)psxM, madr & 0x1fffff);
                        if ((int)size <= 0)
                                size = gpuDmaChainSize(madr);
-                       HW_GPU_STATUS &= ~PSXGPU_nBUSY;
+                       HW_GPU_STATUS &= SWAP32(~PSXGPU_nBUSY);
 
                        // we don't emulate progress, just busy flag and end irq,
                        // so pretend we're already at the last block
@@ -216,7 +216,7 @@ void gpuInterrupt() {
                HW_DMA2_CHCR &= SWAP32(~0x01000000);
                DMA_INTERRUPT(2);
        }
-       HW_GPU_STATUS |= PSXGPU_nBUSY; // GPU no longer busy
+       HW_GPU_STATUS |= SWAP32(PSXGPU_nBUSY); // GPU no longer busy
 }
 
 void psxDma6(u32 madr, u32 bcr, u32 chcr) {
@@ -244,7 +244,7 @@ void psxDma6(u32 madr, u32 bcr, u32 chcr) {
                        *mem-- = SWAP32((madr - 4) & 0xffffff);
                        madr -= 4;
                }
-               mem++; *mem = 0xffffff;
+               *++mem = SWAP32(0xffffff);
 
                //GPUOTCDMA_INT(size);
                // halted
index dbcb989..7b2401b 100644 (file)
@@ -38,7 +38,7 @@ void psxHwReset() {
        mdecInit(); // initialize mdec decoder
        cdrReset();
        psxRcntInit();
-       HW_GPU_STATUS = 0x14802000;
+       HW_GPU_STATUS = SWAP32(0x14802000);
 }
 
 u8 psxHwRead8(u32 add) {
@@ -248,8 +248,8 @@ u32 psxHwRead32(u32 add) {
                        return hard;
                case 0x1f801814:
                        gpuSyncPluginSR();
-                       hard = HW_GPU_STATUS;
-                       if (hSyncCount < 240 && (HW_GPU_STATUS & PSXGPU_ILACE_BITS) != PSXGPU_ILACE_BITS)
+                       hard = SWAP32(HW_GPU_STATUS);
+                       if (hSyncCount < 240 && (hard & PSXGPU_ILACE_BITS) != PSXGPU_ILACE_BITS)
                                hard |= PSXGPU_LCF & (psxRegs.cycle << 20);
 #ifdef PSXHW_LOG
                        PSXHW_LOG("GPU STATUS 32bit read %x\n", hard);
@@ -446,7 +446,7 @@ void psxHwWrite16(u32 add, u16 value) {
                        PSXHW_LOG("IMASK 16bit write %x\n", value);
 #endif
                        psxHu16ref(0x1074) = SWAPu16(value);
-                       if (psxHu16ref(0x1070) & value)
+                       if (psxHu16ref(0x1070) & SWAPu16(value))
                                new_dyna_set_event(PSXINT_NEWDRC_CHECK, 1);
                        return;
 
@@ -560,7 +560,7 @@ void psxHwWrite32(u32 add, u32 value) {
                        PSXHW_LOG("IMASK 32bit write %x\n", value);
 #endif
                        psxHu32ref(0x1074) = SWAPu32(value);
-                       if (psxHu32ref(0x1070) & value)
+                       if (psxHu32ref(0x1070) & SWAPu32(value))
                                new_dyna_set_event(PSXINT_NEWDRC_CHECK, 1);
                        return;