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 "asmutils.h"
\r
19 #include "../../Pico/PicoInt.h"
\r
20 #include "../../Pico/sound/mix.h"
\r
22 /* we will need some gp2x internals here */
\r
23 extern volatile unsigned short *gp2x_memregs; /* from minimal library rlyeh */
\r
24 extern volatile unsigned long *gp2x_memregl;
\r
26 extern int reset_timing;
\r
27 static unsigned char *shared_mem = 0;
\r
28 static _940_data_t *shared_data = 0;
\r
29 _940_ctl_t *shared_ctl = 0;
\r
30 unsigned char *mp3_mem = 0;
\r
32 #define MP3_SIZE_MAX (0x1000000 - 4*640*480)
\r
34 int crashed_940 = 0;
\r
36 static FILE *loaded_mp3 = 0;
\r
38 /***********************************************************/
\r
40 #define MAXOUT (+32767)
\r
41 #define MINOUT (-32768)
\r
44 #define Limit(val, max,min) { \
\r
45 if ( val > max ) val = max; \
\r
46 else if ( val < min ) val = min; \
\r
49 /* these will be managed locally on our side */
\r
50 extern int *ym2612_dacen;
\r
51 extern INT32 *ym2612_dacout;
\r
52 extern void *ym2612_regs;
\r
54 static UINT8 *REGS = 0; /* we will also keep local copy of regs for savestates and such */
\r
55 static INT32 addr_A1; /* address line A1 */
\r
57 static INT32 dacout;
\r
58 static UINT8 ST_address; /* address register */
\r
59 static UINT8 ST_status; /* status flag */
\r
60 static UINT8 ST_mode; /* mode CSM / 3SLOT */
\r
61 static int ST_TA; /* timer a */
\r
62 static int ST_TAC; /* timer a maxval */
\r
63 static int ST_TAT; /* timer a ticker */
\r
64 static UINT8 ST_TB; /* timer b */
\r
65 static int ST_TBC; /* timer b maxval */
\r
66 static int ST_TBT; /* timer b ticker */
\r
68 static int writebuff_ptr = 0;
\r
71 /* OPN Mode Register Write */
\r
72 static int set_timers( int v )
\r
77 /* b6 = 3 slot mode */
\r
80 /* b3 = timer enable b */
\r
81 /* b2 = timer enable a */
\r
84 change = (ST_mode ^ v) & 0xc0;
\r
87 /* reset Timer b flag */
\r
91 /* reset Timer a flag */
\r
101 /* returns 1 if sample affecting state changed */
\r
102 int YM2612Write_940(unsigned int a, unsigned int v)
\r
105 int upd = 1; /* the write affects sample generation */
\r
107 v &= 0xff; /* adjust to 8 bit bus */
\r
110 //printf("%05i:%03i: ym w ([%i] %02x)\n", Pico.m.frame_count, Pico.m.scanline, a, v);
\r
113 case 0: /* address port 0 */
\r
114 if (!addr_A1 && ST_address == v)
\r
115 return 0; /* address already selected, don't send this command to 940 */
\r
117 /* don't send DAC or timer related address changes to 940 */
\r
118 if (!addr_A1 && (v & 0xf0) == 0x20 &&
\r
119 (v == 0x24 || v == 0x25 || v == 0x26 || v == 0x2a))
\r
125 case 1: /* data port 0 */
\r
126 if (addr_A1 != 0) {
\r
127 return 0; /* verified on real YM2608 */
\r
133 switch( addr & 0xf0 )
\r
135 case 0x20: /* 0x20-0x2f Mode */
\r
138 case 0x24: { // timer A High 8
\r
139 int TAnew = (ST_TA & 0x03)|(((int)v)<<2);
\r
140 if(ST_TA != TAnew) {
\r
141 // we should reset ticker only if new value is written. Outrun requires this.
\r
143 ST_TAC = (1024-TAnew)*18;
\r
148 case 0x25: { // timer A Low 2
\r
149 int TAnew = (ST_TA & 0x3fc)|(v&3);
\r
150 if(ST_TA != TAnew) {
\r
152 ST_TAC = (1024-TAnew)*18;
\r
157 case 0x26: // timer B
\r
160 ST_TBC = (256-v)<<4;
\r
165 case 0x27: /* mode, timer control */
\r
166 if (set_timers( v ))
\r
167 break; // other side needs ST.mode for 3slot mode
\r
169 case 0x2a: /* DAC data (YM2612) */
\r
170 dacout = ((int)v - 0x80) << 6; /* level unknown (notaz: 8 seems to be too much) */
\r
172 case 0x2b: /* DAC Sel (YM2612) */
\r
173 /* b7 = dac enable */
\r
176 break; // other side has to know this
\r
184 case 2: /* address port 1 */
\r
185 if (addr_A1 && ST_address == v)
\r
192 case 3: /* data port 1 */
\r
193 if (addr_A1 != 1) {
\r
194 return 0; /* verified on real YM2608 */
\r
197 addr = ST_address | 0x100;
\r
202 //printf("ym pass\n");
\r
204 if(currentConfig.EmuOpt & 4) {
\r
205 UINT16 *writebuff = shared_ctl->writebuffsel ? shared_ctl->writebuff0 : shared_ctl->writebuff1;
\r
207 /* detect rapid ym updates */
\r
208 if (upd && !(writebuff_ptr & 0x80000000) && Pico.m.scanline < 224) {
\r
209 int mid = Pico.m.pal ? 68 : 93;
\r
210 if (Pico.m.scanline > mid) {
\r
211 //printf("%05i:%03i: rapid ym\n", Pico.m.frame_count, Pico.m.scanline);
\r
212 writebuff[writebuff_ptr++ & 0xffff] = 0xfffe;
\r
213 writebuff_ptr |= 0x80000000;
\r
214 //printf("%05i:%03i: ym w ([%02x] %02x, upd=%i)\n", Pico.m.frame_count, Pico.m.scanline, addr, v, upd);
\r
218 /* queue this write for 940 */
\r
219 if ((writebuff_ptr&0xffff) < 2047) {
\r
220 writebuff[writebuff_ptr++ & 0xffff] = (a<<8)|v;
\r
222 printf("warning: writebuff_ptr > 2047 ([%i] %02x)\n", a, v);
\r
226 return 0; // cause the engine to do updates once per frame only
\r
229 UINT8 YM2612Read_940(void)
\r
235 int YM2612PicoTick_940(int n)
\r
240 if(ST_mode & 0x01 && (ST_TAT+=64*n) >= ST_TAC) {
\r
242 if(ST_mode & 0x04) ST_status |= 1;
\r
243 // CSM mode total level latch and auto key on
\r
245 if(ST_mode & 0x80) {
\r
246 CSMKeyControll( &(ym2612_940->CH[2]) ); // Vectorman2, etc.
\r
253 if(ST_mode & 0x02 && (ST_TBT+=64*n) >= ST_TBC) {
\r
255 if(ST_mode & 0x08) ST_status |= 2;
\r
262 #define CHECK_BUSY(job) \
\r
263 (gp2x_memregs[0x3b46>>1] & (1<<(job-1)))
\r
265 static void wait_busy_940(int job)
\r
270 for (i = 0; (gp2x_memregs[0x3b46>>1] & (1<<job)) && i < 0x10000; i++)
\r
271 spend_cycles(8*1024); // tested to be best for mp3 dec
\r
272 if (i < 0x10000) return;
\r
275 printf("940 crashed (cnt: %i, ve: ", shared_ctl->loopc);
\r
276 for (i = 0; i < 8; i++)
\r
277 printf("%i ", shared_ctl->vstarts[i]);
\r
279 printf("irq pending flags: DUALCPU %04x, SRCPND %08lx (see 26), INTPND %08lx\n",
\r
280 gp2x_memregs[0x3b46>>1], gp2x_memregl[0x4500>>2], gp2x_memregl[0x4510>>2]);
\r
281 printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob);
\r
282 printf("trying to interrupt..\n");
\r
283 gp2x_memregs[0x3B3E>>1] = 0xffff;
\r
284 for (i = 0; gp2x_memregs[0x3b46>>1] && i < 0x10000; i++)
\r
285 spend_cycles(8*1024);
\r
286 printf("i = 0x%x\n", i);
\r
287 printf("irq pending flags: DUALCPU %04x, SRCPND %08lx (see 26), INTPND %08lx\n",
\r
288 gp2x_memregs[0x3b46>>1], gp2x_memregl[0x4500>>2], gp2x_memregl[0x4510>>2]);
\r
289 printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob);
\r
291 strcpy(menuErrorMsg, "940 crashed, too much overclock?");
\r
292 engineState = PGS_Menu;
\r
297 static void add_job_940(int job)
\r
299 if (job <= 0 || job > 16) {
\r
300 printf("add_job_940: bad job: %i\n", job);
\r
304 // generate interrupt for this job
\r
306 gp2x_memregs[(0x3B20+job*2)>>1] = 1;
\r
308 // printf("added %i, pending %04x\n", job+1, gp2x_memregs[0x3b46>>1]);
\r
312 void YM2612PicoStateLoad_940(void)
\r
314 int i, old_A1 = addr_A1;
\r
316 // feed all the registers and update internal state
\r
317 for(i = 0; i < 0x100; i++) {
\r
318 YM2612Write_940(0, i);
\r
319 YM2612Write_940(1, REGS[i]);
\r
321 for(i = 0; i < 0x100; i++) {
\r
322 YM2612Write_940(2, i);
\r
323 YM2612Write_940(3, REGS[i|0x100]);
\r
328 // add_job_940(JOB940_PICOSTATELOAD);
\r
332 static void internal_reset(void)
\r
336 ST_status = 0; /* normal mode */
\r
345 /* this must be called after mmu hack, the allocated regions must not get cached */
\r
346 void sharedmem_init(void)
\r
348 if (shared_mem != NULL) return;
\r
350 shared_mem = (unsigned char *) mmap(0, 0x210000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x2000000);
\r
351 if(shared_mem == MAP_FAILED)
\r
353 printf("mmap(shared_data) failed with %i\n", errno);
\r
356 shared_data = (_940_data_t *) (shared_mem+0x100000);
\r
357 /* this area must not get buffered on either side */
\r
358 shared_ctl = (_940_ctl_t *) (shared_mem+0x200000);
\r
359 mp3_mem = (unsigned char *) mmap(0, MP3_SIZE_MAX, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x3000000);
\r
360 if (mp3_mem == MAP_FAILED)
\r
362 printf("mmap(mp3_mem) failed with %i\n", errno);
\r
369 void sharedmem_deinit(void)
\r
371 munmap(shared_mem, 0x210000);
\r
372 munmap(mp3_mem, MP3_SIZE_MAX);
\r
373 shared_mem = mp3_mem = NULL;
\r
374 shared_data = NULL;
\r
379 extern char **g_argv;
\r
381 /* none of the functions in this file should be called before this one */
\r
382 void YM2612Init_940(int baseclock, int rate)
\r
384 printf("YM2612Init_940()\n");
\r
385 printf("Mem usage: shared_data: %i, shared_ctl: %i\n", sizeof(*shared_data), sizeof(*shared_ctl));
\r
390 gp2x_memregs[0x3B40>>1] = 0; // disable DUALCPU interrupts for 920
\r
391 gp2x_memregs[0x3B42>>1] = 1; // enable DUALCPU interrupts for 940
\r
393 gp2x_memregl[0x4504>>2] = 0; // make sure no FIQs will be generated
\r
394 gp2x_memregl[0x4508>>2] = ~(1<<26); // unmask DUALCPU ints in the undocumented 940's interrupt controller
\r
399 unsigned char ucData[1024];
\r
400 int nRead, i, nLen = 0;
\r
401 char binpath[1024];
\r
404 strncpy(binpath, g_argv[0], 1023);
\r
406 for (i = strlen(binpath); i > 0; i--)
\r
407 if (binpath[i] == '/') { binpath[i] = 0; break; }
\r
408 strcat(binpath, "/code940.bin");
\r
410 fp = fopen(binpath, "rb");
\r
413 memset(gp2x_screen, 0, 320*240);
\r
414 gp2x_text_out8(10, 100, "failed to open required file:");
\r
415 gp2x_text_out8(10, 110, "code940.bin");
\r
417 printf("failed to open %s\n", binpath);
\r
423 nRead = fread(ucData, 1, 1024, fp);
\r
426 memcpy(shared_mem + nLen, ucData, nRead);
\r
433 memset(shared_data, 0, sizeof(*shared_data));
\r
434 memset(shared_ctl, 0, sizeof(*shared_ctl));
\r
436 REGS = YM2612GetRegs();
\r
438 ym2612_dacen = &dacen;
\r
439 ym2612_dacout = &dacout;
\r
445 gp2x_memregs[0x3B46>>1] = 0xffff; // clear pending DUALCPU interrupts for 940
\r
446 gp2x_memregl[0x4500>>2] = 0xffffffff; // clear pending IRQs in SRCPND
\r
447 gp2x_memregl[0x4510>>2] = 0xffffffff; // clear pending IRQs in INTPND
\r
449 /* start the 940 */
\r
453 // YM2612ResetChip_940(); // will be done on JOB940_YM2612INIT
\r
455 /* now cause 940 to init it's ym2612 stuff */
\r
456 shared_ctl->baseclock = baseclock;
\r
457 shared_ctl->rate = rate;
\r
458 add_job_940(JOB940_INITALL);
\r
462 void YM2612ResetChip_940(void)
\r
464 printf("YM2612ResetChip_940()\n");
\r
465 if (shared_data == NULL) {
\r
466 printf("YM2612ResetChip_940: reset before init?\n");
\r
472 add_job_940(JOB940_YM2612RESETCHIP);
\r
476 int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty)
\r
478 int *ym_buf = shared_data->ym_buffer;
\r
481 //printf("YM2612UpdateOne_940()\n");
\r
483 if (CHECK_BUSY(JOB940_YM2612UPDATEONE)) wait_busy_940(JOB940_YM2612UPDATEONE);
\r
485 ym_active_chs = shared_ctl->ym_active_chs;
\r
487 // mix in ym buffer. is_buf_empty means nobody mixed there anything yet and it may contain trash
\r
488 if (is_buf_empty && ym_active_chs) memcpy32(buffer, ym_buf, length<<stereo);
\r
489 else memset32(buffer, 0, length<<stereo);
\r
491 if (shared_ctl->writebuffsel == 1) {
\r
492 shared_ctl->writebuff0[writebuff_ptr & 0xffff] = 0xffff;
\r
494 shared_ctl->writebuff1[writebuff_ptr & 0xffff] = 0xffff;
\r
498 /* predict sample counter for next frame */
\r
499 if (PsndLen_exc_add) {
\r
500 if (PsndLen_exc_cnt + PsndLen_exc_add >= 0x10000) length = PsndLen + 1;
\r
501 else length = PsndLen;
\r
504 /* give 940 ym job */
\r
505 shared_ctl->writebuffsel ^= 1;
\r
506 shared_ctl->length = length;
\r
507 shared_ctl->stereo = stereo;
\r
509 add_job_940(JOB940_YM2612UPDATEONE);
\r
511 return ym_active_chs;
\r
515 static int mp3_samples_ready = 0, mp3_buffer_offs = 0;
\r
516 static int mp3_play_bufsel = 0, mp3_job_started = 0;
\r
518 void mp3_update(int *buffer, int length, int stereo)
\r
523 // not data track, CDC is reading, playback was started, track not ended
\r
524 cdda_on = !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1) &&
\r
525 loaded_mp3 && shared_ctl->mp3_offs < shared_ctl->mp3_len;
\r
527 if (!cdda_on) return;
\r
529 if (!(PicoOpt&0x200)) {
\r
530 mp3_update_local(buffer, length, stereo);
\r
534 length_mp3 = length;
\r
535 if (PsndRate == 22050) length_mp3 <<= 1; // mp3s are locked to 44100Hz stereo
\r
536 else if (PsndRate == 11025) length_mp3 <<= 2; // so make length 44100ish
\r
538 /* do we have to wait? */
\r
539 if (mp3_job_started && mp3_samples_ready < length_mp3) {
\r
540 if (CHECK_BUSY(JOB940_MP3DECODE)) wait_busy_940(JOB940_MP3DECODE);
\r
541 mp3_job_started = 0;
\r
542 mp3_samples_ready += 1152;
\r
545 /* mix mp3 data, only stereo */
\r
546 if (mp3_samples_ready >= length_mp3)
\r
549 void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32;
\r
550 if (PsndRate == 22050) { mix_samples = mix_16h_to_32_s1; shr = 1; }
\r
551 else if (PsndRate == 11025) { mix_samples = mix_16h_to_32_s2; shr = 2; }
\r
553 if (1152 - mp3_buffer_offs >= length_mp3) {
\r
554 mix_samples(buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, length<<1);
\r
556 mp3_buffer_offs += length_mp3;
\r
558 // collect samples from both buffers..
\r
559 int left = 1152 - mp3_buffer_offs;
\r
560 if (mp3_play_bufsel == 0)
\r
562 mix_samples(buffer, shared_data->mp3_buffer[0] + mp3_buffer_offs*2, length<<1);
\r
563 mp3_buffer_offs = length_mp3 - left;
\r
564 mp3_play_bufsel = 1;
\r
566 mix_samples(buffer, shared_data->mp3_buffer[1] + mp3_buffer_offs*2, (left>>shr)<<1);
\r
567 mp3_buffer_offs = length_mp3 - left;
\r
568 mix_samples(buffer + ((left>>shr)<<1),
\r
569 shared_data->mp3_buffer[0], (mp3_buffer_offs>>shr)<<1);
\r
570 mp3_play_bufsel = 0;
\r
573 mp3_samples_ready -= length_mp3;
\r
576 // ask to decode more if we already can
\r
577 if (!mp3_job_started)
\r
579 mp3_job_started = 1;
\r
580 shared_ctl->mp3_buffsel ^= 1;
\r
581 add_job_940(JOB940_MP3DECODE);
\r
586 /***********************************************************/
\r
588 void mp3_start_play(FILE *f, int pos) // pos is 0-1023
\r
592 if (!(PicoOpt&0x800)) { // cdda disabled?
\r
596 if (loaded_mp3 != f)
\r
598 // printf("loading mp3... "); fflush(stdout);
\r
599 if (PicoMessage != NULL)
\r
601 fseek(f, 0, SEEK_END);
\r
602 if (ftell(f) > 2*1024*1024)
\r
603 PicoMessage("Loading MP3...");
\r
605 fseek(f, 0, SEEK_SET);
\r
606 fread(mp3_mem, 1, MP3_SIZE_MAX, f);
\r
607 // if (feof(f)) printf("done.\n");
\r
608 // else printf("done. mp3 too large, not all data loaded.\n");
\r
609 shared_ctl->mp3_len = ftell(f);
\r
612 if (PicoOpt&0x200) {
\r
613 // as we are going to change 940's cacheable area, we must invalidate it's cache..
\r
614 if (CHECK_BUSY(JOB940_MP3DECODE)) wait_busy_940(JOB940_MP3DECODE);
\r
615 add_job_940(JOB940_INVALIDATE_DCACHE);
\r
622 byte_offs = (shared_ctl->mp3_len << 6) >> 10;
\r
626 // printf("mp3 pos1024: %i, byte_offs %i/%i\n", pos, byte_offs, shared_ctl->mp3_len);
\r
628 shared_ctl->mp3_offs = byte_offs;
\r
630 // reset buffer pointers and stuff..
\r
631 mp3_samples_ready = mp3_buffer_offs = mp3_play_bufsel = 0;
\r
632 mp3_job_started = 0;
\r
633 shared_ctl->mp3_buffsel = 1; // will change to 0 on first decode
\r
635 if (!(PicoOpt&0x200)) mp3_start_local();
\r
639 int mp3_get_offset(void)
\r
644 cdda_on = (PicoMCD & 1) && (PicoOpt&0x800) && !(Pico_mcd->s68k_regs[0x36] & 1) &&
\r
645 (Pico_mcd->scd.Status_CDC & 1) && loaded_mp3;
\r
648 offs1024 = shared_ctl->mp3_offs << 7;
\r
649 offs1024 /= shared_ctl->mp3_len;
\r
652 printf("offs1024=%i (%i/%i)\n", offs1024, shared_ctl->mp3_offs, shared_ctl->mp3_len);
\r