region detection, cd states wip, fixes, stuff
[picodrive.git] / platform / gp2x / emu.c
CommitLineData
cc68a136 1// (c) Copyright 2006 notaz, All rights reserved.\r
2// Free for non-commercial use.\r
3\r
4// For commercial use, separate licencing terms must be obtained.\r
5\r
6#include <stdio.h>\r
7#include <stdlib.h>\r
8#include <sys/time.h>\r
9#include <linux/limits.h>\r
10#include <ctype.h>\r
11#include <unistd.h>\r
12\r
13#include <stdarg.h>\r
14\r
15#include "emu.h"\r
16#include "gp2x.h"\r
17#include "usbjoy.h"\r
18#include "menu.h"\r
19#include "asmutils.h"\r
20#include "cpuctrl.h"\r
21\r
22#include "Pico/PicoInt.h"\r
23#include "zlib/zlib.h"\r
24\r
25\r
26#ifdef BENCHMARK\r
27#define OSD_FPS_X 220\r
28#else\r
29#define OSD_FPS_X 260\r
30#endif\r
31\r
32// PicoPad[] format: SACB RLDU\r
33char *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
39};\r
40\r
41int engineState;\r
42int select_exits = 0;\r
43char *PicoConfigFile = "picoconfig.bin";\r
44currentConfig_t currentConfig;\r
45\r
46char romFileName[PATH_MAX];\r
47unsigned char *rom_data = NULL;\r
48\r
49extern int crashed_940;\r
50\r
51static char noticeMsg[64]; // notice msg to draw\r
52static struct timeval noticeMsgTime = { 0, 0 }; // when started showing\r
53static int reset_timing, osd_fps_x;\r
54static int combo_keys = 0, combo_acts = 0; // keys and actions which need button combos\r
55static int gp2x_old_gamma = 100;\r
56static unsigned char *movie_data = NULL;\r
57static int movie_size = 0;\r
cc68a136 58unsigned char *framebuff = 0; // temporary buffer for alt renderer\r
59int state_slot = 0;\r
60\r
61/*\r
62// tmp\r
63static FILE *logf = NULL;\r
64\r
65void pprintf(char *texto, ...)\r
66{\r
67 va_list args;\r
68\r
69 va_start(args,texto);\r
70 vfprintf(logf,texto,args);\r
71 va_end(args);\r
72 fflush(logf);\r
73 sync();\r
74}\r
75*/\r
76// utilities\r
77static void strlwr(char* string)\r
78{\r
79 while ( (*string++ = (char)tolower(*string)) );\r
80}\r
81\r
672ad671 82static int try_rfn_cut(void)\r
cc68a136 83{\r
84 FILE *tmp;\r
85 char *p;\r
86\r
672ad671 87 p = romFileName + strlen(romFileName) - 1;\r
88 for (; p > romFileName; p--)\r
89 if (*p == '.') break;\r
90 *p = 0;\r
cc68a136 91\r
92 if((tmp = fopen(romFileName, "rb"))) {\r
93 fclose(tmp);\r
94 return 1;\r
95 }\r
96 return 0;\r
97}\r
98\r
bf098bc5 99static void get_ext(char *file, char *ext)\r
8c1952f0 100{\r
101 char *p;\r
102\r
bf098bc5 103 p = file + strlen(file) - 4;\r
104 if (p < file) p = file;\r
8c1952f0 105 strncpy(ext, p, 4);\r
106 ext[4] = 0;\r
107 strlwr(ext);\r
108}\r
109\r
bf098bc5 110char *biosfiles_us[] = { "us_scd2_9306", "SegaCDBIOS9303", "us_scd1_9210" };\r
111char *biosfiles_eu[] = { "eu_mcd2_9306", "eu_mcd2_9303", "eu_mcd1_9210" };\r
112char *biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };\r
113\r
114extern char **g_argv;\r
115\r
116int find_bios(int region, char **bios_file)\r
117{\r
118 static char bios_path[1024];\r
119 int i, j, count;\r
120 char **files;\r
121 FILE *f = NULL;\r
122\r
123 if (region == 4) { // US\r
124 files = biosfiles_us;\r
125 count = sizeof(biosfiles_us) / sizeof(char *);\r
126 } else if (region == 8) { // EU\r
127 files = biosfiles_eu;\r
128 count = sizeof(biosfiles_eu) / sizeof(char *);\r
129 } else if (region == 1 || region == 2) {\r
130 files = biosfiles_jp;\r
131 count = sizeof(biosfiles_jp) / sizeof(char *);\r
132 } else {\r
133 return 0;\r
134 }\r
135\r
136 for (i = 0; i < count; i++)\r
137 {\r
138 strncpy(bios_path, g_argv[0], 1023);\r
139 bios_path[1024-32] = 0;\r
140 for (j = strlen(bios_path); j > 0; j--)\r
141 if (bios_path[j] == '/') { bios_path[j+1] = 0; break; }\r
142 strcat(bios_path, files[i]);\r
143 strcat(bios_path, ".bin");\r
144 f = fopen(bios_path, "rb");\r
145 if (f) break;\r
146\r
147 bios_path[strlen(bios_path) - 4] = 0;\r
148 strcat(bios_path, ".zip");\r
149 f = fopen(bios_path, "rb");\r
150 if (f) break;\r
151 }\r
152\r
153 if (f) {\r
154 printf("using bios: %s\n", bios_path);\r
155 fclose(f);\r
156 if (bios_file) *bios_file = bios_path;\r
157 return 1;\r
158 } else {\r
159 sprintf(menuErrorMsg, "no %s BIOS files found, read docs",\r
160 region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r
161 printf("%s\n", menuErrorMsg);\r
162 return 0;\r
163 }\r
164}\r
165\r
166/* checks if romFileName points to valid MegaCD image\r
167 * if so, checks for suitable BIOS */\r
168static int cd_check(char *ext, char **bios_file)\r
169{\r
170 unsigned char buf[32];\r
171 FILE *cd_f;\r
172 int type = 0, region = 4; // 1: Japan, 4: US, 8: Europe\r
173\r
174 cd_f = fopen(romFileName, "rb");\r
175 if (!cd_f) return 0; // let the upper level handle this\r
176\r
177 if (fread(buf, 1, 32, cd_f) != 32) {\r
178 fclose(cd_f);\r
179 return 0;\r
180 }\r
181\r
182 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) type = 1; // Sega CD (ISO)\r
183 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) type = 2; // Sega CD (BIN)\r
184 if (type == 0) {\r
185 fclose(cd_f);\r
186 return 0;\r
187 }\r
188\r
189 /* it seems we have a CD image here. Try to detect region and load a suitable BIOS now.. */\r
cb0316e4 190 fseek(cd_f, (type == 1) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);\r
bf098bc5 191 fread(buf, 1, 1, cd_f);\r
192 fclose(cd_f);\r
193\r
cb0316e4 194 if (buf[0] == 0x64) region = 8; // EU\r
bf098bc5 195 if (buf[0] == 0xa1) region = 1; // JAP\r
196\r
197 printf("detected %s Sega/Mega CD image with %s region\n",\r
198 type == 2 ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r
199\r
200 if (PicoRegionOverride) {\r
201 region = PicoRegionOverride;\r
202 printf("overrided region to %s\n", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r
203 }\r
204\r
205 if(find_bios(region, bios_file))\r
206 return type; // CD and BIOS detected\r
207\r
208 return -1; // CD detected but load failed\r
209}\r
210\r
cc68a136 211int emu_ReloadRom(void)\r
212{\r
213 unsigned int rom_size = 0;\r
bf098bc5 214 char *used_rom_name = romFileName;\r
8c1952f0 215 char ext[5];\r
cc68a136 216 FILE *rom;\r
bf098bc5 217 int ret, cd_state;\r
cc68a136 218\r
219 printf("emu_ReloadRom(%s)\n", romFileName);\r
220\r
bf098bc5 221 get_ext(romFileName, ext);\r
cc68a136 222\r
bf098bc5 223 // detect wrong extensions\r
cc68a136 224 if(!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz\r
225 sprintf(menuErrorMsg, "Not a ROM selected.");\r
226 return 0;\r
227 }\r
228\r
229 // check for movie file\r
230 if(movie_data) {\r
231 free(movie_data);\r
232 movie_data = 0;\r
233 }\r
234 if(!strcmp(ext, ".gmv")) {\r
235 // check for both gmv and rom\r
236 int dummy;\r
237 FILE *movie_file = fopen(romFileName, "rb");\r
238 if(!movie_file) {\r
239 sprintf(menuErrorMsg, "Failed to open movie.");\r
240 return 0;\r
241 }\r
242 fseek(movie_file, 0, SEEK_END);\r
243 movie_size = ftell(movie_file);\r
244 fseek(movie_file, 0, SEEK_SET);\r
245 if(movie_size < 64+3) {\r
246 sprintf(menuErrorMsg, "Invalid GMV file.");\r
247 fclose(movie_file);\r
248 return 0;\r
249 }\r
250 movie_data = malloc(movie_size);\r
251 if(movie_data == NULL) {\r
252 sprintf(menuErrorMsg, "low memory.");\r
253 fclose(movie_file);\r
254 return 0;\r
255 }\r
256 fread(movie_data, 1, movie_size, movie_file);\r
257 fclose(movie_file);\r
258 if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {\r
259 sprintf(menuErrorMsg, "Invalid GMV file.");\r
260 return 0;\r
261 }\r
672ad671 262 dummy = try_rfn_cut() || try_rfn_cut();\r
cc68a136 263 if (!dummy) {\r
264 sprintf(menuErrorMsg, "Could't find a ROM for movie.");\r
265 return 0;\r
266 }\r
bf098bc5 267 get_ext(romFileName, ext);\r
cc68a136 268 }\r
269\r
bf098bc5 270 // check for MegaCD image\r
271 cd_state = cd_check(ext, &used_rom_name);\r
272 if (cd_state > 0) {\r
273 PicoMCD |= 1;\r
274 get_ext(used_rom_name, ext);\r
275 } else if (cd_state == -1) {\r
276 // bios_help() ?\r
277 return 0;\r
278 } else {\r
279 PicoMCD &= ~1;\r
280 }\r
281\r
282 rom = fopen(used_rom_name, "rb");\r
cc68a136 283 if(!rom) {\r
284 sprintf(menuErrorMsg, "Failed to open rom.");\r
285 return 0;\r
286 }\r
287\r
288 if(rom_data) {\r
289 free(rom_data);\r
290 rom_data = 0;\r
291 rom_size = 0;\r
292 }\r
293\r
294 // zipfile support\r
295 if(!strcasecmp(ext, ".zip")) {\r
296 fclose(rom);\r
bf098bc5 297 ret = CartLoadZip(used_rom_name, &rom_data, &rom_size);\r
cc68a136 298 if(ret) {\r
bf098bc5 299 if (ret == 4) strcpy(menuErrorMsg, "No ROMs found in zip.");\r
cc68a136 300 else sprintf(menuErrorMsg, "Unzip failed with code %i", ret);\r
301 printf("%s\n", menuErrorMsg);\r
302 return 0;\r
303 }\r
304 } else {\r
305 if( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) {\r
306 sprintf(menuErrorMsg, "PicoCartLoad() failed.");\r
307 printf("%s\n", menuErrorMsg);\r
308 fclose(rom);\r
309 return 0;\r
310 }\r
311 fclose(rom);\r
312 }\r
313\r
314 // detect wrong files (Pico crashes on very small files), also see if ROM EP is good\r
315 if(rom_size <= 0x200 || strncmp((char *)rom_data, "Pico", 4) == 0 ||\r
316 ((*(unsigned short *)(rom_data+4)<<16)|(*(unsigned short *)(rom_data+6))) >= (int)rom_size) {\r
317 if (rom_data) free(rom_data);\r
318 rom_data = 0;\r
319 sprintf(menuErrorMsg, "Not a ROM selected.");\r
320 return 0;\r
321 }\r
322\r
bf098bc5 323 // load config for this ROM (do this before insert to get correct region)\r
324 ret = emu_ReadConfig(1);\r
325 if (!ret)\r
326 emu_ReadConfig(0);\r
327\r
cc68a136 328 printf("PicoCartInsert(%p, %d);\n", rom_data, rom_size);\r
329 if(PicoCartInsert(rom_data, rom_size)) {\r
330 sprintf(menuErrorMsg, "Failed to load ROM.");\r
331 return 0;\r
332 }\r
333\r
bf098bc5 334 Pico.m.frame_count = 0;\r
335\r
336 // insert CD if it was detected\r
337 if (cd_state > 0) {\r
338 ret = Insert_CD(romFileName, cd_state == 2);\r
339 if (ret != 0) {\r
340 sprintf(menuErrorMsg, "Insert_CD() failed, invalid CD image?");\r
341 printf("%s\n", menuErrorMsg);\r
342 return 0;\r
343 }\r
344 }\r
cc68a136 345\r
346 // emu_ReadConfig() might have messed currentConfig.lastRomFile\r
347 strncpy(currentConfig.lastRomFile, romFileName, sizeof(currentConfig.lastRomFile)-1);\r
348 currentConfig.lastRomFile[sizeof(currentConfig.lastRomFile)-1] = 0;\r
349\r
350 // additional movie stuff\r
351 if(movie_data) {\r
352 if(movie_data[0x14] == '6')\r
353 PicoOpt |= 0x20; // 6 button pad\r
354 else PicoOpt &= ~0x20;\r
4f672280 355 PicoOpt |= 0x40; // accurate timing\r
cc68a136 356 if(movie_data[0xF] >= 'A') {\r
4f672280 357 if(movie_data[0x16] & 0x80) {\r
358 PicoRegionOverride = 8;\r
359 } else {\r
360 PicoRegionOverride = 4;\r
361 }\r
362 PicoReset(0);\r
cc68a136 363 // TODO: bits 6 & 5\r
364 }\r
4f672280 365 movie_data[0x18+30] = 0;\r
366 sprintf(noticeMsg, "MOVIE: %s", (char *) &movie_data[0x18]);\r
cc68a136 367 }\r
368 else\r
369 {\r
370 if(Pico.m.pal) {\r
371 strcpy(noticeMsg, "PAL SYSTEM / 50 FPS");\r
372 } else {\r
373 strcpy(noticeMsg, "NTSC SYSTEM / 60 FPS");\r
374 }\r
375 }\r
376 gettimeofday(&noticeMsgTime, 0);\r
b837b69b 377printf("PicoMCD: %x\n", PicoMCD);\r
cc68a136 378 // load SRAM for this ROM\r
379 if(currentConfig.EmuOpt & 1)\r
380 emu_SaveLoadGame(1, 1);\r
381\r
cc68a136 382 return 1;\r
383}\r
384\r
385\r
386void emu_Init(void)\r
387{\r
388 // make temp buffer for alt renderer\r
389 framebuff = malloc((8+320)*(8+240+8));\r
390 if (!framebuff)\r
391 {\r
392 printf("framebuff == 0\n");\r
393 }\r
394\r
395 PicoInit();\r
396\r
397// logf = fopen("log.txt", "w");\r
398}\r
399\r
400\r
401static void romfname_ext(char *dst, char *ext)\r
402{\r
403 char *p;\r
404\r
405 // make save filename\r
406 for (p = romFileName+strlen(romFileName)-1; p >= romFileName && *p != '/'; p--); p++;\r
407 strncpy(dst, p, 511);\r
408 dst[511-8] = 0;\r
409 if(dst[strlen(dst)-4] == '.') dst[strlen(dst)-4] = 0;\r
410 strcat(dst, ext);\r
411}\r
412\r
413\r
414static void find_combos(void)\r
415{\r
416 int act, u;\r
417\r
418 // find out which keys and actions are combos\r
419 combo_keys = combo_acts = 0;\r
420 for (act = 0; act < 32; act++)\r
421 {\r
422 int keyc = 0;\r
423 if (act == 16) continue; // player2 flag\r
424 for (u = 0; u < 32; u++)\r
425 {\r
426 if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;\r
427 }\r
428 if (keyc > 1)\r
429 {\r
430 // loop again and mark those keys and actions as combo\r
431 for (u = 0; u < 32; u++)\r
432 {\r
433 if (currentConfig.KeyBinds[u] & (1 << act)) {\r
434 combo_keys |= 1 << u;\r
435 combo_acts |= 1 << act;\r
436 }\r
437 }\r
438 }\r
439 }\r
440 // printf("combo keys/acts: %08x %08x\n", combo_keys, combo_acts);\r
441}\r
442\r
443\r
444int emu_ReadConfig(int game)\r
445{\r
446 FILE *f;\r
447 char cfg[512];\r
448 int bread = 0;\r
449\r
450 if (!game)\r
451 {\r
452 // set default config\r
453 memset(&currentConfig, 0, sizeof(currentConfig));\r
454 currentConfig.lastRomFile[0] = 0;\r
cb0316e4 455 currentConfig.EmuOpt = 0x1f | 0xc00; // | cd_leds | cd_cdda\r
456 currentConfig.PicoOpt = 0x0f | 0x200; // | use_940\r
457 currentConfig.PsndRate = 44100;\r
cc68a136 458 currentConfig.PicoRegion = 0; // auto\r
51a902ae 459 currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP\r
cc68a136 460 currentConfig.Frameskip = -1; // auto\r
461 currentConfig.CPUclock = 200;\r
462 currentConfig.volume = 50;\r
463 currentConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU\r
464 currentConfig.KeyBinds[ 4] = 1<<1;\r
465 currentConfig.KeyBinds[ 2] = 1<<2;\r
466 currentConfig.KeyBinds[ 6] = 1<<3;\r
467 currentConfig.KeyBinds[14] = 1<<4;\r
468 currentConfig.KeyBinds[13] = 1<<5;\r
469 currentConfig.KeyBinds[12] = 1<<6;\r
470 currentConfig.KeyBinds[ 8] = 1<<7;\r
471 currentConfig.KeyBinds[15] = 1<<26; // switch rend\r
472 currentConfig.KeyBinds[10] = 1<<27; // save state\r
473 currentConfig.KeyBinds[11] = 1<<28; // load state\r
474 currentConfig.KeyBinds[23] = 1<<29; // vol up\r
475 currentConfig.KeyBinds[22] = 1<<30; // vol down\r
476 currentConfig.gamma = 100;\r
477 strncpy(cfg, PicoConfigFile, 511);\r
478 cfg[511] = 0;\r
479 } else {\r
480 romfname_ext(cfg, ".pbcfg");\r
481 }\r
482\r
483 printf("emu_ReadConfig: %s ", cfg);\r
484 f = fopen(cfg, "rb");\r
485 if (f) {\r
486 bread = fread(&currentConfig, 1, sizeof(currentConfig), f);\r
487 fclose(f);\r
488 }\r
489 printf((bread == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n");\r
490\r
491 PicoOpt = currentConfig.PicoOpt;\r
492 PsndRate = currentConfig.PsndRate;\r
493 PicoRegionOverride = currentConfig.PicoRegion;\r
51a902ae 494 PicoAutoRgnOrder = currentConfig.PicoAutoRgnOrder;\r
cc68a136 495 if (PicoOpt & 0x20) {\r
496 actionNames[ 8] = "Z"; actionNames[ 9] = "Y";\r
497 actionNames[10] = "X"; actionNames[11] = "MODE";\r
498 }\r
499 // some sanity checks\r
500 if (currentConfig.CPUclock < 1 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;\r
501 if (currentConfig.gamma < 10 || currentConfig.gamma > 300) currentConfig.gamma = 100;\r
502 // if volume keys are unbound, bind them to volume control\r
503 if (!currentConfig.KeyBinds[23] && !currentConfig.KeyBinds[22]) {\r
504 currentConfig.KeyBinds[23] = 1<<29; // vol up\r
505 currentConfig.KeyBinds[22] = 1<<30; // vol down\r
506 }\r
507\r
508 return (bread == sizeof(currentConfig));\r
509}\r
510\r
511\r
512int emu_WriteConfig(int game)\r
513{\r
514 FILE *f;\r
515 char cfg[512];\r
516 int bwrite = 0;\r
517\r
518 if (!game)\r
519 {\r
520 strncpy(cfg, PicoConfigFile, 511);\r
521 cfg[511] = 0;\r
522 } else {\r
523 romfname_ext(cfg, ".pbcfg");\r
524 }\r
525\r
526 printf("emu_WriteConfig: %s ", cfg);\r
527 f = fopen(cfg, "wb");\r
528 if (f) {\r
529 currentConfig.PicoOpt = PicoOpt;\r
530 currentConfig.PsndRate = PsndRate;\r
531 currentConfig.PicoRegion = PicoRegionOverride;\r
51a902ae 532 currentConfig.PicoAutoRgnOrder = PicoAutoRgnOrder;\r
cc68a136 533 bwrite = fwrite(&currentConfig, 1, sizeof(currentConfig), f);\r
534 fflush(f);\r
535 fclose(f);\r
536 sync();\r
537 }\r
538 printf((bwrite == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n");\r
539\r
540 return (bwrite == sizeof(currentConfig));\r
541}\r
542\r
543\r
544void emu_Deinit(void)\r
545{\r
546 // save SRAM\r
547 if((currentConfig.EmuOpt & 1) && SRam.changed) {\r
548 emu_SaveLoadGame(0, 1);\r
549 SRam.changed = 0;\r
550 }\r
551\r
552 if (!(currentConfig.EmuOpt & 0x20))\r
553 emu_WriteConfig(0);\r
554 free(framebuff);\r
555\r
556 PicoExit();\r
557// fclose(logf);\r
558\r
559 // restore gamma\r
560 if (gp2x_old_gamma != 100)\r
561 set_gamma(100);\r
562}\r
563\r
564\r
565void osd_text(int x, int y, char *text)\r
566{\r
567 int len = strlen(text)*8;\r
568\r
569 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
bf098bc5 570 int *p, i, h;\r
cc68a136 571 x &= ~3; // align x\r
572 len = (len+3) >> 2;\r
573 for (h = 0; h < 8; h++) {\r
574 p = (int *) ((unsigned char *) gp2x_screen+x+320*(y+h));\r
bf098bc5 575 for (i = len; i; i--, p++) *p = 0xe0e0e0e0;\r
cc68a136 576 }\r
bf098bc5 577 gp2x_text_out8_2(x, y, text, 0xf0);\r
cc68a136 578 } else {\r
579 int *p, i, h;\r
580 x &= ~1; // align x\r
581 len = (len+1) >> 1;\r
582 for (h = 0; h < 8; h++) {\r
583 p = (int *) ((unsigned short *) gp2x_screen+x+320*(y+h));\r
584 for (i = len; i; i--, p++) *p = (*p>>2)&0x39e7;\r
585 }\r
586 gp2x_text_out15(x, y, text);\r
587 }\r
588}\r
589\r
bf098bc5 590static void cd_leds(void)\r
591{\r
b837b69b 592 // mmu problems?\r
593// static\r
594 int old_reg;\r
595// if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change\r
bf098bc5 596 old_reg = Pico_mcd->s68k_regs[0];\r
597\r
598 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
599 // 8-bit modes\r
600 unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;\r
601 unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;\r
602 *(unsigned int *)((char *)gp2x_screen + 320*2+306) =\r
603 *(unsigned int *)((char *)gp2x_screen + 320*3+306) =\r
604 *(unsigned int *)((char *)gp2x_screen + 320*4+306) = col_g;\r
605 *(unsigned int *)((char *)gp2x_screen + 320*2+312) =\r
606 *(unsigned int *)((char *)gp2x_screen + 320*3+312) =\r
607 *(unsigned int *)((char *)gp2x_screen + 320*4+312) = col_r;\r
608 } else {\r
609 // 16-bit modes\r
610 unsigned int *p = (unsigned int *)((short *)gp2x_screen + 320*2+306);\r
611 unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;\r
612 unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;\r
613 *p++ = col_g; *p++ = col_g; p++; *p++ = col_r; *p++ = col_r; p += 320/2 - 10/2;\r
614 *p++ = col_g; *p++ = col_g; p++; *p++ = col_r; *p++ = col_r; p += 320/2 - 10/2;\r
615 *p++ = col_g; *p++ = col_g; p++; *p++ = col_r; *p++ = col_r; p += 320/2 - 10/2;\r
616 }\r
617}\r
618\r
cc68a136 619static int EmuScan16(unsigned int num, void *sdata)\r
620{\r
621 if (!(Pico.video.reg[1]&8)) num += 8;\r
622 DrawLineDest = (unsigned short *) gp2x_screen + 320*(num+1);\r
623\r
624 return 0;\r
625}\r
626\r
627static int EmuScan8(unsigned int num, void *sdata)\r
628{\r
629 if (!(Pico.video.reg[1]&8)) num += 8;\r
630 DrawLineDest = (unsigned char *) gp2x_screen + 320*(num+1);\r
631\r
632 return 0;\r
633}\r
634\r
635static int localPal[0x100];\r
636static void (*vidCpyM2)(void *dest, void *src) = NULL;\r
637\r
638static void blit(char *fps, char *notice)\r
639{\r
bf098bc5 640 int emu_opt = currentConfig.EmuOpt;\r
641\r
cc68a136 642 if (PicoOpt&0x10) {\r
643 // 8bit fast renderer\r
644 if (Pico.m.dirtyPal) {\r
645 Pico.m.dirtyPal = 0;\r
646 vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
647 // feed new palette to our device\r
648 gp2x_video_setpalette(localPal, 0x40);\r
649 }\r
650 vidCpyM2((unsigned char *)gp2x_screen+320*8, framebuff+328*8);\r
bf098bc5 651 } else if (!(emu_opt&0x80)) {\r
cc68a136 652 // 8bit accurate renderer\r
653 if (Pico.m.dirtyPal) {\r
654 Pico.m.dirtyPal = 0;\r
655 if(Pico.video.reg[0xC]&8) { // shadow/hilight mode\r
656 vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
657 vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);\r
658 vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);\r
659 blockcpy(localPal+0xc0, localPal+0x40, 0x40*4);\r
bf098bc5 660 localPal[0xc0] = 0x0000c000;\r
661 localPal[0xd0] = 0x00c00000;\r
cc68a136 662 localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
663 localPal[0xf0] = 0x00ffffff;\r
664 gp2x_video_setpalette(localPal, 0x100);\r
665 } else if (rendstatus & 0x20) { // mid-frame palette changes\r
666 vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
667 vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);\r
668 vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);\r
669 gp2x_video_setpalette(localPal, 0xc0);\r
670 } else {\r
671 vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
672 gp2x_video_setpalette(localPal, 0x40);\r
673 }\r
674 }\r
675 }\r
676\r
677 if (notice) osd_text(4, 232, notice);\r
bf098bc5 678 if (emu_opt & 2)\r
cc68a136 679 osd_text(osd_fps_x, 232, fps);\r
bf098bc5 680 if ((emu_opt & 0x400) && (PicoMCD & 1))\r
681 cd_leds();\r
cc68a136 682\r
683 //gp2x_video_wait_vsync();\r
684 gp2x_video_flip();\r
685\r
686 if (!(PicoOpt&0x10)) {\r
687 if (!(Pico.video.reg[1]&8)) {\r
688 if (currentConfig.EmuOpt&0x80) {\r
689 DrawLineDest = (unsigned short *) gp2x_screen + 320*8;\r
690 } else {\r
691 DrawLineDest = (unsigned char *) gp2x_screen + 320*8;\r
692 }\r
693 } else {\r
694 DrawLineDest = gp2x_screen;\r
695 }\r
696 }\r
697}\r
698\r
699\r
700// clears whole screen or just the notice area (in all buffers)\r
701static void clearArea(int full)\r
702{\r
bf098bc5 703 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
704 // 8-bit renderers\r
705 if (full) gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
706 else gp2x_memset_all_buffers(320*232, 0xe0, 320*8);\r
707 } else {\r
cc68a136 708 // 16bit accurate renderer\r
709 if (full) gp2x_memset_all_buffers(0, 0, 320*240*2);\r
710 else gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);\r
cc68a136 711 }\r
712}\r
713\r
714\r
715static void vidResetMode(void)\r
716{\r
717 if (PicoOpt&0x10) {\r
cc68a136 718 gp2x_video_changemode(8);\r
cc68a136 719 } else if (currentConfig.EmuOpt&0x80) {\r
720 gp2x_video_changemode(15);\r
721 PicoDrawSetColorFormat(1);\r
722 PicoScan = EmuScan16;\r
723 PicoScan(0, 0);\r
724 } else {\r
bf098bc5 725 gp2x_video_changemode(8);\r
726 PicoDrawSetColorFormat(2);\r
727 PicoScan = EmuScan8;\r
728 PicoScan(0, 0);\r
729 }\r
730 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
731 // setup pal for 8-bit modes\r
732 localPal[0xc0] = 0x0000c000; // MCD LEDs\r
733 localPal[0xd0] = 0x00c00000;\r
cc68a136 734 localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
735 localPal[0xf0] = 0x00ffffff;\r
cc68a136 736 gp2x_video_setpalette(localPal, 0x100);\r
737 gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
738 gp2x_video_flip();\r
cc68a136 739 }\r
740 Pico.m.dirtyPal = 1;\r
741 // reset scaling\r
742 gp2x_video_RGB_setscaling((PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);\r
743}\r
744\r
745\r
746static int check_save_file(void)\r
747{\r
748 char saveFname[512];\r
749 char ext[16];\r
750 FILE *f;\r
751\r
752 ext[0] = 0;\r
753 if(state_slot > 0 && state_slot < 10) sprintf(ext, ".%i", state_slot);\r
754 strcat(ext, ".mds");\r
755 if(currentConfig.EmuOpt & 8) strcat(ext, ".gz");\r
756\r
757 romfname_ext(saveFname, ext);\r
758 if ((f = fopen(saveFname, "rb"))) {\r
759 fclose(f);\r
760 return 1;\r
761 }\r
762 return 0;\r
763}\r
764\r
765static void RunEvents(unsigned int which)\r
766{\r
767 if(which & 0x1800) { // save or load (but not both)\r
768 int do_it = 1;\r
769 if (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200) && check_save_file()) {\r
770 unsigned long keys;\r
771 blit("", "OVERWRITE SAVE? (Y=yes, X=no)");\r
772 while( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )\r
773 usleep(50*1024);\r
774 if (keys & GP2X_X) do_it = 0;\r
775 clearArea(0);\r
776 }\r
777 if (do_it) {\r
778 blit("", (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");\r
779 emu_SaveLoadGame(which & 0x1000, 0);\r
780 }\r
781\r
782 reset_timing = 1;\r
783 }\r
784 if(which & 0x0400) { // switch renderer\r
785 if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }\r
786 else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;\r
787 else currentConfig.EmuOpt &= ~0x80;\r
788\r
789 vidResetMode();\r
790\r
791 if (PicoOpt&0x10) {\r
792 strcpy(noticeMsg, " 8bit fast renderer");\r
793 } else if (currentConfig.EmuOpt&0x80) {\r
794 strcpy(noticeMsg, "16bit accurate renderer");\r
795 } else {\r
796 strcpy(noticeMsg, " 8bit accurate renderer");\r
797 }\r
798\r
799 gettimeofday(&noticeMsgTime, 0);\r
800 }\r
801 if(which & 0x0300) {\r
802 if(which&0x0200) {\r
803 state_slot -= 1;\r
804 if(state_slot < 0) state_slot = 9;\r
805 } else {\r
806 state_slot += 1;\r
807 if(state_slot > 9) state_slot = 0;\r
808 }\r
809 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, check_save_file() ? "USED" : "FREE");\r
810 gettimeofday(&noticeMsgTime, 0);\r
811 }\r
812 if(which & 0x0080) {\r
813 engineState = PGS_Menu;\r
814 }\r
815}\r
816\r
817\r
8c1952f0 818static void updateMovie(void)\r
819{\r
820 int offs = Pico.m.frame_count*3 + 0x40;\r
821 if (offs+3 > movie_size) {\r
822 free(movie_data);\r
823 movie_data = 0;\r
824 strcpy(noticeMsg, "END OF MOVIE.");\r
825 printf("END OF MOVIE.\n");\r
826 gettimeofday(&noticeMsgTime, 0);\r
827 } else {\r
828 // MXYZ SACB RLDU\r
829 PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU\r
830 if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // A\r
831 if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // B\r
832 if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // A\r
833 PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU\r
834 if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // A\r
835 if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // B\r
836 if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // A\r
837 PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX\r
838 if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X\r
839 if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z\r
840 PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX\r
841 if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X\r
842 if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z\r
843 }\r
844}\r
845\r
846\r
cc68a136 847static void updateKeys(void)\r
848{\r
849 unsigned long keys, allActions[2] = { 0, 0 }, events;\r
850 static unsigned long prevEvents = 0;\r
851 int joy, i;\r
852\r
853 keys = gp2x_joystick_read(0);\r
854 if (keys & GP2X_SELECT) {\r
855 engineState = select_exits ? PGS_Quit : PGS_Menu;\r
856 // wait until select is released, so menu would not resume game\r
857 while (gp2x_joystick_read(1) & GP2X_SELECT) usleep(50*1000);\r
858 }\r
859\r
860 keys &= CONFIGURABLE_KEYS;\r
861\r
862 for (i = 0; i < 32; i++)\r
863 {\r
864 if (keys & (1 << i)) {\r
865 int pl, acts = currentConfig.KeyBinds[i];\r
866 if (!acts) continue;\r
867 pl = (acts >> 16) & 1;\r
868 if (combo_keys & (1 << i)) {\r
869 int u = i+1, acts_c = acts & combo_acts;\r
870 // let's try to find the other one\r
871 if (acts_c)\r
872 for (; u < 32; u++)\r
873 if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) {\r
874 allActions[pl] |= acts_c;\r
875 keys &= ~((1 << i) | (1 << u));\r
876 break;\r
877 }\r
878 // add non-combo actions if combo ones were not found\r
879 if (!acts_c || u == 32)\r
880 allActions[pl] |= acts & ~combo_acts;\r
881 } else {\r
882 allActions[pl] |= acts;\r
883 }\r
884 }\r
885 }\r
886\r
887 // add joy inputs\r
888 if (num_of_joys > 0)\r
889 {\r
890 gp2x_usbjoy_update();\r
891 for (joy = 0; joy < num_of_joys; joy++) {\r
892 int keys = gp2x_usbjoy_check2(joy);\r
893 for (i = 0; i < 32; i++) {\r
894 if (keys & (1 << i)) {\r
895 int acts = currentConfig.JoyBinds[joy][i];\r
896 int pl = (acts >> 16) & 1;\r
897 allActions[pl] |= acts;\r
898 }\r
899 }\r
900 }\r
901 }\r
902\r
8c1952f0 903 PicoPad[0] = (unsigned short) allActions[0];\r
904 PicoPad[1] = (unsigned short) allActions[1];\r
cc68a136 905\r
906 events = (allActions[0] | allActions[1]) >> 16;\r
907\r
908 // volume is treated in special way and triggered every frame\r
909 if(events & 0x6000) {\r
910 int vol = currentConfig.volume;\r
911 if (events & 0x2000) {\r
912 if (vol < 90) vol++;\r
913 } else {\r
914 if (vol > 0) vol--;\r
915 }\r
916 gp2x_sound_volume(vol, vol);\r
917 sprintf(noticeMsg, "VOL: %02i", vol);\r
918 gettimeofday(&noticeMsgTime, 0);\r
919 currentConfig.volume = vol;\r
920 }\r
921\r
922 events &= ~prevEvents;\r
923 if (events) RunEvents(events);\r
8c1952f0 924 if (movie_data) updateMovie();\r
cc68a136 925\r
926 prevEvents = (allActions[0] | allActions[1]) >> 16;\r
927}\r
928\r
929static int snd_excess_add = 0, snd_excess_cnt = 0; // hack\r
930\r
931static void updateSound(void)\r
932{\r
933 int len = (PicoOpt&8)?PsndLen*2:PsndLen;\r
934\r
935 snd_excess_cnt += snd_excess_add;\r
936 if (snd_excess_cnt >= 0x10000) {\r
937 snd_excess_cnt -= 0x10000;\r
938 if (PicoOpt&8) {\r
939 PsndOut[len] = PsndOut[len-2];\r
940 PsndOut[len+1] = PsndOut[len-1];\r
941 len+=2;\r
942 } else {\r
943 PsndOut[len] = PsndOut[len-1];\r
944 len++;\r
945 }\r
946 }\r
947\r
948 gp2x_sound_write(PsndOut, len<<1);\r
949}\r
950\r
951\r
952static void SkipFrame(int do_sound)\r
953{\r
954 void *sndbuff_tmp = 0;\r
955 if (PsndOut && !do_sound) {\r
956 sndbuff_tmp = PsndOut;\r
957 PsndOut = 0;\r
958 }\r
959\r
960 PicoSkipFrame=1;\r
961 PicoFrame();\r
962 PicoSkipFrame=0;\r
963\r
964 if (sndbuff_tmp && !do_sound) {\r
965 PsndOut = sndbuff_tmp;\r
966 }\r
967}\r
968\r
969\r
970static void simpleWait(int thissec, int lim_time)\r
971{\r
972 struct timeval tval;\r
973\r
974 spend_cycles(1024);\r
975 gettimeofday(&tval, 0);\r
976 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
977\r
978 while(tval.tv_usec < lim_time)\r
979 {\r
980 spend_cycles(1024);\r
981 gettimeofday(&tval, 0);\r
982 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
983 }\r
984}\r
985\r
986\r
987void emu_Loop(void)\r
988{\r
989 static int gp2x_old_clock = 200;\r
990 static int PsndRate_old = 0, PicoOpt_old = 0, PsndLen_real = 0, pal_old = 0;\r
991 char fpsbuff[24]; // fps count c string\r
992 struct timeval tval; // timing\r
993 int thissec = 0, frames_done = 0, frames_shown = 0, oldmodes = 0;\r
994 int target_fps, target_frametime, lim_time, i;\r
995 char *notice = 0;\r
996\r
997 printf("entered emu_Loop()\n");\r
998\r
999 if (gp2x_old_clock != currentConfig.CPUclock) {\r
1000 printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);\r
1001 set_FCLK(currentConfig.CPUclock);\r
1002 gp2x_old_clock = currentConfig.CPUclock;\r
1003 printf(" done\n");\r
1004 }\r
1005\r
1006 if (gp2x_old_gamma != currentConfig.gamma) {\r
1007 set_gamma(currentConfig.gamma);\r
1008 gp2x_old_gamma = currentConfig.gamma;\r
1009 printf("updated gamma to %i\n", currentConfig.gamma);\r
1010 }\r
1011\r
1012 fpsbuff[0] = 0;\r
1013\r
1014 // make sure we are in correct mode\r
1015 vidResetMode();\r
1016 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;\r
1017 find_combos();\r
1018\r
1019 // pal/ntsc might have changed, reset related stuff\r
1020 target_fps = Pico.m.pal ? 50 : 60;\r
1021 target_frametime = 1000000/target_fps;\r
1022 reset_timing = 1;\r
1023\r
1024 // prepare sound stuff\r
1025 if(currentConfig.EmuOpt & 4) {\r
1026 if(PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old || crashed_940) {\r
1027 /* if 940 is turned off, we need it to be put back to sleep */\r
1028 if (!(PicoOpt&0x200) && ((PicoOpt^PicoOpt_old)&0x200)) {\r
b837b69b 1029 Reset940(1, 2);\r
cc68a136 1030 Pause940(1);\r
1031 }\r
1032 sound_rerate();\r
1033 }\r
1034 //excess_samples = PsndRate - PsndLen*target_fps;\r
1035 snd_excess_cnt = 0;\r
1036 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r
1037 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
1038 gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);\r
1039 gp2x_sound_volume(currentConfig.volume, currentConfig.volume);\r
1040 PicoWriteSound = updateSound;\r
1041 PsndOut = calloc((PicoOpt&8) ? (PsndLen*4+4) : (PsndLen*2+2), 1);\r
1042 PsndRate_old = PsndRate;\r
1043 PsndLen_real = PsndLen;\r
1044 PicoOpt_old = PicoOpt;\r
1045 pal_old = Pico.m.pal;\r
1046 } else {\r
1047 PsndOut = 0;\r
1048 }\r
1049\r
1050 // loop?\r
1051 while (engineState == PGS_Running)\r
1052 {\r
1053 int modes;\r
1054\r
1055 gettimeofday(&tval, 0);\r
1056 if(reset_timing) {\r
1057 reset_timing = 0;\r
1058 thissec = tval.tv_sec;\r
1059 frames_shown = frames_done = tval.tv_usec/target_frametime;\r
1060 }\r
1061\r
1062 // show notice message?\r
1063 if(noticeMsgTime.tv_sec) {\r
1064 static int noticeMsgSum;\r
1065 if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec\r
1066 noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;\r
1067 clearArea(0);\r
1068 notice = 0;\r
1069 } else {\r
1070 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];\r
1071 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }\r
1072 notice = noticeMsg;\r
1073 }\r
1074 }\r
1075\r
1076 // check for mode changes\r
1077 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);\r
1078 if (modes != oldmodes) {\r
1079 int scalex = 320;\r
1080 osd_fps_x = OSD_FPS_X;\r
1081 if (modes & 4) {\r
1082 vidCpyM2 = vidCpyM2_40col;\r
1083 } else {\r
1084 if (PicoOpt & 0x100) {\r
1085 vidCpyM2 = vidCpyM2_32col_nobord;\r
1086 scalex = 256;\r
1087 osd_fps_x = OSD_FPS_X - 64;\r
1088 } else {\r
1089 vidCpyM2 = vidCpyM2_32col;\r
1090 }\r
1091 }\r
1092 gp2x_video_RGB_setscaling(scalex, 240);\r
1093 oldmodes = modes;\r
1094 clearArea(1);\r
1095 }\r
1096\r
1097 // second changed?\r
1098 if(thissec != tval.tv_sec) {\r
1099#ifdef BENCHMARK\r
1100 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];\r
1101 if(++bench == 10) {\r
1102 bench = 0;\r
1103 bench_fps_s = bench_fps;\r
1104 bf[bfp++ & 3] = bench_fps;\r
1105 bench_fps = 0;\r
1106 }\r
1107 bench_fps += frames_shown;\r
1108 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);\r
1109#else\r
1110 if(currentConfig.EmuOpt & 2)\r
1111 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);\r
1112#endif\r
1113 thissec = tval.tv_sec;\r
1114\r
1115 if(PsndOut == 0 && currentConfig.Frameskip >= 0) {\r
1116 frames_done = frames_shown = 0;\r
1117 } else {\r
1118 // it is quite common for this implementation to leave 1 fame unfinished\r
1119 // when second changes, but we don't want buffer to starve.\r
1120 if(PsndOut && frames_done < target_fps && frames_done > target_fps-5) {\r
1121 updateKeys();\r
1122 SkipFrame(1); frames_done++;\r
1123 }\r
1124\r
1125 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;\r
1126 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;\r
1127 if (frames_shown > frames_done) frames_shown = frames_done;\r
1128 }\r
1129 }\r
1130\r
1131 lim_time = (frames_done+1) * target_frametime;\r
1132 if(currentConfig.Frameskip >= 0) { // frameskip enabled\r
1133 for(i = 0; i < currentConfig.Frameskip; i++) {\r
1134 updateKeys();\r
1135 SkipFrame(1); frames_done++;\r
1136 if (PsndOut) { // do framelimitting if sound is enabled\r
1137 gettimeofday(&tval, 0);\r
1138 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
1139 if(tval.tv_usec < lim_time) { // we are too fast\r
1140 simpleWait(thissec, lim_time);\r
1141 }\r
1142 }\r
1143 lim_time += target_frametime;\r
1144 }\r
1145 } else if(tval.tv_usec > lim_time) { // auto frameskip\r
1146 // no time left for this frame - skip\r
1147 updateKeys();\r
1148 SkipFrame(tval.tv_usec < lim_time+target_frametime); frames_done++;\r
1149 continue;\r
1150 }\r
1151\r
1152 updateKeys();\r
1153 PicoFrame();\r
1154\r
312e9ce1 1155#if 0\r
1156if (Pico.m.frame_count == 31563) {\r
1157 FILE *f;\r
1158 f = fopen("ram_p.bin", "wb");\r
1159 if (!f) { printf("!f\n"); exit(1); }\r
1160 fwrite(Pico.ram, 1, 0x10000, f);\r
1161 fclose(f);\r
1162 exit(0);\r
1163}\r
1164#endif\r
4f672280 1165#if 0\r
1166 // debug\r
1167 {\r
312e9ce1 1168 #define BYTE unsigned char\r
1169 #define WORD unsigned short\r
1170 struct\r
1171 {\r
1172 BYTE IDLength; /* 00h Size of Image ID field */\r
1173 BYTE ColorMapType; /* 01h Color map type */\r
1174 BYTE ImageType; /* 02h Image type code */\r
1175 WORD CMapStart; /* 03h Color map origin */\r
1176 WORD CMapLength; /* 05h Color map length */\r
1177 BYTE CMapDepth; /* 07h Depth of color map entries */\r
1178 WORD XOffset; /* 08h X origin of image */\r
1179 WORD YOffset; /* 0Ah Y origin of image */\r
1180 WORD Width; /* 0Ch Width of image */\r
1181 WORD Height; /* 0Eh Height of image */\r
1182 BYTE PixelDepth; /* 10h Image pixel size */\r
1183 BYTE ImageDescriptor; /* 11h Image descriptor byte */\r
1184 } __attribute__((packed)) TGAHEAD;\r
1185 static unsigned short oldscr[320*240];\r
4f672280 1186 FILE *f; char name[128]; int i;\r
312e9ce1 1187\r
1188 memset(&TGAHEAD, 0, sizeof(TGAHEAD));\r
1189 TGAHEAD.ImageType = 2;\r
1190 TGAHEAD.Width = 320;\r
1191 TGAHEAD.Height = 240;\r
1192 TGAHEAD.PixelDepth = 16;\r
1193 TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left\r
1194\r
1195 #define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?\r
1196\r
1197 for (i = 0; i < 320*240; i++)\r
1198 if(oldscr[i] != CONV(((unsigned short *)gp2x_screen)[i])) break;\r
1199 if (i < 320*240)\r
4f672280 1200 {\r
312e9ce1 1201 for (i = 0; i < 320*240; i++)\r
1202 oldscr[i] = CONV(((unsigned short *)gp2x_screen)[i]);\r
1203 sprintf(name, "%05i.tga", Pico.m.frame_count);\r
4f672280 1204 f = fopen(name, "wb");\r
1205 if (!f) { printf("!f\n"); exit(1); }\r
312e9ce1 1206 fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);\r
1207 fwrite(oldscr, 1, 320*240*2, f);\r
4f672280 1208 fclose(f);\r
1209 }\r
1210 }\r
1211#endif\r
1212\r
cc68a136 1213 // check time\r
1214 gettimeofday(&tval, 0);\r
1215 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
1216\r
1217 // sleep if we are still too fast\r
1218 if(PsndOut != 0 || currentConfig.Frameskip < 0)\r
1219 {\r
1220 // usleep sleeps for ~20ms minimum, so it is not a solution here\r
1221 gettimeofday(&tval, 0);\r
1222 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
1223 if(tval.tv_usec < lim_time)\r
1224 {\r
1225 // we are too fast\r
1226 simpleWait(thissec, lim_time);\r
1227 }\r
1228 }\r
1229\r
1230 blit(fpsbuff, notice);\r
1231\r
1232 frames_done++; frames_shown++;\r
1233 }\r
1234\r
1235 // save SRAM\r
1236 if((currentConfig.EmuOpt & 1) && SRam.changed) {\r
51a902ae 1237 blit("", "Writing SRAM/BRAM..");\r
cc68a136 1238 emu_SaveLoadGame(0, 1);\r
1239 SRam.changed = 0;\r
1240 }\r
1241\r
1242 if (PsndOut != 0) {\r
1243 free(PsndOut);\r
1244 PsndOut = 0;\r
1245 }\r
1246}\r
1247\r
1248\r
1249void emu_ResetGame(void)\r
1250{\r
1251 PicoReset(0);\r
1252 reset_timing = 1;\r
1253}\r
1254\r
1255\r
1256size_t gzRead2(void *p, size_t _size, size_t _n, void *file)\r
1257{\r
1258 return gzread(file, p, _n);\r
1259}\r
1260\r
1261\r
1262size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)\r
1263{\r
1264 return gzwrite(file, p, _n);\r
1265}\r
1266\r
1267typedef unsigned int (*STATE_SL_FUNC)(void *, unsigned int, unsigned int, void *);\r
1268\r
1269int emu_SaveLoadGame(int load, int sram)\r
1270{\r
1271 int ret = 0;\r
1272 char saveFname[512];\r
1273\r
1274 // make save filename\r
1275 romfname_ext(saveFname, "");\r
ab0607f7 1276 if(sram) strcat(saveFname, (PicoMCD&1) ? ".brm" : ".srm");\r
cc68a136 1277 else {\r
1278 if(state_slot > 0 && state_slot < 10) sprintf(saveFname, "%s.%i", saveFname, state_slot);\r
1279 strcat(saveFname, ".mds");\r
1280 }\r
1281\r
1282 printf("saveLoad (%i, %i): %s\n", load, sram, saveFname);\r
1283\r
1284 if(sram) {\r
1285 FILE *sramFile;\r
ab0607f7 1286 int sram_size;\r
1287 unsigned char *sram_data;\r
1288 if (PicoMCD&1) {\r
1289 sram_size = 0x2000;\r
1290 sram_data = Pico_mcd->bram;\r
1291 } else {\r
1292 sram_size = SRam.end-SRam.start+1;\r
1293 if(SRam.reg_back & 4) sram_size=0x2000;\r
1294 sram_data = SRam.data;\r
1295 }\r
1296 if(!sram_data) return 0; // SRam forcefully disabled for this game\r
1297\r
cc68a136 1298 if(load) {\r
1299 sramFile = fopen(saveFname, "rb");\r
1300 if(!sramFile) return -1;\r
ab0607f7 1301 fread(sram_data, 1, sram_size, sramFile);\r
cc68a136 1302 fclose(sramFile);\r
1303 } else {\r
1304 // sram save needs some special processing\r
1305 // see if we have anything to save\r
1306 for(; sram_size > 0; sram_size--)\r
ab0607f7 1307 if(sram_data[sram_size-1]) break;\r
cc68a136 1308\r
1309 if(sram_size) {\r
1310 sramFile = fopen(saveFname, "wb");\r
ab0607f7 1311 ret = fwrite(sram_data, 1, sram_size, sramFile);\r
cc68a136 1312 ret = (ret != sram_size) ? -1 : 0;\r
1313 fclose(sramFile);\r
1314 sync();\r
1315 }\r
1316 }\r
1317 return ret;\r
ab0607f7 1318 }\r
1319 else\r
1320 {\r
cc68a136 1321 void *PmovFile = NULL;\r
1322 // try gzip first\r
1323 if(currentConfig.EmuOpt & 8) {\r
1324 strcat(saveFname, ".gz");\r
1325 if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) {\r
1326 areaRead = gzRead2;\r
1327 areaWrite = gzWrite2;\r
51a902ae 1328 areaEof = (areaeof *) gzeof;\r
cc68a136 1329 if(!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);\r
1330 } else\r
1331 saveFname[strlen(saveFname)-3] = 0;\r
1332 }\r
1333 if(!PmovFile) { // gzip failed or was disabled\r
1334 if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) {\r
51a902ae 1335 areaRead = (arearw *) fread;\r
1336 areaWrite = (arearw *) fwrite;\r
1337 areaEof = (areaeof *) feof;\r
cc68a136 1338 }\r
1339 }\r
1340 if(PmovFile) {\r
1341 PmovState(load ? 6 : 5, PmovFile);\r
1342 strcpy(noticeMsg, load ? "GAME LOADED " : "GAME SAVED ");\r
1343 if(areaRead == gzRead2)\r
1344 gzclose(PmovFile);\r
1345 else fclose ((FILE *) PmovFile);\r
1346 PmovFile = 0;\r
1347 if (!load) sync();\r
1348 else Pico.m.dirtyPal=1;\r
1349 } else {\r
1350 strcpy(noticeMsg, load ? "LOAD FAILED " : "SAVE FAILED ");\r
1351 ret = -1;\r
1352 }\r
1353\r
1354 gettimeofday(&noticeMsgTime, 0);\r
1355 return ret;\r
1356 }\r
1357}\r