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
18 #include <Pico/PicoInt.h>
\r
19 #include <Pico/Patch.h>
\r
20 #include <zlib/zlib.h>
\r
22 #if defined(__GP2X__)
\r
23 #include "../gp2x/gp2x.h"
\r
24 #define SCREEN_WIDTH 320
\r
25 #define SCREEN_BUFFER gp2x_screen
\r
26 #elif defined(__GIZ__)
\r
27 #include "../gizmondo/giz.h"
\r
28 #define SCREEN_WIDTH 321
\r
29 #define SCREEN_BUFFER giz_screen
\r
31 #include "../psp/psp.h"
\r
32 #define SCREEN_WIDTH 512
\r
33 #define SCREEN_BUFFER psp_screen
\r
36 char *PicoConfigFile = "picoconfig.bin";
\r
37 currentConfig_t currentConfig;
\r
38 unsigned char *rom_data = NULL;
\r
41 int config_slot = 0, config_slot_current = 0;
\r
43 unsigned char *movie_data = NULL;
\r
44 static int movie_size = 0;
\r
46 // provided by platform code:
\r
47 extern char romFileName[];
\r
48 extern void emu_noticeMsgUpdated(void);
\r
49 extern void emu_getMainDir(char *dst, int len);
\r
50 extern void emu_setDefaultConfig(void);
\r
51 extern void menu_romload_prepare(const char *rom_name);
\r
52 extern void menu_romload_end(void);
\r
56 static void strlwr_(char* string)
\r
58 while ( (*string++ = (char)tolower(*string)) );
\r
61 static int try_rfn_cut(void)
\r
66 p = romFileName + strlen(romFileName) - 1;
\r
67 for (; p > romFileName; p--)
\r
68 if (*p == '.') break;
\r
71 if((tmp = fopen(romFileName, "rb"))) {
\r
78 static void get_ext(char *file, char *ext)
\r
82 p = file + strlen(file) - 4;
\r
83 if (p < file) p = file;
\r
89 char *biosfiles_us[] = { "us_scd2_9306", "SegaCDBIOS9303", "us_scd1_9210" };
\r
90 char *biosfiles_eu[] = { "eu_mcd2_9306", "eu_mcd2_9303", "eu_mcd1_9210" };
\r
91 char *biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };
\r
93 int emu_findBios(int region, char **bios_file)
\r
95 static char bios_path[1024];
\r
100 if (region == 4) { // US
\r
101 files = biosfiles_us;
\r
102 count = sizeof(biosfiles_us) / sizeof(char *);
\r
103 } else if (region == 8) { // EU
\r
104 files = biosfiles_eu;
\r
105 count = sizeof(biosfiles_eu) / sizeof(char *);
\r
106 } else if (region == 1 || region == 2) {
\r
107 files = biosfiles_jp;
\r
108 count = sizeof(biosfiles_jp) / sizeof(char *);
\r
113 for (i = 0; i < count; i++)
\r
115 emu_getMainDir(bios_path, sizeof(bios_path));
\r
116 strcat(bios_path, files[i]);
\r
117 strcat(bios_path, ".bin");
\r
118 f = fopen(bios_path, "rb");
\r
121 bios_path[strlen(bios_path) - 4] = 0;
\r
122 strcat(bios_path, ".zip");
\r
123 f = fopen(bios_path, "rb");
\r
128 lprintf("using bios: %s\n", bios_path);
\r
130 if (bios_file) *bios_file = bios_path;
\r
133 sprintf(menuErrorMsg, "no %s BIOS files found, read docs",
\r
134 region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
\r
135 lprintf("%s\n", menuErrorMsg);
\r
140 /* check if the name begins with BIOS name */
\r
141 static int emu_isBios(const char *name)
\r
144 for (i = 0; i < sizeof(biosfiles_us)/sizeof(biosfiles_us[0]); i++)
\r
145 if (strstr(name, biosfiles_us[i]) != NULL) return 1;
\r
146 for (i = 0; i < sizeof(biosfiles_eu)/sizeof(biosfiles_eu[0]); i++)
\r
147 if (strstr(name, biosfiles_eu[i]) != NULL) return 1;
\r
148 for (i = 0; i < sizeof(biosfiles_jp)/sizeof(biosfiles_jp[0]); i++)
\r
149 if (strstr(name, biosfiles_jp[i]) != NULL) return 1;
\r
153 /* checks if romFileName points to valid MegaCD image
\r
154 * if so, checks for suitable BIOS */
\r
155 int emu_cdCheck(int *pregion)
\r
157 unsigned char buf[32];
\r
159 int type = 0, region = 4; // 1: Japan, 4: US, 8: Europe
\r
161 cd_f = pm_open(romFileName);
\r
162 if (!cd_f) return 0; // let the upper level handle this
\r
164 if (pm_read(buf, 32, cd_f) != 32) {
\r
169 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) type = 1; // Sega CD (ISO)
\r
170 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) type = 2; // Sega CD (BIN)
\r
176 /* it seems we have a CD image here. Try to detect region now.. */
\r
177 pm_seek(cd_f, (type == 1) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);
\r
178 pm_read(buf, 1, cd_f);
\r
181 if (buf[0] == 0x64) region = 8; // EU
\r
182 if (buf[0] == 0xa1) region = 1; // JAP
\r
184 lprintf("detected %s Sega/Mega CD image with %s region\n",
\r
185 type == 2 ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
\r
187 if (pregion != NULL) *pregion = region;
\r
192 int emu_ReloadRom(void)
\r
194 unsigned int rom_size = 0;
\r
195 char *used_rom_name = romFileName;
\r
198 int ret, cd_state, cd_region, cfg_loaded = 0;
\r
200 lprintf("emu_ReloadRom(%s)\n", romFileName);
\r
202 get_ext(romFileName, ext);
\r
204 // detect wrong extensions
\r
205 if(!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz
\r
206 sprintf(menuErrorMsg, "Not a ROM selected.");
\r
212 // check for movie file
\r
217 if(!strcmp(ext, ".gmv")) {
\r
218 // check for both gmv and rom
\r
220 FILE *movie_file = fopen(romFileName, "rb");
\r
222 sprintf(menuErrorMsg, "Failed to open movie.");
\r
225 fseek(movie_file, 0, SEEK_END);
\r
226 movie_size = ftell(movie_file);
\r
227 fseek(movie_file, 0, SEEK_SET);
\r
228 if(movie_size < 64+3) {
\r
229 sprintf(menuErrorMsg, "Invalid GMV file.");
\r
230 fclose(movie_file);
\r
233 movie_data = malloc(movie_size);
\r
234 if(movie_data == NULL) {
\r
235 sprintf(menuErrorMsg, "low memory.");
\r
236 fclose(movie_file);
\r
239 fread(movie_data, 1, movie_size, movie_file);
\r
240 fclose(movie_file);
\r
241 if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {
\r
242 sprintf(menuErrorMsg, "Invalid GMV file.");
\r
245 dummy = try_rfn_cut() || try_rfn_cut();
\r
247 sprintf(menuErrorMsg, "Could't find a ROM for movie.");
\r
250 get_ext(romFileName, ext);
\r
252 else if (!strcmp(ext, ".pat")) {
\r
254 PicoPatchLoad(romFileName);
\r
255 dummy = try_rfn_cut() || try_rfn_cut();
\r
257 sprintf(menuErrorMsg, "Could't find a ROM to patch.");
\r
260 get_ext(romFileName, ext);
\r
263 if ((PicoMCD & 1) && Pico_mcd != NULL)
\r
266 // check for MegaCD image
\r
267 cd_state = emu_cdCheck(&cd_region);
\r
270 // valid CD image, check for BIOS..
\r
272 // we need to have config loaded at this point
\r
273 ret = emu_ReadConfig(1, 1);
\r
274 if (!ret) emu_ReadConfig(0, 1);
\r
277 if (PicoRegionOverride) {
\r
278 cd_region = PicoRegionOverride;
\r
279 lprintf("overrided region to %s\n", cd_region != 4 ? (cd_region == 8 ? "EU" : "JAP") : "USA");
\r
281 if (!emu_findBios(cd_region, &used_rom_name)) {
\r
287 get_ext(used_rom_name, ext);
\r
291 if (PicoMCD & 1) Stop_CD();
\r
295 rom = pm_open(used_rom_name);
\r
297 sprintf(menuErrorMsg, "Failed to open rom.");
\r
301 menu_romload_prepare(used_rom_name); // also CD load
\r
309 if( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) {
\r
310 sprintf(menuErrorMsg, "PicoCartLoad() failed.");
\r
311 lprintf("%s\n", menuErrorMsg);
\r
313 menu_romload_end();
\r
318 // detect wrong files (Pico crashes on very small files), also see if ROM EP is good
\r
319 if(rom_size <= 0x200 || strncmp((char *)rom_data, "Pico", 4) == 0 ||
\r
320 ((*(unsigned char *)(rom_data+4)<<16)|(*(unsigned short *)(rom_data+6))) >= (int)rom_size) {
\r
321 if (rom_data) free(rom_data);
\r
323 sprintf(menuErrorMsg, "Not a ROM selected.");
\r
324 menu_romload_end();
\r
328 // load config for this ROM (do this before insert to get correct region)
\r
330 ret = emu_ReadConfig(1, 1);
\r
331 if (!ret) emu_ReadConfig(0, 1);
\r
334 lprintf("PicoCartInsert(%p, %d);\n", rom_data, rom_size);
\r
335 if(PicoCartInsert(rom_data, rom_size)) {
\r
336 sprintf(menuErrorMsg, "Failed to load ROM.");
\r
337 menu_romload_end();
\r
341 Pico.m.frame_count = 0;
\r
343 // insert CD if it was detected
\r
344 if (cd_state > 0) {
\r
345 ret = Insert_CD(romFileName, cd_state == 2);
\r
347 sprintf(menuErrorMsg, "Insert_CD() failed, invalid CD image?");
\r
348 lprintf("%s\n", menuErrorMsg);
\r
349 menu_romload_end();
\r
354 menu_romload_end();
\r
356 if (!emu_isBios(romFileName))
\r
358 // emu_ReadConfig() might have messed currentConfig.lastRomFile
\r
359 strncpy(currentConfig.lastRomFile, romFileName, sizeof(currentConfig.lastRomFile)-1);
\r
360 currentConfig.lastRomFile[sizeof(currentConfig.lastRomFile)-1] = 0;
\r
364 PicoPatchPrepare();
\r
368 // additional movie stuff
\r
370 if(movie_data[0x14] == '6')
\r
371 PicoOpt |= 0x20; // 6 button pad
\r
372 else PicoOpt &= ~0x20;
\r
373 PicoOpt |= 0x10040; // accurate timing, no VDP fifo timing
\r
374 if(movie_data[0xF] >= 'A') {
\r
375 if(movie_data[0x16] & 0x80) {
\r
376 PicoRegionOverride = 8;
\r
378 PicoRegionOverride = 4;
\r
381 // TODO: bits 6 & 5
\r
383 movie_data[0x18+30] = 0;
\r
384 sprintf(noticeMsg, "MOVIE: %s", (char *) &movie_data[0x18]);
\r
388 PicoOpt &= ~0x10000;
\r
390 strcpy(noticeMsg, "PAL SYSTEM / 50 FPS");
\r
392 strcpy(noticeMsg, "NTSC SYSTEM / 60 FPS");
\r
395 emu_noticeMsgUpdated();
\r
397 // load SRAM for this ROM
\r
398 if(currentConfig.EmuOpt & 1)
\r
399 emu_SaveLoadGame(1, 1);
\r
405 static void romfname_ext(char *dst, const char *prefix, const char *ext)
\r
408 int prefix_len = 0;
\r
410 // make save filename
\r
411 for (p = romFileName+strlen(romFileName)-1; p >= romFileName && *p != '/'; p--); p++;
\r
414 strcpy(dst, prefix);
\r
415 prefix_len = strlen(prefix);
\r
417 strncpy(dst + prefix_len, p, 511-prefix_len);
\r
419 if (dst[strlen(dst)-4] == '.') dst[strlen(dst)-4] = 0;
\r
420 if (ext) strcat(dst, ext);
\r
424 int emu_ReadConfig(int game, int no_defaults)
\r
427 char cfg[512], extbuf[16];
\r
434 emu_setDefaultConfig();
\r
436 strncpy(cfg, PicoConfigFile, 511);
\r
437 if (config_slot != 0)
\r
439 char *p = strrchr(cfg, '.');
\r
440 if (p == NULL) p = cfg + strlen(cfg);
\r
441 sprintf(extbuf, ".%i.pbcfg", config_slot);
\r
442 strncpy(p, extbuf, 511 - (p - cfg));
\r
446 if (config_slot != 0)
\r
447 sprintf(extbuf, ".%i.pbcfg", config_slot);
\r
448 else strcpy(extbuf, ".pbcfg");
\r
449 romfname_ext(cfg, "cfg/", extbuf);
\r
450 f = fopen(cfg, "rb");
\r
451 if (!f) romfname_ext(cfg, NULL, ".pbcfg");
\r
455 lprintf("emu_ReadConfig: %s ", cfg);
\r
456 f = fopen(cfg, "rb");
\r
458 bread = fread(¤tConfig, 1, sizeof(currentConfig), f);
\r
461 lprintf(bread > 0 ? "(ok)\n" : "(failed)\n");
\r
463 PicoOpt = currentConfig.PicoOpt;
\r
464 PsndRate = currentConfig.PsndRate;
\r
465 PicoRegionOverride = currentConfig.PicoRegion;
\r
466 PicoAutoRgnOrder = currentConfig.PicoAutoRgnOrder;
\r
467 PicoCDBuffers = currentConfig.PicoCDBuffers;
\r
468 //scaling_update();
\r
469 // some sanity checks
\r
470 if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;
\r
472 if (currentConfig.gamma < -4 || currentConfig.gamma > 16) currentConfig.gamma = 0;
\r
474 if (currentConfig.gamma < 10 || currentConfig.gamma > 300) currentConfig.gamma = 100;
\r
476 if (currentConfig.volume < 0 || currentConfig.volume > 99) currentConfig.volume = 50;
\r
478 // if volume keys are unbound, bind them to volume control
\r
479 if (!currentConfig.KeyBinds[23] && !currentConfig.KeyBinds[22]) {
\r
480 currentConfig.KeyBinds[23] = 1<<29; // vol up
\r
481 currentConfig.KeyBinds[22] = 1<<30; // vol down
\r
484 if (bread > 0) config_slot_current = config_slot;
\r
485 return (bread > 0); // == sizeof(currentConfig));
\r
489 int emu_WriteConfig(int game)
\r
492 char cfg[512], extbuf[16];
\r
497 strncpy(cfg, PicoConfigFile, 511);
\r
498 if (config_slot != 0)
\r
500 char *p = strrchr(cfg, '.');
\r
501 if (p == NULL) p = cfg + strlen(cfg);
\r
502 sprintf(extbuf, ".%i.pbcfg", config_slot);
\r
503 strncpy(p, extbuf, 511 - (p - cfg));
\r
507 if (config_slot != 0)
\r
508 sprintf(extbuf, ".%i.pbcfg", config_slot);
\r
509 else strcpy(extbuf, ".pbcfg");
\r
510 romfname_ext(cfg, "cfg/", extbuf);
\r
513 lprintf("emu_WriteConfig: %s ", cfg);
\r
514 f = fopen(cfg, "wb");
\r
516 currentConfig.PicoOpt = PicoOpt;
\r
517 currentConfig.PsndRate = PsndRate;
\r
518 currentConfig.PicoRegion = PicoRegionOverride;
\r
519 currentConfig.PicoAutoRgnOrder = PicoAutoRgnOrder;
\r
520 currentConfig.PicoCDBuffers = PicoCDBuffers;
\r
521 bwrite = fwrite(¤tConfig, 1, sizeof(currentConfig), f);
\r
528 lprintf((bwrite == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n");
\r
530 if (bwrite == sizeof(currentConfig)) config_slot_current = config_slot;
\r
531 return (bwrite == sizeof(currentConfig));
\r
535 void emu_textOut8(int x, int y, const char *text)
\r
537 int i,l,len=strlen(text);
\r
538 unsigned char *screen = (unsigned char *)SCREEN_BUFFER + x + y*SCREEN_WIDTH;
\r
540 /* always using built-in font */
\r
541 for (i = 0; i < len; i++)
\r
545 unsigned char fd = fontdata8x8[((text[i])*8)+l];
\r
546 if (fd&0x80) screen[l*SCREEN_WIDTH+0]=0xf0;
\r
547 if (fd&0x40) screen[l*SCREEN_WIDTH+1]=0xf0;
\r
548 if (fd&0x20) screen[l*SCREEN_WIDTH+2]=0xf0;
\r
549 if (fd&0x10) screen[l*SCREEN_WIDTH+3]=0xf0;
\r
550 if (fd&0x08) screen[l*SCREEN_WIDTH+4]=0xf0;
\r
551 if (fd&0x04) screen[l*SCREEN_WIDTH+5]=0xf0;
\r
552 if (fd&0x02) screen[l*SCREEN_WIDTH+6]=0xf0;
\r
553 if (fd&0x01) screen[l*SCREEN_WIDTH+7]=0xf0;
\r
559 void emu_textOut16(int x, int y, const char *text)
\r
561 int i,l,len=strlen(text);
\r
562 unsigned short *screen = (unsigned short *)SCREEN_BUFFER + x + y*SCREEN_WIDTH;
\r
564 for (i = 0; i < len; i++)
\r
568 unsigned char fd = fontdata8x8[((text[i])*8)+l];
\r
569 if(fd&0x80) screen[l*SCREEN_WIDTH+0]=0xffff;
\r
570 if(fd&0x40) screen[l*SCREEN_WIDTH+1]=0xffff;
\r
571 if(fd&0x20) screen[l*SCREEN_WIDTH+2]=0xffff;
\r
572 if(fd&0x10) screen[l*SCREEN_WIDTH+3]=0xffff;
\r
573 if(fd&0x08) screen[l*SCREEN_WIDTH+4]=0xffff;
\r
574 if(fd&0x04) screen[l*SCREEN_WIDTH+5]=0xffff;
\r
575 if(fd&0x02) screen[l*SCREEN_WIDTH+6]=0xffff;
\r
576 if(fd&0x01) screen[l*SCREEN_WIDTH+7]=0xffff;
\r
583 void emu_updateMovie(void)
\r
585 int offs = Pico.m.frame_count*3 + 0x40;
\r
586 if (offs+3 > movie_size) {
\r
589 strcpy(noticeMsg, "END OF MOVIE.");
\r
590 lprintf("END OF MOVIE.\n");
\r
591 emu_noticeMsgUpdated();
\r
594 PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU
\r
595 if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // A
\r
596 if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // B
\r
597 if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // A
\r
598 PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU
\r
599 if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // A
\r
600 if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // B
\r
601 if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // A
\r
602 PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX
\r
603 if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X
\r
604 if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z
\r
605 PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX
\r
606 if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X
\r
607 if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z
\r
612 static size_t gzRead2(void *p, size_t _size, size_t _n, void *file)
\r
614 return gzread(file, p, _n);
\r
618 static size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)
\r
620 return gzwrite(file, p, _n);
\r
623 static int try_ropen_file(const char *fname)
\r
627 f = fopen(fname, "rb");
\r
635 char *emu_GetSaveFName(int load, int is_sram, int slot)
\r
637 static char saveFname[512];
\r
642 romfname_ext(saveFname, (PicoMCD&1) ? "brm/" : "srm/", (PicoMCD&1) ? ".brm" : ".srm");
\r
644 if (try_ropen_file(saveFname)) return saveFname;
\r
645 // try in current dir..
\r
646 romfname_ext(saveFname, NULL, (PicoMCD&1) ? ".brm" : ".srm");
\r
647 if (try_ropen_file(saveFname)) return saveFname;
\r
648 return NULL; // give up
\r
654 if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);
\r
655 strcat(ext, (currentConfig.EmuOpt & 8) ? ".mds.gz" : ".mds");
\r
657 romfname_ext(saveFname, "mds/", ext);
\r
659 if (try_ropen_file(saveFname)) return saveFname;
\r
660 romfname_ext(saveFname, NULL, ext);
\r
661 if (try_ropen_file(saveFname)) return saveFname;
\r
662 if (currentConfig.EmuOpt & 8) {
\r
664 if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);
\r
665 strcat(ext, ".mds");
\r
667 romfname_ext(saveFname, "mds/", ext);
\r
668 if (try_ropen_file(saveFname)) return saveFname;
\r
669 romfname_ext(saveFname, NULL, ext);
\r
670 if (try_ropen_file(saveFname)) return saveFname;
\r
679 int emu_checkSaveFile(int slot)
\r
681 return emu_GetSaveFName(1, 0, slot) ? 1 : 0;
\r
684 void emu_setSaveStateCbs(int gz)
\r
687 areaRead = gzRead2;
\r
688 areaWrite = gzWrite2;
\r
689 areaEof = (areaeof *) gzeof;
\r
690 areaSeek = (areaseek *) gzseek;
\r
691 areaClose = (areaclose *) gzclose;
\r
693 areaRead = (arearw *) fread;
\r
694 areaWrite = (arearw *) fwrite;
\r
695 areaEof = (areaeof *) feof;
\r
696 areaSeek = (areaseek *) fseek;
\r
697 areaClose = (areaclose *) fclose;
\r
701 int emu_SaveLoadGame(int load, int sram)
\r
706 // make save filename
\r
707 saveFname = emu_GetSaveFName(load, sram, state_slot);
\r
708 if (saveFname == NULL) {
\r
710 strcpy(noticeMsg, load ? "LOAD FAILED (missing file)" : "SAVE FAILED ");
\r
711 emu_noticeMsgUpdated();
\r
716 lprintf("saveLoad (%i, %i): %s\n", load, sram, saveFname);
\r
722 unsigned char *sram_data;
\r
725 if (PicoOpt&0x8000) { // MCD RAM cart?
\r
726 sram_size = 0x12000;
\r
727 sram_data = SRam.data;
\r
729 memcpy32((int *)sram_data, (int *)Pico_mcd->bram, 0x2000/4);
\r
731 sram_size = 0x2000;
\r
732 sram_data = Pico_mcd->bram;
\r
733 truncate = 0; // the .brm may contain RAM cart data after normal brm
\r
736 sram_size = SRam.end-SRam.start+1;
\r
737 if(Pico.m.sram_reg & 4) sram_size=0x2000;
\r
738 sram_data = SRam.data;
\r
740 if (!sram_data) return 0; // SRam forcefully disabled for this game
\r
743 sramFile = fopen(saveFname, "rb");
\r
744 if(!sramFile) return -1;
\r
745 fread(sram_data, 1, sram_size, sramFile);
\r
747 if ((PicoMCD&1) && (PicoOpt&0x8000))
\r
748 memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4);
\r
750 // sram save needs some special processing
\r
751 // see if we have anything to save
\r
752 for (; sram_size > 0; sram_size--)
\r
753 if (sram_data[sram_size-1]) break;
\r
756 sramFile = fopen(saveFname, truncate ? "wb" : "r+b");
\r
757 if (!sramFile) sramFile = fopen(saveFname, "wb"); // retry
\r
758 if (!sramFile) return -1;
\r
759 ret = fwrite(sram_data, 1, sram_size, sramFile);
\r
760 ret = (ret != sram_size) ? -1 : 0;
\r
771 void *PmovFile = NULL;
\r
772 if (strcmp(saveFname + strlen(saveFname) - 3, ".gz") == 0)
\r
774 if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) {
\r
775 emu_setSaveStateCbs(1);
\r
776 if (!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);
\r
781 if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) {
\r
782 emu_setSaveStateCbs(0);
\r
786 ret = PmovState(load ? 6 : 5, PmovFile);
\r
787 areaClose(PmovFile);
\r
789 if (load) Pico.m.dirtyPal=1;
\r
796 strcpy(noticeMsg, load ? "GAME LOADED " : "GAME SAVED ");
\r
799 strcpy(noticeMsg, load ? "LOAD FAILED " : "SAVE FAILED ");
\r
803 emu_noticeMsgUpdated();
\r