1.10 release
authornotaz <notasas@gmail.com>
Wed, 24 Jan 2007 20:38:23 +0000 (20:38 +0000)
committernotaz <notasas@gmail.com>
Wed, 24 Jan 2007 20:38:23 +0000 (20:38 +0000)
git-svn-id: file:///home/notaz/opt/svn/PicoDrive@23 be3aeb3a-fb24-0410-a615-afba39da0efa

Pico/Pico.h
Pico/cd/cd_file.c
Pico/cd/cd_file.h
platform/gp2x/940ctl_ym2612.c
platform/gp2x/Makefile
platform/gp2x/emu.c
platform/gp2x/menu.c
platform/gp2x/mmuhack.o [new file with mode: 0644]
platform/linux/940ctl_ym2612.c
platform/linux/Makefile
platform/linux/gp2x.c

index f1b3b09..4884051 100644 (file)
 extern "C" {\r
 #endif\r
 \r
+// external\r
+int mp3_get_bitrate(FILE *f, int size);\r
+void mp3_start_play(FILE *f, int pos);\r
+\r
+\r
 // Pico.c\r
 // PicoOpt bits LSb->MSb:\r
 // enable_ym2612&dac, enable_sn76496, enable_z80, stereo_sound,\r
index 4f11ba5..277f485 100644 (file)
@@ -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
        {
index 4bf9d19..092e337 100644 (file)
@@ -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];
index 35bf4a7..f1949e6 100644 (file)
@@ -12,6 +12,7 @@
 #include "emu.h"\r
 #include "menu.h"\r
 #include "asmutils.h"\r
+#include "../../Pico/PicoInt.h"\r
 \r
 /* we will need some gp2x internals here */\r
 extern volatile unsigned short *gp2x_memregs; /* from minimal library rlyeh */\r
@@ -20,6 +21,9 @@ extern volatile unsigned long  *gp2x_memregl;
 static unsigned char *shared_mem = 0;\r
 static _940_data_t *shared_data = 0;\r
 static _940_ctl_t *shared_ctl = 0;\r
+static unsigned char *mp3_mem = 0;\r
+\r
+#define MP3_SIZE_MAX (0x1000000 - 4*640*480)\r
 \r
 int crashed_940 = 0;\r
 \r
@@ -321,6 +325,12 @@ void YM2612Init_940(int baseclock, int rate)
                shared_data = (_940_data_t *) (shared_mem+0x100000);\r
                /* this area must not get buffered on either side */\r
                shared_ctl =  (_940_ctl_t *)  (shared_mem+0x200000);\r
+               mp3_mem = (unsigned char *) mmap(0, MP3_SIZE_MAX, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x3000000);\r
+               if (mp3_mem == MAP_FAILED)\r
+               {\r
+                       printf("mmap(mp3_mem) failed with %i\n", errno);\r
+                       exit(1);\r
+               }\r
                crashed_940 = 1;\r
        }\r
 \r
@@ -401,9 +411,70 @@ void YM2612ResetChip_940(void)
 }\r
 \r
 \r
