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 <linux/limits.h>
\r
19 #include "asmutils.h"
\r
20 #include "cpuctrl.h"
\r
22 #include "Pico/PicoInt.h"
\r
23 #include "zlib/zlib.h"
\r
27 #define OSD_FPS_X 220
\r
29 #define OSD_FPS_X 260
\r
32 // PicoPad[] format: SACB RLDU
\r
33 char *actionNames[] = {
\r
34 "UP", "DOWN", "LEFT", "RIGHT", "B", "C", "A", "START",
\r
35 0, 0, 0, 0, 0, 0, 0, 0, // Z, Y, X, MODE (enabled only when needed), ?, ?, ?, ?
\r
36 0, 0, 0, 0, 0, 0, 0, "ENTER MENU", // player2_flag, ?, ?, ?, ?, ?, ?, menu
\r
37 "NEXT SAVE SLOT", "PREV SAVE SLOT", "SWITCH RENDERER", "SAVE STATE",
\r
38 "LOAD STATE", "VOLUME UP", "VOLUME DOWN", "DONE"
\r
42 int select_exits = 0;
\r
43 char *PicoConfigFile = "picoconfig.bin";
\r
44 currentConfig_t currentConfig;
\r
46 char romFileName[PATH_MAX];
\r
47 unsigned char *rom_data = NULL;
\r
49 extern int crashed_940;
\r
51 static short sndBuffer[2*44100/50];
\r
52 static char noticeMsg[64]; // notice msg to draw
\r
53 static struct timeval noticeMsgTime = { 0, 0 }; // when started showing
\r
54 static int reset_timing, osd_fps_x;
\r
55 static int combo_keys = 0, combo_acts = 0; // keys and actions which need button combos
\r
56 static int gp2x_old_gamma = 100;
\r
57 static unsigned char *movie_data = NULL;
\r
58 static int movie_size = 0;
\r
59 unsigned char *framebuff = 0; // temporary buffer for alt renderer
\r
64 static FILE *logf = NULL;
\r
66 void pprintf(char *texto, ...)
\r
70 va_start(args,texto);
\r
71 vfprintf(logf,texto,args);
\r
78 static void strlwr(char* string)
\r
80 while ( (*string++ = (char)tolower(*string)) );
\r
83 static int try_rfn_cut(void)
\r
88 p = romFileName + strlen(romFileName) - 1;
\r
89 for (; p > romFileName; p--)
\r
90 if (*p == '.') break;
\r
93 if((tmp = fopen(romFileName, "rb"))) {
\r
100 static void get_ext(char *file, char *ext)
\r
104 p = file + strlen(file) - 4;
\r
105 if (p < file) p = file;
\r
106 strncpy(ext, p, 4);
\r
111 char *biosfiles_us[] = { "us_scd2_9306", "SegaCDBIOS9303", "us_scd1_9210" };
\r
112 char *biosfiles_eu[] = { "eu_mcd2_9306", "eu_mcd2_9303", "eu_mcd1_9210" };
\r
113 char *biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };
\r
115 extern char **g_argv;
\r
117 int find_bios(int region, char **bios_file)
\r
119 static char bios_path[1024];
\r
124 if (region == 4) { // US
\r
125 files = biosfiles_us;
\r
126 count = sizeof(biosfiles_us) / sizeof(char *);
\r
127 } else if (region == 8) { // EU
\r
128 files = biosfiles_eu;
\r
129 count = sizeof(biosfiles_eu) / sizeof(char *);
\r
130 } else if (region == 1 || region == 2) {
\r
131 files = biosfiles_jp;
\r
132 count = sizeof(biosfiles_jp) / sizeof(char *);
\r
137 for (i = 0; i < count; i++)
\r
139 strncpy(bios_path, g_argv[0], 1023);
\r
140 bios_path[1024-32] = 0;
\r
141 for (j = strlen(bios_path); j > 0; j--)
\r
142 if (bios_path[j] == '/') { bios_path[j+1] = 0; break; }
\r
143 strcat(bios_path, files[i]);
\r
144 strcat(bios_path, ".bin");
\r
145 f = fopen(bios_path, "rb");
\r
148 bios_path[strlen(bios_path) - 4] = 0;
\r
149 strcat(bios_path, ".zip");
\r
150 f = fopen(bios_path, "rb");
\r
155 printf("using bios: %s\n", bios_path);
\r
157 if (bios_file) *bios_file = bios_path;
\r
160 sprintf(menuErrorMsg, "no %s BIOS files found, read docs",
\r
161 region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
\r
162 printf("%s\n", menuErrorMsg);
\r
167 /* checks if romFileName points to valid MegaCD image
\r
168 * if so, checks for suitable BIOS */
\r
169 static int cd_check(char *ext, char **bios_file)
\r
171 unsigned char buf[32];
\r
173 int type = 0, region = 4; // 1: Japan, 4: US, 8: Europe
\r
175 cd_f = fopen(romFileName, "rb");
\r
176 if (!cd_f) return 0; // let the upper level handle this
\r
178 if (fread(buf, 1, 32, cd_f) != 32) {
\r
183 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) type = 1; // Sega CD (ISO)
\r
184 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) type = 2; // Sega CD (BIN)
\r
190 /* it seems we have a CD image here. Try to detect region and load a suitable BIOS now.. */
\r
191 fseek(cd_f, (type == 1) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);
\r
192 fread(buf, 1, 1, cd_f);
\r
195 if (buf[0] == 0x64) region = 8; // EU
\r
196 if (buf[0] == 0xa1) region = 1; // JAP
\r
198 printf("detected %s Sega/Mega CD image with %s region\n",
\r
199 type == 2 ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
\r
201 if (PicoRegionOverride) {
\r
202 region = PicoRegionOverride;
\r
203 printf("overrided region to %s\n", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
\r
206 if(find_bios(region, bios_file))
\r
207 return type; // CD and BIOS detected
\r
209 return -1; // CD detected but load failed
\r
212 int emu_ReloadRom(void)
\r
214 unsigned int rom_size = 0;
\r
215 char *used_rom_name = romFileName;
\r
220 printf("emu_ReloadRom(%s)\n", romFileName);
\r
222 get_ext(romFileName, ext);
\r
224 // detect wrong extensions
\r
225 if(!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz
\r
226 sprintf(menuErrorMsg, "Not a ROM selected.");
\r
230 // check for movie file
\r
235 if(!strcmp(ext, ".gmv")) {
\r
236 // check for both gmv and rom
\r
238 FILE *movie_file = fopen(romFileName, "rb");
\r
240 sprintf(menuErrorMsg, "Failed to open movie.");
\r
243 fseek(movie_file, 0, SEEK_END);
\r
244 movie_size = ftell(movie_file);
\r
245 fseek(movie_file, 0, SEEK_SET);
\r
246 if(movie_size < 64+3) {
\r
247 sprintf(menuErrorMsg, "Invalid GMV file.");
\r
248 fclose(movie_file);
\r
251 movie_data = malloc(movie_size);
\r
252 if(movie_data == NULL) {
\r
253 sprintf(menuErrorMsg, "low memory.");
\r
254 fclose(movie_file);
\r
257 fread(movie_data, 1, movie_size, movie_file);
\r
258 fclose(movie_file);
\r
259 if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {
\r
260 sprintf(menuErrorMsg, "Invalid GMV file.");
\r
263 dummy = try_rfn_cut() || try_rfn_cut();
\r
265 sprintf(menuErrorMsg, "Could't find a ROM for movie.");
\r
268 get_ext(romFileName, ext);
\r
271 // check for MegaCD image
\r
272 cd_state = cd_check(ext, &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 = fopen(used_rom_name, "rb");
\r
286 sprintf(menuErrorMsg, "Failed to open rom.");
\r
297 if(!strcasecmp(ext, ".zip")) {
\r
299 ret = CartLoadZip(used_rom_name, &rom_data, &rom_size);
\r
301 if (ret == 4) strcpy(menuErrorMsg, "No ROMs found in zip.");
\r
302 else sprintf(menuErrorMsg, "Unzip failed with code %i", ret);
\r
303 printf("%s\n", menuErrorMsg);
\r
307 if( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) {
\r
308 sprintf(menuErrorMsg, "PicoCartLoad() failed.");
\r
309 printf("%s\n", menuErrorMsg);
\r
316 // detect wrong files (Pico crashes on very small files), also see if ROM EP is good
\r
317 if(rom_size <= 0x200 || strncmp((char *)rom_data, "Pico", 4) == 0 ||
\r
318 ((*(unsigned short *)(rom_data+4)<<16)|(*(unsigned short *)(rom_data+6))) >= (int)rom_size) {
\r
319 if (rom_data) free(rom_data);
\r
321 sprintf(menuErrorMsg, "Not a ROM selected.");
\r
325 // load config for this ROM (do this before insert to get correct region)
\r
326 ret = emu_ReadConfig(1);
\r
330 printf("PicoCartInsert(%p, %d);\n", rom_data, rom_size);
\r
331 if(PicoCartInsert(rom_data, rom_size)) {
\r
332 sprintf(menuErrorMsg, "Failed to load ROM.");
\r
336 Pico.m.frame_count = 0;
\r
338 // insert CD if it was detected
\r
339 if (cd_state > 0) {
\r
340 ret = Insert_CD(romFileName, cd_state == 2);
\r
342 sprintf(menuErrorMsg, "Insert_CD() failed, invalid CD image?");
\r
343 printf("%s\n", menuErrorMsg);
\r
348 // emu_ReadConfig() might have messed currentConfig.lastRomFile
\r
349 strncpy(currentConfig.lastRomFile, romFileName, sizeof(currentConfig.lastRomFile)-1);
\r
350 currentConfig.lastRomFile[sizeof(currentConfig.lastRomFile)-1] = 0;
\r
352 // additional movie stuff
\r
354 if(movie_data[0x14] == '6')
\r
355 PicoOpt |= 0x20; // 6 button pad
\r
356 else PicoOpt &= ~0x20;
\r
357 PicoOpt |= 0x40; // accurate timing
\r
358 if(movie_data[0xF] >= 'A') {
\r
359 if(movie_data[0x16] & 0x80) {
\r
360 PicoRegionOverride = 8;
\r
362 PicoRegionOverride = 4;
\r
365 // TODO: bits 6 & 5
\r
367 movie_data[0x18+30] = 0;
\r
368 sprintf(noticeMsg, "MOVIE: %s", (char *) &movie_data[0x18]);
\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 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
399 // logf = fopen("log.txt", "w");
\r
403 static void romfname_ext(char *dst, char *ext)
\r
407 // make save filename
\r
408 for (p = romFileName+strlen(romFileName)-1; p >= romFileName && *p != '/'; p--); p++;
\r
409 strncpy(dst, p, 511);
\r
411 if(dst[strlen(dst)-4] == '.') dst[strlen(dst)-4] = 0;
\r
416 static void find_combos(void)
\r
420 // find out which keys and actions are combos
\r
421 combo_keys = combo_acts = 0;
\r
422 for (act = 0; act < 32; act++)
\r
425 if (act == 16) continue; // player2 flag
\r
426 for (u = 0; u < 32; u++)
\r
428 if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;
\r
432 // loop again and mark those keys and actions as combo
\r
433 for (u = 0; u < 32; u++)
\r
435 if (currentConfig.KeyBinds[u] & (1 << act)) {
\r
436 combo_keys |= 1 << u;
\r
437 combo_acts |= 1 << act;
\r
442 // printf("combo keys/acts: %08x %08x\n", combo_keys, combo_acts);
\r
446 int emu_ReadConfig(int game)
\r
454 // set default config
\r
455 memset(¤tConfig, 0, sizeof(currentConfig));
\r
456 currentConfig.lastRomFile[0] = 0;
\r
457 currentConfig.EmuOpt = 0x1f | 0x400; // | cd_leds
\r
458 currentConfig.PicoOpt = 0x0f | 0xe00; // | use_940 | cd_pcm | cd_cdda
\r
459 currentConfig.PsndRate = 22050; // 44100;
\r
460 currentConfig.PicoRegion = 0; // auto
\r
461 currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP
\r
462 currentConfig.Frameskip = -1; // auto
\r
463 currentConfig.CPUclock = 200;
\r
464 currentConfig.volume = 50;
\r
465 currentConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU
\r
466 currentConfig.KeyBinds[ 4] = 1<<1;
\r
467 currentConfig.KeyBinds[ 2] = 1<<2;
\r
468 currentConfig.KeyBinds[ 6] = 1<<3;
\r
469 currentConfig.KeyBinds[14] = 1<<4;
\r
470 currentConfig.KeyBinds[13] = 1<<5;
\r
471 currentConfig.KeyBinds[12] = 1<<6;
\r
472 currentConfig.KeyBinds[ 8] = 1<<7;
\r
473 currentConfig.KeyBinds[15] = 1<<26; // switch rend
\r
474 currentConfig.KeyBinds[10] = 1<<27; // save state
\r
475 currentConfig.KeyBinds[11] = 1<<28; // load state
\r
476 currentConfig.KeyBinds[23] = 1<<29; // vol up
\r
477 currentConfig.KeyBinds[22] = 1<<30; // vol down
\r
478 currentConfig.gamma = 100;
\r
479 strncpy(cfg, PicoConfigFile, 511);
\r
482 romfname_ext(cfg, ".pbcfg");
\r
485 printf("emu_ReadConfig: %s ", cfg);
\r
486 f = fopen(cfg, "rb");
\r
488 bread = fread(¤tConfig, 1, sizeof(currentConfig), f);
\r
491 printf((bread == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n");
\r
493 PicoOpt = currentConfig.PicoOpt;
\r
494 PsndRate = currentConfig.PsndRate;
\r
495 PicoRegionOverride = currentConfig.PicoRegion;
\r
496 PicoAutoRgnOrder = currentConfig.PicoAutoRgnOrder;
\r
497 if (PicoOpt & 0x20) {
\r
498 actionNames[ 8] = "Z"; actionNames[ 9] = "Y";
\r
499 actionNames[10] = "X"; actionNames[11] = "MODE";
\r
501 // some sanity checks
\r
502 if (currentConfig.CPUclock < 1 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;
\r
503 if (currentConfig.gamma < 10 || currentConfig.gamma > 300) currentConfig.gamma = 100;
\r
504 // if volume keys are unbound, bind them to volume control
\r
505 if (!currentConfig.KeyBinds[23] && !currentConfig.KeyBinds[22]) {
\r
506 currentConfig.KeyBinds[23] = 1<<29; // vol up
\r
507 currentConfig.KeyBinds[22] = 1<<30; // vol down
\r
510 return (bread == sizeof(currentConfig));
\r
514 int emu_WriteConfig(int game)
\r
522 strncpy(cfg, PicoConfigFile, 511);
\r
525 romfname_ext(cfg, ".pbcfg");
\r
528 printf("emu_WriteConfig: %s ", cfg);
\r
529 f = fopen(cfg, "wb");
\r
531 currentConfig.PicoOpt = PicoOpt;
\r
532 currentConfig.PsndRate = PsndRate;
\r
533 currentConfig.PicoRegion = PicoRegionOverride;
\r
534 currentConfig.PicoAutoRgnOrder = PicoAutoRgnOrder;
\r
535 bwrite = fwrite(¤tConfig, 1, sizeof(currentConfig), f);
\r
540 printf((bwrite == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n");
\r
542 return (bwrite == sizeof(currentConfig));
\r
546 void emu_Deinit(void)
\r
549 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
550 emu_SaveLoadGame(0, 1);
\r
554 if (!(currentConfig.EmuOpt & 0x20))
\r
555 emu_WriteConfig(0);
\r
562 if (gp2x_old_gamma != 100)
\r
567 void osd_text(int x, int y, char *text)
\r
569 int len = strlen(text)*8;
\r
571 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
573 x &= ~3; // align x
\r
574 len = (len+3) >> 2;
\r
575 for (h = 0; h < 8; h++) {
\r
576 p = (int *) ((unsigned char *) gp2x_screen+x+320*(y+h));
\r
577 for (i = len; i; i--, p++) *p = 0xe0e0e0e0;
\r
579 gp2x_text_out8_2(x, y, text, 0xf0);
\r
582 x &= ~1; // align x
\r
583 len = (len+1) >> 1;
\r
584 for (h = 0; h < 8; h++) {
\r
585 p = (int *) ((unsigned short *) gp2x_screen+x+320*(y+h));
\r
586 for (i = len; i; i--, p++) *p = (*p>>2)&0x39e7;
\r
588 gp2x_text_out15(x, y, text);
\r
592 static void cd_leds(void)
\r
597 // if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change
\r
598 old_reg = Pico_mcd->s68k_regs[0];
\r
600 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
602 unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;
\r
603 unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;
\r
604 *(unsigned int *)((char *)gp2x_screen + 320*2+ 4) =
\r
605 *(unsigned int *)((char *)gp2x_screen + 320*3+ 4) =
\r
606 *(unsigned int *)((char *)gp2x_screen + 320*4+ 4) = col_g;
\r
607 *(unsigned int *)((char *)gp2x_screen + 320*2+12) =
\r
608 *(unsigned int *)((char *)gp2x_screen + 320*3+12) =
\r
609 *(unsigned int *)((char *)gp2x_screen + 320*4+12) = col_r;
\r
612 unsigned int *p = (unsigned int *)((short *)gp2x_screen + 320*2+4);
\r
613 unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;
\r
614 unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;
\r
615 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
616 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
617 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
\r
621 static int EmuScan16(unsigned int num, void *sdata)
\r
623 if (!(Pico.video.reg[1]&8)) num += 8;
\r
624 DrawLineDest = (unsigned short *) gp2x_screen + 320*(num+1);
\r
629 static int EmuScan8(unsigned int num, void *sdata)
\r
631 if (!(Pico.video.reg[1]&8)) num += 8;
\r
632 DrawLineDest = (unsigned char *) gp2x_screen + 320*(num+1);
\r
637 static int localPal[0x100];
\r
638 static void (*vidCpyM2)(void *dest, void *src) = NULL;
\r
640 static void blit(char *fps, char *notice)
\r
642 int emu_opt = currentConfig.EmuOpt;
\r
644 if (PicoOpt&0x10) {
\r
645 // 8bit fast renderer
\r
646 if (Pico.m.dirtyPal) {
\r
647 Pico.m.dirtyPal = 0;
\r
648 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
649 // feed new palette to our device
\r
650 gp2x_video_setpalette(localPal, 0x40);
\r
652 vidCpyM2((unsigned char *)gp2x_screen+320*8, framebuff+328*8);
\r
653 } else if (!(emu_opt&0x80)) {
\r
654 // 8bit accurate renderer
\r
655 if (Pico.m.dirtyPal) {
\r
656 Pico.m.dirtyPal = 0;
\r
657 if(Pico.video.reg[0xC]&8) { // shadow/hilight mode
\r
658 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
659 vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
\r
660 vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);
\r
661 blockcpy(localPal+0xc0, localPal+0x40, 0x40*4);
\r
662 localPal[0xc0] = 0x0000c000;
\r
663 localPal[0xd0] = 0x00c00000;
\r
664 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
665 localPal[0xf0] = 0x00ffffff;
\r
666 gp2x_video_setpalette(localPal, 0x100);
\r
667 } else if (rendstatus & 0x20) { // mid-frame palette changes
\r
668 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
669 vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);
\r
670 vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);
\r
671 gp2x_video_setpalette(localPal, 0xc0);
\r
673 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
674 gp2x_video_setpalette(localPal, 0x40);
\r
679 if (notice) osd_text(4, 232, notice);
\r
681 osd_text(osd_fps_x, 232, fps);
\r
682 if ((emu_opt & 0x400) && (PicoMCD & 1))
\r
685 //gp2x_video_wait_vsync();
\r
688 if (!(PicoOpt&0x10)) {
\r
689 if (!(Pico.video.reg[1]&8)) {
\r
690 if (currentConfig.EmuOpt&0x80) {
\r
691 DrawLineDest = (unsigned short *) gp2x_screen + 320*8;
\r
693 DrawLineDest = (unsigned char *) gp2x_screen + 320*8;
\r
696 DrawLineDest = gp2x_screen;
\r
702 // clears whole screen or just the notice area (in all buffers)
\r
703 static void clearArea(int full)
\r
705 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
707 if (full) gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
708 else gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
\r
710 // 16bit accurate renderer
\r
711 if (full) gp2x_memset_all_buffers(0, 0, 320*240*2);
\r
712 else gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
\r
717 static void vidResetMode(void)
\r
719 if (PicoOpt&0x10) {
\r
720 gp2x_video_changemode(8);
\r
721 } else if (currentConfig.EmuOpt&0x80) {
\r
722 gp2x_video_changemode(15);
\r
723 PicoDrawSetColorFormat(1);
\r
724 PicoScan = EmuScan16;
\r
727 gp2x_video_changemode(8);
\r
728 PicoDrawSetColorFormat(2);
\r
729 PicoScan = EmuScan8;
\r
732 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
733 // setup pal for 8-bit modes
\r
734 localPal[0xc0] = 0x0000c000; // MCD LEDs
\r
735 localPal[0xd0] = 0x00c00000;
\r
736 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
737 localPal[0xf0] = 0x00ffffff;
\r
738 gp2x_video_setpalette(localPal, 0x100);
\r
739 gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
742 Pico.m.dirtyPal = 1;
\r
744 gp2x_video_RGB_setscaling((PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);
\r
748 static int check_save_file(void)
\r
750 char saveFname[512];
\r
755 if(state_slot > 0 && state_slot < 10) sprintf(ext, ".%i", state_slot);
\r
756 strcat(ext, ".mds");
\r
757 if(currentConfig.EmuOpt & 8) strcat(ext, ".gz");
\r
759 romfname_ext(saveFname, ext);
\r
760 if ((f = fopen(saveFname, "rb"))) {
\r
767 static void RunEvents(unsigned int which)
\r
769 if(which & 0x1800) { // save or load (but not both)
\r
771 if (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200) && check_save_file()) {
\r
772 unsigned long keys;
\r
773 blit("", "OVERWRITE SAVE? (Y=yes, X=no)");
\r
774 while( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )
\r
776 if (keys & GP2X_X) do_it = 0;
\r
780 blit("", (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
\r
781 emu_SaveLoadGame(which & 0x1000, 0);
\r
786 if(which & 0x0400) { // switch renderer
\r
787 if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
\r
788 else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;
\r
789 else currentConfig.EmuOpt &= ~0x80;
\r
793 if (PicoOpt&0x10) {
\r
794 strcpy(noticeMsg, " 8bit fast renderer");
\r
795 } else if (currentConfig.EmuOpt&0x80) {
\r
796 strcpy(noticeMsg, "16bit accurate renderer");
\r
798 strcpy(noticeMsg, " 8bit accurate renderer");
\r
801 gettimeofday(¬iceMsgTime, 0);
\r
803 if(which & 0x0300) {
\r
806 if(state_slot < 0) state_slot = 9;
\r
809 if(state_slot > 9) state_slot = 0;
\r
811 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, check_save_file() ? "USED" : "FREE");
\r
812 gettimeofday(¬iceMsgTime, 0);
\r
814 if(which & 0x0080) {
\r
815 engineState = PGS_Menu;
\r
820 static void updateMovie(void)
\r
822 int offs = Pico.m.frame_count*3 + 0x40;
\r
823 if (offs+3 > movie_size) {
\r
826 strcpy(noticeMsg, "END OF MOVIE.");
\r
827 printf("END OF MOVIE.\n");
\r
828 gettimeofday(¬iceMsgTime, 0);
\r
831 PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU
\r
832 if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // A
\r
833 if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // B
\r
834 if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // A
\r
835 PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU
\r
836 if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // A
\r
837 if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // B
\r
838 if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // A
\r
839 PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX
\r
840 if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X
\r
841 if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z
\r
842 PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX
\r
843 if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X
\r
844 if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z
\r
849 static void updateKeys(void)
\r
851 unsigned long keys, allActions[2] = { 0, 0 }, events;
\r
852 static unsigned long prevEvents = 0;
\r
855 keys = gp2x_joystick_read(0);
\r
856 if (keys & GP2X_SELECT) {
\r
857 engineState = select_exits ? PGS_Quit : PGS_Menu;
\r
858 // wait until select is released, so menu would not resume game
\r
859 while (gp2x_joystick_read(1) & GP2X_SELECT) usleep(50*1000);
\r
862 keys &= CONFIGURABLE_KEYS;
\r
864 for (i = 0; i < 32; i++)
\r
866 if (keys & (1 << i)) {
\r
867 int pl, acts = currentConfig.KeyBinds[i];
\r
868 if (!acts) continue;
\r
869 pl = (acts >> 16) & 1;
\r
870 if (combo_keys & (1 << i)) {
\r
871 int u = i+1, acts_c = acts & combo_acts;
\r
872 // let's try to find the other one
\r
874 for (; u < 32; u++)
\r
875 if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) {
\r
876 allActions[pl] |= acts_c;
\r
877 keys &= ~((1 << i) | (1 << u));
\r
880 // add non-combo actions if combo ones were not found
\r
881 if (!acts_c || u == 32)
\r
882 allActions[pl] |= acts & ~combo_acts;
\r
884 allActions[pl] |= acts;
\r
890 if (num_of_joys > 0)
\r
892 gp2x_usbjoy_update();
\r
893 for (joy = 0; joy < num_of_joys; joy++) {
\r
894 int keys = gp2x_usbjoy_check2(joy);
\r
895 for (i = 0; i < 32; i++) {
\r
896 if (keys & (1 << i)) {
\r
897 int acts = currentConfig.JoyBinds[joy][i];
\r
898 int pl = (acts >> 16) & 1;
\r
899 allActions[pl] |= acts;
\r
905 PicoPad[0] = (unsigned short) allActions[0];
\r
906 PicoPad[1] = (unsigned short) allActions[1];
\r
908 events = (allActions[0] | allActions[1]) >> 16;
\r
910 // volume is treated in special way and triggered every frame
\r
911 if(events & 0x6000) {
\r
912 int vol = currentConfig.volume;
\r
913 if (events & 0x2000) {
\r
914 if (vol < 90) vol++;
\r
916 if (vol > 0) vol--;
\r
918 gp2x_sound_volume(vol, vol);
\r
919 sprintf(noticeMsg, "VOL: %02i", vol);
\r
920 gettimeofday(¬iceMsgTime, 0);
\r
921 currentConfig.volume = vol;
\r
924 events &= ~prevEvents;
\r
925 if (events) RunEvents(events);
\r
926 if (movie_data) updateMovie();
\r
928 prevEvents = (allActions[0] | allActions[1]) >> 16;
\r
932 static void updateSound(int len)
\r
934 if (PicoOpt&8) len<<=1;
\r
936 gp2x_sound_write(PsndOut, len<<1);
\r
940 static void SkipFrame(int do_sound)
\r
942 void *sndbuff_tmp = 0;
\r
943 if (PsndOut && !do_sound) {
\r
944 sndbuff_tmp = PsndOut;
\r
952 if (sndbuff_tmp && !do_sound) {
\r
953 PsndOut = sndbuff_tmp;
\r
958 static void simpleWait(int thissec, int lim_time)
\r
960 struct timeval tval;
\r
962 spend_cycles(1024);
\r
963 gettimeofday(&tval, 0);
\r
964 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
966 while(tval.tv_usec < lim_time)
\r
968 spend_cycles(1024);
\r
969 gettimeofday(&tval, 0);
\r
970 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
975 void emu_Loop(void)
\r
977 static int gp2x_old_clock = 200;
\r
978 static int PsndRate_old = 0, PicoOpt_old = 0, PsndLen_real = 0, pal_old = 0;
\r
979 char fpsbuff[24]; // fps count c string
\r
980 struct timeval tval; // timing
\r
981 int thissec = 0, frames_done = 0, frames_shown = 0, oldmodes = 0;
\r
982 int target_fps, target_frametime, lim_time, i;
\r
985 printf("entered emu_Loop()\n");
\r
987 if (gp2x_old_clock != currentConfig.CPUclock) {
\r
988 printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);
\r
989 set_FCLK(currentConfig.CPUclock);
\r
990 gp2x_old_clock = currentConfig.CPUclock;
\r
994 if (gp2x_old_gamma != currentConfig.gamma) {
\r
995 set_gamma(currentConfig.gamma);
\r
996 gp2x_old_gamma = currentConfig.gamma;
\r
997 printf("updated gamma to %i\n", currentConfig.gamma);
\r
1002 // make sure we are in correct mode
\r
1004 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
\r
1007 // pal/ntsc might have changed, reset related stuff
\r
1008 target_fps = Pico.m.pal ? 50 : 60;
\r
1009 target_frametime = 1000000/target_fps;
\r
1012 // prepare sound stuff
\r
1013 if(currentConfig.EmuOpt & 4) {
\r
1014 int snd_excess_add;
\r
1015 if(PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old || crashed_940) {
\r
1016 /* if 940 is turned off, we need it to be put back to sleep */
\r
1017 if (!(PicoOpt&0x200) && ((PicoOpt^PicoOpt_old)&0x200)) {
\r
1023 //excess_samples = PsndRate - PsndLen*target_fps;
\r
1024 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
\r
1025 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
1026 gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);
\r
1027 gp2x_sound_volume(currentConfig.volume, currentConfig.volume);
\r
1028 PicoWriteSound = updateSound;
\r
1029 memset(sndBuffer, 0, sizeof(sndBuffer));
\r
1030 PsndOut = sndBuffer;
\r
1031 PsndRate_old = PsndRate;
\r
1032 PsndLen_real = PsndLen;
\r
1033 PicoOpt_old = PicoOpt;
\r
1034 pal_old = Pico.m.pal;
\r
1040 while (engineState == PGS_Running)
\r
1044 gettimeofday(&tval, 0);
\r
1045 if(reset_timing) {
\r
1047 thissec = tval.tv_sec;
\r
1048 frames_shown = frames_done = tval.tv_usec/target_frametime;
\r
1051 // show notice message?
\r
1052 if(noticeMsgTime.tv_sec) {
\r
1053 static int noticeMsgSum;
\r
1054 if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec
\r
1055 noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;
\r
1059 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
\r
1060 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
\r
1061 notice = noticeMsg;
\r
1065 // check for mode changes
\r
1066 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
\r
1067 if (modes != oldmodes) {
\r
1069 osd_fps_x = OSD_FPS_X;
\r
1071 vidCpyM2 = vidCpyM2_40col;
\r
1073 if (PicoOpt & 0x100) {
\r
1074 vidCpyM2 = vidCpyM2_32col_nobord;
\r
1076 osd_fps_x = OSD_FPS_X - 64;
\r
1078 vidCpyM2 = vidCpyM2_32col;
\r
1081 gp2x_video_RGB_setscaling(scalex, 240);
\r
1086 // second changed?
\r
1087 if(thissec != tval.tv_sec) {
\r
1089 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
\r
1090 if(++bench == 10) {
\r
1092 bench_fps_s = bench_fps;
\r
1093 bf[bfp++ & 3] = bench_fps;
\r
1096 bench_fps += frames_shown;
\r
1097 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
\r
1099 if(currentConfig.EmuOpt & 2)
\r
1100 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
\r
1102 thissec = tval.tv_sec;
\r
1104 if(PsndOut == 0 && currentConfig.Frameskip >= 0) {
\r
1105 frames_done = frames_shown = 0;
\r
1107 // it is quite common for this implementation to leave 1 fame unfinished
\r
1108 // when second changes, but we don't want buffer to starve.
\r
1109 if(PsndOut && frames_done < target_fps && frames_done > target_fps-5) {
\r
1111 SkipFrame(1); frames_done++;
\r
1114 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
\r
1115 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
\r
1116 if (frames_shown > frames_done) frames_shown = frames_done;
\r
1120 lim_time = (frames_done+1) * target_frametime;
\r
1121 if(currentConfig.Frameskip >= 0) { // frameskip enabled
\r
1122 for(i = 0; i < currentConfig.Frameskip; i++) {
\r
1124 SkipFrame(1); frames_done++;
\r
1125 if (PsndOut) { // do framelimitting if sound is enabled
\r
1126 gettimeofday(&tval, 0);
\r
1127 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
1128 if(tval.tv_usec < lim_time) { // we are too fast
\r
1129 simpleWait(thissec, lim_time);
\r
1132 lim_time += target_frametime;
\r
1134 } else if(tval.tv_usec > lim_time) { // auto frameskip
\r
1135 // no time left for this frame - skip
\r
1137 SkipFrame(tval.tv_usec < lim_time+target_frametime); frames_done++;
\r
1145 if (Pico.m.frame_count == 31563) {
\r
1147 f = fopen("ram_p.bin", "wb");
\r
1148 if (!f) { printf("!f\n"); exit(1); }
\r
1149 fwrite(Pico.ram, 1, 0x10000, f);
\r
1157 #define BYTE unsigned char
\r
1158 #define WORD unsigned short
\r
1161 BYTE IDLength; /* 00h Size of Image ID field */
\r
1162 BYTE ColorMapType; /* 01h Color map type */
\r
1163 BYTE ImageType; /* 02h Image type code */
\r
1164 WORD CMapStart; /* 03h Color map origin */
\r
1165 WORD CMapLength; /* 05h Color map length */
\r
1166 BYTE CMapDepth; /* 07h Depth of color map entries */
\r
1167 WORD XOffset; /* 08h X origin of image */
\r
1168 WORD YOffset; /* 0Ah Y origin of image */
\r
1169 WORD Width; /* 0Ch Width of image */
\r
1170 WORD Height; /* 0Eh Height of image */
\r
1171 BYTE PixelDepth; /* 10h Image pixel size */
\r
1172 BYTE ImageDescriptor; /* 11h Image descriptor byte */
\r
1173 } __attribute__((packed)) TGAHEAD;
\r
1174 static unsigned short oldscr[320*240];
\r
1175 FILE *f; char name[128]; int i;
\r
1177 memset(&TGAHEAD, 0, sizeof(TGAHEAD));
\r
1178 TGAHEAD.ImageType = 2;
\r
1179 TGAHEAD.Width = 320;
\r
1180 TGAHEAD.Height = 240;
\r
1181 TGAHEAD.PixelDepth = 16;
\r
1182 TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left
\r
1184 #define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?
\r
1186 for (i = 0; i < 320*240; i++)
\r
1187 if(oldscr[i] != CONV(((unsigned short *)gp2x_screen)[i])) break;
\r
1190 for (i = 0; i < 320*240; i++)
\r
1191 oldscr[i] = CONV(((unsigned short *)gp2x_screen)[i]);
\r
1192 sprintf(name, "%05i.tga", Pico.m.frame_count);
\r
1193 f = fopen(name, "wb");
\r
1194 if (!f) { printf("!f\n"); exit(1); }
\r
1195 fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);
\r
1196 fwrite(oldscr, 1, 320*240*2, f);
\r
1203 gettimeofday(&tval, 0);
\r
1204 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
1206 // sleep if we are still too fast
\r
1207 if(PsndOut != 0 || currentConfig.Frameskip < 0)
\r
1209 // usleep sleeps for ~20ms minimum, so it is not a solution here
\r
1210 gettimeofday(&tval, 0);
\r
1211 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
1212 if(tval.tv_usec < lim_time)
\r
1214 // we are too fast
\r
1215 simpleWait(thissec, lim_time);
\r
1219 blit(fpsbuff, notice);
\r
1221 frames_done++; frames_shown++;
\r
1225 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
1226 blit("", "Writing SRAM/BRAM..");
\r
1227 emu_SaveLoadGame(0, 1);
\r
1233 void emu_ResetGame(void)
\r
1240 size_t gzRead2(void *p, size_t _size, size_t _n, void *file)
\r
1242 return gzread(file, p, _n);
\r
1246 size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)
\r
1248 return gzwrite(file, p, _n);
\r
1252 int emu_SaveLoadGame(int load, int sram)
\r
1255 char saveFname[512];
\r
1257 // make save filename
\r
1258 romfname_ext(saveFname, "");
\r
1259 if(sram) strcat(saveFname, (PicoMCD&1) ? ".brm" : ".srm");
\r
1261 if(state_slot > 0 && state_slot < 10) sprintf(saveFname, "%s.%i", saveFname, state_slot);
\r
1262 strcat(saveFname, ".mds");
\r
1265 printf("saveLoad (%i, %i): %s\n", load, sram, saveFname);
\r
1270 unsigned char *sram_data;
\r
1272 sram_size = 0x2000;
\r
1273 sram_data = Pico_mcd->bram;
\r
1275 sram_size = SRam.end-SRam.start+1;
\r
1276 if(SRam.reg_back & 4) sram_size=0x2000;
\r
1277 sram_data = SRam.data;
\r
1279 if(!sram_data) return 0; // SRam forcefully disabled for this game
\r
1282 sramFile = fopen(saveFname, "rb");
\r
1283 if(!sramFile) return -1;
\r
1284 fread(sram_data, 1, sram_size, sramFile);
\r
1287 // sram save needs some special processing
\r
1288 // see if we have anything to save
\r
1289 for(; sram_size > 0; sram_size--)
\r
1290 if(sram_data[sram_size-1]) break;
\r
1293 sramFile = fopen(saveFname, "wb");
\r
1294 ret = fwrite(sram_data, 1, sram_size, sramFile);
\r
1295 ret = (ret != sram_size) ? -1 : 0;
\r
1304 void *PmovFile = NULL;
\r
1306 if(currentConfig.EmuOpt & 8) {
\r
1307 strcat(saveFname, ".gz");
\r
1308 if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) {
\r
1309 areaRead = gzRead2;
\r
1310 areaWrite = gzWrite2;
\r
1311 areaEof = (areaeof *) gzeof;
\r
1312 areaSeek = (areaseek *) gzseek;
\r
1313 if(!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);
\r
1315 saveFname[strlen(saveFname)-3] = 0;
\r
1317 if(!PmovFile) { // gzip failed or was disabled
\r
1318 if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) {
\r
1319 areaRead = (arearw *) fread;
\r
1320 areaWrite = (arearw *) fwrite;
\r
1321 areaEof = (areaeof *) feof;
\r
1322 areaSeek = (areaseek *) fseek;
\r
1326 ret = PmovState(load ? 6 : 5, PmovFile);
\r
1327 if(areaRead == gzRead2)
\r
1328 gzclose(PmovFile);
\r
1329 else fclose ((FILE *) PmovFile);
\r
1331 if (!load) sync();
\r
1332 else Pico.m.dirtyPal=1;
\r
1336 strcpy(noticeMsg, load ? "GAME LOADED " : "GAME SAVED ");
\r
1339 strcpy(noticeMsg, load ? "LOAD FAILED " : "SAVE FAILED ");
\r
1343 gettimeofday(¬iceMsgTime, 0);
\r