get rid of custom memcpy funcs
[picodrive.git] / platform / gp2x / 940ctl.c
... / ...
CommitLineData
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
8\r
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
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
29#include "plat.h"\r
30#include "940ctl.h"\r
31\r
32static unsigned char *shared_mem = 0;\r
33static _940_data_t *shared_data = 0;\r
34_940_ctl_t *shared_ctl = 0;\r
35unsigned char *mp3_mem = 0;\r
36\r
37#define MP3_SIZE_MAX (0x400000 + 0x800000) // 12M\r
38#define CODE940_FILE "pico940_v3.bin"\r
39\r
40int crashed_940 = 0;\r
41\r
42static FILE *loaded_mp3 = 0;\r
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
56static UINT8 ST_address; /* address register */\r
57static INT32 addr_A1; /* address line A1 */\r
58\r
59static int writebuff_ptr = 0;\r
60\r
61\r
62/* YM2612 write */\r
63/* a = address */\r
64/* v = value */\r
65/* returns 1 if sample affecting state changed */\r
66int YM2612Write_940(unsigned int a, unsigned int v, int scanline)\r
67{\r
68 int upd = 1; /* the write affects sample generation */\r
69\r
70 a &= 3;\r
71\r
72 //printf("%05i:%03i: ym w ([%i] %02x)\n", Pico.m.frame_count, Pico.m.scanline, a, v);\r
73\r
74 switch (a)\r
75 {\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
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
83 return 0;\r
84 upd = 0;\r
85 break;\r
86\r
87 case 2: /* address port 1 */\r
88 if (addr_A1 == 1 && ST_address == v)\r
89 return 0;\r
90 ST_address = v;\r
91 addr_A1 = 1;\r
92 upd = 0;\r
93 break;\r
94 }\r
95\r
96 //printf("ym pass\n");\r
97\r
98 if (currentConfig.EmuOpt & 4)\r
99 {\r
100 UINT16 *writebuff = shared_ctl->writebuffsel ? shared_ctl->writebuff0 : shared_ctl->writebuff1;\r
101\r
102 /* detect rapid ym updates */\r
103 if (upd && !(writebuff_ptr & 0x80000000) && scanline < 224)\r
104 {\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
111 }\r
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
117 } else {\r
118 printf("warning: writebuff_ptr > 2047 ([%i] %02x)\n", a, v);\r
119 }\r
120 }\r
121\r
122 return 0; // cause the engine to do updates once per frame only\r
123}\r
124\r
125\r
126#define CHECK_BUSY(job) \\r
127 (memregs[0x3b46>>1] & (1<<(job-1)))\r
128\r
129static void wait_busy_940(int job)\r
130{\r
131 int i;\r
132\r
133 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
137\r
138 /* 940 crashed */\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
142 printf(")\n");\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
154\r
155 menu_update_msg("940 crashed, too much overclock?");\r
156 engineState = PGS_Menu;\r
157 crashed_940 = 1;\r
158}\r
159\r
160\r
161static void add_job_940(int job)\r
162{\r
163 if (job <= 0 || job > 16) {\r
164 printf("add_job_940: bad job: %i\n", job);\r
165 return;\r
166 }\r
167\r
168 // generate interrupt for this job\r
169 job--;\r
170 memregs[(0x3B20+job*2)>>1] = 1;\r
171\r
172// printf("added %i, pending %04x\n", job+1, memregs[0x3b46>>1]);\r
173}\r
174\r
175\r
176void YM2612PicoStateLoad_940(void)\r
177{\r
178 UINT8 *REGS = YM2612GetRegs();\r
179\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
183\r
184 writebuff_ptr = 0;\r
185 addr_A1 = *(INT32 *) (REGS + 0x200);\r
186}\r
187\r
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
243\r
244static void internal_reset(void)\r
245{\r
246 writebuff_ptr = 0;\r
247 ST_address = addr_A1 = -1;\r
248}\r
249\r
250\r
251/* this must be called after mmu hack, the allocated regions must not get cached */\r
252void sharedmem940_init(void)\r
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
257 if (shared_mem == MAP_FAILED)\r
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
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
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
275void sharedmem940_finish(void)\r
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
285void YM2612Init_940(int baseclock, int rate)\r
286{\r
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
293 printf("YM2612Init_940()\n");\r
294 printf("Mem usage: shared_data: %i, shared_ctl: %i\n", sizeof(*shared_data), sizeof(*shared_ctl));\r
295\r
296 reset940(1, 2);\r
297 pause940(1);\r
298\r
299 memregs[0x3B40>>1] = 0; // disable DUALCPU interrupts for 920\r
300 memregs[0x3B42>>1] = 1; // enable DUALCPU interrupts for 940\r
301\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
304\r
305\r
306 if (crashed_940)\r
307 {\r
308 unsigned char ucData[1024];\r
309 int nRead, nLen = 0;\r
310 char binpath[512];\r
311 FILE *fp;\r
312\r
313 emu_make_path(binpath, CODE940_FILE, sizeof(binpath));\r
314 fp = fopen(binpath, "rb");\r
315 if(!fp)\r
316 {\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
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
341 /* cause local ym2612 to init REGS */\r
342 YM2612Init_(baseclock, rate);\r
343\r
344 internal_reset();\r
345\r
346 loaded_mp3 = NULL;\r
347\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
351\r
352 /* start the 940 */\r
353 reset940(0, 2);\r
354 pause940(0);\r
355\r
356 // YM2612ResetChip_940(); // will be done on JOB940_YM2612INIT\r
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
362\r
363 oldrate = rate;\r
364}\r
365\r
366\r
367void YM2612ResetChip_940(void)\r
368{\r
369 //printf("YM2612ResetChip_940()\n");\r
370 if (shared_data == NULL) {\r
371 printf("YM2612ResetChip_940: reset before init?\n");\r
372 return;\r
373 }\r
374\r
375 YM2612ResetChip_();\r
376 internal_reset();\r
377\r
378 add_job_940(JOB940_YM2612RESETCHIP);\r
379}\r
380\r
381\r
382int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty)\r
383{\r
384 int *ym_buf = shared_data->ym_buffer;\r
385 int ym_active_chs;\r
386\r
387 //printf("YM2612UpdateOne_940()\n");\r
388\r
389 if (CHECK_BUSY(JOB940_YM2612UPDATEONE)) wait_busy_940(JOB940_YM2612UPDATEONE);\r
390\r
391 ym_active_chs = shared_ctl->ym_active_chs;\r
392\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) memcpy(buffer, ym_buf, length << (stereo + 2));\r
395 else memset32(buffer, 0, length<<stereo);\r
396\r
397 if (shared_ctl->writebuffsel == 1) {\r
398 shared_ctl->writebuff0[writebuff_ptr & 0xffff] = 0xffff;\r
399 } else {\r
400 shared_ctl->writebuff1[writebuff_ptr & 0xffff] = 0xffff;\r
401 }\r
402 writebuff_ptr = 0;\r
403\r
404 /* predict sample counter for next frame */\r
405 if (PsndLen_exc_add) {\r
406 length = PsndLen;\r
407 if (PsndLen_exc_cnt + PsndLen_exc_add >= 0x10000) length++;\r
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
417 return ym_active_chs;\r
418}\r
419\r
420\r
421/***********************************************************/\r
422\r
423// FIXME: double buffering no longer used..\r
424\r
425int mp3dec_decode(FILE *f, int *file_pos, int file_len)\r
426{\r
427 if (!(PicoOpt & POPT_EXT_FM)) {\r
428 //mp3_update_local(buffer, length, stereo);\r
429 return 0;\r
430 }\r
431\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
435 return 1;\r
436 }\r
437\r
438 /* do we have to wait? */\r
439 if (CHECK_BUSY(JOB940_MP3DECODE))\r
440 wait_busy_940(JOB940_MP3DECODE);\r
441\r
442 memcpy(cdda_out_buffer,\r
443 shared_data->mp3_buffer[shared_ctl->mp3_buffsel],\r
444 sizeof(cdda_out_buffer));\r
445\r
446 *file_pos = shared_ctl->mp3_offs;\r
447\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
452 }\r
453\r
454 return 0;\r
455}\r
456\r
457int mp3dec_start(FILE *f, int fpos_start)\r
458{\r
459 if (!(PicoOpt & POPT_EXT_FM)) {\r
460 //mp3_start_play_local(f, pos);\r
461 return -1;\r
462 }\r
463\r
464 if (loaded_mp3 != f)\r
465 {\r
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
472 fseek(f, 0, SEEK_SET);\r
473 fread(mp3_mem, 1, MP3_SIZE_MAX, f);\r
474 if (!feof(f))\r
475 printf("Warning: mp3 was too large, not all data loaded.\n");\r
476 shared_ctl->mp3_len = ftell(f);\r
477 loaded_mp3 = f;\r
478\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
484 reset_timing = 1;\r
485 }\r
486\r
487 shared_ctl->mp3_offs = fpos_start;\r
488 shared_ctl->mp3_buffsel = 0;\r
489\r
490 add_job_940(JOB940_MP3RESET);\r
491 if (CHECK_BUSY(JOB940_MP3RESET))\r
492 wait_busy_940(JOB940_MP3RESET);\r
493\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
497 }\r
498\r
499 return 0;\r
500}\r
501\r