+static void mix_samples(short *dest_buf, int *ym_buf, short *mp3_buf, int len, int stereo)\r
+{\r
+       if (mp3_buf)\r
+       {\r
+               if (stereo)\r
+               {\r
+                       for (; len > 0; len--)\r
+                       {\r
+                               int l, r, lm, rm;\r
+                               l = r = *dest_buf;\r
+                               l += *ym_buf++;  r += *ym_buf++;\r
+                               lm = *mp3_buf++; rm = *mp3_buf++;\r
+                               l += lm - lm/2; r += rm - rm/2;\r
+                               Limit( l, MAXOUT, MINOUT );\r
+                               Limit( r, MAXOUT, MINOUT );\r
+                               *dest_buf++ = l; *dest_buf++ = r;\r
+                       }\r
+               } else {\r
+                       for (; len > 0; len--)\r
+                       {\r
+                               // TODO: normalize\r
+                               int l = *ym_buf++;\r
+                               l += *dest_buf;\r
+                               l += *mp3_buf++;\r
+                               Limit( l, MAXOUT, MINOUT );\r
+                               *dest_buf++ = l;\r
+                       }\r
+               }\r
+       }\r
+       else\r
+       {\r
+               if (stereo)\r
+               {\r
+                       for (; len > 0; len--)\r
+                       {\r
+                               int l, r;\r
+                               l = r = *dest_buf;\r
+                               l += *ym_buf++, r += *ym_buf++;\r
+                               Limit( l, MAXOUT, MINOUT );\r
+                               Limit( r, MAXOUT, MINOUT );\r
+                               *dest_buf++ = l; *dest_buf++ = r;\r
+                       }\r
+               } else {\r
+                       for (; len > 0; len--)\r
+                       {\r
+                               int l = *ym_buf++;\r
+                               l += *dest_buf;\r
+                               Limit( l, MAXOUT, MINOUT );\r
+                               *dest_buf++ = l;\r
+                       }\r
+               }\r
+       }\r
+}\r
+\r
+\r
+// here we assume that length is different between games, but constant in one game\r
+\r
+static FILE *loaded_mp3 = 0;\r
+\r
 void YM2612UpdateOne_940(short *buffer, int length, int stereo)\r
 {\r
-       int i, *mix_buffer = shared_data->mix_buffer;\r
+       int cdda_on, *ym_buffer = shared_data->mix_buffer, mp3_job = 0;\r
+       static int mp3_samples_ready = 0, mp3_buffer_offs = 0;\r
+       static int mp3_play_bufsel = 1;\r
 \r
        //printf("YM2612UpdateOne_940()\n");\r
        if (shared_ctl->busy) wait_busy_940();\r
@@ -413,24 +484,29 @@ void YM2612UpdateOne_940(short *buffer, int length, int stereo)
        //      printf("%i ", shared_ctl->vstarts[i]);\r
        //printf(")\n");\r
 \r
+       // emulatind MCD, cdda enabled in config, not data track, CDC is reading, playback was started, track not ended\r
+       cdda_on = (PicoMCD & 1) && (currentConfig.EmuOpt&0x800) && !(Pico_mcd->s68k_regs[0x36] & 1) &&\r
+                       (Pico_mcd->scd.Status_CDC & 1) && loaded_mp3 && shared_ctl->mp3_offs < shared_ctl->mp3_len;\r
+\r
        /* mix data from previous go */\r
-       if (stereo) {\r
-               int *mb = mix_buffer;\r
-               for (i = length; i > 0; i--) {\r
-                       int l, r;\r
-                       l = r = *buffer;\r
-                       l += *mb++, r += *mb++;\r
-                       Limit( l, MAXOUT, MINOUT );\r
-                       Limit( r, MAXOUT, MINOUT );\r
-                       *buffer++ = l; *buffer++ = r;\r
+       if (cdda_on && mp3_samples_ready >= length)\r
+       {\r
+               if (1152 - mp3_buffer_offs >= length) {\r
+                       mix_samples(buffer, ym_buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, length, stereo);\r
+\r
+                       mp3_buffer_offs += length;\r
+               } else {\r
+                       // collect from both buffers..\r
+                       int left = 1152 - mp3_buffer_offs;\r
+                       mix_samples(buffer, ym_buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, left, stereo);\r
+                       mp3_play_bufsel ^= 1;\r
+                       mp3_buffer_offs = length - left;\r
+                       mix_samples(buffer + left * 2, ym_buffer + left * 2,\r
+                               shared_data->mp3_buffer[mp3_play_bufsel], mp3_buffer_offs, stereo);\r
                }\r
+               mp3_samples_ready -= length;\r
        } else {\r
-               for (i = 0; i < length; i++) {\r
-                       int l = mix_buffer[i];\r
-                       l += buffer[i];\r
-                       Limit( l, MAXOUT, MINOUT );\r
-                       buffer[i] = l;\r
-               }\r
+               mix_samples(buffer, ym_buffer, 0, length, stereo);\r
        }\r
 \r
        //printf("new writes: %i\n", writebuff_ptr);\r
@@ -441,12 +517,56 @@ void YM2612UpdateOne_940(short *buffer, int length, int stereo)
        }\r
        writebuff_ptr = 0;\r
 \r
-       /* give 940 another job */\r
+       /* give 940 ym job */\r
        shared_ctl->writebuffsel ^= 1;\r
        shared_ctl->length = length;\r
        shared_ctl->stereo = stereo;\r
-       add_job_940(JOB940_YM2612UPDATEONE, 0);\r
+\r
+       // make sure we will have enough mp3 samples next frame\r
+       if (cdda_on && mp3_samples_ready < length)\r
+       {\r
+               shared_ctl->mp3_buffsel ^= 1;\r
+               mp3_job = JOB940_MP3DECODE;\r
+               mp3_samples_ready += 1152;\r
+       }\r
+\r
+       add_job_940(JOB940_YM2612UPDATEONE, mp3_job);\r
        //spend_cycles(512);\r
        //printf("SRCPND: %08lx, INTMODE: %08lx, INTMASK: %08lx, INTPEND: %08lx\n",\r
        //      gp2x_memregl[0x4500>>2], gp2x_memregl[0x4504>>2], gp2x_memregl[0x4508>>2], gp2x_memregl[0x4510>>2]);\r
 }\r
+\r
+\r
+/***********************************************************/\r
+\r
+void mp3_start_play(FILE *f, int pos) // pos is 0-1023\r
+{\r
+       int byte_offs = 0;\r
+\r
+       if (!(currentConfig.EmuOpt&0x800)) { // cdda disabled?\r
+               return;\r
+       }\r
+\r
+       if (loaded_mp3 != f)\r
+       {\r
+               printf("loading mp3... "); fflush(stdout);\r
+               fseek(f, 0, SEEK_SET);\r
+               fread(mp3_mem, 1, MP3_SIZE_MAX, f);\r
+               if (feof(f)) printf("done.\n");\r
+               else printf("done. mp3 too large, not all data loaded.\n");\r
+               shared_ctl->mp3_len = ftell(f);\r
+               loaded_mp3 = f;\r
+       }\r
+\r
+       // seek..\r
+       if (pos) {\r
+               byte_offs  = (shared_ctl->mp3_len << 6) >> 10;\r
+               byte_offs *= pos;\r
+               byte_offs >>= 6;\r
+       }\r
+       printf("mp3 pos1024: %i, byte_offs %i/%i\n", pos, byte_offs, shared_ctl->mp3_len);\r
+\r
+       shared_ctl->mp3_offs = byte_offs;\r
+}\r
+\r
+\r
index 11e6749..7b41be9 100644 (file)
@@ -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\r
 # unzip\r
 OBJS += ../../unzip/unzip.o\r
+# mp3\r
+OBJS += mp3.o\r
 # CPU cores\r
 ifeq "$(use_musashi)" "1"\r
 DEFINC += -DEMU_M68K\r
@@ -78,13 +80,14 @@ DEFINC += -D_USE_DRZ80
 OBJS += ../../cpu/DrZ80/drz80.o\r
 endif\r
 \r
+\r
 all: PicoDrive.gpe\r
 \r
-PicoDrive.gpe : $(OBJS)\r
+PicoDrive.gpe : $(OBJS) helix/helix_mp3.a\r
        @echo $@\r
-       @$(GCC) $(COPT) $(OBJS) $(PRELIBS) -lm -o $@\r
+       @$(GCC) -o $@ $(COPT) $^ -lm\r
        @$(STRIP) $@\r
-#      @$(GCC) $(COPT) $(OBJS) $(PRELIBS) -lm -o PicoDrive_.gpe\r
+#      @$(GCC) $(COPT) $(OBJS) -lm -o PicoDrive_.gpe\r
 #      @gpecomp PicoDrive_.gpe $@\r
 ifeq "$(up)" "1"\r
        @cmd //C copy $@ \\\\10.0.1.2\\gp2x\\mnt\\sd\\games\\PicoDrive\\\r
@@ -98,7 +101,7 @@ up:
 \r
 testrefr.gpe : test.o gp2x.o asmutils.o\r
        @echo $@\r
-       @$(GCC) $(COPT) $^ $(PRELIBS) -o $@\r
+       @$(GCC) $(COPT) $^ -o $@\r
        @$(STRIP) $@\r
 \r
 .c.o:\r
@@ -126,6 +129,10 @@ testrefr.gpe : test.o gp2x.o asmutils.o
        @echo building Cyclone...\r
        @make -C ../../cpu/Cyclone/proj -f Makefile.linux\r
 \r
+# build helix libs\r
+helix/helix_mp3.a:\r
+       make -C helix\r
+\r
 \r
 # cleanup\r
 clean: tidy\r
@@ -139,6 +146,20 @@ clean_prof:
        find ../.. -name '*.gcno' -delete\r
        find ../.. -name '*.gcda' -delete\r
 \r
+# ----------- release -----------\r
+ifneq ($(findstring rel,$(MAKECMDGOALS)),)\r
+ifeq ($(VER),)\r
+$(error need VER)\r
+endif\r
+endif\r
+\r
+rel: PicoDrive.gpe code940/code940.bin ../readme.txt config.txt\r
+       zip -9 -j ../../PicoDrive_$(VER).zip $^ mmuhack.o\r
+\r
+code940/code940.bin:\r
+       make -C code940/\r
+\r
+\r
 # test\r
 usbjoy.o : usbjoy.c\r
        @echo $<\r
index 49291ec..dcc8f6e 100644 (file)
@@ -187,11 +187,11 @@ static int cd_check(char *ext, char **bios_file)
        }\r
 \r
        /* it seems we have a CD image here. Try to detect region and load a suitable BIOS now.. */\r
