From e362c57310c2b01b206af28210e3c422225e4f94 Mon Sep 17 00:00:00 2001
From: notaz <notasas@gmail.com>
Date: Sat, 12 May 2007 15:06:44 +0000
Subject: [PATCH] fixed mp3 buffer overlapping of GP2X sound buffer

git-svn-id: file:///home/notaz/opt/svn/PicoDrive@129 be3aeb3a-fb24-0410-a615-afba39da0efa
---
 platform/gp2x/940ctl.c          |  8 +++-----
 platform/gp2x/code940/940.c     |  2 +-
 platform/gp2x/code940/940init.s | 31 +++++++++++++++++++------------
 platform/readme.txt             | 11 +++++++++--
 4 files changed, 32 insertions(+), 20 deletions(-)

diff --git a/platform/gp2x/940ctl.c b/platform/gp2x/940ctl.c
index 6c52cca2..8b3c7fe6 100644
--- a/platform/gp2x/940ctl.c
+++ b/platform/gp2x/940ctl.c
@@ -29,7 +29,7 @@ static _940_data_t *shared_data = 0;
 _940_ctl_t *shared_ctl = 0;
 unsigned char *mp3_mem = 0;
 
-#define MP3_SIZE_MAX (0x1000000 - 4*640*480)
+#define MP3_SIZE_MAX (0x400000 + 0x800000) // 12M
 
 int crashed_940 = 0;
 
@@ -360,7 +360,7 @@ void sharedmem_init(void)
 	shared_data = (_940_data_t *) (shared_mem+0x100000);
 	/* this area must not get buffered on either side */
 	shared_ctl =  (_940_ctl_t *)  (shared_mem+0x200000);
