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