1 // (c) Copyright 2006 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 <sys/time.h>
\r
9 #include <sys/stat.h>
\r
10 #include <sys/types.h>
\r
11 #include <linux/limits.h>
\r
21 #include "asmutils.h"
\r
22 #include "cpuctrl.h"
\r
24 #include <Pico/PicoInt.h>
\r
25 #include <Pico/Patch.h>
\r
26 #include <zlib/zlib.h>
\r
30 #define OSD_FPS_X 220
\r
32 #define OSD_FPS_X 260
\r
35 // PicoPad[] format: SACB RLDU
\r
36 char *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
45 int select_exits = 0;
\r
46 char *PicoConfigFile = "picoconfig.bin";
\r
47 currentConfig_t currentConfig;
\r
49 char romFileName[PATH_MAX];
\r
50 unsigned char *rom_data = NULL;
\r
52 extern int crashed_940;
\r
54 static short sndBuffer[2*44100/50];
\r
55 static char noticeMsg[64]; // notice msg to draw
\r
56 static struct timeval noticeMsgTime = { 0, 0 }; // when started showing
\r
57 static int reset_timing, osd_fps_x;
\r
58 static int combo_keys = 0, combo_acts = 0; // keys and actions which need button combos
\r
59 static int gp2x_old_gamma = 100;
\r
60 static unsigned char *movie_data = NULL;
\r
61 static int movie_size = 0;
\r
62 unsigned char *framebuff = 0; // temporary buffer for alt renderer
\r
67 static FILE *logf = NULL;
\r
69 void pprintf(char *texto, ...)
\r
73 va_start(args,texto);
\r
74 vfprintf(logf,texto,args);
\r
81 static void strlwr(char* string)
\r
83 while ( (*string++ = (char)tolower(*string)) );
\r
86 static int try_rfn_cut(void)
\r
91 p = romFileName + strlen(romFileName) - 1;
\r
92 for (; p > romFileName; p--)
\r
93 if (*p == '.') break;
\r
96 if((tmp = fopen(romFileName, "rb"))) {
\r
103 static void get_ext(char *file, char *ext)
\r
107 p = file + strlen(file) - 4;
\r
108 if (p < file) p = file;
\r
109 strncpy(ext, p, 4);
\r
114 char *biosfiles_us[] = { "us_scd2_9306", "SegaCDBIOS9303", "us_scd1_9210" };
\r
115 char *biosfiles_eu[] = { "eu_mcd2_9306", "eu_mcd2_9303", "eu_mcd1_9210" };
\r
116 char *biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };
\r
118 extern char **g_argv;
\r
120 int find_bios(int region, char **bios_file)
\r
122 static char bios_path[1024];
\r
127 if (region == 4) { // US
\r
128 files = biosfiles_us;
\r
129 count = sizeof(biosfiles_us) / sizeof(char *);
\r
130 } else if (region == 8) { // EU
\r
131 files = biosfiles_eu;
\r
132 count = sizeof(biosfiles_eu) / sizeof(char *);
\r
133 } else if (region == 1 || region == 2) {
\r
134 files = biosfiles_jp;
\r
135 count = sizeof(biosfiles_jp) / sizeof(char *);
\r
140 for (i = 0; i < count; i++)
\r
142 strncpy(bios_path, g_argv[0], 1023);
\r
143 bios_path[1024-32] = 0;
\r
144 for (j = strlen(bios_path); j > 0; j--)
\r
145 if (bios_path[j] == '/') { bios_path[j+1] = 0; break; }
\r
146 strcat(bios_path, files[i]);
\r
147 strcat(bios_path, ".bin");
\r
148 f = fopen(bios_path, "rb");
\r
151 bios_path[strlen(bios_path) - 4] = 0;
\r
152 strcat(bios_path, ".zip");
\r
153 f = fopen(bios_path, "rb");
\r
158 printf("using bios: %s\n", bios_path);
\r
160 if (bios_file) *bios_file = bios_path;
\r
163 sprintf(menuErrorMsg, "no %s BIOS files found, read docs",
\r
164 region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
\r
165 printf("%s\n", menuErrorMsg);
\r
170 /* checks if romFileName points to valid MegaCD image
\r
171 * if so, checks for suitable BIOS */
\r
172 static int cd_check(char *ext, char **bios_file)
\r
174 unsigned char buf[32];
\r
176 int type = 0, region = 4; // 1: Japan, 4: US, 8: Europe
\r
178 cd_f = fopen(romFileName, "rb");
\r
179 if (!cd_f) return 0; // let the upper level handle this
\r
181 if (fread(buf, 1, 32, cd_f) != 32) {
\r
186 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) type = 1; // Sega CD (ISO)
\r
187 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) type = 2; // Sega CD (BIN)
\r
193 /* it seems we have a CD image here. Try to detect region and load a suitable BIOS now.. */
\r
194 fseek(cd_f, (type == 1) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);
\r
195 fread(buf, 1, 1, cd_f);
\r
198 if (buf[0] == 0x64) region = 8; // EU
\r
199 if (buf[0] == 0xa1) region = 1; // JAP
\r
201 printf("detected %s Sega/Mega CD image with %s region\n",
\r
202 type == 2 ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
\r
204 if (PicoRegionOverride) {
\r
205 region = PicoRegionOverride;
\r
206 printf("overrided region to %s\n", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
\r
209 if(find_bios(region, bios_file))
\r
210 return type; // CD and BIOS detected
\r
212 return -1; // CD detected but load failed
\r
215 int emu_ReloadRom(void)
\r
217 unsigned int rom_size = 0;
\r
218 char *used_rom_name = romFileName;
\r
223 printf("emu_ReloadRom(%s)\n", romFileName);
\r
225 get_ext(romFileName, ext);
\r
227 // detect wrong extensions
\r
228 if(!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz
\r
229 sprintf(menuErrorMsg, "Not a ROM selected.");
\r
235 // check for movie file
\r
240 if(!strcmp(ext, ".gmv")) {
\r
241 // check for both gmv and rom
\r
243 FILE *movie_file = fopen(romFileName, "rb");
\r
245 sprintf(menuErrorMsg, "Failed to open movie.");
\r
248 fseek(movie_file, 0, SEEK_END);
\r
249 movie_size = ftell(movie_file);
\r
250 fseek(movie_file, 0, SEEK_SET);
\r
251 if(movie_size < 64+3) {
\r
252 sprintf(menuErrorMsg, "Invalid GMV file.");
\r
253 fclose(movie_file);
\r
256 movie_data = malloc(movie_size);
\r
257 if(movie_data == NULL) {
\r
258 sprintf(menuErrorMsg, "low memory.");
\r
259 fclose(movie_file);
\r
262 fread(movie_data, 1, movie_size, movie_file);
\r
263 fclose(movie_file);
\r
264 if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {
\r
265 sprintf(menuErrorMsg, "Invalid GMV file.");
\r
268 dummy = try_rfn_cut() || try_rfn_cut();
\r
270 sprintf(menuErrorMsg, "Could't find a ROM for movie.");
\r
273 get_ext(romFileName, ext);
\r
275 else if (!strcmp(ext, ".pat")) {
\r
277 PicoPatchLoad(romFileName);
\r
278 dummy = try_rfn_cut() || try_rfn_cut();
\r
280 sprintf(menuErrorMsg, "Could't find a ROM to patch.");
\r
283 get_ext(romFileName, ext);
\r
286 // check for MegaCD image
\r
287 cd_state = cd_check(ext, &used_rom_name);
\r
288 if (cd_state > 0) {
\r
290 get_ext(used_rom_name, ext);
\r
291 } else if (cd_state == -1) {
\r
295 if (PicoMCD & 1) PicoExitMCD();
\r
299 rom = fopen(used_rom_name, "rb");
\r
301 sprintf(menuErrorMsg, "Failed to open rom.");
\r
312 if(!strcasecmp(ext, ".zip")) {
\r
314 ret = CartLoadZip(used_rom_name, &rom_data, &rom_size);
\r
316 if (ret == 4) strcpy(menuErrorMsg, "No ROMs found in zip.");
\r
317 else sprintf(menuErrorMsg, "Unzip failed with code %i", ret);
\r
318 printf("%s\n", menuErrorMsg);
\r
322 if( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) {
\r
323 sprintf(menuErrorMsg, "PicoCartLoad() failed.");
\r
324 printf("%s\n", menuErrorMsg);
\r
331 // detect wrong files (Pico crashes on very small files), also see if ROM EP is good
\r
332 if(rom_size <= 0x200 || strncmp((char *)rom_data, "Pico", 4) == 0 ||
\r
333 ((*(unsigned short *)(rom_data+4)<<16)|(*(unsigned short *)(rom_data+6))) >= (int)rom_size) {
\r
334 if (rom_data) free(rom_data);
\r
336 sprintf(menuErrorMsg, "Not a ROM selected.");
\r
340 // load config for this ROM (do this before insert to get correct region)
\r
341 ret = emu_ReadConfig(1);
\r
345 printf("PicoCartInsert(%p, %d);\n", rom_data, rom_size);
\r
346 if(PicoCartInsert(rom_data, rom_size)) {
\r
347 sprintf(menuErrorMsg, "Failed to load ROM.");
\r
351 Pico.m.frame_count = 0;
\r
353 // insert CD if it was detected
\r
354 if (cd_state > 0) {
\r
355 ret = Insert_CD(romFileName, cd_state == 2);
\r
357 sprintf(menuErrorMsg, "Insert_CD() failed, invalid CD image?");
\r
358 printf("%s\n", menuErrorMsg);
\r
363 // emu_ReadConfig() might have messed currentConfig.lastRomFile
\r
364 strncpy(currentConfig.lastRomFile, romFileName, sizeof(currentConfig.lastRomFile)-1);
\r
365 currentConfig.lastRomFile[sizeof(currentConfig.lastRomFile)-1] = 0;
\r
368 PicoPatchPrepare();
\r
372 // additional movie stuff
\r
374 if(movie_data[0x14] == '6')
\r
375 PicoOpt |= 0x20; // 6 button pad
\r
376 else PicoOpt &= ~0x20;
\r
377 PicoOpt |= 0x40; // accurate timing
\r
378 if(movie_data[0xF] >= 'A') {
\r
379 if(movie_data[0x16] & 0x80) {
\r
380 PicoRegionOverride = 8;
\r
382 PicoRegionOverride = 4;
\r
385 // TODO: bits 6 & 5
\r
387 movie_data[0x18+30] = 0;
\r
388 sprintf(noticeMsg, "MOVIE: %s", (char *) &movie_data[0x18]);
\r
393 strcpy(noticeMsg, "PAL SYSTEM / 50 FPS");
\r
395 strcpy(noticeMsg, "NTSC SYSTEM / 60 FPS");
\r
398 gettimeofday(¬iceMsgTime, 0);
\r
400 // load SRAM for this ROM
\r
401 if(currentConfig.EmuOpt & 1)
\r
402 emu_SaveLoadGame(1, 1);
\r
408 void emu_Init(void)
\r
410 // make temp buffer for alt renderer
\r
411 framebuff = malloc((8+320)*(8+240+8));
\r
414 printf("framebuff == 0\n");
\r
417 // make dirs for saves, cfgs, etc.
\r
418 mkdir("mds", 0777);
\r
419 mkdir("srm", 0777);
\r
420 mkdir("brm", 0777);
\r
421 mkdir("cfg", 0777);
\r
425 // logf = fopen("log.txt", "w");
\r
429 static void romfname_ext(char *dst, const char *prefix, const char *ext)
\r
432 int prefix_len = 0;
\r
434 // make save filename
\r
435 for (p = romFileName+strlen(romFileName)-1; p >= romFileName && *p != '/'; p--); p++;
\r
438 strcpy(dst, prefix);
\r
439 prefix_len = strlen(prefix);
\r
441 strncpy(dst + prefix_len, p, 511-prefix_len);
\r
443 if (dst[strlen(dst)-4] == '.') dst[strlen(dst)-4] = 0;
\r
444 if (ext) strcat(dst, ext);
\r
448 static void find_combos(void)
\r
452 // find out which keys and actions are combos
\r
453 combo_keys = combo_acts = 0;
\r
454 for (act = 0; act < 32; act++)
\r
457 if (act == 16) continue; // player2 flag
\r
458 for (u = 0; u < 32; u++)
\r
460 if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;
\r
464 // loop again and mark those keys and actions as combo
\r
465 for (u = 0; u < 32; u++)
\r
467 if (currentConfig.KeyBinds[u] & (1 << act)) {
\r
468 combo_keys |= 1 << u;
\r
469 combo_acts |= 1 << act;
\r
474 // printf("combo keys/acts: %08x %08x\n", combo_keys, combo_acts);
\r
478 int emu_ReadConfig(int game)
\r
486 // set default config
\r
487 memset(¤tConfig, 0, sizeof(currentConfig));
\r
488 currentConfig.lastRomFile[0] = 0;
\r
489 currentConfig.EmuOpt = 0x1f | 0x400; // | cd_leds
\r
490 currentConfig.PicoOpt = 0x0f | 0xe00; // | use_940 | cd_pcm | cd_cdda
\r
491 currentConfig.PsndRate = 22050; // 44100;
\r
492 currentConfig.PicoRegion = 0; // auto
\r
493 currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP
\r
494 currentConfig.Frameskip = -1; // auto
\r
495 currentConfig.CPUclock = 200;
\r
496 currentConfig.volume = 50;
\r
497 currentConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU
\r
498 currentConfig.KeyBinds[ 4] = 1<<1;
\r
499 currentConfig.KeyBinds[ 2] = 1<<2;
\r
500 currentConfig.KeyBinds[ 6] = 1<<3;
\r
501 currentConfig.KeyBinds[14] = 1<<4;
\r
502 currentConfig.KeyBinds[13] = 1<<5;
\r
503 currentConfig.KeyBinds[12] = 1<<6;
\r
504 currentConfig.KeyBinds[ 8] = 1<<7;
\r
505 currentConfig.KeyBinds[15] = 1<<26; // switch rend
\r
506 currentConfig.KeyBinds[10] = 1<<27; // save state
\r
507 currentConfig.KeyBinds[11] = 1<<28; // load state
\r
508 currentConfig.KeyBinds[23] = 1<<29; // vol up
\r
509 currentConfig.KeyBinds[22] = 1<<30; // vol down
\r
510 currentConfig.gamma = 100;
\r
511 strncpy(cfg, PicoConfigFile, 511);
\r
514 romfname_ext(cfg, "cfg/", ".pbcfg");
\r
515 f = fopen(cfg, "rb");
\r
516 if (!f) romfname_ext(cfg, NULL, ".pbcfg");
\r
520 printf("emu_ReadConfig: %s ", cfg);
\r
521 f = fopen(cfg, "rb");
\r
523 bread = fread(¤tConfig, 1, sizeof(currentConfig), f);
\r
526 printf((bread == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n");
\r
528 PicoOpt = currentConfig.PicoOpt;
\r
529 PsndRate = currentConfig.PsndRate;
\r
530 PicoRegionOverride = currentConfig.PicoRegion;
\r
531 PicoAutoRgnOrder = currentConfig.PicoAutoRgnOrder;
\r
532 if (PicoOpt & 0x20) {
\r
533 actionNames[ 8] = "Z"; actionNames[ 9] = "Y";
\r
534 actionNames[10] = "X"; actionNames[11] = "MODE";
\r
536 // some sanity checks
\r
537 if (currentConfig.CPUclock < 1 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;
\r
538 if (currentConfig.gamma < 10 || currentConfig.gamma > 300) currentConfig.gamma = 100;
\r
539 // if volume keys are unbound, bind them to volume control
\r
540 if (!currentConfig.KeyBinds[23] && !currentConfig.KeyBinds[22]) {
\r
541 currentConfig.KeyBinds[23] = 1<<29; // vol up
\r
542 currentConfig.KeyBinds[22] = 1<<30; // vol down
\r
545 return (bread == sizeof(currentConfig));
\r
549 int emu_WriteConfig(int game)
\r
557 strncpy(cfg, PicoConfigFile, 511);
\r
560 romfname_ext(cfg, "cfg", ".pbcfg");
\r
563 printf("emu_WriteConfig: %s ", cfg);
\r
564 f = fopen(cfg, "wb");
\r
566 currentConfig.PicoOpt = PicoOpt;
\r
567 currentConfig.PsndRate = PsndRate;
\r
568 currentConfig.PicoRegion = PicoRegionOverride;
\r
569 currentConfig.PicoAutoRgnOrder = PicoAutoRgnOrder;
\r
570 bwrite = fwrite(¤tConfig, 1, sizeof(currentConfig), f);
\r
575 printf((bwrite == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n");
\r
577 return (bwrite == sizeof(currentConfig));
\r
581 void emu_Deinit(void)
\r
584 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
585 emu_SaveLoadGame(0, 1);
\r
589 if (!(currentConfig.EmuOpt & 0x20))
\r
590 emu_WriteConfig(0);
\r
597 if (gp2x_old_gamma != 100)
\r
602 void osd_text(int x, int y, const char *text)
\r
604 int len = strlen(text)*8;
\r
606 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
608 x &= ~3; // align x
\r
609 len = (len+3) >> 2;
\r
610 for (h = 0; h < 8; h++) {
\r
611 p = (int *) ((unsigned char *) gp2x_screen+x+320*(y+h));
\r
612 for (i = len; i; i--, p++) *p = 0xe0e0e0e0;
\r
614 gp2x_text_out8_2(x, y, text, 0xf0);
\r
617 x &= ~1; // align x
\r
618 len = (len+1) >> 1;
\r
619 for (h = 0; h < 8; h++) {
\r
620 p = (int *) ((unsigned short *) gp2x_screen+x+320*(y+h));
\r
621 for (i = len; i; i--, p++) *p = (*p>>2)&0x39e7;
\r
623 gp2x_text_out15(x, y, text);
\r
627 static void cd_leds(void)
\r
632 // if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change
\r
633 old_reg = Pico_mcd->s68k_regs[0];
\r
635 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
637 unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;
\r
638 unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;
\r
639 *(unsigned int *)((char *)gp2x_screen + 320*2+ 4) =
\r
640 *(unsigned int *)((char *)gp2x_screen + 320*3+ 4) =
\r
641 *(unsigned int *)((char *)gp2x_screen + 320*4+ 4) = col_g;
\r
642 *(unsigned int *)((char *)gp2x_screen + 320*2+12) =
\r
643 *(unsigned int *)((char *)gp2x_screen + 320*3+12) =
\r
644 *(unsigned int *)((char *)gp2x_screen + 320*4+12) = col_r;
\r
647 unsigned int *p = (unsigned int *)((short *)gp2x_screen + 320*2+4);
\r
648 unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;
\r
649 unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;
\r
650 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
651 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
652 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
656 static int EmuScan16(unsigned int num, void *sdata)
\r
658 if (!(Pico.video.reg[1]&8)) num += 8;
\r
659 DrawLineDest = (unsigned short *) gp2x_screen + 320*(num+1);
\r
664 static int EmuScan8(unsigned int num, void *sdata)
\r
666 if (!(Pico.video.reg[1]&8)) num += 8;
\r
667 DrawLineDest = (unsigned char *) gp2x_screen + 320*(num+1);
\r
672 int localPal[0x100];
\r
673 static void (*vidCpyM2)(void *dest, void *src) = NULL;
\r
675 static void blit(const char *fps, const char *notice)
\r
677 int emu_opt = currentConfig.EmuOpt;
\r
679 if (PicoOpt&0x10) {
\r
680 // 8bit fast renderer
\r
681 if (Pico.m.dirtyPal) {
\r
682 Pico.m.dirtyPal = 0;
\r
683 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
684 // feed new palette to our device
\r
685 gp2x_video_setpalette(localPal, 0x40);
\r
687 vidCpyM2((unsigned char *)gp2x_screen+320*8, framebuff+328*8);
\r
688 } else if (!(emu_opt&0x80)) {
\r
689 // 8bit accurate renderer
\r
690 if (Pico.m.dirtyPal) {
\r
691 Pico.m.dirtyPal = 0;
\r
692 if(Pico.video.reg[0xC]&8) { // shadow/hilight mode
\r
693 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
694 vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
\r
695 vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);
\r
696 blockcpy(localPal+0xc0, localPal+0x40, 0x40*4);
\r
697 localPal[0xc0] = 0x0000c000;
\r
698 localPal[0xd0] = 0x00c00000;
\r
699 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
700 localPal[0xf0] = 0x00ffffff;
\r
701 gp2x_video_setpalette(localPal, 0x100);
\r
702 } else if (rendstatus & 0x20) { // mid-frame palette changes
\r
703 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
704 vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);
\r
705 vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);
\r
706 gp2x_video_setpalette(localPal, 0xc0);
\r
708 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
709 gp2x_video_setpalette(localPal, 0x40);
\r
714 if (notice) osd_text(4, 232, notice);
\r
716 osd_text(osd_fps_x, 232, fps);
\r
717 if ((emu_opt & 0x400) && (PicoMCD & 1))
\r
720 //gp2x_video_wait_vsync();
\r
723 if (!(PicoOpt&0x10)) {
\r
724 if (!(Pico.video.reg[1]&8)) {
\r
725 if (currentConfig.EmuOpt&0x80) {
\r
726 DrawLineDest = (unsigned short *) gp2x_screen + 320*8;
\r
728 DrawLineDest = (unsigned char *) gp2x_screen + 320*8;
\r
731 DrawLineDest = gp2x_screen;
\r
737 // clears whole screen or just the notice area (in all buffers)
\r
738 static void clearArea(int full)
\r
740 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
742 if (full) gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
743 else gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
\r
745 // 16bit accurate renderer
\r
746 if (full) gp2x_memset_all_buffers(0, 0, 320*240*2);
\r
747 else gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
\r
752 static void vidResetMode(void)
\r
754 if (PicoOpt&0x10) {
\r
755 gp2x_video_changemode(8);
\r
756 } else if (currentConfig.EmuOpt&0x80) {
\r
757 gp2x_video_changemode(15);
\r
758 PicoDrawSetColorFormat(1);
\r
759 PicoScan = EmuScan16;
\r
762 gp2x_video_changemode(8);
\r
763 PicoDrawSetColorFormat(2);
\r
764 PicoScan = EmuScan8;
\r
767 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
768 // setup pal for 8-bit modes
\r
769 localPal[0xc0] = 0x0000c000; // MCD LEDs
\r
770 localPal[0xd0] = 0x00c00000;
\r
771 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
772 localPal[0xf0] = 0x00ffffff;
\r
773 gp2x_video_setpalette(localPal, 0x100);
\r
774 gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
777 Pico.m.dirtyPal = 1;
\r
779 gp2x_video_RGB_setscaling((PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);
\r
783 static void emu_state_cb(const char *str)
\r
789 static void RunEvents(unsigned int which)
\r
791 if(which & 0x1800) { // save or load (but not both)
\r
793 if (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200) && emu_check_save_file(state_slot)) {
\r
794 unsigned long keys;
\r
795 blit("", "OVERWRITE SAVE? (Y=yes, X=no)");
\r
796 while( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )
\r
798 if (keys & GP2X_X) do_it = 0;
\r
802 osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
\r
803 PicoStateProgressCB = emu_state_cb;
\r
804 gp2x_memcpy_all_buffers(gp2x_screen, 0, 320*240*2);
\r
805 emu_SaveLoadGame((which & 0x1000) >> 12, 0);
\r
806 PicoStateProgressCB = NULL;
\r
811 if(which & 0x0400) { // switch renderer
\r
812 if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
\r
813 else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;
\r
814 else currentConfig.EmuOpt &= ~0x80;
\r
818 if (PicoOpt&0x10) {
\r
819 strcpy(noticeMsg, " 8bit fast renderer");
\r
820 } else if (currentConfig.EmuOpt&0x80) {
\r
821 strcpy(noticeMsg, "16bit accurate renderer");
\r
823 strcpy(noticeMsg, " 8bit accurate renderer");
\r
826 gettimeofday(¬iceMsgTime, 0);
\r
828 if(which & 0x0300) {
\r
831 if(state_slot < 0) state_slot = 9;
\r
834 if(state_slot > 9) state_slot = 0;
\r
836 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_check_save_file(state_slot) ? "USED" : "FREE");
\r
837 gettimeofday(¬iceMsgTime, 0);
\r
839 if(which & 0x0080) {
\r
840 engineState = PGS_Menu;
\r
845 static void updateMovie(void)
\r
847 int offs = Pico.m.frame_count*3 + 0x40;
\r
848 if (offs+3 > movie_size) {
\r
851 strcpy(noticeMsg, "END OF MOVIE.");
\r
852 printf("END OF MOVIE.\n");
\r
853 gettimeofday(¬iceMsgTime, 0);
\r
856 PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU
\r
857 if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // A
\r
858 if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // B
\r
859 if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // A
\r
860 PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU
\r
861 if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // A
\r
862 if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // B
\r
863 if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // A
\r
864 PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX
\r
865 if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X
\r
866 if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z
\r
867 PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX
\r
868 if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X
\r
869 if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z
\r
874 static void updateKeys(void)
\r
876 unsigned long keys, allActions[2] = { 0, 0 }, events;
\r
877 static unsigned long prevEvents = 0;
\r
880 keys = gp2x_joystick_read(0);
\r
881 if (keys & GP2X_SELECT) {
\r
882 engineState = select_exits ? PGS_Quit : PGS_Menu;
\r
883 // wait until select is released, so menu would not resume game
\r
884 while (gp2x_joystick_read(1) & GP2X_SELECT) usleep(50*1000);
\r
887 keys &= CONFIGURABLE_KEYS;
\r
889 for (i = 0; i < 32; i++)
\r
891 if (keys & (1 << i)) {
\r
892 int pl, acts = currentConfig.KeyBinds[i];
\r
893 if (!acts) continue;
\r
894 pl = (acts >> 16) & 1;
\r
895 if (combo_keys & (1 << i)) {
\r
896 int u = i+1, acts_c = acts & combo_acts;
\r
897 // let's try to find the other one
\r
899 for (; u < 32; u++)
\r
900 if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) {
\r
901 allActions[pl] |= acts_c;
\r
902 keys &= ~((1 << i) | (1 << u));
\r
905 // add non-combo actions if combo ones were not found
\r
906 if (!acts_c || u == 32)
\r
907 allActions[pl] |= acts & ~combo_acts;
\r
909 allActions[pl] |= acts;
\r
915 if (num_of_joys > 0)
\r
917 gp2x_usbjoy_update();
\r
918 for (joy = 0; joy < num_of_joys; joy++) {
\r
919 int keys = gp2x_usbjoy_check2(joy);
\r
920 for (i = 0; i < 32; i++) {
\r
921 if (keys & (1 << i)) {
\r
922 int acts = currentConfig.JoyBinds[joy][i];
\r
923 int pl = (acts >> 16) & 1;
\r
924 allActions[pl] |= acts;
\r
930 PicoPad[0] = (unsigned short) allActions[0];
\r
931 PicoPad[1] = (unsigned short) allActions[1];
\r
933 events = (allActions[0] | allActions[1]) >> 16;
\r
935 // volume is treated in special way and triggered every frame
\r
936 if(events & 0x6000) {
\r
937 int vol = currentConfig.volume;
\r
938 if (events & 0x2000) {
\r
939 if (vol < 90) vol++;
\r
941 if (vol > 0) vol--;
\r
943 gp2x_sound_volume(vol, vol);
\r
944 sprintf(noticeMsg, "VOL: %02i", vol);
\r
945 gettimeofday(¬iceMsgTime, 0);
\r
946 currentConfig.volume = vol;
\r
949 events &= ~prevEvents;
\r
950 if (events) RunEvents(events);
\r
951 if (movie_data) updateMovie();
\r
953 prevEvents = (allActions[0] | allActions[1]) >> 16;
\r
957 static void updateSound(int len)
\r
959 if (PicoOpt&8) len<<=1;
\r
961 gp2x_sound_write(PsndOut, len<<1);
\r
965 static void SkipFrame(int do_sound)
\r
967 void *sndbuff_tmp = 0;
\r
968 if (PsndOut && !do_sound) {
\r
969 sndbuff_tmp = PsndOut;
\r
977 if (sndbuff_tmp && !do_sound) {
\r
978 PsndOut = sndbuff_tmp;
\r
983 void emu_forced_frame(void)
\r
985 int po_old = PicoOpt;
\r
991 if (!(Pico.video.reg[12]&1)) {
\r
992 vidCpyM2 = vidCpyM2_32col;
\r
994 } else vidCpyM2 = vidCpyM2_40col;
\r
996 vidCpyM2((unsigned char *)gp2x_screen+320*8, framebuff+328*8);
\r
997 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
998 gp2x_video_setpalette(localPal, 0x40);
\r
1001 static void simpleWait(int thissec, int lim_time)
\r
1003 struct timeval tval;
\r
1005 spend_cycles(1024);
\r
1006 gettimeofday(&tval, 0);
\r
1007 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
1009 while(tval.tv_usec < lim_time)
\r
1011 spend_cycles(1024);
\r
1012 gettimeofday(&tval, 0);
\r
1013 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
1018 void emu_Loop(void)
\r
1020 static int gp2x_old_clock = 200;
\r
1021 static int PsndRate_old = 0, PicoOpt_old = 0, PsndLen_real = 0, pal_old = 0;
\r
1022 char fpsbuff[24]; // fps count c string
\r
1023 struct timeval tval; // timing
\r
1024 int thissec = 0, frames_done = 0, frames_shown = 0, oldmodes = 0;
\r
1025 int target_fps, target_frametime, lim_time, i;
\r
1028 printf("entered emu_Loop()\n");
\r
1030 if (gp2x_old_clock != currentConfig.CPUclock) {
\r
1031 printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);
\r
1032 set_FCLK(currentConfig.CPUclock);
\r
1033 gp2x_old_clock = currentConfig.CPUclock;
\r
1034 printf(" done\n");
\r
1037 if (gp2x_old_gamma != currentConfig.gamma) {
\r
1038 set_gamma(currentConfig.gamma);
\r
1039 gp2x_old_gamma = currentConfig.gamma;
\r
1040 printf("updated gamma to %i\n", currentConfig.gamma);
\r
1045 // make sure we are in correct mode
\r
1047 Pico.m.dirtyPal = 1;
\r
1048 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
\r
1051 // pal/ntsc might have changed, reset related stuff
\r
1052 target_fps = Pico.m.pal ? 50 : 60;
\r
1053 target_frametime = 1000000/target_fps;
\r
1056 // prepare sound stuff
\r
1057 if(currentConfig.EmuOpt & 4) {
\r
1058 int snd_excess_add;
\r
1059 if(PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old || crashed_940) {
\r
1060 /* if 940 is turned off, we need it to be put back to sleep */
\r
1061 if (!(PicoOpt&0x200) && ((PicoOpt^PicoOpt_old)&0x200)) {
\r
1067 //excess_samples = PsndRate - PsndLen*target_fps;
\r
1068 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
\r
1069 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
1070 gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);
\r
1071 gp2x_sound_volume(currentConfig.volume, currentConfig.volume);
\r
1072 PicoWriteSound = updateSound;
\r
1073 memset(sndBuffer, 0, sizeof(sndBuffer));
\r
1074 PsndOut = sndBuffer;
\r
1075 PsndRate_old = PsndRate;
\r
1076 PsndLen_real = PsndLen;
\r
1077 PicoOpt_old = PicoOpt;
\r
1078 pal_old = Pico.m.pal;
\r
1084 while (engineState == PGS_Running)
\r
1088 gettimeofday(&tval, 0);
\r
1089 if(reset_timing) {
\r
1091 thissec = tval.tv_sec;
\r
1092 frames_shown = frames_done = tval.tv_usec/target_frametime;
\r
1095 // show notice message?
\r
1096 if(noticeMsgTime.tv_sec) {
\r
1097 static int noticeMsgSum;
\r
1098 if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec
\r
1099 noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;
\r
1103 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
\r
1104 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
\r
1105 notice = noticeMsg;
\r
1109 // check for mode changes
\r
1110 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
\r
1111 if (modes != oldmodes) {
\r
1113 osd_fps_x = OSD_FPS_X;
\r
1115 vidCpyM2 = vidCpyM2_40col;
\r
1117 if (PicoOpt & 0x100) {
\r
1118 vidCpyM2 = vidCpyM2_32col_nobord;
\r
1120 osd_fps_x = OSD_FPS_X - 64;
\r
1122 vidCpyM2 = vidCpyM2_32col;
\r
1125 gp2x_video_RGB_setscaling(scalex, 240);
\r
1130 // second changed?
\r
1131 if(thissec != tval.tv_sec) {
\r
1133 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
\r
1134 if(++bench == 10) {
\r
1136 bench_fps_s = bench_fps;
\r
1137 bf[bfp++ & 3] = bench_fps;
\r
1140 bench_fps += frames_shown;
\r
1141 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
\r
1143 if(currentConfig.EmuOpt & 2)
\r
1144 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
\r
1146 thissec = tval.tv_sec;
\r
1148 if(PsndOut == 0 && currentConfig.Frameskip >= 0) {
\r
1149 frames_done = frames_shown = 0;
\r
1151 // it is quite common for this implementation to leave 1 fame unfinished
\r
1152 // when second changes, but we don't want buffer to starve.
\r
1153 if(PsndOut && frames_done < target_fps && frames_done > target_fps-5) {
\r
1155 SkipFrame(1); frames_done++;
\r
1158 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
\r
1159 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
\r
1160 if (frames_shown > frames_done) frames_shown = frames_done;
\r
1164 lim_time = (frames_done+1) * target_frametime;
\r
1165 if(currentConfig.Frameskip >= 0) { // frameskip enabled
\r
1166 for(i = 0; i < currentConfig.Frameskip; i++) {
\r
1168 SkipFrame(1); frames_done++;
\r
1169 if (PsndOut) { // do framelimitting if sound is enabled
\r
1170 gettimeofday(&tval, 0);
\r
1171 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
1172 if(tval.tv_usec < lim_time) { // we are too fast
\r
1173 simpleWait(thissec, lim_time);
\r
1176 lim_time += target_frametime;
\r
1178 } else if(tval.tv_usec > lim_time) { // auto frameskip
\r
1179 // no time left for this frame - skip
\r
1181 SkipFrame(tval.tv_usec < lim_time+target_frametime); frames_done++;
\r
1189 if (Pico.m.frame_count == 31563) {
\r
1191 f = fopen("ram_p.bin", "wb");
\r
1192 if (!f) { printf("!f\n"); exit(1); }
\r
1193 fwrite(Pico.ram, 1, 0x10000, f);
\r
1201 #define BYTE unsigned char
\r
1202 #define WORD unsigned short
\r
1205 BYTE IDLength; /* 00h Size of Image ID field */
\r
1206 BYTE ColorMapType; /* 01h Color map type */
\r
1207 BYTE ImageType; /* 02h Image type code */
\r
1208 WORD CMapStart; /* 03h Color map origin */
\r
1209 WORD CMapLength; /* 05h Color map length */
\r
1210 BYTE CMapDepth; /* 07h Depth of color map entries */
\r
1211 WORD XOffset; /* 08h X origin of image */
\r
1212 WORD YOffset; /* 0Ah Y origin of image */
\r
1213 WORD Width; /* 0Ch Width of image */
\r
1214 WORD Height; /* 0Eh Height of image */
\r
1215 BYTE PixelDepth; /* 10h Image pixel size */
\r
1216 BYTE ImageDescriptor; /* 11h Image descriptor byte */
\r
1217 } __attribute__((packed)) TGAHEAD;
\r
1218 static unsigned short oldscr[320*240];
\r
1219 FILE *f; char name[128]; int i;
\r
1221 memset(&TGAHEAD, 0, sizeof(TGAHEAD));
\r
1222 TGAHEAD.ImageType = 2;
\r
1223 TGAHEAD.Width = 320;
\r
1224 TGAHEAD.Height = 240;
\r
1225 TGAHEAD.PixelDepth = 16;
\r
1226 TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left
\r
1228 #define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?
\r
1230 for (i = 0; i < 320*240; i++)
\r
1231 if(oldscr[i] != CONV(((unsigned short *)gp2x_screen)[i])) break;
\r
1234 for (i = 0; i < 320*240; i++)
\r
1235 oldscr[i] = CONV(((unsigned short *)gp2x_screen)[i]);
\r
1236 sprintf(name, "%05i.tga", Pico.m.frame_count);
\r
1237 f = fopen(name, "wb");
\r
1238 if (!f) { printf("!f\n"); exit(1); }
\r
1239 fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);
\r
1240 fwrite(oldscr, 1, 320*240*2, f);
\r
1247 gettimeofday(&tval, 0);
\r
1248 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
1250 // sleep if we are still too fast
\r
1251 if(PsndOut != 0 || currentConfig.Frameskip < 0)
\r
1253 // usleep sleeps for ~20ms minimum, so it is not a solution here
\r
1254 gettimeofday(&tval, 0);
\r
1255 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
1256 if(tval.tv_usec < lim_time)
\r
1258 // we are too fast
\r
1259 simpleWait(thissec, lim_time);
\r
1263 blit(fpsbuff, notice);
\r
1265 frames_done++; frames_shown++;
\r
1269 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
1270 osd_text(4, 232, "Writing SRAM/BRAM..");
\r
1271 emu_SaveLoadGame(0, 1);
\r
1275 // if in 16bit mode, generate 8it image for menu background
\r
1276 if (!(PicoOpt&0x10) && (currentConfig.EmuOpt&0x80))
\r
1277 emu_forced_frame();
\r
1280 gp2x_memcpy_buffers((1<<2), gp2x_screen, 0, 320*240*2);
\r
1284 void emu_ResetGame(void)
\r
1291 size_t gzRead2(void *p, size_t _size, size_t _n, void *file)
\r
1293 return gzread(file, p, _n);
\r
1297 size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)
\r
1299 return gzwrite(file, p, _n);
\r
1302 static int try_ropen_file(const char *fname)
\r
1306 f = fopen(fname, "rb");
\r
1314 char *emu_GetSaveFName(int load, int is_sram, int slot)
\r
1316 static char saveFname[512];
\r
1321 romfname_ext(saveFname, (PicoMCD&1) ? "brm/" : "srm/", (PicoMCD&1) ? ".brm" : ".srm");
\r
1323 if (try_ropen_file(saveFname)) return saveFname;
\r
1324 // try in current dir..
\r
1325 romfname_ext(saveFname, NULL, (PicoMCD&1) ? ".brm" : ".srm");
\r
1326 if (try_ropen_file(saveFname)) return saveFname;
\r
1327 return NULL; // give up
\r
1333 if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);
\r
1334 strcat(ext, (currentConfig.EmuOpt & 8) ? ".mds.gz" : ".mds");
\r
1336 romfname_ext(saveFname, "mds/", ext);
\r
1338 if (try_ropen_file(saveFname)) return saveFname;
\r
1339 romfname_ext(saveFname, NULL, ext);
\r
1340 if (try_ropen_file(saveFname)) return saveFname;
\r
1341 if (currentConfig.EmuOpt & 8) {
\r
1343 if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);
\r
1344 strcat(ext, ".mds");
\r
1346 romfname_ext(saveFname, "mds/", ext);
\r
1347 if (try_ropen_file(saveFname)) return saveFname;
\r
1348 romfname_ext(saveFname, NULL, ext);
\r
1349 if (try_ropen_file(saveFname)) return saveFname;
\r
1358 int emu_check_save_file(int slot)
\r
1360 return emu_GetSaveFName(1, 0, slot) ? 1 : 0;
\r
1363 void emu_set_save_cbs(int gz)
\r
1366 areaRead = gzRead2;
\r
1367 areaWrite = gzWrite2;
\r
1368 areaEof = (areaeof *) gzeof;
\r
1369 areaSeek = (areaseek *) gzseek;
\r
1370 areaClose = (areaclose *) gzclose;
\r
1372 areaRead = (arearw *) fread;
\r
1373 areaWrite = (arearw *) fwrite;
\r
1374 areaEof = (areaeof *) feof;
\r
1375 areaSeek = (areaseek *) fseek;
\r
1376 areaClose = (areaclose *) fclose;
\r
1380 int emu_SaveLoadGame(int load, int sram)
\r
1385 // make save filename
\r
1386 saveFname = emu_GetSaveFName(load, sram, state_slot);
\r
1387 if (saveFname == NULL) {
\r
1389 strcpy(noticeMsg, load ? "LOAD FAILED (missing file)" : "SAVE FAILED ");
\r
1390 gettimeofday(¬iceMsgTime, 0);
\r
1395 printf("saveLoad (%i, %i): %s\n", load, sram, saveFname);
\r
1400 unsigned char *sram_data;
\r
1402 sram_size = 0x2000;
\r
1403 sram_data = Pico_mcd->bram;
\r
1405 sram_size = SRam.end-SRam.start+1;
\r
1406 if(SRam.reg_back & 4) sram_size=0x2000;
\r
1407 sram_data = SRam.data;
\r
1409 if(!sram_data) return 0; // SRam forcefully disabled for this game
\r
1412 sramFile = fopen(saveFname, "rb");
\r
1413 if(!sramFile) return -1;
\r
1414 fread(sram_data, 1, sram_size, sramFile);
\r
1417 // sram save needs some special processing
\r
1418 // see if we have anything to save
\r
1419 for(; sram_size > 0; sram_size--)
\r
1420 if(sram_data[sram_size-1]) break;
\r
1423 sramFile = fopen(saveFname, "wb");
\r
1424 ret = fwrite(sram_data, 1, sram_size, sramFile);
\r
1425 ret = (ret != sram_size) ? -1 : 0;
\r
1434 void *PmovFile = NULL;
\r
1435 if (strcmp(saveFname + strlen(saveFname) - 3, ".gz") == 0) {
\r
1436 if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) {
\r
1437 emu_set_save_cbs(1);
\r
1438 if(!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);
\r
1443 if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) {
\r
1444 emu_set_save_cbs(0);
\r
1448 ret = PmovState(load ? 6 : 5, PmovFile);
\r
1449 areaClose(PmovFile);
\r
1451 if (!load) sync();
\r
1452 else Pico.m.dirtyPal=1;
\r
1456 strcpy(noticeMsg, load ? "GAME LOADED " : "GAME SAVED ");
\r
1459 strcpy(noticeMsg, load ? "LOAD FAILED " : "SAVE FAILED ");
\r
1463 gettimeofday(¬iceMsgTime, 0);
\r