dfsound: Fix issues on big-endian systems
authorPaul Cercueil <paul@crapouillou.net>
Wed, 8 Mar 2023 11:30:00 +0000 (11:30 +0000)
committerPaul Cercueil <paul@crapouillou.net>
Fri, 5 May 2023 16:23:33 +0000 (18:23 +0200)
Without this fix, the BIOS' music is missing on big-endian systems. The
XA and reverb code are also fixed, which fixes games like Vib-Ribbon.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
plugins/dfsound/registers.c
plugins/dfsound/reverb.c
plugins/dfsound/spu.h
plugins/dfsound/xa.c

index e00939e..badd0af 100644 (file)
@@ -22,6 +22,7 @@
 #include "externals.h"\r
 #include "registers.h"\r
 #include "spu_config.h"\r
+#include "spu.h"\r
 \r
 static void SoundOn(int start,int end,unsigned short val);\r
 static void SoundOff(int start,int end,unsigned short val);\r
@@ -127,7 +128,7 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val,
       break;\r
     //-------------------------------------------------//\r
     case H_SPUdata:\r
-      *(unsigned short *)(spu.spuMemC + spu.spuAddr) = val;\r
+      *(unsigned short *)(spu.spuMemC + spu.spuAddr) = HTOLE16(val);\r
       spu.spuAddr += 2;\r
       spu.spuAddr &= 0x7fffe;\r
       break;\r
@@ -334,7 +335,7 @@ unsigned short CALLBACK SPUreadRegister(unsigned long reg)
 \r
     case H_SPUdata:\r
      {\r
-      unsigned short s = *(unsigned short *)(spu.spuMemC + spu.spuAddr);\r
+      unsigned short s = LE16TOH(*(unsigned short *)(spu.spuMemC + spu.spuAddr));\r
       spu.spuAddr += 2;\r
       spu.spuAddr &= 0x7fffe;\r
       return s;\r
index ec570fb..de9b804 100644 (file)
@@ -20,6 +20,7 @@
  ***************************************************************************/\r
 \r
 #include "stdafx.h"\r
+#include "spu.h"\r
 \r
 #define _IN_REVERB\r
 \r
@@ -50,16 +51,16 @@ INLINE int rvb2ram_offs(int curr, int space, int iOff)
 \r
 // get_buffer content helper: takes care about wraps\r
 #define g_buffer(var) \\r
- ((int)(signed short)spu.spuMem[rvb2ram_offs(curr_addr, space, rvb->var)])\r
+ ((int)(signed short)LE16TOH(spu.spuMem[rvb2ram_offs(curr_addr, space, rvb->var)]))\r
 \r
 // saturate iVal and store it as var\r
 #define s_buffer(var, iVal) \\r
  ssat32_to_16(iVal); \\r
- spu.spuMem[rvb2ram_offs(curr_addr, space, rvb->var)] = iVal\r
+ spu.spuMem[rvb2ram_offs(curr_addr, space, rvb->var)] = HTOLE16(iVal)\r
 \r
 #define s_buffer1(var, iVal) \\r
  ssat32_to_16(iVal); \\r
- spu.spuMem[rvb2ram_offs(curr_addr, space, rvb->var + 1)] = iVal\r
+ spu.spuMem[rvb2ram_offs(curr_addr, space, rvb->var + 1)] = HTOLE16(iVal)\r
 \r
 ////////////////////////////////////////////////////////////////////////\r
 \r
index 0cef652..334c680 100644 (file)
 #ifndef __P_SPU_H__\r
 #define __P_SPU_H__\r
 \r
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__\r
+#define HTOLE16(x) __builtin_bswap16(x)\r
+#define LE16TOH(x) __builtin_bswap16(x)\r
+#else\r
+#define HTOLE16(x) (x)\r
+#define LE16TOH(x) (x)\r
+#endif\r
+\r
 void ClearWorkingState(void);\r
 void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap, unsigned int cycle, int is_start);\r
 int  CALLBACK SPUplayCDDAchannel(short *pcm, int bytes, unsigned int cycle, int is_start);\r
index c7a84fd..397ed59 100644 (file)
@@ -16,6 +16,7 @@
  ***************************************************************************/
 
 #include "stdafx.h"
+#include "spu.h"
 #define _IN_XA
 #include <stdint.h>
 
@@ -60,8 +61,8 @@ INLINE void MixXA(int *SSumLR, int ns_to, int decode_pos)
     SSumLR[ns++] += l;
     SSumLR[ns++] += r;
 
-    spu.spuMem[cursor] = v;
-    spu.spuMem[cursor + 0x400/2] = v >> 16;
+    spu.spuMem[cursor] = HTOLE16(v);
+    spu.spuMem[cursor + 0x400/2] = HTOLE16(v >> 16);
     cursor = (cursor + 1) & 0x1ff;
    }
   spu.XALastVal = v;
@@ -80,8 +81,8 @@ INLINE void MixXA(int *SSumLR, int ns_to, int decode_pos)
     SSumLR[ns++] += l;
     SSumLR[ns++] += r;
 
-    spu.spuMem[cursor] = v;
-    spu.spuMem[cursor + 0x400/2] = v >> 16;
+    spu.spuMem[cursor] = HTOLE16(v);
+    spu.spuMem[cursor + 0x400/2] = HTOLE16(v >> 16);
     cursor = (cursor + 1) & 0x1ff;
    }
   spu.XALastVal = v;