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