5 #include <sys/mman.h>
\r
6 #include <sys/ioctl.h>
\r
7 #include <sys/time.h>
\r
11 #include "940shared.h"
\r
12 #include "../gp2x.h"
\r
15 #include "../asmutils.h"
\r
16 #include "../helix/pub/mp3dec.h"
\r
18 /* we will need some gp2x internals here */
\r
19 extern volatile unsigned short *gp2x_memregs; /* from minimal library rlyeh */
\r
20 extern volatile unsigned long *gp2x_memregl;
\r
22 static unsigned char *shared_mem = 0;
\r
23 static _940_data_t *shared_data = 0;
\r
24 static _940_ctl_t *shared_ctl = 0;
\r
25 static unsigned char *mp3_mem = 0;
\r
27 #define MP3_SIZE_MAX (0x1000000 - 4*640*480)
\r
29 int crashed_940 = 0;
\r
32 /***********************************************************/
\r
34 #define MAXOUT (+32767)
\r
35 #define MINOUT (-32768)
\r
38 #define Limit(val, max,min) { \
\r
39 if ( val > max ) val = max; \
\r
40 else if ( val < min ) val = min; \
\r
44 void wait_busy_940(void)
\r
48 printf("940 busy, entering wait loop.. (cnt: %i, wc: %i, ve: ", shared_ctl->loopc, shared_ctl->waitc);
\r
49 for (i = 0; i < 8; i++)
\r
50 printf("%i ", shared_ctl->vstarts[i]);
\r
53 for (i = 0; shared_ctl->busy; i++)
\r
55 spend_cycles(1024); /* needs tuning */
\r
57 printf("wait iterations: %i\n", i);
\r
59 for (i = 0; shared_ctl->busy && i < 0x10000; i++)
\r
60 spend_cycles(8*1024);
\r
61 if (i < 0x10000) return;
\r
64 printf("940 crashed (cnt: %i, ve: ", shared_ctl->loopc);
\r
65 for (i = 0; i < 8; i++)
\r
66 printf("%i ", shared_ctl->vstarts[i]);
\r
73 void add_job_940(int job0, int job1)
\r
75 shared_ctl->jobs[0] = job0;
\r
76 shared_ctl->jobs[1] = job1;
\r
77 shared_ctl->busy = 1;
\r
78 gp2x_memregs[0x3B3E>>1] = 0xffff; // cause an IRQ for 940
\r
82 static int read_to_upper(void *dest, void *tmpbuf, int tmpsize, FILE *f)
\r
84 int nRead, nLen = 0;
\r
88 nRead = fread(tmpbuf, 1, tmpsize, f);
\r
91 memcpy((unsigned char *)dest + nLen, tmpbuf, nRead);
\r
98 static void simpleWait(int thissec, int lim_time)
\r
100 struct timeval tval;
\r
102 spend_cycles(1024);
\r
103 gettimeofday(&tval, 0);
\r
104 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
106 while(tval.tv_usec < lim_time)
\r
108 spend_cycles(1024);
\r
109 gettimeofday(&tval, 0);
\r
110 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
117 /* none of the functions in this file should be called before this one */
\r
118 void YM2612Init_940(int baseclock, int rate)
\r
120 printf("YM2612Init_940()\n");
\r
121 printf("Mem usage: shared_data: %i, shared_ctl: %i\n", sizeof(*shared_data), sizeof(*shared_ctl));
\r
126 gp2x_memregs[0x3B46>>1] = 0xffff; // clear pending DUALCPU interrupts for 940
\r
127 gp2x_memregs[0x3B42>>1] = 0xffff; // enable DUALCPU interrupts for 940
\r
129 gp2x_memregl[0x4508>>2] = ~(1<<26); // unmask DUALCPU ints in the undocumented 940's interrupt controller
\r
131 if (shared_mem == NULL)
\r
133 shared_mem = (unsigned char *) mmap(0, 0x210000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x2000000);
\r
134 if(shared_mem == MAP_FAILED)
\r
136 printf("mmap(shared_data) failed with %i\n", errno);
\r
139 shared_data = (_940_data_t *) (shared_mem+0x100000);
\r
140 /* this area must not get buffered on either side */
\r
141 shared_ctl = (_940_ctl_t *) (shared_mem+0x200000);
\r
142 mp3_mem = (unsigned char *) mmap(0, MP3_SIZE_MAX, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x3000000);
\r
143 if (mp3_mem == MAP_FAILED)
\r
145 printf("mmap(mp3_mem) failed with %i\n", errno);
\r
153 unsigned char ucData[1024];
\r
155 char binpath[1024];
\r
158 strncpy(binpath, g_argv[0], 1023);
\r
160 for (i = strlen(binpath); i > 0; i--)
\r
161 if (binpath[i] == '/') { binpath[i] = 0; break; }
\r
162 strcat(binpath, "/code940.bin");
\r
164 fp = fopen(binpath, "rb");
\r
167 printf("failed to open %s\n", binpath);
\r
171 read_to_upper(shared_mem, ucData, sizeof(ucData), fp);
\r
176 memset(shared_data, 0, sizeof(*shared_data));
\r
177 memset(shared_ctl, 0, sizeof(*shared_ctl));
\r
179 /* now cause 940 to init it's ym2612 stuff */
\r
180 shared_ctl->baseclock = baseclock;
\r
181 shared_ctl->rate = rate;
\r
182 shared_ctl->jobs[0] = JOB940_INITALL;
\r
183 shared_ctl->jobs[1] = 0;
\r
184 shared_ctl->busy = 1;
\r
186 /* start the 940 */
\r
192 unsigned char *mp3_data = 0;
\r
194 void local_decode(void)
\r
196 int mp3_offs = shared_ctl->mp3_offs;
\r
197 unsigned char *readPtr = mp3_data + mp3_offs;
\r
198 int bytesLeft = shared_ctl->mp3_len - mp3_offs;
\r
199 int offset; // frame offset from readPtr
\r
202 if (bytesLeft <= 0) return; // EOF, nothing to do
\r
204 offset = MP3FindSyncWord(readPtr, bytesLeft);
\r
206 shared_ctl->mp3_offs = shared_ctl->mp3_len;
\r
210 bytesLeft -= offset;
\r
212 err = MP3Decode(shared_data->mp3dec, &readPtr, &bytesLeft,
\r
213 shared_data->mp3_buffer[shared_ctl->mp3_buffsel], 0);
\r
215 if (err == ERR_MP3_INDATA_UNDERFLOW) {
\r
216 shared_ctl->mp3_offs = shared_ctl->mp3_len; // EOF
\r
218 } else if (err <= -6 && err >= -12) {
\r
219 // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
\r
220 // just try to skip the offending frame..
\r
223 shared_ctl->mp3_errors++;
\r
224 shared_ctl->mp3_lasterr = err;
\r
226 shared_ctl->mp3_offs = readPtr - mp3_data;
\r
230 void gp2x_sound_sync(void);
\r
232 #define USE_LOCAL 0
\r
233 #define BENCHMARK 0
\r
235 int main(int argc, char *argv[])
\r
239 struct timeval tval; // timing
\r
240 int thissec = 0, fps = 0;
\r
241 int target_frametime, frame_samples, samples_ready, mp3_buffer_offs, play_bufsel;
\r
242 unsigned char play_buffer[44100/50*2*2];
\r
245 printf("usage: %s <mp3file>\n", argv[0]);
\r
252 YM2612Init_940(123, 44100);
\r
255 f = fopen(argv[1], "rb");
\r
257 printf("can't open %s\n", argv[1]);
\r
261 fseek(f, 0, SEEK_END);
\r
262 size = (int) ftell(f);
\r
263 if (size > MP3_SIZE_MAX) {
\r
264 printf("size %i > %i\n", size, MP3_SIZE_MAX);
\r
265 size = MP3_SIZE_MAX;
\r
268 fseek(f, 0, SEEK_SET);
\r
269 if (fread(mp3_mem, 1, size, f) != size) {
\r
270 printf("read failed, errno=%i\n", errno);
\r
275 shared_ctl->mp3_len = size;
\r
278 shared_data->mp3dec = MP3InitDecoder();
\r
279 mp3_data = malloc(size);
\r
280 printf("init: dec: %p ptr: %p\n", shared_data->mp3dec, mp3_data);
\r
282 printf("low mem\n");
\r
285 memcpy(mp3_data, mp3_mem, size);
\r
287 //printf("YM2612UpdateOne_940()\n");
\r
288 if (shared_ctl->busy) wait_busy_940();
\r
291 gp2x_start_sound(44100, 16, 1);
\r
293 #define DESIRED_FPS 50
\r
294 target_frametime = 1000000/DESIRED_FPS;
\r
295 frame_samples = 44100/DESIRED_FPS;
\r
296 samples_ready = mp3_buffer_offs = 0;
\r
303 gettimeofday(&tval, 0);
\r
304 if (tval.tv_sec != thissec)
\r
306 printf("fps: %i\n", fps);
\r
307 thissec = tval.tv_sec;
\r
310 shared_ctl->mp3_offs = 0;
\r
317 shared_ctl->mp3_buffsel ^= 1;
\r
321 shared_ctl->mp3_buffsel ^= 1;
\r
322 add_job_940(JOB940_MP3DECODE, 0);
\r
325 if (shared_ctl->mp3_lasterr) {
\r
326 printf("mp3_lasterr #%i: %i size: %i offs: %i\n", shared_ctl->mp3_errors, shared_ctl->mp3_lasterr,
\r
327 shared_ctl->mp3_len, shared_ctl->mp3_offs);
\r
328 printf("loopc: %i bytes: %08x\n",
\r
329 shared_ctl->loopc, *(int *)(mp3_mem+shared_ctl->mp3_offs));
\r
330 shared_ctl->mp3_lasterr = 0;
\r
336 gp2x_sound_write(shared_data->mp3_buffer[shared_ctl->mp3_buffsel^1], 1152*2*2);
\r
339 lim_time = (fps+1) * target_frametime;
\r
344 if (samples_ready >= frame_samples) {
\r
345 if (1152 - mp3_buffer_offs >= frame_samples) {
\r
346 memcpy(play_buffer, shared_data->mp3_buffer[play_bufsel] + mp3_buffer_offs*2,
\r
347 frame_samples*2*2);
\r
348 mp3_buffer_offs += frame_samples;
\r
350 // collect from both buffers..
\r
351 int left = 1152 - mp3_buffer_offs;
\r
352 memcpy(play_buffer, shared_data->mp3_buffer[play_bufsel] + mp3_buffer_offs*2,
\r
355 mp3_buffer_offs = frame_samples - left;
\r
356 memcpy(play_buffer + left*2*2, shared_data->mp3_buffer[play_bufsel],
\r
357 mp3_buffer_offs*2*2);
\r
359 gp2x_sound_write(play_buffer, frame_samples*2*2);
\r
360 samples_ready -= frame_samples;
\r
363 // make sure we will have enough samples next frame
\r
364 if (samples_ready < frame_samples) {
\r
365 // wait_busy_940();
\r
366 shared_ctl->mp3_buffsel ^= 1;
\r
367 add_job_940(JOB940_MP3DECODE, 0);
\r
368 samples_ready += 1152;
\r
371 gettimeofday(&tval, 0);
\r
372 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
373 if(tval.tv_usec < lim_time)
\r
376 simpleWait(thissec, lim_time);
\r