menu bg, pc linux build
[picodrive.git] / platform / gp2x / emu.c
... / ...
CommitLineData
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 short sndBuffer[2*44100/50];\r
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
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
83static int try_rfn_cut(void)\r
84{\r
85 FILE *tmp;\r
86 char *p;\r
87\r
88 p = romFileName + strlen(romFileName) - 1;\r
89 for (; p > romFileName; p--)\r
90 if (*p == '.') break;\r
91 *p = 0;\r
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
100static void get_ext(char *file, char *ext)\r
101{\r
102 char *p;\r
103\r
104 p = file + strlen(file) - 4;\r
105 if (p < file) p = file;\r
106 strncpy(ext, p, 4);\r
107 ext[4] = 0;\r
108 strlwr(ext);\r
109}\r
110\r
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
191 fseek(cd_f, (type == 1) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);\r
192 fread(buf, 1, 1, cd_f);\r
193 fclose(cd_f);\r
194\r
195 if (buf[0] == 0x64) region = 8; // EU\r
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
212int emu_ReloadRom(void)\r
213{\r
214 unsigned int rom_size = 0;\r
215 char *used_rom_name = romFileName;\r
216 char ext[5];\r
217 FILE *rom;\r
218 int ret, cd_state;\r
219\r
220 printf("emu_ReloadRom(%s)\n", romFileName);\r
221\r
222 get_ext(romFileName, ext);\r
223\r
224 // detect wrong extensions\r
225 if(!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz\r
226 sprintf(menuErrorMsg, "Not a ROM selected.");\r
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
263 dummy = try_rfn_cut() || try_rfn_cut();\r
264 if (!dummy) {\r
265 sprintf(menuErrorMsg, "Could't find a ROM for movie.");\r
266 return 0;\r
267 }\r
268 get_ext(romFileName, ext);\r
269 }\r
270\r
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
280 if (PicoMCD & 1) PicoExitMCD();\r
281 PicoMCD &= ~1;\r
282 }\r
283\r
284 rom = fopen(used_rom_name, "rb");\r
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
299 ret = CartLoadZip(used_rom_name, &rom_data, &rom_size);\r
300 if(ret) {\r
301 if (ret == 4) strcpy(menuErrorMsg, "No ROMs found in zip.");\r
302 else sprintf(menuErrorMsg, "Unzip failed with code %i", ret);\r
303 printf("%s\n", menuErrorMsg);\r
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
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
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
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
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
357 PicoOpt |= 0x40; // accurate timing\r
358 if(movie_data[0xF] >= 'A') {\r
359 if(movie_data[0x16] & 0x80) {\r
360 PicoRegionOverride = 8;\r
361 } else {\r
362 PicoRegionOverride = 4;\r
363 }\r
364 PicoReset(0);\r
365 // TODO: bits 6 & 5\r
366 }\r
367 movie_data[0x18+30] = 0;\r
368 sprintf(noticeMsg, "MOVIE: %s", (char *) &movie_data[0x18]);\r
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
379\r
380 // load SRAM for this ROM\r
381 if(currentConfig.EmuOpt & 1)\r
382 emu_SaveLoadGame(1, 1);\r
383\r
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
457 currentConfig.EmuOpt = 0x1f | 0x400; // | cd_leds\r
458 currentConfig.PicoOpt = 0x0f | 0xe00; // | use_940 | cd_pcm | cd_cdda\r
459 currentConfig.PsndRate = 22050; // 44100;\r
460 currentConfig.PicoRegion = 0; // auto\r
461 currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP\r
462 currentConfig.Frameskip = -1; // auto\r
463 currentConfig.CPUclock = 200;\r
464 currentConfig.volume = 50;\r
465 currentConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU\r
466 currentConfig.KeyBinds[ 4] = 1<<1;\r
467 currentConfig.KeyBinds[ 2] = 1<<2;\r
468 currentConfig.KeyBinds[ 6] = 1<<3;\r
469 currentConfig.KeyBinds[14] = 1<<4;\r
470 currentConfig.KeyBinds[13] = 1<<5;\r
471 currentConfig.KeyBinds[12] = 1<<6;\r
472 currentConfig.KeyBinds[ 8] = 1<<7;\r
473 currentConfig.KeyBinds[15] = 1<<26; // switch rend\r
474 currentConfig.KeyBinds[10] = 1<<27; // save state\r
475 currentConfig.KeyBinds[11] = 1<<28; // load state\r
476 currentConfig.KeyBinds[23] = 1<<29; // vol up\r
477 currentConfig.KeyBinds[22] = 1<<30; // vol down\r
478 currentConfig.gamma = 100;\r
479 strncpy(cfg, PicoConfigFile, 511);\r
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
496 PicoAutoRgnOrder = currentConfig.PicoAutoRgnOrder;\r
497 if (PicoOpt & 0x20) {\r
498 actionNames[ 8] = "Z"; actionNames[ 9] = "Y";\r
499 actionNames[10] = "X"; actionNames[11] = "MODE";\r
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
534 currentConfig.PicoAutoRgnOrder = PicoAutoRgnOrder;\r
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
567void osd_text(int x, int y, char *text)\r
568{\r
569 int len = strlen(text)*8;\r
570\r
571 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
572 int *p, i, h;\r
573 x &= ~3; // align x\r
574 len = (len+3) >> 2;\r
575 for (h = 0; h < 8; h++) {\r
576 p = (int *) ((unsigned char *) gp2x_screen+x+320*(y+h));\r
577 for (i = len; i; i--, p++) *p = 0xe0e0e0e0;\r
578 }\r
579 gp2x_text_out8_2(x, y, text, 0xf0);\r
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
592static void cd_leds(void)\r
593{\r
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
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
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
610 } else {\r
611 // 16-bit modes\r
612 unsigned int *p = (unsigned int *)((short *)gp2x_screen + 320*2+4);\r
613 unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;\r
614 unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;\r
615 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;\r
616 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;\r
617 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;\r
618 }\r
619}\r
620\r
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
637int localPal[0x100];\r
638static void (*vidCpyM2)(void *dest, void *src) = NULL;\r
639\r
640static void blit(char *fps, char *notice)\r
641{\r
642 int emu_opt = currentConfig.EmuOpt;\r
643\r
644 if (PicoOpt&0x10) {\r
645 // 8bit fast renderer\r
646 if (Pico.m.dirtyPal) {\r
647 Pico.m.dirtyPal = 0;\r
648 vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
649 // feed new palette to our device\r
650 gp2x_video_setpalette(localPal, 0x40);\r
651 }\r
652 vidCpyM2((unsigned char *)gp2x_screen+320*8, framebuff+328*8);\r
653 } else if (!(emu_opt&0x80)) {\r
654 // 8bit accurate renderer\r
655 if (Pico.m.dirtyPal) {\r
656 Pico.m.dirtyPal = 0;\r
657 if(Pico.video.reg[0xC]&8) { // shadow/hilight mode\r
658 vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
659 vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);\r
660 vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);\r
661 blockcpy(localPal+0xc0, localPal+0x40, 0x40*4);\r
662 localPal[0xc0] = 0x0000c000;\r
663 localPal[0xd0] = 0x00c00000;\r
664 localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
665 localPal[0xf0] = 0x00ffffff;\r
666 gp2x_video_setpalette(localPal, 0x100);\r
667 } else if (rendstatus & 0x20) { // mid-frame palette changes\r
668 vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
669 vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);\r
670 vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);\r
671 gp2x_video_setpalette(localPal, 0xc0);\r
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
680 if (emu_opt & 2)\r
681 osd_text(osd_fps_x, 232, fps);\r
682 if ((emu_opt & 0x400) && (PicoMCD & 1))\r
683 cd_leds();\r
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
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
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
713 }\r
714}\r
715\r
716\r
717static void vidResetMode(void)\r
718{\r
719 if (PicoOpt&0x10) {\r
720 gp2x_video_changemode(8);\r
721 } else if (currentConfig.EmuOpt&0x80) {\r
722 gp2x_video_changemode(15);\r
723 PicoDrawSetColorFormat(1);\r
724 PicoScan = EmuScan16;\r
725 PicoScan(0, 0);\r
726 } else {\r
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
736 localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
737 localPal[0xf0] = 0x00ffffff;\r
738 gp2x_video_setpalette(localPal, 0x100);\r
739 gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
740 gp2x_video_flip();\r
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
767static void RunEvents(unsigned int which)\r
768{\r
769 if(which & 0x1800) { // save or load (but not both)\r
770 int do_it = 1;\r
771 if (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200) && check_save_file()) {\r
772 unsigned long keys;\r
773 blit("", "OVERWRITE SAVE? (Y=yes, X=no)");\r
774 while( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )\r
775 usleep(50*1024);\r
776 if (keys & GP2X_X) do_it = 0;\r
777 clearArea(0);\r
778 }\r
779 if (do_it) {\r
780 blit("", (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");\r
781 emu_SaveLoadGame(which & 0x1000, 0);\r
782 }\r
783\r
784 reset_timing = 1;\r
785 }\r
786 if(which & 0x0400) { // switch renderer\r
787 if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }\r
788 else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;\r
789 else currentConfig.EmuOpt &= ~0x80;\r
790\r
791 vidResetMode();\r
792\r
793 if (PicoOpt&0x10) {\r
794 strcpy(noticeMsg, " 8bit fast renderer");\r
795 } else if (currentConfig.EmuOpt&0x80) {\r
796 strcpy(noticeMsg, "16bit accurate renderer");\r
797 } else {\r
798 strcpy(noticeMsg, " 8bit accurate renderer");\r
799 }\r
800\r
801 gettimeofday(&noticeMsgTime, 0);\r
802 }\r
803 if(which & 0x0300) {\r
804 if(which&0x0200) {\r
805 state_slot -= 1;\r
806 if(state_slot < 0) state_slot = 9;\r
807 } else {\r
808 state_slot += 1;\r
809 if(state_slot > 9) state_slot = 0;\r
810 }\r
811 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, check_save_file() ? "USED" : "FREE");\r
812 gettimeofday(&noticeMsgTime, 0);\r
813 }\r
814 if(which & 0x0080) {\r
815 engineState = PGS_Menu;\r
816 }\r
817}\r
818\r
819\r
820static void updateMovie(void)\r
821{\r
822 int offs = Pico.m.frame_count*3 + 0x40;\r
823 if (offs+3 > movie_size) {\r
824 free(movie_data);\r
825 movie_data = 0;\r
826 strcpy(noticeMsg, "END OF MOVIE.");\r
827 printf("END OF MOVIE.\n");\r
828 gettimeofday(&noticeMsgTime, 0);\r
829 } else {\r
830 // MXYZ SACB RLDU\r
831 PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU\r
832 if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // A\r
833 if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // B\r
834 if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // A\r
835 PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU\r
836 if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // A\r
837 if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // B\r
838 if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // A\r
839 PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX\r
840 if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X\r
841 if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z\r
842 PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX\r
843 if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X\r
844 if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z\r
845 }\r
846}\r
847\r
848\r
849static void updateKeys(void)\r
850{\r
851 unsigned long keys, allActions[2] = { 0, 0 }, events;\r
852 static unsigned long prevEvents = 0;\r
853 int joy, i;\r
854\r
855 keys = gp2x_joystick_read(0);\r
856 if (keys & GP2X_SELECT) {\r
857 engineState = select_exits ? PGS_Quit : PGS_Menu;\r
858 // wait until select is released, so menu would not resume game\r
859 while (gp2x_joystick_read(1) & GP2X_SELECT) usleep(50*1000);\r
860 }\r
861\r
862 keys &= CONFIGURABLE_KEYS;\r
863\r
864 for (i = 0; i < 32; i++)\r
865 {\r
866 if (keys & (1 << i)) {\r
867 int pl, acts = currentConfig.KeyBinds[i];\r
868 if (!acts) continue;\r
869 pl = (acts >> 16) & 1;\r
870 if (combo_keys & (1 << i)) {\r
871 int u = i+1, acts_c = acts & combo_acts;\r
872 // let's try to find the other one\r
873 if (acts_c)\r
874 for (; u < 32; u++)\r
875 if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) {\r
876 allActions[pl] |= acts_c;\r
877 keys &= ~((1 << i) | (1 << u));\r
878 break;\r
879 }\r
880 // add non-combo actions if combo ones were not found\r
881 if (!acts_c || u == 32)\r
882 allActions[pl] |= acts & ~combo_acts;\r
883 } else {\r
884 allActions[pl] |= acts;\r
885 }\r
886 }\r
887 }\r
888\r
889 // add joy inputs\r
890 if (num_of_joys > 0)\r
891 {\r
892 gp2x_usbjoy_update();\r
893 for (joy = 0; joy < num_of_joys; joy++) {\r
894 int keys = gp2x_usbjoy_check2(joy);\r
895 for (i = 0; i < 32; i++) {\r
896 if (keys & (1 << i)) {\r
897 int acts = currentConfig.JoyBinds[joy][i];\r
898 int pl = (acts >> 16) & 1;\r
899 allActions[pl] |= acts;\r
900 }\r
901 }\r
902 }\r
903 }\r
904\r
905 PicoPad[0] = (unsigned short) allActions[0];\r
906 PicoPad[1] = (unsigned short) allActions[1];\r
907\r
908 events = (allActions[0] | allActions[1]) >> 16;\r
909\r
910 // volume is treated in special way and triggered every frame\r
911 if(events & 0x6000) {\r
912 int vol = currentConfig.volume;\r
913 if (events & 0x2000) {\r
914 if (vol < 90) vol++;\r
915 } else {\r
916 if (vol > 0) vol--;\r
917 }\r
918 gp2x_sound_volume(vol, vol);\r
919 sprintf(noticeMsg, "VOL: %02i", vol);\r
920 gettimeofday(&noticeMsgTime, 0);\r
921 currentConfig.volume = vol;\r
922 }\r
923\r
924 events &= ~prevEvents;\r
925 if (events) RunEvents(events);\r
926 if (movie_data) updateMovie();\r
927\r
928 prevEvents = (allActions[0] | allActions[1]) >> 16;\r
929}\r
930\r
931\r
932static void updateSound(int len)\r
933{\r
934 if (PicoOpt&8) len<<=1;\r
935\r
936 gp2x_sound_write(PsndOut, len<<1);\r
937}\r
938\r
939\r
940static void SkipFrame(int do_sound)\r
941{\r
942 void *sndbuff_tmp = 0;\r
943 if (PsndOut && !do_sound) {\r
944 sndbuff_tmp = PsndOut;\r
945 PsndOut = 0;\r
946 }\r
947\r
948 PicoSkipFrame=1;\r
949 PicoFrame();\r
950 PicoSkipFrame=0;\r
951\r
952 if (sndbuff_tmp && !do_sound) {\r
953 PsndOut = sndbuff_tmp;\r
954 }\r
955}\r
956\r
957\r
958static void simpleWait(int thissec, int lim_time)\r
959{\r
960 struct timeval tval;\r
961\r
962 spend_cycles(1024);\r
963 gettimeofday(&tval, 0);\r
964 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
965\r
966 while(tval.tv_usec < lim_time)\r
967 {\r
968 spend_cycles(1024);\r
969 gettimeofday(&tval, 0);\r
970 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
971 }\r
972}\r
973\r
974\r
975void emu_Loop(void)\r
976{\r
977 static int gp2x_old_clock = 200;\r
978 static int PsndRate_old = 0, PicoOpt_old = 0, PsndLen_real = 0, pal_old = 0;\r
979 char fpsbuff[24]; // fps count c string\r
980 struct timeval tval; // timing\r
981 int thissec = 0, frames_done = 0, frames_shown = 0, oldmodes = 0;\r
982 int target_fps, target_frametime, lim_time, i;\r
983 char *notice = 0;\r
984\r
985 printf("entered emu_Loop()\n");\r
986\r
987 if (gp2x_old_clock != currentConfig.CPUclock) {\r
988 printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);\r
989 set_FCLK(currentConfig.CPUclock);\r
990 gp2x_old_clock = currentConfig.CPUclock;\r
991 printf(" done\n");\r
992 }\r
993\r
994 if (gp2x_old_gamma != currentConfig.gamma) {\r
995 set_gamma(currentConfig.gamma);\r
996 gp2x_old_gamma = currentConfig.gamma;\r
997 printf("updated gamma to %i\n", currentConfig.gamma);\r
998 }\r
999\r
1000 fpsbuff[0] = 0;\r
1001\r
1002 // make sure we are in correct mode\r
1003 vidResetMode();\r
1004 Pico.m.dirtyPal = 1;\r
1005 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;\r
1006 find_combos();\r
1007\r
1008 // pal/ntsc might have changed, reset related stuff\r
1009 target_fps = Pico.m.pal ? 50 : 60;\r
1010 target_frametime = 1000000/target_fps;\r
1011 reset_timing = 1;\r
1012\r
1013 // prepare sound stuff\r
1014 if(currentConfig.EmuOpt & 4) {\r
1015 int snd_excess_add;\r
1016 if(PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old || crashed_940) {\r
1017 /* if 940 is turned off, we need it to be put back to sleep */\r
1018 if (!(PicoOpt&0x200) && ((PicoOpt^PicoOpt_old)&0x200)) {\r
1019 Reset940(1, 2);\r
1020 Pause940(1);\r
1021 }\r
1022 sound_rerate(1);\r
1023 }\r
1024 //excess_samples = PsndRate - PsndLen*target_fps;\r
1025 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r
1026 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
1027 gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);\r
1028 gp2x_sound_volume(currentConfig.volume, currentConfig.volume);\r
1029 PicoWriteSound = updateSound;\r
1030 memset(sndBuffer, 0, sizeof(sndBuffer));\r
1031 PsndOut = sndBuffer;\r
1032 PsndRate_old = PsndRate;\r
1033 PsndLen_real = PsndLen;\r
1034 PicoOpt_old = PicoOpt;\r
1035 pal_old = Pico.m.pal;\r
1036 } else {\r
1037 PsndOut = 0;\r
1038 }\r
1039\r
1040 // loop?\r
1041 while (engineState == PGS_Running)\r
1042 {\r
1043 int modes;\r
1044\r
1045 gettimeofday(&tval, 0);\r
1046 if(reset_timing) {\r
1047 reset_timing = 0;\r
1048 thissec = tval.tv_sec;\r
1049 frames_shown = frames_done = tval.tv_usec/target_frametime;\r
1050 }\r
1051\r
1052 // show notice message?\r
1053 if(noticeMsgTime.tv_sec) {\r
1054 static int noticeMsgSum;\r
1055 if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec\r
1056 noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;\r
1057 clearArea(0);\r
1058 notice = 0;\r
1059 } else {\r
1060 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];\r
1061 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }\r
1062 notice = noticeMsg;\r
1063 }\r
1064 }\r
1065\r
1066 // check for mode changes\r
1067 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);\r
1068 if (modes != oldmodes) {\r
1069 int scalex = 320;\r
1070 osd_fps_x = OSD_FPS_X;\r
1071 if (modes & 4) {\r
1072 vidCpyM2 = vidCpyM2_40col;\r
1073 } else {\r
1074 if (PicoOpt & 0x100) {\r
1075 vidCpyM2 = vidCpyM2_32col_nobord;\r
1076 scalex = 256;\r
1077 osd_fps_x = OSD_FPS_X - 64;\r
1078 } else {\r
1079 vidCpyM2 = vidCpyM2_32col;\r
1080 }\r
1081 }\r
1082 gp2x_video_RGB_setscaling(scalex, 240);\r
1083 oldmodes = modes;\r
1084 clearArea(1);\r
1085 }\r
1086\r
1087 // second changed?\r
1088 if(thissec != tval.tv_sec) {\r
1089#ifdef BENCHMARK\r
1090 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];\r
1091 if(++bench == 10) {\r
1092 bench = 0;\r
1093 bench_fps_s = bench_fps;\r
1094 bf[bfp++ & 3] = bench_fps;\r
1095 bench_fps = 0;\r
1096 }\r
1097 bench_fps += frames_shown;\r
1098 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);\r
1099#else\r
1100 if(currentConfig.EmuOpt & 2)\r
1101 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);\r
1102#endif\r
1103 thissec = tval.tv_sec;\r
1104\r
1105 if(PsndOut == 0 && currentConfig.Frameskip >= 0) {\r
1106 frames_done = frames_shown = 0;\r
1107 } else {\r
1108 // it is quite common for this implementation to leave 1 fame unfinished\r
1109 // when second changes, but we don't want buffer to starve.\r
1110 if(PsndOut && frames_done < target_fps && frames_done > target_fps-5) {\r
1111 updateKeys();\r
1112 SkipFrame(1); frames_done++;\r
1113 }\r
1114\r
1115 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;\r
1116 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;\r
1117 if (frames_shown > frames_done) frames_shown = frames_done;\r
1118 }\r
1119 }\r
1120\r
1121 lim_time = (frames_done+1) * target_frametime;\r
1122 if(currentConfig.Frameskip >= 0) { // frameskip enabled\r
1123 for(i = 0; i < currentConfig.Frameskip; i++) {\r
1124 updateKeys();\r
1125 SkipFrame(1); frames_done++;\r
1126 if (PsndOut) { // do framelimitting if sound is enabled\r
1127 gettimeofday(&tval, 0);\r
1128 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
1129 if(tval.tv_usec < lim_time) { // we are too fast\r
1130 simpleWait(thissec, lim_time);\r
1131 }\r
1132 }\r
1133 lim_time += target_frametime;\r
1134 }\r
1135 } else if(tval.tv_usec > lim_time) { // auto frameskip\r
1136 // no time left for this frame - skip\r
1137 updateKeys();\r
1138 SkipFrame(tval.tv_usec < lim_time+target_frametime); frames_done++;\r
1139 continue;\r
1140 }\r
1141\r
1142 updateKeys();\r
1143 PicoFrame();\r
1144\r
1145#if 0\r
1146if (Pico.m.frame_count == 31563) {\r
1147 FILE *f;\r
1148 f = fopen("ram_p.bin", "wb");\r
1149 if (!f) { printf("!f\n"); exit(1); }\r
1150 fwrite(Pico.ram, 1, 0x10000, f);\r
1151 fclose(f);\r
1152 exit(0);\r
1153}\r
1154#endif\r
1155#if 0\r
1156 // debug\r
1157 {\r
1158 #define BYTE unsigned char\r
1159 #define WORD unsigned short\r
1160 struct\r
1161 {\r
1162 BYTE IDLength; /* 00h Size of Image ID field */\r
1163 BYTE ColorMapType; /* 01h Color map type */\r
1164 BYTE ImageType; /* 02h Image type code */\r
1165 WORD CMapStart; /* 03h Color map origin */\r
1166 WORD CMapLength; /* 05h Color map length */\r
1167 BYTE CMapDepth; /* 07h Depth of color map entries */\r
1168 WORD XOffset; /* 08h X origin of image */\r
1169 WORD YOffset; /* 0Ah Y origin of image */\r
1170 WORD Width; /* 0Ch Width of image */\r
1171 WORD Height; /* 0Eh Height of image */\r
1172 BYTE PixelDepth; /* 10h Image pixel size */\r
1173 BYTE ImageDescriptor; /* 11h Image descriptor byte */\r
1174 } __attribute__((packed)) TGAHEAD;\r
1175 static unsigned short oldscr[320*240];\r
1176 FILE *f; char name[128]; int i;\r
1177\r
1178 memset(&TGAHEAD, 0, sizeof(TGAHEAD));\r
1179 TGAHEAD.ImageType = 2;\r
1180 TGAHEAD.Width = 320;\r
1181 TGAHEAD.Height = 240;\r
1182 TGAHEAD.PixelDepth = 16;\r
1183 TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left\r
1184\r
1185 #define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?\r
1186\r
1187 for (i = 0; i < 320*240; i++)\r
1188 if(oldscr[i] != CONV(((unsigned short *)gp2x_screen)[i])) break;\r
1189 if (i < 320*240)\r
1190 {\r
1191 for (i = 0; i < 320*240; i++)\r
1192 oldscr[i] = CONV(((unsigned short *)gp2x_screen)[i]);\r
1193 sprintf(name, "%05i.tga", Pico.m.frame_count);\r
1194 f = fopen(name, "wb");\r
1195 if (!f) { printf("!f\n"); exit(1); }\r
1196 fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);\r
1197 fwrite(oldscr, 1, 320*240*2, f);\r
1198 fclose(f);\r
1199 }\r
1200 }\r
1201#endif\r
1202\r
1203 // check time\r
1204 gettimeofday(&tval, 0);\r
1205 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
1206\r
1207 // sleep if we are still too fast\r
1208 if(PsndOut != 0 || currentConfig.Frameskip < 0)\r
1209 {\r
1210 // usleep sleeps for ~20ms minimum, so it is not a solution here\r
1211 gettimeofday(&tval, 0);\r
1212 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
1213 if(tval.tv_usec < lim_time)\r
1214 {\r
1215 // we are too fast\r
1216 simpleWait(thissec, lim_time);\r
1217 }\r
1218 }\r
1219\r
1220 blit(fpsbuff, notice);\r
1221\r
1222 frames_done++; frames_shown++;\r
1223 }\r
1224\r
1225 // save SRAM\r
1226 if((currentConfig.EmuOpt & 1) && SRam.changed) {\r
1227 blit("", "Writing SRAM/BRAM..");\r
1228 emu_SaveLoadGame(0, 1);\r
1229 SRam.changed = 0;\r
1230 }\r
1231\r
1232 // if in 16bit mode, generate 8it image for menu background\r
1233 if (!(PicoOpt&0x10) && (currentConfig.EmuOpt&0x80)) {\r
1234 PicoOpt |= 0x10;\r
1235 PicoFrameFull();\r
1236 blit("", NULL); blit("", NULL); blit("", NULL); blit("", NULL); // be sure buffer3 gets updated\r
1237 PicoOpt &= ~0x10;\r
1238 }\r
1239}\r
1240\r
1241\r
1242void emu_ResetGame(void)\r
1243{\r
1244 PicoReset(0);\r
1245 reset_timing = 1;\r
1246}\r
1247\r
1248\r
1249size_t gzRead2(void *p, size_t _size, size_t _n, void *file)\r
1250{\r
1251 return gzread(file, p, _n);\r
1252}\r
1253\r
1254\r
1255size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)\r
1256{\r
1257 return gzwrite(file, p, _n);\r
1258}\r
1259\r
1260\r
1261int emu_SaveLoadGame(int load, int sram)\r
1262{\r
1263 int ret = 0;\r
1264 char saveFname[512];\r
1265\r
1266 // make save filename\r
1267 romfname_ext(saveFname, "");\r
1268 if(sram) strcat(saveFname, (PicoMCD&1) ? ".brm" : ".srm");\r
1269 else {\r
1270 if(state_slot > 0 && state_slot < 10) sprintf(saveFname, "%s.%i", saveFname, state_slot);\r
1271 strcat(saveFname, ".mds");\r
1272 }\r
1273\r
1274 printf("saveLoad (%i, %i): %s\n", load, sram, saveFname);\r
1275\r
1276 if(sram) {\r
1277 FILE *sramFile;\r
1278 int sram_size;\r
1279 unsigned char *sram_data;\r
1280 if (PicoMCD&1) {\r
1281 sram_size = 0x2000;\r
1282 sram_data = Pico_mcd->bram;\r
1283 } else {\r
1284 sram_size = SRam.end-SRam.start+1;\r
1285 if(SRam.reg_back & 4) sram_size=0x2000;\r
1286 sram_data = SRam.data;\r
1287 }\r
1288 if(!sram_data) return 0; // SRam forcefully disabled for this game\r
1289\r
1290 if(load) {\r
1291 sramFile = fopen(saveFname, "rb");\r
1292 if(!sramFile) return -1;\r
1293 fread(sram_data, 1, sram_size, sramFile);\r
1294 fclose(sramFile);\r
1295 } else {\r
1296 // sram save needs some special processing\r
1297 // see if we have anything to save\r
1298 for(; sram_size > 0; sram_size--)\r
1299 if(sram_data[sram_size-1]) break;\r
1300\r
1301 if(sram_size) {\r
1302 sramFile = fopen(saveFname, "wb");\r
1303 ret = fwrite(sram_data, 1, sram_size, sramFile);\r
1304 ret = (ret != sram_size) ? -1 : 0;\r
1305 fclose(sramFile);\r
1306 sync();\r
1307 }\r
1308 }\r
1309 return ret;\r
1310 }\r
1311 else\r
1312 {\r
1313 void *PmovFile = NULL;\r
1314 // try gzip first\r
1315 if(currentConfig.EmuOpt & 8) {\r
1316 strcat(saveFname, ".gz");\r
1317 if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) {\r
1318 areaRead = gzRead2;\r
1319 areaWrite = gzWrite2;\r
1320 areaEof = (areaeof *) gzeof;\r
1321 areaSeek = (areaseek *) gzseek;\r
1322 if(!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);\r
1323 } else\r
1324 saveFname[strlen(saveFname)-3] = 0;\r
1325 }\r
1326 if(!PmovFile) { // gzip failed or was disabled\r
1327 if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) {\r
1328 areaRead = (arearw *) fread;\r
1329 areaWrite = (arearw *) fwrite;\r
1330 areaEof = (areaeof *) feof;\r
1331 areaSeek = (areaseek *) fseek;\r
1332 }\r
1333 }\r
1334 if(PmovFile) {\r
1335 ret = PmovState(load ? 6 : 5, PmovFile);\r
1336 if(areaRead == gzRead2)\r
1337 gzclose(PmovFile);\r
1338 else fclose ((FILE *) PmovFile);\r
1339 PmovFile = 0;\r
1340 if (!load) sync();\r
1341 else Pico.m.dirtyPal=1;\r
1342 }\r
1343 else ret = -1;\r
1344 if (!ret)\r
1345 strcpy(noticeMsg, load ? "GAME LOADED " : "GAME SAVED ");\r
1346 else\r
1347 {\r
1348 strcpy(noticeMsg, load ? "LOAD FAILED " : "SAVE FAILED ");\r
1349 ret = -1;\r
1350 }\r
1351\r
1352 gettimeofday(&noticeMsgTime, 0);\r
1353 return ret;\r
1354 }\r
1355}\r