1 // (c) Copyright 2006-2007 notaz, All rights reserved.
\r
2 // Free for non-commercial use.
\r
4 // For commercial use, separate licencing terms must be obtained.
\r
8 #include <ctype.h> // tolower
\r
16 #include "lprintf.h"
\r
19 #include <Pico/PicoInt.h>
\r
20 #include <Pico/Patch.h>
\r
21 #include <Pico/cd/cue.h>
\r
22 #include <zlib/zlib.h>
\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
31 #define SCREEN_BUFFER giz_screen
\r
33 #include "../psp/psp.h"
\r
34 #define SCREEN_WIDTH 512
\r
35 #define SCREEN_BUFFER psp_screen
\r
38 char *PicoConfigFile = "config.cfg";
\r
39 currentConfig_t currentConfig, defaultConfig;
\r
43 int config_slot = 0, config_slot_current = 0;
\r
44 char lastRomFile[512];
\r
45 int kb_combo_keys = 0, kb_combo_acts = 0; // keys and actions which need button combos
\r
47 unsigned char *movie_data = NULL;
\r
48 static int movie_size = 0;
\r
50 // provided by platform code:
\r
51 extern char romFileName[];
\r
52 extern void emu_noticeMsgUpdated(void);
\r
53 extern void emu_getMainDir(char *dst, int len);
\r
54 extern void emu_setDefaultConfig(void);
\r
55 extern void menu_romload_prepare(const char *rom_name);
\r
56 extern void menu_romload_end(void);
\r
60 static void strlwr_(char* string)
\r
62 while ( (*string++ = (char)tolower(*string)) );
\r
65 static int try_rfn_cut(void)
\r
70 p = romFileName + strlen(romFileName) - 1;
\r
71 for (; p > romFileName; p--)
\r
72 if (*p == '.') break;
\r
75 if((tmp = fopen(romFileName, "rb"))) {
\r
82 static void get_ext(char *file, char *ext)
\r
86 p = file + strlen(file) - 4;
\r
87 if (p < file) p = file;
\r
93 char *biosfiles_us[] = { "us_scd1_9210", "us_scd2_9306", "SegaCDBIOS9303" };
\r
94 char *biosfiles_eu[] = { "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303" };
\r
95 char *biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };
\r
97 int emu_findBios(int region, char **bios_file)
\r
99 static char bios_path[1024];
\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
117 for (i = 0; i < count; i++)
\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
125 bios_path[strlen(bios_path) - 4] = 0;
\r
126 strcat(bios_path, ".zip");
\r
127 f = fopen(bios_path, "rb");
\r
132 lprintf("using bios: %s\n", bios_path);
\r
134 if (bios_file) *bios_file = bios_path;
\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
144 /* check if the name begins with BIOS name */
\r
146 static int emu_isBios(const char *name)
\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
159 static unsigned char id_header[0x100];
\r
161 /* checks if romFileName points to valid MegaCD image
\r
162 * if so, checks for suitable BIOS */
\r
163 int emu_cdCheck(int *pregion)
\r
165 unsigned char buf[32];
\r
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
172 get_ext(romFileName, ext);
\r
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
181 cd_f = pm_open(fname);
\r
182 if (cue_data != NULL)
\r
183 cue_destroy(cue_data);
\r
185 if (!cd_f) return 0; // let the upper level handle this
\r
187 if (pm_read(buf, 32, cd_f) != 32) {
\r
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
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
203 if (type == CT_UNKNOWN) {
\r
208 pm_seek(cd_f, (type == CT_ISO) ? 0x100 : 0x110, SEEK_SET);
\r
209 pm_read(id_header, sizeof(id_header), cd_f);
\r
211 /* it seems we have a CD image here. Try to detect region now.. */
\r
212 pm_seek(cd_f, (type == CT_ISO) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);
\r
213 pm_read(buf, 1, cd_f);
\r
216 if (buf[0] == 0x64) region = 8; // EU
\r
217 if (buf[0] == 0xa1) region = 1; // JAP
\r
219 lprintf("detected %s Sega/Mega CD image with %s region\n",
\r
220 type == CT_BIN ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
\r
222 if (pregion != NULL) *pregion = region;
\r
227 static int extract_text(char *dest, unsigned char *src, int len, int swab)
\r
232 if (swab) swab = 1;
\r
234 for (i = len - 1; i >= 0; i--)
\r
236 if (src[i^swab] != ' ') break;
\r
240 for (i = 0; i < len; i++)
\r
242 unsigned char s = src[i^swab];
\r
243 if (s >= 0x20 && s < 0x7f && s != '#' && s != '|' &&
\r
244 s != '[' && s != ']' && s != '\\')
\r
250 sprintf(p, "\\%02x", s);
\r
258 char *emu_makeRomId(void)
\r
260 static char id_string[3+0x11+0x11+0x30+16];
\r
263 if (PicoAHW & PAHW_MCD) {
\r
264 strcpy(id_string, "CD|");
\r
267 else strcpy(id_string, "MD|");
\r
270 pos += extract_text(id_string + pos, id_header + 0x80, 0x0e, swab); // serial
\r
271 id_string[pos] = '|'; pos++;
\r
272 pos += extract_text(id_string + pos, id_header + 0xf0, 0x03, swab); // region
\r
273 id_string[pos] = '|'; pos++;
\r
274 pos += extract_text(id_string + pos, id_header + 0x50, 0x30, swab); // overseas name
\r
275 id_string[pos] = 0;
\r
280 int emu_ReloadRom(void)
\r
282 unsigned int rom_size = 0;
\r
283 char *used_rom_name = romFileName;
\r
284 unsigned char *rom_data = NULL;
\r
287 int ret, cd_state, cd_region, cfg_loaded = 0;
\r
289 lprintf("emu_ReloadRom(%s)\n", romFileName);
\r
291 get_ext(romFileName, ext);
\r
293 // detect wrong extensions
\r
294 if (!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz
\r
295 sprintf(menuErrorMsg, "Not a ROM selected.");
\r
301 // check for movie file
\r
306 if (!strcmp(ext, ".gmv"))
\r
308 // check for both gmv and rom
\r
310 FILE *movie_file = fopen(romFileName, "rb");
\r
312 sprintf(menuErrorMsg, "Failed to open movie.");
\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
323 movie_data = malloc(movie_size);
\r
324 if(movie_data == NULL) {
\r
325 sprintf(menuErrorMsg, "low memory.");
\r
326 fclose(movie_file);
\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
335 dummy = try_rfn_cut() || try_rfn_cut();
\r
337 sprintf(menuErrorMsg, "Could't find a ROM for movie.");
\r
340 get_ext(romFileName, ext);
\r
342 else if (!strcmp(ext, ".pat")) {
\r
344 PicoPatchLoad(romFileName);
\r
345 dummy = try_rfn_cut() || try_rfn_cut();
\r
347 sprintf(menuErrorMsg, "Could't find a ROM to patch.");
\r
350 get_ext(romFileName, ext);
\r
353 if ((PicoAHW & PAHW_MCD) && Pico_mcd != NULL)
\r
356 // check for MegaCD image
\r
357 cd_state = emu_cdCheck(&cd_region);
\r
358 if (cd_state != CIT_NOT_CD)
\r
360 PicoAHW |= PAHW_MCD;
\r
361 // valid CD image, check for BIOS..
\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
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
372 if (!emu_findBios(cd_region, &used_rom_name)) {
\r
374 PicoAHW &= ~PAHW_MCD;
\r
378 get_ext(used_rom_name, ext);
\r
382 if (PicoAHW & PAHW_MCD) Stop_CD();
\r
383 PicoAHW &= ~PAHW_MCD;
\r
386 rom = pm_open(used_rom_name);
\r
388 sprintf(menuErrorMsg, "Failed to open rom.");
\r
392 menu_romload_prepare(used_rom_name); // also CD load
\r
397 if ( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) {
\r
398 sprintf(menuErrorMsg, "PicoCartLoad() failed.");
\r
399 lprintf("%s\n", menuErrorMsg);
\r
401 menu_romload_end();
\r
406 // detect wrong files (Pico crashes on very small files), also see if ROM EP is good
\r
407 if (rom_size <= 0x200 || strncmp((char *)rom_data, "Pico", 4) == 0 ||
\r
408 ((*(unsigned char *)(rom_data+4)<<16)|(*(unsigned short *)(rom_data+6))) >= (int)rom_size) {
\r
409 if (rom_data) free(rom_data);
\r
410 sprintf(menuErrorMsg, "Not a ROM selected.");
\r
411 menu_romload_end();
\r
415 // load config for this ROM (do this before insert to get correct region)
\r
416 if (!(PicoAHW & PAHW_MCD))
\r
417 memcpy(id_header, rom_data + 0x100, sizeof(id_header));
\r
419 ret = emu_ReadConfig(1, 1);
\r
420 if (!ret) emu_ReadConfig(0, 1);
\r
423 lprintf("PicoCartInsert(%p, %d);\n", rom_data, rom_size);
\r
424 if (PicoCartInsert(rom_data, rom_size)) {
\r
425 sprintf(menuErrorMsg, "Failed to load ROM.");
\r
426 menu_romload_end();
\r
430 Pico.m.frame_count = 0;
\r
432 // insert CD if it was detected
\r
433 if (cd_state != CIT_NOT_CD) {
\r
434 ret = Insert_CD(romFileName, cd_state);
\r
436 sprintf(menuErrorMsg, "Insert_CD() failed, invalid CD image?");
\r
437 lprintf("%s\n", menuErrorMsg);
\r
438 menu_romload_end();
\r
443 menu_romload_end();
\r
446 PicoPatchPrepare();
\r
450 // additional movie stuff
\r
452 if(movie_data[0x14] == '6')
\r
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
456 if(movie_data[0xF] >= 'A') {
\r
457 if(movie_data[0x16] & 0x80) {
\r
458 PicoRegionOverride = 8;
\r
460 PicoRegionOverride = 4;
\r
463 // TODO: bits 6 & 5
\r
465 movie_data[0x18+30] = 0;
\r
466 sprintf(noticeMsg, "MOVIE: %s", (char *) &movie_data[0x18]);
\r
470 PicoOpt &= ~POPT_DIS_VDP_FIFO;
\r
472 strcpy(noticeMsg, "PAL SYSTEM / 50 FPS");
\r
474 strcpy(noticeMsg, "NTSC SYSTEM / 60 FPS");
\r
477 emu_noticeMsgUpdated();
\r
479 // load SRAM for this ROM
\r
480 if (currentConfig.EmuOpt & 1)
\r
481 emu_SaveLoadGame(1, 1);
\r
483 strncpy(lastRomFile, romFileName, sizeof(lastRomFile)-1);
\r
484 lastRomFile[sizeof(lastRomFile)-1] = 0;
\r
490 static void romfname_ext(char *dst, const char *prefix, const char *ext)
\r
493 int prefix_len = 0;
\r
495 // make save filename
\r
496 for (p = romFileName+strlen(romFileName)-1; p >= romFileName && *p != '/'; p--); p++;
\r
499 strcpy(dst, prefix);
\r
500 prefix_len = strlen(prefix);
\r
502 strncpy(dst + prefix_len, p, 511-prefix_len);
\r
504 if (dst[strlen(dst)-4] == '.') dst[strlen(dst)-4] = 0;
\r
505 if (ext) strcat(dst, ext);
\r
509 static void make_config_cfg(char *cfg)
\r
511 strncpy(cfg, PicoConfigFile, 511);
\r
512 if (config_slot != 0)
\r
514 char *p = strrchr(cfg, '.');
\r
515 if (p == NULL) p = cfg + strlen(cfg);
\r
516 sprintf(p, ".%i.cfg", config_slot);
\r
521 int emu_ReadConfig(int game, int no_defaults)
\r
530 emu_setDefaultConfig();
\r
531 make_config_cfg(cfg);
\r
532 ret = config_readsect(cfg, NULL);
\r
536 char *sect = emu_makeRomId();
\r
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
544 if (config_havesect(cfg, sect))
\r
546 // read user's config
\r
547 int vol = currentConfig.volume;
\r
548 emu_setDefaultConfig();
\r
549 ret = config_readsect(cfg, sect);
\r
550 currentConfig.volume = vol; // make vol global (bah)
\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
562 // fall back to old game specific cfg
\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
570 romfname_ext(cfg, NULL, ".pbcfg");
\r
571 f = fopen(cfg, "rb");
\r
575 fseek(f, 512, SEEK_SET); // skip unused lrom buffer
\r
576 bread = fread(¤tConfig, 1, sizeof(currentConfig), f);
\r
577 lprintf("emu_ReadConfig: %s %s\n", cfg, bread > 0 ? "(ok)" : "(failed)");
\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
587 // PicoCDBuffers = currentConfig.s_PicoCDBuffers; // ignore in this case
\r
592 lprintf("loaded cfg from sect \"%s\"\n", sect);
\r
596 // some sanity checks
\r
597 if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;
\r
599 if (currentConfig.gamma < -4 || currentConfig.gamma > 16) currentConfig.gamma = 0;
\r
600 if (currentConfig.gamma2 < 0 || currentConfig.gamma2 > 2) currentConfig.gamma2 = 0;
\r
602 if (currentConfig.gamma < 10 || currentConfig.gamma > 300) currentConfig.gamma = 100;
\r
604 if (currentConfig.volume < 0 || currentConfig.volume > 99) currentConfig.volume = 50;
\r
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
612 if (ret == 0) config_slot_current = config_slot;
\r
617 int emu_WriteConfig(int is_game)
\r
619 char cfg[512], *game_sect = NULL;
\r
620 int ret, write_lrom = 0;
\r
624 strncpy(cfg, PicoConfigFile, 511);
\r
625 if (config_slot != 0)
\r
627 char *p = strrchr(cfg, '.');
\r
628 if (p == NULL) p = cfg + strlen(cfg);
\r
629 sprintf(p, ".%i.cfg", config_slot);
\r
634 if (config_slot != 0)
\r
635 sprintf(cfg, "game.%i.cfg", config_slot);
\r
636 else strcpy(cfg, "game.cfg");
\r
637 game_sect = emu_makeRomId();
\r
638 lprintf("emu_WriteConfig: sect \"%s\"\n", game_sect);
\r
641 lprintf("emu_WriteConfig: %s ", cfg);
\r
642 ret = config_writesect(cfg, game_sect);
\r
643 if (write_lrom) config_writelrom(cfg);
\r
647 lprintf((ret == 0) ? "(ok)\n" : "(failed)\n");
\r
649 if (ret == 0) config_slot_current = config_slot;
\r
654 void emu_textOut8(int x, int y, const char *text)
\r
656 int i,l,len=strlen(text);
\r
657 unsigned char *screen = (unsigned char *)SCREEN_BUFFER + x + y*SCREEN_WIDTH;
\r
659 /* always using built-in font */
\r
660 for (i = 0; i < len; i++)
\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
678 void emu_textOut16(int x, int y, const char *text)
\r
680 int i,l,len=strlen(text);
\r
681 unsigned short *screen = (unsigned short *)SCREEN_BUFFER + x + y*SCREEN_WIDTH;
\r
683 for (i = 0; i < len; i++)
\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
702 #define MAX_COMBO_KEY 23
\r
704 #define MAX_COMBO_KEY 31
\r
707 void emu_findKeyBindCombos(void)
\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
715 int keyc = 0, keyc2 = 0;
\r
716 if (act == 16 || act == 17) continue; // player2 flag
\r
719 for (u = 0; u <= MAX_COMBO_KEY; u++)
\r
720 if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;
\r
724 for (u = 0; u <= MAX_COMBO_KEY; u++)
\r
725 if ((currentConfig.KeyBinds[u] & 0x30000) == 0 && // pl. 1
\r
726 (currentConfig.KeyBinds[u] & (1 << act))) keyc++;
\r
727 for (u = 0; u <= MAX_COMBO_KEY; u++)
\r
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
734 // loop again and mark those keys and actions as combo
\r
735 for (u = 0; u <= MAX_COMBO_KEY; u++)
\r
737 if (currentConfig.KeyBinds[u] & (1 << act)) {
\r
738 kb_combo_keys |= 1 << u;
\r
739 kb_combo_acts |= 1 << act;
\r
745 // printf("combo keys/acts: %08x %08x\n", kb_combo_keys, kb_combo_acts);
\r
749 void emu_updateMovie(void)
\r
751 int offs = Pico.m.frame_count*3 + 0x40;
\r
752 if (offs+3 > movie_size) {
\r
755 strcpy(noticeMsg, "END OF MOVIE.");
\r
756 lprintf("END OF MOVIE.\n");
\r
757 emu_noticeMsgUpdated();
\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
778 static size_t gzRead2(void *p, size_t _size, size_t _n, void *file)
\r
780 return gzread(file, p, _n);
\r
784 static size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)
\r
786 return gzwrite(file, p, _n);
\r
789 static int try_ropen_file(const char *fname)
\r
793 f = fopen(fname, "rb");
\r
801 char *emu_GetSaveFName(int load, int is_sram, int slot)
\r
803 static char saveFname[512];
\r
808 romfname_ext(saveFname, (PicoAHW&1) ? "brm/" : "srm/", (PicoAHW&1) ? ".brm" : ".srm");
\r
810 if (try_ropen_file(saveFname)) return saveFname;
\r
811 // try in current dir..
\r
812 romfname_ext(saveFname, NULL, (PicoAHW & PAHW_MCD) ? ".brm" : ".srm");
\r
813 if (try_ropen_file(saveFname)) return saveFname;
\r
814 return NULL; // give up
\r
820 if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);
\r
821 strcat(ext, (currentConfig.EmuOpt & 8) ? ".mds.gz" : ".mds");
\r
823 romfname_ext(saveFname, "mds/", ext);
\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
830 if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);
\r
831 strcat(ext, ".mds");
\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
845 int emu_checkSaveFile(int slot)
\r
847 return emu_GetSaveFName(1, 0, slot) ? 1 : 0;
\r
850 void emu_setSaveStateCbs(int 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
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
867 int emu_SaveLoadGame(int load, int sram)
\r
872 // make save filename
\r
873 saveFname = emu_GetSaveFName(load, sram, state_slot);
\r
874 if (saveFname == NULL) {
\r
876 strcpy(noticeMsg, load ? "LOAD FAILED (missing file)" : "SAVE FAILED ");
\r
877 emu_noticeMsgUpdated();
\r
882 lprintf("saveLoad (%i, %i): %s\n", load, sram, saveFname);
\r
888 unsigned char *sram_data;
\r
890 if (PicoAHW & PAHW_MCD)
\r
892 if (PicoOpt&POPT_EN_MCD_RAMCART) {
\r
893 sram_size = 0x12000;
\r
894 sram_data = SRam.data;
\r
896 memcpy32((int *)sram_data, (int *)Pico_mcd->bram, 0x2000/4);
\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
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
907 if (!sram_data) return 0; // SRam forcefully disabled for this game
\r
911 sramFile = fopen(saveFname, "rb");
\r
912 if(!sramFile) return -1;
\r
913 fread(sram_data, 1, sram_size, sramFile);
\r
915 if ((PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_RAMCART))
\r
916 memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4);
\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
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
939 void *PmovFile = NULL;
\r
940 if (strcmp(saveFname + strlen(saveFname) - 3, ".gz") == 0)
\r
942 if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) {
\r
943 emu_setSaveStateCbs(1);
\r
944 if (!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);
\r
949 if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) {
\r
950 emu_setSaveStateCbs(0);
\r
954 ret = PmovState(load ? 6 : 5, PmovFile);
\r
955 areaClose(PmovFile);
\r
957 if (load) Pico.m.dirtyPal=1;
\r
964 strcpy(noticeMsg, load ? "GAME LOADED " : "GAME SAVED ");
\r
967 strcpy(noticeMsg, load ? "LOAD FAILED " : "SAVE FAILED ");
\r
971 emu_noticeMsgUpdated();
\r