| 1 | // (c) Copyright 2006-2009 notaz, All rights reserved.\r |
| 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 |
| 8 | \r |
| 9 | #include "plat_gp2x.h"\r |
| 10 | #include "soc.h"\r |
| 11 | #include "../common/plat.h"\r |
| 12 | #include "../common/menu.h"\r |
| 13 | #include "../common/arm_utils.h"\r |
| 14 | #include "../common/fonts.h"\r |
| 15 | #include "../common/emu.h"\r |
| 16 | #include "../common/config.h"\r |
| 17 | #include "../linux/sndout_oss.h"\r |
| 18 | #include "version.h"\r |
| 19 | \r |
| 20 | #include <pico/pico_int.h>\r |
| 21 | #include <pico/patch.h>\r |
| 22 | #include <pico/sound/mix.h>\r |
| 23 | #include <zlib/zlib.h>\r |
| 24 | \r |
| 25 | //#define PFRAMES\r |
| 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 |
| 33 | \r |
| 34 | extern int crashed_940;\r |
| 35 | \r |
| 36 | static short __attribute__((aligned(4))) sndBuffer[2*(44100+100)/50];\r |
| 37 | static unsigned char PicoDraw2FB_[(8+320) * (8+240+8)];\r |
| 38 | unsigned char *PicoDraw2FB = PicoDraw2FB_;\r |
| 39 | static int osd_fps_x, osd_y;\r |
| 40 | \r |
| 41 | extern void *gp2x_screens[4];\r |
| 42 | \r |
| 43 | int plat_get_root_dir(char *dst, int len)\r |
| 44 | {\r |
| 45 | extern char **g_argv;\r |
| 46 | int j;\r |
| 47 | \r |
| 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 |
| 54 | \r |
| 55 | return j + 1;\r |
| 56 | }\r |
| 57 | \r |
| 58 | \r |
| 59 | static void scaling_update(void)\r |
| 60 | {\r |
| 61 | PicoOpt &= ~(POPT_DIS_32C_BORDER|POPT_EN_SOFTSCALE);\r |
| 62 | switch (currentConfig.scaling) {\r |
| 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 |
| 71 | }\r |
| 72 | }\r |
| 73 | \r |
| 74 | \r |
| 75 | void pemu_prep_defconfig(void)\r |
| 76 | {\r |
| 77 | gp2x_soc_t soc;\r |
| 78 | \r |
| 79 | memset(&defaultConfig, 0, sizeof(defaultConfig));\r |
| 80 | defaultConfig.EmuOpt = 0x9d | EOPT_RAM_TIMINGS|EOPT_CONFIRM_SAVE|EOPT_EN_CD_LEDS;\r |
| 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 |
| 84 | defaultConfig.s_PsndRate = 44100;\r |
| 85 | defaultConfig.s_PicoRegion = 0; // auto\r |
| 86 | defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP\r |
| 87 | defaultConfig.s_PicoCDBuffers = 0;\r |
| 88 | defaultConfig.Frameskip = -1; // auto\r |
| 89 | defaultConfig.CPUclock = default_cpu_clock;\r |
| 90 | defaultConfig.volume = 50;\r |
| 91 | defaultConfig.gamma = 100;\r |
| 92 | defaultConfig.scaling = 0;\r |
| 93 | defaultConfig.turbo_rate = 15;\r |
| 94 | \r |
| 95 | soc = soc_detect();\r |
| 96 | if (soc == SOCID_MMSP2)\r |
| 97 | defaultConfig.s_PicoOpt |= POPT_EXT_FM;\r |
| 98 | else if (soc == SOCID_POLLUX)\r |
| 99 | defaultConfig.EmuOpt |= EOPT_WIZ_TEAR_FIX|EOPT_SHOW_RTC;\r |
| 100 | }\r |
| 101 | \r |
| 102 | static void (*osd_text)(int x, int y, const char *text);\r |
| 103 | \r |
| 104 | static void osd_text8(int x, int y, const char *text)\r |
| 105 | {\r |
| 106 | int len = strlen(text)*8;\r |
| 107 | int *p, i, h, offs;\r |
| 108 | \r |
| 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 |
| 119 | static 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 |
| 134 | static 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 |
| 142 | }\r |
| 143 | \r |
| 144 | emu_text_out8_rot(x, y, text);\r |
| 145 | }\r |
| 146 | \r |
| 147 | static 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 |
| 158 | }\r |
| 159 | \r |
| 160 | static void draw_cd_leds(void)\r |
| 161 | {\r |
| 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 |
| 174 | \r |
| 175 | if ((PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP)) {\r |
| 176 | #define p(x) px[(x) >> 2]\r |
| 177 | // 8-bit modes\r |
| 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 |
| 184 | } else {\r |
| 185 | #define p(x) px[(x)*2 >> 2] = px[((x)*2 >> 2) + 1]\r |
| 186 | // 16-bit modes\r |
| 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 |
| 193 | }\r |
| 194 | }\r |
| 195 | \r |
| 196 | static void draw_pico_ptr(void)\r |
| 197 | {\r |
| 198 | unsigned short *p = (unsigned short *)g_screen_ptr;\r |
| 199 | int x, y, pitch = 320;\r |
| 200 | \r |
| 201 | // only if pen enabled and for 16bit modes\r |
| 202 | if (pico_inp_mode == 0 || (PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP))\r |
| 203 | return;\r |
| 204 | \r |
| 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 |
| 209 | \r |
| 210 | if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r |
| 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 |
| 221 | }\r |
| 222 | \r |
| 223 | static int EmuScanBegin16(unsigned int num)\r |
| 224 | {\r |
| 225 | DrawLineDest = (unsigned short *) g_screen_ptr + g_screen_width * num;\r |
| 226 | \r |
| 227 | return 0;\r |
| 228 | }\r |
| 229 | \r |
| 230 | static int EmuScanBegin8(unsigned int num)\r |
| 231 | {\r |
| 232 | DrawLineDest = (unsigned char *) g_screen_ptr + g_screen_width * num;\r |
| 233 | \r |
| 234 | return 0;\r |
| 235 | }\r |
| 236 | \r |
| 237 | /* rot thing for Wiz */\r |
| 238 | static unsigned char __attribute__((aligned(4))) rot_buff[320*4*2];\r |
| 239 | \r |
| 240 | static 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 |
| 246 | static int EmuScanEnd16_rot(unsigned int num)\r |
| 247 | {\r |
| 248 | if ((num & 3) != 3)\r |
| 249 | return 0;\r |
| 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 |
| 255 | static 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 |
| 261 | static int EmuScanEnd8_rot(unsigned int num)\r |
| 262 | {\r |
| 263 | if ((num & 3) != 3)\r |
| 264 | return 0;\r |
| 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 |
| 270 | static int localPal[0x100];\r |
| 271 | static void (*vidcpyM2)(void *dest, void *src, int m32col, int with_32c_border);\r |
| 272 | static int (*make_local_pal)(int fast_mode);\r |
| 273 | \r |
| 274 | static 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 |
| 300 | static 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 |
| 316 | \r |
| 317 | void pemu_update_display(const char *fps, const char *notice)\r |
| 318 | {\r |
| 319 | int emu_opt = currentConfig.EmuOpt;\r |
| 320 | int ret;\r |
| 321 | \r |
| 322 | if (PicoOpt & POPT_ALT_RENDERER)\r |
| 323 | {\r |
| 324 | // 8bit fast renderer\r |
| 325 | if (Pico.m.dirtyPal) {\r |
| 326 | Pico.m.dirtyPal = 0;\r |
| 327 | ret = make_local_pal(1);\r |
| 328 | // feed new palette to our device\r |
| 329 | gp2x_video_setpalette(localPal, ret);\r |
| 330 | }\r |
| 331 | // a hack for VR\r |
| 332 | if (PicoAHW & PAHW_SVP)\r |
| 333 | memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);\r |
| 334 | // do actual copy\r |
| 335 | vidcpyM2(g_screen_ptr, PicoDraw2FB+328*8,\r |
| 336 | !(Pico.video.reg[12] & 1), !(PicoOpt & POPT_DIS_32C_BORDER));\r |
| 337 | }\r |
| 338 | else if (!(emu_opt & EOPT_16BPP))\r |
| 339 | {\r |
| 340 | // 8bit accurate renderer\r |
| 341 | if (Pico.m.dirtyPal)\r |
| 342 | {\r |
| 343 | Pico.m.dirtyPal = 0;\r |
| 344 | ret = make_local_pal(0);\r |
| 345 | gp2x_video_setpalette(localPal, ret);\r |
| 346 | }\r |
| 347 | }\r |
| 348 | \r |
| 349 | if (notice || (emu_opt & 2)) {\r |
| 350 | if (notice)\r |
| 351 | osd_text(4, osd_y, notice);\r |
| 352 | if (emu_opt & 2)\r |
| 353 | osd_text(osd_fps_x, osd_y, fps);\r |
| 354 | }\r |
| 355 | if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))\r |
| 356 | draw_cd_leds();\r |
| 357 | if (PicoAHW & PAHW_PICO)\r |
| 358 | draw_pico_ptr();\r |
| 359 | \r |
| 360 | gp2x_video_flip();\r |
| 361 | }\r |
| 362 | \r |
| 363 | /* XXX */\r |
| 364 | #ifdef __GP2X__\r |
| 365 | unsigned int plat_get_ticks_ms(void)\r |
| 366 | {\r |
| 367 | return gp2x_get_ticks_ms();\r |
| 368 | }\r |
| 369 | \r |
| 370 | unsigned int plat_get_ticks_us(void)\r |
| 371 | {\r |
| 372 | return gp2x_get_ticks_us();\r |
| 373 | }\r |
| 374 | #endif\r |
| 375 | \r |
| 376 | void 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 |
| 390 | void plat_video_wait_vsync(void)\r |
| 391 | {\r |
| 392 | gp2x_video_wait_vsync();\r |
| 393 | }\r |
| 394 | \r |
| 395 | void plat_status_msg_clear(void)\r |
| 396 | {\r |
| 397 | int is_8bit = (PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP);\r |
| 398 | if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r |
| 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 |
| 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 |
| 421 | }\r |
| 422 | \r |
| 423 | void plat_status_msg_busy_next(const char *msg)\r |
| 424 | {\r |
| 425 | plat_status_msg_clear();\r |
| 426 | pemu_update_display("", msg);\r |
| 427 | emu_status_msg("");\r |
| 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 |
| 434 | void 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 |
| 439 | \r |
| 440 | static void vidResetMode(void)\r |
| 441 | {\r |
| 442 | PicoScanEnd = NULL;\r |
| 443 | \r |
| 444 | if (PicoOpt & POPT_ALT_RENDERER) {\r |
| 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 |
| 456 | PicoDrawSetColorFormat(1);\r |
| 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 |
| 469 | PicoDrawSetColorFormat(2);\r |
| 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 |
| 480 | }\r |
| 481 | \r |
| 482 | if ((PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP)) {\r |
| 483 | // setup pal for 8-bit modes\r |
| 484 | localPal[0xc0] = 0x0000c000; // MCD LEDs\r |
| 485 | localPal[0xd0] = 0x00c00000;\r |
| 486 | localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r |
| 487 | localPal[0xf0] = 0x00ffffff;\r |
| 488 | gp2x_video_setpalette(localPal, 0x100);\r |
| 489 | gp2x_memset_all_buffers(0, 0xe0, 320*240);\r |
| 490 | gp2x_video_flip();\r |
| 491 | }\r |
| 492 | Pico.m.dirtyPal = 1;\r |
| 493 | \r |
| 494 | // reset scaling\r |
| 495 | if (currentConfig.scaling == EOPT_SCALE_HW_HV && !(Pico.video.reg[1]&8))\r |
| 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 |
| 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 |
| 501 | }\r |
| 502 | \r |
| 503 | void plat_video_toggle_renderer(int is_next, int force_16bpp, int is_menu)\r |
| 504 | {\r |
| 505 | if (force_16bpp) {\r |
| 506 | PicoOpt &= ~POPT_ALT_RENDERER;\r |
| 507 | currentConfig.EmuOpt |= EOPT_16BPP;\r |
| 508 | }\r |
| 509 | /* alt, 16bpp, 8bpp */\r |
| 510 | else if (PicoOpt & POPT_ALT_RENDERER) {\r |
| 511 | PicoOpt &= ~POPT_ALT_RENDERER;\r |
| 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 |
| 520 | currentConfig.EmuOpt &= ~EOPT_16BPP;\r |
| 521 | if (!is_next)\r |
| 522 | PicoOpt |= POPT_ALT_RENDERER;\r |
| 523 | }\r |
| 524 | \r |
| 525 | if (is_menu)\r |
| 526 | return;\r |
| 527 | \r |
| 528 | vidResetMode();\r |
| 529 | rendstatus_old = -1;\r |
| 530 | \r |
| 531 | if (PicoOpt & POPT_ALT_RENDERER) {\r |
| 532 | emu_status_msg(" 8bit fast renderer");\r |
| 533 | } else if (currentConfig.EmuOpt & EOPT_16BPP) {\r |
| 534 | emu_status_msg("16bit accurate renderer");\r |
| 535 | } else {\r |
| 536 | emu_status_msg(" 8bit accurate renderer");\r |
| 537 | }\r |
| 538 | }\r |
| 539 | \r |
| 540 | #if 0 // TODO\r |
| 541 | static void RunEventsPico(unsigned int events)\r |
| 542 | {\r |
| 543 | int ret, px, py, lim_x;\r |
| 544 | static int pdown_frames = 0;\r |
| 545 | \r |
| 546 | // for F200\r |
| 547 | ret = gp2x_touchpad_read(&px, &py);\r |
| 548 | if (ret >= 0)\r |
| 549 | {\r |
| 550 | if (ret > 35000)\r |
| 551 | {\r |
| 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 |
| 565 | pdown_frames = 0;\r |
| 566 | \r |
| 567 | //if (ret == 0)\r |
| 568 | // PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;\r |
| 569 | }\r |
| 570 | }\r |
| 571 | #endif\r |
| 572 | \r |
| 573 | void plat_update_volume(int has_changed, int is_up)\r |
| 574 | {\r |
| 575 | static int prev_frame = 0, wait_frames = 0;\r |
| 576 | int vol = currentConfig.volume;\r |
| 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 |
| 583 | \r |
| 584 | if (has_changed)\r |
| 585 | {\r |
| 586 | if (need_low_volume && vol < 5 && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)\r |
| 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 |
| 595 | sndout_oss_setvol(vol, vol);\r |
| 596 | currentConfig.volume = vol;\r |
| 597 | }\r |
| 598 | emu_status_msg("VOL: %02i", vol);\r |
| 599 | prev_frame = Pico.m.frame_count;\r |
| 600 | }\r |
| 601 | \r |
| 602 | if (!need_low_volume)\r |
| 603 | return;\r |
| 604 | \r |
| 605 | /* set the right mixer func */\r |
| 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 |
| 614 | static void updateSound(int len)\r |
| 615 | {\r |
| 616 | len <<= 1;\r |
| 617 | if (PicoOpt & POPT_EN_STEREO)\r |
| 618 | len <<= 1;\r |
| 619 | \r |
| 620 | if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && !sndout_oss_can_write(len))\r |
| 621 | return;\r |
| 622 | \r |
| 623 | /* avoid writing audio when lagging behind to prevent audio lag */\r |
| 624 | if (PicoSkipFrame != 2)\r |
| 625 | sndout_oss_write(PsndOut, len);\r |
| 626 | }\r |
| 627 | \r |
| 628 | void pemu_sound_start(void)\r |
| 629 | {\r |
| 630 | static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;\r |
| 631 | \r |
| 632 | PsndOut = NULL;\r |
| 633 | \r |
| 634 | // prepare sound stuff\r |
| 635 | if (currentConfig.EmuOpt & EOPT_EN_SOUND)\r |
| 636 | {\r |
| 637 | int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0;\r |
| 638 | int target_fps = Pico.m.pal ? 50 : 60;\r |
| 639 | int frame_samples, snd_excess_add;\r |
| 640 | int snd_rate_oss = PsndRate;\r |
| 641 | gp2x_soc_t soc;\r |
| 642 | \r |
| 643 | soc = soc_detect();\r |
| 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 |
| 653 | \r |
| 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 |
| 657 | PsndRerate(Pico.m.frame_count ? 1 : 0);\r |
| 658 | }\r |
| 659 | \r |
| 660 | memset(sndBuffer, 0, sizeof(sndBuffer));\r |
| 661 | PsndOut = sndBuffer;\r |
| 662 | PicoWriteSound = updateSound;\r |
| 663 | PsndRate_old = PsndRate;\r |
| 664 | PicoOpt_old = PicoOpt;\r |
| 665 | pal_old = Pico.m.pal;\r |
| 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 |
| 678 | sndout_oss_start(snd_rate_oss, frame_samples, is_stereo);\r |
| 679 | \r |
| 680 | /* Wiz's sound hardware needs more prebuffer */\r |
| 681 | if (soc == SOCID_POLLUX)\r |
| 682 | updateSound(frame_samples);\r |
| 683 | }\r |
| 684 | }\r |
| 685 | \r |
| 686 | void pemu_sound_stop(void)\r |
| 687 | {\r |
| 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 |
| 695 | }\r |
| 696 | \r |
| 697 | void pemu_sound_wait(void)\r |
| 698 | {\r |
| 699 | // don't need to do anything, writes will block by themselves\r |
| 700 | }\r |
| 701 | \r |
| 702 | \r |
| 703 | void pemu_forced_frame(int opts)\r |
| 704 | {\r |
| 705 | int po_old = PicoOpt;\r |
| 706 | int eo_old = currentConfig.EmuOpt;\r |
| 707 | \r |
| 708 | PicoOpt &= ~POPT_ALT_RENDERER;\r |
| 709 | PicoOpt |= opts|POPT_ACC_SPRITES;\r |
| 710 | currentConfig.EmuOpt |= EOPT_16BPP;\r |
| 711 | \r |
| 712 | PicoDrawSetColorFormat(1);\r |
| 713 | PicoScanBegin = EmuScanBegin16;\r |
| 714 | PicoScanEnd = NULL;\r |
| 715 | Pico.m.dirtyPal = 1;\r |
| 716 | PicoFrameDrawOnly();\r |
| 717 | \r |
| 718 | PicoOpt = po_old;\r |
| 719 | currentConfig.EmuOpt = eo_old;\r |
| 720 | }\r |
| 721 | \r |
| 722 | void plat_debug_cat(char *str)\r |
| 723 | {\r |
| 724 | }\r |
| 725 | \r |
| 726 | void emu_video_mode_change(int start_line, int line_count, int is_32cols)\r |
| 727 | {\r |
| 728 | int scalex = 320;\r |
| 729 | osd_fps_x = OSD_FPS_X;\r |
| 730 | osd_y = 232;\r |
| 731 | if (is_32cols && (PicoOpt & POPT_DIS_32C_BORDER)) {\r |
| 732 | scalex = 256;\r |
| 733 | osd_fps_x = OSD_FPS_X - 64;\r |
| 734 | }\r |
| 735 | \r |
| 736 | /* want vertical scaling and game is not in 240 line mode */\r |
| 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 |
| 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 |
| 748 | }\r |
| 749 | \r |
| 750 | #if 0\r |
| 751 | static 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 |
| 783 | if(oldscr[i] != CONV(((unsigned short *)g_screen_ptr)[i])) break;\r |
| 784 | if (i < 320*240)\r |
| 785 | {\r |
| 786 | for (i = 0; i < 320*240; i++)\r |
| 787 | oldscr[i] = CONV(((unsigned short *)g_screen_ptr)[i]);\r |
| 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 |
| 798 | void pemu_loop_prep(void)\r |
| 799 | {\r |
| 800 | static int gp2x_old_clock = -1, EmuOpt_old = 0, pal_old = 0;\r |
| 801 | static int gp2x_old_gamma = 100;\r |
| 802 | gp2x_soc_t soc;\r |
| 803 | \r |
| 804 | soc = soc_detect();\r |
| 805 | \r |
| 806 | if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_RAM_TIMINGS) {\r |
| 807 | if (currentConfig.EmuOpt & EOPT_RAM_TIMINGS)\r |
| 808 | set_ram_timings();\r |
| 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 |
| 815 | if (gp2x_old_clock != currentConfig.CPUclock) {\r |
| 816 | printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);\r |
| 817 | gp2x_set_cpuclk(currentConfig.CPUclock);\r |
| 818 | gp2x_old_clock = currentConfig.CPUclock;\r |
| 819 | printf(" done\n");\r |
| 820 | }\r |
| 821 | \r |
| 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 |
| 824 | gp2x_old_gamma = currentConfig.gamma;\r |
| 825 | printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r |
| 826 | }\r |
| 827 | \r |
| 828 | if (((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_VSYNC) || Pico.m.pal != pal_old) {\r |
| 829 | if ((currentConfig.EmuOpt & EOPT_VSYNC) || soc == SOCID_POLLUX)\r |
| 830 | set_lcd_custom_rate(Pico.m.pal);\r |
| 831 | else if (EmuOpt_old & EOPT_VSYNC)\r |
| 832 | unset_lcd_custom_rate();\r |
| 833 | }\r |
| 834 | \r |
| 835 | if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_MMUHACK)\r |
| 836 | gp2x_make_fb_bufferable(currentConfig.EmuOpt & EOPT_MMUHACK);\r |
| 837 | \r |
| 838 | EmuOpt_old = currentConfig.EmuOpt;\r |
| 839 | pal_old = Pico.m.pal;\r |
| 840 | \r |
| 841 | // make sure we are in correct mode\r |
| 842 | vidResetMode();\r |
| 843 | scaling_update();\r |
| 844 | \r |
| 845 | pemu_sound_start();\r |
| 846 | }\r |
| 847 | \r |
| 848 | void pemu_loop_end(void)\r |
| 849 | {\r |
| 850 | int po_old = PicoOpt;\r |
| 851 | int eo_old = currentConfig.EmuOpt;\r |
| 852 | \r |
| 853 | pemu_sound_stop();\r |
| 854 | memset32(g_screen_ptr, 0, 320*240*2/4);\r |
| 855 | \r |
| 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 |
| 869 | }\r |
| 870 | \r |
| 871 | const 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 |
| 880 | "Squidge: mmuhack\n"\r |
| 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 |