2 * Code for communication with ARM940 and control of it.
\r
3 * (C) notaz, 2006-2009
\r
5 * This work is licensed under the terms of MAME license.
\r
6 * See COPYING file in the top-level directory.
\r
13 #include <sys/mman.h>
\r
14 #include <sys/ioctl.h>
\r
18 #include "../libpicofe/input.h"
\r
19 #include "../libpicofe/gp2x/soc_mmsp2.h"
\r
20 #include "../libpicofe/gp2x/soc.h"
\r
21 #include "../common/mp3.h"
\r
22 #include "../common/arm_utils.h"
\r
23 #include "../common/menu_pico.h"
\r
24 #include "../common/emu.h"
\r
25 #include "../../pico/pico_int.h"
\r
26 #include "../../pico/sound/ym2612.h"
\r
27 #include "../../pico/sound/mix.h"
\r
28 #include "code940/940shared.h"
\r
32 static unsigned char *shared_mem = 0;
\r
33 static _940_data_t *shared_data = 0;
\r
34 _940_ctl_t *shared_ctl = 0;
\r
35 unsigned char *mp3_mem = 0;
\r
37 #define MP3_SIZE_MAX (0x400000 + 0x800000) // 12M
\r
38 #define CODE940_FILE "pico940_v3.bin"
\r
40 int crashed_940 = 0;
\r
42 static FILE *loaded_mp3 = 0;
\r
44 /***********************************************************/
\r
46 #define MAXOUT (+32767)
\r
47 #define MINOUT (-32768)
\r
50 #define Limit(val, max,min) { \
\r
51 if ( val > max ) val = max; \
\r
52 else if ( val < min ) val = min; \
\r
55 /* these will be managed locally on our side */
\r
56 static UINT8 ST_address; /* address register */
\r
57 static INT32 addr_A1; /* address line A1 */
\r
59 static int writebuff_ptr = 0;
\r
65 /* returns 1 if sample affecting state changed */
\r
66 int YM2612Write_940(unsigned int a, unsigned int v, int scanline)
\r
68 int upd = 1; /* the write affects sample generation */
\r
72 //printf("%05i:%03i: ym w ([%i] %02x)\n", Pico.m.frame_count, Pico.m.scanline, a, v);
\r
76 case 0: /* address port 0 */
\r
77 if (addr_A1 == 0 && ST_address == v)
\r
78 return 0; /* address already selected, don't send this command to 940 */
\r
81 /* don't send DAC or timer related address changes to 940 */
\r
82 if (v == 0x24 || v == 0x25 || v == 0x26 || v == 0x2a)
\r
87 case 2: /* address port 1 */
\r
88 if (addr_A1 == 1 && ST_address == v)
\r
96 //printf("ym pass\n");
\r
98 if (currentConfig.EmuOpt & 4)
\r
100 UINT16 *writebuff = shared_ctl->writebuffsel ? shared_ctl->writebuff0 : shared_ctl->writebuff1;
\r
102 /* detect rapid ym updates */
\r
103 if (upd && !(writebuff_ptr & 0x80000000) && scanline < 224)
\r
105 int mid = Pico.m.pal ? 68 : 93;
\r
106 if (scanline > mid) {
\r
107 //printf("%05i:%03i: rapid ym\n", Pico.m.frame_count, scanline);
\r
108 writebuff[writebuff_ptr++ & 0xffff] = 0xfffe;
\r
109 writebuff_ptr |= 0x80000000;
\r
110 //printf("%05i:%03i: ym w ([%02x] %02x, upd=%i)\n", Pico.m.frame_count, scanline, addr, v, upd);
\r
114 /* queue this write for 940 */
\r
115 if ((writebuff_ptr&0xffff) < 2047) {
\r
116 writebuff[writebuff_ptr++ & 0xffff] = (a<<8)|v;
\r
118 printf("warning: writebuff_ptr > 2047 ([%i] %02x)\n", a, v);
\r
122 return 0; // cause the engine to do updates once per frame only
\r
126 #define CHECK_BUSY(job) \
\r
127 (memregs[0x3b46>>1] & (1<<(job-1)))
\r
129 static void wait_busy_940(int job)
\r
134 for (i = 0; (memregs[0x3b46>>1] & (1<<job)) && i < 0x10000; i++)
\r
135 spend_cycles(8*1024); // tested to be best for mp3 dec
\r
136 if (i < 0x10000) return;
\r
139 printf("940 crashed (cnt: %i, ve: ", shared_ctl->loopc);
\r
140 for (i = 0; i < 8; i++)
\r
141 printf("%i ", shared_ctl->vstarts[i]);
\r
143 printf("irq pending flags: DUALCPU %04x, SRCPND %08x (see 26), INTPND %08x\n",
\r
144 memregs[0x3b46>>1], memregl[0x4500>>2], memregl[0x4510>>2]);
\r
145 printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob);
\r
146 printf("trying to interrupt..\n");
\r
147 memregs[0x3B3E>>1] = 0xffff;
\r
148 for (i = 0; memregs[0x3b46>>1] && i < 0x10000; i++)
\r
149 spend_cycles(8*1024);
\r
150 printf("i = 0x%x\n", i);
\r
151 printf("irq pending flags: DUALCPU %04x, SRCPND %08x (see 26), INTPND %08x\n",
\r
152 memregs[0x3b46>>1], memregl[0x4500>>2], memregl[0x4510>>2]);
\r
153 printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob);
\r
155 menu_update_msg("940 crashed, too much overclock?");
\r
156 engineState = PGS_Menu;
\r
161 static void add_job_940(int job)
\r
163 if (job <= 0 || job > 16) {
\r
164 printf("add_job_940: bad job: %i\n", job);
\r
168 // generate interrupt for this job
\r
170 memregs[(0x3B20+job*2)>>1] = 1;
\r
172 // printf("added %i, pending %04x\n", job+1, memregs[0x3b46>>1]);
\r
176 void YM2612PicoStateLoad_940(void)
\r
178 UINT8 *REGS = YM2612GetRegs();
\r
180 /* make sure JOB940_PICOSTATELOAD gets done before next JOB940_YM2612UPDATEONE */
\r
181 add_job_940(JOB940_PICOSTATELOAD);
\r
182 if (CHECK_BUSY(JOB940_PICOSTATELOAD)) wait_busy_940(JOB940_PICOSTATELOAD);
\r
185 addr_A1 = *(INT32 *) (REGS + 0x200);
\r
188 void YM2612PicoStateSave2_940(int tat, int tbt)
\r
190 UINT8 *ym_remote_regs, *ym_local_regs;
\r
191 add_job_940(JOB940_PICOSTATESAVE2);
\r
192 if (CHECK_BUSY(JOB940_PICOSTATESAVE2)) wait_busy_940(JOB940_PICOSTATESAVE2);
\r
194 ym_remote_regs = (UINT8 *) shared_ctl->writebuff0;
\r
195 ym_local_regs = YM2612GetRegs();
\r
196 if (*(UINT32 *)(ym_remote_regs + 0x100) != 0x41534d59) {
\r
197 printf("code940 didn't return valid save data\n");
\r
201 /* copy addin data only */
\r
202 memcpy(ym_local_regs, ym_remote_regs, 0x20);
\r
203 memcpy(ym_local_regs + 0x100, ym_remote_regs + 0x100, 0x30);
\r
204 memcpy(ym_local_regs + 0x0b8, ym_remote_regs + 0x0b8, 0x48);
\r
205 memcpy(ym_local_regs + 0x1b8, ym_remote_regs + 0x1b8, 0x48);
\r
206 *(INT32 *)(ym_local_regs + 0x108) = tat;
\r
207 *(INT32 *)(ym_local_regs + 0x10c) = tbt;
\r
210 int YM2612PicoStateLoad2_940(int *tat, int *tbt)
\r
212 UINT8 *ym_remote_regs, *ym_local_regs;
\r
213 ym_local_regs = YM2612GetRegs();
\r
214 ym_remote_regs = (UINT8 *) shared_ctl->writebuff0;
\r
216 if (*(UINT32 *)(ym_local_regs + 0x100) != 0x41534d59)
\r
219 *tat = *(INT32 *)(ym_local_regs + 0x108);
\r
220 *tbt = *(INT32 *)(ym_local_regs + 0x10c);
\r
222 if (CHECK_BUSY(JOB940_YM2612UPDATEONE)) wait_busy_940(JOB940_YM2612UPDATEONE);
\r
225 if (shared_ctl->writebuffsel == 1) {
\r
226 shared_ctl->writebuff0[writebuff_ptr & 0xffff] = 0xffff;
\r
228 shared_ctl->writebuff1[writebuff_ptr & 0xffff] = 0xffff;
\r
230 shared_ctl->writebuffsel ^= 1;
\r
232 add_job_940(JOB940_PICOSTATELOAD2_PREP);
\r
233 if (CHECK_BUSY(JOB940_PICOSTATELOAD2_PREP)) wait_busy_940(JOB940_PICOSTATELOAD2_PREP);
\r
235 memcpy(ym_remote_regs, ym_local_regs, 0x200);
\r
237 add_job_940(JOB940_PICOSTATELOAD2);
\r
238 if (CHECK_BUSY(JOB940_PICOSTATELOAD2)) wait_busy_940(JOB940_PICOSTATELOAD2);
\r
244 static void internal_reset(void)
\r
247 ST_address = addr_A1 = -1;
\r
251 /* this must be called after mmu hack, the allocated regions must not get cached */
\r
252 void sharedmem940_init(void)
\r
254 if (shared_mem != NULL) return;
\r
256 shared_mem = (unsigned char *) mmap(0, 0x210000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x2000000);
\r
257 if (shared_mem == MAP_FAILED)
\r
259 printf("mmap(shared_data) failed with %i\n", errno);
\r
262 shared_data = (_940_data_t *) (shared_mem+0x100000);
\r
263 /* this area must not get buffered on either side */
\r
264 shared_ctl = (_940_ctl_t *) (shared_mem+0x200000);
\r
265 mp3_mem = (unsigned char *) mmap(0, MP3_SIZE_MAX, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x2400000);
\r
266 if (mp3_mem == MAP_FAILED)
\r
268 printf("mmap(mp3_mem) failed with %i\n", errno);
\r
275 void sharedmem940_finish(void)
\r
277 munmap(shared_mem, 0x210000);
\r
278 munmap(mp3_mem, MP3_SIZE_MAX);
\r
279 shared_mem = mp3_mem = NULL;
\r
280 shared_data = NULL;
\r
285 void YM2612Init_940(int baseclock, int rate)
\r
287 static int oldrate;
\r
290 if (Pico.m.frame_count > 0 && !crashed_940 && rate == oldrate)
\r
293 printf("YM2612Init_940()\n");
\r
294 printf("Mem usage: shared_data: %i, shared_ctl: %i\n", sizeof(*shared_data), sizeof(*shared_ctl));
\r
299 memregs[0x3B40>>1] = 0; // disable DUALCPU interrupts for 920
\r
300 memregs[0x3B42>>1] = 1; // enable DUALCPU interrupts for 940
\r
302 memregl[0x4504>>2] = 0; // make sure no FIQs will be generated
\r
303 memregl[0x4508>>2] = ~(1<<26); // unmask DUALCPU ints in the undocumented 940's interrupt controller
\r
308 unsigned char ucData[1024];
\r
309 int nRead, nLen = 0;
\r
313 emu_make_path(binpath, CODE940_FILE, sizeof(binpath));
\r
314 fp = fopen(binpath, "rb");
\r
317 memset(g_screen_ptr, 0, 320*240*2);
\r
318 text_out16(10, 100, "failed to open required file:");
\r
319 text_out16(10, 110, CODE940_FILE);
\r
320 gp2x_video_flip2();
\r
321 in_menu_wait(PBTN_MOK|PBTN_MBACK, NULL, 100);
\r
322 printf("failed to open %s\n", binpath);
\r
328 nRead = fread(ucData, 1, 1024, fp);
\r
331 memcpy(shared_mem + nLen, ucData, nRead);
\r
338 memset(shared_data, 0, sizeof(*shared_data));
\r
339 memset(shared_ctl, 0, sizeof(*shared_ctl));
\r
341 /* cause local ym2612 to init REGS */
\r
342 YM2612Init_(baseclock, rate);
\r
348 memregs[0x3B46>>1] = 0xffff; // clear pending DUALCPU interrupts for 940
\r
349 memregl[0x4500>>2] = 0xffffffff; // clear pending IRQs in SRCPND
\r
350 memregl[0x4510>>2] = 0xffffffff; // clear pending IRQs in INTPND
\r
352 /* start the 940 */
\r
356 // YM2612ResetChip_940(); // will be done on JOB940_YM2612INIT
\r
358 /* now cause 940 to init it's ym2612 stuff */
\r
359 shared_ctl->baseclock = baseclock;
\r
360 shared_ctl->rate = rate;
\r
361 add_job_940(JOB940_INITALL);
\r
367 void YM2612ResetChip_940(void)
\r
369 //printf("YM2612ResetChip_940()\n");
\r
370 if (shared_data == NULL) {
\r
371 printf("YM2612ResetChip_940: reset before init?\n");
\r
375 YM2612ResetChip_();
\r
378 add_job_940(JOB940_YM2612RESETCHIP);
\r
382 int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty)
\r
384 int *ym_buf = shared_data->ym_buffer;
\r
387 //printf("YM2612UpdateOne_940()\n");
\r
389 if (CHECK_BUSY(JOB940_YM2612UPDATEONE)) wait_busy_940(JOB940_YM2612UPDATEONE);
\r
391 ym_active_chs = shared_ctl->ym_active_chs;
\r
393 // mix in ym buffer. is_buf_empty means nobody mixed there anything yet and it may contain trash
\r
394 if (is_buf_empty && ym_active_chs) memcpy32(buffer, ym_buf, length<<stereo);
\r
395 else memset32(buffer, 0, length<<stereo);
\r
397 if (shared_ctl->writebuffsel == 1) {
\r
398 shared_ctl->writebuff0[writebuff_ptr & 0xffff] = 0xffff;
\r
400 shared_ctl->writebuff1[writebuff_ptr & 0xffff] = 0xffff;
\r
404 /* predict sample counter for next frame */
\r
405 if (PsndLen_exc_add) {
\r
407 if (PsndLen_exc_cnt + PsndLen_exc_add >= 0x10000) length++;
\r
410 /* give 940 ym job */
\r
411 shared_ctl->writebuffsel ^= 1;
\r
412 shared_ctl->length = length;
\r
413 shared_ctl->stereo = stereo;
\r
415 add_job_940(JOB940_YM2612UPDATEONE);
\r
417 return ym_active_chs;
\r
421 /***********************************************************/
\r
423 // FIXME: double buffering no longer used..
\r
425 int mp3dec_decode(FILE *f, int *file_pos, int file_len)
\r
427 if (!(PicoOpt & POPT_EXT_FM)) {
\r
428 //mp3_update_local(buffer, length, stereo);
\r
432 // check if playback was started, track not ended
\r
433 if (loaded_mp3 == NULL || shared_ctl->mp3_offs >= shared_ctl->mp3_len) {
\r
434 *file_pos = file_len;
\r
438 /* do we have to wait? */
\r
439 if (CHECK_BUSY(JOB940_MP3DECODE))
\r
440 wait_busy_940(JOB940_MP3DECODE);
\r
442 memcpy(cdda_out_buffer,
\r
443 shared_data->mp3_buffer[shared_ctl->mp3_buffsel],
\r
444 sizeof(cdda_out_buffer));
\r
446 *file_pos = shared_ctl->mp3_offs;
\r
448 if (shared_ctl->mp3_offs < shared_ctl->mp3_len) {
\r
449 // ask to decode more
\r
450 //shared_ctl->mp3_buffsel ^= 1;
\r
451 add_job_940(JOB940_MP3DECODE);
\r
457 int mp3dec_start(FILE *f, int fpos_start)
\r
459 if (!(PicoOpt & POPT_EXT_FM)) {
\r
460 //mp3_start_play_local(f, pos);
\r
464 if (loaded_mp3 != f)
\r
466 if (PicoMessage != NULL)
\r
468 fseek(f, 0, SEEK_END);
\r
469 if (ftell(f) > 2*1024*1024)
\r
470 PicoMessage("Loading MP3...");
\r
472 fseek(f, 0, SEEK_SET);
\r
473 fread(mp3_mem, 1, MP3_SIZE_MAX, f);
\r
475 printf("Warning: mp3 was too large, not all data loaded.\n");
\r
476 shared_ctl->mp3_len = ftell(f);
\r
479 // as we are going to change 940's cacheable area,
\r
480 // we must invalidate it's cache..
\r
481 if (CHECK_BUSY(JOB940_MP3DECODE))
\r
482 wait_busy_940(JOB940_MP3DECODE);
\r
483 add_job_940(JOB940_INVALIDATE_DCACHE);
\r
487 shared_ctl->mp3_offs = fpos_start;
\r
488 shared_ctl->mp3_buffsel = 0;
\r
490 add_job_940(JOB940_MP3RESET);
\r
491 if (CHECK_BUSY(JOB940_MP3RESET))
\r
492 wait_busy_940(JOB940_MP3RESET);
\r
494 // because we decode ahea, need to start now
\r
495 if (shared_ctl->mp3_offs < shared_ctl->mp3_len) {
\r
496 add_job_940(JOB940_MP3DECODE);
\r