X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=platform%2Fgp2x%2Fcode940%2F940.c;h=a4d3a464d8a0549e9d6ded3473f0176f764bf94b;hb=74e1b42b9256870c7e9f70f5b4ca71ed0e8a63d5;hp=4c7082ac7c8eeda57d4d9e43bc3d2ecd1801b05f;hpb=6cadc2da0070781cf2d8fcff84265d3ca1f423b9;p=picodrive.git diff --git a/platform/gp2x/code940/940.c b/platform/gp2x/code940/940.c index 4c7082a..a4d3a46 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 @@ -18,44 +18,106 @@ void drain_wb(void); // is changed by other core just before we update it void set_if_not_changed(int *val, int oldval, int newval); +void _memcpy(void *dst, const void *src, int count); + // asm volatile ("mov r0, #0" ::: "r0"); // asm volatile ("mcr p15, 0, r0, c7, c6, 0" ::: "r0"); /* flush dcache */ // asm volatile ("mcr p15, 0, r0, c7, c10, 4" ::: "r0"); /* drain write buffer */ +static int find_sync_word(unsigned char *buf, int nBytes) +{ + unsigned char *p, *pe; + + /* find byte-aligned syncword - need 12 (MPEG 1,2) or 11 (MPEG 2.5) matching bits */ + for (p = buf, pe = buf + nBytes - 4; p < pe; p++) + { + int pn; + if (p[0] != 0xff) continue; + pn = p[1]; + if ((pn & 0xf8) != 0xf8 || // currently must be MPEG1 + (pn & 6) == 0) { // invalid layer + p++; continue; + } + pn = p[2]; + if ((pn & 0xf0) < 0x20 || (pn & 0xf0) == 0xf0 || // bitrates + (pn & 0x0c) != 0) { // not 44kHz + continue; + } + + return p - buf; + } + + return -1; +} + static void mp3_decode(void) { int mp3_offs = shared_ctl->mp3_offs; unsigned char *readPtr = mp3_data + mp3_offs; int bytesLeft = shared_ctl->mp3_len - mp3_offs; int offset; // frame offset from readPtr - int err; + int retries = 0, err; if (bytesLeft <= 0) return; // EOF, nothing to do - offset = MP3FindSyncWord(readPtr, bytesLeft); - if (offset < 0) { - set_if_not_changed(&shared_ctl->mp3_offs, 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) { - set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, shared_ctl->mp3_len); - return; - } else if (err <= -6 && err >= -12) { - // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_* - // just try to skip the offending frame.. - readPtr++; + for (retries = 0; retries < 2; retries++) + { + offset = find_sync_word(readPtr, bytesLeft); + if (offset < 0) + goto set_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_MAINDATA_UNDERFLOW) + // just need another frame + continue; + + if (err == ERR_MP3_INDATA_UNDERFLOW) + goto set_eof; + + if (err <= -6 && err >= -12) { + // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_* + // just try to skip the offending frame.. + readPtr++; + bytesLeft--; + continue; + } + shared_ctl->mp3_errors++; + shared_ctl->mp3_lasterr = err; } - shared_ctl->mp3_errors++; - shared_ctl->mp3_lasterr = err; + break; } + set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, readPtr - mp3_data); + return; + +set_eof: + set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, shared_ctl->mp3_len); +} + +static void ym_flush_writes(void) +{ + UINT16 *wbuff; + int i; + + if (shared_ctl->writebuffsel == 1) { + wbuff = shared_ctl->writebuff1; + } else { + wbuff = shared_ctl->writebuff0; + } + + /* playback all writes */ + for (i = 2048; i > 0; i--) { + UINT16 d = *wbuff++; + if (d == 0xffff) break; + if (d == 0xfffe) continue; + YM2612Write_(d >> 8, d); + } } static void ym_update(int *ym_buffer) @@ -86,7 +148,8 @@ static void ym_update(int *ym_buffer) YM2612Write_(d >> 8, d); } - if (two_upds) { + if (two_upds) + { int len1 = shared_ctl->length / 2; shared_ctl->ym_active_chs = YM2612UpdateOne_(ym_buffer, len1, shared_ctl->stereo, 1); @@ -145,6 +208,20 @@ void Main940(void) YM2612PicoStateLoad_(); break; + case JOB940_PICOSTATESAVE2: + YM2612PicoStateSave2(0, 0); + _memcpy(shared_ctl->writebuff0, ym2612_940->REGS, 0x200); + break; + + case JOB940_PICOSTATELOAD2_PREP: + ym_flush_writes(); + break; + + case JOB940_PICOSTATELOAD2: + _memcpy(ym2612_940->REGS, shared_ctl->writebuff0, 0x200); + YM2612PicoStateLoad2(0, 0); + break; + case JOB940_YM2612UPDATEONE: ym_update(ym_buffer); break; @@ -152,6 +229,11 @@ void Main940(void) case JOB940_MP3DECODE: mp3_decode(); break; + + case JOB940_MP3RESET: + if (shared_data->mp3dec) MP3FreeDecoder(shared_data->mp3dec); + shared_data->mp3dec = MP3InitDecoder(); + break; } shared_ctl->loopc++;