1 // Code for communication with ARM940 and control of it.
\r
2 // (c) Copyright 2007, Grazvydas "notaz" Ignotas
\r
8 #include <sys/mman.h>
\r
9 #include <sys/ioctl.h>
\r
13 #include "code940/940shared.h"
\r
17 #include "../common/mp3.h"
\r
18 #include "../common/arm_utils.h"
\r
19 #include "../common/menu.h"
\r
20 #include "../common/emu.h"
\r
21 #include "../../pico/pico_int.h"
\r
22 #include "../../pico/sound/ym2612.h"
\r
23 #include "../../pico/sound/mix.h"
\r
25 /* we will need some gp2x internals here */
\r
26 extern volatile unsigned short *gp2x_memregs; /* from minimal library rlyeh */
\r
27 extern volatile unsigned long *gp2x_memregl;
\r
29 extern int reset_timing;
\r
30 static unsigned char *shared_mem = 0;
\r
31 static _940_data_t *shared_data = 0;
\r
32 _940_ctl_t *shared_ctl = 0;
\r
33 unsigned char *mp3_mem = 0;
\r
35 #define MP3_SIZE_MAX (0x400000 + 0x800000) // 12M
\r
36 #define CODE940_FILE "pico940_v3.bin"
\r
38 int crashed_940 = 0;
\r
40 static FILE *loaded_mp3 = 0;
\r
42 /***********************************************************/
\r
44 #define MAXOUT (+32767)
\r
45 #define MINOUT (-32768)
\r
48 #define Limit(val, max,min) { \
\r
49 if ( val > max ) val = max; \
\r
50 else if ( val < min ) val = min; \
\r
53 /* these will be managed locally on our side */
\r
54 static UINT8 ST_address; /* address register */
\r
55 static INT32 addr_A1; /* address line A1 */
\r
57 static int writebuff_ptr = 0;
\r
63 /* returns 1 if sample affecting state changed */
\r
64 int YM2612Write_940(unsigned int a, unsigned int v, int scanline)
\r
66 int upd = 1; /* the write affects sample generation */
\r
70 //printf("%05i:%03i: ym w ([%i] %02x)\n", Pico.m.frame_count, Pico.m.scanline, a, v);
\r
74 case 0: /* address port 0 */
\r
75 if (addr_A1 == 0 && ST_address == v)
\r
76 return 0; /* address already selected, don't send this command to 940 */
\r
79 /* don't send DAC or timer related address changes to 940 */
\r
80 if (v == 0x24 || v == 0x25 || v == 0x26 || v == 0x2a)
\r
85 case 2: /* address port 1 */
\r
86 if (addr_A1 == 1 && ST_address == v)
\r
94 //printf("ym pass\n");
\r
96 if (currentConfig.EmuOpt & 4)
\r
98 UINT16 *writebuff = shared_ctl->writebuffsel ? shared_ctl->writebuff0 : shared_ctl->writebuff1;
\r
100 /* detect rapid ym updates */
\r
101 if (upd && !(writebuff_ptr & 0x80000000) && scanline < 224)
\r
103 int mid = Pico.m.pal ? 68 : 93;
\r
104 if (scanline > mid) {
\r
105 //printf("%05i:%03i: rapid ym\n", Pico.m.frame_count, scanline);
\r
106 writebuff[writebuff_ptr++ & 0xffff] = 0xfffe;
\r
107 writebuff_ptr |= 0x80000000;
\r
108 //printf("%05i:%03i: ym w ([%02x] %02x, upd=%i)\n", Pico.m.frame_count, scanline, addr, v, upd);
\r
112 /* queue this write for 940 */
\r
113 if ((writebuff_ptr&0xffff) < 2047) {
\r
114 writebuff[writebuff_ptr++ & 0xffff] = (a<<8)|v;
\r
116 printf("warning: writebuff_ptr > 2047 ([%i] %02x)\n", a, v);
\r
120 return 0; // cause the engine to do updates once per frame only
\r
124 #define CHECK_BUSY(job) \
\r
125 (gp2x_memregs[0x3b46>>1] & (1<<(job-1)))
\r
127 static void wait_busy_940(int job)
\r
132 for (i = 0; (gp2x_memregs[0x3b46>>1] & (1<<job)) && i < 0x10000; i++)
\r
133 spend_cycles(8*1024); // tested to be best for mp3 dec
\r
134 if (i < 0x10000) return;
\r
137 printf("940 crashed (cnt: %i, ve: ", shared_ctl->loopc);
\r
138 for (i = 0; i < 8; i++)
\r
139 printf("%i ", shared_ctl->vstarts[i]);
\r
141 printf("irq pending flags: DUALCPU %04x, SRCPND %08lx (see 26), INTPND %08lx\n",
\r
142 gp2x_memregs[0x3b46>>1], gp2x_memregl[0x4500>>2], gp2x_memregl[0x4510>>2]);
\r
143 printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob);
\r
144 printf("trying to interrupt..\n");
\r
145 gp2x_memregs[0x3B3E>>1] = 0xffff;
\r
146 for (i = 0; gp2x_memregs[0x3b46>>1] && i < 0x10000; i++)
\r
147 spend_cycles(8*1024);
\r
148 printf("i = 0x%x\n", i);
\r
149 printf("irq pending flags: DUALCPU %04x, SRCPND %08lx (see 26), INTPND %08lx\n",
\r
150 gp2x_memregs[0x3b46>>1], gp2x_memregl[0x4500>>2], gp2x_memregl[0x4510>>2]);
\r
151 printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob);
\r
153 me_update_msg("940 crashed, too much overclock?");
\r
154 engineState = PGS_Menu;
\r
159 static void add_job_940(int job)
\r
161 if (job <= 0 || job > 16) {
\r
162 printf("add_job_940: bad job: %i\n", job);
\r
166 // generate interrupt for this job
\r
168 gp2x_memregs[(0x3B20+job*2)>>1] = 1;
\r
170 // printf("added %i, pending %04x\n", job+1, gp2x_memregs[0x3b46>>1]);
\r
174 void YM2612PicoStateLoad_940(void)
\r
176 UINT8 *REGS = YM2612GetRegs();
\r
178 /* make sure JOB940_PICOSTATELOAD gets done before next JOB940_YM2612UPDATEONE */
\r
179 add_job_940(JOB940_PICOSTATELOAD);
\r
180 if (CHECK_BUSY(JOB940_PICOSTATELOAD)) wait_busy_940(JOB940_PICOSTATELOAD);
\r
183 addr_A1 = *(INT32 *) (REGS + 0x200);
\r
186 void YM2612PicoStateSave2_940(int tat, int tbt)
\r
188 UINT8 *ym_remote_regs, *ym_local_regs;
\r
189 add_job_940(JOB940_PICOSTATESAVE2);
\r
190 if (CHECK_BUSY(JOB940_PICOSTATESAVE2)) wait_busy_940(JOB940_PICOSTATESAVE2);
\r
192 ym_remote_regs = (UINT8 *) shared_ctl->writebuff0;
\r
193 ym_local_regs = YM2612GetRegs();
\r
194 if (*(UINT32 *)(ym_remote_regs + 0x100) != 0x41534d59) {
\r
195 printf("code940 didn't return valid save data\n");
\r
199 /* copy addin data only */
\r
200 memcpy(ym_local_regs, ym_remote_regs, 0x20);
\r
201 memcpy(ym_local_regs + 0x100, ym_remote_regs + 0x100, 0x30);
\r
202 memcpy(ym_local_regs + 0x0b8, ym_remote_regs + 0x0b8, 0x48);
\r
203 memcpy(ym_local_regs + 0x1b8, ym_remote_regs + 0x1b8, 0x48);
\r
204 *(INT32 *)(ym_local_regs + 0x108) = tat;
\r
205 *(INT32 *)(ym_local_regs + 0x10c) = tbt;
\r
208 int YM2612PicoStateLoad2_940(int *tat, int *tbt)
\r
210 UINT8 *ym_remote_regs, *ym_local_regs;
\r
211 ym_local_regs = YM2612GetRegs();
\r
212 ym_remote_regs = (UINT8 *) shared_ctl->writebuff0;
\r
214 if (*(UINT32 *)(ym_local_regs + 0x100) != 0x41534d59)
\r
217 *tat = *(INT32 *)(ym_local_regs + 0x108);
\r
218 *tbt = *(INT32 *)(ym_local_regs + 0x10c);
\r
220 if (CHECK_BUSY(JOB940_YM2612UPDATEONE)) wait_busy_940(JOB940_YM2612UPDATEONE);
\r
223 if (shared_ctl->writebuffsel == 1) {
\r
224 shared_ctl->writebuff0[writebuff_ptr & 0xffff] = 0xffff;
\r
226 shared_ctl->writebuff1[writebuff_ptr & 0xffff] = 0xffff;
\r
228 shared_ctl->writebuffsel ^= 1;
\r
230 add_job_940(JOB940_PICOSTATELOAD2_PREP);
\r
231 if (CHECK_BUSY(JOB940_PICOSTATELOAD2_PREP)) wait_busy_940(JOB940_PICOSTATELOAD2_PREP);
\r
233 memcpy(ym_remote_regs, ym_local_regs, 0x200);
\r
235 add_job_940(JOB940_PICOSTATELOAD2);
\r
236 if (CHECK_BUSY(JOB940_PICOSTATELOAD2)) wait_busy_940(JOB940_PICOSTATELOAD2);
\r
242 static void internal_reset(void)
\r
245 ST_address = addr_A1 = -1;
\r
249 /* this must be called after mmu hack, the allocated regions must not get cached */
\r
250 void sharedmem_init(void)
\r
252 if (shared_mem != NULL) return;
\r
254 shared_mem = (unsigned char *) mmap(0, 0x210000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x2000000);
\r
255 if (shared_mem == MAP_FAILED)
\r
257 printf("mmap(shared_data) failed with %i\n", errno);
\r
260 shared_data = (_940_data_t *) (shared_mem+0x100000);
\r
261 /* this area must not get buffered on either side */
\r
262 shared_ctl = (_940_ctl_t *) (shared_mem+0x200000);
\r
263 mp3_mem = (unsigned char *) mmap(0, MP3_SIZE_MAX, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x2400000);
\r
264 if (mp3_mem == MAP_FAILED)
\r
266 printf("mmap(mp3_mem) failed with %i\n", errno);
\r
273 void sharedmem_deinit(void)
\r
275 munmap(shared_mem, 0x210000);
\r
276 munmap(mp3_mem, MP3_SIZE_MAX);
\r
277 shared_mem = mp3_mem = NULL;
\r
278 shared_data = NULL;
\r
283 extern char **g_argv;
\r
285 void YM2612Init_940(int baseclock, int rate)
\r
287 printf("YM2612Init_940()\n");
\r
288 printf("Mem usage: shared_data: %i, shared_ctl: %i\n", sizeof(*shared_data), sizeof(*shared_ctl));
\r
293 gp2x_memregs[0x3B40>>1] = 0; // disable DUALCPU interrupts for 920
\r
294 gp2x_memregs[0x3B42>>1] = 1; // enable DUALCPU interrupts for 940
\r
296 gp2x_memregl[0x4504>>2] = 0; // make sure no FIQs will be generated
\r
297 gp2x_memregl[0x4508>>2] = ~(1<<26); // unmask DUALCPU ints in the undocumented 940's interrupt controller
\r
302 unsigned char ucData[1024];
\r
303 int nRead, i, nLen = 0;
\r
304 char binpath[1024];
\r
307 strncpy(binpath, g_argv[0], 1023);
\r
309 for (i = strlen(binpath); i > 0; i--)
\r
310 if (binpath[i] == '/') { binpath[i] = 0; break; }
\r
311 strcat(binpath, "/" CODE940_FILE);
\r
313 fp = fopen(binpath, "rb");
\r
316 memset(g_screen_ptr, 0, 320*240*2);
\r
317 text_out16(10, 100, "failed to open required file:");
\r
318 text_out16(10, 110, CODE940_FILE);
\r
319 gp2x_video_flip2();
\r
320 printf("failed to open %s\n", binpath);
\r
326 nRead = fread(ucData, 1, 1024, fp);
\r
329 memcpy(shared_mem + nLen, ucData, nRead);
\r
336 memset(shared_data, 0, sizeof(*shared_data));
\r
337 memset(shared_ctl, 0, sizeof(*shared_ctl));
\r
339 /* cause local ym2612 to init REGS */
\r
340 YM2612Init_(baseclock, rate);
\r
346 gp2x_memregs[0x3B46>>1] = 0xffff; // clear pending DUALCPU interrupts for 940
\r
347 gp2x_memregl[0x4500>>2] = 0xffffffff; // clear pending IRQs in SRCPND
\r
348 gp2x_memregl[0x4510>>2] = 0xffffffff; // clear pending IRQs in INTPND
\r
350 /* start the 940 */
\r
354 // YM2612ResetChip_940(); // will be done on JOB940_YM2612INIT
\r
356 /* now cause 940 to init it's ym2612 stuff */
\r
357 shared_ctl->baseclock = baseclock;
\r
358 shared_ctl->rate = rate;
\r
359 add_job_940(JOB940_INITALL);
\r
363 void YM2612ResetChip_940(void)
\r
365 //printf("YM2612ResetChip_940()\n");
\r
366 if (shared_data == NULL) {
\r
367 printf("YM2612ResetChip_940: reset before init?\n");
\r
371 YM2612ResetChip_();
\r
374 add_job_940(JOB940_YM2612RESETCHIP);
\r
378 int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty)
\r
380 int *ym_buf = shared_data->ym_buffer;
\r
383 //printf("YM2612UpdateOne_940()\n");
\r
385 if (CHECK_BUSY(JOB940_YM2612UPDATEONE)) wait_busy_940(JOB940_YM2612UPDATEONE);
\r
387 ym_active_chs = shared_ctl->ym_active_chs;
\r
389 // mix in ym buffer. is_buf_empty means nobody mixed there anything yet and it may contain trash
\r
390 if (is_buf_empty && ym_active_chs) memcpy32(buffer, ym_buf, length<<stereo);
\r
391 else memset32(buffer, 0, length<<stereo);
\r
393 if (shared_ctl->writebuffsel == 1) {
\r
394 shared_ctl->writebuff0[writebuff_ptr & 0xffff] = 0xffff;
\r
396 shared_ctl->writebuff1[writebuff_ptr & 0xffff] = 0xffff;
\r
400 /* predict sample counter for next frame */
\r
401 if (PsndLen_exc_add) {
\r
403 if (PsndLen_exc_cnt + PsndLen_exc_add >= 0x10000) length++;
\r
406 /* give 940 ym job */
\r
407 shared_ctl->writebuffsel ^= 1;
\r
408 shared_ctl->length = length;
\r
409 shared_ctl->stereo = stereo;
\r
411 add_job_940(JOB940_YM2612UPDATEONE);
\r
413 return ym_active_chs;
\r
417 static int mp3_samples_ready = 0, mp3_buffer_offs = 0;
\r
418 static int mp3_play_bufsel = 0, mp3_job_started = 0;
\r
420 void mp3_update(int *buffer, int length, int stereo)
\r
425 // playback was started, track not ended
\r
426 cdda_on = loaded_mp3 && shared_ctl->mp3_offs < shared_ctl->mp3_len;
\r
428 if (!cdda_on) return;
\r
430 if (!(PicoOpt&0x200)) {
\r
431 mp3_update_local(buffer, length, stereo);
\r
435 length_mp3 = length;
\r
436 if (PsndRate == 22050) length_mp3 <<= 1; // mp3s are locked to 44100Hz stereo
\r
437 else if (PsndRate == 11025) length_mp3 <<= 2; // so make length 44100ish
\r
439 /* do we have to wait? */
\r
440 if (mp3_job_started && mp3_samples_ready < length_mp3) {
\r
441 if (CHECK_BUSY(JOB940_MP3DECODE)) wait_busy_940(JOB940_MP3DECODE);
\r
442 mp3_job_started = 0;
\r
443 mp3_samples_ready += 1152;
\r
446 /* mix mp3 data, only stereo */
\r
447 if (mp3_samples_ready >= length_mp3)
\r
450 void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32;
\r
451 if (PsndRate == 22050) { mix_samples = mix_16h_to_32_s1; shr = 1; }
\r
452 else if (PsndRate == 11025) { mix_samples = mix_16h_to_32_s2; shr = 2; }
\r
454 if (1152 - mp3_buffer_offs >= length_mp3) {
\r
455 mix_samples(buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, length<<1);
\r
457 mp3_buffer_offs += length_mp3;
\r
459 // collect samples from both buffers..
\r
460 int left = 1152 - mp3_buffer_offs;
\r
461 if (mp3_play_bufsel == 0)
\r
463 mix_samples(buffer, shared_data->mp3_buffer[0] + mp3_buffer_offs*2, length<<1);
\r
464 mp3_buffer_offs = length_mp3 - left;
\r
465 mp3_play_bufsel = 1;
\r
467 mix_samples(buffer, shared_data->mp3_buffer[1] + mp3_buffer_offs*2, (left>>shr)<<1);
\r
468 mp3_buffer_offs = length_mp3 - left;
\r
469 mix_samples(buffer + ((left>>shr)<<1),
\r
470 shared_data->mp3_buffer[0], (mp3_buffer_offs>>shr)<<1);
\r
471 mp3_play_bufsel = 0;
\r
474 mp3_samples_ready -= length_mp3;
\r
477 // ask to decode more if we already can
\r
478 if (!mp3_job_started)
\r
480 mp3_job_started = 1;
\r
481 shared_ctl->mp3_buffsel ^= 1;
\r
482 add_job_940(JOB940_MP3DECODE);
\r
487 /***********************************************************/
\r
489 void mp3_start_play(FILE *f, int pos) // pos is 0-1023
\r
493 if (!(PicoOpt&0x800)) { // cdda disabled?
\r
497 if (loaded_mp3 != f)
\r
499 if (PicoMessage != NULL)
\r
501 fseek(f, 0, SEEK_END);
\r
502 if (ftell(f) > 2*1024*1024)
\r
503 PicoMessage("Loading MP3...");
\r
505 fseek(f, 0, SEEK_SET);
\r
506 fread(mp3_mem, 1, MP3_SIZE_MAX, f);
\r
507 if (!feof(f)) printf("Warning: mp3 was too large, not all data loaded.\n");
\r
508 shared_ctl->mp3_len = ftell(f);
\r
511 if (PicoOpt & POPT_EXT_FM) {
\r
512 // as we are going to change 940's cacheable area, we must invalidate it's cache..
\r
513 if (CHECK_BUSY(JOB940_MP3DECODE)) wait_busy_940(JOB940_MP3DECODE);
\r
514 add_job_940(JOB940_INVALIDATE_DCACHE);
\r
521 byte_offs = (shared_ctl->mp3_len << 6) >> 10;
\r
525 printf(" mp3 pos1024: %i, byte_offs %i/%i\n", pos, byte_offs, shared_ctl->mp3_len);
\r
527 shared_ctl->mp3_offs = byte_offs;
\r
529 // reset buffer pointers and stuff..
\r
530 mp3_samples_ready = mp3_buffer_offs = mp3_play_bufsel = 0;
\r
531 mp3_job_started = 0;
\r
532 shared_ctl->mp3_buffsel = 1; // will change to 0 on first decode
\r
534 if (PicoOpt & POPT_EXT_FM)
\r
536 add_job_940(JOB940_MP3RESET);
\r
537 if (CHECK_BUSY(JOB940_MP3RESET)) wait_busy_940(JOB940_MP3RESET);
\r
544 int mp3_get_offset(void)
\r
546 unsigned int offs1024 = 0;
\r
549 cdda_on = (PicoAHW & PAHW_MCD) && (PicoOpt&0x800) && !(Pico_mcd->s68k_regs[0x36] & 1) &&
\r
550 (Pico_mcd->scd.Status_CDC & 1) && loaded_mp3;
\r
553 offs1024 = shared_ctl->mp3_offs << 7;
\r
554 offs1024 /= shared_ctl->mp3_len >> 3;
\r
556 printf("offs1024=%u (%i/%i)\n", offs1024, shared_ctl->mp3_offs, shared_ctl->mp3_len);
\r