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