From: notaz <notasas@gmail.com>
Date: Wed, 24 Jan 2007 20:38:23 +0000 (+0000)
Subject: 1.10 release
X-Git-Tag: v1.85~764
X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cb0316e4c526ad4c8d50e40e53c9d39044dcaaf9;p=picodrive.git

1.10 release

git-svn-id: file:///home/notaz/opt/svn/PicoDrive@23 be3aeb3a-fb24-0410-a615-afba39da0efa
---

diff --git a/Pico/Pico.h b/Pico/Pico.h
index f1b3b091..48840511 100644
--- a/Pico/Pico.h
+++ b/Pico/Pico.h
@@ -19,6 +19,11 @@
 extern "C" {
 #endif
 
+// external
+int mp3_get_bitrate(FILE *f, int size);
+void mp3_start_play(FILE *f, int pos);
+
+
 // Pico.c
 // PicoOpt bits LSb->MSb:
 // enable_ym2612&dac, enable_sn76496, enable_z80, stereo_sound,
diff --git a/Pico/cd/cd_file.c b/Pico/cd/cd_file.c
index 4f11ba57..277f4859 100644
--- a/Pico/cd/cd_file.c
+++ b/Pico/cd/cd_file.c
@@ -46,7 +46,7 @@ void FILE_End(void)
 int Load_ISO(const char *iso_name, int is_bin)
 {
 	struct stat file_stat;
-	int i, j, num_track, Cur_LBA, index, ret;
+	int i, j, num_track, Cur_LBA, index, ret, iso_name_len;
 	_scd_track *SCD_TOC_Tracks = Pico_mcd->scd.TOC.Tracks;
 	FILE *tmp_file;
 	char tmp_name[1024], tmp_ext[10];
@@ -64,17 +64,17 @@ int Load_ISO(const char *iso_name, int is_bin)
 	ret = stat(iso_name, &file_stat);
 	if (ret != 0) return -1;
 
-	Tracks[0].Lenght = file_stat.st_size;
+	Tracks[0].Length = file_stat.st_size;
 
-	if (Tracks[0].Type == TYPE_ISO) Tracks[0].Lenght >>= 11;	// size in sectors
-	else Tracks[0].Lenght /= 2352;					// size in sectors
+	if (Tracks[0].Type == TYPE_ISO) Tracks[0].Length >>= 11;	// size in sectors
+	else Tracks[0].Length /= 2352;					// size in sectors
 
 
 	Tracks[0].F = fopen(iso_name, "rb");
 	if (Tracks[0].F == NULL)
 	{
 		Tracks[0].Type = 0;
-		Tracks[0].Lenght = 0;
+		Tracks[0].Length = 0;
 		return -1;
 	}
 
@@ -89,37 +89,56 @@ int Load_ISO(const char *iso_name, int is_bin)
 	SCD_TOC_Tracks[0].Num = 1;
 	SCD_TOC_Tracks[0].Type = 1;				// DATA
 
-	SCD_TOC_Tracks[0].MSF.M = 0;
-	SCD_TOC_Tracks[0].MSF.S = 2;
-	SCD_TOC_Tracks[0].MSF.F = 0;
+	SCD_TOC_Tracks[0].MSF.M = 0; // minutes
+	SCD_TOC_Tracks[0].MSF.S = 2; // seconds
+	SCD_TOC_Tracks[0].MSF.F = 0; // frames
 
 	cdprintf("Track 0 - %02d:%02d:%02d %s", SCD_TOC_Tracks[0].MSF.M, SCD_TOC_Tracks[0].MSF.S, SCD_TOC_Tracks[0].MSF.F,
 		SCD_TOC_Tracks[0].Type ? "DATA" : "AUDIO");
 
-	Cur_LBA = Tracks[0].Lenght;				// Size in sectors
+	Cur_LBA = Tracks[0].Length;				// Size in sectors
 
 	strcpy(tmp_name, iso_name);
+	iso_name_len = strlen(iso_name);
 
-	for(num_track = 2, i = 0; i < 100; i++)
+	for (num_track = 2, i = 0; i < 100; i++)
 	{
 		if (sizeof(exts)/sizeof(char *) != 10) { printf("eee"); exit(1); }
 
 		for(j = 0; j < sizeof(exts)/sizeof(char *); j++)
 		{
-			tmp_name[strlen(iso_name) - 4] = 0;
+			int ext_len;
 			sprintf(tmp_ext, exts[j], i);
+			ext_len = strlen(tmp_ext);
+
+			memcpy(tmp_name, iso_name, iso_name_len + 1);
+			tmp_name[iso_name_len - 4] = 0;
 			strcat(tmp_name, tmp_ext);
 
 			tmp_file = fopen(tmp_name, "rb");
+			if (!tmp_file && i > 1 && iso_name_len > ext_len) {
+				tmp_name[iso_name_len - ext_len] = 0;
+				strcat(tmp_name, tmp_ext);
+				tmp_file = fopen(tmp_name, "rb");
+			}
 
 			if (tmp_file)
 			{
-				float fs;
+				// float fs;
+				int fs;
 				index = num_track - Pico_mcd->scd.TOC.First_Track;
 
-				stat(tmp_name, &file_stat);
+				ret = stat(tmp_name, &file_stat);
+				fs = file_stat.st_size;				// used to calculate lenght
 
-				fs = (float) file_stat.st_size;				// used to calculate lenght
+				Tracks[index].KBtps = (short) mp3_get_bitrate(tmp_file, fs);
+				Tracks[index].KBtps >>= 3;
+				if (ret != 0 || Tracks[index].KBtps <= 0)
+				{
+					cdprintf("Error track %i: stat %i, rate %i", index, ret, Tracks[index].KBtps);
+					fclose(tmp_file);
+					continue;
+				}
 
 				Tracks[index].F = tmp_file;
 
@@ -128,16 +147,16 @@ int Load_ISO(const char *iso_name, int is_bin)
 
 				LBA_to_MSF(Cur_LBA, &(SCD_TOC_Tracks[index].MSF));
 
-				cdprintf("Track %i - %02d:%02d:%02d %s", index, SCD_TOC_Tracks[index].MSF.M,
-					SCD_TOC_Tracks[index].MSF.S, SCD_TOC_Tracks[index].MSF.F,
-					SCD_TOC_Tracks[index].Type ? "DATA" : "AUDIO");
-
 				// MP3 File
 				Tracks[index].Type = TYPE_MP3;
-				fs /= (128>>3); // (float) (MP3_Get_Bitrate(Tracks[num_track - 1].F) >> 3);
 				fs *= 75;
-				Tracks[index].Lenght = (int) fs;
-				Cur_LBA += Tracks[index].Lenght;
+				fs /= Tracks[index].KBtps * 1000;
+				Tracks[index].Length = fs;
+				Cur_LBA += Tracks[index].Length;
+
+				cdprintf("Track %i: %s - %02d:%02d:%02d len=%i %s", index, tmp_name, SCD_TOC_Tracks[index].MSF.M,
+					SCD_TOC_Tracks[index].MSF.S, SCD_TOC_Tracks[index].MSF.F, fs,
+					SCD_TOC_Tracks[index].Type ? "DATA" : "AUDIO");
 
 				num_track++;
 				break;
@@ -170,7 +189,7 @@ void Unload_ISO(void)
 	{
 		if (Tracks[i].F) fclose(Tracks[i].F);
 		Tracks[i].F = NULL;
-		Tracks[i].Lenght = 0;
+		Tracks[i].Length = 0;
 		Tracks[i].Type = 0;
 	}
 }
@@ -186,7 +205,7 @@ int FILE_Read_One_LBA_CDC(void)
 		if (Tracks[0].F == NULL) return -1;
 
 		if (Pico_mcd->scd.Cur_LBA < 0) where_read = 0;
-		else if (Pico_mcd->scd.Cur_LBA >= Tracks[0].Lenght) where_read = Tracks[0].Lenght - 1;
+		else if (Pico_mcd->scd.Cur_LBA >= Tracks[0].Length) where_read = Tracks[0].Length - 1;
 		else where_read = Pico_mcd->scd.Cur_LBA;
 
 		if (Tracks[0].Type == TYPE_ISO) where_read <<= 11;
@@ -312,10 +331,13 @@ int FILE_Play_CD_LBA(void)
 
 	if (Tracks[index].Type == TYPE_MP3)
 	{
+		int pos1000 = 0;
 		int Track_LBA_Pos = Pico_mcd->scd.Cur_LBA - Track_to_LBA(Pico_mcd->scd.Cur_Track);
 		if (Track_LBA_Pos < 0) Track_LBA_Pos = 0;
+		if (Track_LBA_Pos)
+			pos1000 = Track_LBA_Pos * 1024 / Tracks[index].Length;
 
-		// MP3_Play(index, Track_LBA_Pos); // TODO
+		mp3_start_play(Tracks[index].F, pos1000);
 	}
 	else
 	{
diff --git a/Pico/cd/cd_file.h b/Pico/cd/cd_file.h
index 4bf9d19e..092e337c 100644
--- a/Pico/cd/cd_file.h
+++ b/Pico/cd/cd_file.h
@@ -15,8 +15,9 @@ extern "C" {
 
 struct _file_track {
 	FILE *F;
-	int Lenght;
-	int Type;
+	int Length;
+	short Type; // can be char
+	short KBtps; // bytes per sec for mp3s (bitrate / 8)
 };
 
 extern struct _file_track Tracks[100];
diff --git a/platform/gp2x/940ctl_ym2612.c b/platform/gp2x/940ctl_ym2612.c
index 35bf4a7c..f1949e68 100644
--- a/platform/gp2x/940ctl_ym2612.c
+++ b/platform/gp2x/940ctl_ym2612.c
@@ -12,6 +12,7 @@
 #include "emu.h"
 #include "menu.h"
 #include "asmutils.h"
+#include "../../Pico/PicoInt.h"
 
 /* we will need some gp2x internals here */
 extern volatile unsigned short *gp2x_memregs; /* from minimal library rlyeh */
@@ -20,6 +21,9 @@ extern volatile unsigned long  *gp2x_memregl;
 static unsigned char *shared_mem = 0;
 static _940_data_t *shared_data = 0;
 static _940_ctl_t *shared_ctl = 0;
+static unsigned char *mp3_mem = 0;
+
+#define MP3_SIZE_MAX (0x1000000 - 4*640*480)
 
 int crashed_940 = 0;
 
@@ -321,6 +325,12 @@ void YM2612Init_940(int baseclock, int rate)
 		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);
+		if (mp3_mem == MAP_FAILED)
+		{
+			printf("mmap(mp3_mem) failed with %i\n", errno);
+			exit(1);
+		}
 		crashed_940 = 1;
 	}
 
@@ -401,9 +411,70 @@ void YM2612ResetChip_940(void)
 }
 
 
+static void mix_samples(short *dest_buf, int *ym_buf, short *mp3_buf, int len, int stereo)
+{
+	if (mp3_buf)
+	{
+		if (stereo)
+		{
+			for (; len > 0; len--)
+			{
+				int l, r, lm, rm;
+				l = r = *dest_buf;
+				l += *ym_buf++;  r += *ym_buf++;
+				lm = *mp3_buf++; rm = *mp3_buf++;
+				l += lm - lm/2; r += rm - rm/2;
+				Limit( l, MAXOUT, MINOUT );
+				Limit( r, MAXOUT, MINOUT );
+				*dest_buf++ = l; *dest_buf++ = r;
+			}
+		} else {
+			for (; len > 0; len--)
+			{
+				// TODO: normalize
+				int l = *ym_buf++;
+				l += *dest_buf;
+				l += *mp3_buf++;
+				Limit( l, MAXOUT, MINOUT );
+				*dest_buf++ = l;
+			}
+		}
+	}
+	else
+	{
+		if (stereo)
+		{
+			for (; len > 0; len--)
+			{
+				int l, r;
+				l = r = *dest_buf;
+				l += *ym_buf++, r += *ym_buf++;
+				Limit( l, MAXOUT, MINOUT );
+				Limit( r, MAXOUT, MINOUT );
+				*dest_buf++ = l; *dest_buf++ = r;
+			}
+		} else {
+			for (; len > 0; len--)
+			{
+				int l = *ym_buf++;
+				l += *dest_buf;
+				Limit( l, MAXOUT, MINOUT );
+				*dest_buf++ = l;
+			}
+		}
+	}
+}
+
+
+// here we assume that length is different between games, but constant in one game
+
+static FILE *loaded_mp3 = 0;
+
 void YM2612UpdateOne_940(short *buffer, int length, int stereo)
 {
-	int i, *mix_buffer = shared_data->mix_buffer;
+	int cdda_on, *ym_buffer = shared_data->mix_buffer, mp3_job = 0;
+	static int mp3_samples_ready = 0, mp3_buffer_offs = 0;
+	static int mp3_play_bufsel = 1;
 
 	//printf("YM2612UpdateOne_940()\n");
 	if (shared_ctl->busy) wait_busy_940();
@@ -413,24 +484,29 @@ void YM2612UpdateOne_940(short *buffer, int length, int stereo)
 	//	printf("%i ", shared_ctl->vstarts[i]);
 	//printf(")\n");
 
+	// emulatind MCD, cdda enabled in config, not data track, CDC is reading, playback was started, track not ended
+	cdda_on = (PicoMCD & 1) && (currentConfig.EmuOpt&0x800) && !(Pico_mcd->s68k_regs[0x36] & 1) &&
+			(Pico_mcd->scd.Status_CDC & 1) && loaded_mp3 && shared_ctl->mp3_offs < shared_ctl->mp3_len;
+
 	/* mix data from previous go */
-	if (stereo) {
-		int *mb = mix_buffer;
-		for (i = length; i > 0; i--) {
-			int l, r;
-			l = r = *buffer;
-			l += *mb++, r += *mb++;
-			Limit( l, MAXOUT, MINOUT );
-			Limit( r, MAXOUT, MINOUT );
-			*buffer++ = l; *buffer++ = r;
+	if (cdda_on && mp3_samples_ready >= length)
+	{
+		if (1152 - mp3_buffer_offs >= length) {
+			mix_samples(buffer, ym_buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, length, stereo);
+
+			mp3_buffer_offs += length;
+		} else {
+			// collect from both buffers..
+			int left = 1152 - mp3_buffer_offs;
+			mix_samples(buffer, ym_buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, left, stereo);
+			mp3_play_bufsel ^= 1;
+			mp3_buffer_offs = length - left;
+			mix_samples(buffer + left * 2, ym_buffer + left * 2,
+				shared_data->mp3_buffer[mp3_play_bufsel], mp3_buffer_offs, stereo);
 		}
+		mp3_samples_ready -= length;
 	} else {
-		for (i = 0; i < length; i++) {
-			int l = mix_buffer[i];
-			l += buffer[i];
-			Limit( l, MAXOUT, MINOUT );
-			buffer[i] = l;
-		}
+		mix_samples(buffer, ym_buffer, 0, length, stereo);
 	}
 
 	//printf("new writes: %i\n", writebuff_ptr);
@@ -441,12 +517,56 @@ void YM2612UpdateOne_940(short *buffer, int length, int stereo)
 	}
 	writebuff_ptr = 0;
 
-	/* give 940 another job */
+	/* give 940 ym job */
 	shared_ctl->writebuffsel ^= 1;
 	shared_ctl->length = length;
 	shared_ctl->stereo = stereo;
-	add_job_940(JOB940_YM2612UPDATEONE, 0);
+
+	// make sure we will have enough mp3 samples next frame
+	if (cdda_on && mp3_samples_ready < length)
+	{
+		shared_ctl->mp3_buffsel ^= 1;
+		mp3_job = JOB940_MP3DECODE;
+		mp3_samples_ready += 1152;
+	}
+
+	add_job_940(JOB940_YM2612UPDATEONE, mp3_job);
 	//spend_cycles(512);
 	//printf("SRCPND: %08lx, INTMODE: %08lx, INTMASK: %08lx, INTPEND: %08lx\n",
 	//	gp2x_memregl[0x4500>>2], gp2x_memregl[0x4504>>2], gp2x_memregl[0x4508>>2], gp2x_memregl[0x4510>>2]);
 }
+
+
+/***********************************************************/
+
+void mp3_start_play(FILE *f, int pos) // pos is 0-1023
+{
+	int byte_offs = 0;
+
+	if (!(currentConfig.EmuOpt&0x800)) { // cdda disabled?
+		return;
+	}
+
+	if (loaded_mp3 != f)
+	{
+		printf("loading mp3... "); fflush(stdout);
+		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");
+		shared_ctl->mp3_len = ftell(f);
+		loaded_mp3 = f;
+	}
+
+	// seek..
+	if (pos) {
+		byte_offs  = (shared_ctl->mp3_len << 6) >> 10;
+		byte_offs *= pos;
+		byte_offs >>= 6;
+	}
+	printf("mp3 pos1024: %i, byte_offs %i/%i\n", pos, byte_offs, shared_ctl->mp3_len);
+
+	shared_ctl->mp3_offs = byte_offs;
+}
+
+
diff --git a/platform/gp2x/Makefile b/platform/gp2x/Makefile
index 11e67490..7b41be93 100644
--- a/platform/gp2x/Makefile
+++ b/platform/gp2x/Makefile
@@ -61,6 +61,8 @@ OBJS += ../../zlib/gzio.o ../../zlib/inffast.o ../../zlib/inflate.o ../../zlib/i
 	../../zlib/deflate.o ../../zlib/crc32.o ../../zlib/adler32.o ../../zlib/zutil.o ../../zlib/compress.o
 # unzip
 OBJS += ../../unzip/unzip.o
+# mp3
+OBJS += mp3.o
 # CPU cores
 ifeq "$(use_musashi)" "1"
 DEFINC += -DEMU_M68K
@@ -78,13 +80,14 @@ DEFINC += -D_USE_DRZ80
 OBJS += ../../cpu/DrZ80/drz80.o
 endif
 
+
 all: PicoDrive.gpe
 
-PicoDrive.gpe : $(OBJS)
+PicoDrive.gpe : $(OBJS) helix/helix_mp3.a
 	@echo $@
-	@$(GCC) $(COPT) $(OBJS) $(PRELIBS) -lm -o $@
+	@$(GCC) -o $@ $(COPT) $^ -lm
 	@$(STRIP) $@
-#	@$(GCC) $(COPT) $(OBJS) $(PRELIBS) -lm -o PicoDrive_.gpe
+#	@$(GCC) $(COPT) $(OBJS) -lm -o PicoDrive_.gpe
 #	@gpecomp PicoDrive_.gpe $@
 ifeq "$(up)" "1"
 	@cmd //C copy $@ \\\\10.0.1.2\\gp2x\\mnt\\sd\\games\\PicoDrive\\
@@ -98,7 +101,7 @@ up:
 
 testrefr.gpe : test.o gp2x.o asmutils.o
 	@echo $@
-	@$(GCC) $(COPT) $^ $(PRELIBS) -o $@
+	@$(GCC) $(COPT) $^ -o $@
 	@$(STRIP) $@
 
 .c.o:
@@ -126,6 +129,10 @@ testrefr.gpe : test.o gp2x.o asmutils.o
 	@echo building Cyclone...
 	@make -C ../../cpu/Cyclone/proj -f Makefile.linux
 
+# build helix libs
+helix/helix_mp3.a:
+	make -C helix
+
 
 # cleanup
 clean: tidy
@@ -139,6 +146,20 @@ clean_prof:
 	find ../.. -name '*.gcno' -delete
 	find ../.. -name '*.gcda' -delete
 
+# ----------- release -----------
+ifneq ($(findstring rel,$(MAKECMDGOALS)),)
+ifeq ($(VER),)
+$(error need VER)
+endif
+endif
+
+rel: PicoDrive.gpe code940/code940.bin ../readme.txt config.txt
+	zip -9 -j ../../PicoDrive_$(VER).zip $^ mmuhack.o
+
+code940/code940.bin:
+	make -C code940/
+
+
 # test
 usbjoy.o : usbjoy.c
 	@echo $<
diff --git a/platform/gp2x/emu.c b/platform/gp2x/emu.c
index 49291eca..dcc8f6e1 100644
--- a/platform/gp2x/emu.c
+++ b/platform/gp2x/emu.c
@@ -187,11 +187,11 @@ static int cd_check(char *ext, char **bios_file)
 	}
 
 	/* it seems we have a CD image here. Try to detect region and load a suitable BIOS now.. */
-	fseek(cd_f, (type == 1) ? 0x100 : 0x110, SEEK_SET);
+	fseek(cd_f, (type == 1) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);
 	fread(buf, 1, 1, cd_f);
 	fclose(cd_f);
 
-	if (buf[0] == 0x64) region = 4; // EU
+	if (buf[0] == 0x64) region = 8; // EU
 	if (buf[0] == 0xa1) region = 1; // JAP
 
 	printf("detected %s Sega/Mega CD image with %s region\n",
@@ -452,9 +452,9 @@ int emu_ReadConfig(int game)
 		// set default config
 		memset(&currentConfig, 0, sizeof(currentConfig));
 		currentConfig.lastRomFile[0] = 0;
-		currentConfig.EmuOpt  = 0x1f;
-		currentConfig.PicoOpt = 0x0f;
-		currentConfig.PsndRate = 22050;
+		currentConfig.EmuOpt  = 0x1f | 0xc00; // | cd_leds | cd_cdda
+		currentConfig.PicoOpt = 0x0f | 0x200; // | use_940
+		currentConfig.PsndRate = 44100;
 		currentConfig.PicoRegion = 0; // auto
 		currentConfig.Frameskip = -1; // auto
 		currentConfig.CPUclock = 200;
diff --git a/platform/gp2x/menu.c b/platform/gp2x/menu.c
index 003bb40d..b771ee5a 100644
--- a/platform/gp2x/menu.c
+++ b/platform/gp2x/menu.c
@@ -630,7 +630,8 @@ static void cd_menu_loop_options(void)
 		if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options
 			switch (menu_sel) {
 				case  3: tmp_opts.EmuOpt ^=0x400; break;
-				case  4: return;
+				case  4: tmp_opts.EmuOpt ^=0x800; break;
+				case  5: return;
 			}
 		}
 		if(inp & (GP2X_X|GP2X_A)) return;
diff --git a/platform/gp2x/mmuhack.o b/platform/gp2x/mmuhack.o
new file mode 100644
index 00000000..475f4a54
Binary files /dev/null and b/platform/gp2x/mmuhack.o differ
diff --git a/platform/linux/940ctl_ym2612.c b/platform/linux/940ctl_ym2612.c
index 97029140..b5263dc5 100644
--- a/platform/linux/940ctl_ym2612.c
+++ b/platform/linux/940ctl_ym2612.c
@@ -12,6 +12,9 @@
 #include "../gp2x/gp2x.h"
 #include "../gp2x/emu.h"
 #include "../gp2x/menu.h"
+#include "../gp2x/code940/940shared.h"
+#include "../gp2x/helix/pub/mp3dec.h"
+#include "../../Pico/PicoInt.h"
 
 
 static YM2612 ym2612;
@@ -20,6 +23,14 @@ YM2612 *ym2612_940 = &ym2612;
 int  mix_buffer_[44100/50*2];	/* this is where the YM2612 samples will be mixed to */
 int *mix_buffer = mix_buffer_;
 
+static _940_data_t  shared_data_;
+static _940_ctl_t   shared_ctl_;
+static _940_data_t *shared_data = &shared_data_;
+static _940_ctl_t  *shared_ctl = &shared_ctl_;
+
+unsigned char *mp3_mem = 0;
+
+#define MP3_SIZE_MAX (0x1000000 - 4*640*480)
 
 /***********************************************************/
 
@@ -73,6 +84,8 @@ void YM2612PicoStateLoad_940(void)
 
 void YM2612Init_940(int baseclock, int rate)
 {
+	mp3_mem = malloc(MP3_SIZE_MAX);
+
 	YM2612Init_(baseclock, rate);
 }
 
@@ -83,30 +96,177 @@ void YM2612ResetChip_940(void)
 }
 
 
+static void mix_samples(short *dest_buf, int *ym_buf, short *mp3_buf, int len, int stereo)
+{
+	if (mp3_buf)
+	{
+		if (stereo)
+		{
+			for (; len > 0; len--)
+			{
+				int l, r;
+				l = r = *dest_buf;
+				l += *ym_buf++;  r += *ym_buf++;
+				l += *mp3_buf++; r += *mp3_buf++;
+				Limit( l, MAXOUT, MINOUT );
+				Limit( r, MAXOUT, MINOUT );
+				*dest_buf++ = l; *dest_buf++ = r;
+			}
+		} else {
+			for (; len > 0; len--)
+			{
+				int l = *ym_buf++;
+				l += *dest_buf;
+				l += *mp3_buf++;
+				Limit( l, MAXOUT, MINOUT );
+				*dest_buf++ = l;
+			}
+		}
+	}
+	else
+	{
+		if (stereo)
+		{
+			for (; len > 0; len--)
+			{
+				int l, r;
+				l = r = *dest_buf;
+				l += *ym_buf++, r += *ym_buf++;
+				Limit( l, MAXOUT, MINOUT );
+				Limit( r, MAXOUT, MINOUT );
+				*dest_buf++ = l; *dest_buf++ = r;
+			}
+		} else {
+			for (; len > 0; len--)
+			{
+				int l = *ym_buf++;
+				l += *dest_buf;
+				Limit( l, MAXOUT, MINOUT );
+				*dest_buf++ = l;
+			}
+		}
+	}
+}
+
+#if 0
+static void local_decode(void)
+{
+	int mp3_offs = shared_ctl->mp3_offs;
+	unsigned char *readPtr = mp3_mem + mp3_offs;
+	int bytesLeft = shared_ctl->mp3_len - mp3_offs;
+	int offset; // frame offset from readPtr
+	int err = 0;
+
+	if (bytesLeft <= 0) return; // EOF, nothing to do
+
+	offset = MP3FindSyncWord(readPtr, bytesLeft);
+	if (offset < 0) {
+		shared_ctl->mp3_offs = shared_ctl->mp3_len;
+		return; // EOF
+	}
+	readPtr += offset;
+	bytesLeft -= offset;
+
+	err = MP3Decode(shared_data->mp3dec, &readPtr, &bytesLeft,
+			shared_data->mp3_buffer[shared_ctl->mp3_buffsel], 0);
+	if (err) {
+		if (err == ERR_MP3_INDATA_UNDERFLOW) {
+			shared_ctl->mp3_offs = shared_ctl->mp3_len; // EOF
+			return;
+		} else if (err <= -6 && err >= -12) {
+			// ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
+			// just try to skip the offending frame..
+			readPtr++;
+		}
+		shared_ctl->mp3_errors++;
+		shared_ctl->mp3_lasterr = err;
+	}
+	shared_ctl->mp3_offs = readPtr - mp3_mem;
+}
+#endif
+
+
+
+
+static FILE *loaded_mp3 = 0;
+
 void YM2612UpdateOne_940(short *buffer, int length, int stereo)
 {
-	int i;
+#if 0
+	int cdda_on, *ym_buffer = mix_buffer;
+	static int mp3_samples_ready = 0, mp3_buffer_offs = 0;
+	static int mp3_play_bufsel = 1;
+
+
+	YM2612UpdateOne_(buffer, length, stereo); // really writes to mix_buffer
 
-	YM2612UpdateOne_(buffer, length, stereo);
-
-	/* mix data */
-	if (stereo) {
-		int *mb = mix_buffer;
-		for (i = length; i > 0; i--) {
-			int l, r;
-			l = r = *buffer;
-			l += *mb++, r += *mb++;
-			Limit( l, MAXOUT, MINOUT );
-			Limit( r, MAXOUT, MINOUT );
-			*buffer++ = l; *buffer++ = r;
+	// emulatind MCD, not data track, CDC is reading, playback was started, track not ended
+	cdda_on = (PicoMCD & 1) && !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1)
+			&& loaded_mp3 && shared_ctl->mp3_offs < shared_ctl->mp3_len;
+
+	/* mix data from previous go */
+	if (cdda_on && mp3_samples_ready >= length)
+	{
+		if (1152 - mp3_buffer_offs >= length) {
+			mix_samples(buffer, ym_buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, length, stereo);
+
+			mp3_buffer_offs += length;
+		} else {
+			// collect from both buffers..
+			int left = 1152 - mp3_buffer_offs;
+			mix_samples(buffer, ym_buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, left, stereo);
+			mp3_play_bufsel ^= 1;
+			mp3_buffer_offs = length - left;
+			mix_samples(buffer + left * 2, ym_buffer + left * 2,
+				shared_data->mp3_buffer[mp3_play_bufsel], mp3_buffer_offs, stereo);
 		}
+		mp3_samples_ready -= length;
 	} else {
-		for (i = 0; i < length; i++) {
-			int l = mix_buffer[i];
-			l += buffer[i];
-			Limit( l, MAXOUT, MINOUT );
-			buffer[i] = l;
-		}
+		mix_samples(buffer, ym_buffer, 0, length, stereo);
+	}
+
+	// make sure we will have enough mp3 samples next frame
+	if (cdda_on && mp3_samples_ready < length)
+	{
+		shared_ctl->mp3_buffsel ^= 1;
+		local_decode();
+		mp3_samples_ready += 1152;
+	}
+#else
+	YM2612UpdateOne_(buffer, length, stereo); // really writes to mix_buffer
+
+	mix_samples(buffer, mix_buffer, 0, length, stereo);
+#endif
+}
+
+
+/***********************************************************/
+
+void mp3_start_play(FILE *f, int pos) // pos is 0-1023
+{
+	int byte_offs = 0;
+
+	if (loaded_mp3 != f)
+	{
+		printf("loading mp3... "); fflush(stdout);
+		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");
+		shared_ctl->mp3_len = ftell(f);
+		loaded_mp3 = f;
 	}
+
+	// seek..
+	if (pos) {
+		byte_offs  = (shared_ctl->mp3_len << 6) >> 10;
+		byte_offs *= pos;
+		byte_offs >>= 6;
+	}
+	printf("mp3 pos1024: %i, byte_offs %i/%i\n", pos, byte_offs, shared_ctl->mp3_len);
+
+	shared_ctl->mp3_offs = byte_offs;
 }
 
+
+
diff --git a/platform/linux/Makefile b/platform/linux/Makefile
index cd369371..6cce5390 100644
--- a/platform/linux/Makefile
+++ b/platform/linux/Makefile
@@ -38,6 +38,8 @@ OBJS += ../../zlib/gzio.o ../../zlib/inffast.o ../../zlib/inflate.o ../../zlib/i
 	../../zlib/deflate.o ../../zlib/crc32.o ../../zlib/adler32.o ../../zlib/zutil.o ../../zlib/compress.o
 # unzip
 OBJS += ../../unzip/unzip.o
+# mp3
+OBJS += ../gp2x/mp3.o
 # CPU cores
 DEFINC += -DEMU_M68K
 OBJS += ../../cpu/musashi/m68kcpu.o  ../../cpu/musashi/m68kopac.o ../../cpu/musashi/m68kopdm.o \
@@ -62,7 +64,7 @@ tidy:
 
 PicoDrive : $(OBJS)
 	@echo $@
-	@$(GCC) $(COPT) $(OBJS) $(LDFLAGS) -lm -o $@
+	@$(GCC) $(COPT) $(OBJS) ../gp2x/helix/helix_mp3_x86.a $(LDFLAGS) -lm -o $@
 
 
 ../../cpu/mz80/mz80.o : ../../cpu/mz80/mz80.asm
diff --git a/platform/linux/gp2x.c b/platform/linux/gp2x.c
index d0efee88..6e442433 100644
--- a/platform/linux/gp2x.c
+++ b/platform/linux/gp2x.c
@@ -298,6 +298,11 @@ void gp2x_sound_write(void *buff, int len)
 	write(sounddev, buff, len);
 }
 
+void gp2x_sound_sync(void)
+{
+	ioctl(sounddev, SOUND_PCM_SYNC, 0);
+}
+
 void gp2x_sound_volume(int l, int r)
 {
  	l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;