enable evdev also for GP2X
[picodrive.git] / platform / gp2x / emu.c
... / ...
CommitLineData
1// (c) Copyright 2006-2009 notaz, All rights reserved.\r
2// Free for non-commercial use.\r
3\r
4// For commercial use, separate licencing terms must be obtained.\r
5\r
6#include <stdio.h>\r
7#include <stdlib.h>\r
8#include <sys/time.h>\r
9#include <stdarg.h>\r
10\r
11#include "plat_gp2x.h"\r
12#include "soc.h"\r
13#include "../common/plat.h"\r
14#include "../common/menu.h"\r
15#include "../common/arm_utils.h"\r
16#include "../common/fonts.h"\r
17#include "../common/emu.h"\r
18#include "../common/config.h"\r
19#include "../linux/sndout_oss.h"\r
20#include "version.h"\r
21\r
22#include <pico/pico_int.h>\r
23#include <pico/patch.h>\r
24#include <pico/sound/mix.h>\r
25#include <zlib/zlib.h>\r
26\r
27//#define PFRAMES\r
28\r
29#ifdef BENCHMARK\r
30#define OSD_FPS_X 220\r
31#else\r
32#define OSD_FPS_X 260\r
33#endif\r
34\r
35\r
36extern int crashed_940;\r
37\r
38static short __attribute__((aligned(4))) sndBuffer[2*44100/50];\r
39static struct timeval noticeMsgTime = { 0, 0 }; // when started showing\r
40static char noticeMsg[40];\r
41static unsigned char PicoDraw2FB_[(8+320) * (8+240+8)];\r
42unsigned char *PicoDraw2FB = PicoDraw2FB_;\r
43static int osd_fps_x;\r
44\r
45extern void *gp2x_screens[4];\r
46\r
47void plat_status_msg(const char *format, ...)\r
48{\r
49 va_list vl;\r
50\r
51 va_start(vl, format);\r
52 vsnprintf(noticeMsg, sizeof(noticeMsg), format, vl);\r
53 va_end(vl);\r
54\r
55 gettimeofday(&noticeMsgTime, 0);\r
56}\r
57\r
58int plat_get_root_dir(char *dst, int len)\r
59{\r
60 extern char **g_argv;\r
61 int j;\r
62\r
63 strncpy(dst, g_argv[0], len);\r
64 len -= 32; // reserve\r
65 if (len < 0) len = 0;\r
66 dst[len] = 0;\r
67 for (j = strlen(dst); j > 0; j--)\r
68 if (dst[j] == '/') { dst[j+1] = 0; break; }\r
69\r
70 return j + 1;\r
71}\r
72\r
73\r
74static void scaling_update(void)\r
75{\r
76 PicoOpt &= ~(POPT_DIS_32C_BORDER|POPT_EN_SOFTSCALE);\r
77 switch (currentConfig.scaling) {\r
78 default:break;\r
79 case EOPT_SCALE_HW_H:\r
80 case EOPT_SCALE_HW_HV:\r
81 PicoOpt |= POPT_DIS_32C_BORDER;\r
82 break;\r
83 case EOPT_SCALE_SW_H:\r
84 PicoOpt |= POPT_EN_SOFTSCALE;\r
85 break;\r
86 }\r
87}\r
88\r
89\r
90void pemu_prep_defconfig(void)\r
91{\r
92 gp2x_soc_t soc;\r
93\r
94 memset(&defaultConfig, 0, sizeof(defaultConfig));\r
95 defaultConfig.EmuOpt = 0x9d | EOPT_RAM_TIMINGS|EOPT_CONFIRM_SAVE|EOPT_EN_CD_LEDS;\r
96 defaultConfig.s_PicoOpt = 0x0f | POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC|POPT_ACC_SPRITES;\r
97 defaultConfig.s_PsndRate = 44100;\r
98 defaultConfig.s_PicoRegion = 0; // auto\r
99 defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP\r
100 defaultConfig.s_PicoCDBuffers = 0;\r
101 defaultConfig.Frameskip = -1; // auto\r
102 defaultConfig.CPUclock = default_cpu_clock;\r
103 defaultConfig.volume = 50;\r
104 defaultConfig.gamma = 100;\r
105 defaultConfig.scaling = 0;\r
106 defaultConfig.turbo_rate = 15;\r
107\r
108 soc = soc_detect();\r
109 if (soc == SOCID_MMSP2)\r
110 defaultConfig.s_PicoOpt |= POPT_EXT_FM;\r
111 else if (soc == SOCID_POLLUX)\r
112 defaultConfig.EmuOpt |= EOPT_WIZ_TEAR_FIX|EOPT_SHOW_RTC;\r
113}\r
114\r
115static void (*osd_text)(int x, int y, const char *text);\r
116\r
117static void osd_text8(int x, int y, const char *text)\r
118{\r
119 int len = strlen(text)*8;\r
120 int *p, i, h, offs;\r
121\r
122 len = (len+3) >> 2;\r
123 for (h = 0; h < 8; h++) {\r
124 offs = (x + g_screen_width * (y+h)) & ~3;\r
125 p = (int *) ((char *)g_screen_ptr + offs);\r
126 for (i = len; i; i--, p++)\r
127 *p = 0xe0e0e0e0;\r
128 }\r
129 emu_text_out8(x, y, text);\r
130}\r
131\r
132static void osd_text16(int x, int y, const char *text)\r
133{\r
134 int len = strlen(text)*8;\r
135 int *p, i, h, offs;\r
136\r
137 len = (len+1) >> 1;\r
138 for (h = 0; h < 8; h++) {\r
139 offs = (x + g_screen_width * (y+h)) & ~1;\r
140 p = (int *) ((short *)g_screen_ptr + offs);\r
141 for (i = len; i; i--, p++)\r
142 *p = (*p >> 2) & 0x39e7;\r
143 }\r
144 emu_text_out16(x, y, text);\r
145}\r
146\r
147static void osd_text8_rot(int x, int y, const char *text)\r
148{\r
149 int len = strlen(text) * 8;\r
150 char *p = (char *)g_screen_ptr + 240*(320-x) + y;\r
151\r
152 while (len--) {\r
153 memset(p, 0xe0, 8);\r
154 p -= 240;\r
155 }\r
156\r
157 emu_text_out8_rot(x, y, text);\r
158}\r
159\r
160static void osd_text16_rot(int x, int y, const char *text)\r
161{\r
162 int len = strlen(text) * 8;\r
163 short *p = (short *)g_screen_ptr + 240*(320-x) + y;\r
164\r
165 while (len--) {\r
166 memset(p, 0, 8*2);\r
167 p -= 240;\r
168 }\r
169\r
170 emu_text_out16_rot(x, y, text);\r
171}\r
172\r
173static void draw_cd_leds(void)\r
174{\r
175 int led_reg, pitch, scr_offs, led_offs;\r
176 led_reg = Pico_mcd->s68k_regs[0];\r
177\r
178 if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
179 pitch = 240;\r
180 led_offs = -pitch * 6;\r
181 scr_offs = pitch * (320 - 4);\r
182 } else {\r
183 pitch = 320;\r
184 led_offs = 4;\r
185 scr_offs = pitch * 2 + 4;\r
186 }\r
187\r
188 if ((PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP)) {\r
189 #define p(x) px[(x) >> 2]\r
190 // 8-bit modes\r
191 unsigned int *px = (unsigned int *)((char *)g_screen_ptr + scr_offs);\r
192 unsigned int col_g = (led_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;\r
193 unsigned int col_r = (led_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;\r
194 p(pitch*0) = p(pitch*1) = p(pitch*2) = col_g;\r
195 p(pitch*0 + led_offs) = p(pitch*1 + led_offs) = p(pitch*2 + led_offs) = col_r;\r
196 #undef p\r
197 } else {\r
198 #define p(x) px[(x)*2 >> 2] = px[((x)*2 >> 2) + 1]\r
199 // 16-bit modes\r
200 unsigned int *px = (unsigned int *)((short *)g_screen_ptr + scr_offs);\r
201 unsigned int col_g = (led_reg & 2) ? 0x06000600 : 0;\r
202 unsigned int col_r = (led_reg & 1) ? 0xc000c000 : 0;\r
203 p(pitch*0) = p(pitch*1) = p(pitch*2) = col_g;\r
204 p(pitch*0 + led_offs) = p(pitch*1 + led_offs) = p(pitch*2 + led_offs) = col_r;\r
205 #undef p\r
206 }\r
207}\r
208\r
209static void draw_pico_ptr(void)\r
210{\r
211 unsigned short *p = (unsigned short *)g_screen_ptr;\r
212 int x, y, pitch = 320;\r
213\r
214 // only if pen enabled and for 16bit modes\r
215 if (pico_inp_mode == 0 || (PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP))\r
216 return;\r
217\r
218 x = pico_pen_x + PICO_PEN_ADJUST_X;\r
219 y = pico_pen_y + PICO_PEN_ADJUST_Y;\r
220 if (!(Pico.video.reg[12]&1) && !(PicoOpt & POPT_DIS_32C_BORDER))\r
221 x += 32;\r
222\r
223 if (EOPT_WIZ_TEAR_FIX) {\r
224 pitch = 240;\r
225 p += (319 - x) * pitch + y;\r
226 } else\r
227 p += x + y * pitch;\r
228\r
229 p[0] ^= 0xffff;\r
230 p[pitch-1] ^= 0xffff;\r
231 p[pitch] ^= 0xffff;\r
232 p[pitch+1] ^= 0xffff;\r
233 p[pitch*2] ^= 0xffff;\r
234}\r
235\r
236static int EmuScanBegin16(unsigned int num)\r
237{\r
238 if (!(Pico.video.reg[1]&8)) num += 8;\r
239 DrawLineDest = (unsigned short *) g_screen_ptr + g_screen_width * num;\r
240\r
241 return 0;\r
242}\r
243\r
244static int EmuScanBegin8(unsigned int num)\r
245{\r
246 if (!(Pico.video.reg[1]&8)) num += 8;\r
247 DrawLineDest = (unsigned char *) g_screen_ptr + g_screen_width * num;\r
248\r
249 return 0;\r
250}\r
251\r
252/* rot thing for Wiz */\r
253static unsigned char __attribute__((aligned(4))) rot_buff[320*4*2];\r
254\r
255static int EmuScanBegin16_rot(unsigned int num)\r
256{\r
257 DrawLineDest = rot_buff + (num & 3) * 320 * 2;\r
258 return 0;\r
259}\r
260\r
261static int EmuScanEnd16_rot(unsigned int num)\r
262{\r
263 if ((num & 3) != 3)\r
264 return 0;\r
265 if (!(Pico.video.reg[1] & 8))\r
266 num += 8;\r
267 rotated_blit16(g_screen_ptr, rot_buff, num + 1,\r
268 !(Pico.video.reg[12] & 1) && !(PicoOpt & POPT_EN_SOFTSCALE));\r
269 return 0;\r
270}\r
271\r
272static int EmuScanBegin8_rot(unsigned int num)\r
273{\r
274 DrawLineDest = rot_buff + (num & 3) * 320;\r
275 return 0;\r
276}\r
277\r
278static int EmuScanEnd8_rot(unsigned int num)\r
279{\r
280 if ((num & 3) != 3)\r
281 return 0;\r
282 if (!(Pico.video.reg[1] & 8))\r
283 num += 8;\r
284 rotated_blit8(g_screen_ptr, rot_buff, num + 1,\r
285 !(Pico.video.reg[12] & 1));\r
286 return 0;\r
287}\r
288\r
289int localPal[0x100];\r
290static void (*vidcpyM2)(void *dest, void *src, int m32col, int with_32c_border) = NULL;\r
291\r
292static void blit(const char *fps, const char *notice)\r
293{\r
294 int emu_opt = currentConfig.EmuOpt;\r
295\r
296 if (PicoOpt & POPT_ALT_RENDERER)\r
297 {\r
298 // 8bit fast renderer\r
299 if (Pico.m.dirtyPal) {\r
300 Pico.m.dirtyPal = 0;\r
301 vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
302 // feed new palette to our device\r
303 gp2x_video_setpalette(localPal, 0x40);\r
304 }\r
305 // a hack for VR\r
306 if (PicoRead16Hook == PicoSVPRead16)\r
307 memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);\r
308 // do actual copy\r
309 vidcpyM2(g_screen_ptr, PicoDraw2FB+328*8,\r
310 !(Pico.video.reg[12] & 1), !(PicoOpt & POPT_DIS_32C_BORDER));\r
311 }\r
312 else if (!(emu_opt & EOPT_16BPP))\r
313 {\r
314 // 8bit accurate renderer\r
315 if (Pico.m.dirtyPal)\r
316 {\r
317 int pallen = 0xc0;\r
318 Pico.m.dirtyPal = 0;\r
319 if (Pico.video.reg[0xC]&8) // shadow/hilight mode\r
320 {\r
321 vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
322 vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);\r
323 vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);\r
324 memcpy32(localPal+0xc0, localPal+0x40, 0x40);\r
325 pallen = 0x100;\r
326 }\r
327 else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes\r
328 vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
329 vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);\r
330 vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);\r
331 }\r
332 else {\r
333 vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
334 memcpy32(localPal+0x80, localPal, 0x40); // for spr prio mess\r
335 }\r
336 if (pallen > 0xc0) {\r
337 localPal[0xc0] = 0x0000c000;\r
338 localPal[0xd0] = 0x00c00000;\r
339 localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
340 localPal[0xf0] = 0x00ffffff;\r
341 }\r
342 gp2x_video_setpalette(localPal, pallen);\r
343 }\r
344 }\r
345\r
346 if (notice || (emu_opt & 2)) {\r
347 int h = 232;\r
348 if (currentConfig.scaling == EOPT_SCALE_HW_HV && !(Pico.video.reg[1]&8))\r
349 h -= 8;\r
350 if (notice)\r
351 osd_text(4, h, notice);\r
352 if (emu_opt & 2)\r
353 osd_text(osd_fps_x, h, fps);\r
354 }\r
355 if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))\r
356 draw_cd_leds();\r
357 if (PicoAHW & PAHW_PICO)\r
358 draw_pico_ptr();\r
359\r
360 gp2x_video_flip();\r
361}\r
362\r
363// clears whole screen or just the notice area (in all buffers)\r
364static void clearArea(int full)\r
365{\r
366 int is_8bit = (PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP);\r
367 if (!full && (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX)) {\r
368 /* ugh.. */\r
369 int i, u, *p;\r
370 if (is_8bit) {\r
371 for (i = 0; i < 4; i++) {\r
372 p = (int *)gp2x_screens[i] + (240-8) / 4;\r
373 for (u = 320; u > 0; u--, p += 240/4)\r
374 p[0] = p[1] = 0xe0e0e0e0;\r
375 }\r
376 } else {\r
377 for (i = 0; i < 4; i++) {\r
378 p = (int *)gp2x_screens[i] + (240-8)*2 / 4;\r
379 for (u = 320; u > 0; u--, p += 240*2/4)\r
380 p[0] = p[1] = p[2] = p[3] = 0;\r
381 }\r
382 }\r
383 return;\r
384 }\r
385\r
386 if (is_8bit) {\r
387 // 8-bit renderers\r
388 if (full) gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
389 else gp2x_memset_all_buffers(320*232, 0xe0, 320*8);\r
390 } else {\r
391 // 16bit accurate renderer\r
392 if (full) gp2x_memset_all_buffers(0, 0, 320*240*2);\r
393 else gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);\r
394 }\r
395}\r
396\r
397void plat_status_msg_busy_next(const char *msg)\r
398{\r
399 clearArea(0);\r
400 blit("", msg);\r
401\r
402 /* assumption: msg_busy_next gets called only when\r
403 * something slow is about to happen */\r
404 reset_timing = 1;\r
405}\r
406\r
407void plat_status_msg_busy_first(const char *msg)\r
408{\r
409 gp2x_memcpy_all_buffers(g_screen_ptr, 0, 320*240*2);\r
410 plat_status_msg_busy_next(msg);\r
411}\r
412\r
413static void vidResetMode(void)\r
414{\r
415 PicoScanEnd = NULL;\r
416\r
417 if (PicoOpt & POPT_ALT_RENDERER) {\r
418 if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
419 gp2x_video_changemode(-8);\r
420 vidcpyM2 = vidcpy_m2_rot;\r
421 osd_text = osd_text8_rot;\r
422 } else {\r
423 gp2x_video_changemode(8);\r
424 vidcpyM2 = vidcpy_m2;\r
425 osd_text = osd_text8;\r
426 }\r
427 }\r
428 else if (currentConfig.EmuOpt & EOPT_16BPP) {\r
429 PicoDrawSetColorFormat(1);\r
430 if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
431 gp2x_video_changemode(-16);\r
432 PicoScanBegin = EmuScanBegin16_rot;\r
433 PicoScanEnd = EmuScanEnd16_rot;\r
434 osd_text = osd_text16_rot;\r
435 } else {\r
436 gp2x_video_changemode(16);\r
437 PicoScanBegin = EmuScanBegin16;\r
438 osd_text = osd_text16;\r
439 }\r
440 }\r
441 else {\r
442 PicoDrawSetColorFormat(2);\r
443 if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
444 gp2x_video_changemode(-8);\r
445 PicoScanBegin = EmuScanBegin8_rot;\r
446 PicoScanEnd = EmuScanEnd8_rot;\r
447 osd_text = osd_text8_rot;\r
448 } else {\r
449 gp2x_video_changemode(8);\r
450 PicoScanBegin = EmuScanBegin8;\r
451 osd_text = osd_text8;\r
452 }\r
453 }\r
454\r
455 if ((PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP)) {\r
456 // setup pal for 8-bit modes\r
457 localPal[0xc0] = 0x0000c000; // MCD LEDs\r
458 localPal[0xd0] = 0x00c00000;\r
459 localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
460 localPal[0xf0] = 0x00ffffff;\r
461 gp2x_video_setpalette(localPal, 0x100);\r
462 gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
463 gp2x_video_flip();\r
464 }\r
465 Pico.m.dirtyPal = 1;\r
466\r
467 // reset scaling\r
468 if (currentConfig.scaling == EOPT_SCALE_HW_HV && !(Pico.video.reg[1]&8))\r
469 gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);\r
470 else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);\r
471}\r
472\r
473void plat_video_toggle_renderer(int is_next, int is_menu)\r
474{\r
475 /* alt, 16bpp, 8bpp */\r
476 if (PicoOpt & POPT_ALT_RENDERER) {\r
477 PicoOpt &= ~POPT_ALT_RENDERER;\r
478 if (is_next)\r
479 currentConfig.EmuOpt |= EOPT_16BPP;\r
480 } else if (!(currentConfig.EmuOpt & EOPT_16BPP)) {\r
481 if (is_next)\r
482 PicoOpt |= POPT_ALT_RENDERER;\r
483 else\r
484 currentConfig.EmuOpt |= EOPT_16BPP;\r
485 } else {\r
486 currentConfig.EmuOpt &= ~EOPT_16BPP;\r
487 if (!is_next)\r
488 PicoOpt |= POPT_ALT_RENDERER;\r
489 }\r
490\r
491 if (is_menu)\r
492 return;\r
493\r
494 vidResetMode();\r
495\r
496 if (PicoOpt & POPT_ALT_RENDERER) {\r
497 plat_status_msg(" 8bit fast renderer");\r
498 } else if (currentConfig.EmuOpt & EOPT_16BPP) {\r
499 plat_status_msg("16bit accurate renderer");\r
500 } else {\r
501 plat_status_msg(" 8bit accurate renderer");\r
502 }\r
503}\r
504\r
505#if 0 // TODO\r
506static void RunEventsPico(unsigned int events)\r
507{\r
508 int ret, px, py, lim_x;\r
509 static int pdown_frames = 0;\r
510\r
511 // for F200\r
512 ret = gp2x_touchpad_read(&px, &py);\r
513 if (ret >= 0)\r
514 {\r
515 if (ret > 35000)\r
516 {\r
517 if (pdown_frames++ > 5)\r
518 PicoPad[0] |= 0x20;\r
519\r
520 pico_pen_x = px;\r
521 pico_pen_y = py;\r
522 if (!(Pico.video.reg[12]&1)) {\r
523 pico_pen_x -= 32;\r
524 if (pico_pen_x < 0) pico_pen_x = 0;\r
525 if (pico_pen_x > 248) pico_pen_x = 248;\r
526 }\r
527 if (pico_pen_y > 224) pico_pen_y = 224;\r
528 }\r
529 else\r
530 pdown_frames = 0;\r
531\r
532 //if (ret == 0)\r
533 // PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;\r
534 }\r
535}\r
536#endif\r
537\r
538void plat_update_volume(int has_changed, int is_up)\r
539{\r
540 static int prev_frame = 0, wait_frames = 0;\r
541 int vol = currentConfig.volume;\r
542 int need_low_volume = 0;\r
543 gp2x_soc_t soc;\r
544\r
545 soc = soc_detect();\r
546 if ((PicoOpt & POPT_EN_STEREO) && soc == SOCID_MMSP2)\r
547 need_low_volume = 1;\r
548\r
549 if (has_changed)\r
550 {\r
551 if (need_low_volume && vol < 5 && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)\r
552 wait_frames++;\r
553 else {\r
554 if (is_up) {\r
555 if (vol < 99) vol++;\r
556 } else {\r
557 if (vol > 0) vol--;\r
558 }\r
559 wait_frames = 0;\r
560 sndout_oss_setvol(vol, vol);\r
561 currentConfig.volume = vol;\r
562 }\r
563 plat_status_msg("VOL: %02i", vol);\r
564 prev_frame = Pico.m.frame_count;\r
565 }\r
566\r
567 if (need_low_volume)\r
568 return;\r
569\r
570 /* set the right mixer func */\r
571 if (vol >= 5)\r
572 PsndMix_32_to_16l = mix_32_to_16l_stereo;\r
573 else {\r
574 mix_32_to_16l_level = 5 - vol;\r
575 PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;\r
576 }\r
577}\r
578\r
579\r
580static void updateSound(int len)\r
581{\r
582 if (PicoOpt&8) len<<=1;\r
583\r
584 /* avoid writing audio when lagging behind to prevent audio lag */\r
585 if (PicoSkipFrame != 2)\r
586 sndout_oss_write(PsndOut, len<<1);\r
587}\r
588\r
589void pemu_sound_start(void)\r
590{\r
591 static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;\r
592 int target_fps = Pico.m.pal ? 50 : 60;\r
593\r
594 PsndOut = NULL;\r
595\r
596 // prepare sound stuff\r
597 if (currentConfig.EmuOpt & 4)\r
598 {\r
599 int snd_excess_add;\r
600 if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||\r
601 ((PicoOpt&0x200) && crashed_940)) {\r
602 PsndRerate(Pico.m.frame_count ? 1 : 0);\r
603 }\r
604 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r
605 printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r
606 PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);\r
607 sndout_oss_start(PsndRate, 16, (PicoOpt&8)>>3);\r
608 sndout_oss_setvol(currentConfig.volume, currentConfig.volume);\r
609 PicoWriteSound = updateSound;\r
610 plat_update_volume(0, 0);\r
611 memset(sndBuffer, 0, sizeof(sndBuffer));\r
612 PsndOut = sndBuffer;\r
613 PsndRate_old = PsndRate;\r
614 PicoOpt_old = PicoOpt;\r
615 pal_old = Pico.m.pal;\r
616 }\r
617}\r
618\r
619void pemu_sound_stop(void)\r
620{\r
621}\r
622\r
623void pemu_sound_wait(void)\r
624{\r
625 // don't need to do anything, writes will block by themselves\r
626}\r
627\r
628\r
629static void SkipFrame(int do_audio)\r
630{\r
631 PicoSkipFrame=do_audio ? 1 : 2;\r
632 PicoFrame();\r
633 PicoSkipFrame=0;\r
634}\r
635\r
636\r
637void pemu_forced_frame(int opts)\r
638{\r
639 int po_old = PicoOpt;\r
640 int eo_old = currentConfig.EmuOpt;\r
641\r
642 PicoOpt &= ~POPT_ALT_RENDERER;\r
643 PicoOpt |= opts|POPT_ACC_SPRITES;\r
644 currentConfig.EmuOpt |= EOPT_16BPP;\r
645\r
646 PicoDrawSetColorFormat(1);\r
647 PicoScanBegin = EmuScanBegin16;\r
648 PicoScanEnd = NULL;\r
649 Pico.m.dirtyPal = 1;\r
650 PicoFrameDrawOnly();\r
651\r
652 PicoOpt = po_old;\r
653 currentConfig.EmuOpt = eo_old;\r
654}\r
655\r
656void plat_debug_cat(char *str)\r
657{\r
658}\r
659\r
660static void simpleWait(int thissec, int lim_time)\r
661{\r
662 struct timeval tval;\r
663\r
664 spend_cycles(1024);\r
665 gettimeofday(&tval, 0);\r
666 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
667\r
668 while (tval.tv_usec < lim_time)\r
669 {\r
670 spend_cycles(1024);\r
671 gettimeofday(&tval, 0);\r
672 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
673 }\r
674}\r
675\r
676\r
677#if 0\r
678static void tga_dump(void)\r
679{\r
680#define BYTE unsigned char\r
681#define WORD unsigned short\r
682 struct\r
683 {\r
684 BYTE IDLength; /* 00h Size of Image ID field */\r
685 BYTE ColorMapType; /* 01h Color map type */\r
686 BYTE ImageType; /* 02h Image type code */\r
687 WORD CMapStart; /* 03h Color map origin */\r
688 WORD CMapLength; /* 05h Color map length */\r
689 BYTE CMapDepth; /* 07h Depth of color map entries */\r
690 WORD XOffset; /* 08h X origin of image */\r
691 WORD YOffset; /* 0Ah Y origin of image */\r
692 WORD Width; /* 0Ch Width of image */\r
693 WORD Height; /* 0Eh Height of image */\r
694 BYTE PixelDepth; /* 10h Image pixel size */\r
695 BYTE ImageDescriptor; /* 11h Image descriptor byte */\r
696 } __attribute__((packed)) TGAHEAD;\r
697 static unsigned short oldscr[320*240];\r
698 FILE *f; char name[128]; int i;\r
699\r
700 memset(&TGAHEAD, 0, sizeof(TGAHEAD));\r
701 TGAHEAD.ImageType = 2;\r
702 TGAHEAD.Width = 320;\r
703 TGAHEAD.Height = 240;\r
704 TGAHEAD.PixelDepth = 16;\r
705 TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left\r
706\r
707#define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?\r
708\r
709 for (i = 0; i < 320*240; i++)\r
710 if(oldscr[i] != CONV(((unsigned short *)g_screen_ptr)[i])) break;\r
711 if (i < 320*240)\r
712 {\r
713 for (i = 0; i < 320*240; i++)\r
714 oldscr[i] = CONV(((unsigned short *)g_screen_ptr)[i]);\r
715 sprintf(name, "%05i.tga", Pico.m.frame_count);\r
716 f = fopen(name, "wb");\r
717 if (!f) { printf("!f\n"); exit(1); }\r
718 fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);\r
719 fwrite(oldscr, 1, 320*240*2, f);\r
720 fclose(f);\r
721 }\r
722}\r
723#endif\r
724\r
725\r
726void pemu_loop(void)\r
727{\r
728 static int gp2x_old_clock = -1, EmuOpt_old = 0, pal_old = 0;\r
729 static int gp2x_old_gamma = 100;\r
730 char fpsbuff[24]; // fps count c string\r
731 struct timeval tval; // timing\r
732 int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync\r
733 int frames_done, frames_shown, thissec; // actual frames\r
734 int oldmodes = 0, target_fps, target_frametime, lim_time, vsync_offset, i;\r
735 char *notice = 0;\r
736\r
737 printf("entered emu_Loop()\n");\r
738\r
739 if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_RAM_TIMINGS) {\r
740 if (currentConfig.EmuOpt & EOPT_RAM_TIMINGS)\r
741 set_ram_timings();\r
742 else\r
743 unset_ram_timings();\r
744 }\r
745\r
746 if (gp2x_old_clock < 0)\r
747 gp2x_old_clock = default_cpu_clock;\r
748 if (gp2x_old_clock != currentConfig.CPUclock) {\r
749 printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);\r
750 gp2x_set_cpuclk(currentConfig.CPUclock);\r
751 gp2x_old_clock = currentConfig.CPUclock;\r
752 printf(" done\n");\r
753 }\r
754\r
755 if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {\r
756 set_lcd_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
757 gp2x_old_gamma = currentConfig.gamma;\r
758 printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
759 }\r
760\r
761 if (((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_PSYNC) || Pico.m.pal != pal_old) {\r
762 if (currentConfig.EmuOpt & EOPT_PSYNC)\r
763 set_lcd_custom_rate(Pico.m.pal);\r
764 else\r
765 unset_lcd_custom_rate();\r
766 }\r
767\r
768 if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_MMUHACK)\r
769 gp2x_make_fb_bufferable(currentConfig.EmuOpt & EOPT_MMUHACK);\r
770\r
771 EmuOpt_old = currentConfig.EmuOpt;\r
772 pal_old = Pico.m.pal;\r
773 fpsbuff[0] = 0;\r
774\r
775 // make sure we are in correct mode\r
776 vidResetMode();\r
777 scaling_update();\r
778 Pico.m.dirtyPal = 1;\r
779 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;\r
780\r
781 // pal/ntsc might have changed, reset related stuff\r
782 target_fps = Pico.m.pal ? 50 : 60;\r
783 target_frametime = 1000000/target_fps;\r
784 reset_timing = 1;\r
785\r
786 pemu_sound_start();\r
787\r
788 // prepare CD buffer\r
789 if (PicoAHW & PAHW_MCD) PicoCDBufferInit();\r
790\r
791 // calc vsync offset to sync timing code with vsync\r
792 if (currentConfig.EmuOpt & EOPT_PSYNC) {\r
793 gettimeofday(&tval, 0);\r
794 gp2x_video_wait_vsync();\r
795 gettimeofday(&tval, 0);\r
796 vsync_offset = tval.tv_usec;\r
797 while (vsync_offset >= target_frametime)\r
798 vsync_offset -= target_frametime;\r
799 if (!vsync_offset) vsync_offset++;\r
800 printf("vsync_offset: %i\n", vsync_offset);\r
801 } else\r
802 vsync_offset = 0;\r
803\r
804 frames_done = frames_shown = thissec =\r
805 pframes_done = pframes_shown = pthissec = 0;\r
806\r
807 // loop\r
808 while (engineState == PGS_Running)\r
809 {\r
810 int modes;\r
811\r
812 gettimeofday(&tval, 0);\r
813 if (reset_timing) {\r
814 reset_timing = 0;\r
815 pthissec = tval.tv_sec;\r
816 pframes_shown = pframes_done = tval.tv_usec/target_frametime;\r
817 }\r
818\r
819 // show notice message?\r
820 if (noticeMsgTime.tv_sec)\r
821 {\r
822 static int noticeMsgSum;\r
823 if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec\r
824 noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;\r
825 clearArea(0);\r
826 notice = 0;\r
827 } else {\r
828 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];\r
829 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }\r
830 notice = noticeMsg;\r
831 }\r
832 }\r
833\r
834 // check for mode changes\r
835 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);\r
836 if (modes != oldmodes)\r
837 {\r
838 int scalex = 320;\r
839 osd_fps_x = OSD_FPS_X;\r
840 if (!(modes & 4) && (PicoOpt & POPT_DIS_32C_BORDER)) {\r
841 scalex = 256;\r
842 osd_fps_x = OSD_FPS_X - 64;\r
843 }\r
844 /* want vertical scaling and game is not in 240 line mode */\r
845 if (currentConfig.scaling == EOPT_SCALE_HW_HV && !(modes&8))\r
846 gp2x_video_RGB_setscaling(8, scalex, 224);\r
847 else gp2x_video_RGB_setscaling(0, scalex, 240);\r
848 oldmodes = modes;\r
849 clearArea(1);\r
850 }\r
851\r
852 // second changed?\r
853 if (thissec != tval.tv_sec)\r
854 {\r
855#ifdef BENCHMARK\r
856 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];\r
857 if (++bench == 10) {\r
858 bench = 0;\r
859 bench_fps_s = bench_fps;\r
860 bf[bfp++ & 3] = bench_fps;\r
861 bench_fps = 0;\r
862 }\r
863 bench_fps += frames_shown;\r
864 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);\r
865#else\r
866 if (currentConfig.EmuOpt & 2) {\r
867 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);\r
868 if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }\r
869 }\r
870#endif\r
871 frames_shown = frames_done = 0;\r
872 thissec = tval.tv_sec;\r
873 }\r
874#ifdef PFRAMES\r
875 sprintf(fpsbuff, "%i", Pico.m.frame_count);\r
876#endif\r
877\r
878 if (pthissec != tval.tv_sec)\r
879 {\r
880 if (PsndOut == 0 && currentConfig.Frameskip >= 0) {\r
881 pframes_done = pframes_shown = 0;\r
882 } else {\r
883 // it is quite common for this implementation to leave 1 fame unfinished\r
884 // when second changes, but we don't want buffer to starve.\r
885 if (PsndOut && pframes_done < target_fps && pframes_done > target_fps-5) {\r
886 emu_update_input();\r
887 SkipFrame(1); pframes_done++;\r
888 }\r
889\r
890 pframes_done -= target_fps; if (pframes_done < 0) pframes_done = 0;\r
891 pframes_shown -= target_fps; if (pframes_shown < 0) pframes_shown = 0;\r
892 if (pframes_shown > pframes_done) pframes_shown = pframes_done;\r
893 }\r
894 pthissec = tval.tv_sec;\r
895 }\r
896\r
897 lim_time = (pframes_done+1) * target_frametime + vsync_offset;\r
898 if (currentConfig.Frameskip >= 0) // frameskip enabled\r
899 {\r
900 for(i = 0; i < currentConfig.Frameskip; i++) {\r
901 emu_update_input();\r
902 SkipFrame(1); pframes_done++; frames_done++;\r
903 if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled\r
904 gettimeofday(&tval, 0);\r
905 if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;\r
906 if (tval.tv_usec < lim_time) { // we are too fast\r
907 simpleWait(pthissec, lim_time);\r
908 }\r
909 }\r
910 lim_time += target_frametime;\r
911 }\r
912 }\r
913 else if (tval.tv_usec > lim_time) // auto frameskip\r
914 {\r
915 // no time left for this frame - skip\r
916 if (tval.tv_usec - lim_time >= 300000) {\r
917 /* something caused a slowdown for us (disk access? cache flush?)\r
918 * try to recover by resetting timing... */\r
919 reset_timing = 1;\r
920 continue;\r
921 }\r
922 emu_update_input();\r
923 SkipFrame(tval.tv_usec < lim_time+target_frametime*2); pframes_done++; frames_done++;\r
924 continue;\r
925 }\r
926\r
927 emu_update_input();\r
928 PicoFrame();\r
929\r
930 // check time\r
931 gettimeofday(&tval, 0);\r
932 if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;\r
933\r
934 if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection\r
935 reset_timing = 1;\r
936 else if (PsndOut != NULL || currentConfig.Frameskip < 0)\r
937 {\r
938 // sleep or vsync if we are still too fast\r
939 // usleep sleeps for ~20ms minimum, so it is not a solution here\r
940 if (!reset_timing && tval.tv_usec < lim_time)\r
941 {\r
942 // we are too fast\r
943 if (vsync_offset) {\r
944 if (lim_time - tval.tv_usec > target_frametime/2)\r
945 simpleWait(pthissec, lim_time - target_frametime/4);\r
946 gp2x_video_wait_vsync();\r
947 } else {\r
948 simpleWait(pthissec, lim_time);\r
949 }\r
950 }\r
951 }\r
952\r
953 blit(fpsbuff, notice);\r
954\r
955 pframes_done++; pframes_shown++;\r
956 frames_done++; frames_shown++;\r
957 }\r
958\r
959 emu_set_fastforward(0);\r
960\r
961 if (PicoAHW & PAHW_MCD)\r
962 PicoCDBufferFree();\r
963\r
964 // save SRAM\r
965 if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {\r
966 plat_status_msg_busy_first("Writing SRAM/BRAM...");\r
967 emu_save_load_game(0, 1);\r
968 SRam.changed = 0;\r
969 }\r
970\r
971 // do menu background to be sure it's right\r
972 pemu_forced_frame(POPT_EN_SOFTSCALE);\r
973}\r
974\r
975const char *plat_get_credits(void)\r
976{\r
977 return "PicoDrive v" VERSION " (c) notaz, 2006-2009\n\n\n"\r
978 "Credits:\n"\r
979 "fDave: Cyclone 68000 core,\n"\r
980 " base code of PicoDrive\n"\r
981 "Reesy & FluBBa: DrZ80 core\n"\r
982 "MAME devs: YM2612 and SN76496 cores\n"\r
983 "rlyeh and others: minimal SDK\n"\r
984 "Squidge: mmuhack\n"\r
985 "Dzz: ARM940 sample\n"\r
986 "GnoStiC / Puck2099: USB joy code\n"\r
987 "craigix: GP2X hardware\n"\r
988 "ketchupgun: skin design\n"\r
989 "\n"\r
990 "special thanks (for docs, ideas):\n"\r
991 " Charles MacDonald, Haze,\n"\r
992 " Stephane Dallongeville,\n"\r
993 " Lordus, Exophase, Rokas,\n"\r
994 " Nemesis, Tasco Deluxe";\r
995}\r