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 char noticeMsg[64]; // notice msg to draw
\r
52 static struct timeval noticeMsgTime = { 0, 0 }; // when started showing
\r
53 static int reset_timing, osd_fps_x;
\r
54 static int combo_keys = 0, combo_acts = 0; // keys and actions which need button combos
\r
55 static int gp2x_old_gamma = 100;
\r
56 static unsigned char *movie_data = NULL;
\r
57 static int movie_size = 0;
\r
58 static int frame_count = 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_ext(char *ext)
\r
88 p = romFileName + strlen(romFileName) - 4;
\r
89 if (p < romFileName) p = romFileName;
\r
92 if((tmp = fopen(romFileName, "rb"))) {
\r
99 int emu_ReloadRom(void)
\r
101 unsigned int rom_size = 0;
\r
106 printf("emu_ReloadRom(%s)\n", romFileName);
\r
108 // detect wrong extensions (.srm and .mds)
\r
109 p = romFileName + strlen(romFileName) - 4;
\r
110 if (p < romFileName) p = romFileName;
\r
111 strncpy(ext, p, 4);
\r
115 if(!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz
\r
116 sprintf(menuErrorMsg, "Not a ROM selected.");
\r
120 // check for movie file
\r
125 if(!strcmp(ext, ".gmv")) {
\r
126 // check for both gmv and rom
\r
128 FILE *movie_file = fopen(romFileName, "rb");
\r
130 sprintf(menuErrorMsg, "Failed to open movie.");
\r
133 fseek(movie_file, 0, SEEK_END);
\r
134 movie_size = ftell(movie_file);
\r
135 fseek(movie_file, 0, SEEK_SET);
\r
136 if(movie_size < 64+3) {
\r
137 sprintf(menuErrorMsg, "Invalid GMV file.");
\r
138 fclose(movie_file);
\r
141 movie_data = malloc(movie_size);
\r
142 if(movie_data == NULL) {
\r
143 sprintf(menuErrorMsg, "low memory.");
\r
144 fclose(movie_file);
\r
147 fread(movie_data, 1, movie_size, movie_file);
\r
148 fclose(movie_file);
\r
149 if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {
\r
150 sprintf(menuErrorMsg, "Invalid GMV file.");
\r
153 dummy = try_rfn_ext(".zip") || try_rfn_ext(".bin") ||
\r
154 try_rfn_ext(".smd") || try_rfn_ext(".gen");
\r
156 sprintf(menuErrorMsg, "Could't find a ROM for movie.");
\r
161 rom = fopen(romFileName, "rb");
\r
163 sprintf(menuErrorMsg, "Failed to open rom.");
\r
174 if(!strcasecmp(ext, ".zip")) {
\r
176 ret = CartLoadZip(romFileName, &rom_data, &rom_size);
\r
178 if (ret == 4) strcpy(menuErrorMsg, "No ROMs in zip found.");
\r
179 else sprintf(menuErrorMsg, "Unzip failed with code %i", ret);
\r
180 printf("%s\n", menuErrorMsg);
\r
184 if( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) {
\r
185 sprintf(menuErrorMsg, "PicoCartLoad() failed.");
\r
186 printf("%s\n", menuErrorMsg);
\r
193 // detect wrong files (Pico crashes on very small files), also see if ROM EP is good
\r
194 if(rom_size <= 0x200 || strncmp((char *)rom_data, "Pico", 4) == 0 ||
\r
195 ((*(unsigned short *)(rom_data+4)<<16)|(*(unsigned short *)(rom_data+6))) >= (int)rom_size) {
\r
196 if (rom_data) free(rom_data);
\r
198 sprintf(menuErrorMsg, "Not a ROM selected.");
\r
202 printf("PicoCartInsert(%p, %d);\n", rom_data, rom_size);
\r
203 if(PicoCartInsert(rom_data, rom_size)) {
\r
204 sprintf(menuErrorMsg, "Failed to load ROM.");
\r
208 // load config for this ROM
\r
209 ret = emu_ReadConfig(1);
\r
213 // emu_ReadConfig() might have messed currentConfig.lastRomFile
\r
214 strncpy(currentConfig.lastRomFile, romFileName, sizeof(currentConfig.lastRomFile)-1);
\r
215 currentConfig.lastRomFile[sizeof(currentConfig.lastRomFile)-1] = 0;
\r
217 // additional movie stuff
\r
219 if(movie_data[0x14] == '6')
\r
220 PicoOpt |= 0x20; // 6 button pad
\r
221 else PicoOpt &= ~0x20;
\r
222 if(movie_data[0xF] >= 'A') {
\r
223 //Pico.m.pal = movie_data[0x16] >> 7;
\r
224 // TODO: bits 6 & 5
\r
226 strcpy(noticeMsg, "MOVIE LOADED");
\r
231 strcpy(noticeMsg, "PAL SYSTEM / 50 FPS");
\r
233 strcpy(noticeMsg, "NTSC SYSTEM / 60 FPS");
\r
236 gettimeofday(¬iceMsgTime, 0);
\r
238 // load SRAM for this ROM
\r
239 if(currentConfig.EmuOpt & 1)
\r
240 emu_SaveLoadGame(1, 1);
\r
248 void emu_Init(void)
\r
250 // make temp buffer for alt renderer
\r
251 framebuff = malloc((8+320)*(8+240+8));
\r
254 printf("framebuff == 0\n");
\r
259 // logf = fopen("log.txt", "w");
\r
263 static void romfname_ext(char *dst, char *ext)
\r
267 // make save filename
\r
268 for (p = romFileName+strlen(romFileName)-1; p >= romFileName && *p != '/'; p--); p++;
\r
269 strncpy(dst, p, 511);
\r
271 if(dst[strlen(dst)-4] == '.') dst[strlen(dst)-4] = 0;
\r
276 static void find_combos(void)
\r
280 // find out which keys and actions are combos
\r
281 combo_keys = combo_acts = 0;
\r
282 for (act = 0; act < 32; act++)
\r
285 if (act == 16) continue; // player2 flag
\r
286 for (u = 0; u < 32; u++)
\r
288 if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;
\r
292 // loop again and mark those keys and actions as combo
\r
293 for (u = 0; u < 32; u++)
\r
295 if (currentConfig.KeyBinds[u] & (1 << act)) {
\r
296 combo_keys |= 1 << u;
\r
297 combo_acts |= 1 << act;
\r
302 // printf("combo keys/acts: %08x %08x\n", combo_keys, combo_acts);
\r
306 int emu_ReadConfig(int game)
\r
314 // set default config
\r
315 memset(¤tConfig, 0, sizeof(currentConfig));
\r
316 currentConfig.lastRomFile[0] = 0;
\r
317 currentConfig.EmuOpt = 0x1f;
\r
318 currentConfig.PicoOpt = 0x0f;
\r
319 currentConfig.PsndRate = 22050;
\r
320 currentConfig.PicoRegion = 0; // auto
\r
321 currentConfig.Frameskip = -1; // auto
\r
322 currentConfig.CPUclock = 200;
\r
323 currentConfig.volume = 50;
\r
324 currentConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU
\r
325 currentConfig.KeyBinds[ 4] = 1<<1;
\r
326 currentConfig.KeyBinds[ 2] = 1<<2;
\r
327 currentConfig.KeyBinds[ 6] = 1<<3;
\r
328 currentConfig.KeyBinds[14] = 1<<4;
\r
329 currentConfig.KeyBinds[13] = 1<<5;
\r
330 currentConfig.KeyBinds[12] = 1<<6;
\r
331 currentConfig.KeyBinds[ 8] = 1<<7;
\r
332 currentConfig.KeyBinds[15] = 1<<26; // switch rend
\r
333 currentConfig.KeyBinds[10] = 1<<27; // save state
\r
334 currentConfig.KeyBinds[11] = 1<<28; // load state
\r
335 currentConfig.KeyBinds[23] = 1<<29; // vol up
\r
336 currentConfig.KeyBinds[22] = 1<<30; // vol down
\r
337 currentConfig.gamma = 100;
\r
338 strncpy(cfg, PicoConfigFile, 511);
\r
341 romfname_ext(cfg, ".pbcfg");
\r
344 printf("emu_ReadConfig: %s ", cfg);
\r
345 f = fopen(cfg, "rb");
\r
347 bread = fread(¤tConfig, 1, sizeof(currentConfig), f);
\r
350 printf((bread == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n");
\r
352 PicoOpt = currentConfig.PicoOpt;
\r
353 PsndRate = currentConfig.PsndRate;
\r
354 PicoRegionOverride = currentConfig.PicoRegion;
\r
355 if (PicoOpt & 0x20) {
\r
356 actionNames[ 8] = "Z"; actionNames[ 9] = "Y";
\r
357 actionNames[10] = "X"; actionNames[11] = "MODE";
\r
359 // some sanity checks
\r
360 if (currentConfig.CPUclock < 1 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;
\r
361 if (currentConfig.gamma < 10 || currentConfig.gamma > 300) currentConfig.gamma = 100;
\r
362 // if volume keys are unbound, bind them to volume control
\r
363 if (!currentConfig.KeyBinds[23] && !currentConfig.KeyBinds[22]) {
\r
364 currentConfig.KeyBinds[23] = 1<<29; // vol up
\r
365 currentConfig.KeyBinds[22] = 1<<30; // vol down
\r
368 return (bread == sizeof(currentConfig));
\r
372 int emu_WriteConfig(int game)
\r
380 strncpy(cfg, PicoConfigFile, 511);
\r
383 romfname_ext(cfg, ".pbcfg");
\r
386 printf("emu_WriteConfig: %s ", cfg);
\r
387 f = fopen(cfg, "wb");
\r
389 currentConfig.PicoOpt = PicoOpt;
\r
390 currentConfig.PsndRate = PsndRate;
\r
391 currentConfig.PicoRegion = PicoRegionOverride;
\r
392 bwrite = fwrite(¤tConfig, 1, sizeof(currentConfig), f);
\r
397 printf((bwrite == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n");
\r
399 return (bwrite == sizeof(currentConfig));
\r
403 void emu_Deinit(void)
\r
406 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
407 emu_SaveLoadGame(0, 1);
\r
411 if (!(currentConfig.EmuOpt & 0x20))
\r
412 emu_WriteConfig(0);
\r
419 if (gp2x_old_gamma != 100)
\r
424 void osd_text(int x, int y, char *text)
\r
426 int len = strlen(text)*8;
\r
428 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
\r
429 int *p, i, h, black, white;
\r
430 if (PicoOpt&0x10) {
\r
431 black = 0x40404040; white = 0x41;
\r
433 black = 0xe0e0e0e0; white = 0xf0;
\r
435 x &= ~3; // align x
\r
436 len = (len+3) >> 2;
\r
437 for (h = 0; h < 8; h++) {
\r
438 p = (int *) ((unsigned char *) gp2x_screen+x+320*(y+h));
\r
439 for (i = len; i; i--, p++) *p = black;
\r
441 gp2x_text_out8_2(x, y, text, white);
\r
444 x &= ~1; // align x
\r
445 len = (len+1) >> 1;
\r
446 for (h = 0; h < 8; h++) {
\r
447 p = (int *) ((unsigned short *) gp2x_screen+x+320*(y+h));
\r
448 for (i = len; i; i--, p++) *p = (*p>>2)&0x39e7;
\r
450 gp2x_text_out15(x, y, text);
\r
454 static int EmuScan16(unsigned int num, void *sdata)
\r
456 if (!(Pico.video.reg[1]&8)) num += 8;
\r
457 DrawLineDest = (unsigned short *) gp2x_screen + 320*(num+1);
\r
462 static int EmuScan8(unsigned int num, void *sdata)
\r
464 if (!(Pico.video.reg[1]&8)) num += 8;
\r
465 DrawLineDest = (unsigned char *) gp2x_screen + 320*(num+1);
\r
470 static int localPal[0x100];
\r
471 static void (*vidCpyM2)(void *dest, void *src) = NULL;
\r
473 static void blit(char *fps, char *notice)
\r
475 if (PicoOpt&0x10) {
\r
476 // 8bit fast renderer
\r
477 if (Pico.m.dirtyPal) {
\r
478 Pico.m.dirtyPal = 0;
\r
479 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
480 // feed new palette to our device
\r
481 gp2x_video_setpalette(localPal, 0x40);
\r
483 vidCpyM2((unsigned char *)gp2x_screen+320*8, framebuff+328*8);
\r
484 } else if (!(currentConfig.EmuOpt&0x80)) {
\r
485 // 8bit accurate renderer
\r
486 if (Pico.m.dirtyPal) {
\r
487 Pico.m.dirtyPal = 0;
\r
488 if(Pico.video.reg[0xC]&8) { // shadow/hilight mode
\r
489 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
490 vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
\r
491 vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);
\r
492 blockcpy(localPal+0xc0, localPal+0x40, 0x40*4);
\r
493 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
494 localPal[0xf0] = 0x00ffffff;
\r
495 gp2x_video_setpalette(localPal, 0x100);
\r
496 } else if (rendstatus & 0x20) { // mid-frame palette changes
\r
497 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
498 vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);
\r
499 vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);
\r
500 gp2x_video_setpalette(localPal, 0xc0);
\r
502 vidConvCpyRGB32(localPal, Pico.cram, 0x40);
\r
503 gp2x_video_setpalette(localPal, 0x40);
\r
508 if (notice) osd_text(4, 232, notice);
\r
509 if (currentConfig.EmuOpt & 2)
\r
510 osd_text(osd_fps_x, 232, fps);
\r
512 //gp2x_video_wait_vsync();
\r
515 if (!(PicoOpt&0x10)) {
\r
516 if (!(Pico.video.reg[1]&8)) {
\r
517 if (currentConfig.EmuOpt&0x80) {
\r
518 DrawLineDest = (unsigned short *) gp2x_screen + 320*8;
\r
520 DrawLineDest = (unsigned char *) gp2x_screen + 320*8;
\r
523 DrawLineDest = gp2x_screen;
\r
529 // clears whole screen or just the notice area (in all buffers)
\r
530 static void clearArea(int full)
\r
532 if (PicoOpt&0x10) {
\r
533 // 8bit fast renderer
\r
534 if (full) gp2x_memset_all_buffers(0, 0x40, 320*240);
\r
535 else gp2x_memset_all_buffers(320*232, 0x40, 320*8);
\r
536 } else if (currentConfig.EmuOpt&0x80) {
\r
537 // 16bit accurate renderer
\r
538 if (full) gp2x_memset_all_buffers(0, 0, 320*240*2);
\r
539 else gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
\r
541 // 8bit accurate renderer
\r
542 if (full) gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
543 else gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
\r
548 static void vidResetMode(void)
\r
550 if (PicoOpt&0x10) {
\r
551 localPal[0x40] = 0;
\r
552 localPal[0x41] = 0x00ffffff;
\r
553 gp2x_video_changemode(8);
\r
554 gp2x_video_setpalette(localPal, 0x42);
\r
555 gp2x_memset_all_buffers(0, 0x40, 320*240);
\r
557 } else if (currentConfig.EmuOpt&0x80) {
\r
558 gp2x_video_changemode(15);
\r
559 PicoDrawSetColorFormat(1);
\r
560 PicoScan = EmuScan16;
\r
563 localPal[0xe0] = 0x00000000; // reserved pixels for OSD
\r
564 localPal[0xf0] = 0x00ffffff;
\r
565 gp2x_video_changemode(8);
\r
566 gp2x_video_setpalette(localPal, 0x100);
\r
567 gp2x_memset_all_buffers(0, 0xe0, 320*240);
\r
569 PicoDrawSetColorFormat(2);
\r
570 PicoScan = EmuScan8;
\r
573 Pico.m.dirtyPal = 1;
\r
575 gp2x_video_RGB_setscaling((PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);
\r
579 static int check_save_file(void)
\r
581 char saveFname[512];
\r
586 if(state_slot > 0 && state_slot < 10) sprintf(ext, ".%i", state_slot);
\r
587 strcat(ext, ".mds");
\r
588 if(currentConfig.EmuOpt & 8) strcat(ext, ".gz");
\r
590 romfname_ext(saveFname, ext);
\r
591 if ((f = fopen(saveFname, "rb"))) {
\r
598 static void RunEvents(unsigned int which)
\r
600 if(which & 0x1800) { // save or load (but not both)
\r
602 if (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200) && check_save_file()) {
\r
603 unsigned long keys;
\r
604 blit("", "OVERWRITE SAVE? (Y=yes, X=no)");
\r
605 while( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )
\r
607 if (keys & GP2X_X) do_it = 0;
\r
611 blit("", (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
\r
612 emu_SaveLoadGame(which & 0x1000, 0);
\r
617 if(which & 0x0400) { // switch renderer
\r
618 if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
\r
619 else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;
\r
620 else currentConfig.EmuOpt &= ~0x80;
\r
624 if (PicoOpt&0x10) {
\r
625 strcpy(noticeMsg, " 8bit fast renderer");
\r
626 } else if (currentConfig.EmuOpt&0x80) {
\r
627 strcpy(noticeMsg, "16bit accurate renderer");
\r
629 strcpy(noticeMsg, " 8bit accurate renderer");
\r
632 gettimeofday(¬iceMsgTime, 0);
\r
634 if(which & 0x0300) {
\r
637 if(state_slot < 0) state_slot = 9;
\r
640 if(state_slot > 9) state_slot = 0;
\r
642 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, check_save_file() ? "USED" : "FREE");
\r
643 gettimeofday(¬iceMsgTime, 0);
\r
645 if(which & 0x0080) {
\r
646 engineState = PGS_Menu;
\r
651 static void updateKeys(void)
\r
653 unsigned long keys, allActions[2] = { 0, 0 }, events;
\r
654 static unsigned long prevEvents = 0;
\r
657 keys = gp2x_joystick_read(0);
\r
658 if (keys & GP2X_SELECT) {
\r
659 engineState = select_exits ? PGS_Quit : PGS_Menu;
\r
660 // wait until select is released, so menu would not resume game
\r
661 while (gp2x_joystick_read(1) & GP2X_SELECT) usleep(50*1000);
\r
664 keys &= CONFIGURABLE_KEYS;
\r
666 for (i = 0; i < 32; i++)
\r
668 if (keys & (1 << i)) {
\r
669 int pl, acts = currentConfig.KeyBinds[i];
\r
670 if (!acts) continue;
\r
671 pl = (acts >> 16) & 1;
\r
672 if (combo_keys & (1 << i)) {
\r
673 int u = i+1, acts_c = acts & combo_acts;
\r
674 // let's try to find the other one
\r
676 for (; u < 32; u++)
\r
677 if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) {
\r
678 allActions[pl] |= acts_c;
\r
679 keys &= ~((1 << i) | (1 << u));
\r
682 // add non-combo actions if combo ones were not found
\r
683 if (!acts_c || u == 32)
\r
684 allActions[pl] |= acts & ~combo_acts;
\r
686 allActions[pl] |= acts;
\r
692 if (num_of_joys > 0)
\r
694 gp2x_usbjoy_update();
\r
695 for (joy = 0; joy < num_of_joys; joy++) {
\r
696 int keys = gp2x_usbjoy_check2(joy);
\r
697 for (i = 0; i < 32; i++) {
\r
698 if (keys & (1 << i)) {
\r
699 int acts = currentConfig.JoyBinds[joy][i];
\r
700 int pl = (acts >> 16) & 1;
\r
701 allActions[pl] |= acts;
\r
709 int offs = frame_count*3 + 0x40;
\r
710 if (offs+3 > movie_size) {
\r
713 strcpy(noticeMsg, "END OF MOVIE.");
\r
714 printf("END OF MOVIE.\n");
\r
715 gettimeofday(¬iceMsgTime, 0);
\r
718 PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU
\r
719 if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // A
\r
720 if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // B
\r
721 if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // A
\r
722 PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU
\r
723 if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // A
\r
724 if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // B
\r
725 if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // A
\r
726 PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX
\r
727 if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X
\r
728 if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z
\r
729 PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX
\r
730 if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X
\r
731 if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z
\r
732 if ((PicoPad[0] & 0x80) || (PicoPad[1] & 0x80))
\r
733 printf("%d: start\n", frame_count);
\r
738 PicoPad[0] = (unsigned short) allActions[0];
\r
739 PicoPad[1] = (unsigned short) allActions[1];
\r
743 events = (allActions[0] | allActions[1]) >> 16;
\r
745 // volume is treated in special way and triggered every frame
\r
746 if(events & 0x6000) {
\r
747 int vol = currentConfig.volume;
\r
748 if (events & 0x2000) {
\r
749 if (vol < 90) vol++;
\r
751 if (vol > 0) vol--;
\r
753 gp2x_sound_volume(vol, vol);
\r
754 sprintf(noticeMsg, "VOL: %02i", vol);
\r
755 gettimeofday(¬iceMsgTime, 0);
\r
756 currentConfig.volume = vol;
\r
759 events &= ~prevEvents;
\r
760 if (events) RunEvents(events);
\r
762 prevEvents = (allActions[0] | allActions[1]) >> 16;
\r
765 static int snd_excess_add = 0, snd_excess_cnt = 0; // hack
\r
767 static void updateSound(void)
\r
769 int len = (PicoOpt&8)?PsndLen*2:PsndLen;
\r
771 snd_excess_cnt += snd_excess_add;
\r
772 if (snd_excess_cnt >= 0x10000) {
\r
773 snd_excess_cnt -= 0x10000;
\r
775 PsndOut[len] = PsndOut[len-2];
\r
776 PsndOut[len+1] = PsndOut[len-1];
\r
779 PsndOut[len] = PsndOut[len-1];
\r
784 gp2x_sound_write(PsndOut, len<<1);
\r
788 static void SkipFrame(int do_sound)
\r
790 void *sndbuff_tmp = 0;
\r
791 if (PsndOut && !do_sound) {
\r
792 sndbuff_tmp = PsndOut;
\r
800 if (sndbuff_tmp && !do_sound) {
\r
801 PsndOut = sndbuff_tmp;
\r
806 static void simpleWait(int thissec, int lim_time)
\r
808 struct timeval tval;
\r
810 spend_cycles(1024);
\r
811 gettimeofday(&tval, 0);
\r
812 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
814 while(tval.tv_usec < lim_time)
\r
816 spend_cycles(1024);
\r
817 gettimeofday(&tval, 0);
\r
818 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
823 void emu_Loop(void)
\r
825 static int gp2x_old_clock = 200;
\r
826 static int PsndRate_old = 0, PicoOpt_old = 0, PsndLen_real = 0, pal_old = 0;
\r
827 char fpsbuff[24]; // fps count c string
\r
828 struct timeval tval; // timing
\r
829 int thissec = 0, frames_done = 0, frames_shown = 0, oldmodes = 0;
\r
830 int target_fps, target_frametime, lim_time, i;
\r
833 printf("entered emu_Loop()\n");
\r
835 if (gp2x_old_clock != currentConfig.CPUclock) {
\r
836 printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);
\r
837 set_FCLK(currentConfig.CPUclock);
\r
838 gp2x_old_clock = currentConfig.CPUclock;
\r
842 if (gp2x_old_gamma != currentConfig.gamma) {
\r
843 set_gamma(currentConfig.gamma);
\r
844 gp2x_old_gamma = currentConfig.gamma;
\r
845 printf("updated gamma to %i\n", currentConfig.gamma);
\r
850 // make sure we are in correct mode
\r
852 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
\r
855 // pal/ntsc might have changed, reset related stuff
\r
856 target_fps = Pico.m.pal ? 50 : 60;
\r
857 target_frametime = 1000000/target_fps;
\r
860 // prepare sound stuff
\r
861 if(currentConfig.EmuOpt & 4) {
\r
862 if(PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old || crashed_940) {
\r
863 /* if 940 is turned off, we need it to be put back to sleep */
\r
864 if (!(PicoOpt&0x200) && ((PicoOpt^PicoOpt_old)&0x200)) {
\r
870 //excess_samples = PsndRate - PsndLen*target_fps;
\r
871 snd_excess_cnt = 0;
\r
872 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
\r
873 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
874 gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);
\r
875 gp2x_sound_volume(currentConfig.volume, currentConfig.volume);
\r
876 PicoWriteSound = updateSound;
\r
877 PsndOut = calloc((PicoOpt&8) ? (PsndLen*4+4) : (PsndLen*2+2), 1);
\r
878 PsndRate_old = PsndRate;
\r
879 PsndLen_real = PsndLen;
\r
880 PicoOpt_old = PicoOpt;
\r
881 pal_old = Pico.m.pal;
\r
887 while (engineState == PGS_Running)
\r
891 gettimeofday(&tval, 0);
\r
894 thissec = tval.tv_sec;
\r
895 frames_shown = frames_done = tval.tv_usec/target_frametime;
\r
898 // show notice message?
\r
899 if(noticeMsgTime.tv_sec) {
\r
900 static int noticeMsgSum;
\r
901 if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec
\r
902 noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;
\r
906 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
\r
907 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
\r
908 notice = noticeMsg;
\r
912 // check for mode changes
\r
913 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
\r
914 if (modes != oldmodes) {
\r
916 osd_fps_x = OSD_FPS_X;
\r
918 vidCpyM2 = vidCpyM2_40col;
\r
920 if (PicoOpt & 0x100) {
\r
921 vidCpyM2 = vidCpyM2_32col_nobord;
\r
923 osd_fps_x = OSD_FPS_X - 64;
\r
925 vidCpyM2 = vidCpyM2_32col;
\r
928 gp2x_video_RGB_setscaling(scalex, 240);
\r
934 if(thissec != tval.tv_sec) {
\r
936 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
\r
937 if(++bench == 10) {
\r
939 bench_fps_s = bench_fps;
\r
940 bf[bfp++ & 3] = bench_fps;
\r
943 bench_fps += frames_shown;
\r
944 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
\r
946 if(currentConfig.EmuOpt & 2)
\r
947 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
\r
949 thissec = tval.tv_sec;
\r
951 if(PsndOut == 0 && currentConfig.Frameskip >= 0) {
\r
952 frames_done = frames_shown = 0;
\r
954 // it is quite common for this implementation to leave 1 fame unfinished
\r
955 // when second changes, but we don't want buffer to starve.
\r
956 if(PsndOut && frames_done < target_fps && frames_done > target_fps-5) {
\r
958 SkipFrame(1); frames_done++;
\r
961 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
\r
962 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
\r
963 if (frames_shown > frames_done) frames_shown = frames_done;
\r
967 lim_time = (frames_done+1) * target_frametime;
\r
968 if(currentConfig.Frameskip >= 0) { // frameskip enabled
\r
969 for(i = 0; i < currentConfig.Frameskip; i++) {
\r
971 SkipFrame(1); frames_done++;
\r
972 if (PsndOut) { // do framelimitting if sound is enabled
\r
973 gettimeofday(&tval, 0);
\r
974 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
975 if(tval.tv_usec < lim_time) { // we are too fast
\r
976 simpleWait(thissec, lim_time);
\r
979 lim_time += target_frametime;
\r
981 } else if(tval.tv_usec > lim_time) { // auto frameskip
\r
982 // no time left for this frame - skip
\r
984 SkipFrame(tval.tv_usec < lim_time+target_frametime); frames_done++;
\r
992 gettimeofday(&tval, 0);
\r
993 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
995 // sleep if we are still too fast
\r
996 if(PsndOut != 0 || currentConfig.Frameskip < 0)
\r
998 // usleep sleeps for ~20ms minimum, so it is not a solution here
\r
999 gettimeofday(&tval, 0);
\r
1000 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
1001 if(tval.tv_usec < lim_time)
\r
1003 // we are too fast
\r
1004 simpleWait(thissec, lim_time);
\r
1008 blit(fpsbuff, notice);
\r
1010 frames_done++; frames_shown++;
\r
1014 if((currentConfig.EmuOpt & 1) && SRam.changed) {
\r
1015 emu_SaveLoadGame(0, 1);
\r
1019 if (PsndOut != 0) {
\r
1026 void emu_ResetGame(void)
\r
1033 size_t gzRead2(void *p, size_t _size, size_t _n, void *file)
\r
1035 return gzread(file, p, _n);
\r
1039 size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)
\r
1041 return gzwrite(file, p, _n);
\r
1044 typedef unsigned int (*STATE_SL_FUNC)(void *, unsigned int, unsigned int, void *);
\r
1046 int emu_SaveLoadGame(int load, int sram)
\r
1049 char saveFname[512];
\r
1051 // make save filename
\r
1052 romfname_ext(saveFname, "");
\r
1053 if(sram) strcat(saveFname, ".srm");
\r
1055 if(state_slot > 0 && state_slot < 10) sprintf(saveFname, "%s.%i", saveFname, state_slot);
\r
1056 strcat(saveFname, ".mds");
\r
1059 printf("saveLoad (%i, %i): %s\n", load, sram, saveFname);
\r
1063 int sram_size = SRam.end-SRam.start+1;
\r
1064 if(SRam.reg_back & 4) sram_size=0x2000;
\r
1065 if(!SRam.data) return 0; // SRam forcefully disabled for this game
\r
1067 sramFile = fopen(saveFname, "rb");
\r
1068 if(!sramFile) return -1;
\r
1069 fread(SRam.data, 1, sram_size, sramFile);
\r
1072 // sram save needs some special processing
\r
1073 // see if we have anything to save
\r
1074 for(; sram_size > 0; sram_size--)
\r
1075 if(SRam.data[sram_size-1]) break;
\r
1078 sramFile = fopen(saveFname, "wb");
\r
1079 ret = fwrite(SRam.data, 1, sram_size, sramFile);
\r
1080 ret = (ret != sram_size) ? -1 : 0;
\r
1087 void *PmovFile = NULL;
\r
1089 if(currentConfig.EmuOpt & 8) {
\r
1090 strcat(saveFname, ".gz");
\r
1091 if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) {
\r
1092 areaRead = gzRead2;
\r
1093 areaWrite = gzWrite2;
\r
1094 if(!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);
\r
1096 saveFname[strlen(saveFname)-3] = 0;
\r
1098 if(!PmovFile) { // gzip failed or was disabled
\r
1099 if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) {
\r
1100 areaRead = (STATE_SL_FUNC) fread;
\r
1101 areaWrite = (STATE_SL_FUNC) fwrite;
\r
1105 PmovState(load ? 6 : 5, PmovFile);
\r
1106 strcpy(noticeMsg, load ? "GAME LOADED " : "GAME SAVED ");
\r
1107 if(areaRead == gzRead2)
\r
1108 gzclose(PmovFile);
\r
1109 else fclose ((FILE *) PmovFile);
\r
1111 if (!load) sync();
\r
1112 else Pico.m.dirtyPal=1;
\r
1114 strcpy(noticeMsg, load ? "LOAD FAILED " : "SAVE FAILED ");
\r
1118 gettimeofday(¬iceMsgTime, 0);
\r