From 598e7c06cd865f9c4f82e3ebb9b08e8b064f00e8 Mon Sep 17 00:00:00 2001 From: notaz Date: Wed, 24 Jan 2007 20:38:23 +0000 Subject: [PATCH] 1.10 release git-svn-id: file:///home/notaz/opt/svn/PicoDrive/platform@23 be3aeb3a-fb24-0410-a615-afba39da0efa --- gp2x/940ctl_ym2612.c | 156 +++++++++++++++++++++++++++++---- gp2x/Makefile | 29 ++++++- gp2x/emu.c | 10 +-- gp2x/menu.c | 3 +- gp2x/mmuhack.o | Bin 0 -> 1720 bytes linux/940ctl_ym2612.c | 198 ++++++++++++++++++++++++++++++++++++++---- linux/Makefile | 4 +- linux/gp2x.c | 5 ++ 8 files changed, 357 insertions(+), 48 deletions(-) create mode 100644 gp2x/mmuhack.o diff --git a/gp2x/940ctl_ym2612.c b/gp2x/940ctl_ym2612.c index 35bf4a7..f1949e6 100644 --- a/gp2x/940ctl_ym2612.c +++ b/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/gp2x/Makefile b/gp2x/Makefile index 11e6749..7b41be9 100644 --- a/gp2x/Makefile +++ b/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/gp2x/emu.c b/gp2x/emu.c index 49291ec..dcc8f6e 100644 --- a/gp2x/emu.c +++ b/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(¤tConfig, 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/gp2x/menu.c b/gp2x/menu.c index 003bb40..b771ee5 100644 --- a/gp2x/menu.c +++ b/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/gp2x/mmuhack.o b/gp2x/mmuhack.o new file mode 100644 index 0000000000000000000000000000000000000000..475f4a54ae074c9a5cd1848f07d720cce3d9ccf0 GIT binary patch literal 1720 zcmb_c&ubGw6n?v#+O)PN7E!Stwjh!sUDDJZ{4Er1g(?l|MTEE}+jeQP3E8daNo@~$ z@Sq?fUR-az6#N4eauK|F@gSnmWEES4qTs=k@q5{sxGg<8F!|p1-ka~u%)ZI(Gm~c( zMKMVXg;JzQ0=%k*px!>ri-D@ZV z55MhzUyWT1deRGuGJK~~`n0GodAHG|Y_NyuV=U>{yD3}mh0XgS@`GoI&hc zH@y-NY7V1joSuLE(`gkjDhHuC=W2N9v&uHt0Qto(>=D@mi^O3^waZy`{+B;BLQ{)4~={hB$98 zCx{{Io&#qInO6hZFOz^WFDvq7=o9h|N)n0fge~{RVHLtx*bswj@rc%BejJ6S;M@*7 zfys4~n0&`ky$2mcui%qI>Pap!>W|USrt~vv?OO`H+J*`o?`ZAg-RKAAl+{P6^I$SAoJLkZt}Z%r5~sSI6Xh6ZjjGzXkK?(9i)+#KHG8zlkqy z;*U1*7vQp9AM}^OWj*Hd;Tn0G$>%dUyrYU;#G9sE4v%cUXyrEk4afz#a>a7mWV&2& zC6So39ouUY>n2F0YCGN>q`g_&75Y@N-F&WU&DgGIRYO*=+MGpXW*&bZ&U{5uzFP%- y_gJ7WuqN;=Bbw|zP{m_fWsb)E#T?BR$MKau^dFEp4;7cJGeVd3c^$uZj{gPHjSv<9 literal 0 HcmV?d00001 diff --git a/linux/940ctl_ym2612.c b/linux/940ctl_ym2612.c index 9702914..b5263dc 100644 --- a/linux/940ctl_ym2612.c +++ b/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/linux/Makefile b/linux/Makefile index cd36937..6cce539 100644 --- a/linux/Makefile +++ b/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/linux/gp2x.c b/linux/gp2x.c index d0efee8..6e44243 100644 --- a/linux/gp2x.c +++ b/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; -- 2.39.5