input: kill "merge with defaults" behavior
[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
0d9bf4fc 18#include "plat_gp2x.h"\r
42171343 19#include "soc.h"\r
9f6a4e90 20#include "soc_pollux.h"\r
bcd94522 21#include "../common/plat.h"\r
991473ad 22#include "../common/menu.h"\r
e5f426aa 23#include "../common/arm_utils.h"\r
24#include "../common/fonts.h"\r
ea8c405f 25#include "../common/emu.h"\r
58c86d00 26#include "../common/config.h"\r
45285368 27#include "../common/input.h"\r
e5ab6faf 28#include "../linux/sndout_oss.h"\r
713c9224 29#include "version.h"\r
cc68a136 30\r
efcba75f 31#include <pico/pico_int.h>\r
32#include <pico/patch.h>\r
33#include <pico/sound/mix.h>\r
b67ef287 34#include <zlib/zlib.h>\r
cc68a136 35\r
cc68a136 36#ifdef BENCHMARK\r
37#define OSD_FPS_X 220\r
38#else\r
39#define OSD_FPS_X 260\r
40#endif\r
41\r
cc68a136 42\r
cc68a136 43extern int crashed_940;\r
44\r
02da059d 45static short __attribute__((aligned(4))) sndBuffer[2*(44100+100)/50];\r
f2cf8472 46static unsigned char PicoDraw2FB_[(8+320) * (8+240+8)];\r
47unsigned char *PicoDraw2FB = PicoDraw2FB_;\r
f4750ee0 48static int osd_fps_x, osd_y, doing_bg_frame;\r
fcdefcf6 49const char *renderer_names[] = { "16bit accurate", " 8bit accurate", " 8bit fast", NULL };\r
50const char *renderer_names32x[] = { "accurate", "faster", "fastest", NULL };\r
5a681086 51enum renderer_types { RT_16BIT, RT_8BIT_ACC, RT_8BIT_FAST, RT_COUNT };\r
cc68a136 52\r
f4750ee0 53static int (*emu_scan_begin)(unsigned int num) = NULL;\r
54static int (*emu_scan_end)(unsigned int num) = NULL;\r
cc68a136 55\r
f4750ee0 56extern void *gp2x_screens[4];\r
2433f409 57\r
58\r
f2cf8472 59void pemu_prep_defconfig(void)\r
58c86d00 60{\r
ee2a3bdf 61 gp2x_soc_t soc;\r
62\r
ee2a3bdf 63 defaultConfig.CPUclock = default_cpu_clock;\r
5a681086 64 defaultConfig.renderer32x = RT_8BIT_FAST;\r
45285368 65 defaultConfig.analog_deadzone = 50;\r
ee2a3bdf 66\r
67 soc = soc_detect();\r
68 if (soc == SOCID_MMSP2)\r
69 defaultConfig.s_PicoOpt |= POPT_EXT_FM;\r
cc41eb4f 70 else if (soc == SOCID_POLLUX)\r
61753a67 71 defaultConfig.EmuOpt |= EOPT_WIZ_TEAR_FIX|EOPT_SHOW_RTC;\r
58c86d00 72}\r
73\r
697746df 74void pemu_validate_config(void)\r
75{\r
f4750ee0 76 if (gp2x_dev_id != GP2X_DEV_GP2X)\r
697746df 77 PicoOpt &= ~POPT_EXT_FM;\r
f4750ee0 78 if (gp2x_dev_id != GP2X_DEV_WIZ)\r
697746df 79 currentConfig.EmuOpt &= ~EOPT_WIZ_TEAR_FIX;\r
80\r
81 if (currentConfig.gamma < 10 || currentConfig.gamma > 300)\r
82 currentConfig.gamma = 100;\r
83\r
84 if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 1024)\r
85 currentConfig.CPUclock = default_cpu_clock;\r
86}\r
87\r
5a681086 88static int get_renderer(void)\r
89{\r
90 if (PicoAHW & PAHW_32X)\r
91 return currentConfig.renderer32x;\r
92 else\r
93 return currentConfig.renderer;\r
94}\r
95\r
96static void change_renderer(int diff)\r
97{\r
98 int *r;\r
99 if (PicoAHW & PAHW_32X)\r
100 r = &currentConfig.renderer32x;\r
101 else\r
102 r = &currentConfig.renderer;\r
103 *r += diff;\r
f4750ee0 104\r
105 // 8bpp fast is not there (yet?)\r
106 if ((PicoAHW & PAHW_SMS) && *r == RT_8BIT_FAST)\r
107 (*r)++;\r
108\r
5a681086 109 if (*r >= RT_COUNT)\r
110 *r = 0;\r
111 else if (*r < 0)\r
112 *r = RT_COUNT - 1;\r
113}\r
114\r
115#define is_16bit_mode() \\r
116 (get_renderer() == RT_16BIT || (PicoAHW & PAHW_32X))\r
117\r
cc41eb4f 118static void (*osd_text)(int x, int y, const char *text);\r
119\r
120static void osd_text8(int x, int y, const char *text)\r
cc68a136 121{\r
122 int len = strlen(text)*8;\r
e2de9939 123 int *p, i, h, offs;\r
cc68a136 124\r
cc41eb4f 125 len = (len+3) >> 2;\r
126 for (h = 0; h < 8; h++) {\r
127 offs = (x + g_screen_width * (y+h)) & ~3;\r
128 p = (int *) ((char *)g_screen_ptr + offs);\r
129 for (i = len; i; i--, p++)\r
130 *p = 0xe0e0e0e0;\r
131 }\r
132 emu_text_out8(x, y, text);\r
133}\r
134\r
135static void osd_text16(int x, int y, const char *text)\r
136{\r
137 int len = strlen(text)*8;\r
138 int *p, i, h, offs;\r
139\r
140 len = (len+1) >> 1;\r
141 for (h = 0; h < 8; h++) {\r
142 offs = (x + g_screen_width * (y+h)) & ~1;\r
143 p = (int *) ((short *)g_screen_ptr + offs);\r
144 for (i = len; i; i--, p++)\r
145 *p = (*p >> 2) & 0x39e7;\r
146 }\r
147 emu_text_out16(x, y, text);\r
148}\r
149\r
150static void osd_text8_rot(int x, int y, const char *text)\r
151{\r
152 int len = strlen(text) * 8;\r
153 char *p = (char *)g_screen_ptr + 240*(320-x) + y;\r
154\r
155 while (len--) {\r
156 memset(p, 0xe0, 8);\r
157 p -= 240;\r
cc68a136 158 }\r
cc41eb4f 159\r
160 emu_text_out8_rot(x, y, text);\r
161}\r
162\r
163static void osd_text16_rot(int x, int y, const char *text)\r
164{\r
165 int len = strlen(text) * 8;\r
166 short *p = (short *)g_screen_ptr + 240*(320-x) + y;\r
167\r
168 while (len--) {\r
169 memset(p, 0, 8*2);\r
170 p -= 240;\r
171 }\r
172\r
173 emu_text_out16_rot(x, y, text);\r
cc68a136 174}\r
175\r
213c16ad 176static void draw_cd_leds(void)\r
bf098bc5 177{\r
cc41eb4f 178 int led_reg, pitch, scr_offs, led_offs;\r
179 led_reg = Pico_mcd->s68k_regs[0];\r
180\r
181 if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
182 pitch = 240;\r
183 led_offs = -pitch * 6;\r
184 scr_offs = pitch * (320 - 4);\r
185 } else {\r
186 pitch = 320;\r
187 led_offs = 4;\r
188 scr_offs = pitch * 2 + 4;\r
189 }\r
bf098bc5 190\r
5a681086 191 if (!is_16bit_mode()) {\r
cc41eb4f 192 #define p(x) px[(x) >> 2]\r
bf098bc5 193 // 8-bit modes\r
cc41eb4f 194 unsigned int *px = (unsigned int *)((char *)g_screen_ptr + scr_offs);\r
195 unsigned int col_g = (led_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;\r
196 unsigned int col_r = (led_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;\r
197 p(pitch*0) = p(pitch*1) = p(pitch*2) = col_g;\r
198 p(pitch*0 + led_offs) = p(pitch*1 + led_offs) = p(pitch*2 + led_offs) = col_r;\r
199 #undef p\r
bf098bc5 200 } else {\r
cc41eb4f 201 #define p(x) px[(x)*2 >> 2] = px[((x)*2 >> 2) + 1]\r
bf098bc5 202 // 16-bit modes\r
cc41eb4f 203 unsigned int *px = (unsigned int *)((short *)g_screen_ptr + scr_offs);\r
204 unsigned int col_g = (led_reg & 2) ? 0x06000600 : 0;\r
205 unsigned int col_r = (led_reg & 1) ? 0xc000c000 : 0;\r
206 p(pitch*0) = p(pitch*1) = p(pitch*2) = col_g;\r
207 p(pitch*0 + led_offs) = p(pitch*1 + led_offs) = p(pitch*2 + led_offs) = col_r;\r
208 #undef p\r
bf098bc5 209 }\r
210}\r
211\r
213c16ad 212static void draw_pico_ptr(void)\r
213{\r
e2de9939 214 unsigned short *p = (unsigned short *)g_screen_ptr;\r
cc41eb4f 215 int x, y, pitch = 320;\r
213c16ad 216\r
217 // only if pen enabled and for 16bit modes\r
5a681086 218 if (pico_inp_mode == 0 || currentConfig.EmuOpt != RT_16BIT)\r
cc41eb4f 219 return;\r
213c16ad 220\r
cc41eb4f 221 x = pico_pen_x + PICO_PEN_ADJUST_X;\r
222 y = pico_pen_y + PICO_PEN_ADJUST_Y;\r
223 if (!(Pico.video.reg[12]&1) && !(PicoOpt & POPT_DIS_32C_BORDER))\r
224 x += 32;\r
213c16ad 225\r
02da059d 226 if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
cc41eb4f 227 pitch = 240;\r
228 p += (319 - x) * pitch + y;\r
229 } else\r
230 p += x + y * pitch;\r
231\r
232 p[0] ^= 0xffff;\r
233 p[pitch-1] ^= 0xffff;\r
234 p[pitch] ^= 0xffff;\r
235 p[pitch+1] ^= 0xffff;\r
236 p[pitch*2] ^= 0xffff;\r
213c16ad 237}\r
238\r
cc41eb4f 239/* rot thing for Wiz */\r
240static unsigned char __attribute__((aligned(4))) rot_buff[320*4*2];\r
241\r
242static int EmuScanBegin16_rot(unsigned int num)\r
243{\r
244 DrawLineDest = rot_buff + (num & 3) * 320 * 2;\r
245 return 0;\r
246}\r
247\r
248static int EmuScanEnd16_rot(unsigned int num)\r
249{\r
250 if ((num & 3) != 3)\r
251 return 0;\r
cc41eb4f 252 rotated_blit16(g_screen_ptr, rot_buff, num + 1,\r
253 !(Pico.video.reg[12] & 1) && !(PicoOpt & POPT_EN_SOFTSCALE));\r
254 return 0;\r
255}\r
256\r
257static int EmuScanBegin8_rot(unsigned int num)\r
258{\r
259 DrawLineDest = rot_buff + (num & 3) * 320;\r
260 return 0;\r
261}\r
262\r
263static int EmuScanEnd8_rot(unsigned int num)\r
264{\r
265 if ((num & 3) != 3)\r
266 return 0;\r
cc41eb4f 267 rotated_blit8(g_screen_ptr, rot_buff, num + 1,\r
268 !(Pico.video.reg[12] & 1));\r
269 return 0;\r
270}\r
271\r
f4750ee0 272/* line doublers */\r
273static unsigned int ld_counter;\r
274static int ld_left, ld_lines;\r
275\r
276static int EmuScanBegin16_ld(unsigned int num)\r
277{\r
278 if ((signed int)(ld_counter - num) > 100)\r
279 ld_counter = 0;\r
280\r
281 if (emu_scan_begin)\r
282 return emu_scan_begin(ld_counter);\r
283 else\r
284 DrawLineDest = (char *)g_screen_ptr + 320 * ld_counter * gp2x_current_bpp / 8;\r
285\r
286 return 0;\r
287}\r
288\r
289static int EmuScanEnd16_ld(unsigned int num)\r
290{\r
291 void *oldline = DrawLineDest;\r
292\r
293 if (emu_scan_end)\r
294 emu_scan_end(ld_counter);\r
295\r
296 ld_counter++;\r
297 ld_left--;\r
298 if (ld_left <= 0) {\r
299 ld_left = ld_lines;\r
300\r
301 EmuScanBegin16_ld(num);\r
302 memcpy32(DrawLineDest, oldline, 320 * gp2x_current_bpp / 8 / 4);\r
303 if (emu_scan_end)\r
304 emu_scan_end(ld_counter);\r
305\r
306 ld_counter++;\r
307 }\r
308\r
309 return 0;\r
310}\r
311\r
19954be1 312static int localPal[0x100];\r
313static void (*vidcpyM2)(void *dest, void *src, int m32col, int with_32c_border);\r
314static int (*make_local_pal)(int fast_mode);\r
315\r
316static int make_local_pal_md(int fast_mode)\r
317{\r
318 int pallen = 0xc0;\r
319\r
320 bgr444_to_rgb32(localPal, Pico.cram);\r
321 if (fast_mode)\r
322 return 0x40;\r
323\r
324 if (Pico.video.reg[0xC] & 8) { // shadow/hilight mode\r
325 bgr444_to_rgb32_sh(localPal, Pico.cram);\r
326 localPal[0xc0] = 0x0000c000;\r
327 localPal[0xd0] = 0x00c00000;\r
328 localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
329 localPal[0xf0] = 0x00ffffff;\r
330 pallen = 0x100;\r
331 }\r
332 else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes\r
333 bgr444_to_rgb32(localPal+0x40, HighPal);\r
334 bgr444_to_rgb32(localPal+0x80, HighPal+0x40);\r
335 }\r
336 else\r
337 memcpy32(localPal+0x80, localPal, 0x40); // for spr prio mess\r
338\r
339 return pallen;\r
340}\r
341\r
342static int make_local_pal_sms(int fast_mode)\r
343{\r
344 unsigned short *spal = Pico.cram;\r
345 unsigned int *dpal = (void *)localPal;\r
346 unsigned int i, t;\r
347\r
348 for (i = 0x40; i > 0; i--) {\r
349 t = *spal++;\r
350 t = ((t & 0x0003) << 22) | ((t & 0x000c) << 12) | ((t & 0x0030) << 2);\r
351 t |= t >> 2;\r
352 t |= t >> 4;\r
353 *dpal++ = t;\r
354 }\r
355\r
356 return 0x40;\r
357}\r
cc68a136 358\r
d08e7326 359void pemu_finalize_frame(const char *fps, const char *notice)\r
cc68a136 360{\r
bf098bc5 361 int emu_opt = currentConfig.EmuOpt;\r
19954be1 362 int ret;\r
bf098bc5 363\r
5a681086 364 if (PicoAHW & PAHW_32X)\r
365 ; // nothing to do\r
366 else if (get_renderer() == RT_8BIT_FAST)\r
fad24893 367 {\r
cc68a136 368 // 8bit fast renderer\r
369 if (Pico.m.dirtyPal) {\r
370 Pico.m.dirtyPal = 0;\r
19954be1 371 ret = make_local_pal(1);\r
cc68a136 372 // feed new palette to our device\r
19954be1 373 gp2x_video_setpalette(localPal, ret);\r
cc68a136 374 }\r
fad24893 375 // a hack for VR\r
45f2f245 376 if (PicoAHW & PAHW_SVP)\r
fad24893 377 memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);\r
378 // do actual copy\r
cc41eb4f 379 vidcpyM2(g_screen_ptr, PicoDraw2FB+328*8,\r
380 !(Pico.video.reg[12] & 1), !(PicoOpt & POPT_DIS_32C_BORDER));\r
fad24893 381 }\r
5a681086 382 else if (get_renderer() == RT_8BIT_ACC)\r
fad24893 383 {\r
cc68a136 384 // 8bit accurate renderer\r
602133e1 385 if (Pico.m.dirtyPal)\r
386 {\r
cc68a136 387 Pico.m.dirtyPal = 0;\r
19954be1 388 ret = make_local_pal(0);\r
389 gp2x_video_setpalette(localPal, ret);\r
cc68a136 390 }\r
391 }\r
392\r
f4750ee0 393 if (notice)\r
394 osd_text(4, osd_y, notice);\r
395 if (emu_opt & EOPT_SHOW_FPS)\r
396 osd_text(osd_fps_x, osd_y, fps);\r
697746df 397 if ((PicoAHW & PAHW_MCD) && (emu_opt & EOPT_EN_CD_LEDS))\r
213c16ad 398 draw_cd_leds();\r
399 if (PicoAHW & PAHW_PICO)\r
400 draw_pico_ptr();\r
d08e7326 401}\r
cc68a136 402\r
d08e7326 403void plat_video_flip(void)\r
404{\r
5a681086 405 int stride = g_screen_width;\r
cc68a136 406 gp2x_video_flip();\r
5a681086 407\r
408 if (is_16bit_mode())\r
409 stride *= 2;\r
410 PicoDrawSetOutBuf(g_screen_ptr, stride);\r
cc68a136 411}\r
412\r
b24e0f6c 413/* XXX */\r
414#ifdef __GP2X__\r
415unsigned int plat_get_ticks_ms(void)\r
416{\r
417 return gp2x_get_ticks_ms();\r
418}\r
419\r
420unsigned int plat_get_ticks_us(void)\r
421{\r
422 return gp2x_get_ticks_us();\r
423}\r
424#endif\r
425\r
426void plat_wait_till_us(unsigned int us_to)\r
427{\r
428 unsigned int now;\r
429\r
430 spend_cycles(1024);\r
431 now = plat_get_ticks_us();\r
432\r
433 while ((signed int)(us_to - now) > 512)\r
434 {\r
435 spend_cycles(1024);\r
436 now = plat_get_ticks_us();\r
437 }\r
438}\r
439\r
440void plat_video_wait_vsync(void)\r
441{\r
442 gp2x_video_wait_vsync();\r
443}\r
444\r
445void plat_status_msg_clear(void)\r
cc68a136 446{\r
5a681086 447 int is_8bit = !is_16bit_mode();\r
b24e0f6c 448 if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
cc41eb4f 449 /* ugh.. */\r
450 int i, u, *p;\r
451 if (is_8bit) {\r
452 for (i = 0; i < 4; i++) {\r
453 p = (int *)gp2x_screens[i] + (240-8) / 4;\r
454 for (u = 320; u > 0; u--, p += 240/4)\r
455 p[0] = p[1] = 0xe0e0e0e0;\r
456 }\r
457 } else {\r
458 for (i = 0; i < 4; i++) {\r
459 p = (int *)gp2x_screens[i] + (240-8)*2 / 4;\r
460 for (u = 320; u > 0; u--, p += 240*2/4)\r
461 p[0] = p[1] = p[2] = p[3] = 0;\r
462 }\r
463 }\r
464 return;\r
465 }\r
466\r
b24e0f6c 467 if (is_8bit)\r
468 gp2x_memset_all_buffers(320*232, 0xe0, 320*8);\r
469 else\r
470 gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);\r
cc68a136 471}\r
472\r
d34a42f9 473void plat_status_msg_busy_next(const char *msg)\r
474{\r
b24e0f6c 475 plat_status_msg_clear();\r
d08e7326 476 pemu_finalize_frame("", msg);\r
477 plat_video_flip();\r
7c34867a 478 emu_status_msg("");\r
d34a42f9 479\r
480 /* assumption: msg_busy_next gets called only when\r
481 * something slow is about to happen */\r
482 reset_timing = 1;\r
483}\r
484\r
485void plat_status_msg_busy_first(const char *msg)\r
486{\r
487 gp2x_memcpy_all_buffers(g_screen_ptr, 0, 320*240*2);\r
488 plat_status_msg_busy_next(msg);\r
489}\r
cc68a136 490\r
f4750ee0 491static void vid_reset_mode(void)\r
cc68a136 492{\r
5a681086 493 int gp2x_mode = 16;\r
494 int renderer = get_renderer();\r
495\r
f4750ee0 496 PicoOpt &= ~POPT_ALT_RENDERER;\r
497 emu_scan_begin = NULL;\r
498 emu_scan_end = NULL;\r
cc41eb4f 499\r
5a681086 500 switch (renderer) {\r
501 case RT_16BIT:\r
5a681086 502 PicoDrawSetOutFormat(PDF_RGB555, 0);\r
503 PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r
5a681086 504 break;\r
505 case RT_8BIT_ACC:\r
5a681086 506 PicoDrawSetOutFormat(PDF_8BIT, 0);\r
507 PicoDrawSetOutBuf(g_screen_ptr, g_screen_width);\r
5a681086 508 gp2x_mode = 8;\r
509 break;\r
510 case RT_8BIT_FAST:\r
f4750ee0 511 PicoOpt |= POPT_ALT_RENDERER;\r
5a681086 512 PicoDrawSetOutFormat(PDF_NONE, 0);\r
f4750ee0 513 vidcpyM2 = vidcpy_m2;\r
5a681086 514 gp2x_mode = 8;\r
515 break;\r
f4750ee0 516 default:\r
517 printf("bad renderer\n");\r
518 break;\r
bf098bc5 519 }\r
cc41eb4f 520\r
5a681086 521 if (PicoAHW & PAHW_32X) {\r
5a681086 522 // Wiz 16bit is an exception, uses line rendering due to rotation mess\r
523 if (renderer == RT_16BIT && (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX)) {\r
524 PicoDrawSetOutFormat(PDF_RGB555, 1);\r
525 PicoDraw32xSetFrameMode(0, 0);\r
526 }\r
527 else {\r
f4750ee0 528 PicoDrawSetOutFormat(PDF_NONE, 0);\r
45285368 529 PicoDraw32xSetFrameMode(1, 0);\r
5a681086 530 }\r
531 PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r
45285368 532 gp2x_mode = 16;\r
5a681086 533 }\r
534\r
f4750ee0 535 if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
536 if ((PicoAHW & PAHW_32X) || renderer == RT_16BIT) {\r
537 emu_scan_begin = EmuScanBegin16_rot;\r
538 emu_scan_end = EmuScanEnd16_rot;\r
539 }\r
540 else if (renderer == RT_8BIT_ACC) {\r
541 emu_scan_begin = EmuScanBegin8_rot;\r
542 emu_scan_end = EmuScanEnd8_rot;\r
543 }\r
544 else if (renderer == RT_8BIT_FAST)\r
545 vidcpyM2 = vidcpy_m2_rot;\r
546 }\r
547\r
548 PicoDrawSetCallbacks(emu_scan_begin, emu_scan_end);\r
549\r
550 if (is_16bit_mode())\r
551 osd_text = (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) ? osd_text16_rot : osd_text16;\r
552 else\r
553 osd_text = (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) ? osd_text8_rot : osd_text8;\r
554\r
f4750ee0 555 gp2x_video_wait_vsync();\r
5a681086 556 if (!is_16bit_mode()) {\r
bf098bc5 557 // setup pal for 8-bit modes\r
558 localPal[0xc0] = 0x0000c000; // MCD LEDs\r
559 localPal[0xd0] = 0x00c00000;\r
cc68a136 560 localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
561 localPal[0xf0] = 0x00ffffff;\r
cc68a136 562 gp2x_video_setpalette(localPal, 0x100);\r
563 gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
cc68a136 564 }\r
f4750ee0 565 else\r
566 gp2x_memset_all_buffers(0, 0, 320*240*2);\r
567\r
45285368 568 if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX)\r
569 gp2x_mode = -gp2x_mode;\r
570\r
571 gp2x_video_changemode(gp2x_mode);\r
572\r
cc68a136 573 Pico.m.dirtyPal = 1;\r
cc41eb4f 574\r
f4750ee0 575 PicoOpt &= ~POPT_EN_SOFTSCALE;\r
576 if (currentConfig.scaling == EOPT_SCALE_SW)\r
577 PicoOpt |= POPT_EN_SOFTSCALE;\r
19954be1 578\r
579 // palette converters for 8bit modes\r
580 make_local_pal = (PicoAHW & PAHW_SMS) ? make_local_pal_sms : make_local_pal_md;\r
cc68a136 581}\r
582\r
f4750ee0 583void emu_video_mode_change(int start_line, int line_count, int is_32cols)\r
584{\r
585 int scalex = 320, scaley = 240;\r
586 int ln_offs = 0;\r
587\r
588 if (doing_bg_frame)\r
589 return;\r
590\r
591 osd_fps_x = OSD_FPS_X;\r
592 osd_y = 232;\r
593\r
594 /* set up hwscaling here */\r
595 PicoOpt &= ~POPT_DIS_32C_BORDER;\r
596 if (is_32cols && currentConfig.scaling == EOPT_SCALE_HW) {\r
597 scalex = 256;\r
598 PicoOpt |= POPT_DIS_32C_BORDER;\r
599 osd_fps_x = OSD_FPS_X - 64;\r
600 }\r
601\r
602 if (currentConfig.vscaling == EOPT_SCALE_HW) {\r
603 ln_offs = start_line;\r
604 scaley = line_count;\r
605 osd_y = start_line + line_count - 8;\r
606 }\r
607\r
608 gp2x_video_RGB_setscaling(ln_offs, scalex, scaley);\r
609\r
610 /* line doubling */\r
611 if (currentConfig.vscaling == EOPT_SCALE_SW && line_count < 240) {\r
612 ld_lines = ld_left = line_count / (240 - line_count);\r
613 PicoDrawSetCallbacks(EmuScanBegin16_ld, EmuScanEnd16_ld);\r
614 }\r
615\r
616 // clear whole screen in all buffers\r
617 if (!is_16bit_mode())\r
618 gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
619 else\r
620 gp2x_memset_all_buffers(0, 0, 320*240*2);\r
621}\r
622\r
5a681086 623void plat_video_toggle_renderer(int change, int is_menu_call)\r
66fdc0f0 624{\r
5a681086 625 change_renderer(change);\r
d90f5bd7 626\r
5a681086 627 if (is_menu_call)\r
d90f5bd7 628 return;\r
7a1f6e45 629\r
f4750ee0 630 vid_reset_mode();\r
19954be1 631 rendstatus_old = -1;\r
66fdc0f0 632\r
5a681086 633 if (PicoAHW & PAHW_32X)\r
634 emu_status_msg(renderer_names32x[get_renderer()]);\r
635 else\r
636 emu_status_msg(renderer_names[get_renderer()]);\r
fa1e5e29 637}\r
638\r
f2cf8472 639#if 0 // TODO\r
991473ad 640static void RunEventsPico(unsigned int events)\r
406c96c5 641{\r
f0f0d2df 642 int ret, px, py, lim_x;\r
85a4b5a4 643 static int pdown_frames = 0;\r
644\r
85a4b5a4 645 // for F200\r
646 ret = gp2x_touchpad_read(&px, &py);\r
f0f0d2df 647 if (ret >= 0)\r
648 {\r
649 if (ret > 35000)\r
650 {\r
85a4b5a4 651 if (pdown_frames++ > 5)\r
652 PicoPad[0] |= 0x20;\r
653\r
654 pico_pen_x = px;\r
655 pico_pen_y = py;\r
656 if (!(Pico.video.reg[12]&1)) {\r
657 pico_pen_x -= 32;\r
658 if (pico_pen_x < 0) pico_pen_x = 0;\r
659 if (pico_pen_x > 248) pico_pen_x = 248;\r
660 }\r
661 if (pico_pen_y > 224) pico_pen_y = 224;\r
662 }\r
663 else\r
f0f0d2df 664 pdown_frames = 0;\r
85a4b5a4 665\r
666 //if (ret == 0)\r
667 // PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;\r
668 }\r
406c96c5 669}\r
f2cf8472 670#endif\r
406c96c5 671\r
d34a42f9 672void plat_update_volume(int has_changed, int is_up)\r
4a32f01f 673{\r
674 static int prev_frame = 0, wait_frames = 0;\r
675 int vol = currentConfig.volume;\r
d34a42f9 676 int need_low_volume = 0;\r
677 gp2x_soc_t soc;\r
678\r
679 soc = soc_detect();\r
680 if ((PicoOpt & POPT_EN_STEREO) && soc == SOCID_MMSP2)\r
681 need_low_volume = 1;\r
4a32f01f 682\r
683 if (has_changed)\r
684 {\r
d34a42f9 685 if (need_low_volume && vol < 5 && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)\r
4a32f01f 686 wait_frames++;\r
687 else {\r
688 if (is_up) {\r
689 if (vol < 99) vol++;\r
690 } else {\r
691 if (vol > 0) vol--;\r
692 }\r
693 wait_frames = 0;\r
e5ab6faf 694 sndout_oss_setvol(vol, vol);\r
4a32f01f 695 currentConfig.volume = vol;\r
696 }\r
b24e0f6c 697 emu_status_msg("VOL: %02i", vol);\r
4a32f01f 698 prev_frame = Pico.m.frame_count;\r
699 }\r
700\r
54646a39 701 if (!need_low_volume)\r
d34a42f9 702 return;\r
703\r
704 /* set the right mixer func */\r
4a32f01f 705 if (vol >= 5)\r
706 PsndMix_32_to_16l = mix_32_to_16l_stereo;\r
707 else {\r
708 mix_32_to_16l_level = 5 - vol;\r
709 PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;\r
710 }\r
711}\r
712\r
a4edca53 713static void oss_write_nonblocking(int len)\r
cc68a136 714{\r
a4edca53 715 // sndout_oss_can_write() is not reliable, only use with no_frmlimit\r
b08ccf71 716 if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && !sndout_oss_can_write(len))\r
717 return;\r
718\r
a4edca53 719 sndout_oss_write_nb(PsndOut, len);\r
cc68a136 720}\r
721\r
f2cf8472 722void pemu_sound_start(void)\r
7b3f44c6 723{\r
724 static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;\r
7b3f44c6 725\r
726 PsndOut = NULL;\r
727\r
728 // prepare sound stuff\r
7c34867a 729 if (currentConfig.EmuOpt & EOPT_EN_SOUND)\r
7b3f44c6 730 {\r
7c34867a 731 int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0;\r
02da059d 732 int snd_rate_oss = PsndRate;\r
7c34867a 733 gp2x_soc_t soc;\r
734\r
9f6a4e90 735 memset(sndBuffer, 0, sizeof(sndBuffer));\r
736 PsndOut = sndBuffer;\r
a4edca53 737 PicoWriteSound = oss_write_nonblocking;\r
9f6a4e90 738 plat_update_volume(0, 0);\r
739\r
740 printf("starting audio: %i len: %i stereo: %i, pal: %i\n",\r
741 PsndRate, PsndLen, is_stereo, Pico.m.pal);\r
742 sndout_oss_start(snd_rate_oss, is_stereo, 1);\r
743 sndout_oss_setvol(currentConfig.volume, currentConfig.volume);\r
744\r
b08ccf71 745 soc = soc_detect();\r
9f6a4e90 746 if (soc == SOCID_POLLUX)\r
747 PsndRate = pollux_get_real_snd_rate(PsndRate);\r
b08ccf71 748\r
7c34867a 749 #define SOUND_RERATE_FLAGS (POPT_EN_FM|POPT_EN_PSG|POPT_EN_STEREO|POPT_EXT_FM|POPT_EN_MCD_CDDA)\r
750 if (PsndRate != PsndRate_old || Pico.m.pal != pal_old || ((PicoOpt & POPT_EXT_FM) && crashed_940) ||\r
751 ((PicoOpt ^ PicoOpt_old) & SOUND_RERATE_FLAGS)) {\r
7b3f44c6 752 PsndRerate(Pico.m.frame_count ? 1 : 0);\r
753 }\r
b08ccf71 754\r
7b3f44c6 755 PsndRate_old = PsndRate;\r
756 PicoOpt_old = PicoOpt;\r
757 pal_old = Pico.m.pal;\r
758 }\r
759}\r
760\r
9f6a4e90 761static const int sound_rates[] = { 44100, 32000, 22050, 16000, 11025, 8000 };\r
762\r
f2cf8472 763void pemu_sound_stop(void)\r
7b3f44c6 764{\r
9f6a4e90 765 int i;\r
766\r
767 /* get back from Pollux pain */\r
768 PsndRate += 1000;\r
769 for (i = 0; i < ARRAY_SIZE(sound_rates); i++) {\r
770 if (PsndRate >= sound_rates[i]) {\r
771 PsndRate = sound_rates[i];\r
772 break;\r
773 }\r
02da059d 774 }\r
7b3f44c6 775}\r
776\r
f2cf8472 777void pemu_sound_wait(void)\r
7b3f44c6 778{\r
779 // don't need to do anything, writes will block by themselves\r
780}\r
781\r
45285368 782void pemu_forced_frame(int no_scale, int do_emu)\r
860c6322 783{\r
a4edca53 784 doing_bg_frame = 1;\r
5a681086 785 PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r
786 PicoDraw32xSetFrameMode(0, 0);\r
f4750ee0 787 PicoDrawSetCallbacks(NULL, NULL);\r
a12e0116 788 Pico.m.dirtyPal = 1;\r
860c6322 789\r
a4edca53 790 emu_cmn_forced_frame(no_scale, do_emu);\r
f4750ee0 791\r
45285368 792 g_menubg_src_ptr = g_screen_ptr;\r
a4edca53 793 doing_bg_frame = 0;\r
860c6322 794}\r
795\r
f2cf8472 796void plat_debug_cat(char *str)\r
fcf94fcc 797{\r
fcf94fcc 798}\r
799\r
da310283 800#if 0\r
801static void tga_dump(void)\r
802{\r
803#define BYTE unsigned char\r
804#define WORD unsigned short\r
805 struct\r
806 {\r
807 BYTE IDLength; /* 00h Size of Image ID field */\r
808 BYTE ColorMapType; /* 01h Color map type */\r
809 BYTE ImageType; /* 02h Image type code */\r
810 WORD CMapStart; /* 03h Color map origin */\r
811 WORD CMapLength; /* 05h Color map length */\r
812 BYTE CMapDepth; /* 07h Depth of color map entries */\r
813 WORD XOffset; /* 08h X origin of image */\r
814 WORD YOffset; /* 0Ah Y origin of image */\r
815 WORD Width; /* 0Ch Width of image */\r
816 WORD Height; /* 0Eh Height of image */\r
817 BYTE PixelDepth; /* 10h Image pixel size */\r
818 BYTE ImageDescriptor; /* 11h Image descriptor byte */\r
819 } __attribute__((packed)) TGAHEAD;\r
820 static unsigned short oldscr[320*240];\r
821 FILE *f; char name[128]; int i;\r
822\r
823 memset(&TGAHEAD, 0, sizeof(TGAHEAD));\r
824 TGAHEAD.ImageType = 2;\r
825 TGAHEAD.Width = 320;\r
826 TGAHEAD.Height = 240;\r
827 TGAHEAD.PixelDepth = 16;\r
828 TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left\r
829\r
830#define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?\r
831\r
832 for (i = 0; i < 320*240; i++)\r
e2de9939 833 if(oldscr[i] != CONV(((unsigned short *)g_screen_ptr)[i])) break;\r
da310283 834 if (i < 320*240)\r
835 {\r
836 for (i = 0; i < 320*240; i++)\r
e2de9939 837 oldscr[i] = CONV(((unsigned short *)g_screen_ptr)[i]);\r
da310283 838 sprintf(name, "%05i.tga", Pico.m.frame_count);\r
839 f = fopen(name, "wb");\r
840 if (!f) { printf("!f\n"); exit(1); }\r
841 fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);\r
842 fwrite(oldscr, 1, 320*240*2, f);\r
843 fclose(f);\r
844 }\r
845}\r
846#endif\r
847\r
b24e0f6c 848void pemu_loop_prep(void)\r
cc68a136 849{\r
cc41eb4f 850 static int gp2x_old_clock = -1, EmuOpt_old = 0, pal_old = 0;\r
851 static int gp2x_old_gamma = 100;\r
7b436906 852 gp2x_soc_t soc;\r
853\r
854 soc = soc_detect();\r
cc68a136 855\r
0d9bf4fc 856 if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_RAM_TIMINGS) {\r
857 if (currentConfig.EmuOpt & EOPT_RAM_TIMINGS)\r
ee2a3bdf 858 set_ram_timings();\r
0d9bf4fc 859 else\r
860 unset_ram_timings();\r
861 }\r
862\r
863 if (gp2x_old_clock < 0)\r
864 gp2x_old_clock = default_cpu_clock;\r
f4750ee0 865 if (gp2x_old_clock != currentConfig.CPUclock && gp2x_set_cpuclk != NULL) {\r
cc68a136 866 printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);\r
0d9bf4fc 867 gp2x_set_cpuclk(currentConfig.CPUclock);\r
cc68a136 868 gp2x_old_clock = currentConfig.CPUclock;\r
869 printf(" done\n");\r
870 }\r
871\r
7b436906 872 if (gp2x_old_gamma != currentConfig.gamma || ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_A_SN_GAMMA)) {\r
873 set_lcd_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt & EOPT_A_SN_GAMMA));\r
cc68a136 874 gp2x_old_gamma = currentConfig.gamma;\r
82bc9cdd 875 printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
cc68a136 876 }\r
877\r
7b436906 878 if (((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_VSYNC) || Pico.m.pal != pal_old) {\r
879 if ((currentConfig.EmuOpt & EOPT_VSYNC) || soc == SOCID_POLLUX)\r
0d9bf4fc 880 set_lcd_custom_rate(Pico.m.pal);\r
7b436906 881 else if (EmuOpt_old & EOPT_VSYNC)\r
0d9bf4fc 882 unset_lcd_custom_rate();\r
aae35e84 883 }\r
884\r
45285368 885 if (gp2x_dev_id == GP2X_DEV_CAANOO)\r
886 in_set_config_int(in_name_to_id("evdev:pollux-analog"), IN_CFG_ABS_DEAD_ZONE,\r
887 currentConfig.analog_deadzone);\r
888\r
0d9bf4fc 889 if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_MMUHACK)\r
890 gp2x_make_fb_bufferable(currentConfig.EmuOpt & EOPT_MMUHACK);\r
891\r
aae35e84 892 EmuOpt_old = currentConfig.EmuOpt;\r
cc41eb4f 893 pal_old = Pico.m.pal;\r
cc68a136 894\r
895 // make sure we are in correct mode\r
f4750ee0 896 change_renderer(0);\r
897 vid_reset_mode();\r
cc68a136 898\r
c7eb229a 899 // dirty buffers better go now than during gameplay\r
900 sync();\r
901 sleep(0);\r
902\r
f2cf8472 903 pemu_sound_start();\r
b24e0f6c 904}\r
cc68a136 905\r
b24e0f6c 906void pemu_loop_end(void)\r
907{\r
02da059d 908 pemu_sound_stop();\r
909\r
730259b7 910 /* do one more frame for menu bg */\r
45285368 911 pemu_forced_frame(0, 1);\r
cc68a136 912}\r
913\r
713c9224 914const char *plat_get_credits(void)\r
915{\r
fcdefcf6 916 return "PicoDrive v" VERSION " (c) notaz, 2006-2010\n\n\n"\r
713c9224 917 "Credits:\n"\r
918 "fDave: Cyclone 68000 core,\n"\r
919 " base code of PicoDrive\n"\r
920 "Reesy & FluBBa: DrZ80 core\n"\r
921 "MAME devs: YM2612 and SN76496 cores\n"\r
922 "rlyeh and others: minimal SDK\n"\r
42171343 923 "Squidge: mmuhack\n"\r
713c9224 924 "Dzz: ARM940 sample\n"\r
925 "GnoStiC / Puck2099: USB joy code\n"\r
926 "craigix: GP2X hardware\n"\r
927 "ketchupgun: skin design\n"\r
928 "\n"\r
929 "special thanks (for docs, ideas):\n"\r
930 " Charles MacDonald, Haze,\n"\r
931 " Stephane Dallongeville,\n"\r
932 " Lordus, Exophase, Rokas,\n"\r
933 " Nemesis, Tasco Deluxe";\r
934}\r