| 1 | // (c) Copyright 2006-2007 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 | #include <sys/time.h>\r |
| 9 | #include <sys/stat.h>\r |
| 10 | #include <sys/types.h>\r |
| 11 | #include <linux/limits.h>\r |
| 12 | #include <ctype.h>\r |
| 13 | #include <unistd.h>\r |
| 14 | \r |
| 15 | #include <stdarg.h>\r |
| 16 | \r |
| 17 | #include "emu.h"\r |
| 18 | #include "gp2x.h"\r |
| 19 | #include "usbjoy.h"\r |
| 20 | #include "menu.h"\r |
| 21 | #include "../common/arm_utils.h"\r |
| 22 | #include "../common/fonts.h"\r |
| 23 | #include "cpuctrl.h"\r |
| 24 | \r |
| 25 | #include <Pico/PicoInt.h>\r |
| 26 | #include <Pico/Patch.h>\r |
| 27 | #include <zlib/zlib.h>\r |
| 28 | \r |
| 29 | \r |
| 30 | #ifdef BENCHMARK\r |
| 31 | #define OSD_FPS_X 220\r |
| 32 | #else\r |
| 33 | #define OSD_FPS_X 260\r |
| 34 | #endif\r |
| 35 | \r |
| 36 | \r |
| 37 | int engineState;\r |
| 38 | int select_exits = 0;\r |
| 39 | char *PicoConfigFile = "picoconfig.bin";\r |
| 40 | currentConfig_t currentConfig;\r |
| 41 | \r |
| 42 | char romFileName[PATH_MAX];\r |
| 43 | unsigned char *rom_data = NULL;\r |
| 44 | \r |
| 45 | extern int crashed_940;\r |
| 46 | \r |
| 47 | static short sndBuffer[2*44100/50];\r |
| 48 | static char noticeMsg[64]; // notice msg to draw\r |
| 49 | static struct timeval noticeMsgTime = { 0, 0 }; // when started showing\r |
| 50 | static int osd_fps_x;\r |
| 51 | static int combo_keys = 0, combo_acts = 0; // keys and actions which need button combos\r |
| 52 | static int gp2x_old_gamma = 100;\r |
| 53 | static unsigned char *movie_data = NULL;\r |
| 54 | static int movie_size = 0;\r |
| 55 | unsigned char *PicoDraw2FB = NULL; // temporary buffer for alt renderer\r |
| 56 | int state_slot = 0;\r |
| 57 | int reset_timing = 0;\r |
| 58 | int config_slot = 0, config_slot_current = 0;\r |
| 59 | \r |
| 60 | \r |
| 61 | // utilities\r |
| 62 | static void strlwr(char* string)\r |
| 63 | {\r |
| 64 | while ( (*string++ = (char)tolower(*string)) );\r |
| 65 | }\r |
| 66 | \r |
| 67 | static int try_rfn_cut(void)\r |
| 68 | {\r |
| 69 | FILE *tmp;\r |
| 70 | char *p;\r |
| 71 | \r |
| 72 | p = romFileName + strlen(romFileName) - 1;\r |
| 73 | for (; p > romFileName; p--)\r |
| 74 | if (*p == '.') break;\r |
| 75 | *p = 0;\r |
| 76 | \r |
| 77 | if((tmp = fopen(romFileName, "rb"))) {\r |
| 78 | fclose(tmp);\r |
| 79 | return 1;\r |
| 80 | }\r |
| 81 | return 0;\r |
| 82 | }\r |
| 83 | \r |
| 84 | static void get_ext(char *file, char *ext)\r |
| 85 | {\r |
| 86 | char *p;\r |
| 87 | \r |
| 88 | p = file + strlen(file) - 4;\r |
| 89 | if (p < file) p = file;\r |
| 90 | strncpy(ext, p, 4);\r |
| 91 | ext[4] = 0;\r |
| 92 | strlwr(ext);\r |
| 93 | }\r |
| 94 | \r |
| 95 | char *biosfiles_us[] = { "us_scd2_9306", "SegaCDBIOS9303", "us_scd1_9210" };\r |
| 96 | char *biosfiles_eu[] = { "eu_mcd2_9306", "eu_mcd2_9303", "eu_mcd1_9210" };\r |
| 97 | char *biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };\r |
| 98 | \r |
| 99 | extern char **g_argv;\r |
| 100 | \r |
| 101 | int find_bios(int region, char **bios_file)\r |
| 102 | {\r |
| 103 | static char bios_path[1024];\r |
| 104 | int i, j, count;\r |
| 105 | char **files;\r |
| 106 | FILE *f = NULL;\r |
| 107 | \r |
| 108 | if (region == 4) { // US\r |
| 109 | files = biosfiles_us;\r |
| 110 | count = sizeof(biosfiles_us) / sizeof(char *);\r |
| 111 | } else if (region == 8) { // EU\r |
| 112 | files = biosfiles_eu;\r |
| 113 | count = sizeof(biosfiles_eu) / sizeof(char *);\r |
| 114 | } else if (region == 1 || region == 2) {\r |
| 115 | files = biosfiles_jp;\r |
| 116 | count = sizeof(biosfiles_jp) / sizeof(char *);\r |
| 117 | } else {\r |
| 118 | return 0;\r |
| 119 | }\r |
| 120 | \r |
| 121 | for (i = 0; i < count; i++)\r |
| 122 | {\r |
| 123 | strncpy(bios_path, g_argv[0], 1023);\r |
| 124 | bios_path[1024-32] = 0;\r |
| 125 | for (j = strlen(bios_path); j > 0; j--)\r |
| 126 | if (bios_path[j] == '/') { bios_path[j+1] = 0; break; }\r |
| 127 | strcat(bios_path, files[i]);\r |
| 128 | strcat(bios_path, ".bin");\r |
| 129 | f = fopen(bios_path, "rb");\r |
| 130 | if (f) break;\r |
| 131 | \r |
| 132 | bios_path[strlen(bios_path) - 4] = 0;\r |
| 133 | strcat(bios_path, ".zip");\r |
| 134 | f = fopen(bios_path, "rb");\r |
| 135 | if (f) break;\r |
| 136 | }\r |
| 137 | \r |
| 138 | if (f) {\r |
| 139 | printf("using bios: %s\n", bios_path);\r |
| 140 | fclose(f);\r |
| 141 | if (bios_file) *bios_file = bios_path;\r |
| 142 | return 1;\r |
| 143 | } else {\r |
| 144 | sprintf(menuErrorMsg, "no %s BIOS files found, read docs",\r |
| 145 | region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r |
| 146 | printf("%s\n", menuErrorMsg);\r |
| 147 | return 0;\r |
| 148 | }\r |
| 149 | }\r |
| 150 | \r |
| 151 | /* checks if romFileName points to valid MegaCD image\r |
| 152 | * if so, checks for suitable BIOS */\r |
| 153 | int emu_cd_check(char **bios_file)\r |
| 154 | {\r |
| 155 | unsigned char buf[32];\r |
| 156 | pm_file *cd_f;\r |
| 157 | int type = 0, region = 4; // 1: Japan, 4: US, 8: Europe\r |
| 158 | \r |
| 159 | cd_f = pm_open(romFileName);\r |
| 160 | if (!cd_f) return 0; // let the upper level handle this\r |
| 161 | \r |
| 162 | if (pm_read(buf, 32, cd_f) != 32) {\r |
| 163 | pm_close(cd_f);\r |
| 164 | return 0;\r |
| 165 | }\r |
| 166 | \r |
| 167 | if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) type = 1; // Sega CD (ISO)\r |
| 168 | if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) type = 2; // Sega CD (BIN)\r |
| 169 | if (type == 0) {\r |
| 170 | pm_close(cd_f);\r |
| 171 | return 0;\r |
| 172 | }\r |
| 173 | \r |
| 174 | /* it seems we have a CD image here. Try to detect region now.. */\r |
| 175 | pm_seek(cd_f, (type == 1) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);\r |
| 176 | pm_read(buf, 1, cd_f);\r |
| 177 | pm_close(cd_f);\r |
| 178 | \r |
| 179 | if (buf[0] == 0x64) region = 8; // EU\r |
| 180 | if (buf[0] == 0xa1) region = 1; // JAP\r |
| 181 | \r |
| 182 | printf("detected %s Sega/Mega CD image with %s region\n",\r |
| 183 | type == 2 ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r |
| 184 | \r |
| 185 | if (PicoRegionOverride) {\r |
| 186 | region = PicoRegionOverride;\r |
| 187 | printf("overrided region to %s\n", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r |
| 188 | }\r |
| 189 | \r |
| 190 | if (bios_file == NULL) return type;\r |
| 191 | \r |
| 192 | if (find_bios(region, bios_file))\r |
| 193 | return type; // CD and BIOS detected\r |
| 194 | \r |
| 195 | return -1; // CD detected but load failed\r |
| 196 | }\r |
| 197 | \r |
| 198 | int emu_ReloadRom(void)\r |
| 199 | {\r |
| 200 | unsigned int rom_size = 0;\r |
| 201 | char *used_rom_name = romFileName;\r |
| 202 | char ext[5];\r |
| 203 | pm_file *rom;\r |
| 204 | int ret, cd_state;\r |
| 205 | \r |
| 206 | printf("emu_ReloadRom(%s)\n", romFileName);\r |
| 207 | \r |
| 208 | get_ext(romFileName, ext);\r |
| 209 | \r |
| 210 | // detect wrong extensions\r |
| 211 | if(!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz\r |
| 212 | sprintf(menuErrorMsg, "Not a ROM selected.");\r |
| 213 | return 0;\r |
| 214 | }\r |
| 215 | \r |
| 216 | PicoPatchUnload();\r |
| 217 | \r |
| 218 | // check for movie file\r |
| 219 | if(movie_data) {\r |
| 220 | free(movie_data);\r |
| 221 | movie_data = 0;\r |
| 222 | }\r |
| 223 | if(!strcmp(ext, ".gmv")) {\r |
| 224 | // check for both gmv and rom\r |
| 225 | int dummy;\r |
| 226 | FILE *movie_file = fopen(romFileName, "rb");\r |
| 227 | if(!movie_file) {\r |
| 228 | sprintf(menuErrorMsg, "Failed to open movie.");\r |
| 229 | return 0;\r |
| 230 | }\r |
| 231 | fseek(movie_file, 0, SEEK_END);\r |
| 232 | movie_size = ftell(movie_file);\r |
| 233 | fseek(movie_file, 0, SEEK_SET);\r |
| 234 | if(movie_size < 64+3) {\r |
| 235 | sprintf(menuErrorMsg, "Invalid GMV file.");\r |
| 236 | fclose(movie_file);\r |
| 237 | return 0;\r |
| 238 | }\r |
| 239 | movie_data = malloc(movie_size);\r |
| 240 | if(movie_data == NULL) {\r |
| 241 | sprintf(menuErrorMsg, "low memory.");\r |
| 242 | fclose(movie_file);\r |
| 243 | return 0;\r |
| 244 | }\r |
| 245 | fread(movie_data, 1, movie_size, movie_file);\r |
| 246 | fclose(movie_file);\r |
| 247 | if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {\r |
| 248 | sprintf(menuErrorMsg, "Invalid GMV file.");\r |
| 249 | return 0;\r |
| 250 | }\r |
| 251 | dummy = try_rfn_cut() || try_rfn_cut();\r |
| 252 | if (!dummy) {\r |
| 253 | sprintf(menuErrorMsg, "Could't find a ROM for movie.");\r |
| 254 | return 0;\r |
| 255 | }\r |
| 256 | get_ext(romFileName, ext);\r |
| 257 | }\r |
| 258 | else if (!strcmp(ext, ".pat")) {\r |
| 259 | int dummy;\r |
| 260 | PicoPatchLoad(romFileName);\r |
| 261 | dummy = try_rfn_cut() || try_rfn_cut();\r |
| 262 | if (!dummy) {\r |
| 263 | sprintf(menuErrorMsg, "Could't find a ROM to patch.");\r |
| 264 | return 0;\r |
| 265 | }\r |
| 266 | get_ext(romFileName, ext);\r |
| 267 | }\r |
| 268 | \r |
| 269 | if ((PicoMCD & 1) && Pico_mcd != NULL)\r |
| 270 | Stop_CD();\r |
| 271 | \r |
| 272 | // check for MegaCD image\r |
| 273 | cd_state = emu_cd_check(&used_rom_name);\r |
| 274 | if (cd_state > 0) {\r |
| 275 | PicoMCD |= 1;\r |
| 276 | get_ext(used_rom_name, ext);\r |
| 277 | } else if (cd_state == -1) {\r |
| 278 | // bios_help() ?\r |
| 279 | return 0;\r |
| 280 | } else {\r |
| 281 | if (PicoMCD & 1) Stop_CD();\r |
| 282 | PicoMCD &= ~1;\r |
| 283 | }\r |
| 284 | \r |
| 285 | rom = pm_open(used_rom_name);\r |
| 286 | if(!rom) {\r |
| 287 | sprintf(menuErrorMsg, "Failed to open rom.");\r |
| 288 | return 0;\r |
| 289 | }\r |
| 290 | \r |
| 291 | menu_romload_prepare(used_rom_name);\r |
| 292 | \r |
| 293 | if(rom_data) {\r |
| 294 | free(rom_data);\r |
| 295 | rom_data = 0;\r |
| 296 | rom_size = 0;\r |
| 297 | }\r |
| 298 | \r |
| 299 | if( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) {\r |
| 300 | sprintf(menuErrorMsg, "PicoCartLoad() failed.");\r |
| 301 | printf("%s\n", menuErrorMsg);\r |
| 302 | pm_close(rom);\r |
| 303 | menu_romload_end();\r |
| 304 | return 0;\r |
| 305 | }\r |
| 306 | pm_close(rom);\r |
| 307 | menu_romload_end();\r |
| 308 | \r |
| 309 | // detect wrong files (Pico crashes on very small files), also see if ROM EP is good\r |
| 310 | if(rom_size <= 0x200 || strncmp((char *)rom_data, "Pico", 4) == 0 ||\r |
| 311 | ((*(unsigned char *)(rom_data+4)<<16)|(*(unsigned short *)(rom_data+6))) >= (int)rom_size) {\r |
| 312 | if (rom_data) free(rom_data);\r |
| 313 | rom_data = 0;\r |
| 314 | sprintf(menuErrorMsg, "Not a ROM selected.");\r |
| 315 | return 0;\r |
| 316 | }\r |
| 317 | \r |
| 318 | // load config for this ROM (do this before insert to get correct region)\r |
| 319 | ret = emu_ReadConfig(1, 1);\r |
| 320 | if (!ret)\r |
| 321 | emu_ReadConfig(0, 1);\r |
| 322 | \r |
| 323 | printf("PicoCartInsert(%p, %d);\n", rom_data, rom_size);\r |
| 324 | if(PicoCartInsert(rom_data, rom_size)) {\r |
| 325 | sprintf(menuErrorMsg, "Failed to load ROM.");\r |
| 326 | return 0;\r |
| 327 | }\r |
| 328 | \r |
| 329 | Pico.m.frame_count = 0;\r |
| 330 | \r |
| 331 | // insert CD if it was detected\r |
| 332 | if (cd_state > 0) {\r |
| 333 | ret = Insert_CD(romFileName, cd_state == 2);\r |
| 334 | if (ret != 0) {\r |
| 335 | sprintf(menuErrorMsg, "Insert_CD() failed, invalid CD image?");\r |
| 336 | printf("%s\n", menuErrorMsg);\r |
| 337 | return 0;\r |
| 338 | }\r |
| 339 | }\r |
| 340 | \r |
| 341 | // emu_ReadConfig() might have messed currentConfig.lastRomFile\r |
| 342 | strncpy(currentConfig.lastRomFile, romFileName, sizeof(currentConfig.lastRomFile)-1);\r |
| 343 | currentConfig.lastRomFile[sizeof(currentConfig.lastRomFile)-1] = 0;\r |
| 344 | \r |
| 345 | if (PicoPatches) {\r |
| 346 | PicoPatchPrepare();\r |
| 347 | PicoPatchApply();\r |
| 348 | }\r |
| 349 | \r |
| 350 | // additional movie stuff\r |
| 351 | if(movie_data) {\r |
| 352 | if(movie_data[0x14] == '6')\r |
| 353 | PicoOpt |= 0x20; // 6 button pad\r |
| 354 | else PicoOpt &= ~0x20;\r |
| 355 | PicoOpt |= 0x40; // accurate timing\r |
| 356 | if(movie_data[0xF] >= 'A') {\r |
| 357 | if(movie_data[0x16] & 0x80) {\r |
| 358 | PicoRegionOverride = 8;\r |
| 359 | } else {\r |
| 360 | PicoRegionOverride = 4;\r |
| 361 | }\r |
| 362 | PicoReset(0);\r |
| 363 | // TODO: bits 6 & 5\r |
| 364 | }\r |
| 365 | movie_data[0x18+30] = 0;\r |
| 366 | sprintf(noticeMsg, "MOVIE: %s", (char *) &movie_data[0x18]);\r |
| 367 | }\r |
| 368 | else\r |
| 369 | {\r |
| 370 | if(Pico.m.pal) {\r |
| 371 | strcpy(noticeMsg, "PAL SYSTEM / 50 FPS");\r |
| 372 | } else {\r |
| 373 | strcpy(noticeMsg, "NTSC SYSTEM / 60 FPS");\r |
| 374 | }\r |
| 375 | }\r |
| 376 | gettimeofday(¬iceMsgTime, 0);\r |
| 377 | \r |
| 378 | // load SRAM for this ROM\r |
| 379 | if(currentConfig.EmuOpt & 1)\r |
| 380 | emu_SaveLoadGame(1, 1);\r |
| 381 | \r |
| 382 | return 1;\r |
| 383 | }\r |
| 384 | \r |
| 385 | \r |
| 386 | static void emu_msg_cb(const char *msg);\r |
| 387 | static void emu_msg_tray_open(void);\r |
| 388 | \r |
| 389 | void emu_Init(void)\r |
| 390 | {\r |
| 391 | // make temp buffer for alt renderer\r |
| 392 | PicoDraw2FB = malloc((8+320)*(8+240+8));\r |
| 393 | if (!PicoDraw2FB)\r |
| 394 | {\r |
| 395 | printf("PicoDraw2FB == 0\n");\r |
| 396 | }\r |
| 397 | \r |
| 398 | // make dirs for saves, cfgs, etc.\r |
| 399 | mkdir("mds", 0777);\r |
| 400 | mkdir("srm", 0777);\r |
| 401 | mkdir("brm", 0777);\r |
| 402 | mkdir("cfg", 0777);\r |
| 403 | \r |
| 404 | PicoInit();\r |
| 405 | PicoMessage = emu_msg_cb;\r |
| 406 | PicoMCDopenTray = emu_msg_tray_open;\r |
| 407 | PicoMCDcloseTray = menu_loop_tray;\r |
| 408 | }\r |
| 409 | \r |
| 410 | \r |
| 411 | static void romfname_ext(char *dst, const char *prefix, const char *ext)\r |
| 412 | {\r |
| 413 | char *p;\r |
| 414 | int prefix_len = 0;\r |
| 415 | \r |
| 416 | // make save filename\r |
| 417 | for (p = romFileName+strlen(romFileName)-1; p >= romFileName && *p != '/'; p--); p++;\r |
| 418 | *dst = 0;\r |
| 419 | if (prefix) {\r |
| 420 | strcpy(dst, prefix);\r |
| 421 | prefix_len = strlen(prefix);\r |
| 422 | }\r |
| 423 | strncpy(dst + prefix_len, p, 511-prefix_len);\r |
| 424 | dst[511-8] = 0;\r |
| 425 | if (dst[strlen(dst)-4] == '.') dst[strlen(dst)-4] = 0;\r |
| 426 | if (ext) strcat(dst, ext);\r |
| 427 | }\r |
| 428 | \r |
| 429 | \r |
| 430 | static void find_combos(void)\r |
| 431 | {\r |
| 432 | int act, u;\r |
| 433 | \r |
| 434 | // find out which keys and actions are combos\r |
| 435 | combo_keys = combo_acts = 0;\r |
| 436 | for (act = 0; act < 32; act++)\r |
| 437 | {\r |
| 438 | int keyc = 0;\r |
| 439 | if (act == 16) continue; // player2 flag\r |
| 440 | for (u = 0; u < 32; u++)\r |
| 441 | {\r |
| 442 | if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;\r |
| 443 | }\r |
| 444 | if (keyc > 1)\r |
| 445 | {\r |
| 446 | // loop again and mark those keys and actions as combo\r |
| 447 | for (u = 0; u < 32; u++)\r |
| 448 | {\r |
| 449 | if (currentConfig.KeyBinds[u] & (1 << act)) {\r |
| 450 | combo_keys |= 1 << u;\r |
| 451 | combo_acts |= 1 << act;\r |
| 452 | }\r |
| 453 | }\r |
| 454 | }\r |
| 455 | }\r |
| 456 | // printf("combo keys/acts: %08x %08x\n", combo_keys, combo_acts);\r |
| 457 | }\r |
| 458 | \r |
| 459 | \r |
| 460 | void scaling_update(void)\r |
| 461 | {\r |
| 462 | PicoOpt &= ~0x4100;\r |
| 463 | switch (currentConfig.scaling) {\r |
| 464 | default: break; // off\r |
| 465 | case 1: // hw hor\r |
| 466 | case 2: PicoOpt |= 0x0100; break; // hw hor+vert\r |
| 467 | case 3: PicoOpt |= 0x4000; break; // sw hor\r |
| 468 | }\r |
| 469 | }\r |
| 470 | \r |
| 471 | \r |
| 472 | int emu_ReadConfig(int game, int no_defaults)\r |
| 473 | {\r |
| 474 | FILE *f;\r |
| 475 | char cfg[512], extbuf[16];\r |
| 476 | int bread = 0;\r |
| 477 | \r |
| 478 | if (!game)\r |
| 479 | {\r |
| 480 | if (!no_defaults)\r |
| 481 | {\r |
| 482 | // set default config\r |
| 483 | memset(¤tConfig, 0, sizeof(currentConfig));\r |
| 484 | currentConfig.lastRomFile[0] = 0;\r |
| 485 | currentConfig.EmuOpt = 0x1f | 0x600; // | confirm_save, cd_leds\r |
| 486 | currentConfig.PicoOpt = 0x0f | 0xe00; // | use_940, cd_pcm, cd_cdda\r |
| 487 | currentConfig.PsndRate = 22050; // 44100;\r |
| 488 | currentConfig.PicoRegion = 0; // auto\r |
| 489 | currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP\r |
| 490 | currentConfig.Frameskip = -1; // auto\r |
| 491 | currentConfig.CPUclock = 200;\r |
| 492 | currentConfig.volume = 50;\r |
| 493 | currentConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU\r |
| 494 | currentConfig.KeyBinds[ 4] = 1<<1;\r |
| 495 | currentConfig.KeyBinds[ 2] = 1<<2;\r |
| 496 | currentConfig.KeyBinds[ 6] = 1<<3;\r |
| 497 | currentConfig.KeyBinds[14] = 1<<4;\r |
| 498 | currentConfig.KeyBinds[13] = 1<<5;\r |
| 499 | currentConfig.KeyBinds[12] = 1<<6;\r |
| 500 | currentConfig.KeyBinds[ 8] = 1<<7;\r |
| 501 | currentConfig.KeyBinds[15] = 1<<26; // switch rend\r |
| 502 | currentConfig.KeyBinds[10] = 1<<27; // save state\r |
| 503 | currentConfig.KeyBinds[11] = 1<<28; // load state\r |
| 504 | currentConfig.KeyBinds[23] = 1<<29; // vol up\r |
| 505 | currentConfig.KeyBinds[22] = 1<<30; // vol down\r |
| 506 | currentConfig.gamma = 100;\r |
| 507 | currentConfig.PicoCDBuffers = 64;\r |
| 508 | currentConfig.scaling = 0;\r |
| 509 | }\r |
| 510 | strncpy(cfg, PicoConfigFile, 511);\r |
| 511 | if (config_slot != 0)\r |
| 512 | {\r |
| 513 | char *p = strrchr(cfg, '.');\r |
| 514 | if (p == NULL) p = cfg + strlen(cfg);\r |
| 515 | sprintf(extbuf, ".%i.pbcfg", config_slot);\r |
| 516 | strncpy(p, extbuf, 511 - (p - cfg));\r |
| 517 | }\r |
| 518 | cfg[511] = 0;\r |
| 519 | } else {\r |
| 520 | if (config_slot != 0)\r |
| 521 | sprintf(extbuf, ".%i.pbcfg", config_slot);\r |
| 522 | else strcpy(extbuf, ".pbcfg");\r |
| 523 | romfname_ext(cfg, "cfg/", extbuf);\r |
| 524 | f = fopen(cfg, "rb");\r |
| 525 | if (!f) romfname_ext(cfg, NULL, ".pbcfg");\r |
| 526 | else fclose(f);\r |
| 527 | }\r |
| 528 | \r |
| 529 | printf("emu_ReadConfig: %s ", cfg);\r |
| 530 | f = fopen(cfg, "rb");\r |
| 531 | if (f) {\r |
| 532 | bread = fread(¤tConfig, 1, sizeof(currentConfig), f);\r |
| 533 | fclose(f);\r |
| 534 | }\r |
| 535 | printf(bread > 0 ? "(ok)\n" : "(failed)\n");\r |
| 536 | \r |
| 537 | PicoOpt = currentConfig.PicoOpt;\r |
| 538 | PsndRate = currentConfig.PsndRate;\r |
| 539 | PicoRegionOverride = currentConfig.PicoRegion;\r |
| 540 | PicoAutoRgnOrder = currentConfig.PicoAutoRgnOrder;\r |
| 541 | PicoCDBuffers = currentConfig.PicoCDBuffers;\r |
| 542 | scaling_update();\r |
| 543 | // some sanity checks\r |
| 544 | if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;\r |
| 545 | if (currentConfig.gamma < 10 || currentConfig.gamma > 300) currentConfig.gamma = 100;\r |
| 546 | if (currentConfig.volume < 0 || currentConfig.volume > 99) currentConfig.volume = 50;\r |
| 547 | // if volume keys are unbound, bind them to volume control\r |
| 548 | if (!currentConfig.KeyBinds[23] && !currentConfig.KeyBinds[22]) {\r |
| 549 | currentConfig.KeyBinds[23] = 1<<29; // vol up\r |
| 550 | currentConfig.KeyBinds[22] = 1<<30; // vol down\r |
| 551 | }\r |
| 552 | \r |
| 553 | if (bread > 0) config_slot_current = config_slot;\r |
| 554 | return (bread > 0); // == sizeof(currentConfig));\r |
| 555 | }\r |
| 556 | \r |
| 557 | \r |
| 558 | int emu_WriteConfig(int game)\r |
| 559 | {\r |
| 560 | FILE *f;\r |
| 561 | char cfg[512], extbuf[16];\r |
| 562 | int bwrite = 0;\r |
| 563 | \r |
| 564 | if (!game)\r |
| 565 | {\r |
| 566 | strncpy(cfg, PicoConfigFile, 511);\r |
| 567 | if (config_slot != 0)\r |
| 568 | {\r |
| 569 | char *p = strrchr(cfg, '.');\r |
| 570 | if (p == NULL) p = cfg + strlen(cfg);\r |
| 571 | sprintf(extbuf, ".%i.pbcfg", config_slot);\r |
| 572 | strncpy(p, extbuf, 511 - (p - cfg));\r |
| 573 | }\r |
| 574 | cfg[511] = 0;\r |
| 575 | } else {\r |
| 576 | if (config_slot != 0)\r |
| 577 | sprintf(extbuf, ".%i.pbcfg", config_slot);\r |
| 578 | else strcpy(extbuf, ".pbcfg");\r |
| 579 | romfname_ext(cfg, "cfg/", extbuf);\r |
| 580 | }\r |
| 581 | \r |
| 582 | printf("emu_WriteConfig: %s ", cfg);\r |
| 583 | f = fopen(cfg, "wb");\r |
| 584 | if (f) {\r |
| 585 | currentConfig.PicoOpt = PicoOpt;\r |
| 586 | currentConfig.PsndRate = PsndRate;\r |
| 587 | currentConfig.PicoRegion = PicoRegionOverride;\r |
| 588 | currentConfig.PicoAutoRgnOrder = PicoAutoRgnOrder;\r |
| 589 | currentConfig.PicoCDBuffers = PicoCDBuffers;\r |
| 590 | bwrite = fwrite(¤tConfig, 1, sizeof(currentConfig), f);\r |
| 591 | fflush(f);\r |
| 592 | fclose(f);\r |
| 593 | #ifndef NO_SYNC\r |
| 594 | sync();\r |
| 595 | #endif\r |
| 596 | }\r |
| 597 | printf((bwrite == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n");\r |
| 598 | \r |
| 599 | if (bwrite == sizeof(currentConfig)) config_slot_current = config_slot;\r |
| 600 | return (bwrite == sizeof(currentConfig));\r |
| 601 | }\r |
| 602 | \r |
| 603 | \r |
| 604 | void emu_Deinit(void)\r |
| 605 | {\r |
| 606 | // save SRAM\r |
| 607 | if((currentConfig.EmuOpt & 1) && SRam.changed) {\r |
| 608 | emu_SaveLoadGame(0, 1);\r |
| 609 | SRam.changed = 0;\r |
| 610 | }\r |
| 611 | \r |
| 612 | if (!(currentConfig.EmuOpt & 0x20)) {\r |
| 613 | FILE *f = fopen(PicoConfigFile, "r+b");\r |
| 614 | if (!f) emu_WriteConfig(0);\r |
| 615 | else {\r |
| 616 | // if we already have config, reload it, except last ROM\r |
| 617 | fseek(f, sizeof(currentConfig.lastRomFile), SEEK_SET);\r |
| 618 | fread(¤tConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f);\r |
| 619 | fseek(f, 0, SEEK_SET);\r |
| 620 | fwrite(¤tConfig, 1, sizeof(currentConfig), f);\r |
| 621 | fflush(f);\r |
| 622 | fclose(f);\r |
| 623 | #ifndef NO_SYNC\r |
| 624 | sync();\r |
| 625 | #endif\r |
| 626 | }\r |
| 627 | }\r |
| 628 | \r |
| 629 | free(PicoDraw2FB);\r |
| 630 | \r |
| 631 | PicoExit();\r |
| 632 | \r |
| 633 | // restore gamma\r |
| 634 | if (gp2x_old_gamma != 100)\r |
| 635 | set_gamma(100, 0);\r |
| 636 | }\r |
| 637 | \r |
| 638 | static void text_out8_builtin(int x, int y, const char *text)\r |
| 639 | {\r |
| 640 | int i,l,len=strlen(text);\r |
| 641 | unsigned char *screen = (unsigned char *)gp2x_screen + x + y*320;\r |
| 642 | \r |
| 643 | /* always using built-in font */\r |
| 644 | for (i = 0; i < len; i++)\r |
| 645 | {\r |
| 646 | for (l=0;l<8;l++)\r |
| 647 | {\r |
| 648 | unsigned char fd = fontdata8x8[((text[i])*8)+l];\r |
| 649 | if (fd&0x80) screen[l*320+0]=0xf0;\r |
| 650 | if (fd&0x40) screen[l*320+1]=0xf0;\r |
| 651 | if (fd&0x20) screen[l*320+2]=0xf0;\r |
| 652 | if (fd&0x10) screen[l*320+3]=0xf0;\r |
| 653 | if (fd&0x08) screen[l*320+4]=0xf0;\r |
| 654 | if (fd&0x04) screen[l*320+5]=0xf0;\r |
| 655 | if (fd&0x02) screen[l*320+6]=0xf0;\r |
| 656 | if (fd&0x01) screen[l*320+7]=0xf0;\r |
| 657 | }\r |
| 658 | screen += 8;\r |
| 659 | }\r |
| 660 | }\r |
| 661 | \r |
| 662 | static void text_out16_builtin(int x, int y, const char *text)\r |
| 663 | {\r |
| 664 | int i,l,len=strlen(text);\r |
| 665 | unsigned short *screen = (unsigned short *)gp2x_screen + x + y*320;\r |
| 666 | \r |
| 667 | for (i = 0; i < len; i++)\r |
| 668 | {\r |
| 669 | for (l=0;l<8;l++)\r |
| 670 | {\r |
| 671 | unsigned char fd = fontdata8x8[((text[i])*8)+l];\r |
| 672 | if(fd&0x80) screen[l*320+0]=0xffff;\r |
| 673 | if(fd&0x40) screen[l*320+1]=0xffff;\r |
| 674 | if(fd&0x20) screen[l*320+2]=0xffff;\r |
| 675 | if(fd&0x10) screen[l*320+3]=0xffff;\r |
| 676 | if(fd&0x08) screen[l*320+4]=0xffff;\r |
| 677 | if(fd&0x04) screen[l*320+5]=0xffff;\r |
| 678 | if(fd&0x02) screen[l*320+6]=0xffff;\r |
| 679 | if(fd&0x01) screen[l*320+7]=0xffff;\r |
| 680 | }\r |
| 681 | screen += 8;\r |
| 682 | }\r |
| 683 | }\r |
| 684 | \r |
| 685 | \r |
| 686 | void osd_text(int x, int y, const char *text)\r |
| 687 | {\r |
| 688 | int len = strlen(text)*8;\r |
| 689 | \r |
| 690 | if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r |
| 691 | int *p, i, h;\r |
| 692 | x &= ~3; // align x\r |
| 693 | len = (len+3) >> 2;\r |
| 694 | for (h = 0; h < 8; h++) {\r |
| 695 | p = (int *) ((unsigned char *) gp2x_screen+x+320*(y+h));\r |
| 696 | for (i = len; i; i--, p++) *p = 0xe0e0e0e0;\r |
| 697 | }\r |
| 698 | text_out8_builtin(x, y, text);\r |
| 699 | } else {\r |
| 700 | int *p, i, h;\r |
| 701 | x &= ~1; // align x\r |
| 702 | len = (len+1) >> 1;\r |
| 703 | for (h = 0; h < 8; h++) {\r |
| 704 | p = (int *) ((unsigned short *) gp2x_screen+x+320*(y+h));\r |
| 705 | for (i = len; i; i--, p++) *p = (*p>>2)&0x39e7;\r |
| 706 | }\r |
| 707 | text_out16_builtin(x, y, text);\r |
| 708 | }\r |
| 709 | }\r |
| 710 | \r |
| 711 | static void cd_leds(void)\r |
| 712 | {\r |
| 713 | // mmu problems?\r |
| 714 | // static\r |
| 715 | int old_reg;\r |
| 716 | // if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change\r |
| 717 | old_reg = Pico_mcd->s68k_regs[0];\r |
| 718 | \r |
| 719 | if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r |
| 720 | // 8-bit modes\r |
| 721 | unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;\r |
| 722 | unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;\r |
| 723 | *(unsigned int *)((char *)gp2x_screen + 320*2+ 4) =\r |
| 724 | *(unsigned int *)((char *)gp2x_screen + 320*3+ 4) =\r |
| 725 | *(unsigned int *)((char *)gp2x_screen + 320*4+ 4) = col_g;\r |
| 726 | *(unsigned int *)((char *)gp2x_screen + 320*2+12) =\r |
| 727 | *(unsigned int *)((char *)gp2x_screen + 320*3+12) =\r |
| 728 | *(unsigned int *)((char *)gp2x_screen + 320*4+12) = col_r;\r |
| 729 | } else {\r |
| 730 | // 16-bit modes\r |
| 731 | unsigned int *p = (unsigned int *)((short *)gp2x_screen + 320*2+4);\r |
| 732 | unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;\r |
| 733 | unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;\r |
| 734 | *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;\r |
| 735 | *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;\r |
| 736 | *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;\r |
| 737 | }\r |
| 738 | }\r |
| 739 | \r |
| 740 | static int EmuScan16(unsigned int num, void *sdata)\r |
| 741 | {\r |
| 742 | if (!(Pico.video.reg[1]&8)) num += 8;\r |
| 743 | DrawLineDest = (unsigned short *) gp2x_screen + 320*(num+1);\r |
| 744 | \r |
| 745 | return 0;\r |
| 746 | }\r |
| 747 | \r |
| 748 | static int EmuScan8(unsigned int num, void *sdata)\r |
| 749 | {\r |
| 750 | if (!(Pico.video.reg[1]&8)) num += 8;\r |
| 751 | DrawLineDest = (unsigned char *) gp2x_screen + 320*(num+1);\r |
| 752 | \r |
| 753 | return 0;\r |
| 754 | }\r |
| 755 | \r |
| 756 | int localPal[0x100];\r |
| 757 | static void (*vidCpyM2)(void *dest, void *src) = NULL;\r |
| 758 | \r |
| 759 | static void blit(const char *fps, const char *notice)\r |
| 760 | {\r |
| 761 | int emu_opt = currentConfig.EmuOpt;\r |
| 762 | \r |
| 763 | if (PicoOpt&0x10) {\r |
| 764 | // 8bit fast renderer\r |
| 765 | if (Pico.m.dirtyPal) {\r |
| 766 | Pico.m.dirtyPal = 0;\r |
| 767 | vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r |
| 768 | // feed new palette to our device\r |
| 769 | gp2x_video_setpalette(localPal, 0x40);\r |
| 770 | }\r |
| 771 | vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);\r |
| 772 | } else if (!(emu_opt&0x80)) {\r |
| 773 | // 8bit accurate renderer\r |
| 774 | if (Pico.m.dirtyPal) {\r |
| 775 | Pico.m.dirtyPal = 0;\r |
| 776 | if(Pico.video.reg[0xC]&8) { // shadow/hilight mode\r |
| 777 | vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r |
| 778 | vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);\r |
| 779 | vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);\r |
| 780 | blockcpy(localPal+0xc0, localPal+0x40, 0x40*4);\r |
| 781 | localPal[0xc0] = 0x0000c000;\r |
| 782 | localPal[0xd0] = 0x00c00000;\r |
| 783 | localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r |
| 784 | localPal[0xf0] = 0x00ffffff;\r |
| 785 | gp2x_video_setpalette(localPal, 0x100);\r |
| 786 | } else if (rendstatus & 0x20) { // mid-frame palette changes\r |
| 787 | vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r |
| 788 | vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);\r |
| 789 | vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);\r |
| 790 | gp2x_video_setpalette(localPal, 0xc0);\r |
| 791 | } else {\r |
| 792 | vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r |
| 793 | gp2x_video_setpalette(localPal, 0x40);\r |
| 794 | }\r |
| 795 | }\r |
| 796 | }\r |
| 797 | \r |
| 798 | if (notice || (emu_opt & 2)) {\r |
| 799 | int h = 232;\r |
| 800 | if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8)) h -= 8;\r |
| 801 | if (notice) osd_text(4, h, notice);\r |
| 802 | if (emu_opt & 2)\r |
| 803 | osd_text(osd_fps_x, h, fps);\r |
| 804 | }\r |
| 805 | if ((emu_opt & 0x400) && (PicoMCD & 1))\r |
| 806 | cd_leds();\r |
| 807 | \r |
| 808 | //gp2x_video_wait_vsync();\r |
| 809 | gp2x_video_flip();\r |
| 810 | \r |
| 811 | if (!(PicoOpt&0x10)) {\r |
| 812 | if (!(Pico.video.reg[1]&8)) {\r |
| 813 | if (currentConfig.EmuOpt&0x80) {\r |
| 814 | DrawLineDest = (unsigned short *) gp2x_screen + 320*8;\r |
| 815 | } else {\r |
| 816 | DrawLineDest = (unsigned char *) gp2x_screen + 320*8;\r |
| 817 | }\r |
| 818 | } else {\r |
| 819 | DrawLineDest = gp2x_screen;\r |
| 820 | }\r |
| 821 | }\r |
| 822 | }\r |
| 823 | \r |
| 824 | \r |
| 825 | // clears whole screen or just the notice area (in all buffers)\r |
| 826 | static void clearArea(int full)\r |
| 827 | {\r |
| 828 | if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r |
| 829 | // 8-bit renderers\r |
| 830 | if (full) gp2x_memset_all_buffers(0, 0xe0, 320*240);\r |
| 831 | else gp2x_memset_all_buffers(320*232, 0xe0, 320*8);\r |
| 832 | } else {\r |
| 833 | // 16bit accurate renderer\r |
| 834 | if (full) gp2x_memset_all_buffers(0, 0, 320*240*2);\r |
| 835 | else gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);\r |
| 836 | }\r |
| 837 | }\r |
| 838 | \r |
| 839 | \r |
| 840 | static void vidResetMode(void)\r |
| 841 | {\r |
| 842 | if (PicoOpt&0x10) {\r |
| 843 | gp2x_video_changemode(8);\r |
| 844 | } else if (currentConfig.EmuOpt&0x80) {\r |
| 845 | gp2x_video_changemode(16);\r |
| 846 | PicoDrawSetColorFormat(1);\r |
| 847 | PicoScan = EmuScan16;\r |
| 848 | PicoScan(0, 0);\r |
| 849 | } else {\r |
| 850 | gp2x_video_changemode(8);\r |
| 851 | PicoDrawSetColorFormat(2);\r |
| 852 | PicoScan = EmuScan8;\r |
| 853 | PicoScan(0, 0);\r |
| 854 | }\r |
| 855 | if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r |
| 856 | // setup pal for 8-bit modes\r |
| 857 | localPal[0xc0] = 0x0000c000; // MCD LEDs\r |
| 858 | localPal[0xd0] = 0x00c00000;\r |
| 859 | localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r |
| 860 | localPal[0xf0] = 0x00ffffff;\r |
| 861 | gp2x_video_setpalette(localPal, 0x100);\r |
| 862 | gp2x_memset_all_buffers(0, 0xe0, 320*240);\r |
| 863 | gp2x_video_flip();\r |
| 864 | }\r |
| 865 | Pico.m.dirtyPal = 1;\r |
| 866 | // reset scaling\r |
| 867 | if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8))\r |
| 868 | gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);\r |
| 869 | else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);\r |
| 870 | }\r |
| 871 | \r |
| 872 | \r |
| 873 | static void emu_msg_cb(const char *msg)\r |
| 874 | {\r |
| 875 | if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r |
| 876 | // 8-bit renderers\r |
| 877 | gp2x_memset_all_buffers(320*232, 0xe0, 320*8);\r |
| 878 | osd_text(4, 232, msg);\r |
| 879 | gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232, 320*232, 320*8);\r |
| 880 | } else {\r |
| 881 | // 16bit accurate renderer\r |
| 882 | gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);\r |
| 883 | osd_text(4, 232, msg);\r |
| 884 | gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232*2, 320*232*2, 320*8*2);\r |
| 885 | }\r |
| 886 | gettimeofday(¬iceMsgTime, 0);\r |
| 887 | noticeMsgTime.tv_sec -= 2;\r |
| 888 | \r |
| 889 | /* assumption: emu_msg_cb gets called only when something slow is about to happen */\r |
| 890 | reset_timing = 1;\r |
| 891 | }\r |
| 892 | \r |
| 893 | static void emu_state_cb(const char *str)\r |
| 894 | {\r |
| 895 | clearArea(0);\r |
| 896 | blit("", str);\r |
| 897 | }\r |
| 898 | \r |
| 899 | static void emu_msg_tray_open(void)\r |
| 900 | {\r |
| 901 | strcpy(noticeMsg, "CD tray opened");\r |
| 902 | gettimeofday(¬iceMsgTime, 0);\r |
| 903 | }\r |
| 904 | \r |
| 905 | static void RunEvents(unsigned int which)\r |
| 906 | {\r |
| 907 | if(which & 0x1800) { // save or load (but not both)\r |
| 908 | int do_it = 1;\r |
| 909 | if ( emu_check_save_file(state_slot) &&\r |
| 910 | (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load\r |
| 911 | (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) { // save\r |
| 912 | unsigned long keys;\r |
| 913 | blit("", (which & 0x1000) ? "LOAD STATE? (Y=yes, X=no)" : "OVERWRITE SAVE? (Y=yes, X=no)");\r |
| 914 | while( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )\r |
| 915 | usleep(50*1024);\r |
| 916 | if (keys & GP2X_X) do_it = 0;\r |
| 917 | clearArea(0);\r |
| 918 | }\r |
| 919 | if (do_it) {\r |
| 920 | osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");\r |
| 921 | PicoStateProgressCB = emu_state_cb;\r |
| 922 | gp2x_memcpy_all_buffers(gp2x_screen, 0, 320*240*2);\r |
| 923 | emu_SaveLoadGame((which & 0x1000) >> 12, 0);\r |
| 924 | PicoStateProgressCB = NULL;\r |
| 925 | }\r |
| 926 | \r |
| 927 | reset_timing = 1;\r |
| 928 | }\r |
| 929 | if(which & 0x0400) { // switch renderer\r |
| 930 | if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }\r |
| 931 | else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;\r |
| 932 | else currentConfig.EmuOpt &= ~0x80;\r |
| 933 | \r |
| 934 | vidResetMode();\r |
| 935 | \r |
| 936 | if (PicoOpt&0x10) {\r |
| 937 | strcpy(noticeMsg, " 8bit fast renderer");\r |
| 938 | } else if (currentConfig.EmuOpt&0x80) {\r |
| 939 | strcpy(noticeMsg, "16bit accurate renderer");\r |
| 940 | } else {\r |
| 941 | strcpy(noticeMsg, " 8bit accurate renderer");\r |
| 942 | }\r |
| 943 | \r |
| 944 | gettimeofday(¬iceMsgTime, 0);\r |
| 945 | }\r |
| 946 | if(which & 0x0300) {\r |
| 947 | if(which&0x0200) {\r |
| 948 | state_slot -= 1;\r |
| 949 | if(state_slot < 0) state_slot = 9;\r |
| 950 | } else {\r |
| 951 | state_slot += 1;\r |
| 952 | if(state_slot > 9) state_slot = 0;\r |
| 953 | }\r |
| 954 | sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_check_save_file(state_slot) ? "USED" : "FREE");\r |
| 955 | gettimeofday(¬iceMsgTime, 0);\r |
| 956 | }\r |
| 957 | if(which & 0x0080) {\r |
| 958 | engineState = PGS_Menu;\r |
| 959 | }\r |
| 960 | }\r |
| 961 | \r |
| 962 | \r |
| 963 | static void updateMovie(void)\r |
| 964 | {\r |
| 965 | int offs = Pico.m.frame_count*3 + 0x40;\r |
| 966 | if (offs+3 > movie_size) {\r |
| 967 | free(movie_data);\r |
| 968 | movie_data = 0;\r |
| 969 | strcpy(noticeMsg, "END OF MOVIE.");\r |
| 970 | printf("END OF MOVIE.\n");\r |
| 971 | gettimeofday(¬iceMsgTime, 0);\r |
| 972 | } else {\r |
| 973 | // MXYZ SACB RLDU\r |
| 974 | PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU\r |
| 975 | if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // A\r |
| 976 | if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // B\r |
| 977 | if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // A\r |
| 978 | PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU\r |
| 979 | if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // A\r |
| 980 | if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // B\r |
| 981 | if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // A\r |
| 982 | PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX\r |
| 983 | if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X\r |
| 984 | if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z\r |
| 985 | PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX\r |
| 986 | if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X\r |
| 987 | if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z\r |
| 988 | }\r |
| 989 | }\r |
| 990 | \r |
| 991 | \r |
| 992 | static void updateKeys(void)\r |
| 993 | {\r |
| 994 | unsigned long keys, allActions[2] = { 0, 0 }, events;\r |
| 995 | static unsigned long prevEvents = 0;\r |
| 996 | int joy, i;\r |
| 997 | \r |
| 998 | keys = gp2x_joystick_read(0);\r |
| 999 | if (keys & GP2X_SELECT) {\r |
| 1000 | engineState = select_exits ? PGS_Quit : PGS_Menu;\r |
| 1001 | // wait until select is released, so menu would not resume game\r |
| 1002 | while (gp2x_joystick_read(1) & GP2X_SELECT) usleep(50*1000);\r |
| 1003 | }\r |
| 1004 | \r |
| 1005 | keys &= CONFIGURABLE_KEYS;\r |
| 1006 | \r |
| 1007 | for (i = 0; i < 32; i++)\r |
| 1008 | {\r |
| 1009 | if (keys & (1 << i)) {\r |
| 1010 | int pl, acts = currentConfig.KeyBinds[i];\r |
| 1011 | if (!acts) continue;\r |
| 1012 | pl = (acts >> 16) & 1;\r |
| 1013 | if (combo_keys & (1 << i)) {\r |
| 1014 | int u = i+1, acts_c = acts & combo_acts;\r |
| 1015 | // let's try to find the other one\r |
| 1016 | if (acts_c)\r |
| 1017 | for (; u < 32; u++)\r |
| 1018 | if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) {\r |
| 1019 | allActions[pl] |= acts_c;\r |
| 1020 | keys &= ~((1 << i) | (1 << u));\r |
| 1021 | break;\r |
| 1022 | }\r |
| 1023 | // add non-combo actions if combo ones were not found\r |
| 1024 | if (!acts_c || u == 32)\r |
| 1025 | allActions[pl] |= acts & ~combo_acts;\r |
| 1026 | } else {\r |
| 1027 | allActions[pl] |= acts;\r |
| 1028 | }\r |
| 1029 | }\r |
| 1030 | }\r |
| 1031 | \r |
| 1032 | // add joy inputs\r |
| 1033 | if (num_of_joys > 0)\r |
| 1034 | {\r |
| 1035 | gp2x_usbjoy_update();\r |
| 1036 | for (joy = 0; joy < num_of_joys; joy++) {\r |
| 1037 | int keys = gp2x_usbjoy_check2(joy);\r |
| 1038 | for (i = 0; i < 32; i++) {\r |
| 1039 | if (keys & (1 << i)) {\r |
| 1040 | int acts = currentConfig.JoyBinds[joy][i];\r |
| 1041 | int pl = (acts >> 16) & 1;\r |
| 1042 | allActions[pl] |= acts;\r |
| 1043 | }\r |
| 1044 | }\r |
| 1045 | }\r |
| 1046 | }\r |
| 1047 | \r |
| 1048 | PicoPad[0] = (unsigned short) allActions[0];\r |
| 1049 | PicoPad[1] = (unsigned short) allActions[1];\r |
| 1050 | \r |
| 1051 | events = (allActions[0] | allActions[1]) >> 16;\r |
| 1052 | \r |
| 1053 | // volume is treated in special way and triggered every frame\r |
| 1054 | if(events & 0x6000) {\r |
| 1055 | int vol = currentConfig.volume;\r |
| 1056 | if (events & 0x2000) {\r |
| 1057 | if (vol < 99) vol++;\r |
| 1058 | } else {\r |
| 1059 | if (vol > 0) vol--;\r |
| 1060 | }\r |
| 1061 | gp2x_sound_volume(vol, vol);\r |
| 1062 | sprintf(noticeMsg, "VOL: %02i", vol);\r |
| 1063 | gettimeofday(¬iceMsgTime, 0);\r |
| 1064 | currentConfig.volume = vol;\r |
| 1065 | }\r |
| 1066 | \r |
| 1067 | events &= ~prevEvents;\r |
| 1068 | if (events) RunEvents(events);\r |
| 1069 | if (movie_data) updateMovie();\r |
| 1070 | \r |
| 1071 | prevEvents = (allActions[0] | allActions[1]) >> 16;\r |
| 1072 | }\r |
| 1073 | \r |
| 1074 | \r |
| 1075 | static void updateSound(int len)\r |
| 1076 | {\r |
| 1077 | if (PicoOpt&8) len<<=1;\r |
| 1078 | \r |
| 1079 | /* avoid writing audio when lagging behind to prevent audio lag */\r |
| 1080 | if (PicoSkipFrame != 2)\r |
| 1081 | gp2x_sound_write(PsndOut, len<<1);\r |
| 1082 | }\r |
| 1083 | \r |
| 1084 | \r |
| 1085 | static void SkipFrame(int do_audio)\r |
| 1086 | {\r |
| 1087 | PicoSkipFrame=do_audio ? 1 : 2;\r |
| 1088 | PicoFrame();\r |
| 1089 | PicoSkipFrame=0;\r |
| 1090 | }\r |
| 1091 | \r |
| 1092 | \r |
| 1093 | void emu_forced_frame(void)\r |
| 1094 | {\r |
| 1095 | int po_old = PicoOpt;\r |
| 1096 | int eo_old = currentConfig.EmuOpt;\r |
| 1097 | \r |
| 1098 | PicoOpt &= ~0x0010;\r |
| 1099 | PicoOpt |= 0x4080; // soft_scale | acc_sprites\r |
| 1100 | currentConfig.EmuOpt |= 0x80;\r |
| 1101 | \r |
| 1102 | //vidResetMode();\r |
| 1103 | PicoDrawSetColorFormat(1);\r |
| 1104 | PicoScan = EmuScan16;\r |
| 1105 | PicoScan(0, 0);\r |
| 1106 | Pico.m.dirtyPal = 1;\r |
| 1107 | PicoFrameDrawOnly();\r |
| 1108 | \r |
| 1109 | /*\r |
| 1110 | if (!(Pico.video.reg[12]&1)) {\r |
| 1111 | vidCpyM2 = vidCpyM2_32col;\r |
| 1112 | clearArea(1);\r |
| 1113 | } else vidCpyM2 = vidCpyM2_40col;\r |
| 1114 | \r |
| 1115 | vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);\r |
| 1116 | vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r |
| 1117 | gp2x_video_setpalette(localPal, 0x40);\r |
| 1118 | */\r |
| 1119 | PicoOpt = po_old;\r |
| 1120 | currentConfig.EmuOpt = eo_old;\r |
| 1121 | }\r |
| 1122 | \r |
| 1123 | static void simpleWait(int thissec, int lim_time)\r |
| 1124 | {\r |
| 1125 | struct timeval tval;\r |
| 1126 | \r |
| 1127 | spend_cycles(1024);\r |
| 1128 | gettimeofday(&tval, 0);\r |
| 1129 | if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r |
| 1130 | \r |
| 1131 | while(tval.tv_usec < lim_time)\r |
| 1132 | {\r |
| 1133 | spend_cycles(1024);\r |
| 1134 | gettimeofday(&tval, 0);\r |
| 1135 | if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r |
| 1136 | }\r |
| 1137 | }\r |
| 1138 | \r |
| 1139 | \r |
| 1140 | void emu_Loop(void)\r |
| 1141 | {\r |
| 1142 | static int gp2x_old_clock = 200;\r |
| 1143 | static int PsndRate_old = 0, PicoOpt_old = 0, EmuOpt_old = 0, PsndLen_real = 0, pal_old = 0;\r |
| 1144 | char fpsbuff[24]; // fps count c string\r |
| 1145 | struct timeval tval; // timing\r |
| 1146 | int thissec = 0, frames_done = 0, frames_shown = 0, oldmodes = 0;\r |
| 1147 | int target_fps, target_frametime, lim_time, vsync_offset, i;\r |
| 1148 | char *notice = 0;\r |
| 1149 | \r |
| 1150 | printf("entered emu_Loop()\n");\r |
| 1151 | \r |
| 1152 | if (gp2x_old_clock != currentConfig.CPUclock) {\r |
| 1153 | printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);\r |
| 1154 | set_FCLK(currentConfig.CPUclock);\r |
| 1155 | gp2x_old_clock = currentConfig.CPUclock;\r |
| 1156 | printf(" done\n");\r |
| 1157 | }\r |
| 1158 | \r |
| 1159 | if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {\r |
| 1160 | set_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r |
| 1161 | gp2x_old_gamma = currentConfig.gamma;\r |
| 1162 | printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r |
| 1163 | }\r |
| 1164 | \r |
| 1165 | if ((EmuOpt_old&0x2000) != (currentConfig.EmuOpt&0x2000)) {\r |
| 1166 | if (currentConfig.EmuOpt&0x2000)\r |
| 1167 | set_LCD_custom_rate(Pico.m.pal ? LCDR_100 : LCDR_120);\r |
| 1168 | else unset_LCD_custom_rate();\r |
| 1169 | }\r |
| 1170 | \r |
| 1171 | EmuOpt_old = currentConfig.EmuOpt;\r |
| 1172 | fpsbuff[0] = 0;\r |
| 1173 | \r |
| 1174 | // make sure we are in correct mode\r |
| 1175 | vidResetMode();\r |
| 1176 | Pico.m.dirtyPal = 1;\r |
| 1177 | oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;\r |
| 1178 | find_combos();\r |
| 1179 | \r |
| 1180 | // pal/ntsc might have changed, reset related stuff\r |
| 1181 | target_fps = Pico.m.pal ? 50 : 60;\r |
| 1182 | target_frametime = 1000000/target_fps;\r |
| 1183 | reset_timing = 1;\r |
| 1184 | \r |
| 1185 | // prepare sound stuff\r |
| 1186 | if(currentConfig.EmuOpt & 4) {\r |
| 1187 | int snd_excess_add;\r |
| 1188 | if(PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old || crashed_940) {\r |
| 1189 | /* if 940 is turned off, we need it to be put back to sleep */\r |
| 1190 | if (!(PicoOpt&0x200) && ((PicoOpt^PicoOpt_old)&0x200)) {\r |
| 1191 | Reset940(1, 2);\r |
| 1192 | Pause940(1);\r |
| 1193 | }\r |
| 1194 | sound_rerate(1);\r |
| 1195 | }\r |
| 1196 | //excess_samples = PsndRate - PsndLen*target_fps;\r |
| 1197 | snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r |
| 1198 | printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n", PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);\r |
| 1199 | gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);\r |
| 1200 | gp2x_sound_volume(currentConfig.volume, currentConfig.volume);\r |
| 1201 | PicoWriteSound = updateSound;\r |
| 1202 | memset(sndBuffer, 0, sizeof(sndBuffer));\r |
| 1203 | PsndOut = sndBuffer;\r |
| 1204 | PsndRate_old = PsndRate;\r |
| 1205 | PsndLen_real = PsndLen;\r |
| 1206 | PicoOpt_old = PicoOpt;\r |
| 1207 | pal_old = Pico.m.pal;\r |
| 1208 | } else {\r |
| 1209 | PsndOut = 0;\r |
| 1210 | }\r |
| 1211 | \r |
| 1212 | // prepare CD buffer\r |
| 1213 | if (PicoMCD & 1) PicoCDBufferInit();\r |
| 1214 | \r |
| 1215 | // calc vsync offset to sync timing code with vsync\r |
| 1216 | if (currentConfig.EmuOpt&0x2000) {\r |
| 1217 | gettimeofday(&tval, 0);\r |
| 1218 | gp2x_video_wait_vsync();\r |
| 1219 | gettimeofday(&tval, 0);\r |
| 1220 | vsync_offset = tval.tv_usec;\r |
| 1221 | while (vsync_offset >= target_frametime)\r |
| 1222 | vsync_offset -= target_frametime;\r |
| 1223 | if (!vsync_offset) vsync_offset++;\r |
| 1224 | printf("vsync_offset: %i\n", vsync_offset);\r |
| 1225 | } else\r |
| 1226 | vsync_offset = 0;\r |
| 1227 | \r |
| 1228 | // loop?\r |
| 1229 | while (engineState == PGS_Running)\r |
| 1230 | {\r |
| 1231 | int modes;\r |
| 1232 | \r |
| 1233 | gettimeofday(&tval, 0);\r |
| 1234 | if(reset_timing) {\r |
| 1235 | reset_timing = 0;\r |
| 1236 | thissec = tval.tv_sec;\r |
| 1237 | frames_shown = frames_done = tval.tv_usec/target_frametime;\r |
| 1238 | }\r |
| 1239 | \r |
| 1240 | // show notice message?\r |
| 1241 | if(noticeMsgTime.tv_sec) {\r |
| 1242 | static int noticeMsgSum;\r |
| 1243 | if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec\r |
| 1244 | noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;\r |
| 1245 | clearArea(0);\r |
| 1246 | notice = 0;\r |
| 1247 | } else {\r |
| 1248 | int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];\r |
| 1249 | if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }\r |
| 1250 | notice = noticeMsg;\r |
| 1251 | }\r |
| 1252 | }\r |
| 1253 | \r |
| 1254 | // check for mode changes\r |
| 1255 | modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);\r |
| 1256 | if (modes != oldmodes) {\r |
| 1257 | int scalex = 320;\r |
| 1258 | osd_fps_x = OSD_FPS_X;\r |
| 1259 | if (modes & 4) {\r |
| 1260 | vidCpyM2 = vidCpyM2_40col;\r |
| 1261 | } else {\r |
| 1262 | if (PicoOpt & 0x100) {\r |
| 1263 | vidCpyM2 = vidCpyM2_32col_nobord;\r |
| 1264 | scalex = 256;\r |
| 1265 | osd_fps_x = OSD_FPS_X - 64;\r |
| 1266 | } else {\r |
| 1267 | vidCpyM2 = vidCpyM2_32col;\r |
| 1268 | }\r |
| 1269 | }\r |
| 1270 | if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode\r |
| 1271 | gp2x_video_RGB_setscaling(8, scalex, 224);\r |
| 1272 | else gp2x_video_RGB_setscaling(0, scalex, 240);\r |
| 1273 | oldmodes = modes;\r |
| 1274 | clearArea(1);\r |
| 1275 | }\r |
| 1276 | \r |
| 1277 | // second changed?\r |
| 1278 | if(thissec != tval.tv_sec) {\r |
| 1279 | #ifdef BENCHMARK\r |
| 1280 | static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];\r |
| 1281 | if(++bench == 10) {\r |
| 1282 | bench = 0;\r |
| 1283 | bench_fps_s = bench_fps;\r |
| 1284 | bf[bfp++ & 3] = bench_fps;\r |
| 1285 | bench_fps = 0;\r |
| 1286 | }\r |
| 1287 | bench_fps += frames_shown;\r |
| 1288 | sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);\r |
| 1289 | #else\r |
| 1290 | if(currentConfig.EmuOpt & 2)\r |
| 1291 | sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);\r |
| 1292 | #endif\r |
| 1293 | thissec = tval.tv_sec;\r |
| 1294 | \r |
| 1295 | if(PsndOut == 0 && currentConfig.Frameskip >= 0) {\r |
| 1296 | frames_done = frames_shown = 0;\r |
| 1297 | } else {\r |
| 1298 | // it is quite common for this implementation to leave 1 fame unfinished\r |
| 1299 | // when second changes, but we don't want buffer to starve.\r |
| 1300 | if(PsndOut && frames_done < target_fps && frames_done > target_fps-5) {\r |
| 1301 | updateKeys();\r |
| 1302 | SkipFrame(1); frames_done++;\r |
| 1303 | }\r |
| 1304 | \r |
| 1305 | frames_done -= target_fps; if (frames_done < 0) frames_done = 0;\r |
| 1306 | frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;\r |
| 1307 | if (frames_shown > frames_done) frames_shown = frames_done;\r |
| 1308 | }\r |
| 1309 | }\r |
| 1310 | \r |
| 1311 | lim_time = (frames_done+1) * target_frametime + vsync_offset;\r |
| 1312 | if(currentConfig.Frameskip >= 0) { // frameskip enabled\r |
| 1313 | for(i = 0; i < currentConfig.Frameskip; i++) {\r |
| 1314 | updateKeys();\r |
| 1315 | SkipFrame(1); frames_done++;\r |
| 1316 | if (PsndOut) { // do framelimitting if sound is enabled\r |
| 1317 | gettimeofday(&tval, 0);\r |
| 1318 | if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r |
| 1319 | if(tval.tv_usec < lim_time) { // we are too fast\r |
| 1320 | simpleWait(thissec, lim_time);\r |
| 1321 | }\r |
| 1322 | }\r |
| 1323 | lim_time += target_frametime;\r |
| 1324 | }\r |
| 1325 | } else if(tval.tv_usec > lim_time) { // auto frameskip\r |
| 1326 | // no time left for this frame - skip\r |
| 1327 | if (tval.tv_usec - lim_time >= 0x300000) {\r |
| 1328 | /* something caused a slowdown for us (disk access? cache flush?)\r |
| 1329 | * try to recover by resetting timing... */\r |
| 1330 | reset_timing = 1;\r |
| 1331 | continue;\r |
| 1332 | }\r |
| 1333 | updateKeys();\r |
| 1334 | SkipFrame(tval.tv_usec < lim_time+target_frametime*2); frames_done++;\r |
| 1335 | continue;\r |
| 1336 | }\r |
| 1337 | \r |
| 1338 | updateKeys();\r |
| 1339 | PicoFrame();\r |
| 1340 | \r |
| 1341 | #if 0\r |
| 1342 | if (Pico.m.frame_count == 31563) {\r |
| 1343 | FILE *f;\r |
| 1344 | f = fopen("ram_p.bin", "wb");\r |
| 1345 | if (!f) { printf("!f\n"); exit(1); }\r |
| 1346 | fwrite(Pico.ram, 1, 0x10000, f);\r |
| 1347 | fclose(f);\r |
| 1348 | exit(0);\r |
| 1349 | }\r |
| 1350 | #endif\r |
| 1351 | #if 0\r |
| 1352 | // debug\r |
| 1353 | {\r |
| 1354 | #define BYTE unsigned char\r |
| 1355 | #define WORD unsigned short\r |
| 1356 | struct\r |
| 1357 | {\r |
| 1358 | BYTE IDLength; /* 00h Size of Image ID field */\r |
| 1359 | BYTE ColorMapType; /* 01h Color map type */\r |
| 1360 | BYTE ImageType; /* 02h Image type code */\r |
| 1361 | WORD CMapStart; /* 03h Color map origin */\r |
| 1362 | WORD CMapLength; /* 05h Color map length */\r |
| 1363 | BYTE CMapDepth; /* 07h Depth of color map entries */\r |
| 1364 | WORD XOffset; /* 08h X origin of image */\r |
| 1365 | WORD YOffset; /* 0Ah Y origin of image */\r |
| 1366 | WORD Width; /* 0Ch Width of image */\r |
| 1367 | WORD Height; /* 0Eh Height of image */\r |
| 1368 | BYTE PixelDepth; /* 10h Image pixel size */\r |
| 1369 | BYTE ImageDescriptor; /* 11h Image descriptor byte */\r |
| 1370 | } __attribute__((packed)) TGAHEAD;\r |
| 1371 | static unsigned short oldscr[320*240];\r |
| 1372 | FILE *f; char name[128]; int i;\r |
| 1373 | \r |
| 1374 | memset(&TGAHEAD, 0, sizeof(TGAHEAD));\r |
| 1375 | TGAHEAD.ImageType = 2;\r |
| 1376 | TGAHEAD.Width = 320;\r |
| 1377 | TGAHEAD.Height = 240;\r |
| 1378 | TGAHEAD.PixelDepth = 16;\r |
| 1379 | TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left\r |
| 1380 | \r |
| 1381 | #define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?\r |
| 1382 | \r |
| 1383 | for (i = 0; i < 320*240; i++)\r |
| 1384 | if(oldscr[i] != CONV(((unsigned short *)gp2x_screen)[i])) break;\r |
| 1385 | if (i < 320*240)\r |
| 1386 | {\r |
| 1387 | for (i = 0; i < 320*240; i++)\r |
| 1388 | oldscr[i] = CONV(((unsigned short *)gp2x_screen)[i]);\r |
| 1389 | sprintf(name, "%05i.tga", Pico.m.frame_count);\r |
| 1390 | f = fopen(name, "wb");\r |
| 1391 | if (!f) { printf("!f\n"); exit(1); }\r |
| 1392 | fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);\r |
| 1393 | fwrite(oldscr, 1, 320*240*2, f);\r |
| 1394 | fclose(f);\r |
| 1395 | }\r |
| 1396 | }\r |
| 1397 | #endif\r |
| 1398 | \r |
| 1399 | // check time\r |
| 1400 | gettimeofday(&tval, 0);\r |
| 1401 | if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r |
| 1402 | \r |
| 1403 | if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 0x300000) // slowdown detection\r |
| 1404 | reset_timing = 1;\r |
| 1405 | else if (PsndOut != NULL || currentConfig.Frameskip < 0)\r |
| 1406 | {\r |
| 1407 | // sleep or vsync if we are still too fast\r |
| 1408 | // usleep sleeps for ~20ms minimum, so it is not a solution here\r |
| 1409 | if(tval.tv_usec < lim_time)\r |
| 1410 | {\r |
| 1411 | // we are too fast\r |
| 1412 | if (vsync_offset) {\r |
| 1413 | if (lim_time - tval.tv_usec > target_frametime/2)\r |
| 1414 | simpleWait(thissec, lim_time - target_frametime/4);\r |
| 1415 | gp2x_video_wait_vsync();\r |
| 1416 | } else {\r |
| 1417 | simpleWait(thissec, lim_time);\r |
| 1418 | }\r |
| 1419 | }\r |
| 1420 | }\r |
| 1421 | \r |
| 1422 | blit(fpsbuff, notice);\r |
| 1423 | \r |
| 1424 | frames_done++; frames_shown++;\r |
| 1425 | }\r |
| 1426 | \r |
| 1427 | \r |
| 1428 | if (PicoMCD & 1) PicoCDBufferFree();\r |
| 1429 | \r |
| 1430 | // save SRAM\r |
| 1431 | if((currentConfig.EmuOpt & 1) && SRam.changed) {\r |
| 1432 | emu_state_cb("Writing SRAM/BRAM..");\r |
| 1433 | emu_SaveLoadGame(0, 1);\r |
| 1434 | SRam.changed = 0;\r |
| 1435 | }\r |
| 1436 | \r |
| 1437 | // if in 8bit mode, generate 16bit image for menu background\r |
| 1438 | if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))\r |
| 1439 | emu_forced_frame();\r |
| 1440 | }\r |
| 1441 | \r |
| 1442 | \r |
| 1443 | void emu_ResetGame(void)\r |
| 1444 | {\r |
| 1445 | PicoReset(0);\r |
| 1446 | reset_timing = 1;\r |
| 1447 | }\r |
| 1448 | \r |
| 1449 | \r |
| 1450 | size_t gzRead2(void *p, size_t _size, size_t _n, void *file)\r |
| 1451 | {\r |
| 1452 | return gzread(file, p, _n);\r |
| 1453 | }\r |
| 1454 | \r |
| 1455 | \r |
| 1456 | size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)\r |
| 1457 | {\r |
| 1458 | return gzwrite(file, p, _n);\r |
| 1459 | }\r |
| 1460 | \r |
| 1461 | static int try_ropen_file(const char *fname)\r |
| 1462 | {\r |
| 1463 | FILE *f;\r |
| 1464 | \r |
| 1465 | f = fopen(fname, "rb");\r |
| 1466 | if (f) {\r |
| 1467 | fclose(f);\r |
| 1468 | return 1;\r |
| 1469 | }\r |
| 1470 | return 0;\r |
| 1471 | }\r |
| 1472 | \r |
| 1473 | char *emu_GetSaveFName(int load, int is_sram, int slot)\r |
| 1474 | {\r |
| 1475 | static char saveFname[512];\r |
| 1476 | char ext[16];\r |
| 1477 | \r |
| 1478 | if (is_sram)\r |
| 1479 | {\r |
| 1480 | romfname_ext(saveFname, (PicoMCD&1) ? "brm/" : "srm/", (PicoMCD&1) ? ".brm" : ".srm");\r |
| 1481 | if (load) {\r |
| 1482 | if (try_ropen_file(saveFname)) return saveFname;\r |
| 1483 | // try in current dir..\r |
| 1484 | romfname_ext(saveFname, NULL, (PicoMCD&1) ? ".brm" : ".srm");\r |
| 1485 | if (try_ropen_file(saveFname)) return saveFname;\r |
| 1486 | return NULL; // give up\r |
| 1487 | }\r |
| 1488 | }\r |
| 1489 | else\r |
| 1490 | {\r |
| 1491 | ext[0] = 0;\r |
| 1492 | if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);\r |
| 1493 | strcat(ext, (currentConfig.EmuOpt & 8) ? ".mds.gz" : ".mds");\r |
| 1494 | \r |
| 1495 | romfname_ext(saveFname, "mds/", ext);\r |
| 1496 | if (load) {\r |
| 1497 | if (try_ropen_file(saveFname)) return saveFname;\r |
| 1498 | romfname_ext(saveFname, NULL, ext);\r |
| 1499 | if (try_ropen_file(saveFname)) return saveFname;\r |
| 1500 | if (currentConfig.EmuOpt & 8) {\r |
| 1501 | ext[0] = 0;\r |
| 1502 | if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);\r |
| 1503 | strcat(ext, ".mds");\r |
| 1504 | \r |
| 1505 | romfname_ext(saveFname, "mds/", ext);\r |
| 1506 | if (try_ropen_file(saveFname)) return saveFname;\r |
| 1507 | romfname_ext(saveFname, NULL, ext);\r |
| 1508 | if (try_ropen_file(saveFname)) return saveFname;\r |
| 1509 | }\r |
| 1510 | return NULL;\r |
| 1511 | }\r |
| 1512 | }\r |
| 1513 | \r |
| 1514 | return saveFname;\r |
| 1515 | }\r |
| 1516 | \r |
| 1517 | int emu_check_save_file(int slot)\r |
| 1518 | {\r |
| 1519 | return emu_GetSaveFName(1, 0, slot) ? 1 : 0;\r |
| 1520 | }\r |
| 1521 | \r |
| 1522 | void emu_set_save_cbs(int gz)\r |
| 1523 | {\r |
| 1524 | if (gz) {\r |
| 1525 | areaRead = gzRead2;\r |
| 1526 | areaWrite = gzWrite2;\r |
| 1527 | areaEof = (areaeof *) gzeof;\r |
| 1528 | areaSeek = (areaseek *) gzseek;\r |
| 1529 | areaClose = (areaclose *) gzclose;\r |
| 1530 | } else {\r |
| 1531 | areaRead = (arearw *) fread;\r |
| 1532 | areaWrite = (arearw *) fwrite;\r |
| 1533 | areaEof = (areaeof *) feof;\r |
| 1534 | areaSeek = (areaseek *) fseek;\r |
| 1535 | areaClose = (areaclose *) fclose;\r |
| 1536 | }\r |
| 1537 | }\r |
| 1538 | \r |
| 1539 | int emu_SaveLoadGame(int load, int sram)\r |
| 1540 | {\r |
| 1541 | int ret = 0;\r |
| 1542 | char *saveFname;\r |
| 1543 | \r |
| 1544 | // make save filename\r |
| 1545 | saveFname = emu_GetSaveFName(load, sram, state_slot);\r |
| 1546 | if (saveFname == NULL) {\r |
| 1547 | if (!sram) {\r |
| 1548 | strcpy(noticeMsg, load ? "LOAD FAILED (missing file)" : "SAVE FAILED ");\r |
| 1549 | gettimeofday(¬iceMsgTime, 0);\r |
| 1550 | }\r |
| 1551 | return -1;\r |
| 1552 | }\r |
| 1553 | \r |
| 1554 | printf("saveLoad (%i, %i): %s\n", load, sram, saveFname);\r |
| 1555 | \r |
| 1556 | if(sram) {\r |
| 1557 | FILE *sramFile;\r |
| 1558 | int sram_size;\r |
| 1559 | unsigned char *sram_data;\r |
| 1560 | int truncate = 1;\r |
| 1561 | if (PicoMCD&1) {\r |
| 1562 | if (PicoOpt&0x8000) { // MCD RAM cart?\r |
| 1563 | sram_size = 0x12000;\r |
| 1564 | sram_data = SRam.data;\r |
| 1565 | if (sram_data)\r |
| 1566 | memcpy32((int *)sram_data, (int *)Pico_mcd->bram, 0x2000/4);\r |
| 1567 | } else {\r |
| 1568 | sram_size = 0x2000;\r |
| 1569 | sram_data = Pico_mcd->bram;\r |
| 1570 | truncate = 0; // the .brm may contain RAM cart data after normal brm\r |
| 1571 | }\r |
| 1572 | } else {\r |
| 1573 | sram_size = SRam.end-SRam.start+1;\r |
| 1574 | if(SRam.reg_back & 4) sram_size=0x2000;\r |
| 1575 | sram_data = SRam.data;\r |
| 1576 | }\r |
| 1577 | if (!sram_data) return 0; // SRam forcefully disabled for this game\r |
| 1578 | \r |
| 1579 | if (load) {\r |
| 1580 | sramFile = fopen(saveFname, "rb");\r |
| 1581 | if(!sramFile) return -1;\r |
| 1582 | fread(sram_data, 1, sram_size, sramFile);\r |
| 1583 | fclose(sramFile);\r |
| 1584 | if ((PicoMCD&1) && (PicoOpt&0x8000))\r |
| 1585 | memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4);\r |
| 1586 | } else {\r |
| 1587 | // sram save needs some special processing\r |
| 1588 | // see if we have anything to save\r |
| 1589 | for (; sram_size > 0; sram_size--)\r |
| 1590 | if (sram_data[sram_size-1]) break;\r |
| 1591 | \r |
| 1592 | if (sram_size) {\r |
| 1593 | sramFile = fopen(saveFname, truncate ? "wb" : "r+b");\r |
| 1594 | if (!sramFile) sramFile = fopen(saveFname, "wb"); // retry\r |
| 1595 | if (!sramFile) return -1;\r |
| 1596 | ret = fwrite(sram_data, 1, sram_size, sramFile);\r |
| 1597 | ret = (ret != sram_size) ? -1 : 0;\r |
| 1598 | fclose(sramFile);\r |
| 1599 | #ifndef NO_SYNC\r |
| 1600 | sync();\r |
| 1601 | #endif\r |
| 1602 | }\r |
| 1603 | }\r |
| 1604 | return ret;\r |
| 1605 | }\r |
| 1606 | else\r |
| 1607 | {\r |
| 1608 | void *PmovFile = NULL;\r |
| 1609 | if (strcmp(saveFname + strlen(saveFname) - 3, ".gz") == 0) {\r |
| 1610 | if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) {\r |
| 1611 | emu_set_save_cbs(1);\r |
| 1612 | if(!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);\r |
| 1613 | }\r |
| 1614 | }\r |
| 1615 | else\r |
| 1616 | {\r |
| 1617 | if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) {\r |
| 1618 | emu_set_save_cbs(0);\r |
| 1619 | }\r |
| 1620 | }\r |
| 1621 | if(PmovFile) {\r |
| 1622 | ret = PmovState(load ? 6 : 5, PmovFile);\r |
| 1623 | areaClose(PmovFile);\r |
| 1624 | PmovFile = 0;\r |
| 1625 | if (load) Pico.m.dirtyPal=1;\r |
| 1626 | #ifndef NO_SYNC\r |
| 1627 | else sync();\r |
| 1628 | #endif\r |
| 1629 | }\r |
| 1630 | else ret = -1;\r |
| 1631 | if (!ret)\r |
| 1632 | strcpy(noticeMsg, load ? "GAME LOADED " : "GAME SAVED ");\r |
| 1633 | else\r |
| 1634 | {\r |
| 1635 | strcpy(noticeMsg, load ? "LOAD FAILED " : "SAVE FAILED ");\r |
| 1636 | ret = -1;\r |
| 1637 | }\r |
| 1638 | \r |
| 1639 | gettimeofday(¬iceMsgTime, 0);\r |
| 1640 | return ret;\r |
| 1641 | }\r |
| 1642 | }\r |