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