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 "../common/arm_utils.h"
\r
22 #include "../common/fonts.h"
\r
23 #include "cpuctrl.h"
\r
25 #include <Pico/PicoInt.h>
\r
26 #include <Pico/Patch.h>
\r
27 #include <zlib/zlib.h>
\r
32 #define OSD_FPS_X 220
\r
34 #define OSD_FPS_X 260
\r
39 int select_exits = 0;
\r
40 char *PicoConfigFile = "picoconfig.bin";
\r
41 currentConfig_t currentConfig;
\r
43 char romFileName[PATH_MAX];
\r
44 unsigned char *rom_data = NULL;
\r
46 extern int crashed_940;
\r
48 static short sndBuffer[2*44100/50];
\r
49 static char noticeMsg[64]; // notice msg to draw
\r
50 static struct timeval noticeMsgTime = { 0, 0 }; // when started showing
\r
51 static int osd_fps_x;
\r
52 static int combo_keys = 0, combo_acts = 0; // keys and actions which need button combos
\r
53 static int gp2x_old_gamma = 100;
\r
54 static unsigned char *movie_data = NULL;
\r
55 static int movie_size = 0;
\r
56 unsigned char *PicoDraw2FB = NULL; // temporary buffer for alt renderer
\r
58 int reset_timing = 0;
\r
59 int config_slot = 0, config_slot_current = 0;
\r
63 static void strlwr(char* string)
\r
65 while ( (*string++ = (char)tolower(*string)) );
\r
68 static int try_rfn_cut(void)
\r
73 p = romFileName + strlen(romFileName) - 1;
\r
74 for (; p > romFileName; p--)
\r
75 if (*p == '.') break;
\r
78 if((tmp = fopen(romFileName, "rb"))) {
\r
85 static void get_ext(char *file, char *ext)
\r
89 p = file + strlen(file) - 4;
\r
90 if (p < file) p = file;
\r
96 char *biosfiles_us[] = { "us_scd2_9306", "SegaCDBIOS9303", "us_scd1_9210" };
\r
97 char *biosfiles_eu[] = { "eu_mcd2_9306", "eu_mcd2_9303", "eu_mcd1_9210" };
\r
98 char *biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };
\r
100 extern char **g_argv;
\r
102 int find_bios(int region, char **bios_file)
\r
104 static char bios_path[1024];
\r
109 if (region == 4) { // US
\r
110 files = biosfiles_us;
\r
111 count = sizeof(biosfiles_us) / sizeof(char *);
\r
112 } else if (region == 8) { // EU
\r
113 files = biosfiles_eu;
\r
114 count = sizeof(biosfiles_eu) / sizeof(char *);
\r
115 } else if (region == 1 || region == 2) {
\r
116 files = biosfiles_jp;
\r
117 count = sizeof(biosfiles_jp) / sizeof(char *);
\r
122 for (i = 0; i < count; i++)
\r
124 strncpy(bios_path, g_argv[0], 1023);
\r
125 bios_path[1024-32] = 0;
\r
126 for (j = strlen(bios_path); j > 0; j--)
\r
127 if (bios_path[j] == '/') { bios_path[j+1] = 0; break; }
\r
128 strcat(bios_path, files[i]);
\r
129 strcat(bios_path, ".bin");
\r
130 f = fopen(bios_path, "rb");
\r
133 bios_path[strlen(bios_path) - 4] = 0;
\r
134 strcat(bios_path, ".zip");
\r
135 f = fopen(bios_path, "rb");
\r
140 printf("using bios: %s\n", bios_path);
\r
142 if (bios_file) *bios_file = bios_path;
\r
145 sprintf(menuErrorMsg, "no %s BIOS files found, read docs",
\r
146 region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
\r
147 printf("%s\n", menuErrorMsg);
\r
152 /* checks if romFileName points to valid MegaCD image
\r
153 * if so, checks for suitable BIOS */
\r
154 int emu_cd_check(char **bios_file)
\r
156 unsigned char buf[32];
\r
158 int type = 0, region = 4; // 1: Japan, 4: US, 8: Europe
\r
160 cd_f = pm_open(romFileName);
\r
161 if (!cd_f) return 0; // let the upper level handle this
\r
163 if (pm_read(buf, 32, cd_f) != 32) {
\r
168 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) type = 1; // Sega CD (ISO)
\r
169 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) type = 2; // Sega CD (BIN)
\r
175 /* it seems we have a CD image here. Try to detect region now.. */
\r
176 pm_seek(cd_f, (type == 1) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);
\r
177 pm_read(buf, 1, cd_f);
\r
180 if (buf[0] == 0x64) region = 8; // EU
\r
181 if (buf[0] == 0xa1) region = 1; // JAP
\r
183 printf("detected %s Sega/Mega CD image with %s region\n",
\r
184 type == 2 ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
\r
186 if (PicoRegionOverride) {
\r
187 region = PicoRegionOverride;
\r
188 printf("overrided region to %s\n", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
\r
191 if (bios_file == NULL) return type;
\r
193 if (find_bios(region, bios_file))
\r
194 return type; // CD and BIOS detected
\r
196 return -1; // CD detected but load failed
\r
199 int emu_ReloadRom(void)
\r
201 unsigned int rom_size = 0;
\r
202 char *used_rom_name = romFileName;
\r
207 printf("emu_ReloadRom(%s)\n", romFileName);
\r
209 get_ext(romFileName, ext);
\r
211 // detect wrong extensions
\r
212 if(!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz
\r
213 sprintf(menuErrorMsg, "Not a ROM selected.");
\r
219 // check for movie file
\r
224 if(!strcmp(ext, ".gmv")) {
\r
225 // check for both gmv and rom
\r
227 FILE *movie_file = fopen(romFileName, "rb");
\r
229 sprintf(menuErrorMsg, "Failed to open movie.");
\r
232 fseek(movie_file, 0, SEEK_END);
\r
233 movie_size = ftell(movie_file);
\r
234 fseek(movie_file, 0, SEEK_SET);
\r
235 if(movie_size < 64+3) {
\r
236 sprintf(menuErrorMsg, "Invalid GMV file.");
\r
237 fclose(movie_file);
\r
240 movie_data = malloc(movie_size);
\r
241 if(movie_data == NULL) {
\r
242 sprintf(menuErrorMsg, "low memory.");
\r
243 fclose(movie_file);
\r
246 fread(movie_data, 1, movie_size, movie_file);
\r
247 fclose(movie_file);
\r
248 if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {
\r
249 sprintf(menuErrorMsg, "Invalid GMV file.");
\r
252 dummy = try_rfn_cut() || try_rfn_cut();
\r
254 sprintf(menuErrorMsg, "Could't find a ROM for movie.");
\r
257 get_ext(romFileName, ext);
\r
259 else if (!strcmp(ext, ".pat")) {
\r
261 PicoPatchLoad(romFileName);
\r
262 dummy = try_rfn_cut() || try_rfn_cut();
\r
264 sprintf(menuErrorMsg, "Could't find a ROM to patch.");
\r
267 get_ext(romFileName, ext);
\r
270 if ((PicoMCD & 1) && Pico_mcd != NULL)
\r
273 // check for MegaCD image
\r
274 cd_state = emu_cd_check(&used_rom_name);
\r
275 if (cd_state > 0) {
\r
277 get_ext(used_rom_name, ext);
\r
278 } else if (cd_state == -1) {
\r
282 if (PicoMCD & 1) Stop_CD();
\r
286 rom = pm_open(used_rom_name);
\r
288 sprintf(menuErrorMsg, "Failed to open rom.");
\r
292 menu_romload_prepare(used_rom_name);
\r
300 if( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) {
\r
301 sprintf(menuErrorMsg, "PicoCartLoad() failed.");
\r
302 printf("%s\n", menuErrorMsg);
\r
304 menu_romload_end();
\r
308 menu_romload_end();
\r
310 // detect wrong files (Pico crashes on very small files), also see if ROM EP is good
\r
311 if(rom_size <= 0x200 || strncmp((char *)rom_data, "Pico", 4) == 0 ||
\r
312 ((*(unsigned char *)(rom_data+4)<<16)|(*(unsigned short *)(rom_data+6))) >= (int)rom_size) {
\r
313 if (rom_data) free(rom_data);
\r
315 sprintf(menuErrorMsg, "Not a ROM selected.");
\r
319 // load config for this ROM (do this before insert to get correct region)
\r
320 ret = emu_ReadConfig(1, 1);
\r
322 emu_ReadConfig(0, 1);
\r
324 printf("PicoCartInsert(%p, %d);\n", rom_data, rom_size);
\r
325 if(PicoCartInsert(rom_data, rom_size)) {
\r
326 sprintf(menuErrorMsg, "Failed to load ROM.");
\r
330 Pico.m.frame_count = 0;
\r
332 // insert CD if it was detected
\r
333 if (cd_state > 0) {
\r
334 ret = Insert_CD(romFileName, cd_state == 2);
\r
336 sprintf(menuErrorMsg, "Insert_CD() failed, invalid CD image?");
\r
337 printf("%s\n", menuErrorMsg);
\r
342 // emu_ReadConfig() might have messed currentConfig.lastRomFile
\r
343 strncpy(currentConfig.lastRomFile, romFileName, sizeof(currentConfig.lastRomFile)-1);
\r
344 currentConfig.lastRomFile[sizeof(currentConfig.lastRomFile)-1] = 0;
\r
347 PicoPatchPrepare();
\r
351 // additional movie stuff
\r
353 if(movie_data[0x14] == '6')
\r
354 PicoOpt |= 0x20; // 6 button pad
\r
355 else PicoOpt &= ~0x20;
\r
356 PicoOpt |= 0x10040; // accurate timing, no VDP fifo timing
\r
357 if(movie_data[0xF] >= 'A') {
\r
358 if(movie_data[0x16] & 0x80) {
\r
359 PicoRegionOverride = 8;
\r
361 PicoRegionOverride = 4;
\r
364 // TODO: bits 6 & 5
\r
366 movie_data[0x18+30] = 0;
\r
367 sprintf(noticeMsg, "MOVIE: %s", (char *) &movie_data[0x18]);
\r
371 PicoOpt &= ~0x10000;
\r
373 strcpy(noticeMsg, "PAL SYSTEM / 50 FPS");
\r
375 strcpy(noticeMsg, "NTSC SYSTEM / 60 FPS");
\r
378 gettimeofday(¬iceMsgTime, 0);
\r
380 // load SRAM for this ROM
\r
381 if(currentConfig.EmuOpt & 1)
\r
382 emu_SaveLoadGame(1, 1);
\r
388 static void emu_msg_cb(const char *msg);
\r
389 static void emu_msg_tray_open(void);
\r
391 void emu_Init(void)
\r
393 // make temp buffer for alt renderer
\r
394 PicoDraw2FB = malloc((8+320)*(8+240+8));
\r
397 printf("PicoDraw2FB == 0\n");
\r
400 // make dirs for saves, cfgs, etc.
\r
401 mkdir("mds", 0777);
\r
402 mkdir("srm", 0777);
\r
403 mkdir("brm", 0777);
\r
404 mkdir("cfg", 0777);
\r
407 PicoMessage = emu_msg_cb;
\r
408 PicoMCDopenTray = emu_msg_tray_open;
\r
409 PicoMCDcloseTray = menu_loop_tray;
\r
413 static void romfname_ext(char *dst, const char *prefix, const char *ext)
\r
416 int prefix_len = 0;
\r
418 // make save filename
\r
419 for (p = romFileName+strlen(romFileName)-1; p >= romFileName && *p != '/'; p--); p++;
\r
422 strcpy(dst, prefix);
\r
423 prefix_len = strlen(prefix);
\r
425 strncpy(dst + prefix_len, p, 511-prefix_len);
\r
427 if (dst[strlen(dst)-4] == '.') dst[strlen(dst)-4] = 0;
\r
428 if (ext) strcat(dst, ext);
\r
432 static void find_combos(void)
\r
436 // find out which keys and actions are combos
\r
437 combo_keys = combo_acts = 0;
\r
438 for (act = 0; act < 32; act++)
\r
441 if (act == 16) continue; // player2 flag
\r
442 for (u = 0; u < 32; u++)
\r
444 if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;
\r
448 // loop again and mark those keys and actions as combo
\r
449 for (u = 0; u < 32; u++)
\r
451 if (currentConfig.KeyBinds[u] & (1 << act)) {
\r
452 combo_keys |= 1 << u;
\r
453 combo_acts |= 1 << act;
\r
458 // printf("combo keys/acts: %08x %08x\n", combo_keys, combo_acts);
\r
462 void scaling_update(void)
\r
464 PicoOpt &= ~0x4100;
\r
465 switch (currentConfig.scaling) {
\r
466 default: break; // off
\r
468 case 2: PicoOpt |= 0x0100; break; // hw hor+vert
\r
469 case 3: PicoOpt |= 0x4000; break; // sw hor
\r
474 int emu_ReadConfig(int game, int no_defaults)
\r
477 char cfg[512], extbuf[16];
\r
484 // set default config
\r
485 memset(¤tConfig, 0, sizeof(currentConfig));
\r
486 currentConfig.lastRomFile[0] = 0;
\r
487 currentConfig.EmuOpt = 0x1f | 0x600; // | confirm_save, cd_leds
\r
488 currentConfig.PicoOpt = 0x0f | 0xe00; // | use_940, cd_pcm, cd_cdda
\r
489 currentConfig.PsndRate = 22050; // 44100;
\r
490 currentConfig.PicoRegion = 0; // auto
\r
491 currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP
\r
492 currentConfig.Frameskip = -1; // auto
\r
493 currentConfig.CPUclock = 200;
\r
494 currentConfig.volume = 50;
\r
495 currentConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU
\r
496 currentConfig.KeyBinds[ 4] = 1<<1;
\r
497 currentConfig.KeyBinds[ 2] = 1<<2;
\r
498 currentConfig.KeyBinds[ 6] = 1<<3;
\r
499 currentConfig.KeyBinds[14] = 1<<4;
\r
500 currentConfig.KeyBinds[13] = 1<<5;
\r
501 currentConfig.KeyBinds[12] = 1<<6;
\r
502 currentConfig.KeyBinds[ 8] = 1<<7;
\r
503 currentConfig.KeyBinds[15] = 1<<26; // switch rend
\r
504 currentConfig.KeyBinds[10] = 1<<27; // save state
\r
505 currentConfig.KeyBinds[11] = 1<<28; // load state
\r
506 currentConfig.KeyBinds[23] = 1<<29; // vol up
\r
507 currentConfig.KeyBinds[22] = 1<<30; // vol down
\r
508 currentConfig.gamma = 100;
\r
509 currentConfig.PicoCDBuffers = 64;
\r
510 currentConfig.scaling = 0;
\r
512 strncpy(cfg, PicoConfigFile, 511);
\r
513 if (config_slot != 0)
\r
515 char *p = strrchr(cfg, '.');
\r
516 if (p == NULL) p = cfg + strlen(cfg);
\r
517 sprintf(extbuf, ".%i.pbcfg", config_slot);
\r
518 strncpy(p, extbuf, 511 - (p - cfg));
\r
522 if (config_slot != 0)
\r
523 sprintf(extbuf, ".%i.pbcfg", config_slot);
\r
524 else strcpy(extbuf, ".pbcfg");
\r
525 romfname_ext(cfg, "cfg/", extbuf);
\r
526 f = fopen(cfg, "rb");
\r
527 if (!f) romfname_ext(cfg, NULL, ".pbcfg");
\r
531 printf("emu_ReadConfig: %s ", cfg);
\r
532 f = fopen(cfg, "rb");
\r
534 bread = fread(¤tConfig, 1, sizeof(currentConfig), f);
\r
537 printf(bread > 0 ? "(ok)\n" : "(failed)\n");
\r
539 PicoOpt = currentConfig.PicoOpt;
\r
540 PsndRate = currentConfig.PsndRate;
\r
541 PicoRegionOverride = currentConfig.PicoRegion;
\r
542 PicoAutoRgnOrder = currentConfig.PicoAutoRgnOrder;
\r
543 PicoCDBuffers = currentConfig.PicoCDBuffers;
\r
545 // some sanity checks
\r
546 if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;
\r
547 if (currentConfig.gamma < 10 || currentConfig.gamma > 300) currentConfig.gamma = 100;
\r
548 if (currentConfig.volume < 0 || currentConfig.volume > 99) currentConfig.volume = 50;
\r
549 // if volume keys are unbound, bind them to volume control
\r
550 if (!currentConfig.KeyBinds[23] && !currentConfig.KeyBinds[22]) {
\r
551 currentConfig.KeyBinds[23] = 1<<29; // vol up
\r
552 currentConfig.KeyBinds[22] = 1<<30; // vol down
\r
555 if (bread > 0) config_slot_current = config_slot;
\r
556 return (bread > 0); // == sizeof(currentConfig));
\r
560 int emu_WriteConfig(int game)
\r
563 char cfg[512], extbuf[16];
\r
568 strncpy(cfg, PicoConfigFile, 511);
\r
569 if (config_slot != 0)
\r
571 char *p = strrchr(cfg, '.');
\r
572 if (p == NULL) p = cfg + strlen(cfg);
\r
573 sprintf(extbuf, ".%i.pbcfg", config_slot);
\r
574 strncpy(p, extbuf, 511 - (p - cfg));
\r
578 if (config_slot != 0)
\r
579 sprintf(extbuf, ".%i.pbcfg", config_slot);
\r
580 else strcpy(extbuf, ".pbcfg");
\r
581 romfname_ext(cfg, "cfg/", extbuf);
\r
584 printf("emu_WriteConfig: %s ", cfg);
\r
585 f = fopen(cfg, "wb");
\r
587 currentConfig.PicoOpt = PicoOpt;
\r
588 currentConfig.PsndRate = PsndRate;
\r
589 currentConfig.PicoRegion = PicoRegionOverride;
\r
590 currentConfig.PicoAutoRgnOrder = PicoAutoRgnOrder;
\r
591 currentConfig.PicoCDBuffers = PicoCDBuffers;
\r
592 bwrite = fwrite(¤tConfig, 1, sizeof(currentConfig), f);
\r
599 printf((bwrite == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n");
\r
601 if (bwrite == sizeof(currentConfig)) config_slot_current = config_slot;
\r
602 return (bwrite == sizeof(currentConfig));
\r
606 void emu_Deinit(void)
\r
609 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
610 emu_SaveLoadGame(0, 1);
\r
614 if (!(currentConfig.EmuOpt & 0x20)) {
\r
615 FILE *f = fopen(PicoConfigFile, "r+b");
\r
616 if (!f) emu_WriteConfig(0);
\r
618 // if we already have config, reload it, except last ROM
\r
619 fseek(f, sizeof(currentConfig.lastRomFile), SEEK_SET);
\r
620 fread(¤tConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f);
\r
621 fseek(f, 0, SEEK_SET);
\r
622 fwrite(¤tConfig, 1, sizeof(currentConfig), f);
\r
636 if (gp2x_old_gamma != 100)
\r
640 static void text_out8_builtin(int x, int y, const char *text)
\r
642 int i,l,len=strlen(text);
\r
643 unsigned char *screen = (unsigned char *)gp2x_screen + x + y*320;
\r
645 /* always using built-in font */
\r
646 for (i = 0; i < len; i++)
\r
650 unsigned char fd = fontdata8x8[((text[i])*8)+l];
\r
651 if (fd&0x80) screen[l*320+0]=0xf0;
\r
652 if (fd&0x40) screen[l*320+1]=0xf0;
\r
653 if (fd&0x20) screen[l*320+2]=0xf0;
\r
654 if (fd&0x10) screen[l*320+3]=0xf0;
\r
655 if (fd&0x08) screen[l*320+4]=0xf0;
\r
656 if (fd&0x04) screen[l*320+5]=0xf0;
\r
657 if (fd&0x02) screen[l*320+6]=0xf0;
\r
658 if (fd&0x01) screen[l*320+7]=0xf0;
\r
664 static void text_out16_builtin(int x, int y, const char *text)
\r
666 int i,l,len=strlen(text);
\r
667 unsigned short *screen = (unsigned short *)gp2x_screen + x + y*320;
\r
669 for (i = 0; i < len; i++)
\r
673 unsigned char fd = fontdata8x8[((text[i])*8)+l];
\r
674 if(fd&0x80) screen[l*320+0]=0xffff;
\r
675 if(fd&0x40) screen[l*320+1]=0xffff;
\r
676 if(fd&0x20) screen[l*320+2]=0xffff;
\r
677 if(fd&0x10) screen[l*320+3]=0xffff;
\r
678 if(fd&0x08) screen[l*320+4]=0xffff;
\r
679 if(fd&0x04) screen[l*320+5]=0xffff;
\r
680 if(fd&0x02) screen[l*320+6]=0xffff;
\r
681 if(fd&0x01) screen[l*320+7]=0xffff;
\r
688 void osd_text(int x, int y, const char *text)
\r
690 int len = strlen(text)*8;
\r
692 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
694 x &= ~3; // align x
\r
695 len = (len+3) >> 2;
\r
696 for (h = 0; h < 8; h++) {
\r
697 p = (int *) ((unsigned char *) gp2x_screen+x+320*(y+h));
\r
698 for (i = len; i; i--, p++) *p = 0xe0e0e0e0;
\r
700 text_out8_builtin(x, y, text);
\r
703 x &= ~1; // align x
\r
704 len = (len+1) >> 1;
\r
705 for (h = 0; h < 8; h++) {
\r
706 p = (int *) ((unsigned short *) gp2x_screen+x+320*(y+h));
\r
707 for (i = len; i; i--, p++) *p = (*p>>2)&0x39e7;
\r
709 text_out16_builtin(x, y, text);
\r
713 static void cd_leds(void)
\r
718 // if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change
\r
719 old_reg = Pico_mcd->s68k_regs[0];
\r
721 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
723 unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;
\r
724 unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;
\r
725 *(unsigned int *)((char *)gp2x_screen + 320*2+ 4) =
\r
726 *(unsigned int *)((char *)gp2x_screen + 320*3+ 4) =
\r
727 *(unsigned int *)((char *)gp2x_screen + 320*4+ 4) = col_g;
\r
728 *(unsigned int *)((char *)gp2x_screen + 320*2+12) =
\r
729 *(unsigned int *)((char *)gp2x_screen + 320*3+12) =
\r
730 *(unsigned int *)((char *)gp2x_screen + 320*4+12) = col_r;
\r
733 unsigned int *p = (unsigned int *)((short *)gp2x_screen + 320*2+4);
\r
734 unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;
\r
735 unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;
\r
736 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
737 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
738 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
742 static int EmuScan16(unsigned int num, void *sdata)
\r
744 if (!(Pico.video.reg[1]&8)) num += 8;
\r
745 DrawLineDest = (unsigned short *) gp2x_screen + 320*(num+1);
\r
750 static int EmuScan8(unsigned int num, void *sdata)
\r
752 if (!(Pico.video.reg[1]&8)) num += 8;
\r
753 DrawLineDest = (unsigned char *) gp2x_screen + 320*(num+1);
\r
758 int localPal[0x100];
\r
759 static void (*vidCpyM2)(void *dest, void *src) = NULL;
\r
761 static void blit(const char *fps, const char *notice)
\r
763 int emu_opt = currentConfig.EmuOpt;
\r
765 if (PicoOpt&0x10) {
\r
766 // 8bit fast renderer
\r
767 if (Pico.m.dirtyPal) {
\r
768 Pico.m.dirtyPal = 0;
\r
769 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
770 // feed new palette to our device
\r
771 gp2x_video_setpalette(localPal, 0x40);
\r
773 vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);
\r
774 } else if (!(emu_opt&0x80)) {
\r
775 // 8bit accurate renderer
\r
776 if (Pico.m.dirtyPal) {
\r
777 Pico.m.dirtyPal = 0;
\r
778 if(Pico.video.reg[0xC]&8) { // shadow/hilight mode
\r
779 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
780 vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
\r
781 vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);
\r
782 blockcpy(localPal+0xc0, localPal+0x40, 0x40*4);
\r
783 localPal[0xc0] = 0x0000c000;
\r
784 localPal[0xd0] = 0x00c00000;
\r
785 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
786 localPal[0xf0] = 0x00ffffff;
\r
787 gp2x_video_setpalette(localPal, 0x100);
\r
788 } else if (rendstatus & 0x20) { // mid-frame palette changes
\r
789 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
790 vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);
\r
791 vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);
\r
792 gp2x_video_setpalette(localPal, 0xc0);
\r
794 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
795 gp2x_video_setpalette(localPal, 0x40);
\r
800 if (notice || (emu_opt & 2)) {
\r
802 if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8)) h -= 8;
\r
803 if (notice) osd_text(4, h, notice);
\r
805 osd_text(osd_fps_x, h, fps);
\r
807 if ((emu_opt & 0x400) && (PicoMCD & 1))
\r
810 //gp2x_video_wait_vsync();
\r
813 if (!(PicoOpt&0x10)) {
\r
814 if (!(Pico.video.reg[1]&8)) {
\r
815 if (currentConfig.EmuOpt&0x80) {
\r
816 DrawLineDest = (unsigned short *) gp2x_screen + 320*8;
\r
818 DrawLineDest = (unsigned char *) gp2x_screen + 320*8;
\r
821 DrawLineDest = gp2x_screen;
\r
827 // clears whole screen or just the notice area (in all buffers)
\r
828 static void clearArea(int full)
\r
830 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
832 if (full) gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
833 else gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
\r
835 // 16bit accurate renderer
\r
836 if (full) gp2x_memset_all_buffers(0, 0, 320*240*2);
\r
837 else gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
\r
842 static void vidResetMode(void)
\r
844 if (PicoOpt&0x10) {
\r
845 gp2x_video_changemode(8);
\r
846 } else if (currentConfig.EmuOpt&0x80) {
\r
847 gp2x_video_changemode(16);
\r
848 PicoDrawSetColorFormat(1);
\r
849 PicoScan = EmuScan16;
\r
852 gp2x_video_changemode(8);
\r
853 PicoDrawSetColorFormat(2);
\r
854 PicoScan = EmuScan8;
\r
857 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
858 // setup pal for 8-bit modes
\r
859 localPal[0xc0] = 0x0000c000; // MCD LEDs
\r
860 localPal[0xd0] = 0x00c00000;
\r
861 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
862 localPal[0xf0] = 0x00ffffff;
\r
863 gp2x_video_setpalette(localPal, 0x100);
\r
864 gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
867 Pico.m.dirtyPal = 1;
\r
869 if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8))
\r
870 gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);
\r
871 else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);
\r
875 static void emu_msg_cb(const char *msg)
\r
877 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
879 gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
\r
880 osd_text(4, 232, msg);
\r
881 gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232, 320*232, 320*8);
\r
883 // 16bit accurate renderer
\r
884 gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
\r
885 osd_text(4, 232, msg);
\r
886 gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232*2, 320*232*2, 320*8*2);
\r
888 gettimeofday(¬iceMsgTime, 0);
\r
889 noticeMsgTime.tv_sec -= 2;
\r
891 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
\r
895 static void emu_state_cb(const char *str)
\r
901 static void emu_msg_tray_open(void)
\r
903 strcpy(noticeMsg, "CD tray opened");
\r
904 gettimeofday(¬iceMsgTime, 0);
\r
907 static void RunEvents(unsigned int which)
\r
909 if(which & 0x1800) { // save or load (but not both)
\r
911 if ( emu_check_save_file(state_slot) &&
\r
912 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
\r
913 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) { // save
\r
914 unsigned long keys;
\r
915 blit("", (which & 0x1000) ? "LOAD STATE? (Y=yes, X=no)" : "OVERWRITE SAVE? (Y=yes, X=no)");
\r
916 while( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )
\r
918 if (keys & GP2X_X) do_it = 0;
\r
922 osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
\r
923 PicoStateProgressCB = emu_state_cb;
\r
924 gp2x_memcpy_all_buffers(gp2x_screen, 0, 320*240*2);
\r
925 emu_SaveLoadGame((which & 0x1000) >> 12, 0);
\r
926 PicoStateProgressCB = NULL;
\r
931 if(which & 0x0400) { // switch renderer
\r
932 if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
\r
933 else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;
\r
934 else currentConfig.EmuOpt &= ~0x80;
\r
938 if (PicoOpt&0x10) {
\r
939 strcpy(noticeMsg, " 8bit fast renderer");
\r
940 } else if (currentConfig.EmuOpt&0x80) {
\r
941 strcpy(noticeMsg, "16bit accurate renderer");
\r
943 strcpy(noticeMsg, " 8bit accurate renderer");
\r
946 gettimeofday(¬iceMsgTime, 0);
\r
948 if(which & 0x0300) {
\r
951 if(state_slot < 0) state_slot = 9;
\r
954 if(state_slot > 9) state_slot = 0;
\r
956 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_check_save_file(state_slot) ? "USED" : "FREE");
\r
957 gettimeofday(¬iceMsgTime, 0);
\r
959 if(which & 0x0080) {
\r
960 engineState = PGS_Menu;
\r
965 static void updateMovie(void)
\r
967 int offs = Pico.m.frame_count*3 + 0x40;
\r
968 if (offs+3 > movie_size) {
\r
971 strcpy(noticeMsg, "END OF MOVIE.");
\r
972 printf("END OF MOVIE.\n");
\r
973 gettimeofday(¬iceMsgTime, 0);
\r
976 PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU
\r
977 if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // A
\r
978 if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // B
\r
979 if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // A
\r
980 PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU
\r
981 if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // A
\r
982 if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // B
\r
983 if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // A
\r
984 PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX
\r
985 if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X
\r
986 if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z
\r
987 PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX
\r
988 if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X
\r
989 if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z
\r
994 static void updateKeys(void)
\r
996 unsigned long keys, allActions[2] = { 0, 0 }, events;
\r
997 static unsigned long prevEvents = 0;
\r
1000 keys = gp2x_joystick_read(0);
\r
1001 if (keys & GP2X_SELECT) {
\r
1002 engineState = select_exits ? PGS_Quit : PGS_Menu;
\r
1003 // wait until select is released, so menu would not resume game
\r
1004 while (gp2x_joystick_read(1) & GP2X_SELECT) usleep(50*1000);
\r
1007 keys &= CONFIGURABLE_KEYS;
\r
1009 for (i = 0; i < 32; i++)
\r
1011 if (keys & (1 << i)) {
\r
1012 int pl, acts = currentConfig.KeyBinds[i];
\r
1013 if (!acts) continue;
\r
1014 pl = (acts >> 16) & 1;
\r
1015 if (combo_keys & (1 << i)) {
\r
1016 int u = i+1, acts_c = acts & combo_acts;
\r
1017 // let's try to find the other one
\r
1019 for (; u < 32; u++)
\r
1020 if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) {
\r
1021 allActions[pl] |= acts_c;
\r
1022 keys &= ~((1 << i) | (1 << u));
\r
1025 // add non-combo actions if combo ones were not found
\r
1026 if (!acts_c || u == 32)
\r
1027 allActions[pl] |= acts & ~combo_acts;
\r
1029 allActions[pl] |= acts;
\r
1035 if (num_of_joys > 0)
\r
1037 gp2x_usbjoy_update();
\r
1038 for (joy = 0; joy < num_of_joys; joy++) {
\r
1039 int keys = gp2x_usbjoy_check2(joy);
\r
1040 for (i = 0; i < 32; i++) {
\r
1041 if (keys & (1 << i)) {
\r
1042 int acts = currentConfig.JoyBinds[joy][i];
\r
1043 int pl = (acts >> 16) & 1;
\r
1044 allActions[pl] |= acts;
\r
1050 PicoPad[0] = (unsigned short) allActions[0];
\r
1051 PicoPad[1] = (unsigned short) allActions[1];
\r
1053 events = (allActions[0] | allActions[1]) >> 16;
\r
1055 // volume is treated in special way and triggered every frame
\r
1056 if(events & 0x6000) {
\r
1057 int vol = currentConfig.volume;
\r
1058 if (events & 0x2000) {
\r
1059 if (vol < 99) vol++;
\r
1061 if (vol > 0) vol--;
\r
1063 gp2x_sound_volume(vol, vol);
\r
1064 sprintf(noticeMsg, "VOL: %02i", vol);
\r
1065 gettimeofday(¬iceMsgTime, 0);
\r
1066 currentConfig.volume = vol;
\r
1069 events &= ~prevEvents;
\r
1070 if (events) RunEvents(events);
\r
1071 if (movie_data) updateMovie();
\r
1073 prevEvents = (allActions[0] | allActions[1]) >> 16;
\r
1077 static void updateSound(int len)
\r
1079 if (PicoOpt&8) len<<=1;
\r
1081 /* avoid writing audio when lagging behind to prevent audio lag */
\r
1082 if (PicoSkipFrame != 2)
\r
1083 gp2x_sound_write(PsndOut, len<<1);
\r
1087 static void SkipFrame(int do_audio)
\r
1089 PicoSkipFrame=do_audio ? 1 : 2;
\r
1095 void emu_forced_frame(void)
\r
1097 int po_old = PicoOpt;
\r
1098 int eo_old = currentConfig.EmuOpt;
\r
1100 PicoOpt &= ~0x0010;
\r
1101 PicoOpt |= 0x4080; // soft_scale | acc_sprites
\r
1102 currentConfig.EmuOpt |= 0x80;
\r
1105 PicoDrawSetColorFormat(1);
\r
1106 PicoScan = EmuScan16;
\r
1108 Pico.m.dirtyPal = 1;
\r
1109 PicoFrameDrawOnly();
\r
1112 if (!(Pico.video.reg[12]&1)) {
\r
1113 vidCpyM2 = vidCpyM2_32col;
\r
1115 } else vidCpyM2 = vidCpyM2_40col;
\r
1117 vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);
\r
1118 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
1119 gp2x_video_setpalette(localPal, 0x40);
\r
1122 currentConfig.EmuOpt = eo_old;
\r
1125 static void simpleWait(int thissec, int lim_time)
\r
1127 struct timeval tval;
\r
1129 spend_cycles(1024);
\r
1130 gettimeofday(&tval, 0);
\r
1131 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
1133 while(tval.tv_usec < lim_time)
\r
1135 spend_cycles(1024);
\r
1136 gettimeofday(&tval, 0);
\r
1137 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
1142 void emu_Loop(void)
\r
1144 static int gp2x_old_clock = 200;
\r
1145 static int PsndRate_old = 0, PicoOpt_old = 0, EmuOpt_old = 0, PsndLen_real = 0, pal_old = 0;
\r
1146 char fpsbuff[24]; // fps count c string
\r
1147 struct timeval tval; // timing
\r
1148 int thissec = 0, frames_done = 0, frames_shown = 0, oldmodes = 0;
\r
1149 int target_fps, target_frametime, lim_time, vsync_offset, i;
\r
1152 printf("entered emu_Loop()\n");
\r
1154 if (gp2x_old_clock != currentConfig.CPUclock) {
\r
1155 printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);
\r
1156 set_FCLK(currentConfig.CPUclock);
\r
1157 gp2x_old_clock = currentConfig.CPUclock;
\r
1158 printf(" done\n");
\r
1161 if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {
\r
1162 set_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));
\r
1163 gp2x_old_gamma = currentConfig.gamma;
\r
1164 printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));
\r
1167 if ((EmuOpt_old&0x2000) != (currentConfig.EmuOpt&0x2000)) {
\r
1168 if (currentConfig.EmuOpt&0x2000)
\r
1169 set_LCD_custom_rate(Pico.m.pal ? LCDR_100 : LCDR_120);
\r
1170 else unset_LCD_custom_rate();
\r
1173 EmuOpt_old = currentConfig.EmuOpt;
\r
1176 // make sure we are in correct mode
\r
1178 Pico.m.dirtyPal = 1;
\r
1179 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
\r
1182 // pal/ntsc might have changed, reset related stuff
\r
1183 target_fps = Pico.m.pal ? 50 : 60;
\r
1184 target_frametime = 1000000/target_fps;
\r
1187 // prepare sound stuff
\r
1188 if(currentConfig.EmuOpt & 4) {
\r
1189 int snd_excess_add;
\r
1190 if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||
\r
1191 ((PicoOpt&0x200) && crashed_940)) {
\r
1192 /* if 940 is turned off, we need it to be put back to sleep */
\r
1193 if (!(PicoOpt&0x200) && ((PicoOpt^PicoOpt_old)&0x200)) {
\r
1197 sound_rerate(Pico.m.frame_count ? 1 : 0);
\r
1199 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
\r
1200 printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
\r
1201 PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);
\r
1202 gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);
\r
1203 gp2x_sound_volume(currentConfig.volume, currentConfig.volume);
\r
1204 PicoWriteSound = updateSound;
\r
1205 memset(sndBuffer, 0, sizeof(sndBuffer));
\r
1206 PsndOut = sndBuffer;
\r
1207 PsndRate_old = PsndRate;
\r
1208 PsndLen_real = PsndLen;
\r
1209 PicoOpt_old = PicoOpt;
\r
1210 pal_old = Pico.m.pal;
\r
1215 // prepare CD buffer
\r
1216 if (PicoMCD & 1) PicoCDBufferInit();
\r
1218 // calc vsync offset to sync timing code with vsync
\r
1219 if (currentConfig.EmuOpt&0x2000) {
\r
1220 gettimeofday(&tval, 0);
\r
1221 gp2x_video_wait_vsync();
\r
1222 gettimeofday(&tval, 0);
\r
1223 vsync_offset = tval.tv_usec;
\r
1224 while (vsync_offset >= target_frametime)
\r
1225 vsync_offset -= target_frametime;
\r
1226 if (!vsync_offset) vsync_offset++;
\r
1227 printf("vsync_offset: %i\n", vsync_offset);
\r
1232 while (engineState == PGS_Running)
\r
1236 gettimeofday(&tval, 0);
\r
1237 if(reset_timing) {
\r
1239 thissec = tval.tv_sec;
\r
1240 frames_shown = frames_done = tval.tv_usec/target_frametime;
\r
1243 // show notice message?
\r
1244 if(noticeMsgTime.tv_sec) {
\r
1245 static int noticeMsgSum;
\r
1246 if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec
\r
1247 noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;
\r
1251 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
\r
1252 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
\r
1253 notice = noticeMsg;
\r
1257 // check for mode changes
\r
1258 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
\r
1259 if (modes != oldmodes) {
\r
1261 osd_fps_x = OSD_FPS_X;
\r
1263 vidCpyM2 = vidCpyM2_40col;
\r
1265 if (PicoOpt & 0x100) {
\r
1266 vidCpyM2 = vidCpyM2_32col_nobord;
\r
1268 osd_fps_x = OSD_FPS_X - 64;
\r
1270 vidCpyM2 = vidCpyM2_32col;
\r
1273 if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode
\r
1274 gp2x_video_RGB_setscaling(8, scalex, 224);
\r
1275 else gp2x_video_RGB_setscaling(0, scalex, 240);
\r
1280 // second changed?
\r
1281 if(thissec != tval.tv_sec) {
\r
1283 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
\r
1284 if(++bench == 10) {
\r
1286 bench_fps_s = bench_fps;
\r
1287 bf[bfp++ & 3] = bench_fps;
\r
1290 bench_fps += frames_shown;
\r
1291 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
\r
1293 if(currentConfig.EmuOpt & 2)
\r
1294 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
\r
1296 thissec = tval.tv_sec;
\r
1298 if(PsndOut == 0 && currentConfig.Frameskip >= 0) {
\r
1299 frames_done = frames_shown = 0;
\r
1301 // it is quite common for this implementation to leave 1 fame unfinished
\r
1302 // when second changes, but we don't want buffer to starve.
\r
1303 if(PsndOut && frames_done < target_fps && frames_done > target_fps-5) {
\r
1305 SkipFrame(1); frames_done++;
\r
1308 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
\r
1309 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
\r
1310 if (frames_shown > frames_done) frames_shown = frames_done;
\r
1314 sprintf(fpsbuff, "%i", Pico.m.frame_count);
\r
1317 lim_time = (frames_done+1) * target_frametime + vsync_offset;
\r
1318 if(currentConfig.Frameskip >= 0) { // frameskip enabled
\r
1319 for(i = 0; i < currentConfig.Frameskip; i++) {
\r
1321 SkipFrame(1); frames_done++;
\r
1322 if (PsndOut) { // do framelimitting if sound is enabled
\r
1323 gettimeofday(&tval, 0);
\r
1324 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
1325 if(tval.tv_usec < lim_time) { // we are too fast
\r
1326 simpleWait(thissec, lim_time);
\r
1329 lim_time += target_frametime;
\r
1331 } else if(tval.tv_usec > lim_time) { // auto frameskip
\r
1332 // no time left for this frame - skip
\r
1333 if (tval.tv_usec - lim_time >= 0x300000) {
\r
1334 /* something caused a slowdown for us (disk access? cache flush?)
\r
1335 * try to recover by resetting timing... */
\r
1340 SkipFrame(tval.tv_usec < lim_time+target_frametime*2); frames_done++;
\r
1348 if (Pico.m.frame_count == 31563) {
\r
1350 f = fopen("ram_p.bin", "wb");
\r
1351 if (!f) { printf("!f\n"); exit(1); }
\r
1352 fwrite(Pico.ram, 1, 0x10000, f);
\r
1360 #define BYTE unsigned char
\r
1361 #define WORD unsigned short
\r
1364 BYTE IDLength; /* 00h Size of Image ID field */
\r
1365 BYTE ColorMapType; /* 01h Color map type */
\r
1366 BYTE ImageType; /* 02h Image type code */
\r
1367 WORD CMapStart; /* 03h Color map origin */
\r
1368 WORD CMapLength; /* 05h Color map length */
\r
1369 BYTE CMapDepth; /* 07h Depth of color map entries */
\r
1370 WORD XOffset; /* 08h X origin of image */
\r
1371 WORD YOffset; /* 0Ah Y origin of image */
\r
1372 WORD Width; /* 0Ch Width of image */
\r
1373 WORD Height; /* 0Eh Height of image */
\r
1374 BYTE PixelDepth; /* 10h Image pixel size */
\r
1375 BYTE ImageDescriptor; /* 11h Image descriptor byte */
\r
1376 } __attribute__((packed)) TGAHEAD;
\r
1377 static unsigned short oldscr[320*240];
\r
1378 FILE *f; char name[128]; int i;
\r
1380 memset(&TGAHEAD, 0, sizeof(TGAHEAD));
\r
1381 TGAHEAD.ImageType = 2;
\r
1382 TGAHEAD.Width = 320;
\r
1383 TGAHEAD.Height = 240;
\r
1384 TGAHEAD.PixelDepth = 16;
\r
1385 TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left
\r
1387 #define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?
\r
1389 for (i = 0; i < 320*240; i++)
\r
1390 if(oldscr[i] != CONV(((unsigned short *)gp2x_screen)[i])) break;
\r
1393 for (i = 0; i < 320*240; i++)
\r
1394 oldscr[i] = CONV(((unsigned short *)gp2x_screen)[i]);
\r
1395 sprintf(name, "%05i.tga", Pico.m.frame_count);
\r
1396 f = fopen(name, "wb");
\r
1397 if (!f) { printf("!f\n"); exit(1); }
\r
1398 fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);
\r
1399 fwrite(oldscr, 1, 320*240*2, f);
\r
1406 gettimeofday(&tval, 0);
\r
1407 if (thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
1409 if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 0x300000) // slowdown detection
\r
1411 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
\r
1413 // sleep or vsync if we are still too fast
\r
1414 // usleep sleeps for ~20ms minimum, so it is not a solution here
\r
1415 if(tval.tv_usec < lim_time)
\r
1417 // we are too fast
\r
1418 if (vsync_offset) {
\r
1419 if (lim_time - tval.tv_usec > target_frametime/2)
\r
1420 simpleWait(thissec, lim_time - target_frametime/4);
\r
1421 gp2x_video_wait_vsync();
\r
1423 simpleWait(thissec, lim_time);
\r
1428 blit(fpsbuff, notice);
\r
1430 frames_done++; frames_shown++;
\r
1434 if (PicoMCD & 1) PicoCDBufferFree();
\r
1437 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
1438 emu_state_cb("Writing SRAM/BRAM..");
\r
1439 emu_SaveLoadGame(0, 1);
\r
1443 // if in 8bit mode, generate 16bit image for menu background
\r
1444 if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))
\r
1445 emu_forced_frame();
\r
1449 void emu_ResetGame(void)
\r
1456 size_t gzRead2(void *p, size_t _size, size_t _n, void *file)
\r
1458 return gzread(file, p, _n);
\r
1462 size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)
\r
1464 return gzwrite(file, p, _n);
\r
1467 static int try_ropen_file(const char *fname)
\r
1471 f = fopen(fname, "rb");
\r
1479 char *emu_GetSaveFName(int load, int is_sram, int slot)
\r
1481 static char saveFname[512];
\r
1486 romfname_ext(saveFname, (PicoMCD&1) ? "brm/" : "srm/", (PicoMCD&1) ? ".brm" : ".srm");
\r
1488 if (try_ropen_file(saveFname)) return saveFname;
\r
1489 // try in current dir..
\r
1490 romfname_ext(saveFname, NULL, (PicoMCD&1) ? ".brm" : ".srm");
\r
1491 if (try_ropen_file(saveFname)) return saveFname;
\r
1492 return NULL; // give up
\r
1498 if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);
\r
1499 strcat(ext, (currentConfig.EmuOpt & 8) ? ".mds.gz" : ".mds");
\r
1501 romfname_ext(saveFname, "mds/", ext);
\r
1503 if (try_ropen_file(saveFname)) return saveFname;
\r
1504 romfname_ext(saveFname, NULL, ext);
\r
1505 if (try_ropen_file(saveFname)) return saveFname;
\r
1506 if (currentConfig.EmuOpt & 8) {
\r
1508 if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);
\r
1509 strcat(ext, ".mds");
\r
1511 romfname_ext(saveFname, "mds/", ext);
\r
1512 if (try_ropen_file(saveFname)) return saveFname;
\r
1513 romfname_ext(saveFname, NULL, ext);
\r
1514 if (try_ropen_file(saveFname)) return saveFname;
\r
1523 int emu_check_save_file(int slot)
\r
1525 return emu_GetSaveFName(1, 0, slot) ? 1 : 0;
\r
1528 void emu_set_save_cbs(int gz)
\r
1531 areaRead = gzRead2;
\r
1532 areaWrite = gzWrite2;
\r
1533 areaEof = (areaeof *) gzeof;
\r
1534 areaSeek = (areaseek *) gzseek;
\r
1535 areaClose = (areaclose *) gzclose;
\r
1537 areaRead = (arearw *) fread;
\r
1538 areaWrite = (arearw *) fwrite;
\r
1539 areaEof = (areaeof *) feof;
\r
1540 areaSeek = (areaseek *) fseek;
\r
1541 areaClose = (areaclose *) fclose;
\r
1545 int emu_SaveLoadGame(int load, int sram)
\r
1550 // make save filename
\r
1551 saveFname = emu_GetSaveFName(load, sram, state_slot);
\r
1552 if (saveFname == NULL) {
\r
1554 strcpy(noticeMsg, load ? "LOAD FAILED (missing file)" : "SAVE FAILED ");
\r
1555 gettimeofday(¬iceMsgTime, 0);
\r
1560 printf("saveLoad (%i, %i): %s\n", load, sram, saveFname);
\r
1565 unsigned char *sram_data;
\r
1568 if (PicoOpt&0x8000) { // MCD RAM cart?
\r
1569 sram_size = 0x12000;
\r
1570 sram_data = SRam.data;
\r
1572 memcpy32((int *)sram_data, (int *)Pico_mcd->bram, 0x2000/4);
\r
1574 sram_size = 0x2000;
\r
1575 sram_data = Pico_mcd->bram;
\r
1576 truncate = 0; // the .brm may contain RAM cart data after normal brm
\r
1579 sram_size = SRam.end-SRam.start+1;
\r
1580 if(Pico.m.sram_reg & 4) sram_size=0x2000;
\r
1581 sram_data = SRam.data;
\r
1583 if (!sram_data) return 0; // SRam forcefully disabled for this game
\r
1586 sramFile = fopen(saveFname, "rb");
\r
1587 if(!sramFile) return -1;
\r
1588 fread(sram_data, 1, sram_size, sramFile);
\r
1590 if ((PicoMCD&1) && (PicoOpt&0x8000))
\r
1591 memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4);
\r
1593 // sram save needs some special processing
\r
1594 // see if we have anything to save
\r
1595 for (; sram_size > 0; sram_size--)
\r
1596 if (sram_data[sram_size-1]) break;
\r
1599 sramFile = fopen(saveFname, truncate ? "wb" : "r+b");
\r
1600 if (!sramFile) sramFile = fopen(saveFname, "wb"); // retry
\r
1601 if (!sramFile) return -1;
\r
1602 ret = fwrite(sram_data, 1, sram_size, sramFile);
\r
1603 ret = (ret != sram_size) ? -1 : 0;
\r
1614 void *PmovFile = NULL;
\r
1615 if (strcmp(saveFname + strlen(saveFname) - 3, ".gz") == 0) {
\r
1616 if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) {
\r
1617 emu_set_save_cbs(1);
\r
1618 if(!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);
\r
1623 if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) {
\r
1624 emu_set_save_cbs(0);
\r
1628 ret = PmovState(load ? 6 : 5, PmovFile);
\r
1629 areaClose(PmovFile);
\r
1631 if (load) Pico.m.dirtyPal=1;
\r
1638 strcpy(noticeMsg, load ? "GAME LOADED " : "GAME SAVED ");
\r
1641 strcpy(noticeMsg, load ? "LOAD FAILED " : "SAVE FAILED ");
\r
1645 gettimeofday(¬iceMsgTime, 0);
\r