add a header fixup
[picodrive.git] / platform / gp2x / emu.c
... / ...
CommitLineData
1/*\r
2 * (c) Copyright 2006-2010 notaz, All rights reserved.\r
3 *\r
4 * For performance reasons 3 renderers are exported for both MD and 32x modes:\r
5 * - 16bpp line renderer\r
6 * - 8bpp line renderer (slightly faster)\r
7 * - 8bpp tile renderer\r
8 * In 32x mode:\r
9 * - 32x layer is overlayed on top of 16bpp one\r
10 * - line internal one done on .Draw2FB, then mixed with 32x\r
11 * - tile internal one done on .Draw2FB, then mixed with 32x\r
12 */\r
13\r
14#include <stdio.h>\r
15#include <stdlib.h>\r
16#include <unistd.h>\r
17\r
18#include "../libpicofe/gp2x/plat_gp2x.h"\r
19#include "../libpicofe/gp2x/soc.h"\r
20#include "../libpicofe/input.h"\r
21#include "../libpicofe/plat.h"\r
22#include "../libpicofe/gp2x/soc_pollux.h"\r
23#include "../common/menu_pico.h"\r
24#include "../common/arm_utils.h"\r
25#include "../common/emu.h"\r
26#include "../common/config_file.h"\r
27#include "../common/version.h"\r
28#include "plat.h"\r
29\r
30#include <pico/pico_int.h>\r
31#include <pico/patch.h>\r
32#include <pico/sound/mix.h>\r
33#include <zlib.h>\r
34\r
35#ifdef BENCHMARK\r
36#define OSD_FPS_X 220\r
37#else\r
38#define OSD_FPS_X 260\r
39#endif\r
40\r
41\r
42//extern int crashed_940;\r
43\r
44static int osd_fps_x, osd_y, doing_bg_frame;\r
45const char *renderer_names[] = { "16bit accurate", " 8bit accurate", " 8bit fast", NULL };\r
46const char *renderer_names32x[] = { "accurate", "faster", "fastest", NULL };\r
47enum renderer_types { RT_16BIT, RT_8BIT_ACC, RT_8BIT_FAST, RT_COUNT };\r
48\r
49static int (*emu_scan_begin)(unsigned int num) = NULL;\r
50static int (*emu_scan_end)(unsigned int num) = NULL;\r
51\r
52\r
53void pemu_prep_defconfig(void)\r
54{\r
55 gp2x_soc_t soc;\r
56\r
57 defaultConfig.CPUclock = default_cpu_clock;\r
58 defaultConfig.renderer32x = RT_8BIT_FAST;\r
59 defaultConfig.analog_deadzone = 50;\r
60\r
61 soc = soc_detect();\r
62 if (soc == SOCID_MMSP2)\r
63 defaultConfig.s_PicoOpt |= POPT_EXT_FM;\r
64 else if (soc == SOCID_POLLUX) {\r
65 defaultConfig.EmuOpt |= EOPT_WIZ_TEAR_FIX|EOPT_SHOW_RTC;\r
66 defaultConfig.s_PicoOpt |= POPT_EN_MCD_GFX;\r
67 }\r
68}\r
69\r
70void pemu_validate_config(void)\r
71{\r
72 if (gp2x_dev_id != GP2X_DEV_GP2X)\r
73 PicoIn.opt &= ~POPT_EXT_FM;\r
74 if (gp2x_dev_id != GP2X_DEV_WIZ)\r
75 currentConfig.EmuOpt &= ~EOPT_WIZ_TEAR_FIX;\r
76\r
77 if (currentConfig.gamma < 10 || currentConfig.gamma > 300)\r
78 currentConfig.gamma = 100;\r
79\r
80 if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 1024)\r
81 currentConfig.CPUclock = default_cpu_clock;\r
82}\r
83\r
84static int get_renderer(void)\r
85{\r
86 if (PicoIn.AHW & PAHW_32X)\r
87 return currentConfig.renderer32x;\r
88 else\r
89 return currentConfig.renderer;\r
90}\r
91\r
92static void change_renderer(int diff)\r
93{\r
94 int *r;\r
95 if (PicoIn.AHW & PAHW_32X)\r
96 r = &currentConfig.renderer32x;\r
97 else\r
98 r = &currentConfig.renderer;\r
99 *r += diff;\r
100\r
101 // 8bpp fast is not there (yet?)\r
102 if ((PicoIn.AHW & PAHW_SMS) && *r == RT_8BIT_FAST)\r
103 (*r)++;\r
104\r
105 if (*r >= RT_COUNT)\r
106 *r = 0;\r
107 else if (*r < 0)\r
108 *r = RT_COUNT - 1;\r
109}\r
110\r
111#define is_16bit_mode() \\r
112 (get_renderer() == RT_16BIT || (PicoIn.AHW & PAHW_32X))\r
113\r
114static void (*osd_text)(int x, int y, const char *text);\r
115\r
116static void osd_text8(int x, int y, const char *text)\r
117{\r
118 int len = strlen(text)*8;\r
119 int *p, i, h, offs;\r
120\r
121 len = (len+3) >> 2;\r
122 for (h = 0; h < 8; h++) {\r
123 offs = (x + g_screen_width * (y+h)) & ~3;\r
124 p = (int *) ((char *)g_screen_ptr + offs);\r
125 for (i = len; i; i--, p++)\r
126 *p = 0xe0e0e0e0;\r
127 }\r
128 emu_text_out8(x, y, text);\r
129}\r
130\r
131static void osd_text8_rot(int x, int y, const char *text)\r
132{\r
133 int len = strlen(text) * 8;\r
134 char *p = (char *)g_screen_ptr + 240*(320-x) + y;\r
135\r
136 while (len--) {\r
137 memset(p, 0xe0, 8);\r
138 p -= 240;\r
139 }\r
140\r
141 emu_text_out8_rot(x, y, text);\r
142}\r
143\r
144static void osd_text16_rot(int x, int y, const char *text)\r
145{\r
146 int len = strlen(text) * 8;\r
147 short *p = (short *)g_screen_ptr + 240*(320-x) + y;\r
148\r
149 while (len--) {\r
150 memset(p, 0, 8*2);\r
151 p -= 240;\r
152 }\r
153\r
154 emu_text_out16_rot(x, y, text);\r
155}\r
156\r
157static void draw_cd_leds(void)\r
158{\r
159 int led_reg, pitch, scr_offs, led_offs;\r
160 led_reg = Pico_mcd->s68k_regs[0];\r
161\r
162 if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
163 pitch = 240;\r
164 led_offs = -pitch * 6;\r
165 scr_offs = pitch * (320 - 4);\r
166 } else {\r
167 pitch = 320;\r
168 led_offs = 4;\r
169 scr_offs = pitch * 2 + 4;\r
170 }\r
171\r
172 if (!is_16bit_mode()) {\r
173 #define p(x) px[(x) >> 2]\r
174 // 8-bit modes\r
175 unsigned int *px = (unsigned int *)((char *)g_screen_ptr + scr_offs);\r
176 unsigned int col_g = (led_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;\r
177 unsigned int col_r = (led_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;\r
178 p(pitch*0) = p(pitch*1) = p(pitch*2) = col_g;\r
179 p(pitch*0 + led_offs) = p(pitch*1 + led_offs) = p(pitch*2 + led_offs) = col_r;\r
180 #undef p\r
181 } else {\r
182 #define p(x) px[(x)*2 >> 2] = px[((x)*2 >> 2) + 1]\r
183 // 16-bit modes\r
184 unsigned int *px = (unsigned int *)((short *)g_screen_ptr + scr_offs);\r
185 unsigned int col_g = (led_reg & 2) ? 0x06000600 : 0;\r
186 unsigned int col_r = (led_reg & 1) ? 0xc000c000 : 0;\r
187 p(pitch*0) = p(pitch*1) = p(pitch*2) = col_g;\r
188 p(pitch*0 + led_offs) = p(pitch*1 + led_offs) = p(pitch*2 + led_offs) = col_r;\r
189 #undef p\r
190 }\r
191}\r
192\r
193static void draw_pico_ptr(void)\r
194{\r
195 unsigned short *p = (unsigned short *)g_screen_ptr;\r
196 int x, y, pitch = 320;\r
197\r
198 // only if pen enabled and for 16bit modes\r
199 if (pico_inp_mode == 0 || currentConfig.EmuOpt != RT_16BIT)\r
200 return;\r
201\r
202 x = pico_pen_x + PICO_PEN_ADJUST_X;\r
203 y = pico_pen_y + PICO_PEN_ADJUST_Y;\r
204 if (!(Pico.video.reg[12]&1) && !(PicoIn.opt & POPT_DIS_32C_BORDER))\r
205 x += 32;\r
206\r
207 if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
208 pitch = 240;\r
209 p += (319 - x) * pitch + y;\r
210 } else\r
211 p += x + y * pitch;\r
212\r
213 p[0] ^= 0xffff;\r
214 p[pitch-1] ^= 0xffff;\r
215 p[pitch] ^= 0xffff;\r
216 p[pitch+1] ^= 0xffff;\r
217 p[pitch*2] ^= 0xffff;\r
218}\r
219\r
220/* rot thing for Wiz */\r
221static unsigned char __attribute__((aligned(4))) rot_buff[320*4*2];\r
222\r
223static int EmuScanBegin16_rot(unsigned int num)\r
224{\r
225 Pico.est.DrawLineDest = rot_buff + (num & 3) * 320 * 2;\r
226 return 0;\r
227}\r
228\r
229static int EmuScanEnd16_rot(unsigned int num)\r
230{\r
231 if ((num & 3) != 3)\r
232 return 0;\r
233 rotated_blit16(g_screen_ptr, rot_buff, num + 1,\r
234 !(Pico.video.reg[12] & 1) && !(PicoIn.opt & POPT_EN_SOFTSCALE));\r
235 return 0;\r
236}\r
237\r
238static int EmuScanBegin8_rot(unsigned int num)\r
239{\r
240 Pico.est.DrawLineDest = rot_buff + (num & 3) * 320;\r
241 return 0;\r
242}\r
243\r
244static int EmuScanEnd8_rot(unsigned int num)\r
245{\r
246 if ((num & 3) != 3)\r
247 return 0;\r
248 rotated_blit8(g_screen_ptr, rot_buff, num + 1,\r
249 !(Pico.video.reg[12] & 1));\r
250 return 0;\r
251}\r
252\r
253/* line doublers */\r
254static unsigned int ld_counter;\r
255static int ld_left, ld_lines;\r
256\r
257static int EmuScanBegin16_ld(unsigned int num)\r
258{\r
259 if ((signed int)(ld_counter - num) > 100)\r
260 ld_counter = 0;\r
261\r
262 if (emu_scan_begin)\r
263 return emu_scan_begin(ld_counter);\r
264 else\r
265 Pico.est.DrawLineDest = (char *)g_screen_ptr + 320 * ld_counter * gp2x_current_bpp / 8;\r
266\r
267 return 0;\r
268}\r
269\r
270static int EmuScanEnd16_ld(unsigned int num)\r
271{\r
272 void *oldline = Pico.est.DrawLineDest;\r
273\r
274 if (emu_scan_end)\r
275 emu_scan_end(ld_counter);\r
276\r
277 ld_counter++;\r
278 ld_left--;\r
279 if (ld_left <= 0) {\r
280 ld_left = ld_lines;\r
281\r
282 EmuScanBegin16_ld(num);\r
283 memcpy(Pico.est.DrawLineDest, oldline, 320 * gp2x_current_bpp / 8);\r
284 if (emu_scan_end)\r
285 emu_scan_end(ld_counter);\r
286\r
287 ld_counter++;\r
288 }\r
289\r
290 return 0;\r
291}\r
292\r
293static int localPal[0x100];\r
294static void (*vidcpyM2)(void *dest, void *src, int m32col, int with_32c_border);\r
295static int (*make_local_pal)(int fast_mode);\r
296\r
297static int make_local_pal_md(int fast_mode)\r
298{\r
299 int pallen = 0xc0;\r
300\r
301 bgr444_to_rgb32(localPal, Pico.cram);\r
302 if (fast_mode)\r
303 return 0x40;\r
304\r
305 if (Pico.video.reg[0xC] & 8) { // shadow/hilight mode\r
306 bgr444_to_rgb32_sh(localPal, Pico.cram);\r
307 localPal[0xc0] = 0x0000c000;\r
308 localPal[0xd0] = 0x00c00000;\r
309 localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
310 localPal[0xf0] = 0x00ffffff;\r
311 pallen = 0x100;\r
312 }\r
313 else if (Pico.est.rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes\r
314 bgr444_to_rgb32(localPal+0x40, Pico.est.HighPal);\r
315 bgr444_to_rgb32(localPal+0x80, Pico.est.HighPal+0x40);\r
316 }\r
317 else\r
318 memcpy(localPal + 0x80, localPal, 0x40 * 4); // for spr prio mess\r
319\r
320 return pallen;\r
321}\r
322\r
323static int make_local_pal_sms(int fast_mode)\r
324{\r
325 unsigned short *spal = Pico.cram;\r
326 unsigned int *dpal = (void *)localPal;\r
327 unsigned int i, t;\r
328\r
329 for (i = 0x40; i > 0; i--) {\r
330 t = *spal++;\r
331 t = ((t & 0x0003) << 22) | ((t & 0x000c) << 12) | ((t & 0x0030) << 2);\r
332 t |= t >> 2;\r
333 t |= t >> 4;\r
334 *dpal++ = t;\r
335 }\r
336\r
337 return 0x40;\r
338}\r
339\r
340void pemu_finalize_frame(const char *fps, const char *notice)\r
341{\r
342 int emu_opt = currentConfig.EmuOpt;\r
343 int ret;\r
344\r
345 if (PicoIn.AHW & PAHW_32X)\r
346 ; // nothing to do\r
347 else if (get_renderer() == RT_8BIT_FAST)\r
348 {\r
349 // 8bit fast renderer\r
350 if (Pico.m.dirtyPal) {\r
351 Pico.m.dirtyPal = 0;\r
352 ret = make_local_pal(1);\r
353 // feed new palette to our device\r
354 gp2x_video_setpalette(localPal, ret);\r
355 }\r
356 // a hack for VR\r
357 if (PicoIn.AHW & PAHW_SVP)\r
358 memset32((int *)(Pico.est.Draw2FB+328*8+328*223), 0xe0e0e0e0, 328);\r
359 // do actual copy\r
360 vidcpyM2(g_screen_ptr, Pico.est.Draw2FB+328*8,\r
361 !(Pico.video.reg[12] & 1), !(PicoIn.opt & POPT_DIS_32C_BORDER));\r
362 }\r
363 else if (get_renderer() == RT_8BIT_ACC)\r
364 {\r
365 // 8bit accurate renderer\r
366 if (Pico.m.dirtyPal)\r
367 {\r
368 Pico.m.dirtyPal = 0;\r
369 ret = make_local_pal(0);\r
370 gp2x_video_setpalette(localPal, ret);\r
371 }\r
372 }\r
373\r
374 if (notice)\r
375 osd_text(4, osd_y, notice);\r
376 if (emu_opt & EOPT_SHOW_FPS)\r
377 osd_text(osd_fps_x, osd_y, fps);\r
378 if ((PicoIn.AHW & PAHW_MCD) && (emu_opt & EOPT_EN_CD_LEDS))\r
379 draw_cd_leds();\r
380 if (PicoIn.AHW & PAHW_PICO)\r
381 draw_pico_ptr();\r
382}\r
383\r
384void plat_video_flip(void)\r
385{\r
386 int stride = g_screen_width;\r
387 gp2x_video_flip();\r
388\r
389 if (is_16bit_mode())\r
390 stride *= 2;\r
391 PicoDrawSetOutBuf(g_screen_ptr, stride);\r
392}\r
393\r
394/* XXX */\r
395unsigned int plat_get_ticks_ms(void)\r
396{\r
397 return gp2x_get_ticks_ms();\r
398}\r
399\r
400unsigned int plat_get_ticks_us(void)\r
401{\r
402 return gp2x_get_ticks_us();\r
403}\r
404\r
405void plat_wait_till_us(unsigned int us_to)\r
406{\r
407 unsigned int now;\r
408\r
409 spend_cycles(1024);\r
410 now = plat_get_ticks_us();\r
411\r
412 while ((signed int)(us_to - now) > 512)\r
413 {\r
414 spend_cycles(1024);\r
415 now = plat_get_ticks_us();\r
416 }\r
417}\r
418\r
419void plat_video_wait_vsync(void)\r
420{\r
421 gp2x_video_wait_vsync();\r
422}\r
423\r
424void plat_status_msg_clear(void)\r
425{\r
426 int is_8bit = !is_16bit_mode();\r
427 if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
428 /* ugh.. */\r
429 int i, u, *p;\r
430 if (is_8bit) {\r
431 for (i = 0; i < 4; i++) {\r
432 p = (int *)gp2x_screens[i] + (240-8) / 4;\r
433 for (u = 320; u > 0; u--, p += 240/4)\r
434 p[0] = p[1] = 0xe0e0e0e0;\r
435 }\r
436 } else {\r
437 for (i = 0; i < 4; i++) {\r
438 p = (int *)gp2x_screens[i] + (240-8)*2 / 4;\r
439 for (u = 320; u > 0; u--, p += 240*2/4)\r
440 p[0] = p[1] = p[2] = p[3] = 0;\r
441 }\r
442 }\r
443 return;\r
444 }\r
445\r
446 if (is_8bit)\r
447 gp2x_memset_all_buffers(320*232, 0xe0, 320*8);\r
448 else\r
449 gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);\r
450}\r
451\r
452void plat_status_msg_busy_next(const char *msg)\r
453{\r
454 plat_status_msg_clear();\r
455 pemu_finalize_frame("", msg);\r
456 plat_video_flip();\r
457 emu_status_msg("");\r
458\r
459 /* assumption: msg_busy_next gets called only when\r
460 * something slow is about to happen */\r
461 reset_timing = 1;\r
462}\r
463\r
464void plat_status_msg_busy_first(const char *msg)\r
465{\r
466 gp2x_memcpy_all_buffers(g_screen_ptr, 0, 320*240*2);\r
467 plat_status_msg_busy_next(msg);\r
468}\r
469\r
470static void vid_reset_mode(void)\r
471{\r
472 int gp2x_mode = 16;\r
473 int renderer = get_renderer();\r
474\r
475 PicoIn.opt &= ~POPT_ALT_RENDERER;\r
476 emu_scan_begin = NULL;\r
477 emu_scan_end = NULL;\r
478\r
479 switch (renderer) {\r
480 case RT_16BIT:\r
481 PicoDrawSetOutFormat(PDF_RGB555, 0);\r
482 PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r
483 break;\r
484 case RT_8BIT_ACC:\r
485 PicoDrawSetOutFormat(PDF_8BIT, 0);\r
486 PicoDrawSetOutBuf(g_screen_ptr, g_screen_width);\r
487 gp2x_mode = 8;\r
488 break;\r
489 case RT_8BIT_FAST:\r
490 PicoIn.opt |= POPT_ALT_RENDERER;\r
491 PicoDrawSetOutFormat(PDF_NONE, 0);\r
492 vidcpyM2 = vidcpy_m2;\r
493 gp2x_mode = 8;\r
494 break;\r
495 default:\r
496 printf("bad renderer\n");\r
497 break;\r
498 }\r
499\r
500 if (PicoIn.AHW & PAHW_32X) {\r
501 // Wiz 16bit is an exception, uses line rendering due to rotation mess\r
502 if (renderer == RT_16BIT && (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX)) {\r
503 PicoDrawSetOutFormat(PDF_RGB555, 1);\r
504 }\r
505 else {\r
506 PicoDrawSetOutFormat(PDF_NONE, 0);\r
507 }\r
508 PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r
509 gp2x_mode = 16;\r
510 }\r
511\r
512 if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
513 if ((PicoIn.AHW & PAHW_32X) || renderer == RT_16BIT) {\r
514 emu_scan_begin = EmuScanBegin16_rot;\r
515 emu_scan_end = EmuScanEnd16_rot;\r
516 }\r
517 else if (renderer == RT_8BIT_ACC) {\r
518 emu_scan_begin = EmuScanBegin8_rot;\r
519 emu_scan_end = EmuScanEnd8_rot;\r
520 }\r
521 else if (renderer == RT_8BIT_FAST)\r
522 vidcpyM2 = vidcpy_m2_rot;\r
523 }\r
524\r
525 PicoDrawSetCallbacks(emu_scan_begin, emu_scan_end);\r
526\r
527 if (is_16bit_mode())\r
528 osd_text = (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) ? osd_text16_rot : emu_osd_text16;\r
529 else\r
530 osd_text = (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) ? osd_text8_rot : osd_text8;\r
531\r
532 gp2x_video_wait_vsync();\r
533 if (!is_16bit_mode()) {\r
534 // setup pal for 8-bit modes\r
535 localPal[0xc0] = 0x0000c000; // MCD LEDs\r
536 localPal[0xd0] = 0x00c00000;\r
537 localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
538 localPal[0xf0] = 0x00ffffff;\r
539 gp2x_video_setpalette(localPal, 0x100);\r
540 gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
541 }\r
542 else\r
543 gp2x_memset_all_buffers(0, 0, 320*240*2);\r
544\r
545 if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX)\r
546 gp2x_mode = -gp2x_mode;\r
547\r
548 gp2x_video_changemode(gp2x_mode, Pico.m.pal);\r
549\r
550 Pico.m.dirtyPal = 1;\r
551\r
552 PicoIn.opt &= ~POPT_EN_SOFTSCALE;\r
553 if (currentConfig.scaling == EOPT_SCALE_SW)\r
554 PicoIn.opt |= POPT_EN_SOFTSCALE;\r
555\r
556 // palette converters for 8bit modes\r
557 make_local_pal = (PicoIn.AHW & PAHW_SMS) ? make_local_pal_sms : make_local_pal_md;\r
558}\r
559\r
560void emu_video_mode_change(int start_line, int line_count, int is_32cols)\r
561{\r
562 int scalex = 320, scaley = 240;\r
563 int ln_offs = 0;\r
564\r
565 if (doing_bg_frame)\r
566 return;\r
567\r
568 osd_fps_x = OSD_FPS_X;\r
569 osd_y = 232;\r
570\r
571 /* set up hwscaling here */\r
572 PicoIn.opt &= ~POPT_DIS_32C_BORDER;\r
573 if (is_32cols && currentConfig.scaling == EOPT_SCALE_HW) {\r
574 scalex = 256;\r
575 PicoIn.opt |= POPT_DIS_32C_BORDER;\r
576 osd_fps_x = OSD_FPS_X - 64;\r
577 }\r
578\r
579 if (currentConfig.vscaling == EOPT_SCALE_HW) {\r
580 ln_offs = start_line;\r
581 scaley = line_count;\r
582 osd_y = start_line + line_count - 8;\r
583 }\r
584\r
585 gp2x_video_RGB_setscaling(ln_offs, scalex, scaley);\r
586\r
587 /* line doubling */\r
588 if (currentConfig.vscaling == EOPT_SCALE_SW && line_count < 240) {\r
589 ld_lines = ld_left = line_count / (240 - line_count);\r
590 PicoDrawSetCallbacks(EmuScanBegin16_ld, EmuScanEnd16_ld);\r
591 }\r
592\r
593 // clear whole screen in all buffers\r
594 if (!is_16bit_mode())\r
595 gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
596 else\r
597 gp2x_memset_all_buffers(0, 0, 320*240*2);\r
598}\r
599\r
600void plat_video_toggle_renderer(int change, int is_menu_call)\r
601{\r
602 change_renderer(change);\r
603\r
604 if (is_menu_call)\r
605 return;\r
606\r
607 vid_reset_mode();\r
608 rendstatus_old = -1;\r
609\r
610 if (PicoIn.AHW & PAHW_32X)\r
611 emu_status_msg(renderer_names32x[get_renderer()]);\r
612 else\r
613 emu_status_msg(renderer_names[get_renderer()]);\r
614}\r
615\r
616#if 0 // TODO\r
617static void RunEventsPico(unsigned int events)\r
618{\r
619 int ret, px, py, lim_x;\r
620 static int pdown_frames = 0;\r
621\r
622 // for F200\r
623 ret = gp2x_touchpad_read(&px, &py);\r
624 if (ret >= 0)\r
625 {\r
626 if (ret > 35000)\r
627 {\r
628 if (pdown_frames++ > 5)\r
629 PicoIn.pad[0] |= 0x20;\r
630\r
631 pico_pen_x = px;\r
632 pico_pen_y = py;\r
633 if (!(Pico.video.reg[12]&1)) {\r
634 pico_pen_x -= 32;\r
635 if (pico_pen_x < 0) pico_pen_x = 0;\r
636 if (pico_pen_x > 248) pico_pen_x = 248;\r
637 }\r
638 if (pico_pen_y > 224) pico_pen_y = 224;\r
639 }\r
640 else\r
641 pdown_frames = 0;\r
642\r
643 //if (ret == 0)\r
644 // PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;\r
645 }\r
646}\r
647#endif\r
648\r
649void plat_update_volume(int has_changed, int is_up)\r
650{\r
651 static int prev_frame = 0, wait_frames = 0;\r
652 int need_low_volume = 0;\r
653 int vol = currentConfig.volume;\r
654 gp2x_soc_t soc;\r
655\r
656 soc = soc_detect();\r
657 if ((PicoIn.opt & POPT_EN_STEREO) && soc == SOCID_MMSP2)\r
658 need_low_volume = 1;\r
659\r
660 if (has_changed)\r
661 {\r
662 if (need_low_volume && vol < 5 && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)\r
663 wait_frames++;\r
664 else {\r
665 wait_frames = 0;\r
666 plat_target_step_volume(&currentConfig.volume, is_up ? 1 : -1);\r
667 vol = currentConfig.volume;\r
668 }\r
669 emu_status_msg("VOL: %02i", vol);\r
670 prev_frame = Pico.m.frame_count;\r
671 }\r
672\r
673 if (!need_low_volume)\r
674 return;\r
675\r
676 /* set the right mixer func */\r
677 if (vol >= 5)\r
678 PsndMix_32_to_16l = mix_32_to_16l_stereo;\r
679 else {\r
680 mix_32_to_16l_level = 5 - vol;\r
681 PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;\r
682 }\r
683}\r
684\r
685void pemu_sound_start(void)\r
686{\r
687 gp2x_soc_t soc;\r
688\r
689 emu_sound_start();\r
690\r
691 if (currentConfig.EmuOpt & EOPT_EN_SOUND)\r
692 {\r
693 soc = soc_detect();\r
694 if (soc == SOCID_POLLUX) {\r
695 PicoIn.sndRate = pollux_get_real_snd_rate(PicoIn.sndRate);\r
696 PsndRerate(Pico.m.frame_count ? 1 : 0);\r
697 }\r
698\r
699 plat_target_step_volume(&currentConfig.volume, 0);\r
700 }\r
701}\r
702\r
703static const int sound_rates[] = { 44100, 32000, 22050, 16000, 11025, 8000 };\r
704\r
705void pemu_sound_stop(void)\r
706{\r
707 int i;\r
708\r
709 /* get back from Pollux pain */\r
710 PicoIn.sndRate += 1000;\r
711 for (i = 0; i < ARRAY_SIZE(sound_rates); i++) {\r
712 if (PicoIn.sndRate >= sound_rates[i]) {\r
713 PicoIn.sndRate = sound_rates[i];\r
714 break;\r
715 }\r
716 }\r
717}\r
718\r
719void pemu_forced_frame(int no_scale, int do_emu)\r
720{\r
721 doing_bg_frame = 1;\r
722 PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r
723 PicoDrawSetCallbacks(NULL, NULL);\r
724 Pico.m.dirtyPal = 1;\r
725\r
726 emu_cmn_forced_frame(no_scale, do_emu);\r
727\r
728 g_menubg_src_ptr = g_screen_ptr;\r
729 doing_bg_frame = 0;\r
730}\r
731\r
732void plat_debug_cat(char *str)\r
733{\r
734}\r
735\r
736void plat_video_loop_prepare(void) \r
737{\r
738 // make sure we are in correct mode\r
739 change_renderer(0);\r
740 vid_reset_mode();\r
741}\r
742\r
743void pemu_loop_prep(void)\r
744{\r
745 if (gp2x_dev_id == GP2X_DEV_CAANOO)\r
746 in_set_config_int(in_name_to_id("evdev:pollux-analog"),\r
747 IN_CFG_ABS_DEAD_ZONE,\r
748 currentConfig.analog_deadzone);\r
749\r
750 // dirty buffers better go now than during gameplay\r
751 sync();\r
752 sleep(0);\r
753}\r
754\r
755void pemu_loop_end(void)\r
756{\r
757 pemu_sound_stop();\r
758\r
759 /* do one more frame for menu bg */\r
760 pemu_forced_frame(0, 1);\r
761}\r
762\r