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