cue/bin finally implemented
[picodrive.git] / platform / common / emu.c
CommitLineData
ea8c405f 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 <ctype.h> // tolower\r
9#ifndef NO_SYNC\r
10#include <unistd.h>\r
11#endif\r
12\r
13#include "emu.h"\r
14#include "menu.h"\r
15#include "fonts.h"\r
16#include "lprintf.h"\r
58c86d00 17#include "config.h"\r
ea8c405f 18\r
19#include <Pico/PicoInt.h>\r
20#include <Pico/Patch.h>\r
9037e45d 21#include <Pico/cd/cue.h>\r
ea8c405f 22#include <zlib/zlib.h>\r
23\r
24#if defined(__GP2X__)\r
25 #include "../gp2x/gp2x.h"\r
26 #define SCREEN_WIDTH 320\r
27 #define SCREEN_BUFFER gp2x_screen\r
28#elif defined(__GIZ__)\r
29 #include "../gizmondo/giz.h"\r
30 #define SCREEN_WIDTH 321\r
1820b5a7 31 #define SCREEN_BUFFER giz_screen\r
32#elif defined(PSP)\r
33 #include "../psp/psp.h"\r
34 #define SCREEN_WIDTH 512\r
35 #define SCREEN_BUFFER psp_screen\r
ea8c405f 36#endif\r
37\r
c46ffd31 38char *PicoConfigFile = "config.cfg";\r
58c86d00 39currentConfig_t currentConfig, defaultConfig;\r
eacee137 40int rom_loaded = 0;\r
ea8c405f 41char noticeMsg[64];\r
42int state_slot = 0;\r
43int config_slot = 0, config_slot_current = 0;\r
1ca2ea4f 44char lastRomFile[512];\r
bdec53c9 45int kb_combo_keys = 0, kb_combo_acts = 0; // keys and actions which need button combos\r
ea8c405f 46\r
47unsigned char *movie_data = NULL;\r
48static int movie_size = 0;\r
49\r
50// provided by platform code:\r
51extern char romFileName[];\r
52extern void emu_noticeMsgUpdated(void);\r
53extern void emu_getMainDir(char *dst, int len);\r
54extern void emu_setDefaultConfig(void);\r
55extern void menu_romload_prepare(const char *rom_name);\r
56extern void menu_romload_end(void);\r
57\r
58\r
59// utilities\r
60static void strlwr_(char* string)\r
61{\r
62 while ( (*string++ = (char)tolower(*string)) );\r
63}\r
64\r
65static int try_rfn_cut(void)\r
66{\r
67 FILE *tmp;\r
68 char *p;\r
69\r
70 p = romFileName + strlen(romFileName) - 1;\r
71 for (; p > romFileName; p--)\r
72 if (*p == '.') break;\r
73 *p = 0;\r
74\r
75 if((tmp = fopen(romFileName, "rb"))) {\r
76 fclose(tmp);\r
77 return 1;\r
78 }\r
79 return 0;\r
80}\r
81\r
82static void get_ext(char *file, char *ext)\r
83{\r
84 char *p;\r
85\r
86 p = file + strlen(file) - 4;\r
87 if (p < file) p = file;\r
88 strncpy(ext, p, 4);\r
89 ext[4] = 0;\r
90 strlwr_(ext);\r
91}\r
92\r
2d2247c2 93char *biosfiles_us[] = { "us_scd1_9210", "us_scd2_9306", "SegaCDBIOS9303" };\r
94char *biosfiles_eu[] = { "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303" };\r
ea8c405f 95char *biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };\r
96\r
97int emu_findBios(int region, char **bios_file)\r
98{\r
99 static char bios_path[1024];\r
100 int i, count;\r
101 char **files;\r
102 FILE *f = NULL;\r
103\r
104 if (region == 4) { // US\r
105 files = biosfiles_us;\r
106 count = sizeof(biosfiles_us) / sizeof(char *);\r
107 } else if (region == 8) { // EU\r
108 files = biosfiles_eu;\r
109 count = sizeof(biosfiles_eu) / sizeof(char *);\r
110 } else if (region == 1 || region == 2) {\r
111 files = biosfiles_jp;\r
112 count = sizeof(biosfiles_jp) / sizeof(char *);\r
113 } else {\r
114 return 0;\r
115 }\r
116\r
117 for (i = 0; i < count; i++)\r
118 {\r
119 emu_getMainDir(bios_path, sizeof(bios_path));\r
120 strcat(bios_path, files[i]);\r
121 strcat(bios_path, ".bin");\r
122 f = fopen(bios_path, "rb");\r
123 if (f) break;\r
124\r
125 bios_path[strlen(bios_path) - 4] = 0;\r
126 strcat(bios_path, ".zip");\r
127 f = fopen(bios_path, "rb");\r
128 if (f) break;\r
129 }\r
130\r
131 if (f) {\r
132 lprintf("using bios: %s\n", bios_path);\r
133 fclose(f);\r
134 if (bios_file) *bios_file = bios_path;\r
135 return 1;\r
136 } else {\r
137 sprintf(menuErrorMsg, "no %s BIOS files found, read docs",\r
138 region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r
139 lprintf("%s\n", menuErrorMsg);\r
140 return 0;\r
141 }\r
142}\r
143\r
c6196c0f 144/* check if the name begins with BIOS name */\r
1ca2ea4f 145/*\r
c6196c0f 146static int emu_isBios(const char *name)\r
147{\r
148 int i;\r
149 for (i = 0; i < sizeof(biosfiles_us)/sizeof(biosfiles_us[0]); i++)\r
150 if (strstr(name, biosfiles_us[i]) != NULL) return 1;\r
151 for (i = 0; i < sizeof(biosfiles_eu)/sizeof(biosfiles_eu[0]); i++)\r
152 if (strstr(name, biosfiles_eu[i]) != NULL) return 1;\r
153 for (i = 0; i < sizeof(biosfiles_jp)/sizeof(biosfiles_jp[0]); i++)\r
154 if (strstr(name, biosfiles_jp[i]) != NULL) return 1;\r
155 return 0;\r
156}\r
1ca2ea4f 157*/\r
c6196c0f 158\r
1ca2ea4f 159static unsigned char id_header[0x100];\r
58c86d00 160\r
ea8c405f 161/* checks if romFileName points to valid MegaCD image\r
162 * if so, checks for suitable BIOS */\r
163int emu_cdCheck(int *pregion)\r
164{\r
165 unsigned char buf[32];\r
166 pm_file *cd_f;\r
9037e45d 167 int region = 4; // 1: Japan, 4: US, 8: Europe\r
168 char ext[5], *fname = romFileName;\r
169 cue_track_type type = CT_UNKNOWN;\r
170 cue_data_t *cue_data = NULL;\r
b923ecbe 171\r
172 get_ext(romFileName, ext);\r
9037e45d 173 if (strcasecmp(ext, ".cue") == 0) {\r
174 cue_data = cue_parse(romFileName);\r
175 if (cue_data != NULL) {\r
176 fname = cue_data->tracks[1].fname;\r
177 type = cue_data->tracks[1].type;\r
178 }\r
179 }\r
180\r
181 cd_f = pm_open(fname);\r
182 if (cue_data != NULL)\r
183 cue_destroy(cue_data);\r
ea8c405f 184\r
ea8c405f 185 if (!cd_f) return 0; // let the upper level handle this\r
186\r
187 if (pm_read(buf, 32, cd_f) != 32) {\r
188 pm_close(cd_f);\r
189 return 0;\r
190 }\r
191\r
9037e45d 192 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) {\r
193 if (type && type != CT_ISO)\r
194 elprintf(EL_STATUS, ".cue has wrong type: %i", type);\r
195 type = CT_ISO; // Sega CD (ISO)\r
196 }\r
197 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) {\r
198 if (type && type != CT_BIN)\r
199 elprintf(EL_STATUS, ".cue has wrong type: %i", type);\r
200 type = CT_BIN; // Sega CD (BIN)\r
201 }\r
202\r
203 if (type == CT_UNKNOWN) {\r
ea8c405f 204 pm_close(cd_f);\r
205 return 0;\r
206 }\r
207\r
9037e45d 208 pm_seek(cd_f, (type == CT_ISO) ? 0x100 : 0x110, SEEK_SET);\r
1ca2ea4f 209 pm_read(id_header, sizeof(id_header), cd_f);\r
58c86d00 210\r
ea8c405f 211 /* it seems we have a CD image here. Try to detect region now.. */\r
9037e45d 212 pm_seek(cd_f, (type == CT_ISO) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);\r
ea8c405f 213 pm_read(buf, 1, cd_f);\r
214 pm_close(cd_f);\r
215\r
216 if (buf[0] == 0x64) region = 8; // EU\r
217 if (buf[0] == 0xa1) region = 1; // JAP\r
218\r
219 lprintf("detected %s Sega/Mega CD image with %s region\n",\r
9037e45d 220 type == CT_BIN ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r
ea8c405f 221\r
222 if (pregion != NULL) *pregion = region;\r
223\r
224 return type;\r
225}\r
226\r
58c86d00 227static int extract_text(char *dest, unsigned char *src, int len, int swab)\r
228{\r
229 char *p = dest;\r
230 int i;\r
231\r
232 if (swab) swab = 1;\r
233\r
234 for (i = len - 1; i >= 0; i--)\r
235 {\r
236 if (src[i^swab] != ' ') break;\r
237 }\r
238 len = i + 1;\r
239\r
240 for (i = 0; i < len; i++)\r
241 {\r
242 unsigned char s = src[i^swab];\r
243 if (s >= 0x20 && s < 0x7f && s != '#' && s != '|' &&\r
244 s != '[' && s != ']' && s != '\\')\r
245 {\r
246 *p++ = s;\r
247 }\r
248 else\r
249 {\r
250 sprintf(p, "\\%02x", s);\r
251 p += 3;\r
252 }\r
253 }\r
254\r
255 return p - dest;\r
256}\r
257\r
258char *emu_makeRomId(void)\r
259{\r
260 static char id_string[3+0x11+0x11+0x30+16];\r
bdec53c9 261 int pos, swab = 1;\r
58c86d00 262\r
602133e1 263 if (PicoAHW & PAHW_MCD) {\r
bdec53c9 264 strcpy(id_string, "CD|");\r
265 swab = 0;\r
266 }\r
1ca2ea4f 267 else strcpy(id_string, "MD|");\r
58c86d00 268 pos = 3;\r
269\r
bdec53c9 270 pos += extract_text(id_string + pos, id_header + 0x80, 0x0e, swab); // serial\r
58c86d00 271 id_string[pos] = '|'; pos++;\r
bdec53c9 272 pos += extract_text(id_string + pos, id_header + 0xf0, 0x03, swab); // region\r
58c86d00 273 id_string[pos] = '|'; pos++;\r
bdec53c9 274 pos += extract_text(id_string + pos, id_header + 0x50, 0x30, swab); // overseas name\r
58c86d00 275 id_string[pos] = 0;\r
276\r
58c86d00 277 return id_string;\r
278}\r
279\r
ea8c405f 280int emu_ReloadRom(void)\r
281{\r
282 unsigned int rom_size = 0;\r
283 char *used_rom_name = romFileName;\r
eacee137 284 unsigned char *rom_data = NULL;\r
ea8c405f 285 char ext[5];\r
286 pm_file *rom;\r
287 int ret, cd_state, cd_region, cfg_loaded = 0;\r
288\r
289 lprintf("emu_ReloadRom(%s)\n", romFileName);\r
290\r
291 get_ext(romFileName, ext);\r
292\r
293 // detect wrong extensions\r
eacee137 294 if (!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz\r
ea8c405f 295 sprintf(menuErrorMsg, "Not a ROM selected.");\r
296 return 0;\r
297 }\r
298\r
299 PicoPatchUnload();\r
300\r
301 // check for movie file\r
eacee137 302 if (movie_data) {\r
ea8c405f 303 free(movie_data);\r
304 movie_data = 0;\r
305 }\r
2d2247c2 306 if (!strcmp(ext, ".gmv"))\r
307 {\r
ea8c405f 308 // check for both gmv and rom\r
309 int dummy;\r
310 FILE *movie_file = fopen(romFileName, "rb");\r
311 if(!movie_file) {\r
312 sprintf(menuErrorMsg, "Failed to open movie.");\r
313 return 0;\r
314 }\r
315 fseek(movie_file, 0, SEEK_END);\r
316 movie_size = ftell(movie_file);\r
317 fseek(movie_file, 0, SEEK_SET);\r
318 if(movie_size < 64+3) {\r
319 sprintf(menuErrorMsg, "Invalid GMV file.");\r
320 fclose(movie_file);\r
321 return 0;\r
322 }\r
323 movie_data = malloc(movie_size);\r
324 if(movie_data == NULL) {\r
325 sprintf(menuErrorMsg, "low memory.");\r
326 fclose(movie_file);\r
327 return 0;\r
328 }\r
329 fread(movie_data, 1, movie_size, movie_file);\r
330 fclose(movie_file);\r
331 if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {\r
332 sprintf(menuErrorMsg, "Invalid GMV file.");\r
333 return 0;\r
334 }\r
335 dummy = try_rfn_cut() || try_rfn_cut();\r
336 if (!dummy) {\r
337 sprintf(menuErrorMsg, "Could't find a ROM for movie.");\r
338 return 0;\r
339 }\r
340 get_ext(romFileName, ext);\r
341 }\r
342 else if (!strcmp(ext, ".pat")) {\r
343 int dummy;\r
344 PicoPatchLoad(romFileName);\r
345 dummy = try_rfn_cut() || try_rfn_cut();\r
346 if (!dummy) {\r
347 sprintf(menuErrorMsg, "Could't find a ROM to patch.");\r
348 return 0;\r
349 }\r
350 get_ext(romFileName, ext);\r
351 }\r
352\r
602133e1 353 if ((PicoAHW & PAHW_MCD) && Pico_mcd != NULL)\r
ea8c405f 354 Stop_CD();\r
355\r
356 // check for MegaCD image\r
357 cd_state = emu_cdCheck(&cd_region);\r
b923ecbe 358 if (cd_state != CIT_NOT_CD)\r
ea8c405f 359 {\r
602133e1 360 PicoAHW |= PAHW_MCD;\r
ea8c405f 361 // valid CD image, check for BIOS..\r
362\r
363 // we need to have config loaded at this point\r
364 ret = emu_ReadConfig(1, 1);\r
365 if (!ret) emu_ReadConfig(0, 1);\r
366 cfg_loaded = 1;\r
367\r
368 if (PicoRegionOverride) {\r
369 cd_region = PicoRegionOverride;\r
370 lprintf("overrided region to %s\n", cd_region != 4 ? (cd_region == 8 ? "EU" : "JAP") : "USA");\r
371 }\r
372 if (!emu_findBios(cd_region, &used_rom_name)) {\r
373 // bios_help() ?\r
602133e1 374 PicoAHW &= ~PAHW_MCD;\r
ea8c405f 375 return 0;\r
376 }\r
377\r
ea8c405f 378 get_ext(used_rom_name, ext);\r
379 }\r
380 else\r
381 {\r
602133e1 382 if (PicoAHW & PAHW_MCD) Stop_CD();\r
383 PicoAHW &= ~PAHW_MCD;\r
ea8c405f 384 }\r
385\r
386 rom = pm_open(used_rom_name);\r
eacee137 387 if (!rom) {\r
ea8c405f 388 sprintf(menuErrorMsg, "Failed to open rom.");\r
389 return 0;\r
390 }\r
391\r
4b167c12 392 menu_romload_prepare(used_rom_name); // also CD load\r
ea8c405f 393\r
eacee137 394 PicoCartUnload();\r
395 rom_loaded = 0;\r
ea8c405f 396\r
eacee137 397 if ( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) {\r
ea8c405f 398 sprintf(menuErrorMsg, "PicoCartLoad() failed.");\r
399 lprintf("%s\n", menuErrorMsg);\r
400 pm_close(rom);\r
401 menu_romload_end();\r
402 return 0;\r
403 }\r
404 pm_close(rom);\r
ea8c405f 405\r
406 // detect wrong files (Pico crashes on very small files), also see if ROM EP is good\r
eacee137 407 if (rom_size <= 0x200 || strncmp((char *)rom_data, "Pico", 4) == 0 ||\r
ea8c405f 408 ((*(unsigned char *)(rom_data+4)<<16)|(*(unsigned short *)(rom_data+6))) >= (int)rom_size) {\r
409 if (rom_data) free(rom_data);\r
ea8c405f 410 sprintf(menuErrorMsg, "Not a ROM selected.");\r
4b167c12 411 menu_romload_end();\r
ea8c405f 412 return 0;\r
413 }\r
414\r
415 // load config for this ROM (do this before insert to get correct region)\r
602133e1 416 if (!(PicoAHW & PAHW_MCD))\r
1ca2ea4f 417 memcpy(id_header, rom_data + 0x100, sizeof(id_header));\r
ea8c405f 418 if (!cfg_loaded) {\r
419 ret = emu_ReadConfig(1, 1);\r
420 if (!ret) emu_ReadConfig(0, 1);\r
421 }\r
422\r
423 lprintf("PicoCartInsert(%p, %d);\n", rom_data, rom_size);\r
eacee137 424 if (PicoCartInsert(rom_data, rom_size)) {\r
ea8c405f 425 sprintf(menuErrorMsg, "Failed to load ROM.");\r
4b167c12 426 menu_romload_end();\r
ea8c405f 427 return 0;\r
428 }\r
429\r
430 Pico.m.frame_count = 0;\r
431\r
432 // insert CD if it was detected\r
b923ecbe 433 if (cd_state != CIT_NOT_CD) {\r
434 ret = Insert_CD(romFileName, cd_state);\r
ea8c405f 435 if (ret != 0) {\r
436 sprintf(menuErrorMsg, "Insert_CD() failed, invalid CD image?");\r
437 lprintf("%s\n", menuErrorMsg);\r
4b167c12 438 menu_romload_end();\r
ea8c405f 439 return 0;\r
440 }\r
441 }\r
442\r
4b167c12 443 menu_romload_end();\r
444\r
ea8c405f 445 if (PicoPatches) {\r
446 PicoPatchPrepare();\r
447 PicoPatchApply();\r
448 }\r
449\r
450 // additional movie stuff\r
451 if (movie_data) {\r
452 if(movie_data[0x14] == '6')\r
602133e1 453 PicoOpt |= POPT_6BTN_PAD; // 6 button pad\r
454 else PicoOpt &= ~POPT_6BTN_PAD;\r
455 PicoOpt |= POPT_DIS_VDP_FIFO|POPT_ACC_TIMING; // accurate timing, no VDP fifo timing\r
ea8c405f 456 if(movie_data[0xF] >= 'A') {\r
457 if(movie_data[0x16] & 0x80) {\r
458 PicoRegionOverride = 8;\r
459 } else {\r
460 PicoRegionOverride = 4;\r
461 }\r
1cb1584b 462 PicoReset();\r
ea8c405f 463 // TODO: bits 6 & 5\r
464 }\r
465 movie_data[0x18+30] = 0;\r
466 sprintf(noticeMsg, "MOVIE: %s", (char *) &movie_data[0x18]);\r
467 }\r
468 else\r
469 {\r
602133e1 470 PicoOpt &= ~POPT_DIS_VDP_FIFO;\r
ea8c405f 471 if(Pico.m.pal) {\r
472 strcpy(noticeMsg, "PAL SYSTEM / 50 FPS");\r
473 } else {\r
474 strcpy(noticeMsg, "NTSC SYSTEM / 60 FPS");\r
475 }\r
476 }\r
477 emu_noticeMsgUpdated();\r
478\r
479 // load SRAM for this ROM\r
eacee137 480 if (currentConfig.EmuOpt & 1)\r
ea8c405f 481 emu_SaveLoadGame(1, 1);\r
482\r
1ca2ea4f 483 strncpy(lastRomFile, romFileName, sizeof(lastRomFile)-1);\r
484 lastRomFile[sizeof(lastRomFile)-1] = 0;\r
eacee137 485 rom_loaded = 1;\r
ea8c405f 486 return 1;\r
487}\r
488\r
489\r
490static void romfname_ext(char *dst, const char *prefix, const char *ext)\r
491{\r
492 char *p;\r
493 int prefix_len = 0;\r
494\r
495 // make save filename\r
496 for (p = romFileName+strlen(romFileName)-1; p >= romFileName && *p != '/'; p--); p++;\r
497 *dst = 0;\r
498 if (prefix) {\r
499 strcpy(dst, prefix);\r
500 prefix_len = strlen(prefix);\r
501 }\r
502 strncpy(dst + prefix_len, p, 511-prefix_len);\r
503 dst[511-8] = 0;\r
504 if (dst[strlen(dst)-4] == '.') dst[strlen(dst)-4] = 0;\r
505 if (ext) strcat(dst, ext);\r
506}\r
507\r
508\r
08fe8094 509static void make_config_cfg(char *cfg)\r
510{\r
511 strncpy(cfg, PicoConfigFile, 511);\r
512 if (config_slot != 0)\r
513 {\r
514 char *p = strrchr(cfg, '.');\r
515 if (p == NULL) p = cfg + strlen(cfg);\r
516 sprintf(p, ".%i.cfg", config_slot);\r
517 }\r
518 cfg[511] = 0;\r
519}\r
520\r
ea8c405f 521int emu_ReadConfig(int game, int no_defaults)\r
522{\r
58c86d00 523 char cfg[512];\r
ea8c405f 524 FILE *f;\r
58c86d00 525 int ret;\r
ea8c405f 526\r
527 if (!game)\r
528 {\r
529 if (!no_defaults)\r
ea8c405f 530 emu_setDefaultConfig();\r
08fe8094 531 make_config_cfg(cfg);\r
58c86d00 532 ret = config_readsect(cfg, NULL);\r
ea8c405f 533 }\r
58c86d00 534 else\r
535 {\r
1ca2ea4f 536 char *sect = emu_makeRomId();\r
ea8c405f 537\r
58c86d00 538 // try new .cfg way\r
539 if (config_slot != 0)\r
540 sprintf(cfg, "game.%i.cfg", config_slot);\r
541 else strcpy(cfg, "game.cfg");\r
1ca2ea4f 542\r
543 ret = -1;\r
08fe8094 544 if (config_havesect(cfg, sect))\r
545 {\r
546 // read user's config\r
7b802576 547 int vol = currentConfig.volume;\r
1ca2ea4f 548 emu_setDefaultConfig();\r
549 ret = config_readsect(cfg, sect);\r
7b802576 550 currentConfig.volume = vol; // make vol global (bah)\r
1ca2ea4f 551 }\r
a39b7867 552 else\r
08fe8094 553 {\r
554 // read global config, and apply game_def.cfg on top\r
555 make_config_cfg(cfg);\r
556 config_readsect(cfg, NULL);\r
557 ret = config_readsect("game_def.cfg", sect);\r
558 }\r
58c86d00 559\r
560 if (ret != 0)\r
561 {\r
08fe8094 562 // fall back to old game specific cfg\r
58c86d00 563 char extbuf[16];\r
564 if (config_slot != 0)\r
565 sprintf(extbuf, ".%i.pbcfg", config_slot);\r
566 else strcpy(extbuf, ".pbcfg");\r
567 romfname_ext(cfg, "cfg/", extbuf);\r
568 f = fopen(cfg, "rb");\r
569 if (!f) {\r
570 romfname_ext(cfg, NULL, ".pbcfg");\r
571 f = fopen(cfg, "rb");\r
572 }\r
573 if (f) {\r
1ca2ea4f 574 int bread;\r
575 fseek(f, 512, SEEK_SET); // skip unused lrom buffer\r
576 bread = fread(&currentConfig, 1, sizeof(currentConfig), f);\r
58c86d00 577 lprintf("emu_ReadConfig: %s %s\n", cfg, bread > 0 ? "(ok)" : "(failed)");\r
578 fclose(f);\r
579 ret = 0;\r
580 }\r
581\r
582 if (ret == 0) {\r
583 PicoOpt = currentConfig.s_PicoOpt;\r
584 PsndRate = currentConfig.s_PsndRate;\r
585 PicoRegionOverride = currentConfig.s_PicoRegion;\r
586 PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder;\r
2d2247c2 587 // PicoCDBuffers = currentConfig.s_PicoCDBuffers; // ignore in this case\r
58c86d00 588 }\r
589 }\r
590 else\r
591 {\r
1ca2ea4f 592 lprintf("loaded cfg from sect \"%s\"\n", sect);\r
58c86d00 593 }\r
ea8c405f 594 }\r
58c86d00 595\r
ea8c405f 596 // some sanity checks\r
597 if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;\r
2445b7cb 598#ifdef PSP\r
599 if (currentConfig.gamma < -4 || currentConfig.gamma > 16) currentConfig.gamma = 0;\r
6fc57144 600 if (currentConfig.gamma2 < 0 || currentConfig.gamma2 > 2) currentConfig.gamma2 = 0;\r
2445b7cb 601#else\r
ea8c405f 602 if (currentConfig.gamma < 10 || currentConfig.gamma > 300) currentConfig.gamma = 100;\r
2445b7cb 603#endif\r
ea8c405f 604 if (currentConfig.volume < 0 || currentConfig.volume > 99) currentConfig.volume = 50;\r
9839d126 605#ifdef __GP2X__\r
ea8c405f 606 // if volume keys are unbound, bind them to volume control\r
607 if (!currentConfig.KeyBinds[23] && !currentConfig.KeyBinds[22]) {\r
608 currentConfig.KeyBinds[23] = 1<<29; // vol up\r
609 currentConfig.KeyBinds[22] = 1<<30; // vol down\r
610 }\r
9839d126 611#endif\r
58c86d00 612 if (ret == 0) config_slot_current = config_slot;\r
613 return (ret == 0);\r
ea8c405f 614}\r
615\r
616\r
58c86d00 617int emu_WriteConfig(int is_game)\r
ea8c405f 618{\r
58c86d00 619 char cfg[512], *game_sect = NULL;\r
620 int ret, write_lrom = 0;\r
ea8c405f 621\r
58c86d00 622 if (!is_game)\r
ea8c405f 623 {\r
624 strncpy(cfg, PicoConfigFile, 511);\r
625 if (config_slot != 0)\r
626 {\r
627 char *p = strrchr(cfg, '.');\r
628 if (p == NULL) p = cfg + strlen(cfg);\r
58c86d00 629 sprintf(p, ".%i.cfg", config_slot);\r
ea8c405f 630 }\r
631 cfg[511] = 0;\r
58c86d00 632 write_lrom = 1;\r
ea8c405f 633 } else {\r
634 if (config_slot != 0)\r
58c86d00 635 sprintf(cfg, "game.%i.cfg", config_slot);\r
636 else strcpy(cfg, "game.cfg");\r
637 game_sect = emu_makeRomId();\r
1ca2ea4f 638 lprintf("emu_WriteConfig: sect \"%s\"\n", game_sect);\r
ea8c405f 639 }\r
640\r
641 lprintf("emu_WriteConfig: %s ", cfg);\r
58c86d00 642 ret = config_writesect(cfg, game_sect);\r
1ca2ea4f 643 if (write_lrom) config_writelrom(cfg);\r
ea8c405f 644#ifndef NO_SYNC\r
58c86d00 645 sync();\r
ea8c405f 646#endif\r
58c86d00 647 lprintf((ret == 0) ? "(ok)\n" : "(failed)\n");\r
ea8c405f 648\r
58c86d00 649 if (ret == 0) config_slot_current = config_slot;\r
650 return ret == 0;\r
ea8c405f 651}\r
652\r
653\r
654void emu_textOut8(int x, int y, const char *text)\r
655{\r
656 int i,l,len=strlen(text);\r
657 unsigned char *screen = (unsigned char *)SCREEN_BUFFER + x + y*SCREEN_WIDTH;\r
658\r
659 /* always using built-in font */\r
660 for (i = 0; i < len; i++)\r
661 {\r
662 for (l=0;l<8;l++)\r
663 {\r
664 unsigned char fd = fontdata8x8[((text[i])*8)+l];\r
665 if (fd&0x80) screen[l*SCREEN_WIDTH+0]=0xf0;\r
666 if (fd&0x40) screen[l*SCREEN_WIDTH+1]=0xf0;\r
667 if (fd&0x20) screen[l*SCREEN_WIDTH+2]=0xf0;\r
668 if (fd&0x10) screen[l*SCREEN_WIDTH+3]=0xf0;\r
669 if (fd&0x08) screen[l*SCREEN_WIDTH+4]=0xf0;\r
670 if (fd&0x04) screen[l*SCREEN_WIDTH+5]=0xf0;\r
671 if (fd&0x02) screen[l*SCREEN_WIDTH+6]=0xf0;\r
672 if (fd&0x01) screen[l*SCREEN_WIDTH+7]=0xf0;\r
673 }\r
674 screen += 8;\r
675 }\r
676}\r
677\r
678void emu_textOut16(int x, int y, const char *text)\r
679{\r
680 int i,l,len=strlen(text);\r
681 unsigned short *screen = (unsigned short *)SCREEN_BUFFER + x + y*SCREEN_WIDTH;\r
682\r
683 for (i = 0; i < len; i++)\r
684 {\r
685 for (l=0;l<8;l++)\r
686 {\r
687 unsigned char fd = fontdata8x8[((text[i])*8)+l];\r
688 if(fd&0x80) screen[l*SCREEN_WIDTH+0]=0xffff;\r
689 if(fd&0x40) screen[l*SCREEN_WIDTH+1]=0xffff;\r
690 if(fd&0x20) screen[l*SCREEN_WIDTH+2]=0xffff;\r
691 if(fd&0x10) screen[l*SCREEN_WIDTH+3]=0xffff;\r
692 if(fd&0x08) screen[l*SCREEN_WIDTH+4]=0xffff;\r
693 if(fd&0x04) screen[l*SCREEN_WIDTH+5]=0xffff;\r
694 if(fd&0x02) screen[l*SCREEN_WIDTH+6]=0xffff;\r
695 if(fd&0x01) screen[l*SCREEN_WIDTH+7]=0xffff;\r
696 }\r
697 screen += 8;\r
698 }\r
699}\r
700\r
6fc57144 701#ifdef PSP\r
702#define MAX_COMBO_KEY 23\r
703#else\r
704#define MAX_COMBO_KEY 31\r
705#endif\r
706\r
bdec53c9 707void emu_findKeyBindCombos(void)\r
708{\r
709 int act, u;\r
710\r
711 // find out which keys and actions are combos\r
712 kb_combo_keys = kb_combo_acts = 0;\r
713 for (act = 0; act < 32; act++)\r
714 {\r
715 int keyc = 0, keyc2 = 0;\r
716 if (act == 16 || act == 17) continue; // player2 flag\r
717 if (act > 17)\r
718 {\r
6fc57144 719 for (u = 0; u <= MAX_COMBO_KEY; u++)\r
bdec53c9 720 if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;\r
721 }\r
722 else\r
723 {\r
6fc57144 724 for (u = 0; u <= MAX_COMBO_KEY; u++)\r
bdec53c9 725 if ((currentConfig.KeyBinds[u] & 0x30000) == 0 && // pl. 1\r
726 (currentConfig.KeyBinds[u] & (1 << act))) keyc++;\r
6fc57144 727 for (u = 0; u <= MAX_COMBO_KEY; u++)\r
bdec53c9 728 if ((currentConfig.KeyBinds[u] & 0x30000) == 1 && // pl. 2\r
729 (currentConfig.KeyBinds[u] & (1 << act))) keyc2++;\r
730 if (keyc2 > keyc) keyc = keyc2;\r
731 }\r
732 if (keyc > 1)\r
733 {\r
734 // loop again and mark those keys and actions as combo\r
6fc57144 735 for (u = 0; u <= MAX_COMBO_KEY; u++)\r
bdec53c9 736 {\r
737 if (currentConfig.KeyBinds[u] & (1 << act)) {\r
738 kb_combo_keys |= 1 << u;\r
739 kb_combo_acts |= 1 << act;\r
740 }\r
741 }\r
742 }\r
743 }\r
744\r
745 // printf("combo keys/acts: %08x %08x\n", kb_combo_keys, kb_combo_acts);\r
746}\r
747\r
ea8c405f 748\r
749void emu_updateMovie(void)\r
750{\r
751 int offs = Pico.m.frame_count*3 + 0x40;\r
752 if (offs+3 > movie_size) {\r
753 free(movie_data);\r
754 movie_data = 0;\r
755 strcpy(noticeMsg, "END OF MOVIE.");\r
756 lprintf("END OF MOVIE.\n");\r
757 emu_noticeMsgUpdated();\r
758 } else {\r
759 // MXYZ SACB RLDU\r
760 PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU\r
761 if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // A\r
762 if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // B\r
763 if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // A\r
764 PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU\r
765 if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // A\r
766 if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // B\r
767 if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // A\r
768 PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX\r
769 if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X\r
770 if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z\r
771 PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX\r
772 if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X\r
773 if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z\r
774 }\r
775}\r
776\r
777\r
778static size_t gzRead2(void *p, size_t _size, size_t _n, void *file)\r
779{\r
780 return gzread(file, p, _n);\r
781}\r
782\r
783\r
784static size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)\r
785{\r
786 return gzwrite(file, p, _n);\r
787}\r
788\r
789static int try_ropen_file(const char *fname)\r
790{\r
791 FILE *f;\r
792\r
793 f = fopen(fname, "rb");\r
794 if (f) {\r
795 fclose(f);\r
796 return 1;\r
797 }\r
798 return 0;\r
799}\r
800\r
801char *emu_GetSaveFName(int load, int is_sram, int slot)\r
802{\r
803 static char saveFname[512];\r
804 char ext[16];\r
805\r
806 if (is_sram)\r
807 {\r
602133e1 808 romfname_ext(saveFname, (PicoAHW&1) ? "brm/" : "srm/", (PicoAHW&1) ? ".brm" : ".srm");\r
ea8c405f 809 if (load) {\r
810 if (try_ropen_file(saveFname)) return saveFname;\r
811 // try in current dir..\r
602133e1 812 romfname_ext(saveFname, NULL, (PicoAHW & PAHW_MCD) ? ".brm" : ".srm");\r
ea8c405f 813 if (try_ropen_file(saveFname)) return saveFname;\r
814 return NULL; // give up\r
815 }\r
816 }\r
817 else\r
818 {\r
819 ext[0] = 0;\r
820 if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);\r
821 strcat(ext, (currentConfig.EmuOpt & 8) ? ".mds.gz" : ".mds");\r
822\r
823 romfname_ext(saveFname, "mds/", ext);\r
824 if (load) {\r
825 if (try_ropen_file(saveFname)) return saveFname;\r
826 romfname_ext(saveFname, NULL, ext);\r
827 if (try_ropen_file(saveFname)) return saveFname;\r
828 if (currentConfig.EmuOpt & 8) {\r
829 ext[0] = 0;\r
830 if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);\r
831 strcat(ext, ".mds");\r
832\r
833 romfname_ext(saveFname, "mds/", ext);\r
834 if (try_ropen_file(saveFname)) return saveFname;\r
835 romfname_ext(saveFname, NULL, ext);\r
836 if (try_ropen_file(saveFname)) return saveFname;\r
837 }\r
838 return NULL;\r
839 }\r
840 }\r
841\r
842 return saveFname;\r
843}\r
844\r
845int emu_checkSaveFile(int slot)\r
846{\r
847 return emu_GetSaveFName(1, 0, slot) ? 1 : 0;\r
848}\r
849\r
850void emu_setSaveStateCbs(int gz)\r
851{\r
852 if (gz) {\r
853 areaRead = gzRead2;\r
854 areaWrite = gzWrite2;\r
855 areaEof = (areaeof *) gzeof;\r
856 areaSeek = (areaseek *) gzseek;\r
857 areaClose = (areaclose *) gzclose;\r
858 } else {\r
859 areaRead = (arearw *) fread;\r
860 areaWrite = (arearw *) fwrite;\r
861 areaEof = (areaeof *) feof;\r
862 areaSeek = (areaseek *) fseek;\r
863 areaClose = (areaclose *) fclose;\r
864 }\r
865}\r
866\r
867int emu_SaveLoadGame(int load, int sram)\r
868{\r
869 int ret = 0;\r
870 char *saveFname;\r
871\r
872 // make save filename\r
873 saveFname = emu_GetSaveFName(load, sram, state_slot);\r
874 if (saveFname == NULL) {\r
875 if (!sram) {\r
876 strcpy(noticeMsg, load ? "LOAD FAILED (missing file)" : "SAVE FAILED ");\r
877 emu_noticeMsgUpdated();\r
878 }\r
879 return -1;\r
880 }\r
881\r
882 lprintf("saveLoad (%i, %i): %s\n", load, sram, saveFname);\r
883\r
da42200b 884 if (sram)\r
885 {\r
ea8c405f 886 FILE *sramFile;\r
887 int sram_size;\r
888 unsigned char *sram_data;\r
889 int truncate = 1;\r
602133e1 890 if (PicoAHW & PAHW_MCD)\r
891 {\r
892 if (PicoOpt&POPT_EN_MCD_RAMCART) {\r
ea8c405f 893 sram_size = 0x12000;\r
894 sram_data = SRam.data;\r
895 if (sram_data)\r
896 memcpy32((int *)sram_data, (int *)Pico_mcd->bram, 0x2000/4);\r
897 } else {\r
898 sram_size = 0x2000;\r
899 sram_data = Pico_mcd->bram;\r
900 truncate = 0; // the .brm may contain RAM cart data after normal brm\r
901 }\r
902 } else {\r
903 sram_size = SRam.end-SRam.start+1;\r
904 if(Pico.m.sram_reg & 4) sram_size=0x2000;\r
905 sram_data = SRam.data;\r
906 }\r
907 if (!sram_data) return 0; // SRam forcefully disabled for this game\r
908\r
602133e1 909 if (load)\r
910 {\r
ea8c405f 911 sramFile = fopen(saveFname, "rb");\r
912 if(!sramFile) return -1;\r
913 fread(sram_data, 1, sram_size, sramFile);\r
914 fclose(sramFile);\r
602133e1 915 if ((PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_RAMCART))\r
ea8c405f 916 memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4);\r
917 } else {\r
918 // sram save needs some special processing\r
919 // see if we have anything to save\r
920 for (; sram_size > 0; sram_size--)\r
921 if (sram_data[sram_size-1]) break;\r
922\r
923 if (sram_size) {\r
924 sramFile = fopen(saveFname, truncate ? "wb" : "r+b");\r
925 if (!sramFile) sramFile = fopen(saveFname, "wb"); // retry\r
926 if (!sramFile) return -1;\r
927 ret = fwrite(sram_data, 1, sram_size, sramFile);\r
928 ret = (ret != sram_size) ? -1 : 0;\r
929 fclose(sramFile);\r
930#ifndef NO_SYNC\r
931 sync();\r
932#endif\r
933 }\r
934 }\r
935 return ret;\r
936 }\r
937 else\r
938 {\r
939 void *PmovFile = NULL;\r
da42200b 940 if (strcmp(saveFname + strlen(saveFname) - 3, ".gz") == 0)\r
941 {\r
ea8c405f 942 if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) {\r
943 emu_setSaveStateCbs(1);\r
da42200b 944 if (!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);\r
ea8c405f 945 }\r
946 }\r
947 else\r
948 {\r
949 if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) {\r
950 emu_setSaveStateCbs(0);\r
951 }\r
952 }\r
953 if(PmovFile) {\r
954 ret = PmovState(load ? 6 : 5, PmovFile);\r
955 areaClose(PmovFile);\r
956 PmovFile = 0;\r
957 if (load) Pico.m.dirtyPal=1;\r
958#ifndef NO_SYNC\r
959 else sync();\r
960#endif\r
961 }\r
962 else ret = -1;\r
963 if (!ret)\r
964 strcpy(noticeMsg, load ? "GAME LOADED " : "GAME SAVED ");\r
965 else\r
966 {\r
967 strcpy(noticeMsg, load ? "LOAD FAILED " : "SAVE FAILED ");\r
968 ret = -1;\r
969 }\r
970\r
971 emu_noticeMsgUpdated();\r
972 return ret;\r
973 }\r
974}\r
bdec53c9 975\r