-       fseek(cd_f, (type == 1) ? 0x100 : 0x110, SEEK_SET);\r
+       fseek(cd_f, (type == 1) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);\r
        fread(buf, 1, 1, cd_f);\r
        fclose(cd_f);\r
 \r
-       if (buf[0] == 0x64) region = 4; // EU\r
+       if (buf[0] == 0x64) region = 8; // EU\r
        if (buf[0] == 0xa1) region = 1; // JAP\r
 \r
        printf("detected %s Sega/Mega CD image with %s region\n",\r
@@ -452,9 +452,9 @@ int emu_ReadConfig(int game)
                // set default config\r
                memset(&currentConfig, 0, sizeof(currentConfig));\r
                currentConfig.lastRomFile[0] = 0;\r
-               currentConfig.EmuOpt  = 0x1f;\r
-               currentConfig.PicoOpt = 0x0f;\r
-               currentConfig.PsndRate = 22050;\r
+               currentConfig.EmuOpt  = 0x1f | 0xc00; // | cd_leds | cd_cdda\r
+               currentConfig.PicoOpt = 0x0f | 0x200; // | use_940\r
+               currentConfig.PsndRate = 44100;\r
                currentConfig.PicoRegion = 0; // auto\r
                currentConfig.Frameskip = -1; // auto\r
                currentConfig.CPUclock = 200;\r
index 003bb40..b771ee5 100644 (file)
@@ -630,7 +630,8 @@ static void cd_menu_loop_options(void)
                if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
                        switch (menu_sel) {\r
                                case  3: tmp_opts.EmuOpt ^=0x400; break;\r
-                               case  4: return;\r
+                               case  4: tmp_opts.EmuOpt ^=0x800; break;\r
+                               case  5: return;\r
                        }\r
                }\r
                if(inp & (GP2X_X|GP2X_A)) return;\r
diff --git a/platform/gp2x/mmuhack.o b/platform/gp2x/mmuhack.o
new file mode 100644 (file)
index 0000000..475f4a5
Binary files /dev/null and b/platform/gp2x/mmuhack.o differ
index 9702914..b5263dc 100644 (file)
@@ -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;
 }
 
+
+
index cd36937..6cce539 100644 (file)
@@ -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
index d0efee8..6e44243 100644 (file)
@@ -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;