| 1 | /*\r |
| 2 | * (c) Copyright 2006-2010 notaz, All rights reserved.\r |
| 3 | * (c) Copyright 2019-2024 irixxxx\r |
| 4 | *\r |
| 5 | * For performance reasons 3 renderers are exported for both MD and 32x modes:\r |
| 6 | * - 16bpp line renderer\r |
| 7 | * - 8bpp line renderer (slightly faster)\r |
| 8 | * - 8bpp tile renderer\r |
| 9 | * In 32x mode:\r |
| 10 | * - 32x layer is overlayed on top of 16bpp one\r |
| 11 | * - line internal one done on .Draw2FB, then mixed with 32x\r |
| 12 | * - tile internal one done on .Draw2FB, then mixed with 32x\r |
| 13 | */\r |
| 14 | \r |
| 15 | #include <stdio.h>\r |
| 16 | #include <stdlib.h>\r |
| 17 | #include <unistd.h>\r |
| 18 | \r |
| 19 | #include "../libpicofe/gp2x/plat_gp2x.h"\r |
| 20 | #include "../libpicofe/gp2x/soc.h"\r |
| 21 | #include "../libpicofe/input.h"\r |
| 22 | #include "../libpicofe/plat.h"\r |
| 23 | #include "../libpicofe/gp2x/soc_pollux.h"\r |
| 24 | #include "../common/menu_pico.h"\r |
| 25 | #include "../common/arm_utils.h"\r |
| 26 | #include "../common/emu.h"\r |
| 27 | #include "../common/keyboard.h"\r |
| 28 | #include "../common/config_file.h"\r |
| 29 | #include "../common/version.h"\r |
| 30 | #include "plat.h"\r |
| 31 | \r |
| 32 | #include <pico/pico_int.h>\r |
| 33 | #include <pico/patch.h>\r |
| 34 | #include <pico/sound/mix.h>\r |
| 35 | #include <zlib.h>\r |
| 36 | \r |
| 37 | #ifdef BENCHMARK\r |
| 38 | #define OSD_FPS_X 220\r |
| 39 | #else\r |
| 40 | #define OSD_FPS_X 260\r |
| 41 | #endif\r |
| 42 | \r |
| 43 | \r |
| 44 | //extern int crashed_940;\r |
| 45 | \r |
| 46 | static int osd_fps_x, osd_y, doing_bg_frame;\r |
| 47 | const char *renderer_names[] = { "16bit accurate", " 8bit accurate", " 8bit fast", NULL };\r |
| 48 | const char *renderer_names32x[] = { "accurate", "faster", "fastest", NULL };\r |
| 49 | enum renderer_types { RT_16BIT, RT_8BIT_ACC, RT_8BIT_FAST, RT_COUNT };\r |
| 50 | \r |
| 51 | static int is_1stblanked;\r |
| 52 | static int firstline, linecount;\r |
| 53 | static int firstcol, colcount;\r |
| 54 | \r |
| 55 | static int (*emu_scan_begin)(unsigned int num) = NULL;\r |
| 56 | static int (*emu_scan_end)(unsigned int num) = NULL;\r |
| 57 | \r |
| 58 | \r |
| 59 | void pemu_prep_defconfig(void)\r |
| 60 | {\r |
| 61 | gp2x_soc_t soc;\r |
| 62 | \r |
| 63 | defaultConfig.CPUclock = default_cpu_clock;\r |
| 64 | defaultConfig.renderer32x = RT_8BIT_ACC;\r |
| 65 | defaultConfig.analog_deadzone = 50;\r |
| 66 | \r |
| 67 | soc = soc_detect();\r |
| 68 | if (soc == SOCID_MMSP2)\r |
| 69 | defaultConfig.s_PicoOpt |= POPT_EXT_FM;\r |
| 70 | else if (soc == SOCID_POLLUX) {\r |
| 71 | defaultConfig.EmuOpt |= EOPT_WIZ_TEAR_FIX|EOPT_SHOW_RTC;\r |
| 72 | defaultConfig.s_PicoOpt |= POPT_EN_MCD_GFX;\r |
| 73 | }\r |
| 74 | }\r |
| 75 | \r |
| 76 | void pemu_validate_config(void)\r |
| 77 | {\r |
| 78 | if (gp2x_dev_id != GP2X_DEV_GP2X)\r |
| 79 | PicoIn.opt &= ~POPT_EXT_FM;\r |
| 80 | if (gp2x_dev_id != GP2X_DEV_WIZ)\r |
| 81 | currentConfig.EmuOpt &= ~EOPT_WIZ_TEAR_FIX;\r |
| 82 | \r |
| 83 | if (currentConfig.gamma < 10 || currentConfig.gamma > 300)\r |
| 84 | currentConfig.gamma = 100;\r |
| 85 | \r |
| 86 | if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 1024)\r |
| 87 | currentConfig.CPUclock = default_cpu_clock;\r |
| 88 | }\r |
| 89 | \r |
| 90 | static int get_renderer(void)\r |
| 91 | {\r |
| 92 | if (doing_bg_frame)\r |
| 93 | return RT_16BIT;\r |
| 94 | if (PicoIn.AHW & PAHW_32X)\r |
| 95 | return currentConfig.renderer32x;\r |
| 96 | else\r |
| 97 | return currentConfig.renderer;\r |
| 98 | }\r |
| 99 | \r |
| 100 | static void change_renderer(int diff)\r |
| 101 | {\r |
| 102 | int *r;\r |
| 103 | if (PicoIn.AHW & PAHW_32X)\r |
| 104 | r = ¤tConfig.renderer32x;\r |
| 105 | else\r |
| 106 | r = ¤tConfig.renderer;\r |
| 107 | *r += diff;\r |
| 108 | \r |
| 109 | if (*r >= RT_COUNT)\r |
| 110 | *r = 0;\r |
| 111 | else if (*r < 0)\r |
| 112 | *r = RT_COUNT - 1;\r |
| 113 | }\r |
| 114 | \r |
| 115 | #define is_16bit_mode() \\r |
| 116 | (currentConfig.renderer == RT_16BIT || (PicoIn.AHW & PAHW_32X) || doing_bg_frame)\r |
| 117 | \r |
| 118 | static void (*osd_text)(int x, int y, const char *text);\r |
| 119 | \r |
| 120 | static void osd_text8(int x, int y, const char *text)\r |
| 121 | {\r |
| 122 | int len = strlen(text)*8;\r |
| 123 | int *p, i, h, offs;\r |
| 124 | \r |
| 125 | len = (len+3) >> 2;\r |
| 126 | for (h = 0; h < 8; h++) {\r |
| 127 | offs = (x + g_screen_width * (y+h)) & ~3;\r |
| 128 | p = (int *) ((char *)g_screen_ptr + offs);\r |
| 129 | for (i = len; i; i--, p++)\r |
| 130 | *p = 0xe0e0e0e0;\r |
| 131 | }\r |
| 132 | emu_text_out8(x, y, text);\r |
| 133 | }\r |
| 134 | \r |
| 135 | static void osd_text8_rot(int x, int y, const char *text)\r |
| 136 | {\r |
| 137 | int len = strlen(text) * 8;\r |
| 138 | char *p = (char *)g_screen_ptr + 240*(320-x) + y;\r |
| 139 | \r |
| 140 | while (len--) {\r |
| 141 | memset(p, 0xe0, 8);\r |
| 142 | p -= 240;\r |
| 143 | }\r |
| 144 | \r |
| 145 | emu_text_out8_rot(x, y, text);\r |
| 146 | }\r |
| 147 | \r |
| 148 | static void osd_text16_rot(int x, int y, const char *text)\r |
| 149 | {\r |
| 150 | int len = strlen(text) * 8;\r |
| 151 | short *p = (short *)g_screen_ptr + 240*(320-x) + y;\r |
| 152 | \r |
| 153 | while (len--) {\r |
| 154 | memset(p, 0, 8*2);\r |
| 155 | p -= 240;\r |
| 156 | }\r |
| 157 | \r |
| 158 | emu_text_out16_rot(x, y, text);\r |
| 159 | }\r |
| 160 | \r |
| 161 | static void draw_cd_leds(void)\r |
| 162 | {\r |
| 163 | int led_reg, pitch, scr_offs, led_offs;\r |
| 164 | led_reg = Pico_mcd->s68k_regs[0];\r |
| 165 | \r |
| 166 | if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r |
| 167 | pitch = 240;\r |
| 168 | led_offs = -pitch * 6;\r |
| 169 | scr_offs = pitch * (320 - 4);\r |
| 170 | } else {\r |
| 171 | pitch = 320;\r |
| 172 | led_offs = 4;\r |
| 173 | scr_offs = pitch * 2 + 4;\r |
| 174 | }\r |
| 175 | \r |
| 176 | if (!is_16bit_mode()) {\r |
| 177 | #define p(x) px[(x) >> 2]\r |
| 178 | // 8-bit modes\r |
| 179 | unsigned int *px = (unsigned int *)((char *)g_screen_ptr + scr_offs);\r |
| 180 | unsigned int col_g = (led_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;\r |
| 181 | unsigned int col_r = (led_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;\r |
| 182 | p(pitch*0) = p(pitch*1) = p(pitch*2) = col_g;\r |
| 183 | p(pitch*0 + led_offs) = p(pitch*1 + led_offs) = p(pitch*2 + led_offs) = col_r;\r |
| 184 | #undef p\r |
| 185 | } else {\r |
| 186 | #define p(x) px[(x)*2 >> 2] = px[((x)*2 >> 2) + 1]\r |
| 187 | // 16-bit modes\r |
| 188 | unsigned int *px = (unsigned int *)((short *)g_screen_ptr + scr_offs);\r |
| 189 | unsigned int col_g = (led_reg & 2) ? 0x06000600 : 0;\r |
| 190 | unsigned int col_r = (led_reg & 1) ? 0xc000c000 : 0;\r |
| 191 | p(pitch*0) = p(pitch*1) = p(pitch*2) = col_g;\r |
| 192 | p(pitch*0 + led_offs) = p(pitch*1 + led_offs) = p(pitch*2 + led_offs) = col_r;\r |
| 193 | #undef p\r |
| 194 | }\r |
| 195 | }\r |
| 196 | \r |
| 197 | static void draw_pico_ptr(void)\r |
| 198 | {\r |
| 199 | int up = (PicoPicohw.pen_pos[0]|PicoPicohw.pen_pos[1]) & 0x8000;\r |
| 200 | int x, y, pitch = 320, offs;\r |
| 201 | // storyware pages are actually squished, 2:1\r |
| 202 | int h = (pico_inp_mode == 1 ? 160 : linecount);\r |
| 203 | if (h < 224) y++;\r |
| 204 | \r |
| 205 | x = ((pico_pen_x * colcount * ((1ULL<<32)/320 + 1)) >> 32) + firstcol;\r |
| 206 | y = ((pico_pen_y * h * ((1ULL<<32)/224 + 1)) >> 32) + firstline;\r |
| 207 | \r |
| 208 | if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r |
| 209 | pitch = 240;\r |
| 210 | offs = (319 - x) * pitch + y;\r |
| 211 | } else\r |
| 212 | offs = x + y * pitch;\r |
| 213 | \r |
| 214 | if (is_16bit_mode()) {\r |
| 215 | unsigned short *p = (unsigned short *)g_screen_ptr + offs;\r |
| 216 | int o = (up ? 0x0000 : 0xffff), _ = (up ? 0xffff : 0x0000);\r |
| 217 | \r |
| 218 | p[-pitch-1] ^= o; p[-pitch] ^= _; p[-pitch+1] ^= _; p[-pitch+2] ^= o;\r |
| 219 | p[-1] ^= _; p[0] ^= o; p[1] ^= o; p[2] ^= _;\r |
| 220 | p[pitch-1] ^= _; p[pitch] ^= o; p[pitch+1] ^= o; p[pitch+2] ^= _;\r |
| 221 | p[2*pitch-1]^= o; p[2*pitch]^= _; p[2*pitch+1]^= _; p[2*pitch+2]^= o;\r |
| 222 | } else {\r |
| 223 | unsigned char *p = (unsigned char *)g_screen_ptr + offs;\r |
| 224 | int o = (up ? 0xe0 : 0xf0), _ = (up ? 0xf0 : 0xe0);\r |
| 225 | \r |
| 226 | p[-pitch-1] = o; p[-pitch] = _; p[-pitch+1] = _; p[-pitch+2] = o;\r |
| 227 | p[-1] = _; p[0] = o; p[1] = o; p[2] = _;\r |
| 228 | p[pitch-1] = _; p[pitch] = o; p[pitch+1] = o; p[pitch+2] = _;\r |
| 229 | p[2*pitch-1]= o; p[2*pitch]= _; p[2*pitch+1]= _; p[2*pitch+2]= o;\r |
| 230 | }\r |
| 231 | }\r |
| 232 | \r |
| 233 | static void clear_1st_column(int firstcol, int firstline, int linecount)\r |
| 234 | {\r |
| 235 | int size = is_16bit_mode() ? 2 : 1;\r |
| 236 | int black = is_16bit_mode() ? 0 : 0xe0;\r |
| 237 | int i;\r |
| 238 | \r |
| 239 | // SMS 1st column blanked, replace with black\r |
| 240 | if ((currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) && !doing_bg_frame) {\r |
| 241 | int pitch = 240*size;\r |
| 242 | char *p = (char *)g_screen_ptr + (319-(firstcol-8))*pitch;\r |
| 243 | for (i = 0; i < 8; i++, p -= pitch)\r |
| 244 | memset(p+(firstline)*size, black, linecount*size);\r |
| 245 | } else {\r |
| 246 | int pitch = 320*size;\r |
| 247 | char *p = (char *)g_screen_ptr + (firstline)*pitch;\r |
| 248 | for (i = 0; i < linecount; i++, p += pitch)\r |
| 249 | memset(p+(firstcol-8)*size, black, 8*size);\r |
| 250 | }\r |
| 251 | }\r |
| 252 | \r |
| 253 | /* rot thing for Wiz */\r |
| 254 | static unsigned char __attribute__((aligned(4))) rot_buff[320*4*2];\r |
| 255 | \r |
| 256 | static int EmuScanBegin16_rot(unsigned int num)\r |
| 257 | {\r |
| 258 | Pico.est.DrawLineDest = rot_buff + (num & 3) * 320 * 2;\r |
| 259 | return 0;\r |
| 260 | }\r |
| 261 | \r |
| 262 | static int EmuScanEnd16_rot(unsigned int num)\r |
| 263 | {\r |
| 264 | if ((num & 3) != 3)\r |
| 265 | return 0;\r |
| 266 | rotated_blit16(g_screen_ptr, rot_buff, num + 1,\r |
| 267 | !(Pico.video.reg[12] & 1) && !(PicoIn.opt & POPT_EN_SOFTSCALE));\r |
| 268 | return 0;\r |
| 269 | }\r |
| 270 | \r |
| 271 | static int EmuScanBegin8_rot(unsigned int num)\r |
| 272 | {\r |
| 273 | Pico.est.DrawLineDest = rot_buff + (num & 3) * 320;\r |
| 274 | return 0;\r |
| 275 | }\r |
| 276 | \r |
| 277 | static int EmuScanEnd8_rot(unsigned int num)\r |
| 278 | {\r |
| 279 | if ((num & 3) != 3)\r |
| 280 | return 0;\r |
| 281 | rotated_blit8(g_screen_ptr, rot_buff, num + 1,\r |
| 282 | !(Pico.video.reg[12] & 1) && !(PicoIn.opt & POPT_EN_SOFTSCALE));\r |
| 283 | return 0;\r |
| 284 | }\r |
| 285 | \r |
| 286 | /* line doublers */\r |
| 287 | static unsigned int ld_counter;\r |
| 288 | static int ld_left, ld_lines; // numbers in Q1 format\r |
| 289 | \r |
| 290 | static int EmuScanBegin16_ld(unsigned int num)\r |
| 291 | {\r |
| 292 | if ((signed int)(ld_counter - num) > 100) {\r |
| 293 | // vsync, offset so that the upscaled image is centered\r |
| 294 | ld_counter = 120 - (120-num) * (ld_lines+2)/ld_lines;\r |
| 295 | ld_left = ld_lines;\r |
| 296 | }\r |
| 297 | \r |
| 298 | if (emu_scan_begin)\r |
| 299 | return emu_scan_begin(ld_counter);\r |
| 300 | else\r |
| 301 | Pico.est.DrawLineDest = (char *)g_screen_ptr + 320 * ld_counter * gp2x_current_bpp / 8;\r |
| 302 | \r |
| 303 | return 0;\r |
| 304 | }\r |
| 305 | \r |
| 306 | static int EmuScanEnd16_ld(unsigned int num)\r |
| 307 | {\r |
| 308 | void *oldline = Pico.est.DrawLineDest;\r |
| 309 | \r |
| 310 | if (emu_scan_end)\r |
| 311 | emu_scan_end(ld_counter);\r |
| 312 | \r |
| 313 | ld_counter++;\r |
| 314 | ld_left -= 2;\r |
| 315 | if (ld_left <= 0) {\r |
| 316 | ld_left += ld_lines;\r |
| 317 | \r |
| 318 | EmuScanBegin16_ld(num);\r |
| 319 | memcpy(Pico.est.DrawLineDest, oldline, 320 * gp2x_current_bpp / 8);\r |
| 320 | if (emu_scan_end)\r |
| 321 | emu_scan_end(ld_counter);\r |
| 322 | \r |
| 323 | ld_counter++;\r |
| 324 | }\r |
| 325 | \r |
| 326 | return 0;\r |
| 327 | }\r |
| 328 | \r |
| 329 | static int localPal[0x100];\r |
| 330 | static int localPalSize;\r |
| 331 | \r |
| 332 | static void (*vidcpy8bit)(void *dest, void *src, int x_y, int w_h);\r |
| 333 | static int (*make_local_pal)(int fast_mode);\r |
| 334 | \r |
| 335 | static int make_local_pal_md(int fast_mode)\r |
| 336 | {\r |
| 337 | int pallen = 0x100;\r |
| 338 | \r |
| 339 | if (fast_mode) {\r |
| 340 | bgr444_to_rgb32(localPal, PicoMem.cram, 64);\r |
| 341 | pallen = 0x40;\r |
| 342 | Pico.m.dirtyPal = 0;\r |
| 343 | }\r |
| 344 | else if (Pico.est.rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes\r |
| 345 | switch (Pico.est.SonicPalCount) {\r |
| 346 | case 3: bgr444_to_rgb32(localPal+0xc0, Pico.est.SonicPal+0xc0, 64);\r |
| 347 | case 2: bgr444_to_rgb32(localPal+0x80, Pico.est.SonicPal+0x80, 64);\r |
| 348 | case 1: bgr444_to_rgb32(localPal+0x40, Pico.est.SonicPal+0x40, 64);\r |
| 349 | default:bgr444_to_rgb32(localPal, Pico.est.SonicPal, 64);\r |
| 350 | }\r |
| 351 | pallen = (Pico.est.SonicPalCount+1)*0x40;\r |
| 352 | }\r |
| 353 | else if (Pico.video.reg[0xC] & 8) { // shadow/hilight mode\r |
| 354 | bgr444_to_rgb32(localPal, Pico.est.SonicPal, 64);\r |
| 355 | bgr444_to_rgb32_sh(localPal, Pico.est.SonicPal);\r |
| 356 | memcpy(localPal+0xc0, localPal, 0x40*4); // for spr prio mess\r |
| 357 | }\r |
| 358 | else {\r |
| 359 | bgr444_to_rgb32(localPal, Pico.est.SonicPal, 64);\r |
| 360 | memcpy(localPal+0x40, localPal, 0x40*4); // for spr prio mess\r |
| 361 | memcpy(localPal+0x80, localPal, 0x80*4); // for spr prio mess\r |
| 362 | }\r |
| 363 | localPal[0xc0] = 0x0000c000;\r |
| 364 | localPal[0xd0] = 0x00c00000;\r |
| 365 | localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r |
| 366 | localPal[0xf0] = 0x00ffffff;\r |
| 367 | \r |
| 368 | if (Pico.m.dirtyPal == 2)\r |
| 369 | Pico.m.dirtyPal = 0;\r |
| 370 | return pallen;\r |
| 371 | }\r |
| 372 | \r |
| 373 | static int make_local_pal_sms(int fast_mode)\r |
| 374 | {\r |
| 375 | static u16 tmspal[32] = {\r |
| 376 | // SMS palette for TMS modes\r |
| 377 | 0x0000, 0x0000, 0x00a0, 0x00f0, 0x0500, 0x0f00, 0x0005, 0x0ff0,\r |
| 378 | 0x000a, 0x000f, 0x0055, 0x00ff, 0x0050, 0x0f0f, 0x0555, 0x0fff,\r |
| 379 | // TMS palette\r |
| 380 | 0x0000, 0x0000, 0x04c2, 0x07d6, 0x0e55, 0x0f77, 0x055c, 0x0ee4,\r |
| 381 | 0x055f, 0x077f, 0x05bc, 0x08ce, 0x03a2, 0x0b5c, 0x0ccc, 0x0fff,\r |
| 382 | };\r |
| 383 | int i;\r |
| 384 | \r |
| 385 | if (!(Pico.video.reg[0] & 0x4)) {\r |
| 386 | for (i = Pico.est.SonicPalCount; i >= 0; i--) {\r |
| 387 | int sg = !!(PicoIn.AHW & (PAHW_SG|PAHW_SC));\r |
| 388 | bgr444_to_rgb32(localPal+i*0x40, tmspal+sg*0x10, 32);\r |
| 389 | memcpy(localPal+i*0x40+0x20, localPal+i*0x40, 0x20*4);\r |
| 390 | }\r |
| 391 | } else if (fast_mode) {\r |
| 392 | for (i = 0;i >= 0; i--) {\r |
| 393 | bgr444_to_rgb32(localPal+i*0x40, PicoMem.cram+i*0x40, 32);\r |
| 394 | memcpy(localPal+i*0x40+0x20, localPal+i*0x40, 0x20*4);\r |
| 395 | }\r |
| 396 | } else {\r |
| 397 | for (i = Pico.est.SonicPalCount; i >= 0; i--) {\r |
| 398 | bgr444_to_rgb32(localPal+i*0x40, Pico.est.SonicPal+i*0x40, 32);\r |
| 399 | memcpy(localPal+i*0x40+0x20, localPal+i*0x40, 0x20*4);\r |
| 400 | }\r |
| 401 | }\r |
| 402 | if (Pico.m.dirtyPal == 2)\r |
| 403 | Pico.m.dirtyPal = 0;\r |
| 404 | return (Pico.est.SonicPalCount+1)*0x40;\r |
| 405 | }\r |
| 406 | \r |
| 407 | void pemu_finalize_frame(const char *fps, const char *notice)\r |
| 408 | {\r |
| 409 | int emu_opt = currentConfig.EmuOpt;\r |
| 410 | int direct_rendered = 1;\r |
| 411 | \r |
| 412 | if (is_16bit_mode())\r |
| 413 | localPalSize = 0; // nothing to do\r |
| 414 | else if (get_renderer() == RT_8BIT_FAST)\r |
| 415 | {\r |
| 416 | // 8bit fast renderer\r |
| 417 | if (Pico.m.dirtyPal)\r |
| 418 | localPalSize = make_local_pal(1);\r |
| 419 | // a hack for VR\r |
| 420 | if (PicoIn.AHW & PAHW_SVP)\r |
| 421 | memset32((int *)(Pico.est.Draw2FB+328*8+328*223), 0xe0e0e0e0, 328/4);\r |
| 422 | // do actual copy\r |
| 423 | vidcpy8bit(g_screen_ptr, Pico.est.Draw2FB,\r |
| 424 | (firstcol << 16) | firstline, (colcount << 16) | linecount);\r |
| 425 | direct_rendered = 0;\r |
| 426 | }\r |
| 427 | else if (get_renderer() == RT_8BIT_ACC)\r |
| 428 | {\r |
| 429 | // 8bit accurate renderer\r |
| 430 | if (Pico.m.dirtyPal)\r |
| 431 | localPalSize = make_local_pal(0);\r |
| 432 | }\r |
| 433 | \r |
| 434 | // blank 1st column, only needed in modes directly rendering to screen\r |
| 435 | if (is_1stblanked && direct_rendered)\r |
| 436 | clear_1st_column(firstcol, firstline, linecount);\r |
| 437 | \r |
| 438 | if (notice)\r |
| 439 | osd_text(4, osd_y, notice);\r |
| 440 | if (emu_opt & EOPT_SHOW_FPS)\r |
| 441 | osd_text(osd_fps_x, osd_y, fps);\r |
| 442 | if ((PicoIn.AHW & PAHW_MCD) && (emu_opt & EOPT_EN_CD_LEDS))\r |
| 443 | draw_cd_leds();\r |
| 444 | if (PicoIn.AHW & PAHW_PICO) {\r |
| 445 | int h = linecount, w = colcount;\r |
| 446 | u16 *pd = g_screen_ptr + firstline*g_screen_ppitch + firstcol;\r |
| 447 | \r |
| 448 | if (pico_inp_mode && is_16bit_mode())\r |
| 449 | emu_pico_overlay(pd, w, h, g_screen_ppitch);\r |
| 450 | if (pico_inp_mode /*== 2 || overlay*/)\r |
| 451 | draw_pico_ptr();\r |
| 452 | }\r |
| 453 | // draw virtual keyboard on display\r |
| 454 | if (kbd_mode && currentConfig.keyboard == 1 && vkbd)\r |
| 455 | vkbd_draw(vkbd);\r |
| 456 | }\r |
| 457 | \r |
| 458 | void plat_video_flip(void)\r |
| 459 | {\r |
| 460 | int stride = g_screen_width;\r |
| 461 | gp2x_video_flip();\r |
| 462 | // switching the palette takes immediate effect, whilst flipping only\r |
| 463 | // takes effect with the next vsync; unavoidable flicker may occur!\r |
| 464 | if (localPalSize)\r |
| 465 | gp2x_video_setpalette(localPal, localPalSize);\r |
| 466 | \r |
| 467 | if (is_16bit_mode())\r |
| 468 | stride *= 2;\r |
| 469 | // the fast renderer has overlap areas and can't directly render to\r |
| 470 | // screen buffers. Its output is copied to screen in finalize_frame\r |
| 471 | if (get_renderer() != RT_8BIT_FAST || (PicoIn.AHW & PAHW_32X))\r |
| 472 | PicoDrawSetOutBuf(g_screen_ptr, stride);\r |
| 473 | }\r |
| 474 | \r |
| 475 | /* XXX */\r |
| 476 | unsigned int plat_get_ticks_ms(void)\r |
| 477 | {\r |
| 478 | return gp2x_get_ticks_ms();\r |
| 479 | }\r |
| 480 | \r |
| 481 | unsigned int plat_get_ticks_us(void)\r |
| 482 | {\r |
| 483 | return gp2x_get_ticks_us();\r |
| 484 | }\r |
| 485 | \r |
| 486 | void plat_wait_till_us(unsigned int us_to)\r |
| 487 | {\r |
| 488 | unsigned int now;\r |
| 489 | \r |
| 490 | spend_cycles(1024);\r |
| 491 | now = plat_get_ticks_us();\r |
| 492 | \r |
| 493 | while ((signed int)(us_to - now) > 512)\r |
| 494 | {\r |
| 495 | spend_cycles(1024);\r |
| 496 | now = plat_get_ticks_us();\r |
| 497 | }\r |
| 498 | }\r |
| 499 | \r |
| 500 | void plat_video_wait_vsync(void)\r |
| 501 | {\r |
| 502 | gp2x_video_wait_vsync();\r |
| 503 | }\r |
| 504 | \r |
| 505 | void plat_status_msg_clear(void)\r |
| 506 | {\r |
| 507 | int i, is_8bit = !is_16bit_mode();\r |
| 508 | \r |
| 509 | for (i = 0; i < 4; i++) {\r |
| 510 | if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r |
| 511 | /* ugh.. */\r |
| 512 | int u, *p;\r |
| 513 | if (is_8bit) {\r |
| 514 | p = (int *)gp2x_screens[i] + (240-8) / 4;\r |
| 515 | for (u = 320; u > 0; u--, p += 240/4)\r |
| 516 | p[0] = p[1] = 0xe0e0e0e0;\r |
| 517 | } else {\r |
| 518 | p = (int *)gp2x_screens[i] + (240-8)*2 / 4;\r |
| 519 | for (u = 320; u > 0; u--, p += 240*2/4)\r |
| 520 | p[0] = p[1] = p[2] = p[3] = 0;\r |
| 521 | }\r |
| 522 | } else {\r |
| 523 | if (is_8bit) {\r |
| 524 | char *d = (char *)gp2x_screens[i] + 320 * (240-8);\r |
| 525 | memset32((int *)d, 0xe0e0e0e0, 320 * 8 / 4);\r |
| 526 | } else {\r |
| 527 | char *d = (char *)gp2x_screens[i] + 320*2 * (240-8);\r |
| 528 | memset32((int *)d, 0, 2*320 * 8 / 4);\r |
| 529 | }\r |
| 530 | }\r |
| 531 | }\r |
| 532 | }\r |
| 533 | \r |
| 534 | void plat_status_msg_busy_next(const char *msg)\r |
| 535 | {\r |
| 536 | plat_status_msg_clear();\r |
| 537 | pemu_finalize_frame("", msg);\r |
| 538 | plat_video_flip();\r |
| 539 | emu_status_msg("");\r |
| 540 | \r |
| 541 | /* assumption: msg_busy_next gets called only when\r |
| 542 | * something slow is about to happen */\r |
| 543 | reset_timing = 1;\r |
| 544 | }\r |
| 545 | \r |
| 546 | void plat_status_msg_busy_first(const char *msg)\r |
| 547 | {\r |
| 548 | plat_status_msg_busy_next(msg);\r |
| 549 | }\r |
| 550 | \r |
| 551 | void plat_status_msg_busy_done(void)\r |
| 552 | {\r |
| 553 | }\r |
| 554 | \r |
| 555 | static void vid_reset_mode(void)\r |
| 556 | {\r |
| 557 | int gp2x_mode = 16;\r |
| 558 | int renderer = get_renderer();\r |
| 559 | \r |
| 560 | PicoIn.opt &= ~(POPT_ALT_RENDERER|POPT_DIS_32C_BORDER|POPT_EN_SOFTSCALE);\r |
| 561 | if (currentConfig.scaling == EOPT_SCALE_SW) {\r |
| 562 | PicoIn.opt |= POPT_EN_SOFTSCALE;\r |
| 563 | PicoIn.filter = EOPT_FILTER_BILINEAR2;\r |
| 564 | } else if (currentConfig.scaling == EOPT_SCALE_HW)\r |
| 565 | // hw scaling, render without any padding\r |
| 566 | PicoIn.opt |= POPT_DIS_32C_BORDER;\r |
| 567 | \r |
| 568 | switch (renderer) {\r |
| 569 | case RT_16BIT:\r |
| 570 | PicoDrawSetOutFormat(PDF_RGB555, 0);\r |
| 571 | PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r |
| 572 | break;\r |
| 573 | case RT_8BIT_ACC:\r |
| 574 | PicoDrawSetOutFormat(PDF_8BIT, 0);\r |
| 575 | PicoDrawSetOutBuf(g_screen_ptr, g_screen_width);\r |
| 576 | gp2x_mode = 8;\r |
| 577 | break;\r |
| 578 | case RT_8BIT_FAST:\r |
| 579 | PicoIn.opt |= POPT_ALT_RENDERER;\r |
| 580 | PicoDrawSetOutFormat(PDF_NONE, 0);\r |
| 581 | vidcpy8bit = vidcpy_8bit;\r |
| 582 | gp2x_mode = 8;\r |
| 583 | break;\r |
| 584 | default:\r |
| 585 | printf("bad renderer\n");\r |
| 586 | break;\r |
| 587 | }\r |
| 588 | \r |
| 589 | if (PicoIn.AHW & PAHW_32X) {\r |
| 590 | // Wiz 16bit is an exception, uses line rendering due to rotation mess\r |
| 591 | if (renderer == RT_16BIT && (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX)) {\r |
| 592 | PicoDrawSetOutFormat(PDF_RGB555, 1);\r |
| 593 | }\r |
| 594 | PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r |
| 595 | gp2x_mode = 16;\r |
| 596 | }\r |
| 597 | \r |
| 598 | emu_scan_begin = NULL;\r |
| 599 | emu_scan_end = NULL;\r |
| 600 | \r |
| 601 | if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r |
| 602 | if ((PicoIn.AHW & PAHW_32X) || renderer == RT_16BIT) {\r |
| 603 | emu_scan_begin = EmuScanBegin16_rot;\r |
| 604 | emu_scan_end = EmuScanEnd16_rot;\r |
| 605 | memset(rot_buff, 0, 320*4*2);\r |
| 606 | }\r |
| 607 | else if (renderer == RT_8BIT_ACC) {\r |
| 608 | emu_scan_begin = EmuScanBegin8_rot;\r |
| 609 | emu_scan_end = EmuScanEnd8_rot;\r |
| 610 | memset(rot_buff, 0xe0, 320*4);\r |
| 611 | }\r |
| 612 | else if (renderer == RT_8BIT_FAST)\r |
| 613 | vidcpy8bit = vidcpy_8bit_rot;\r |
| 614 | }\r |
| 615 | \r |
| 616 | PicoDrawSetCallbacks(emu_scan_begin, emu_scan_end);\r |
| 617 | \r |
| 618 | if (is_16bit_mode())\r |
| 619 | osd_text = (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) ? osd_text16_rot : emu_osd_text16;\r |
| 620 | else\r |
| 621 | osd_text = (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) ? osd_text8_rot : osd_text8;\r |
| 622 | \r |
| 623 | gp2x_video_wait_vsync();\r |
| 624 | if (!is_16bit_mode()) {\r |
| 625 | // setup pal for 8-bit modes\r |
| 626 | localPal[0xc0] = 0x0000c000; // MCD LEDs\r |
| 627 | localPal[0xd0] = 0x00c00000;\r |
| 628 | localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r |
| 629 | localPal[0xf0] = 0x00ffffff;\r |
| 630 | gp2x_video_setpalette(localPal, 0x100);\r |
| 631 | }\r |
| 632 | \r |
| 633 | if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX)\r |
| 634 | gp2x_mode = -gp2x_mode;\r |
| 635 | \r |
| 636 | gp2x_video_changemode(gp2x_mode, Pico.m.pal);\r |
| 637 | \r |
| 638 | // clear whole screen in all buffers\r |
| 639 | if (!is_16bit_mode())\r |
| 640 | gp2x_memset_all_buffers(0, 0xe0, 320*240);\r |
| 641 | else\r |
| 642 | gp2x_memset_all_buffers(0, 0, 320*240*2);\r |
| 643 | \r |
| 644 | Pico.m.dirtyPal = 1;\r |
| 645 | \r |
| 646 | // palette converters for 8bit modes\r |
| 647 | make_local_pal = (PicoIn.AHW & PAHW_SMS) ? make_local_pal_sms : make_local_pal_md;\r |
| 648 | }\r |
| 649 | \r |
| 650 | void emu_video_mode_change(int start_line, int line_count, int start_col, int col_count)\r |
| 651 | {\r |
| 652 | int scalex = 320, scaley = 240;\r |
| 653 | int ln_offs = 0;\r |
| 654 | \r |
| 655 | if (currentConfig.vscaling != EOPT_SCALE_NONE &&\r |
| 656 | (is_16bit_mode() || get_renderer() != RT_8BIT_FAST)) {\r |
| 657 | /* NTSC always has 224 visible lines, anything smaller has bars */\r |
| 658 | if (line_count < 224 && line_count > 144) {\r |
| 659 | start_line -= (224-line_count) /2;\r |
| 660 | line_count = 224;\r |
| 661 | }\r |
| 662 | \r |
| 663 | /* line doubling for swscaling, also needed for bg frames */\r |
| 664 | if (currentConfig.vscaling == EOPT_SCALE_SW && line_count < 240) {\r |
| 665 | ld_lines = ld_left = 2*line_count / (240 - line_count);\r |
| 666 | PicoDrawSetCallbacks(EmuScanBegin16_ld,EmuScanEnd16_ld);\r |
| 667 | }\r |
| 668 | }\r |
| 669 | \r |
| 670 | /* blanking for SMS with 1st tile blanked */\r |
| 671 | is_1stblanked = (col_count == 248);\r |
| 672 | firstline = start_line; linecount = line_count;\r |
| 673 | firstcol = start_col; colcount = col_count;\r |
| 674 | \r |
| 675 | if (doing_bg_frame)\r |
| 676 | return;\r |
| 677 | \r |
| 678 | osd_fps_x = OSD_FPS_X;\r |
| 679 | osd_y = 232;\r |
| 680 | \r |
| 681 | /* set up hwscaling here */\r |
| 682 | if (col_count < 320 && currentConfig.scaling == EOPT_SCALE_HW) {\r |
| 683 | scalex = col_count;\r |
| 684 | osd_fps_x = col_count - (320-OSD_FPS_X);\r |
| 685 | }\r |
| 686 | \r |
| 687 | if (currentConfig.vscaling == EOPT_SCALE_HW) {\r |
| 688 | ln_offs = start_line;\r |
| 689 | scaley = line_count;\r |
| 690 | osd_y = start_line + line_count - 8;\r |
| 691 | }\r |
| 692 | \r |
| 693 | gp2x_video_RGB_setscaling(ln_offs, scalex, scaley);\r |
| 694 | \r |
| 695 | // clear whole screen in all buffers\r |
| 696 | if (!is_16bit_mode())\r |
| 697 | gp2x_memset_all_buffers(0, 0xe0, 320*240);\r |
| 698 | else\r |
| 699 | gp2x_memset_all_buffers(0, 0, 320*240*2);\r |
| 700 | }\r |
| 701 | \r |
| 702 | void plat_video_clear_buffers(void)\r |
| 703 | {\r |
| 704 | if (!is_16bit_mode())\r |
| 705 | gp2x_memset_all_buffers(0, 0xe0, 320*240);\r |
| 706 | else\r |
| 707 | gp2x_memset_all_buffers(0, 0, 320*240*2);\r |
| 708 | }\r |
| 709 | \r |
| 710 | void plat_video_toggle_renderer(int change, int is_menu_call)\r |
| 711 | {\r |
| 712 | change_renderer(change);\r |
| 713 | \r |
| 714 | if (is_menu_call)\r |
| 715 | return;\r |
| 716 | \r |
| 717 | vid_reset_mode();\r |
| 718 | rendstatus_old = -1;\r |
| 719 | \r |
| 720 | if (PicoIn.AHW & PAHW_32X)\r |
| 721 | emu_status_msg(renderer_names32x[get_renderer()]);\r |
| 722 | else\r |
| 723 | emu_status_msg(renderer_names[get_renderer()]);\r |
| 724 | }\r |
| 725 | \r |
| 726 | #if 0 // TODO\r |
| 727 | static void RunEventsPico(unsigned int events)\r |
| 728 | {\r |
| 729 | int ret, px, py, lim_x;\r |
| 730 | static int pdown_frames = 0;\r |
| 731 | \r |
| 732 | // for F200\r |
| 733 | ret = gp2x_touchpad_read(&px, &py);\r |
| 734 | if (ret >= 0)\r |
| 735 | {\r |
| 736 | if (ret > 35000)\r |
| 737 | {\r |
| 738 | if (pdown_frames++ > 5)\r |
| 739 | PicoIn.pad[0] |= 0x20;\r |
| 740 | \r |
| 741 | pico_pen_x = px;\r |
| 742 | pico_pen_y = py;\r |
| 743 | if (!(Pico.video.reg[12]&1)) {\r |
| 744 | pico_pen_x -= 32;\r |
| 745 | if (pico_pen_x < 0) pico_pen_x = 0;\r |
| 746 | if (pico_pen_x > 248) pico_pen_x = 248;\r |
| 747 | }\r |
| 748 | if (pico_pen_y > 224) pico_pen_y = 224;\r |
| 749 | }\r |
| 750 | else\r |
| 751 | pdown_frames = 0;\r |
| 752 | \r |
| 753 | //if (ret == 0)\r |
| 754 | // PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;\r |
| 755 | }\r |
| 756 | }\r |
| 757 | #endif\r |
| 758 | \r |
| 759 | void plat_update_volume(int has_changed, int is_up)\r |
| 760 | {\r |
| 761 | static int prev_frame = 0, wait_frames = 0;\r |
| 762 | int need_low_volume = 0;\r |
| 763 | int vol = currentConfig.volume;\r |
| 764 | gp2x_soc_t soc;\r |
| 765 | \r |
| 766 | soc = soc_detect();\r |
| 767 | if ((PicoIn.opt & POPT_EN_STEREO) && soc == SOCID_MMSP2)\r |
| 768 | need_low_volume = 1;\r |
| 769 | \r |
| 770 | if (has_changed)\r |
| 771 | {\r |
| 772 | if (need_low_volume && vol < 5 && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)\r |
| 773 | wait_frames++;\r |
| 774 | else {\r |
| 775 | wait_frames = 0;\r |
| 776 | plat_target_step_volume(¤tConfig.volume, is_up ? 1 : -1);\r |
| 777 | vol = currentConfig.volume;\r |
| 778 | }\r |
| 779 | emu_status_msg("VOL: %02i", vol);\r |
| 780 | prev_frame = Pico.m.frame_count;\r |
| 781 | }\r |
| 782 | \r |
| 783 | if (!need_low_volume)\r |
| 784 | return;\r |
| 785 | \r |
| 786 | /* set the right mixer func */\r |
| 787 | if (vol >= 5)\r |
| 788 | PsndMix_32_to_16 = mix_32_to_16_stereo;\r |
| 789 | else {\r |
| 790 | mix_32_to_16_level = 5 - vol;\r |
| 791 | PsndMix_32_to_16 = mix_32_to_16_stereo_lvl;\r |
| 792 | }\r |
| 793 | }\r |
| 794 | \r |
| 795 | void pemu_sound_start(void)\r |
| 796 | {\r |
| 797 | gp2x_soc_t soc;\r |
| 798 | \r |
| 799 | emu_sound_start();\r |
| 800 | \r |
| 801 | if (currentConfig.EmuOpt & EOPT_EN_SOUND)\r |
| 802 | {\r |
| 803 | soc = soc_detect();\r |
| 804 | if (soc == SOCID_POLLUX) {\r |
| 805 | PicoIn.sndRate = pollux_get_real_snd_rate(PicoIn.sndRate);\r |
| 806 | PsndRerate(Pico.m.frame_count ? 1 : 0);\r |
| 807 | }\r |
| 808 | \r |
| 809 | plat_target_step_volume(¤tConfig.volume, 0);\r |
| 810 | }\r |
| 811 | }\r |
| 812 | \r |
| 813 | static const int sound_rates[] = { 52000, 44100, 32000, 22050, 16000, 11025, 8000 };\r |
| 814 | \r |
| 815 | void pemu_sound_stop(void)\r |
| 816 | {\r |
| 817 | int i;\r |
| 818 | \r |
| 819 | /* get back from Pollux pain */\r |
| 820 | PicoIn.sndRate += 1000;\r |
| 821 | for (i = 0; i < ARRAY_SIZE(sound_rates); i++) {\r |
| 822 | if (PicoIn.sndRate >= sound_rates[i]) {\r |
| 823 | PicoIn.sndRate = sound_rates[i];\r |
| 824 | break;\r |
| 825 | }\r |
| 826 | }\r |
| 827 | }\r |
| 828 | \r |
| 829 | void pemu_forced_frame(int no_scale, int do_emu)\r |
| 830 | {\r |
| 831 | doing_bg_frame = 1;\r |
| 832 | PicoDrawSetCallbacks(NULL, NULL);\r |
| 833 | Pico.m.dirtyPal = 1;\r |
| 834 | PicoIn.opt &= ~POPT_DIS_32C_BORDER;\r |
| 835 | gp2x_current_bpp = 16;\r |
| 836 | // always render in screen 3 since menu uses 0-2\r |
| 837 | g_screen_ptr = gp2x_screens[3];\r |
| 838 | \r |
| 839 | if (!no_scale)\r |
| 840 | no_scale = currentConfig.scaling == EOPT_SCALE_NONE;\r |
| 841 | emu_cmn_forced_frame(no_scale, do_emu, g_screen_ptr);\r |
| 842 | \r |
| 843 | if (is_1stblanked)\r |
| 844 | clear_1st_column(firstcol, firstline, linecount);\r |
| 845 | \r |
| 846 | g_menubg_src_ptr = g_screen_ptr;\r |
| 847 | doing_bg_frame = 0;\r |
| 848 | }\r |
| 849 | \r |
| 850 | void plat_debug_cat(char *str)\r |
| 851 | {\r |
| 852 | }\r |
| 853 | \r |
| 854 | void plat_video_loop_prepare(void) \r |
| 855 | {\r |
| 856 | // make sure we are in correct mode\r |
| 857 | change_renderer(0);\r |
| 858 | vid_reset_mode();\r |
| 859 | rendstatus_old = -1;\r |
| 860 | }\r |
| 861 | \r |
| 862 | void pemu_loop_prep(void)\r |
| 863 | {\r |
| 864 | if (gp2x_dev_id == GP2X_DEV_CAANOO)\r |
| 865 | in_set_config_int(in_name_to_id("evdev:pollux-analog"),\r |
| 866 | IN_CFG_ABS_DEAD_ZONE,\r |
| 867 | currentConfig.analog_deadzone);\r |
| 868 | \r |
| 869 | // dirty buffers better go now than during gameplay\r |
| 870 | sync();\r |
| 871 | sleep(0);\r |
| 872 | }\r |
| 873 | \r |
| 874 | void pemu_loop_end(void)\r |
| 875 | {\r |
| 876 | pemu_sound_stop();\r |
| 877 | \r |
| 878 | if (g_screen_ptr == gp2x_screens[0]) {\r |
| 879 | /* currently on screen 3, which is needed for forced_frame */\r |
| 880 | int size = gp2x_current_bpp / 8;\r |
| 881 | gp2x_memcpy_all_buffers(g_screen_ptr, 0, 320*240 * size);\r |
| 882 | gp2x_video_flip();\r |
| 883 | }\r |
| 884 | /* do one more frame for menu bg */\r |
| 885 | pemu_forced_frame(0, 1);\r |
| 886 | }\r |
| 887 | \r |