(MSVC) Rename pico/pico/pico.c and pico/pico/memory.c
[picodrive.git] / platform / gp2x / 940ctl.c
CommitLineData
cff531af 1/*\r
2 * Code for communication with ARM940 and control of it.\r
3 * (C) notaz, 2006-2009\r
4 *\r
5 * This work is licensed under the terms of MAME license.\r
6 * See COPYING file in the top-level directory.\r
7 */\r
6cadc2da 8\r
cc68a136 9#include <stdio.h>\r
10#include <stdlib.h>\r
11#include <string.h>\r
12#include <unistd.h>\r
13#include <sys/mman.h>\r
14#include <sys/ioctl.h>\r
15#include <fcntl.h>\r
16#include <errno.h>\r
17\r
75a30842 18#include "../libpicofe/input.h"\r
19#include "../libpicofe/gp2x/soc_mmsp2.h"\r
20#include "../libpicofe/gp2x/soc.h"\r
da42200b 21#include "../common/mp3.h"\r
e5f426aa 22#include "../common/arm_utils.h"\r
75a30842 23#include "../common/menu_pico.h"\r
ea8c405f 24#include "../common/emu.h"\r
efcba75f 25#include "../../pico/pico_int.h"\r
26#include "../../pico/sound/ym2612.h"\r
27#include "../../pico/sound/mix.h"\r
75a30842 28#include "code940/940shared.h"\r
29#include "plat.h"\r
30#include "940ctl.h"\r
4f265db7 31\r
cc68a136 32static unsigned char *shared_mem = 0;\r
33static _940_data_t *shared_data = 0;\r
abe0ea43 34_940_ctl_t *shared_ctl = 0;\r
35unsigned char *mp3_mem = 0;\r
cb0316e4 36\r
e362c573 37#define MP3_SIZE_MAX (0x400000 + 0x800000) // 12M\r
e4fb433c 38#define CODE940_FILE "pico940_v3.bin"\r
cc68a136 39\r
40int crashed_940 = 0;\r
41\r
51a902ae 42static FILE *loaded_mp3 = 0;\r
cc68a136 43\r
44/***********************************************************/\r
45\r
46#define MAXOUT (+32767)\r
47#define MINOUT (-32768)\r
48\r
49/* limitter */\r
50#define Limit(val, max,min) { \\r
51 if ( val > max ) val = max; \\r
52 else if ( val < min ) val = min; \\r
53}\r
54\r
55/* these will be managed locally on our side */\r
cc68a136 56static UINT8 ST_address; /* address register */\r
4b9c5888 57static INT32 addr_A1; /* address line A1 */\r
cc68a136 58\r
59static int writebuff_ptr = 0;\r
60\r
61\r
cc68a136 62/* YM2612 write */\r
63/* a = address */\r
64/* v = value */\r
65/* returns 1 if sample affecting state changed */\r
4b9c5888 66int YM2612Write_940(unsigned int a, unsigned int v, int scanline)\r
cc68a136 67{\r
2433f409 68 int upd = 1; /* the write affects sample generation */\r
cc68a136 69\r
cc68a136 70 a &= 3;\r
71\r
2433f409 72 //printf("%05i:%03i: ym w ([%i] %02x)\n", Pico.m.frame_count, Pico.m.scanline, a, v);\r
73\r
e4fb433c 74 switch (a)\r
75 {\r
4b9c5888 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
79 ST_address = v;\r
80 addr_A1 = 0;\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
4ff2d527 83 return 0;\r
4b9c5888 84 upd = 0;\r
85 break;\r
cc68a136 86\r
4b9c5888 87 case 2: /* address port 1 */\r
88 if (addr_A1 == 1 && ST_address == v)\r
cc68a136 89 return 0;\r
4b9c5888 90 ST_address = v;\r
91 addr_A1 = 1;\r
92 upd = 0;\r
cc68a136 93 break;\r
cc68a136 94 }\r
95\r
2433f409 96 //printf("ym pass\n");\r
97\r
4b9c5888 98 if (currentConfig.EmuOpt & 4)\r
99 {\r
2433f409 100 UINT16 *writebuff = shared_ctl->writebuffsel ? shared_ctl->writebuff0 : shared_ctl->writebuff1;\r
101\r
102 /* detect rapid ym updates */\r
4b9c5888 103 if (upd && !(writebuff_ptr & 0x80000000) && scanline < 224)\r
104 {\r
2433f409 105 int mid = Pico.m.pal ? 68 : 93;\r
4b9c5888 106 if (scanline > mid) {\r
107 //printf("%05i:%03i: rapid ym\n", Pico.m.frame_count, scanline);\r
2433f409 108 writebuff[writebuff_ptr++ & 0xffff] = 0xfffe;\r
109 writebuff_ptr |= 0x80000000;\r
4b9c5888 110 //printf("%05i:%03i: ym w ([%02x] %02x, upd=%i)\n", Pico.m.frame_count, scanline, addr, v, upd);\r
cc68a136 111 }\r
2433f409 112 }\r
113\r
114 /* queue this write for 940 */\r
115 if ((writebuff_ptr&0xffff) < 2047) {\r
116 writebuff[writebuff_ptr++ & 0xffff] = (a<<8)|v;\r
cc68a136 117 } else {\r
4ff2d527 118 printf("warning: writebuff_ptr > 2047 ([%i] %02x)\n", a, v);\r
cc68a136 119 }\r
120 }\r
121\r
122 return 0; // cause the engine to do updates once per frame only\r
123}\r
124\r
cc68a136 125\r
abe0ea43 126#define CHECK_BUSY(job) \\r
75a30842 127 (memregs[0x3b46>>1] & (1<<(job-1)))\r
abe0ea43 128\r
129static void wait_busy_940(int job)\r
cc68a136 130{\r
131 int i;\r
cc68a136 132\r
abe0ea43 133 job--;\r
75a30842 134 for (i = 0; (memregs[0x3b46>>1] & (1<<job)) && i < 0x10000; i++)\r
42c7b147 135 spend_cycles(8*1024); // tested to be best for mp3 dec\r
cc68a136 136 if (i < 0x10000) return;\r
137\r
138 /* 940 crashed */\r
42c7b147 139 printf("940 crashed (cnt: %i, ve: ", shared_ctl->loopc);\r
cc68a136 140 for (i = 0; i < 8; i++)\r
141 printf("%i ", shared_ctl->vstarts[i]);\r
142 printf(")\n");\r
75a30842 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
abe0ea43 145 printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob);\r
4f265db7 146 printf("trying to interrupt..\n");\r
75a30842 147 memregs[0x3B3E>>1] = 0xffff;\r
148 for (i = 0; memregs[0x3b46>>1] && i < 0x10000; i++)\r
4f265db7 149 spend_cycles(8*1024);\r
150 printf("i = 0x%x\n", i);\r
75a30842 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
abe0ea43 153 printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob);\r
4f265db7 154\r
75a30842 155 menu_update_msg("940 crashed, too much overclock?");\r
cc68a136 156 engineState = PGS_Menu;\r
157 crashed_940 = 1;\r
cc68a136 158}\r
159\r
160\r
abe0ea43 161static void add_job_940(int job)\r
cc68a136 162{\r
abe0ea43 163 if (job <= 0 || job > 16) {\r
164 printf("add_job_940: bad job: %i\n", job);\r
165 return;\r
4f265db7 166 }\r
abe0ea43 167\r
168 // generate interrupt for this job\r
169 job--;\r
75a30842 170 memregs[(0x3B20+job*2)>>1] = 1;\r
abe0ea43 171\r
75a30842 172// printf("added %i, pending %04x\n", job+1, memregs[0x3b46>>1]);\r
cc68a136 173}\r
174\r
175\r
176void YM2612PicoStateLoad_940(void)\r
177{\r
4b9c5888 178 UINT8 *REGS = YM2612GetRegs();\r
179\r
d5d11924 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
183\r
184 writebuff_ptr = 0;\r
4b9c5888 185 addr_A1 = *(INT32 *) (REGS + 0x200);\r
cc68a136 186}\r
187\r
e4fb433c 188void YM2612PicoStateSave2_940(int tat, int tbt)\r
189{\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
193\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
198 return;\r
199 }\r
200\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
208}\r
209\r
210int YM2612PicoStateLoad2_940(int *tat, int *tbt)\r
211{\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
215\r
216 if (*(UINT32 *)(ym_local_regs + 0x100) != 0x41534d59)\r
217 return -1;\r
218\r
219 *tat = *(INT32 *)(ym_local_regs + 0x108);\r
220 *tbt = *(INT32 *)(ym_local_regs + 0x10c);\r
221\r
222 if (CHECK_BUSY(JOB940_YM2612UPDATEONE)) wait_busy_940(JOB940_YM2612UPDATEONE);\r
223\r
224 /* flush writes */\r
225 if (shared_ctl->writebuffsel == 1) {\r
226 shared_ctl->writebuff0[writebuff_ptr & 0xffff] = 0xffff;\r
227 } else {\r
228 shared_ctl->writebuff1[writebuff_ptr & 0xffff] = 0xffff;\r
229 }\r
230 shared_ctl->writebuffsel ^= 1;\r
231 writebuff_ptr = 0;\r
232 add_job_940(JOB940_PICOSTATELOAD2_PREP);\r
233 if (CHECK_BUSY(JOB940_PICOSTATELOAD2_PREP)) wait_busy_940(JOB940_PICOSTATELOAD2_PREP);\r
234\r
235 memcpy(ym_remote_regs, ym_local_regs, 0x200);\r
236\r
237 add_job_940(JOB940_PICOSTATELOAD2);\r
238 if (CHECK_BUSY(JOB940_PICOSTATELOAD2)) wait_busy_940(JOB940_PICOSTATELOAD2);\r
239\r
240 return 0;\r
241}\r
242\r
cc68a136 243\r
244static void internal_reset(void)\r
245{\r
246 writebuff_ptr = 0;\r
4b9c5888 247 ST_address = addr_A1 = -1;\r
cc68a136 248}\r
249\r
250\r
f80d67e1 251/* this must be called after mmu hack, the allocated regions must not get cached */\r
0d9bf4fc 252void sharedmem940_init(void)\r
f80d67e1 253{\r
254 if (shared_mem != NULL) return;\r
255\r
256 shared_mem = (unsigned char *) mmap(0, 0x210000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x2000000);\r
e4fb433c 257 if (shared_mem == MAP_FAILED)\r
f80d67e1 258 {\r
259 printf("mmap(shared_data) failed with %i\n", errno);\r
260 exit(1);\r
261 }\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
e362c573 265 mp3_mem = (unsigned char *) mmap(0, MP3_SIZE_MAX, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x2400000);\r
f80d67e1 266 if (mp3_mem == MAP_FAILED)\r
267 {\r
268 printf("mmap(mp3_mem) failed with %i\n", errno);\r
269 exit(1);\r
270 }\r
271 crashed_940 = 1;\r
272}\r
273\r
274\r
0d9bf4fc 275void sharedmem940_finish(void)\r
85f8e929 276{\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
281 shared_ctl = NULL;\r
282}\r
283\r
284\r
cc68a136 285void YM2612Init_940(int baseclock, int rate)\r
286{\r
2d646065 287 static int oldrate;\r
288\r
289 // HACK\r
290 if (Pico.m.frame_count > 0 && !crashed_940 && rate == oldrate)\r
291 return;\r
292\r
cc68a136 293 printf("YM2612Init_940()\n");\r
b837b69b 294 printf("Mem usage: shared_data: %i, shared_ctl: %i\n", sizeof(*shared_data), sizeof(*shared_ctl));\r
cc68a136 295\r
42171343 296 reset940(1, 2);\r
297 pause940(1);\r
cc68a136 298\r
75a30842 299 memregs[0x3B40>>1] = 0; // disable DUALCPU interrupts for 920\r
300 memregs[0x3B42>>1] = 1; // enable DUALCPU interrupts for 940\r
cc68a136 301\r
75a30842 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
abe0ea43 304\r
305\r
cc68a136 306 if (crashed_940)\r
307 {\r
308 unsigned char ucData[1024];\r
cc41eb4f 309 int nRead, nLen = 0;\r
27701801 310 char binpath[512];\r
cc68a136 311 FILE *fp;\r
312\r
27701801 313 emu_make_path(binpath, CODE940_FILE, sizeof(binpath));\r
cc68a136 314 fp = fopen(binpath, "rb");\r
315 if(!fp)\r
316 {\r
e2de9939 317 memset(g_screen_ptr, 0, 320*240*2);\r
a12e0116 318 text_out16(10, 100, "failed to open required file:");\r
319 text_out16(10, 110, CODE940_FILE);\r
320 gp2x_video_flip2();\r
75a30842 321 in_menu_wait(PBTN_MOK|PBTN_MBACK, NULL, 100);\r
cc68a136 322 printf("failed to open %s\n", binpath);\r
323 exit(1);\r
324 }\r
325\r
326 while(1)\r
327 {\r
328 nRead = fread(ucData, 1, 1024, fp);\r
329 if(nRead <= 0)\r
330 break;\r
331 memcpy(shared_mem + nLen, ucData, nRead);\r
332 nLen += nRead;\r
333 }\r
334 fclose(fp);\r
335 crashed_940 = 0;\r
336 }\r
337\r
338 memset(shared_data, 0, sizeof(*shared_data));\r
339 memset(shared_ctl, 0, sizeof(*shared_ctl));\r
340\r
5f8c85be 341 /* cause local ym2612 to init REGS */\r
342 YM2612Init_(baseclock, rate);\r
343\r
cc68a136 344 internal_reset();\r
345\r
75a30842 346 loaded_mp3 = NULL;\r
51a902ae 347\r
75a30842 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
cc68a136 351\r
352 /* start the 940 */\r
42171343 353 reset940(0, 2);\r
354 pause940(0);\r
cc68a136 355\r
356 // YM2612ResetChip_940(); // will be done on JOB940_YM2612INIT\r
abe0ea43 357\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
2d646065 362\r
363 oldrate = rate;\r
cc68a136 364}\r
365\r
366\r
367void YM2612ResetChip_940(void)\r
368{\r
daec7253 369 //printf("YM2612ResetChip_940()\n");\r
cc68a136 370 if (shared_data == NULL) {\r
371 printf("YM2612ResetChip_940: reset before init?\n");\r
372 return;\r
373 }\r
374\r
4b9c5888 375 YM2612ResetChip_();\r
cc68a136 376 internal_reset();\r
377\r
abe0ea43 378 add_job_940(JOB940_YM2612RESETCHIP);\r
cc68a136 379}\r
380\r
381\r
abe0ea43 382int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty)\r
cb0316e4 383{\r
abe0ea43 384 int *ym_buf = shared_data->ym_buffer;\r
85f8e929 385 int ym_active_chs;\r
4f265db7 386\r
abe0ea43 387 //printf("YM2612UpdateOne_940()\n");\r
388\r
389 if (CHECK_BUSY(JOB940_YM2612UPDATEONE)) wait_busy_940(JOB940_YM2612UPDATEONE);\r
390\r
85f8e929 391 ym_active_chs = shared_ctl->ym_active_chs;\r
abe0ea43 392\r
85f8e929 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
abe0ea43 396\r
397 if (shared_ctl->writebuffsel == 1) {\r
2433f409 398 shared_ctl->writebuff0[writebuff_ptr & 0xffff] = 0xffff;\r
4f265db7 399 } else {\r
2433f409 400 shared_ctl->writebuff1[writebuff_ptr & 0xffff] = 0xffff;\r
cb0316e4 401 }\r
abe0ea43 402 writebuff_ptr = 0;\r
403\r
404 /* predict sample counter for next frame */\r
405 if (PsndLen_exc_add) {\r
52250671 406 length = PsndLen;\r
407 if (PsndLen_exc_cnt + PsndLen_exc_add >= 0x10000) length++;\r
abe0ea43 408 }\r
409\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
414\r
415 add_job_940(JOB940_YM2612UPDATEONE);\r
416\r
85f8e929 417 return ym_active_chs;\r
cb0316e4 418}\r
cb0316e4 419\r
cb0316e4 420\r
7c34867a 421/***********************************************************/\r
422\r
75a30842 423// FIXME: double buffering no longer used..\r
cb0316e4 424\r
75a30842 425int mp3dec_decode(FILE *f, int *file_pos, int file_len)\r
cc68a136 426{\r
7c34867a 427 if (!(PicoOpt & POPT_EXT_FM)) {\r
75a30842 428 //mp3_update_local(buffer, length, stereo);\r
429 return 0;\r
abe0ea43 430 }\r
cc68a136 431\r
7c34867a 432 // check if playback was started, track not ended\r
75a30842 433 if (loaded_mp3 == NULL || shared_ctl->mp3_offs >= shared_ctl->mp3_len) {\r
434 *file_pos = file_len;\r
435 return 1;\r
abe0ea43 436 }\r
cb0316e4 437\r
75a30842 438 /* do we have to wait? */\r
439 if (CHECK_BUSY(JOB940_MP3DECODE))\r
440 wait_busy_940(JOB940_MP3DECODE);\r
cb0316e4 441\r
75a30842 442 memcpy(cdda_out_buffer,\r
443 shared_data->mp3_buffer[shared_ctl->mp3_buffsel],\r
444 sizeof(cdda_out_buffer));\r
4f265db7 445\r
75a30842 446 *file_pos = shared_ctl->mp3_offs;\r
cc68a136 447\r
75a30842 448 if (shared_ctl->mp3_offs < shared_ctl->mp3_len) {\r
449 // ask to decode more\r
450 //shared_ctl->mp3_buffsel ^= 1;\r
abe0ea43 451 add_job_940(JOB940_MP3DECODE);\r
cb0316e4 452 }\r
cb0316e4 453\r
75a30842 454 return 0;\r
455}\r
cb0316e4 456\r
75a30842 457int mp3dec_start(FILE *f, int fpos_start)\r
cb0316e4 458{\r
7c34867a 459 if (!(PicoOpt & POPT_EXT_FM)) {\r
75a30842 460 //mp3_start_play_local(f, pos);\r
461 return -1;\r
cb0316e4 462 }\r
463\r
464 if (loaded_mp3 != f)\r
465 {\r
66fdc0f0 466 if (PicoMessage != NULL)\r
467 {\r
468 fseek(f, 0, SEEK_END);\r
469 if (ftell(f) > 2*1024*1024)\r
470 PicoMessage("Loading MP3...");\r
471 }\r
cb0316e4 472 fseek(f, 0, SEEK_SET);\r
473 fread(mp3_mem, 1, MP3_SIZE_MAX, f);\r
75a30842 474 if (!feof(f))\r
475 printf("Warning: mp3 was too large, not all data loaded.\n");\r
cb0316e4 476 shared_ctl->mp3_len = ftell(f);\r
477 loaded_mp3 = f;\r
abe0ea43 478\r
75a30842 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
00bd648e 484 reset_timing = 1;\r
cb0316e4 485 }\r
486\r
75a30842 487 shared_ctl->mp3_offs = fpos_start;\r
488 shared_ctl->mp3_buffsel = 0;\r
cb0316e4 489\r
75a30842 490 add_job_940(JOB940_MP3RESET);\r
491 if (CHECK_BUSY(JOB940_MP3RESET))\r
492 wait_busy_940(JOB940_MP3RESET);\r
51a902ae 493\r
75a30842 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
497 }\r
abe0ea43 498\r
75a30842 499 return 0;\r
75736070 500}\r
501\r