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 <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
37 int select_exits = 0;
\r
38 char *PicoConfigFile = "picoconfig.bin";
\r
39 currentConfig_t currentConfig;
\r
41 char romFileName[PATH_MAX];
\r
42 unsigned char *rom_data = NULL;
\r
44 extern int crashed_940;
\r
46 static short sndBuffer[2*44100/50];
\r
47 static char noticeMsg[64]; // notice msg to draw
\r
48 static struct timeval noticeMsgTime = { 0, 0 }; // when started showing
\r
49 static int osd_fps_x;
\r
50 static int combo_keys = 0, combo_acts = 0; // keys and actions which need button combos
\r
51 static int gp2x_old_gamma = 100;
\r
52 static unsigned char *movie_data = NULL;
\r
53 static int movie_size = 0;
\r
54 unsigned char *framebuff = 0; // temporary buffer for alt renderer
\r
56 int reset_timing = 0;
\r
57 int config_slot = 0, config_slot_current = 0;
\r
61 static void strlwr(char* string)
\r
63 while ( (*string++ = (char)tolower(*string)) );
\r
66 static int try_rfn_cut(void)
\r
71 p = romFileName + strlen(romFileName) - 1;
\r
72 for (; p > romFileName; p--)
\r
73 if (*p == '.') break;
\r
76 if((tmp = fopen(romFileName, "rb"))) {
\r
83 static void get_ext(char *file, char *ext)
\r
87 p = file + strlen(file) - 4;
\r
88 if (p < file) p = file;
\r
94 char *biosfiles_us[] = { "us_scd2_9306", "SegaCDBIOS9303", "us_scd1_9210" };
\r
95 char *biosfiles_eu[] = { "eu_mcd2_9306", "eu_mcd2_9303", "eu_mcd1_9210" };
\r
96 char *biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };
\r
98 extern char **g_argv;
\r
100 int find_bios(int region, char **bios_file)
\r
102 static char bios_path[1024];
\r
107 if (region == 4) { // US
\r
108 files = biosfiles_us;
\r
109 count = sizeof(biosfiles_us) / sizeof(char *);
\r
110 } else if (region == 8) { // EU
\r
111 files = biosfiles_eu;
\r
112 count = sizeof(biosfiles_eu) / sizeof(char *);
\r
113 } else if (region == 1 || region == 2) {
\r
114 files = biosfiles_jp;
\r
115 count = sizeof(biosfiles_jp) / sizeof(char *);
\r
120 for (i = 0; i < count; i++)
\r
122 strncpy(bios_path, g_argv[0], 1023);
\r
123 bios_path[1024-32] = 0;
\r
124 for (j = strlen(bios_path); j > 0; j--)
\r
125 if (bios_path[j] == '/') { bios_path[j+1] = 0; break; }
\r
126 strcat(bios_path, files[i]);
\r
127 strcat(bios_path, ".bin");
\r
128 f = fopen(bios_path, "rb");
\r
131 bios_path[strlen(bios_path) - 4] = 0;
\r
132 strcat(bios_path, ".zip");
\r
133 f = fopen(bios_path, "rb");
\r
138 printf("using bios: %s\n", bios_path);
\r
140 if (bios_file) *bios_file = bios_path;
\r
143 sprintf(menuErrorMsg, "no %s BIOS files found, read docs",
\r
144 region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
\r
145 printf("%s\n", menuErrorMsg);
\r
150 /* checks if romFileName points to valid MegaCD image
\r
151 * if so, checks for suitable BIOS */
\r
152 int emu_cd_check(char **bios_file)
\r
154 unsigned char buf[32];
\r
156 int type = 0, region = 4; // 1: Japan, 4: US, 8: Europe
\r
158 cd_f = pm_open(romFileName);
\r
159 if (!cd_f) return 0; // let the upper level handle this
\r
161 if (pm_read(buf, 32, cd_f) != 32) {
\r
166 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) type = 1; // Sega CD (ISO)
\r
167 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) type = 2; // Sega CD (BIN)
\r
173 /* it seems we have a CD image here. Try to detect region now.. */
\r
174 pm_seek(cd_f, (type == 1) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);
\r
175 pm_read(buf, 1, cd_f);
\r
178 if (buf[0] == 0x64) region = 8; // EU
\r
179 if (buf[0] == 0xa1) region = 1; // JAP
\r
181 printf("detected %s Sega/Mega CD image with %s region\n",
\r
182 type == 2 ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
\r
184 if (PicoRegionOverride) {
\r
185 region = PicoRegionOverride;
\r
186 printf("overrided region to %s\n", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
\r
189 if (bios_file == NULL) return type;
\r
191 if (find_bios(region, bios_file))
\r
192 return type; // CD and BIOS detected
\r
194 return -1; // CD detected but load failed
\r
197 int emu_ReloadRom(void)
\r
199 unsigned int rom_size = 0;
\r
200 char *used_rom_name = romFileName;
\r
205 printf("emu_ReloadRom(%s)\n", romFileName);
\r
207 get_ext(romFileName, ext);
\r
209 // detect wrong extensions
\r
210 if(!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz
\r
211 sprintf(menuErrorMsg, "Not a ROM selected.");
\r
217 // check for movie file
\r
222 if(!strcmp(ext, ".gmv")) {
\r
223 // check for both gmv and rom
\r
225 FILE *movie_file = fopen(romFileName, "rb");
\r
227 sprintf(menuErrorMsg, "Failed to open movie.");
\r
230 fseek(movie_file, 0, SEEK_END);
\r
231 movie_size = ftell(movie_file);
\r
232 fseek(movie_file, 0, SEEK_SET);
\r
233 if(movie_size < 64+3) {
\r
234 sprintf(menuErrorMsg, "Invalid GMV file.");
\r
235 fclose(movie_file);
\r
238 movie_data = malloc(movie_size);
\r
239 if(movie_data == NULL) {
\r
240 sprintf(menuErrorMsg, "low memory.");
\r
241 fclose(movie_file);
\r
244 fread(movie_data, 1, movie_size, movie_file);
\r
245 fclose(movie_file);
\r
246 if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {
\r
247 sprintf(menuErrorMsg, "Invalid GMV file.");
\r
250 dummy = try_rfn_cut() || try_rfn_cut();
\r
252 sprintf(menuErrorMsg, "Could't find a ROM for movie.");
\r
255 get_ext(romFileName, ext);
\r
257 else if (!strcmp(ext, ".pat")) {
\r
259 PicoPatchLoad(romFileName);
\r
260 dummy = try_rfn_cut() || try_rfn_cut();
\r
262 sprintf(menuErrorMsg, "Could't find a ROM to patch.");
\r
265 get_ext(romFileName, ext);
\r
268 if ((PicoMCD & 1) && Pico_mcd != NULL)
\r
271 // check for MegaCD image
\r
272 cd_state = emu_cd_check(&used_rom_name);
\r
273 if (cd_state > 0) {
\r
275 get_ext(used_rom_name, ext);
\r
276 } else if (cd_state == -1) {
\r
280 if (PicoMCD & 1) PicoExitMCD();
\r
284 rom = pm_open(used_rom_name);
\r
286 sprintf(menuErrorMsg, "Failed to open rom.");
\r
290 menu_romload_prepare(used_rom_name);
\r
298 if( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) {
\r
299 sprintf(menuErrorMsg, "PicoCartLoad() failed.");
\r
300 printf("%s\n", menuErrorMsg);
\r
302 menu_romload_end();
\r
306 menu_romload_end();
\r
308 // detect wrong files (Pico crashes on very small files), also see if ROM EP is good
\r
309 if(rom_size <= 0x200 || strncmp((char *)rom_data, "Pico", 4) == 0 ||
\r
310 ((*(unsigned char *)(rom_data+4)<<16)|(*(unsigned short *)(rom_data+6))) >= (int)rom_size) {
\r
311 if (rom_data) free(rom_data);
\r
313 sprintf(menuErrorMsg, "Not a ROM selected.");
\r
317 // load config for this ROM (do this before insert to get correct region)
\r
318 ret = emu_ReadConfig(1, 1);
\r
320 emu_ReadConfig(0, 1);
\r
322 printf("PicoCartInsert(%p, %d);\n", rom_data, rom_size);
\r
323 if(PicoCartInsert(rom_data, rom_size)) {
\r
324 sprintf(menuErrorMsg, "Failed to load ROM.");
\r
328 Pico.m.frame_count = 0;
\r
330 // insert CD if it was detected
\r
331 if (cd_state > 0) {
\r
332 ret = Insert_CD(romFileName, cd_state == 2);
\r
334 sprintf(menuErrorMsg, "Insert_CD() failed, invalid CD image?");
\r
335 printf("%s\n", menuErrorMsg);
\r
340 // emu_ReadConfig() might have messed currentConfig.lastRomFile
\r
341 strncpy(currentConfig.lastRomFile, romFileName, sizeof(currentConfig.lastRomFile)-1);
\r
342 currentConfig.lastRomFile[sizeof(currentConfig.lastRomFile)-1] = 0;
\r
345 PicoPatchPrepare();
\r
349 // additional movie stuff
\r
351 if(movie_data[0x14] == '6')
\r
352 PicoOpt |= 0x20; // 6 button pad
\r
353 else PicoOpt &= ~0x20;
\r
354 PicoOpt |= 0x40; // accurate timing
\r
355 if(movie_data[0xF] >= 'A') {
\r
356 if(movie_data[0x16] & 0x80) {
\r
357 PicoRegionOverride = 8;
\r
359 PicoRegionOverride = 4;
\r
362 // TODO: bits 6 & 5
\r
364 movie_data[0x18+30] = 0;
\r
365 sprintf(noticeMsg, "MOVIE: %s", (char *) &movie_data[0x18]);
\r
370 strcpy(noticeMsg, "PAL SYSTEM / 50 FPS");
\r
372 strcpy(noticeMsg, "NTSC SYSTEM / 60 FPS");
\r
375 gettimeofday(¬iceMsgTime, 0);
\r
377 // load SRAM for this ROM
\r
378 if(currentConfig.EmuOpt & 1)
\r
379 emu_SaveLoadGame(1, 1);
\r
385 static void emu_msg_cb(const char *msg);
\r
386 static void emu_msg_tray_open(void);
\r
388 void emu_Init(void)
\r
390 // make temp buffer for alt renderer
\r
391 framebuff = malloc((8+320)*(8+240+8));
\r
394 printf("framebuff == 0\n");
\r
397 // make dirs for saves, cfgs, etc.
\r
398 mkdir("mds", 0777);
\r
399 mkdir("srm", 0777);
\r
400 mkdir("brm", 0777);
\r
401 mkdir("cfg", 0777);
\r
404 PicoMessage = emu_msg_cb;
\r
405 PicoMCDopenTray = emu_msg_tray_open;
\r
406 PicoMCDcloseTray = menu_loop_tray;
\r
410 static void romfname_ext(char *dst, const char *prefix, const char *ext)
\r
413 int prefix_len = 0;
\r
415 // make save filename
\r
416 for (p = romFileName+strlen(romFileName)-1; p >= romFileName && *p != '/'; p--); p++;
\r
419 strcpy(dst, prefix);
\r
420 prefix_len = strlen(prefix);
\r
422 strncpy(dst + prefix_len, p, 511-prefix_len);
\r
424 if (dst[strlen(dst)-4] == '.') dst[strlen(dst)-4] = 0;
\r
425 if (ext) strcat(dst, ext);
\r
429 static void find_combos(void)
\r
433 // find out which keys and actions are combos
\r
434 combo_keys = combo_acts = 0;
\r
435 for (act = 0; act < 32; act++)
\r
438 if (act == 16) continue; // player2 flag
\r
439 for (u = 0; u < 32; u++)
\r
441 if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;
\r
445 // loop again and mark those keys and actions as combo
\r
446 for (u = 0; u < 32; u++)
\r
448 if (currentConfig.KeyBinds[u] & (1 << act)) {
\r
449 combo_keys |= 1 << u;
\r
450 combo_acts |= 1 << act;
\r
455 // printf("combo keys/acts: %08x %08x\n", combo_keys, combo_acts);
\r
459 void scaling_update(void)
\r
461 PicoOpt &= ~0x4100;
\r
462 switch (currentConfig.scaling) {
\r
463 default: break; // off
\r
465 case 2: PicoOpt |= 0x0100; break; // hw hor+vert
\r
466 case 3: PicoOpt |= 0x4000; break; // sw hor
\r
471 int emu_ReadConfig(int game, int no_defaults)
\r
474 char cfg[512], extbuf[16];
\r
481 // set default config
\r
482 memset(¤tConfig, 0, sizeof(currentConfig));
\r
483 currentConfig.lastRomFile[0] = 0;
\r
484 currentConfig.EmuOpt = 0x1f | 0x600; // | confirm_save, cd_leds
\r
485 currentConfig.PicoOpt = 0x0f | 0xe00; // | use_940, cd_pcm, cd_cdda
\r
486 currentConfig.PsndRate = 22050; // 44100;
\r
487 currentConfig.PicoRegion = 0; // auto
\r
488 currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP
\r
489 currentConfig.Frameskip = -1; // auto
\r
490 currentConfig.CPUclock = 200;
\r
491 currentConfig.volume = 50;
\r
492 currentConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU
\r
493 currentConfig.KeyBinds[ 4] = 1<<1;
\r
494 currentConfig.KeyBinds[ 2] = 1<<2;
\r
495 currentConfig.KeyBinds[ 6] = 1<<3;
\r
496 currentConfig.KeyBinds[14] = 1<<4;
\r
497 currentConfig.KeyBinds[13] = 1<<5;
\r
498 currentConfig.KeyBinds[12] = 1<<6;
\r
499 currentConfig.KeyBinds[ 8] = 1<<7;
\r
500 currentConfig.KeyBinds[15] = 1<<26; // switch rend
\r
501 currentConfig.KeyBinds[10] = 1<<27; // save state
\r
502 currentConfig.KeyBinds[11] = 1<<28; // load state
\r
503 currentConfig.KeyBinds[23] = 1<<29; // vol up
\r
504 currentConfig.KeyBinds[22] = 1<<30; // vol down
\r
505 currentConfig.gamma = 100;
\r
506 currentConfig.PicoCDBuffers = 64;
\r
507 currentConfig.scaling = 0;
\r
509 strncpy(cfg, PicoConfigFile, 511);
\r
510 if (config_slot != 0)
\r
512 char *p = strrchr(cfg, '.');
\r
513 if (p == NULL) p = cfg + strlen(cfg);
\r
514 sprintf(extbuf, ".%i.pbcfg", config_slot);
\r
515 strncpy(p, extbuf, 511 - (p - cfg));
\r
519 if (config_slot != 0)
\r
520 sprintf(extbuf, ".%i.pbcfg", config_slot);
\r
521 else strcpy(extbuf, ".pbcfg");
\r
522 romfname_ext(cfg, "cfg/", extbuf);
\r
523 f = fopen(cfg, "rb");
\r
524 if (!f) romfname_ext(cfg, NULL, ".pbcfg");
\r
528 printf("emu_ReadConfig: %s ", cfg);
\r
529 f = fopen(cfg, "rb");
\r
531 bread = fread(¤tConfig, 1, sizeof(currentConfig), f);
\r
534 printf(bread > 0 ? "(ok)\n" : "(failed)\n");
\r
536 PicoOpt = currentConfig.PicoOpt;
\r
537 PsndRate = currentConfig.PsndRate;
\r
538 PicoRegionOverride = currentConfig.PicoRegion;
\r
539 PicoAutoRgnOrder = currentConfig.PicoAutoRgnOrder;
\r
540 PicoCDBuffers = currentConfig.PicoCDBuffers;
\r
542 // some sanity checks
\r
543 if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;
\r
544 if (currentConfig.gamma < 10 || currentConfig.gamma > 300) currentConfig.gamma = 100;
\r
545 if (currentConfig.volume < 0 || currentConfig.volume > 99) currentConfig.volume = 50;
\r
546 // if volume keys are unbound, bind them to volume control
\r
547 if (!currentConfig.KeyBinds[23] && !currentConfig.KeyBinds[22]) {
\r
548 currentConfig.KeyBinds[23] = 1<<29; // vol up
\r
549 currentConfig.KeyBinds[22] = 1<<30; // vol down
\r
552 if (bread > 0) config_slot_current = config_slot;
\r
553 return (bread > 0); // == sizeof(currentConfig));
\r
557 int emu_WriteConfig(int game)
\r
560 char cfg[512], extbuf[16];
\r
565 strncpy(cfg, PicoConfigFile, 511);
\r
566 if (config_slot != 0)
\r
568 char *p = strrchr(cfg, '.');
\r
569 if (p == NULL) p = cfg + strlen(cfg);
\r
570 sprintf(extbuf, ".%i.pbcfg", config_slot);
\r
571 strncpy(p, extbuf, 511 - (p - cfg));
\r
575 if (config_slot != 0)
\r
576 sprintf(extbuf, ".%i.pbcfg", config_slot);
\r
577 else strcpy(extbuf, ".pbcfg");
\r
578 romfname_ext(cfg, "cfg/", extbuf);
\r
581 printf("emu_WriteConfig: %s ", cfg);
\r
582 f = fopen(cfg, "wb");
\r
584 currentConfig.PicoOpt = PicoOpt;
\r
585 currentConfig.PsndRate = PsndRate;
\r
586 currentConfig.PicoRegion = PicoRegionOverride;
\r
587 currentConfig.PicoAutoRgnOrder = PicoAutoRgnOrder;
\r
588 currentConfig.PicoCDBuffers = PicoCDBuffers;
\r
589 bwrite = fwrite(¤tConfig, 1, sizeof(currentConfig), f);
\r
596 printf((bwrite == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n");
\r
598 if (bwrite == sizeof(currentConfig)) config_slot_current = config_slot;
\r
599 return (bwrite == sizeof(currentConfig));
\r
603 void emu_Deinit(void)
\r
606 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
607 emu_SaveLoadGame(0, 1);
\r
611 if (!(currentConfig.EmuOpt & 0x20)) {
\r
612 FILE *f = fopen(PicoConfigFile, "r+b");
\r
613 if (!f) emu_WriteConfig(0);
\r
615 // if we already have config, reload it, except last ROM
\r
616 fseek(f, sizeof(currentConfig.lastRomFile), SEEK_SET);
\r
617 fread(¤tConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f);
\r
618 fseek(f, 0, SEEK_SET);
\r
619 fwrite(¤tConfig, 1, sizeof(currentConfig), f);
\r
633 if (gp2x_old_gamma != 100)
\r
638 void osd_text(int x, int y, const char *text)
\r
640 int len = strlen(text)*8;
\r
642 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
644 x &= ~3; // align x
\r
645 len = (len+3) >> 2;
\r
646 for (h = 0; h < 8; h++) {
\r
647 p = (int *) ((unsigned char *) gp2x_screen+x+320*(y+h));
\r
648 for (i = len; i; i--, p++) *p = 0xe0e0e0e0;
\r
650 gp2x_text_out8_2(x, y, text, 0xf0);
\r
653 x &= ~1; // align x
\r
654 len = (len+1) >> 1;
\r
655 for (h = 0; h < 8; h++) {
\r
656 p = (int *) ((unsigned short *) gp2x_screen+x+320*(y+h));
\r
657 for (i = len; i; i--, p++) *p = (*p>>2)&0x39e7;
\r
659 gp2x_text_out15(x, y, text);
\r
663 static void cd_leds(void)
\r
668 // if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change
\r
669 old_reg = Pico_mcd->s68k_regs[0];
\r
671 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
673 unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;
\r
674 unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;
\r
675 *(unsigned int *)((char *)gp2x_screen + 320*2+ 4) =
\r
676 *(unsigned int *)((char *)gp2x_screen + 320*3+ 4) =
\r
677 *(unsigned int *)((char *)gp2x_screen + 320*4+ 4) = col_g;
\r
678 *(unsigned int *)((char *)gp2x_screen + 320*2+12) =
\r
679 *(unsigned int *)((char *)gp2x_screen + 320*3+12) =
\r
680 *(unsigned int *)((char *)gp2x_screen + 320*4+12) = col_r;
\r
683 unsigned int *p = (unsigned int *)((short *)gp2x_screen + 320*2+4);
\r
684 unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;
\r
685 unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;
\r
686 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
687 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
688 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
692 static int EmuScan16(unsigned int num, void *sdata)
\r
694 if (!(Pico.video.reg[1]&8)) num += 8;
\r
695 DrawLineDest = (unsigned short *) gp2x_screen + 320*(num+1);
\r
700 static int EmuScan8(unsigned int num, void *sdata)
\r
702 if (!(Pico.video.reg[1]&8)) num += 8;
\r
703 DrawLineDest = (unsigned char *) gp2x_screen + 320*(num+1);
\r
708 int localPal[0x100];
\r
709 static void (*vidCpyM2)(void *dest, void *src) = NULL;
\r
711 static void blit(const char *fps, const char *notice)
\r
713 int emu_opt = currentConfig.EmuOpt;
\r
715 if (PicoOpt&0x10) {
\r
716 // 8bit fast renderer
\r
717 if (Pico.m.dirtyPal) {
\r
718 Pico.m.dirtyPal = 0;
\r
719 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
720 // feed new palette to our device
\r
721 gp2x_video_setpalette(localPal, 0x40);
\r
723 vidCpyM2((unsigned char *)gp2x_screen+320*8, framebuff+328*8);
\r
724 } else if (!(emu_opt&0x80)) {
\r
725 // 8bit accurate renderer
\r
726 if (Pico.m.dirtyPal) {
\r
727 Pico.m.dirtyPal = 0;
\r
728 if(Pico.video.reg[0xC]&8) { // shadow/hilight mode
\r
729 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
730 vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
\r
731 vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);
\r
732 blockcpy(localPal+0xc0, localPal+0x40, 0x40*4);
\r
733 localPal[0xc0] = 0x0000c000;
\r
734 localPal[0xd0] = 0x00c00000;
\r
735 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
736 localPal[0xf0] = 0x00ffffff;
\r
737 gp2x_video_setpalette(localPal, 0x100);
\r
738 } else if (rendstatus & 0x20) { // mid-frame palette changes
\r
739 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
740 vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);
\r
741 vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);
\r
742 gp2x_video_setpalette(localPal, 0xc0);
\r
744 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
745 gp2x_video_setpalette(localPal, 0x40);
\r
750 if (notice || (emu_opt & 2)) {
\r
752 if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8)) h -= 8;
\r
753 if (notice) osd_text(4, h, notice);
\r
755 osd_text(osd_fps_x, h, fps);
\r
757 if ((emu_opt & 0x400) && (PicoMCD & 1))
\r
760 //gp2x_video_wait_vsync();
\r
763 if (!(PicoOpt&0x10)) {
\r
764 if (!(Pico.video.reg[1]&8)) {
\r
765 if (currentConfig.EmuOpt&0x80) {
\r
766 DrawLineDest = (unsigned short *) gp2x_screen + 320*8;
\r
768 DrawLineDest = (unsigned char *) gp2x_screen + 320*8;
\r
771 DrawLineDest = gp2x_screen;
\r
777 // clears whole screen or just the notice area (in all buffers)
\r
778 static void clearArea(int full)
\r
780 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
782 if (full) gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
783 else gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
\r
785 // 16bit accurate renderer
\r
786 if (full) gp2x_memset_all_buffers(0, 0, 320*240*2);
\r
787 else gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
\r
792 static void vidResetMode(void)
\r
794 if (PicoOpt&0x10) {
\r
795 gp2x_video_changemode(8);
\r
796 } else if (currentConfig.EmuOpt&0x80) {
\r
797 gp2x_video_changemode(15);
\r
798 PicoDrawSetColorFormat(1);
\r
799 PicoScan = EmuScan16;
\r
802 gp2x_video_changemode(8);
\r
803 PicoDrawSetColorFormat(2);
\r
804 PicoScan = EmuScan8;
\r
807 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
808 // setup pal for 8-bit modes
\r
809 localPal[0xc0] = 0x0000c000; // MCD LEDs
\r
810 localPal[0xd0] = 0x00c00000;
\r
811 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
812 localPal[0xf0] = 0x00ffffff;
\r
813 gp2x_video_setpalette(localPal, 0x100);
\r
814 gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
817 Pico.m.dirtyPal = 1;
\r
819 if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8))
\r
820 gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);
\r
821 else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);
\r
825 static void emu_msg_cb(const char *msg)
\r
827 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
829 gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
\r
830 osd_text(4, 232, msg);
\r
831 gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232, 320*232, 320*8);
\r
833 // 16bit accurate renderer
\r
834 gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
\r
835 osd_text(4, 232, msg);
\r
836 gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232*2, 320*232*2, 320*8*2);
\r
838 gettimeofday(¬iceMsgTime, 0);
\r
839 noticeMsgTime.tv_sec -= 2;
\r
841 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
\r
845 static void emu_state_cb(const char *str)
\r
851 static void emu_msg_tray_open(void)
\r
853 strcpy(noticeMsg, "CD tray opened");
\r
854 gettimeofday(¬iceMsgTime, 0);
\r
857 static void RunEvents(unsigned int which)
\r
859 if(which & 0x1800) { // save or load (but not both)
\r
861 if ( emu_check_save_file(state_slot) &&
\r
862 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
\r
863 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) { // save
\r
864 unsigned long keys;
\r
865 blit("", (which & 0x1000) ? "LOAD STATE? (Y=yes, X=no)" : "OVERWRITE SAVE? (Y=yes, X=no)");
\r
866 while( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )
\r
868 if (keys & GP2X_X) do_it = 0;
\r
872 osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
\r
873 PicoStateProgressCB = emu_state_cb;
\r
874 gp2x_memcpy_all_buffers(gp2x_screen, 0, 320*240*2);
\r
875 emu_SaveLoadGame((which & 0x1000) >> 12, 0);
\r
876 PicoStateProgressCB = NULL;
\r
881 if(which & 0x0400) { // switch renderer
\r
882 if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
\r
883 else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;
\r
884 else currentConfig.EmuOpt &= ~0x80;
\r
888 if (PicoOpt&0x10) {
\r
889 strcpy(noticeMsg, " 8bit fast renderer");
\r
890 } else if (currentConfig.EmuOpt&0x80) {
\r
891 strcpy(noticeMsg, "16bit accurate renderer");
\r
893 strcpy(noticeMsg, " 8bit accurate renderer");
\r
896 gettimeofday(¬iceMsgTime, 0);
\r
898 if(which & 0x0300) {
\r
901 if(state_slot < 0) state_slot = 9;
\r
904 if(state_slot > 9) state_slot = 0;
\r
906 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_check_save_file(state_slot) ? "USED" : "FREE");
\r
907 gettimeofday(¬iceMsgTime, 0);
\r
909 if(which & 0x0080) {
\r
910 engineState = PGS_Menu;
\r
915 static void updateMovie(void)
\r
917 int offs = Pico.m.frame_count*3 + 0x40;
\r
918 if (offs+3 > movie_size) {
\r
921 strcpy(noticeMsg, "END OF MOVIE.");
\r
922 printf("END OF MOVIE.\n");
\r
923 gettimeofday(¬iceMsgTime, 0);
\r
926 PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU
\r
927 if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // A
\r
928 if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // B
\r
929 if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // A
\r
930 PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU
\r
931 if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // A
\r
932 if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // B
\r
933 if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // A
\r
934 PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX
\r
935 if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X
\r
936 if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z
\r
937 PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX
\r
938 if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X
\r
939 if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z
\r
944 static void updateKeys(void)
\r
946 unsigned long keys, allActions[2] = { 0, 0 }, events;
\r
947 static unsigned long prevEvents = 0;
\r
950 keys = gp2x_joystick_read(0);
\r
951 if (keys & GP2X_SELECT) {
\r
952 engineState = select_exits ? PGS_Quit : PGS_Menu;
\r
953 // wait until select is released, so menu would not resume game
\r
954 while (gp2x_joystick_read(1) & GP2X_SELECT) usleep(50*1000);
\r
957 keys &= CONFIGURABLE_KEYS;
\r
959 for (i = 0; i < 32; i++)
\r
961 if (keys & (1 << i)) {
\r
962 int pl, acts = currentConfig.KeyBinds[i];
\r
963 if (!acts) continue;
\r
964 pl = (acts >> 16) & 1;
\r
965 if (combo_keys & (1 << i)) {
\r
966 int u = i+1, acts_c = acts & combo_acts;
\r
967 // let's try to find the other one
\r
969 for (; u < 32; u++)
\r
970 if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) {
\r
971 allActions[pl] |= acts_c;
\r
972 keys &= ~((1 << i) | (1 << u));
\r
975 // add non-combo actions if combo ones were not found
\r
976 if (!acts_c || u == 32)
\r
977 allActions[pl] |= acts & ~combo_acts;
\r
979 allActions[pl] |= acts;
\r
985 if (num_of_joys > 0)
\r
987 gp2x_usbjoy_update();
\r
988 for (joy = 0; joy < num_of_joys; joy++) {
\r
989 int keys = gp2x_usbjoy_check2(joy);
\r
990 for (i = 0; i < 32; i++) {
\r
991 if (keys & (1 << i)) {
\r
992 int acts = currentConfig.JoyBinds[joy][i];
\r
993 int pl = (acts >> 16) & 1;
\r
994 allActions[pl] |= acts;
\r
1000 PicoPad[0] = (unsigned short) allActions[0];
\r
1001 PicoPad[1] = (unsigned short) allActions[1];
\r
1003 events = (allActions[0] | allActions[1]) >> 16;
\r
1005 // volume is treated in special way and triggered every frame
\r
1006 if(events & 0x6000) {
\r
1007 int vol = currentConfig.volume;
\r
1008 if (events & 0x2000) {
\r
1009 if (vol < 99) vol++;
\r
1011 if (vol > 0) vol--;
\r
1013 gp2x_sound_volume(vol, vol);
\r
1014 sprintf(noticeMsg, "VOL: %02i", vol);
\r
1015 gettimeofday(¬iceMsgTime, 0);
\r
1016 currentConfig.volume = vol;
\r
1019 events &= ~prevEvents;
\r
1020 if (events) RunEvents(events);
\r
1021 if (movie_data) updateMovie();
\r
1023 prevEvents = (allActions[0] | allActions[1]) >> 16;
\r
1027 static void updateSound(int len)
\r
1029 if (PicoOpt&8) len<<=1;
\r
1031 /* avoid writing audio when lagging behind to prevent audio lag */
\r
1032 if (PicoSkipFrame != 2)
\r
1033 gp2x_sound_write(PsndOut, len<<1);
\r
1037 static void SkipFrame(int do_audio)
\r
1039 PicoSkipFrame=do_audio ? 1 : 2;
\r
1045 void emu_forced_frame(void)
\r
1047 int po_old = PicoOpt;
\r
1052 if (!(Pico.video.reg[12]&1)) {
\r
1053 vidCpyM2 = vidCpyM2_32col;
\r
1055 } else vidCpyM2 = vidCpyM2_40col;
\r
1057 vidCpyM2((unsigned char *)gp2x_screen+320*8, framebuff+328*8);
\r
1058 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
1059 gp2x_video_setpalette(localPal, 0x40);
\r
1064 static void simpleWait(int thissec, int lim_time)
\r
1066 struct timeval tval;
\r
1068 spend_cycles(1024);
\r
1069 gettimeofday(&tval, 0);
\r
1070 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
1072 while(tval.tv_usec < lim_time)
\r
1074 spend_cycles(1024);
\r
1075 gettimeofday(&tval, 0);
\r
1076 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
1081 void emu_Loop(void)
\r
1083 static int gp2x_old_clock = 200;
\r
1084 static int PsndRate_old = 0, PicoOpt_old = 0, EmuOpt_old = 0, PsndLen_real = 0, pal_old = 0;
\r
1085 char fpsbuff[24]; // fps count c string
\r
1086 struct timeval tval; // timing
\r
1087 int thissec = 0, frames_done = 0, frames_shown = 0, oldmodes = 0;
\r
1088 int target_fps, target_frametime, lim_time, vsync_offset, i;
\r
1091 printf("entered emu_Loop()\n");
\r
1093 if (gp2x_old_clock != currentConfig.CPUclock) {
\r
1094 printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);
\r
1095 set_FCLK(currentConfig.CPUclock);
\r
1096 gp2x_old_clock = currentConfig.CPUclock;
\r
1097 printf(" done\n");
\r
1100 if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {
\r
1101 set_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));
\r
1102 gp2x_old_gamma = currentConfig.gamma;
\r
1103 printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));
\r
1106 if ((EmuOpt_old&0x2000) != (currentConfig.EmuOpt&0x2000)) {
\r
1107 if (currentConfig.EmuOpt&0x2000)
\r
1108 set_LCD_custom_rate(Pico.m.pal ? LCDR_100 : LCDR_120);
\r
1109 else unset_LCD_custom_rate();
\r
1112 EmuOpt_old = currentConfig.EmuOpt;
\r
1115 // make sure we are in correct mode
\r
1117 Pico.m.dirtyPal = 1;
\r
1118 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
\r
1121 // pal/ntsc might have changed, reset related stuff
\r
1122 target_fps = Pico.m.pal ? 50 : 60;
\r
1123 target_frametime = 1000000/target_fps;
\r
1126 // prepare sound stuff
\r
1127 if(currentConfig.EmuOpt & 4) {
\r
1128 int snd_excess_add;
\r
1129 if(PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old || crashed_940) {
\r
1130 /* if 940 is turned off, we need it to be put back to sleep */
\r
1131 if (!(PicoOpt&0x200) && ((PicoOpt^PicoOpt_old)&0x200)) {
\r
1137 //excess_samples = PsndRate - PsndLen*target_fps;
\r
1138 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
\r
1139 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
1140 gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);
\r
1141 gp2x_sound_volume(currentConfig.volume, currentConfig.volume);
\r
1142 PicoWriteSound = updateSound;
\r
1143 memset(sndBuffer, 0, sizeof(sndBuffer));
\r
1144 PsndOut = sndBuffer;
\r
1145 PsndRate_old = PsndRate;
\r
1146 PsndLen_real = PsndLen;
\r
1147 PicoOpt_old = PicoOpt;
\r
1148 pal_old = Pico.m.pal;
\r
1153 // prepare CD buffer
\r
1154 if (PicoMCD & 1) PicoCDBufferInit();
\r
1156 // calc vsync offset to sync timing code with vsync
\r
1157 if (currentConfig.EmuOpt&0x2000) {
\r
1158 gettimeofday(&tval, 0);
\r
1159 gp2x_video_wait_vsync();
\r
1160 gettimeofday(&tval, 0);
\r
1161 vsync_offset = tval.tv_usec;
\r
1162 while (vsync_offset >= target_frametime)
\r
1163 vsync_offset -= target_frametime;
\r
1164 if (!vsync_offset) vsync_offset++;
\r
1165 printf("vsync_offset: %i\n", vsync_offset);
\r
1170 while (engineState == PGS_Running)
\r
1174 gettimeofday(&tval, 0);
\r
1175 if(reset_timing) {
\r
1177 thissec = tval.tv_sec;
\r
1178 frames_shown = frames_done = tval.tv_usec/target_frametime;
\r
1181 // show notice message?
\r
1182 if(noticeMsgTime.tv_sec) {
\r
1183 static int noticeMsgSum;
\r
1184 if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec
\r
1185 noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;
\r
1189 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
\r
1190 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
\r
1191 notice = noticeMsg;
\r
1195 // check for mode changes
\r
1196 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
\r
1197 if (modes != oldmodes) {
\r
1199 osd_fps_x = OSD_FPS_X;
\r
1201 vidCpyM2 = vidCpyM2_40col;
\r
1203 if (PicoOpt & 0x100) {
\r
1204 vidCpyM2 = vidCpyM2_32col_nobord;
\r
1206 osd_fps_x = OSD_FPS_X - 64;
\r
1208 vidCpyM2 = vidCpyM2_32col;
\r
1211 if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode
\r
1212 gp2x_video_RGB_setscaling(8, scalex, 224);
\r
1213 else gp2x_video_RGB_setscaling(0, scalex, 240);
\r
1218 // second changed?
\r
1219 if(thissec != tval.tv_sec) {
\r
1221 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
\r
1222 if(++bench == 10) {
\r
1224 bench_fps_s = bench_fps;
\r
1225 bf[bfp++ & 3] = bench_fps;
\r
1228 bench_fps += frames_shown;
\r
1229 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
\r
1231 if(currentConfig.EmuOpt & 2)
\r
1232 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
\r
1234 thissec = tval.tv_sec;
\r
1236 if(PsndOut == 0 && currentConfig.Frameskip >= 0) {
\r
1237 frames_done = frames_shown = 0;
\r
1239 // it is quite common for this implementation to leave 1 fame unfinished
\r
1240 // when second changes, but we don't want buffer to starve.
\r
1241 if(PsndOut && frames_done < target_fps && frames_done > target_fps-5) {
\r
1243 SkipFrame(1); frames_done++;
\r
1246 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
\r
1247 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
\r
1248 if (frames_shown > frames_done) frames_shown = frames_done;
\r
1252 lim_time = (frames_done+1) * target_frametime + vsync_offset;
\r
1253 if(currentConfig.Frameskip >= 0) { // frameskip enabled
\r
1254 for(i = 0; i < currentConfig.Frameskip; i++) {
\r
1256 SkipFrame(1); frames_done++;
\r
1257 if (PsndOut) { // do framelimitting if sound is enabled
\r
1258 gettimeofday(&tval, 0);
\r
1259 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
1260 if(tval.tv_usec < lim_time) { // we are too fast
\r
1261 simpleWait(thissec, lim_time);
\r
1264 lim_time += target_frametime;
\r
1266 } else if(tval.tv_usec > lim_time) { // auto frameskip
\r
1267 // no time left for this frame - skip
\r
1268 if (tval.tv_usec - lim_time >= 0x300000) {
\r
1269 /* something caused a slowdown for us (disk access? cache flush?)
\r
1270 * try to recover by resetting timing... */
\r
1275 SkipFrame(tval.tv_usec < lim_time+target_frametime*2); frames_done++;
\r
1283 if (Pico.m.frame_count == 31563) {
\r
1285 f = fopen("ram_p.bin", "wb");
\r
1286 if (!f) { printf("!f\n"); exit(1); }
\r
1287 fwrite(Pico.ram, 1, 0x10000, f);
\r
1295 #define BYTE unsigned char
\r
1296 #define WORD unsigned short
\r
1299 BYTE IDLength; /* 00h Size of Image ID field */
\r
1300 BYTE ColorMapType; /* 01h Color map type */
\r
1301 BYTE ImageType; /* 02h Image type code */
\r
1302 WORD CMapStart; /* 03h Color map origin */
\r
1303 WORD CMapLength; /* 05h Color map length */
\r
1304 BYTE CMapDepth; /* 07h Depth of color map entries */
\r
1305 WORD XOffset; /* 08h X origin of image */
\r
1306 WORD YOffset; /* 0Ah Y origin of image */
\r
1307 WORD Width; /* 0Ch Width of image */
\r
1308 WORD Height; /* 0Eh Height of image */
\r
1309 BYTE PixelDepth; /* 10h Image pixel size */
\r
1310 BYTE ImageDescriptor; /* 11h Image descriptor byte */
\r
1311 } __attribute__((packed)) TGAHEAD;
\r
1312 static unsigned short oldscr[320*240];
\r
1313 FILE *f; char name[128]; int i;
\r
1315 memset(&TGAHEAD, 0, sizeof(TGAHEAD));
\r
1316 TGAHEAD.ImageType = 2;
\r
1317 TGAHEAD.Width = 320;
\r
1318 TGAHEAD.Height = 240;
\r
1319 TGAHEAD.PixelDepth = 16;
\r
1320 TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left
\r
1322 #define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?
\r
1324 for (i = 0; i < 320*240; i++)
\r
1325 if(oldscr[i] != CONV(((unsigned short *)gp2x_screen)[i])) break;
\r
1328 for (i = 0; i < 320*240; i++)
\r
1329 oldscr[i] = CONV(((unsigned short *)gp2x_screen)[i]);
\r
1330 sprintf(name, "%05i.tga", Pico.m.frame_count);
\r
1331 f = fopen(name, "wb");
\r
1332 if (!f) { printf("!f\n"); exit(1); }
\r
1333 fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);
\r
1334 fwrite(oldscr, 1, 320*240*2, f);
\r
1341 gettimeofday(&tval, 0);
\r
1342 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
1344 if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 0x300000) // slowdown detection
\r
1346 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
\r
1348 // sleep or vsync if we are still too fast
\r
1349 // usleep sleeps for ~20ms minimum, so it is not a solution here
\r
1350 if(tval.tv_usec < lim_time)
\r
1352 // we are too fast
\r
1353 if (vsync_offset) {
\r
1354 if (lim_time - tval.tv_usec > target_frametime/2)
\r
1355 simpleWait(thissec, lim_time - target_frametime/4);
\r
1356 gp2x_video_wait_vsync();
\r
1358 simpleWait(thissec, lim_time);
\r
1363 blit(fpsbuff, notice);
\r
1365 frames_done++; frames_shown++;
\r
1369 if (PicoMCD & 1) PicoCDBufferFree();
\r
1372 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
1373 emu_state_cb("Writing SRAM/BRAM..");
\r
1374 emu_SaveLoadGame(0, 1);
\r
1378 // if in 16bit mode, generate 8it image for menu background
\r
1379 if (!(PicoOpt&0x10) && (currentConfig.EmuOpt&0x80))
\r
1380 emu_forced_frame();
\r
1383 gp2x_memcpy_buffers((1<<2), gp2x_screen, 0, 320*240*2);
\r
1387 void emu_ResetGame(void)
\r
1394 size_t gzRead2(void *p, size_t _size, size_t _n, void *file)
\r
1396 return gzread(file, p, _n);
\r
1400 size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)
\r
1402 return gzwrite(file, p, _n);
\r
1405 static int try_ropen_file(const char *fname)
\r
1409 f = fopen(fname, "rb");
\r
1417 char *emu_GetSaveFName(int load, int is_sram, int slot)
\r
1419 static char saveFname[512];
\r
1424 romfname_ext(saveFname, (PicoMCD&1) ? "brm/" : "srm/", (PicoMCD&1) ? ".brm" : ".srm");
\r
1426 if (try_ropen_file(saveFname)) return saveFname;
\r
1427 // try in current dir..
\r
1428 romfname_ext(saveFname, NULL, (PicoMCD&1) ? ".brm" : ".srm");
\r
1429 if (try_ropen_file(saveFname)) return saveFname;
\r
1430 return NULL; // give up
\r
1436 if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);
\r
1437 strcat(ext, (currentConfig.EmuOpt & 8) ? ".mds.gz" : ".mds");
\r
1439 romfname_ext(saveFname, "mds/", ext);
\r
1441 if (try_ropen_file(saveFname)) return saveFname;
\r
1442 romfname_ext(saveFname, NULL, ext);
\r
1443 if (try_ropen_file(saveFname)) return saveFname;
\r
1444 if (currentConfig.EmuOpt & 8) {
\r
1446 if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);
\r
1447 strcat(ext, ".mds");
\r
1449 romfname_ext(saveFname, "mds/", ext);
\r
1450 if (try_ropen_file(saveFname)) return saveFname;
\r
1451 romfname_ext(saveFname, NULL, ext);
\r
1452 if (try_ropen_file(saveFname)) return saveFname;
\r
1461 int emu_check_save_file(int slot)
\r
1463 return emu_GetSaveFName(1, 0, slot) ? 1 : 0;
\r
1466 void emu_set_save_cbs(int gz)
\r
1469 areaRead = gzRead2;
\r
1470 areaWrite = gzWrite2;
\r
1471 areaEof = (areaeof *) gzeof;
\r
1472 areaSeek = (areaseek *) gzseek;
\r
1473 areaClose = (areaclose *) gzclose;
\r
1475 areaRead = (arearw *) fread;
\r
1476 areaWrite = (arearw *) fwrite;
\r
1477 areaEof = (areaeof *) feof;
\r
1478 areaSeek = (areaseek *) fseek;
\r
1479 areaClose = (areaclose *) fclose;
\r
1483 int emu_SaveLoadGame(int load, int sram)
\r
1488 // make save filename
\r
1489 saveFname = emu_GetSaveFName(load, sram, state_slot);
\r
1490 if (saveFname == NULL) {
\r
1492 strcpy(noticeMsg, load ? "LOAD FAILED (missing file)" : "SAVE FAILED ");
\r
1493 gettimeofday(¬iceMsgTime, 0);
\r
1498 printf("saveLoad (%i, %i): %s\n", load, sram, saveFname);
\r
1503 unsigned char *sram_data;
\r
1506 if (PicoOpt&0x8000) { // MCD RAM cart?
\r
1507 sram_size = 0x12000;
\r
1508 sram_data = SRam.data;
\r
1510 memcpy32((int *)sram_data, (int *)Pico_mcd->bram, 0x2000/4);
\r
1512 sram_size = 0x2000;
\r
1513 sram_data = Pico_mcd->bram;
\r
1514 truncate = 0; // the .brm may contain RAM cart data after normal brm
\r
1517 sram_size = SRam.end-SRam.start+1;
\r
1518 if(SRam.reg_back & 4) sram_size=0x2000;
\r
1519 sram_data = SRam.data;
\r
1521 if (!sram_data) return 0; // SRam forcefully disabled for this game
\r
1524 sramFile = fopen(saveFname, "rb");
\r
1525 if(!sramFile) return -1;
\r
1526 fread(sram_data, 1, sram_size, sramFile);
\r
1528 if ((PicoMCD&1) && (PicoOpt&0x8000))
\r
1529 memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4);
\r
1531 // sram save needs some special processing
\r
1532 // see if we have anything to save
\r
1533 for (; sram_size > 0; sram_size--)
\r
1534 if (sram_data[sram_size-1]) break;
\r
1537 sramFile = fopen(saveFname, truncate ? "wb" : "r+b");
\r
1538 if (!sramFile) sramFile = fopen(saveFname, "wb"); // retry
\r
1539 if (!sramFile) return -1;
\r
1540 ret = fwrite(sram_data, 1, sram_size, sramFile);
\r
1541 ret = (ret != sram_size) ? -1 : 0;
\r
1552 void *PmovFile = NULL;
\r
1553 if (strcmp(saveFname + strlen(saveFname) - 3, ".gz") == 0) {
\r
1554 if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) {
\r
1555 emu_set_save_cbs(1);
\r
1556 if(!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);
\r
1561 if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) {
\r
1562 emu_set_save_cbs(0);
\r
1566 ret = PmovState(load ? 6 : 5, PmovFile);
\r
1567 areaClose(PmovFile);
\r
1569 if (load) Pico.m.dirtyPal=1;
\r
1576 strcpy(noticeMsg, load ? "GAME LOADED " : "GAME SAVED ");
\r
1579 strcpy(noticeMsg, load ? "LOAD FAILED " : "SAVE FAILED ");
\r
1583 gettimeofday(¬iceMsgTime, 0);
\r