some fps counter cosmetics
[picodrive.git] / platform / gp2x / emu.c
CommitLineData
5a681086 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 PicoDraw2FB, then mixed with 32x\r
11 * - tile internal one done on PicoDraw2FB, then mixed with 32x\r
12 */\r
cc68a136 13\r
14#include <stdio.h>\r
15#include <stdlib.h>\r
c7eb229a 16#include <unistd.h>\r
cc68a136 17\r
75a30842 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
e5f426aa 24#include "../common/arm_utils.h"\r
ea8c405f 25#include "../common/emu.h"\r
75a30842 26#include "../common/config_file.h"\r
27#include "../common/version.h"\r
28#include "plat.h"\r
cc68a136 29\r
efcba75f 30#include <pico/pico_int.h>\r
31#include <pico/patch.h>\r
32#include <pico/sound/mix.h>\r
b67ef287 33#include <zlib/zlib.h>\r
cc68a136 34\r
cc68a136 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
cc68a136 41\r
75a30842 42//extern int crashed_940;\r
cc68a136 43\r
f4750ee0 44static int osd_fps_x, osd_y, doing_bg_frame;\r
fcdefcf6 45const char *renderer_names[] = { "16bit accurate", " 8bit accurate", " 8bit fast", NULL };\r
46const char *renderer_names32x[] = { "accurate", "faster", "fastest", NULL };\r
5a681086 47enum renderer_types { RT_16BIT, RT_8BIT_ACC, RT_8BIT_FAST, RT_COUNT };\r
cc68a136 48\r
f4750ee0 49static int (*emu_scan_begin)(unsigned int num) = NULL;\r
50static int (*emu_scan_end)(unsigned int num) = NULL;\r
cc68a136 51\r
2433f409 52\r
f2cf8472 53void pemu_prep_defconfig(void)\r
58c86d00 54{\r
ee2a3bdf 55 gp2x_soc_t soc;\r
56\r
ee2a3bdf 57 defaultConfig.CPUclock = default_cpu_clock;\r
5a681086 58 defaultConfig.renderer32x = RT_8BIT_FAST;\r
45285368 59 defaultConfig.analog_deadzone = 50;\r
ee2a3bdf 60\r
61 soc = soc_detect();\r
62 if (soc == SOCID_MMSP2)\r
63 defaultConfig.s_PicoOpt |= POPT_EXT_FM;\r
8340e7c9 64 else if (soc == SOCID_POLLUX) {\r
61753a67 65 defaultConfig.EmuOpt |= EOPT_WIZ_TEAR_FIX|EOPT_SHOW_RTC;\r
8340e7c9 66 defaultConfig.s_PicoOpt |= POPT_EN_MCD_GFX;\r
67 }\r
58c86d00 68}\r
69\r
697746df 70void pemu_validate_config(void)\r
71{\r
f4750ee0 72 if (gp2x_dev_id != GP2X_DEV_GP2X)\r
697746df 73 PicoOpt &= ~POPT_EXT_FM;\r
f4750ee0 74 if (gp2x_dev_id != GP2X_DEV_WIZ)\r
697746df 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
5a681086 84static int get_renderer(void)\r
85{\r
86 if (PicoAHW & 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 (PicoAHW & PAHW_32X)\r
96 r = &currentConfig.renderer32x;\r
97 else\r
98 r = &currentConfig.renderer;\r
99 *r += diff;\r
f4750ee0 100\r
101 // 8bpp fast is not there (yet?)\r
102 if ((PicoAHW & PAHW_SMS) && *r == RT_8BIT_FAST)\r
103 (*r)++;\r
104\r
5a681086 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 || (PicoAHW & PAHW_32X))\r
113\r
cc41eb4f 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
cc68a136 117{\r
118 int len = strlen(text)*8;\r
e2de9939 119 int *p, i, h, offs;\r
cc68a136 120\r
cc41eb4f 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
cc41eb4f 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
cc68a136 139 }\r
cc41eb4f 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
cc68a136 155}\r
156\r
213c16ad 157static void draw_cd_leds(void)\r
bf098bc5 158{\r
cc41eb4f 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
bf098bc5 171\r
5a681086 172 if (!is_16bit_mode()) {\r
cc41eb4f 173 #define p(x) px[(x) >> 2]\r
bf098bc5 174 // 8-bit modes\r
cc41eb4f 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
bf098bc5 181 } else {\r
cc41eb4f 182 #define p(x) px[(x)*2 >> 2] = px[((x)*2 >> 2) + 1]\r
bf098bc5 183 // 16-bit modes\r
cc41eb4f 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
bf098bc5 190 }\r
191}\r
192\r
213c16ad 193static void draw_pico_ptr(void)\r
194{\r
e2de9939 195 unsigned short *p = (unsigned short *)g_screen_ptr;\r
cc41eb4f 196 int x, y, pitch = 320;\r
213c16ad 197\r
198 // only if pen enabled and for 16bit modes\r
5a681086 199 if (pico_inp_mode == 0 || currentConfig.EmuOpt != RT_16BIT)\r
cc41eb4f 200 return;\r
213c16ad 201\r
cc41eb4f 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) && !(PicoOpt & POPT_DIS_32C_BORDER))\r
205 x += 32;\r
213c16ad 206\r
02da059d 207 if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
cc41eb4f 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
213c16ad 218}\r
219\r
cc41eb4f 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
4981de9c 225 Pico.est.DrawLineDest = rot_buff + (num & 3) * 320 * 2;\r
cc41eb4f 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
cc41eb4f 233 rotated_blit16(g_screen_ptr, rot_buff, num + 1,\r
234 !(Pico.video.reg[12] & 1) && !(PicoOpt & POPT_EN_SOFTSCALE));\r
235 return 0;\r
236}\r
237\r
238static int EmuScanBegin8_rot(unsigned int num)\r
239{\r
4981de9c 240 Pico.est.DrawLineDest = rot_buff + (num & 3) * 320;\r
cc41eb4f 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
cc41eb4f 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
f4750ee0 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
4981de9c 265 Pico.est.DrawLineDest = (char *)g_screen_ptr + 320 * ld_counter * gp2x_current_bpp / 8;\r
f4750ee0 266\r
267 return 0;\r
268}\r
269\r
270static int EmuScanEnd16_ld(unsigned int num)\r
271{\r
4981de9c 272 void *oldline = Pico.est.DrawLineDest;\r
f4750ee0 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
4981de9c 283 memcpy32(Pico.est.DrawLineDest, oldline, 320 * gp2x_current_bpp / 8 / 4);\r
f4750ee0 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
19954be1 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
d01fa204 313 else if (Pico.est.rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes\r
19954be1 314 bgr444_to_rgb32(localPal+0x40, HighPal);\r
315 bgr444_to_rgb32(localPal+0x80, HighPal+0x40);\r
316 }\r
317 else\r
318 memcpy32(localPal+0x80, localPal, 0x40); // 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
cc68a136 339\r
d08e7326 340void pemu_finalize_frame(const char *fps, const char *notice)\r
cc68a136 341{\r
bf098bc5 342 int emu_opt = currentConfig.EmuOpt;\r
19954be1 343 int ret;\r
bf098bc5 344\r
5a681086 345 if (PicoAHW & PAHW_32X)\r
346 ; // nothing to do\r
347 else if (get_renderer() == RT_8BIT_FAST)\r
fad24893 348 {\r
cc68a136 349 // 8bit fast renderer\r
350 if (Pico.m.dirtyPal) {\r
351 Pico.m.dirtyPal = 0;\r
19954be1 352 ret = make_local_pal(1);\r
cc68a136 353 // feed new palette to our device\r
19954be1 354 gp2x_video_setpalette(localPal, ret);\r
cc68a136 355 }\r
fad24893 356 // a hack for VR\r
45f2f245 357 if (PicoAHW & PAHW_SVP)\r
fad24893 358 memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);\r
359 // do actual copy\r
cc41eb4f 360 vidcpyM2(g_screen_ptr, PicoDraw2FB+328*8,\r
361 !(Pico.video.reg[12] & 1), !(PicoOpt & POPT_DIS_32C_BORDER));\r
fad24893 362 }\r
5a681086 363 else if (get_renderer() == RT_8BIT_ACC)\r
fad24893 364 {\r
cc68a136 365 // 8bit accurate renderer\r
602133e1 366 if (Pico.m.dirtyPal)\r
367 {\r
cc68a136 368 Pico.m.dirtyPal = 0;\r
19954be1 369 ret = make_local_pal(0);\r
370 gp2x_video_setpalette(localPal, ret);\r
cc68a136 371 }\r
372 }\r
373\r
f4750ee0 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
697746df 378 if ((PicoAHW & PAHW_MCD) && (emu_opt & EOPT_EN_CD_LEDS))\r
213c16ad 379 draw_cd_leds();\r
380 if (PicoAHW & PAHW_PICO)\r
381 draw_pico_ptr();\r
d08e7326 382}\r
cc68a136 383\r
d08e7326 384void plat_video_flip(void)\r
385{\r
5a681086 386 int stride = g_screen_width;\r
cc68a136 387 gp2x_video_flip();\r
5a681086 388\r
389 if (is_16bit_mode())\r
390 stride *= 2;\r
391 PicoDrawSetOutBuf(g_screen_ptr, stride);\r
cc68a136 392}\r
393\r
b24e0f6c 394/* XXX */\r
b24e0f6c 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
b24e0f6c 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
cc68a136 425{\r
5a681086 426 int is_8bit = !is_16bit_mode();\r
b24e0f6c 427 if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
cc41eb4f 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
b24e0f6c 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
cc68a136 450}\r
451\r
d34a42f9 452void plat_status_msg_busy_next(const char *msg)\r
453{\r
b24e0f6c 454 plat_status_msg_clear();\r
d08e7326 455 pemu_finalize_frame("", msg);\r
456 plat_video_flip();\r
7c34867a 457 emu_status_msg("");\r
d34a42f9 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
cc68a136 469\r
f4750ee0 470static void vid_reset_mode(void)\r
cc68a136 471{\r
5a681086 472 int gp2x_mode = 16;\r
473 int renderer = get_renderer();\r
474\r
f4750ee0 475 PicoOpt &= ~POPT_ALT_RENDERER;\r
476 emu_scan_begin = NULL;\r
477 emu_scan_end = NULL;\r
cc41eb4f 478\r
5a681086 479 switch (renderer) {\r
480 case RT_16BIT:\r
5a681086 481 PicoDrawSetOutFormat(PDF_RGB555, 0);\r
482 PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r
5a681086 483 break;\r
484 case RT_8BIT_ACC:\r
5a681086 485 PicoDrawSetOutFormat(PDF_8BIT, 0);\r
486 PicoDrawSetOutBuf(g_screen_ptr, g_screen_width);\r
5a681086 487 gp2x_mode = 8;\r
488 break;\r
489 case RT_8BIT_FAST:\r
f4750ee0 490 PicoOpt |= POPT_ALT_RENDERER;\r
5a681086 491 PicoDrawSetOutFormat(PDF_NONE, 0);\r
f4750ee0 492 vidcpyM2 = vidcpy_m2;\r
5a681086 493 gp2x_mode = 8;\r
494 break;\r
f4750ee0 495 default:\r
496 printf("bad renderer\n");\r
497 break;\r
bf098bc5 498 }\r
cc41eb4f 499\r
5a681086 500 if (PicoAHW & PAHW_32X) {\r
5a681086 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
5a681086 504 }\r
505 else {\r
f4750ee0 506 PicoDrawSetOutFormat(PDF_NONE, 0);\r
5a681086 507 }\r
508 PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r
45285368 509 gp2x_mode = 16;\r
5a681086 510 }\r
511\r
f4750ee0 512 if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
513 if ((PicoAHW & 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
f7e40c9b 528 osd_text = (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) ? osd_text16_rot : emu_osd_text16;\r
f4750ee0 529 else\r
530 osd_text = (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) ? osd_text8_rot : osd_text8;\r
531\r
f4750ee0 532 gp2x_video_wait_vsync();\r
5a681086 533 if (!is_16bit_mode()) {\r
bf098bc5 534 // setup pal for 8-bit modes\r
535 localPal[0xc0] = 0x0000c000; // MCD LEDs\r
536 localPal[0xd0] = 0x00c00000;\r
cc68a136 537 localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
538 localPal[0xf0] = 0x00ffffff;\r
cc68a136 539 gp2x_video_setpalette(localPal, 0x100);\r
540 gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
cc68a136 541 }\r
f4750ee0 542 else\r
543 gp2x_memset_all_buffers(0, 0, 320*240*2);\r
544\r
45285368 545 if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX)\r
546 gp2x_mode = -gp2x_mode;\r
547\r
31f944ea 548 gp2x_video_changemode(gp2x_mode, Pico.m.pal);\r
45285368 549\r
cc68a136 550 Pico.m.dirtyPal = 1;\r
cc41eb4f 551\r
f4750ee0 552 PicoOpt &= ~POPT_EN_SOFTSCALE;\r
553 if (currentConfig.scaling == EOPT_SCALE_SW)\r
554 PicoOpt |= POPT_EN_SOFTSCALE;\r
19954be1 555\r
556 // palette converters for 8bit modes\r
557 make_local_pal = (PicoAHW & PAHW_SMS) ? make_local_pal_sms : make_local_pal_md;\r
cc68a136 558}\r
559\r
f4750ee0 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 PicoOpt &= ~POPT_DIS_32C_BORDER;\r
573 if (is_32cols && currentConfig.scaling == EOPT_SCALE_HW) {\r
574 scalex = 256;\r
575 PicoOpt |= 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
5a681086 600void plat_video_toggle_renderer(int change, int is_menu_call)\r
66fdc0f0 601{\r
5a681086 602 change_renderer(change);\r
d90f5bd7 603\r
5a681086 604 if (is_menu_call)\r
d90f5bd7 605 return;\r
7a1f6e45 606\r
f4750ee0 607 vid_reset_mode();\r
19954be1 608 rendstatus_old = -1;\r
66fdc0f0 609\r
5a681086 610 if (PicoAHW & PAHW_32X)\r
611 emu_status_msg(renderer_names32x[get_renderer()]);\r
612 else\r
613 emu_status_msg(renderer_names[get_renderer()]);\r
fa1e5e29 614}\r
615\r
f2cf8472 616#if 0 // TODO\r
991473ad 617static void RunEventsPico(unsigned int events)\r
406c96c5 618{\r
f0f0d2df 619 int ret, px, py, lim_x;\r
85a4b5a4 620 static int pdown_frames = 0;\r
621\r
85a4b5a4 622 // for F200\r
623 ret = gp2x_touchpad_read(&px, &py);\r
f0f0d2df 624 if (ret >= 0)\r
625 {\r
626 if (ret > 35000)\r
627 {\r
85a4b5a4 628 if (pdown_frames++ > 5)\r
629 PicoPad[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
f0f0d2df 641 pdown_frames = 0;\r
85a4b5a4 642\r
643 //if (ret == 0)\r
644 // PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;\r
645 }\r
406c96c5 646}\r
f2cf8472 647#endif\r
406c96c5 648\r
d34a42f9 649void plat_update_volume(int has_changed, int is_up)\r
4a32f01f 650{\r
651 static int prev_frame = 0, wait_frames = 0;\r
d34a42f9 652 int need_low_volume = 0;\r
75a30842 653 int vol = currentConfig.volume;\r
d34a42f9 654 gp2x_soc_t soc;\r
655\r
656 soc = soc_detect();\r
657 if ((PicoOpt & POPT_EN_STEREO) && soc == SOCID_MMSP2)\r
658 need_low_volume = 1;\r
4a32f01f 659\r
660 if (has_changed)\r
661 {\r
d34a42f9 662 if (need_low_volume && vol < 5 && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)\r
4a32f01f 663 wait_frames++;\r
664 else {\r
4a32f01f 665 wait_frames = 0;\r
75a30842 666 plat_target_step_volume(&currentConfig.volume, is_up ? 1 : -1);\r
667 vol = currentConfig.volume;\r
4a32f01f 668 }\r
b24e0f6c 669 emu_status_msg("VOL: %02i", vol);\r
4a32f01f 670 prev_frame = Pico.m.frame_count;\r
671 }\r
672\r
54646a39 673 if (!need_low_volume)\r
d34a42f9 674 return;\r
675\r
676 /* set the right mixer func */\r
4a32f01f 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
75a30842 685void pemu_sound_start(void)\r
cc68a136 686{\r
31f944ea 687 gp2x_soc_t soc;\r
7b3f44c6 688\r
31f944ea 689 emu_sound_start();\r
7b3f44c6 690\r
7c34867a 691 if (currentConfig.EmuOpt & EOPT_EN_SOUND)\r
7b3f44c6 692 {\r
b08ccf71 693 soc = soc_detect();\r
31f944ea 694 if (soc == SOCID_POLLUX) {\r
9f6a4e90 695 PsndRate = pollux_get_real_snd_rate(PsndRate);\r
7b3f44c6 696 PsndRerate(Pico.m.frame_count ? 1 : 0);\r
697 }\r
b08ccf71 698\r
31f944ea 699 plat_target_step_volume(&currentConfig.volume, 0);\r
7b3f44c6 700 }\r
701}\r
702\r
9f6a4e90 703static const int sound_rates[] = { 44100, 32000, 22050, 16000, 11025, 8000 };\r
704\r
f2cf8472 705void pemu_sound_stop(void)\r
7b3f44c6 706{\r
9f6a4e90 707 int i;\r
708\r
709 /* get back from Pollux pain */\r
710 PsndRate += 1000;\r
711 for (i = 0; i < ARRAY_SIZE(sound_rates); i++) {\r
712 if (PsndRate >= sound_rates[i]) {\r
713 PsndRate = sound_rates[i];\r
714 break;\r
715 }\r
02da059d 716 }\r
7b3f44c6 717}\r
718\r
45285368 719void pemu_forced_frame(int no_scale, int do_emu)\r
860c6322 720{\r
a4edca53 721 doing_bg_frame = 1;\r
5a681086 722 PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r
f4750ee0 723 PicoDrawSetCallbacks(NULL, NULL);\r
a12e0116 724 Pico.m.dirtyPal = 1;\r
860c6322 725\r
a4edca53 726 emu_cmn_forced_frame(no_scale, do_emu);\r
f4750ee0 727\r
45285368 728 g_menubg_src_ptr = g_screen_ptr;\r
a4edca53 729 doing_bg_frame = 0;\r
860c6322 730}\r
731\r
f2cf8472 732void plat_debug_cat(char *str)\r
fcf94fcc 733{\r
fcf94fcc 734}\r
735\r
75a30842 736void plat_video_loop_prepare(void) \r
da310283 737{\r
75a30842 738 // make sure we are in correct mode\r
739 change_renderer(0);\r
740 vid_reset_mode();\r
da310283 741}\r
da310283 742\r
b24e0f6c 743void pemu_loop_prep(void)\r
cc68a136 744{\r
45285368 745 if (gp2x_dev_id == GP2X_DEV_CAANOO)\r
75a30842 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
cc68a136 749\r
c7eb229a 750 // dirty buffers better go now than during gameplay\r
751 sync();\r
752 sleep(0);\r
b24e0f6c 753}\r
cc68a136 754\r
b24e0f6c 755void pemu_loop_end(void)\r
756{\r
02da059d 757 pemu_sound_stop();\r
758\r
730259b7 759 /* do one more frame for menu bg */\r
45285368 760 pemu_forced_frame(0, 1);\r
cc68a136 761}\r
762\r