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