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
14 #include "soc_mmsp2.h"
\r
16 #include "../common/mp3.h"
\r
17 #include "../common/arm_utils.h"
\r
18 #include "../common/menu.h"
\r
19 #include "../common/emu.h"
\r
20 #include "../common/input.h"
\r
21 #include "../../pico/pico_int.h"
\r
22 #include "../../pico/sound/ym2612.h"
\r
23 #include "../../pico/sound/mix.h"
\r
25 static unsigned char *shared_mem = 0;
\r
26 static _940_data_t *shared_data = 0;
\r
27 _940_ctl_t *shared_ctl = 0;
\r
28 unsigned char *mp3_mem = 0;
\r
30 #define MP3_SIZE_MAX (0x400000 + 0x800000) // 12M
\r
31 #define CODE940_FILE "pico940_v3.bin"
\r
33 int crashed_940 = 0;
\r
35 static FILE *loaded_mp3 = 0;
\r
37 /***********************************************************/
\r
39 #define MAXOUT (+32767)
\r
40 #define MINOUT (-32768)
\r
43 #define Limit(val, max,min) { \
\r
44 if ( val > max ) val = max; \
\r
45 else if ( val < min ) val = min; \
\r
48 /* these will be managed locally on our side */
\r
49 static UINT8 ST_address; /* address register */
\r
50 static INT32 addr_A1; /* address line A1 */
\r
52 static int writebuff_ptr = 0;
\r
58 /* returns 1 if sample affecting state changed */
\r
59 int YM2612Write_940(unsigned int a, unsigned int v, int scanline)
\r
61 int upd = 1; /* the write affects sample generation */
\r
65 //printf("%05i:%03i: ym w ([%i] %02x)\n", Pico.m.frame_count, Pico.m.scanline, a, v);
\r
69 case 0: /* address port 0 */
\r
70 if (addr_A1 == 0 && ST_address == v)
\r
71 return 0; /* address already selected, don't send this command to 940 */
\r
74 /* don't send DAC or timer related address changes to 940 */
\r
75 if (v == 0x24 || v == 0x25 || v == 0x26 || v == 0x2a)
\r
80 case 2: /* address port 1 */
\r
81 if (addr_A1 == 1 && ST_address == v)
\r
89 //printf("ym pass\n");
\r
91 if (currentConfig.EmuOpt & 4)
\r
93 UINT16 *writebuff = shared_ctl->writebuffsel ? shared_ctl->writebuff0 : shared_ctl->writebuff1;
\r
95 /* detect rapid ym updates */
\r
96 if (upd && !(writebuff_ptr & 0x80000000) && scanline < 224)
\r
98 int mid = Pico.m.pal ? 68 : 93;
\r
99 if (scanline > mid) {
\r
100 //printf("%05i:%03i: rapid ym\n", Pico.m.frame_count, scanline);
\r
101 writebuff[writebuff_ptr++ & 0xffff] = 0xfffe;
\r
102 writebuff_ptr |= 0x80000000;
\r
103 //printf("%05i:%03i: ym w ([%02x] %02x, upd=%i)\n", Pico.m.frame_count, scanline, addr, v, upd);
\r
107 /* queue this write for 940 */
\r
108 if ((writebuff_ptr&0xffff) < 2047) {
\r
109 writebuff[writebuff_ptr++ & 0xffff] = (a<<8)|v;
\r
111 printf("warning: writebuff_ptr > 2047 ([%i] %02x)\n", a, v);
\r
115 return 0; // cause the engine to do updates once per frame only
\r
119 #define CHECK_BUSY(job) \
\r
120 (gp2x_memregs[0x3b46>>1] & (1<<(job-1)))
\r
122 static void wait_busy_940(int job)
\r
127 for (i = 0; (gp2x_memregs[0x3b46>>1] & (1<<job)) && i < 0x10000; i++)
\r
128 spend_cycles(8*1024); // tested to be best for mp3 dec
\r
129 if (i < 0x10000) return;
\r
132 printf("940 crashed (cnt: %i, ve: ", shared_ctl->loopc);
\r
133 for (i = 0; i < 8; i++)
\r
134 printf("%i ", shared_ctl->vstarts[i]);
\r
136 printf("irq pending flags: DUALCPU %04x, SRCPND %08lx (see 26), INTPND %08lx\n",
\r
137 gp2x_memregs[0x3b46>>1], gp2x_memregl[0x4500>>2], gp2x_memregl[0x4510>>2]);
\r
138 printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob);
\r
139 printf("trying to interrupt..\n");
\r
140 gp2x_memregs[0x3B3E>>1] = 0xffff;
\r
141 for (i = 0; gp2x_memregs[0x3b46>>1] && i < 0x10000; i++)
\r
142 spend_cycles(8*1024);
\r
143 printf("i = 0x%x\n", i);
\r
144 printf("irq pending flags: DUALCPU %04x, SRCPND %08lx (see 26), INTPND %08lx\n",
\r
145 gp2x_memregs[0x3b46>>1], gp2x_memregl[0x4500>>2], gp2x_memregl[0x4510>>2]);
\r
146 printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob);
\r
148 me_update_msg("940 crashed, too much overclock?");
\r
149 engineState = PGS_Menu;
\r
154 static void add_job_940(int job)
\r
156 if (job <= 0 || job > 16) {
\r
157 printf("add_job_940: bad job: %i\n", job);
\r
161 // generate interrupt for this job
\r
163 gp2x_memregs[(0x3B20+job*2)>>1] = 1;
\r
165 // printf("added %i, pending %04x\n", job+1, gp2x_memregs[0x3b46>>1]);
\r
169 void YM2612PicoStateLoad_940(void)
\r
171 UINT8 *REGS = YM2612GetRegs();
\r
173 /* make sure JOB940_PICOSTATELOAD gets done before next JOB940_YM2612UPDATEONE */
\r
174 add_job_940(JOB940_PICOSTATELOAD);
\r
175 if (CHECK_BUSY(JOB940_PICOSTATELOAD)) wait_busy_940(JOB940_PICOSTATELOAD);
\r
178 addr_A1 = *(INT32 *) (REGS + 0x200);
\r
181 void YM2612PicoStateSave2_940(int tat, int tbt)
\r
183 UINT8 *ym_remote_regs, *ym_local_regs;
\r
184 add_job_940(JOB940_PICOSTATESAVE2);
\r
185 if (CHECK_BUSY(JOB940_PICOSTATESAVE2)) wait_busy_940(JOB940_PICOSTATESAVE2);
\r
187 ym_remote_regs = (UINT8 *) shared_ctl->writebuff0;
\r
188 ym_local_regs = YM2612GetRegs();
\r
189 if (*(UINT32 *)(ym_remote_regs + 0x100) != 0x41534d59) {
\r
190 printf("code940 didn't return valid save data\n");
\r
194 /* copy addin data only */
\r
195 memcpy(ym_local_regs, ym_remote_regs, 0x20);
\r
196 memcpy(ym_local_regs + 0x100, ym_remote_regs + 0x100, 0x30);
\r
197 memcpy(ym_local_regs + 0x0b8, ym_remote_regs + 0x0b8, 0x48);
\r
198 memcpy(ym_local_regs + 0x1b8, ym_remote_regs + 0x1b8, 0x48);
\r
199 *(INT32 *)(ym_local_regs + 0x108) = tat;
\r
200 *(INT32 *)(ym_local_regs + 0x10c) = tbt;
\r
203 int YM2612PicoStateLoad2_940(int *tat, int *tbt)
\r
205 UINT8 *ym_remote_regs, *ym_local_regs;
\r
206 ym_local_regs = YM2612GetRegs();
\r
207 ym_remote_regs = (UINT8 *) shared_ctl->writebuff0;
\r
209 if (*(UINT32 *)(ym_local_regs + 0x100) != 0x41534d59)
\r
212 *tat = *(INT32 *)(ym_local_regs + 0x108);
\r
213 *tbt = *(INT32 *)(ym_local_regs + 0x10c);
\r
215 if (CHECK_BUSY(JOB940_YM2612UPDATEONE)) wait_busy_940(JOB940_YM2612UPDATEONE);
\r
218 if (shared_ctl->writebuffsel == 1) {
\r
219 shared_ctl->writebuff0[writebuff_ptr & 0xffff] = 0xffff;
\r
221 shared_ctl->writebuff1[writebuff_ptr & 0xffff] = 0xffff;
\r
223 shared_ctl->writebuffsel ^= 1;
\r
225 add_job_940(JOB940_PICOSTATELOAD2_PREP);
\r
226 if (CHECK_BUSY(JOB940_PICOSTATELOAD2_PREP)) wait_busy_940(JOB940_PICOSTATELOAD2_PREP);
\r
228 memcpy(ym_remote_regs, ym_local_regs, 0x200);
\r
230 add_job_940(JOB940_PICOSTATELOAD2);
\r
231 if (CHECK_BUSY(JOB940_PICOSTATELOAD2)) wait_busy_940(JOB940_PICOSTATELOAD2);
\r
237 static void internal_reset(void)
\r
240 ST_address = addr_A1 = -1;
\r
244 /* this must be called after mmu hack, the allocated regions must not get cached */
\r
245 void sharedmem940_init(void)
\r
247 if (shared_mem != NULL) return;
\r
249 shared_mem = (unsigned char *) mmap(0, 0x210000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x2000000);
\r
250 if (shared_mem == MAP_FAILED)
\r
252 printf("mmap(shared_data) failed with %i\n", errno);
\r
255 shared_data = (_940_data_t *) (shared_mem+0x100000);
\r
256 /* this area must not get buffered on either side */
\r
257 shared_ctl = (_940_ctl_t *) (shared_mem+0x200000);
\r
258 mp3_mem = (unsigned char *) mmap(0, MP3_SIZE_MAX, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x2400000);
\r
259 if (mp3_mem == MAP_FAILED)
\r
261 printf("mmap(mp3_mem) failed with %i\n", errno);
\r
268 void sharedmem940_finish(void)
\r
270 munmap(shared_mem, 0x210000);
\r
271 munmap(mp3_mem, MP3_SIZE_MAX);
\r
272 shared_mem = mp3_mem = NULL;
\r
273 shared_data = NULL;
\r
278 void YM2612Init_940(int baseclock, int rate)
\r
280 printf("YM2612Init_940()\n");
\r
281 printf("Mem usage: shared_data: %i, shared_ctl: %i\n", sizeof(*shared_data), sizeof(*shared_ctl));
\r
286 gp2x_memregs[0x3B40>>1] = 0; // disable DUALCPU interrupts for 920
\r
287 gp2x_memregs[0x3B42>>1] = 1; // enable DUALCPU interrupts for 940
\r
289 gp2x_memregl[0x4504>>2] = 0; // make sure no FIQs will be generated
\r
290 gp2x_memregl[0x4508>>2] = ~(1<<26); // unmask DUALCPU ints in the undocumented 940's interrupt controller
\r
295 unsigned char ucData[1024];
\r
296 int nRead, nLen = 0;
\r
300 emu_make_path(binpath, CODE940_FILE, sizeof(binpath));
\r
301 fp = fopen(binpath, "rb");
\r
304 memset(g_screen_ptr, 0, 320*240*2);
\r
305 text_out16(10, 100, "failed to open required file:");
\r
306 text_out16(10, 110, CODE940_FILE);
\r
307 gp2x_video_flip2();
\r
308 in_menu_wait(PBTN_MOK|PBTN_MBACK, 100);
\r
309 printf("failed to open %s\n", binpath);
\r
315 nRead = fread(ucData, 1, 1024, fp);
\r
318 memcpy(shared_mem + nLen, ucData, nRead);
\r
325 memset(shared_data, 0, sizeof(*shared_data));
\r
326 memset(shared_ctl, 0, sizeof(*shared_ctl));
\r
328 /* cause local ym2612 to init REGS */
\r
329 YM2612Init_(baseclock, rate);
\r
335 gp2x_memregs[0x3B46>>1] = 0xffff; // clear pending DUALCPU interrupts for 940
\r
336 gp2x_memregl[0x4500>>2] = 0xffffffff; // clear pending IRQs in SRCPND
\r
337 gp2x_memregl[0x4510>>2] = 0xffffffff; // clear pending IRQs in INTPND
\r
339 /* start the 940 */
\r
343 // YM2612ResetChip_940(); // will be done on JOB940_YM2612INIT
\r
345 /* now cause 940 to init it's ym2612 stuff */
\r
346 shared_ctl->baseclock = baseclock;
\r
347 shared_ctl->rate = rate;
\r
348 add_job_940(JOB940_INITALL);
\r
352 void YM2612ResetChip_940(void)
\r
354 //printf("YM2612ResetChip_940()\n");
\r
355 if (shared_data == NULL) {
\r
356 printf("YM2612ResetChip_940: reset before init?\n");
\r
360 YM2612ResetChip_();
\r
363 add_job_940(JOB940_YM2612RESETCHIP);
\r
367 int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty)
\r
369 int *ym_buf = shared_data->ym_buffer;
\r
372 //printf("YM2612UpdateOne_940()\n");
\r
374 if (CHECK_BUSY(JOB940_YM2612UPDATEONE)) wait_busy_940(JOB940_YM2612UPDATEONE);
\r
376 ym_active_chs = shared_ctl->ym_active_chs;
\r
378 // mix in ym buffer. is_buf_empty means nobody mixed there anything yet and it may contain trash
\r
379 if (is_buf_empty && ym_active_chs) memcpy32(buffer, ym_buf, length<<stereo);
\r
380 else memset32(buffer, 0, length<<stereo);
\r
382 if (shared_ctl->writebuffsel == 1) {
\r
383 shared_ctl->writebuff0[writebuff_ptr & 0xffff] = 0xffff;
\r
385 shared_ctl->writebuff1[writebuff_ptr & 0xffff] = 0xffff;
\r
389 /* predict sample counter for next frame */
\r
390 if (PsndLen_exc_add) {
\r
392 if (PsndLen_exc_cnt + PsndLen_exc_add >= 0x10000) length++;
\r
395 /* give 940 ym job */
\r
396 shared_ctl->writebuffsel ^= 1;
\r
397 shared_ctl->length = length;
\r
398 shared_ctl->stereo = stereo;
\r
400 add_job_940(JOB940_YM2612UPDATEONE);
\r
402 return ym_active_chs;
\r
406 /***********************************************************/
\r
408 static int mp3_samples_ready = 0, mp3_buffer_offs = 0;
\r
409 static int mp3_play_bufsel = 0, mp3_job_started = 0;
\r
411 void mp3_update(int *buffer, int length, int stereo)
\r
415 if (!(PicoOpt & POPT_EXT_FM)) {
\r
416 mp3_update_local(buffer, length, stereo);
\r
420 // check if playback was started, track not ended
\r
421 if (loaded_mp3 == NULL || shared_ctl->mp3_offs >= shared_ctl->mp3_len)
\r
424 length_mp3 = length;
\r
425 if (PsndRate == 22050) length_mp3 <<= 1; // mp3s are locked to 44100Hz stereo
\r
426 else if (PsndRate == 11025) length_mp3 <<= 2; // so make length 44100ish
\r
428 /* do we have to wait? */
\r
429 if (mp3_job_started && mp3_samples_ready < length_mp3) {
\r
430 if (CHECK_BUSY(JOB940_MP3DECODE)) wait_busy_940(JOB940_MP3DECODE);
\r
431 mp3_job_started = 0;
\r
432 mp3_samples_ready += 1152;
\r
435 /* mix mp3 data, only stereo */
\r
436 if (mp3_samples_ready >= length_mp3)
\r
439 void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32;
\r
440 if (PsndRate == 22050) { mix_samples = mix_16h_to_32_s1; shr = 1; }
\r
441 else if (PsndRate == 11025) { mix_samples = mix_16h_to_32_s2; shr = 2; }
\r
443 if (1152 - mp3_buffer_offs >= length_mp3) {
\r
444 mix_samples(buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, length<<1);
\r
446 mp3_buffer_offs += length_mp3;
\r
448 // collect samples from both buffers..
\r
449 int left = 1152 - mp3_buffer_offs;
\r
450 if (mp3_play_bufsel == 0)
\r
452 mix_samples(buffer, shared_data->mp3_buffer[0] + mp3_buffer_offs*2, length<<1);
\r
453 mp3_buffer_offs = length_mp3 - left;
\r
454 mp3_play_bufsel = 1;
\r
456 mix_samples(buffer, shared_data->mp3_buffer[1] + mp3_buffer_offs*2, (left>>shr)<<1);
\r
457 mp3_buffer_offs = length_mp3 - left;
\r
458 mix_samples(buffer + ((left>>shr)<<1),
\r
459 shared_data->mp3_buffer[0], (mp3_buffer_offs>>shr)<<1);
\r
460 mp3_play_bufsel = 0;
\r
463 mp3_samples_ready -= length_mp3;
\r
466 // ask to decode more if we already can
\r
467 if (!mp3_job_started)
\r
469 mp3_job_started = 1;
\r
470 shared_ctl->mp3_buffsel ^= 1;
\r
471 add_job_940(JOB940_MP3DECODE);
\r
476 void mp3_start_play(void *f_, int pos) // pos is 0-1023
\r
481 if (!(PicoOpt & POPT_EN_MCD_CDDA) || f == NULL)
\r
484 if (!(PicoOpt & POPT_EXT_FM)) {
\r
485 mp3_start_play_local(f, pos);
\r
489 if (loaded_mp3 != f)
\r
491 if (PicoMessage != NULL)
\r
493 fseek(f, 0, SEEK_END);
\r
494 if (ftell(f) > 2*1024*1024)
\r
495 PicoMessage("Loading MP3...");
\r
497 fseek(f, 0, SEEK_SET);
\r
498 fread(mp3_mem, 1, MP3_SIZE_MAX, f);
\r
499 if (!feof(f)) printf("Warning: mp3 was too large, not all data loaded.\n");
\r
500 shared_ctl->mp3_len = ftell(f);
\r
503 if (PicoOpt & POPT_EXT_FM) {
\r
504 // as we are going to change 940's cacheable area, we must invalidate it's cache..
\r
505 if (CHECK_BUSY(JOB940_MP3DECODE)) wait_busy_940(JOB940_MP3DECODE);
\r
506 add_job_940(JOB940_INVALIDATE_DCACHE);
\r
513 byte_offs = (shared_ctl->mp3_len << 6) >> 10;
\r
517 printf(" mp3 pos1024: %i, byte_offs %i/%i\n", pos, byte_offs, shared_ctl->mp3_len);
\r
519 shared_ctl->mp3_offs = byte_offs;
\r
521 // reset buffer pointers and stuff..
\r
522 mp3_samples_ready = mp3_buffer_offs = mp3_play_bufsel = 0;
\r
523 mp3_job_started = 0;
\r
524 shared_ctl->mp3_buffsel = 1; // will change to 0 on first decode
\r
526 add_job_940(JOB940_MP3RESET);
\r
527 if (CHECK_BUSY(JOB940_MP3RESET)) wait_busy_940(JOB940_MP3RESET);
\r