-	mp3_mem = (unsigned char *) mmap(0, MP3_SIZE_MAX, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x3000000);
+	mp3_mem = (unsigned char *) mmap(0, MP3_SIZE_MAX, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x2400000);
 	if (mp3_mem == MAP_FAILED)
 	{
 		printf("mmap(mp3_mem) failed with %i\n", errno);
@@ -599,7 +599,6 @@ void mp3_start_play(FILE *f, int pos) // pos is 0-1023
 
 	if (loaded_mp3 != f)
 	{
-		// printf("loading mp3... "); fflush(stdout);
 		if (PicoMessage != NULL)
 		{
 			fseek(f, 0, SEEK_END);
@@ -608,8 +607,7 @@ void mp3_start_play(FILE *f, int pos) // pos is 0-1023
 		}
 		fseek(f, 0, SEEK_SET);
 		fread(mp3_mem, 1, MP3_SIZE_MAX, f);
-		// if (feof(f)) printf("done.\n");
-		// else printf("done. mp3 too large, not all data loaded.\n");
+		if (!feof(f)) printf("Warning: mp3 was too large, not all data loaded.\n");
 		shared_ctl->mp3_len = ftell(f);
 		loaded_mp3 = f;
 
diff --git a/platform/gp2x/code940/940.c b/platform/gp2x/code940/940.c
index 4c7082ac..97a3b5c9 100644
--- a/platform/gp2x/code940/940.c
+++ b/platform/gp2x/code940/940.c
@@ -5,7 +5,7 @@
 
 static _940_data_t *shared_data = (_940_data_t *)   0x00100000;
 static _940_ctl_t  *shared_ctl  = (_940_ctl_t *)    0x00200000;
-static unsigned char *mp3_data  = (unsigned char *) 0x01000000;
+static unsigned char *mp3_data  = (unsigned char *) 0x00400000;
 YM2612 *ym2612_940;
 
 // from init.s
diff --git a/platform/gp2x/code940/940init.s b/platform/gp2x/code940/940init.s
index 1a12399c..201ee865 100644
--- a/platform/gp2x/code940/940init.s
+++ b/platform/gp2x/code940/940init.s
@@ -87,19 +87,25 @@ code940:
     mcr p15, 0, r0, c6, c3, 0
     mcr p15, 0, r0, c6, c3, 1
 
-    @ set up region 4: 16M 0x01000000-0x02000000 (mp3 area)
-    mov r0, #(0x17<<1)|1
-    orr r0, r0, #0x01000000
+    @ region 4: 4K 0x00000000-0x00001000 (boot code protection region)
+    mov r0, #(0x0b<<1)|1
     mcr p15, 0, r0, c6, c4, 0
     mcr p15, 0, r0, c6, c4, 1
 
-    @ region 5: 4K 0x00000000-0x00001000 (boot code protection region)
-    mov r0, #(0x0b<<1)|1
+    @ region 5: 4M 0x00400000-0x00800000 (mp3 area part1)
+    mov r0, #(0x15<<1)|1
+    orr r0, r0, #0x00400000
     mcr p15, 0, r0, c6, c5, 0
     mcr p15, 0, r0, c6, c5, 1
 
-    @ set regions 1, 4 and 5 to be cacheable (so the first 2M and mp3 area will be cacheable)
-    mov r0, #(1<<1)|(1<<4)|(1<<5)
+    @ region 6: 8M 0x00800000-0x01000000 (mp3 area part2)
+    mov r0, #(0x16<<1)|1
+    orr r0, r0, #0x00800000
+    mcr p15, 0, r0, c6, c6, 0
+    mcr p15, 0, r0, c6, c6, 1
+
+    @ set regions 1, 4, 5 and 6 to be cacheable (so the first 2M and mp3 area will be cacheable)
+    mov r0, #(1<<1)|(1<<4)|(1<<5)|(1<<6)
     mcr p15, 0, r0, c2, c0, 0
     mcr p15, 0, r0, c2, c0, 1
 
@@ -108,12 +114,13 @@ code940:
     mcr p15, 0, r0, c3, c0, 0
 
     @ set access protection
-    @ data: [no, full, full, full, full, no access] for regions [5 4 3 2 1 0]
-    mov r0, #(0<<10)|(3<<8)|(3<<6)|(3<<4)|(3<<2)|(0)
+    @ data: [full, full, no, full, full, full, no access] for regions [6 5 4 3 2 1 0]
+    mov r0,     #       (3<<12)|(3<<10)|(0<<8)
+    orr r0, r0, #(3<<6)|(3<< 4)|(3<< 2)|(0<<0)
     mcr p15, 0, r0, c5, c0, 0
-    @ instructions: [full, no access, no, no, full, no]
-    mov r0,         #(0<< 6)|(0<<4)|(3<<2)|(0)
-    orr r0, r0,     #(3<<10)|(0<<8)
+    @ instructions: [no, no, full, no, no, full, no]
+    mov r0,     #       (0<<12)|(0<<10)|(3<<8)
+    orr r0, r0, #(0<<6)|(0<< 4)|(3<< 2)|(0<<0)
     mcr p15, 0, r0, c5, c0, 1
 
     mrc p15, 0, r0, c1, c0, 0   @ fetch current control reg
diff --git a/platform/readme.txt b/platform/readme.txt
index ffe2d942..881480c7 100644
--- a/platform/readme.txt
+++ b/platform/readme.txt
@@ -89,8 +89,8 @@ Other important stuff
   Badly encoded mp3s can cause various kind of problems, like noises, incorrect
   playback speeds, not repeating music or even prevent game from starting.
 * Use lower bitrate for better performance (96 or 128kbps CBRs recommended).
-* Due to internal implementation mp3s must not be larger that ~15MB
-  (15548416 bytes). Larger mp3s will not be fully loaded.
+* Due to internal implementation mp3s must not be larger that 12MB
+  (12582912 bytes). Larger mp3s will not be fully loaded.
 * RAM timings option is good for dualcore operation (it is disabled by
   default because it doesn't work on every GP2X, so enable it in advanced
   options).
@@ -224,6 +224,13 @@ Symbian:
 
 Changelog
 ---------
+1.321
+  * Sometimes stray sounds were played after loading a savestate. Fixed.
+  * Fixed a problem where >6MB mp3s were corrupted in memory (sound glitches in
+    Snatcher).
+  * PD no longer overwrites video player code in memory, video player now can be
+    used after PicoDrive.
+
 1.32
   + Added some new scaling options.
   + Added ability to reload CD images while game is running (needed for games
-- 
2.39.5