1 /* faked 940 code just uses local copy of ym2612 */
11 #include "../../Pico/sound/ym2612.h"
12 #include "../gp2x/gp2x.h"
13 #include "../gp2x/emu.h"
14 #include "../gp2x/menu.h"
15 #include "../gp2x/code940/940shared.h"
16 #include "../gp2x/helix/pub/mp3dec.h"
17 #include "../../Pico/PicoInt.h"
22 YM2612 *ym2612_940 = &ym2612;
23 int mix_buffer_[44100/50*2]; /* this is where the YM2612 samples will be mixed to */
24 int *mix_buffer = mix_buffer_;
26 static _940_data_t shared_data_;
27 static _940_ctl_t shared_ctl_;
28 static _940_data_t *shared_data = &shared_data_;
29 static _940_ctl_t *shared_ctl = &shared_ctl_;
31 unsigned char *mp3_mem = 0;
33 #define MP3_SIZE_MAX (0x1000000 - 4*640*480)
35 /***********************************************************/
37 #define MAXOUT (+32767)
38 #define MINOUT (-32768)
41 #define Limit(val, max,min) { \
42 if ( val > max ) val = max; \
43 else if ( val < min ) val = min; \
47 int YM2612Write_940(unsigned int a, unsigned int v)
51 return 0; // cause the engine to do updates once per frame only
54 UINT8 YM2612Read_940(void)
60 int YM2612PicoTick_940(int n)
68 void YM2612PicoStateLoad_940(void)
72 YM2612PicoStateLoad_();
74 for(i = 0; i < 0x100; i++) {
76 YM2612Write_(1, ym2612.REGS[i]);
78 for(i = 0; i < 0x100; i++) {
80 YM2612Write_(3, ym2612.REGS[i|0x100]);
85 void YM2612Init_940(int baseclock, int rate)
87 mp3_mem = malloc(MP3_SIZE_MAX);
89 YM2612Init_(baseclock, rate);
93 void YM2612ResetChip_940(void)
99 static void mix_samples(short *dest_buf, int *ym_buf, short *mp3_buf, int len, int stereo)
105 for (; len > 0; len--)
109 l += *ym_buf++; r += *ym_buf++;
110 l += *mp3_buf++; r += *mp3_buf++;
111 Limit( l, MAXOUT, MINOUT );
112 Limit( r, MAXOUT, MINOUT );
113 *dest_buf++ = l; *dest_buf++ = r;
116 for (; len > 0; len--)
121 Limit( l, MAXOUT, MINOUT );
130 for (; len > 0; len--)
134 l += *ym_buf++, r += *ym_buf++;
135 Limit( l, MAXOUT, MINOUT );
136 Limit( r, MAXOUT, MINOUT );
137 *dest_buf++ = l; *dest_buf++ = r;
140 for (; len > 0; len--)
144 Limit( l, MAXOUT, MINOUT );
152 static void local_decode(void)
154 int mp3_offs = shared_ctl->mp3_offs;
155 unsigned char *readPtr = mp3_mem + mp3_offs;
156 int bytesLeft = shared_ctl->mp3_len - mp3_offs;
157 int offset; // frame offset from readPtr
160 if (bytesLeft <= 0) return; // EOF, nothing to do
162 offset = MP3FindSyncWord(readPtr, bytesLeft);
164 shared_ctl->mp3_offs = shared_ctl->mp3_len;
170 err = MP3Decode(shared_data->mp3dec, &readPtr, &bytesLeft,
171 shared_data->mp3_buffer[shared_ctl->mp3_buffsel], 0);
173 if (err == ERR_MP3_INDATA_UNDERFLOW) {
174 shared_ctl->mp3_offs = shared_ctl->mp3_len; // EOF
176 } else if (err <= -6 && err >= -12) {
177 // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
178 // just try to skip the offending frame..
181 shared_ctl->mp3_errors++;
182 shared_ctl->mp3_lasterr = err;
184 shared_ctl->mp3_offs = readPtr - mp3_mem;
191 static FILE *loaded_mp3 = 0;
193 void YM2612UpdateOne_940(short *buffer, int length, int stereo)
196 int cdda_on, *ym_buffer = mix_buffer;
197 static int mp3_samples_ready = 0, mp3_buffer_offs = 0;
198 static int mp3_play_bufsel = 1;
201 YM2612UpdateOne_(buffer, length, stereo); // really writes to mix_buffer
203 // emulatind MCD, not data track, CDC is reading, playback was started, track not ended
204 cdda_on = (PicoMCD & 1) && !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1)
205 && loaded_mp3 && shared_ctl->mp3_offs < shared_ctl->mp3_len;
207 /* mix data from previous go */
208 if (cdda_on && mp3_samples_ready >= length)
210 if (1152 - mp3_buffer_offs >= length) {
211 mix_samples(buffer, ym_buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, length, stereo);
213 mp3_buffer_offs += length;
215 // collect from both buffers..
216 int left = 1152 - mp3_buffer_offs;
217 mix_samples(buffer, ym_buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, left, stereo);
218 mp3_play_bufsel ^= 1;
219 mp3_buffer_offs = length - left;
220 mix_samples(buffer + left * 2, ym_buffer + left * 2,
221 shared_data->mp3_buffer[mp3_play_bufsel], mp3_buffer_offs, stereo);
223 mp3_samples_ready -= length;
225 mix_samples(buffer, ym_buffer, 0, length, stereo);
228 // make sure we will have enough mp3 samples next frame
229 if (cdda_on && mp3_samples_ready < length)
231 shared_ctl->mp3_buffsel ^= 1;
233 mp3_samples_ready += 1152;
236 YM2612UpdateOne_(buffer, length, stereo); // really writes to mix_buffer
238 mix_samples(buffer, mix_buffer, 0, length, stereo);
243 /***********************************************************/
245 void mp3_start_play(FILE *f, int pos) // pos is 0-1023
251 printf("loading mp3... "); fflush(stdout);
252 fseek(f, 0, SEEK_SET);
253 fread(mp3_mem, 1, MP3_SIZE_MAX, f);
254 if (feof(f)) printf("done.\n");
255 else printf("done. mp3 too large, not all data loaded.\n");
256 shared_ctl->mp3_len = ftell(f);
262 byte_offs = (shared_ctl->mp3_len << 6) >> 10;
266 printf("mp3 pos1024: %i, byte_offs %i/%i\n", pos, byte_offs, shared_ctl->mp3_len);
268 shared_ctl->mp3_offs = byte_offs;