ea8c405f |
1 | // (c) Copyright 2006-2007 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 <ctype.h> // tolower\r |
9 | #ifndef NO_SYNC\r |
10 | #include <unistd.h>\r |
11 | #endif\r |
12 | \r |
13 | #include "emu.h"\r |
14 | #include "menu.h"\r |
15 | #include "fonts.h"\r |
16 | #include "lprintf.h"\r |
17 | \r |
18 | #include <Pico/PicoInt.h>\r |
19 | #include <Pico/Patch.h>\r |
20 | #include <zlib/zlib.h>\r |
21 | \r |
22 | #if defined(__GP2X__)\r |
23 | #include "../gp2x/gp2x.h"\r |
24 | #define SCREEN_WIDTH 320\r |
25 | #define SCREEN_BUFFER gp2x_screen\r |
26 | #elif defined(__GIZ__)\r |
27 | #include "../gizmondo/giz.h"\r |
28 | #define SCREEN_WIDTH 321\r |
29 | #define SCREEN_BUFFER giz_screen // ?\r |
30 | #endif\r |
31 | \r |
32 | char *PicoConfigFile = "picoconfig.bin";\r |
33 | currentConfig_t currentConfig;\r |
34 | unsigned char *rom_data = NULL;\r |
35 | char noticeMsg[64];\r |
36 | int state_slot = 0;\r |
37 | int config_slot = 0, config_slot_current = 0;\r |
38 | \r |
39 | unsigned char *movie_data = NULL;\r |
40 | static int movie_size = 0;\r |
41 | \r |
42 | // provided by platform code:\r |
43 | extern char romFileName[];\r |
44 | extern void emu_noticeMsgUpdated(void);\r |
45 | extern void emu_getMainDir(char *dst, int len);\r |
46 | extern void emu_setDefaultConfig(void);\r |
47 | extern void menu_romload_prepare(const char *rom_name);\r |
48 | extern void menu_romload_end(void);\r |
49 | \r |
50 | \r |
51 | // utilities\r |
52 | static void strlwr_(char* string)\r |
53 | {\r |
54 | while ( (*string++ = (char)tolower(*string)) );\r |
55 | }\r |
56 | \r |
57 | static int try_rfn_cut(void)\r |
58 | {\r |
59 | FILE *tmp;\r |
60 | char *p;\r |
61 | \r |
62 | p = romFileName + strlen(romFileName) - 1;\r |
63 | for (; p > romFileName; p--)\r |
64 | if (*p == '.') break;\r |
65 | *p = 0;\r |
66 | \r |
67 | if((tmp = fopen(romFileName, "rb"))) {\r |
68 | fclose(tmp);\r |
69 | return 1;\r |
70 | }\r |
71 | return 0;\r |
72 | }\r |
73 | \r |
74 | static void get_ext(char *file, char *ext)\r |
75 | {\r |
76 | char *p;\r |
77 | \r |
78 | p = file + strlen(file) - 4;\r |
79 | if (p < file) p = file;\r |
80 | strncpy(ext, p, 4);\r |
81 | ext[4] = 0;\r |
82 | strlwr_(ext);\r |
83 | }\r |
84 | \r |
85 | char *biosfiles_us[] = { "us_scd2_9306", "SegaCDBIOS9303", "us_scd1_9210" };\r |
86 | char *biosfiles_eu[] = { "eu_mcd2_9306", "eu_mcd2_9303", "eu_mcd1_9210" };\r |
87 | char *biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };\r |
88 | \r |
89 | int emu_findBios(int region, char **bios_file)\r |
90 | {\r |
91 | static char bios_path[1024];\r |
92 | int i, count;\r |
93 | char **files;\r |
94 | FILE *f = NULL;\r |
95 | \r |
96 | if (region == 4) { // US\r |
97 | files = biosfiles_us;\r |
98 | count = sizeof(biosfiles_us) / sizeof(char *);\r |
99 | } else if (region == 8) { // EU\r |
100 | files = biosfiles_eu;\r |
101 | count = sizeof(biosfiles_eu) / sizeof(char *);\r |
102 | } else if (region == 1 || region == 2) {\r |
103 | files = biosfiles_jp;\r |
104 | count = sizeof(biosfiles_jp) / sizeof(char *);\r |
105 | } else {\r |
106 | return 0;\r |
107 | }\r |
108 | \r |
109 | for (i = 0; i < count; i++)\r |
110 | {\r |
111 | emu_getMainDir(bios_path, sizeof(bios_path));\r |
112 | strcat(bios_path, files[i]);\r |
113 | strcat(bios_path, ".bin");\r |
114 | f = fopen(bios_path, "rb");\r |
115 | if (f) break;\r |
116 | \r |
117 | bios_path[strlen(bios_path) - 4] = 0;\r |
118 | strcat(bios_path, ".zip");\r |
119 | f = fopen(bios_path, "rb");\r |
120 | if (f) break;\r |
121 | }\r |
122 | \r |
123 | if (f) {\r |
124 | lprintf("using bios: %s\n", bios_path);\r |
125 | fclose(f);\r |
126 | if (bios_file) *bios_file = bios_path;\r |
127 | return 1;\r |
128 | } else {\r |
129 | sprintf(menuErrorMsg, "no %s BIOS files found, read docs",\r |
130 | region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r |
131 | lprintf("%s\n", menuErrorMsg);\r |
132 | return 0;\r |
133 | }\r |
134 | }\r |
135 | \r |
136 | /* checks if romFileName points to valid MegaCD image\r |
137 | * if so, checks for suitable BIOS */\r |
138 | int emu_cdCheck(int *pregion)\r |
139 | {\r |
140 | unsigned char buf[32];\r |
141 | pm_file *cd_f;\r |
142 | int type = 0, region = 4; // 1: Japan, 4: US, 8: Europe\r |
143 | \r |
144 | cd_f = pm_open(romFileName);\r |
145 | if (!cd_f) return 0; // let the upper level handle this\r |
146 | \r |
147 | if (pm_read(buf, 32, cd_f) != 32) {\r |
148 | pm_close(cd_f);\r |
149 | return 0;\r |
150 | }\r |
151 | \r |
152 | if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) type = 1; // Sega CD (ISO)\r |
153 | if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) type = 2; // Sega CD (BIN)\r |
154 | if (type == 0) {\r |
155 | pm_close(cd_f);\r |
156 | return 0;\r |
157 | }\r |
158 | \r |
159 | /* it seems we have a CD image here. Try to detect region now.. */\r |
160 | pm_seek(cd_f, (type == 1) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);\r |
161 | pm_read(buf, 1, cd_f);\r |
162 | pm_close(cd_f);\r |
163 | \r |
164 | if (buf[0] == 0x64) region = 8; // EU\r |
165 | if (buf[0] == 0xa1) region = 1; // JAP\r |
166 | \r |
167 | lprintf("detected %s Sega/Mega CD image with %s region\n",\r |
168 | type == 2 ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r |
169 | \r |
170 | if (pregion != NULL) *pregion = region;\r |
171 | \r |
172 | return type;\r |
173 | }\r |
174 | \r |
175 | int emu_ReloadRom(void)\r |
176 | {\r |
177 | unsigned int rom_size = 0;\r |
178 | char *used_rom_name = romFileName;\r |
179 | char ext[5];\r |
180 | pm_file *rom;\r |
181 | int ret, cd_state, cd_region, cfg_loaded = 0;\r |
182 | \r |
183 | lprintf("emu_ReloadRom(%s)\n", romFileName);\r |
184 | \r |
185 | get_ext(romFileName, ext);\r |
186 | \r |
187 | // detect wrong extensions\r |
188 | if(!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz\r |
189 | sprintf(menuErrorMsg, "Not a ROM selected.");\r |
190 | return 0;\r |
191 | }\r |
192 | \r |
193 | PicoPatchUnload();\r |
194 | \r |
195 | // check for movie file\r |
196 | if(movie_data) {\r |
197 | free(movie_data);\r |
198 | movie_data = 0;\r |
199 | }\r |
200 | if(!strcmp(ext, ".gmv")) {\r |
201 | // check for both gmv and rom\r |
202 | int dummy;\r |
203 | FILE *movie_file = fopen(romFileName, "rb");\r |
204 | if(!movie_file) {\r |
205 | sprintf(menuErrorMsg, "Failed to open movie.");\r |
206 | return 0;\r |
207 | }\r |
208 | fseek(movie_file, 0, SEEK_END);\r |
209 | movie_size = ftell(movie_file);\r |
210 | fseek(movie_file, 0, SEEK_SET);\r |
211 | if(movie_size < 64+3) {\r |
212 | sprintf(menuErrorMsg, "Invalid GMV file.");\r |
213 | fclose(movie_file);\r |
214 | return 0;\r |
215 | }\r |
216 | movie_data = malloc(movie_size);\r |
217 | if(movie_data == NULL) {\r |
218 | sprintf(menuErrorMsg, "low memory.");\r |
219 | fclose(movie_file);\r |
220 | return 0;\r |
221 | }\r |
222 | fread(movie_data, 1, movie_size, movie_file);\r |
223 | fclose(movie_file);\r |
224 | if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {\r |
225 | sprintf(menuErrorMsg, "Invalid GMV file.");\r |
226 | return 0;\r |
227 | }\r |
228 | dummy = try_rfn_cut() || try_rfn_cut();\r |
229 | if (!dummy) {\r |
230 | sprintf(menuErrorMsg, "Could't find a ROM for movie.");\r |
231 | return 0;\r |
232 | }\r |
233 | get_ext(romFileName, ext);\r |
234 | }\r |
235 | else if (!strcmp(ext, ".pat")) {\r |
236 | int dummy;\r |
237 | PicoPatchLoad(romFileName);\r |
238 | dummy = try_rfn_cut() || try_rfn_cut();\r |
239 | if (!dummy) {\r |
240 | sprintf(menuErrorMsg, "Could't find a ROM to patch.");\r |
241 | return 0;\r |
242 | }\r |
243 | get_ext(romFileName, ext);\r |
244 | }\r |
245 | \r |
246 | if ((PicoMCD & 1) && Pico_mcd != NULL)\r |
247 | Stop_CD();\r |
248 | \r |
249 | // check for MegaCD image\r |
250 | cd_state = emu_cdCheck(&cd_region);\r |
251 | if (cd_state > 0)\r |
252 | {\r |
253 | // valid CD image, check for BIOS..\r |
254 | \r |
255 | // we need to have config loaded at this point\r |
256 | ret = emu_ReadConfig(1, 1);\r |
257 | if (!ret) emu_ReadConfig(0, 1);\r |
258 | cfg_loaded = 1;\r |
259 | \r |
260 | if (PicoRegionOverride) {\r |
261 | cd_region = PicoRegionOverride;\r |
262 | lprintf("overrided region to %s\n", cd_region != 4 ? (cd_region == 8 ? "EU" : "JAP") : "USA");\r |
263 | }\r |
264 | if (!emu_findBios(cd_region, &used_rom_name)) {\r |
265 | // bios_help() ?\r |
266 | return 0;\r |
267 | }\r |
268 | \r |
269 | PicoMCD |= 1;\r |
270 | get_ext(used_rom_name, ext);\r |
271 | }\r |
272 | else\r |
273 | {\r |
274 | if (PicoMCD & 1) Stop_CD();\r |
275 | PicoMCD &= ~1;\r |
276 | }\r |
277 | \r |
278 | rom = pm_open(used_rom_name);\r |
279 | if(!rom) {\r |
280 | sprintf(menuErrorMsg, "Failed to open rom.");\r |
281 | return 0;\r |
282 | }\r |
283 | \r |
284 | menu_romload_prepare(used_rom_name);\r |
285 | \r |
286 | if(rom_data) {\r |
287 | free(rom_data);\r |
288 | rom_data = 0;\r |
289 | rom_size = 0;\r |
290 | }\r |
291 | \r |
292 | if( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) {\r |
293 | sprintf(menuErrorMsg, "PicoCartLoad() failed.");\r |
294 | lprintf("%s\n", menuErrorMsg);\r |
295 | pm_close(rom);\r |
296 | menu_romload_end();\r |
297 | return 0;\r |
298 | }\r |
299 | pm_close(rom);\r |
300 | menu_romload_end();\r |
301 | \r |
302 | // detect wrong files (Pico crashes on very small files), also see if ROM EP is good\r |
303 | if(rom_size <= 0x200 || strncmp((char *)rom_data, "Pico", 4) == 0 ||\r |
304 | ((*(unsigned char *)(rom_data+4)<<16)|(*(unsigned short *)(rom_data+6))) >= (int)rom_size) {\r |
305 | if (rom_data) free(rom_data);\r |
306 | rom_data = 0;\r |
307 | sprintf(menuErrorMsg, "Not a ROM selected.");\r |
308 | return 0;\r |
309 | }\r |
310 | \r |
311 | // load config for this ROM (do this before insert to get correct region)\r |
312 | if (!cfg_loaded) {\r |
313 | ret = emu_ReadConfig(1, 1);\r |
314 | if (!ret) emu_ReadConfig(0, 1);\r |
315 | }\r |
316 | \r |
317 | lprintf("PicoCartInsert(%p, %d);\n", rom_data, rom_size);\r |
318 | if(PicoCartInsert(rom_data, rom_size)) {\r |
319 | sprintf(menuErrorMsg, "Failed to load ROM.");\r |
320 | return 0;\r |
321 | }\r |
322 | \r |
323 | Pico.m.frame_count = 0;\r |
324 | \r |
325 | // insert CD if it was detected\r |
326 | if (cd_state > 0) {\r |
327 | ret = Insert_CD(romFileName, cd_state == 2);\r |
328 | if (ret != 0) {\r |
329 | sprintf(menuErrorMsg, "Insert_CD() failed, invalid CD image?");\r |
330 | lprintf("%s\n", menuErrorMsg);\r |
331 | return 0;\r |
332 | }\r |
333 | }\r |
334 | \r |
335 | // emu_ReadConfig() might have messed currentConfig.lastRomFile\r |
336 | strncpy(currentConfig.lastRomFile, romFileName, sizeof(currentConfig.lastRomFile)-1);\r |
337 | currentConfig.lastRomFile[sizeof(currentConfig.lastRomFile)-1] = 0;\r |
338 | \r |
339 | if (PicoPatches) {\r |
340 | PicoPatchPrepare();\r |
341 | PicoPatchApply();\r |
342 | }\r |
343 | \r |
344 | // additional movie stuff\r |
345 | if (movie_data) {\r |
346 | if(movie_data[0x14] == '6')\r |
347 | PicoOpt |= 0x20; // 6 button pad\r |
348 | else PicoOpt &= ~0x20;\r |
349 | PicoOpt |= 0x10040; // accurate timing, no VDP fifo timing\r |
350 | if(movie_data[0xF] >= 'A') {\r |
351 | if(movie_data[0x16] & 0x80) {\r |
352 | PicoRegionOverride = 8;\r |
353 | } else {\r |
354 | PicoRegionOverride = 4;\r |
355 | }\r |
356 | PicoReset(0);\r |
357 | // TODO: bits 6 & 5\r |
358 | }\r |
359 | movie_data[0x18+30] = 0;\r |
360 | sprintf(noticeMsg, "MOVIE: %s", (char *) &movie_data[0x18]);\r |
361 | }\r |
362 | else\r |
363 | {\r |
364 | PicoOpt &= ~0x10000;\r |
365 | if(Pico.m.pal) {\r |
366 | strcpy(noticeMsg, "PAL SYSTEM / 50 FPS");\r |
367 | } else {\r |
368 | strcpy(noticeMsg, "NTSC SYSTEM / 60 FPS");\r |
369 | }\r |
370 | }\r |
371 | emu_noticeMsgUpdated();\r |
372 | \r |
373 | // load SRAM for this ROM\r |
374 | if(currentConfig.EmuOpt & 1)\r |
375 | emu_SaveLoadGame(1, 1);\r |
376 | \r |
377 | return 1;\r |
378 | }\r |
379 | \r |
380 | \r |
381 | static void romfname_ext(char *dst, const char *prefix, const char *ext)\r |
382 | {\r |
383 | char *p;\r |
384 | int prefix_len = 0;\r |
385 | \r |
386 | // make save filename\r |
387 | for (p = romFileName+strlen(romFileName)-1; p >= romFileName && *p != '/'; p--); p++;\r |
388 | *dst = 0;\r |
389 | if (prefix) {\r |
390 | strcpy(dst, prefix);\r |
391 | prefix_len = strlen(prefix);\r |
392 | }\r |
393 | strncpy(dst + prefix_len, p, 511-prefix_len);\r |
394 | dst[511-8] = 0;\r |
395 | if (dst[strlen(dst)-4] == '.') dst[strlen(dst)-4] = 0;\r |
396 | if (ext) strcat(dst, ext);\r |
397 | }\r |
398 | \r |
399 | \r |
400 | int emu_ReadConfig(int game, int no_defaults)\r |
401 | {\r |
402 | FILE *f;\r |
403 | char cfg[512], extbuf[16];\r |
404 | int bread = 0;\r |
405 | \r |
406 | if (!game)\r |
407 | {\r |
408 | if (!no_defaults)\r |
409 | {\r |
410 | emu_setDefaultConfig();\r |
411 | }\r |
412 | strncpy(cfg, PicoConfigFile, 511);\r |
413 | if (config_slot != 0)\r |
414 | {\r |
415 | char *p = strrchr(cfg, '.');\r |
416 | if (p == NULL) p = cfg + strlen(cfg);\r |
417 | sprintf(extbuf, ".%i.pbcfg", config_slot);\r |
418 | strncpy(p, extbuf, 511 - (p - cfg));\r |
419 | }\r |
420 | cfg[511] = 0;\r |
421 | } else {\r |
422 | if (config_slot != 0)\r |
423 | sprintf(extbuf, ".%i.pbcfg", config_slot);\r |
424 | else strcpy(extbuf, ".pbcfg");\r |
425 | romfname_ext(cfg, "cfg/", extbuf);\r |
426 | f = fopen(cfg, "rb");\r |
427 | if (!f) romfname_ext(cfg, NULL, ".pbcfg");\r |
428 | else fclose(f);\r |
429 | }\r |
430 | \r |
431 | lprintf("emu_ReadConfig: %s ", cfg);\r |
432 | f = fopen(cfg, "rb");\r |
433 | if (f) {\r |
434 | bread = fread(¤tConfig, 1, sizeof(currentConfig), f);\r |
435 | fclose(f);\r |
436 | }\r |
437 | lprintf(bread > 0 ? "(ok)\n" : "(failed)\n");\r |
438 | \r |
439 | PicoOpt = currentConfig.PicoOpt;\r |
440 | PsndRate = currentConfig.PsndRate;\r |
441 | PicoRegionOverride = currentConfig.PicoRegion;\r |
442 | PicoAutoRgnOrder = currentConfig.PicoAutoRgnOrder;\r |
443 | PicoCDBuffers = currentConfig.PicoCDBuffers;\r |
444 | //scaling_update();\r |
445 | // some sanity checks\r |
446 | if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;\r |
447 | if (currentConfig.gamma < 10 || currentConfig.gamma > 300) currentConfig.gamma = 100;\r |
448 | if (currentConfig.volume < 0 || currentConfig.volume > 99) currentConfig.volume = 50;\r |
449 | // if volume keys are unbound, bind them to volume control\r |
450 | if (!currentConfig.KeyBinds[23] && !currentConfig.KeyBinds[22]) {\r |
451 | currentConfig.KeyBinds[23] = 1<<29; // vol up\r |
452 | currentConfig.KeyBinds[22] = 1<<30; // vol down\r |
453 | }\r |
454 | \r |
455 | if (bread > 0) config_slot_current = config_slot;\r |
456 | return (bread > 0); // == sizeof(currentConfig));\r |
457 | }\r |
458 | \r |
459 | \r |
460 | int emu_WriteConfig(int game)\r |
461 | {\r |
462 | FILE *f;\r |
463 | char cfg[512], extbuf[16];\r |
464 | int bwrite = 0;\r |
465 | \r |
466 | if (!game)\r |
467 | {\r |
468 | strncpy(cfg, PicoConfigFile, 511);\r |
469 | if (config_slot != 0)\r |
470 | {\r |
471 | char *p = strrchr(cfg, '.');\r |
472 | if (p == NULL) p = cfg + strlen(cfg);\r |
473 | sprintf(extbuf, ".%i.pbcfg", config_slot);\r |
474 | strncpy(p, extbuf, 511 - (p - cfg));\r |
475 | }\r |
476 | cfg[511] = 0;\r |
477 | } else {\r |
478 | if (config_slot != 0)\r |
479 | sprintf(extbuf, ".%i.pbcfg", config_slot);\r |
480 | else strcpy(extbuf, ".pbcfg");\r |
481 | romfname_ext(cfg, "cfg/", extbuf);\r |
482 | }\r |
483 | \r |
484 | lprintf("emu_WriteConfig: %s ", cfg);\r |
485 | f = fopen(cfg, "wb");\r |
486 | if (f) {\r |
487 | currentConfig.PicoOpt = PicoOpt;\r |
488 | currentConfig.PsndRate = PsndRate;\r |
489 | currentConfig.PicoRegion = PicoRegionOverride;\r |
490 | currentConfig.PicoAutoRgnOrder = PicoAutoRgnOrder;\r |
491 | currentConfig.PicoCDBuffers = PicoCDBuffers;\r |
492 | bwrite = fwrite(¤tConfig, 1, sizeof(currentConfig), f);\r |
493 | fflush(f);\r |
494 | fclose(f);\r |
495 | #ifndef NO_SYNC\r |
496 | sync();\r |
497 | #endif\r |
498 | }\r |
499 | lprintf((bwrite == sizeof(currentConfig)) ? "(ok)\n" : "(failed)\n");\r |
500 | \r |
501 | if (bwrite == sizeof(currentConfig)) config_slot_current = config_slot;\r |
502 | return (bwrite == sizeof(currentConfig));\r |
503 | }\r |
504 | \r |
505 | \r |
506 | void emu_textOut8(int x, int y, const char *text)\r |
507 | {\r |
508 | int i,l,len=strlen(text);\r |
509 | unsigned char *screen = (unsigned char *)SCREEN_BUFFER + x + y*SCREEN_WIDTH;\r |
510 | \r |
511 | /* always using built-in font */\r |
512 | for (i = 0; i < len; i++)\r |
513 | {\r |
514 | for (l=0;l<8;l++)\r |
515 | {\r |
516 | unsigned char fd = fontdata8x8[((text[i])*8)+l];\r |
517 | if (fd&0x80) screen[l*SCREEN_WIDTH+0]=0xf0;\r |
518 | if (fd&0x40) screen[l*SCREEN_WIDTH+1]=0xf0;\r |
519 | if (fd&0x20) screen[l*SCREEN_WIDTH+2]=0xf0;\r |
520 | if (fd&0x10) screen[l*SCREEN_WIDTH+3]=0xf0;\r |
521 | if (fd&0x08) screen[l*SCREEN_WIDTH+4]=0xf0;\r |
522 | if (fd&0x04) screen[l*SCREEN_WIDTH+5]=0xf0;\r |
523 | if (fd&0x02) screen[l*SCREEN_WIDTH+6]=0xf0;\r |
524 | if (fd&0x01) screen[l*SCREEN_WIDTH+7]=0xf0;\r |
525 | }\r |
526 | screen += 8;\r |
527 | }\r |
528 | }\r |
529 | \r |
530 | void emu_textOut16(int x, int y, const char *text)\r |
531 | {\r |
532 | int i,l,len=strlen(text);\r |
533 | unsigned short *screen = (unsigned short *)SCREEN_BUFFER + x + y*SCREEN_WIDTH;\r |
534 | \r |
535 | for (i = 0; i < len; i++)\r |
536 | {\r |
537 | for (l=0;l<8;l++)\r |
538 | {\r |
539 | unsigned char fd = fontdata8x8[((text[i])*8)+l];\r |
540 | if(fd&0x80) screen[l*SCREEN_WIDTH+0]=0xffff;\r |
541 | if(fd&0x40) screen[l*SCREEN_WIDTH+1]=0xffff;\r |
542 | if(fd&0x20) screen[l*SCREEN_WIDTH+2]=0xffff;\r |
543 | if(fd&0x10) screen[l*SCREEN_WIDTH+3]=0xffff;\r |
544 | if(fd&0x08) screen[l*SCREEN_WIDTH+4]=0xffff;\r |
545 | if(fd&0x04) screen[l*SCREEN_WIDTH+5]=0xffff;\r |
546 | if(fd&0x02) screen[l*SCREEN_WIDTH+6]=0xffff;\r |
547 | if(fd&0x01) screen[l*SCREEN_WIDTH+7]=0xffff;\r |
548 | }\r |
549 | screen += 8;\r |
550 | }\r |
551 | }\r |
552 | \r |
553 | \r |
554 | void emu_updateMovie(void)\r |
555 | {\r |
556 | int offs = Pico.m.frame_count*3 + 0x40;\r |
557 | if (offs+3 > movie_size) {\r |
558 | free(movie_data);\r |
559 | movie_data = 0;\r |
560 | strcpy(noticeMsg, "END OF MOVIE.");\r |
561 | lprintf("END OF MOVIE.\n");\r |
562 | emu_noticeMsgUpdated();\r |
563 | } else {\r |
564 | // MXYZ SACB RLDU\r |
565 | PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU\r |
566 | if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // A\r |
567 | if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // B\r |
568 | if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // A\r |
569 | PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU\r |
570 | if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // A\r |
571 | if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // B\r |
572 | if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // A\r |
573 | PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX\r |
574 | if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X\r |
575 | if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z\r |
576 | PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX\r |
577 | if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X\r |
578 | if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z\r |
579 | }\r |
580 | }\r |
581 | \r |
582 | \r |
583 | static size_t gzRead2(void *p, size_t _size, size_t _n, void *file)\r |
584 | {\r |
585 | return gzread(file, p, _n);\r |
586 | }\r |
587 | \r |
588 | \r |
589 | static size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)\r |
590 | {\r |
591 | return gzwrite(file, p, _n);\r |
592 | }\r |
593 | \r |
594 | static int try_ropen_file(const char *fname)\r |
595 | {\r |
596 | FILE *f;\r |
597 | \r |
598 | f = fopen(fname, "rb");\r |
599 | if (f) {\r |
600 | fclose(f);\r |
601 | return 1;\r |
602 | }\r |
603 | return 0;\r |
604 | }\r |
605 | \r |
606 | char *emu_GetSaveFName(int load, int is_sram, int slot)\r |
607 | {\r |
608 | static char saveFname[512];\r |
609 | char ext[16];\r |
610 | \r |
611 | if (is_sram)\r |
612 | {\r |
613 | romfname_ext(saveFname, (PicoMCD&1) ? "brm/" : "srm/", (PicoMCD&1) ? ".brm" : ".srm");\r |
614 | if (load) {\r |
615 | if (try_ropen_file(saveFname)) return saveFname;\r |
616 | // try in current dir..\r |
617 | romfname_ext(saveFname, NULL, (PicoMCD&1) ? ".brm" : ".srm");\r |
618 | if (try_ropen_file(saveFname)) return saveFname;\r |
619 | return NULL; // give up\r |
620 | }\r |
621 | }\r |
622 | else\r |
623 | {\r |
624 | ext[0] = 0;\r |
625 | if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);\r |
626 | strcat(ext, (currentConfig.EmuOpt & 8) ? ".mds.gz" : ".mds");\r |
627 | \r |
628 | romfname_ext(saveFname, "mds/", ext);\r |
629 | if (load) {\r |
630 | if (try_ropen_file(saveFname)) return saveFname;\r |
631 | romfname_ext(saveFname, NULL, ext);\r |
632 | if (try_ropen_file(saveFname)) return saveFname;\r |
633 | if (currentConfig.EmuOpt & 8) {\r |
634 | ext[0] = 0;\r |
635 | if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);\r |
636 | strcat(ext, ".mds");\r |
637 | \r |
638 | romfname_ext(saveFname, "mds/", ext);\r |
639 | if (try_ropen_file(saveFname)) return saveFname;\r |
640 | romfname_ext(saveFname, NULL, ext);\r |
641 | if (try_ropen_file(saveFname)) return saveFname;\r |
642 | }\r |
643 | return NULL;\r |
644 | }\r |
645 | }\r |
646 | \r |
647 | return saveFname;\r |
648 | }\r |
649 | \r |
650 | int emu_checkSaveFile(int slot)\r |
651 | {\r |
652 | return emu_GetSaveFName(1, 0, slot) ? 1 : 0;\r |
653 | }\r |
654 | \r |
655 | void emu_setSaveStateCbs(int gz)\r |
656 | {\r |
657 | if (gz) {\r |
658 | areaRead = gzRead2;\r |
659 | areaWrite = gzWrite2;\r |
660 | areaEof = (areaeof *) gzeof;\r |
661 | areaSeek = (areaseek *) gzseek;\r |
662 | areaClose = (areaclose *) gzclose;\r |
663 | } else {\r |
664 | areaRead = (arearw *) fread;\r |
665 | areaWrite = (arearw *) fwrite;\r |
666 | areaEof = (areaeof *) feof;\r |
667 | areaSeek = (areaseek *) fseek;\r |
668 | areaClose = (areaclose *) fclose;\r |
669 | }\r |
670 | }\r |
671 | \r |
672 | int emu_SaveLoadGame(int load, int sram)\r |
673 | {\r |
674 | int ret = 0;\r |
675 | char *saveFname;\r |
676 | \r |
677 | // make save filename\r |
678 | saveFname = emu_GetSaveFName(load, sram, state_slot);\r |
679 | if (saveFname == NULL) {\r |
680 | if (!sram) {\r |
681 | strcpy(noticeMsg, load ? "LOAD FAILED (missing file)" : "SAVE FAILED ");\r |
682 | emu_noticeMsgUpdated();\r |
683 | }\r |
684 | return -1;\r |
685 | }\r |
686 | \r |
687 | lprintf("saveLoad (%i, %i): %s\n", load, sram, saveFname);\r |
688 | \r |
689 | if(sram) {\r |
690 | FILE *sramFile;\r |
691 | int sram_size;\r |
692 | unsigned char *sram_data;\r |
693 | int truncate = 1;\r |
694 | if (PicoMCD&1) {\r |
695 | if (PicoOpt&0x8000) { // MCD RAM cart?\r |
696 | sram_size = 0x12000;\r |
697 | sram_data = SRam.data;\r |
698 | if (sram_data)\r |
699 | memcpy32((int *)sram_data, (int *)Pico_mcd->bram, 0x2000/4);\r |
700 | } else {\r |
701 | sram_size = 0x2000;\r |
702 | sram_data = Pico_mcd->bram;\r |
703 | truncate = 0; // the .brm may contain RAM cart data after normal brm\r |
704 | }\r |
705 | } else {\r |
706 | sram_size = SRam.end-SRam.start+1;\r |
707 | if(Pico.m.sram_reg & 4) sram_size=0x2000;\r |
708 | sram_data = SRam.data;\r |
709 | }\r |
710 | if (!sram_data) return 0; // SRam forcefully disabled for this game\r |
711 | \r |
712 | if (load) {\r |
713 | sramFile = fopen(saveFname, "rb");\r |
714 | if(!sramFile) return -1;\r |
715 | fread(sram_data, 1, sram_size, sramFile);\r |
716 | fclose(sramFile);\r |
717 | if ((PicoMCD&1) && (PicoOpt&0x8000))\r |
718 | memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4);\r |
719 | } else {\r |
720 | // sram save needs some special processing\r |
721 | // see if we have anything to save\r |
722 | for (; sram_size > 0; sram_size--)\r |
723 | if (sram_data[sram_size-1]) break;\r |
724 | \r |
725 | if (sram_size) {\r |
726 | sramFile = fopen(saveFname, truncate ? "wb" : "r+b");\r |
727 | if (!sramFile) sramFile = fopen(saveFname, "wb"); // retry\r |
728 | if (!sramFile) return -1;\r |
729 | ret = fwrite(sram_data, 1, sram_size, sramFile);\r |
730 | ret = (ret != sram_size) ? -1 : 0;\r |
731 | fclose(sramFile);\r |
732 | #ifndef NO_SYNC\r |
733 | sync();\r |
734 | #endif\r |
735 | }\r |
736 | }\r |
737 | return ret;\r |
738 | }\r |
739 | else\r |
740 | {\r |
741 | void *PmovFile = NULL;\r |
742 | if (strcmp(saveFname + strlen(saveFname) - 3, ".gz") == 0) {\r |
743 | if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) {\r |
744 | emu_setSaveStateCbs(1);\r |
745 | if(!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);\r |
746 | }\r |
747 | }\r |
748 | else\r |
749 | {\r |
750 | if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) {\r |
751 | emu_setSaveStateCbs(0);\r |
752 | }\r |
753 | }\r |
754 | if(PmovFile) {\r |
755 | ret = PmovState(load ? 6 : 5, PmovFile);\r |
756 | areaClose(PmovFile);\r |
757 | PmovFile = 0;\r |
758 | if (load) Pico.m.dirtyPal=1;\r |
759 | #ifndef NO_SYNC\r |
760 | else sync();\r |
761 | #endif\r |
762 | }\r |
763 | else ret = -1;\r |
764 | if (!ret)\r |
765 | strcpy(noticeMsg, load ? "GAME LOADED " : "GAME SAVED ");\r |
766 | else\r |
767 | {\r |
768 | strcpy(noticeMsg, load ? "LOAD FAILED " : "SAVE FAILED ");\r |
769 | ret = -1;\r |
770 | }\r |
771 | \r |
772 | emu_noticeMsgUpdated();\r |
773 | return ret;\r |
774 | }\r |
775 | }\r |