1 /* FCE Ultra - NES/Famicom Emulator
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <sys/types.h>
40 #include "../libpicofe/config_file.h"
42 #include "../../fce.h"
43 #include "../../cart.h"
46 // these are now here to try to make compatible configs
47 // between different versions of the emu
49 CFGSTRUCT DriverConfig[]={
50 AC(Settings.turbo_rate_add),
51 AC(Settings.sound_rate),
54 AC(Settings.frameskip),
55 AC(Settings.sstate_confirm),
56 AC(Settings.region_force),
57 AC(Settings.cpuclock),
59 AC(Settings.ramtimings),
61 AC(Settings.perfect_vsync),
62 AC(Settings.accurate_mode),
63 AC(Settings.sw_filter),
64 AC(Settings.hw_filter),
68 void CleanSurface(void);
71 extern uint8 Exit; // exit emu loop flag
75 FCEUGI *fceugi = NULL;
76 int ntsccol=0,ntschue=-1,ntsctint=-1;
80 int srendlinev[2]={8,0};
81 int erendlinev[2]={231,239};
82 int srendline,erendline;
85 static char BaseDirectory[2048];
89 static void DriverKill(void);
90 static int DriverInitialize(void);
95 static void ParseGI(FCEUGI *gi)
99 InputType[0]=UsrInputType[0];
100 InputType[1]=UsrInputType[1];
101 InputTypeFC=UsrInputTypeFC;
104 InputType[0]=gi->input[0];
106 InputType[1]=gi->input[1];
108 InputTypeFC=gi->inputfc;
111 void FCEUD_PrintError(char *s)
117 void LoadCPalette(void)
122 if(!(fp=fopen(cpalette,"rb")))
124 printf(" Error loading custom palette from file: %s\n",cpalette);
129 fread(tmpp,1,192,fp);
130 FCEUI_SetPaletteArray((uint8 *)tmpp);
134 static CFGSTRUCT fceuconfig[]={
147 ADDCFGSTRUCT(DriverConfig),
151 static const char *skip_path(const char *path)
154 if (path == NULL) return NULL;
155 for (p = path+strlen(path)-1; p > path && *p != '/'; p--);
160 int SaveConfig(const char *llgn_path)
162 const char *name = skip_path(llgn_path);
168 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
169 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
170 printf("saving cfg to %s ... ", tdir); fflush(stdout);
171 FCEUI_GetNTSCTH(&ntsctint, &ntschue);
172 ret=SaveFCEUConfig(tdir,fceuconfig);
173 printf(ret == 0 ? "done\n" : "failed\n");
177 sprintf(tdir,"%s"PSS"cfg"PSS"%s_keys.cfg",BaseDirectory,name);
178 else sprintf(tdir,"%s"PSS"fceu_keys.cfg",BaseDirectory);
181 config_write_keys(f);
188 static void LoadKeys(const char *llgn_path)
190 const char *name = skip_path(llgn_path);
196 sprintf(tdir,"%s"PSS"cfg"PSS"%s_keys.cfg",BaseDirectory,name);
197 else sprintf(tdir,"%s"PSS"fceu_keys.cfg",BaseDirectory);
200 l=fread(tdir,1,sizeof(tdir)-1,f);
202 config_read_keys(tdir);
207 static int LoadConfig(const char *llgn_path)
209 const char *name = skip_path(llgn_path);
214 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
215 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
216 printf("loading cfg from %s ... ", tdir); fflush(stdout);
217 FCEUI_GetNTSCTH(&ntsctint, &ntschue); /* Get default settings for if no config file exists. */
218 ret=LoadFCEUConfig(tdir,fceuconfig);
219 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
220 printf(ret == 0 ? "done\n" : "failed\n");
227 static void LoadLLGN(void)
232 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
236 len = fread(lastLoadedGameName, 1, sizeof(lastLoadedGameName)-1, f);
237 lastLoadedGameName[len] = 0;
242 platform_get_def_rompath(lastLoadedGameName, sizeof(lastLoadedGameName));
246 static void SaveLLGN(void)
248 // save last loaded game name
249 if (lastLoadedGameName[0] && !(eoptions&EO_NOAUTOWRITE))
253 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
257 fwrite(lastLoadedGameName, 1, strlen(lastLoadedGameName), f);
265 static void CreateDirs(void)
267 char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg","pal"};
271 mkdir(BaseDirectory,S_IRWXU);
272 for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
274 sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
279 static void SetSignals(void (*t)(int))
281 int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2};
287 static void CloseStuff(int signum)
290 printf("\nSignal %d has been caught and dealt with...\n",signum);
293 case SIGINT:printf("How DARE you interrupt me!\n");break;
294 case SIGTERM:printf("MUST TERMINATE ALL HUMANS\n");break;
295 case SIGHUP:printf("Reach out and hang-up on someone.\n");break;
296 case SIGPIPE:printf("The pipe has broken! Better watch out for floods...\n");break;
297 case SIGSEGV:printf("Iyeeeeeeeee!!! A segmentation fault has occurred. Have a fluffy day.\n");break;
299 case SIGBUS:printf("I told you to be nice to the driver.\n");break;
301 case SIGFPE:printf("Those darn floating points. Ne'er know when they'll bite!\n");break;
302 case SIGALRM:printf("Don't throw your clock at the meowing cats!\n");break;
303 case SIGABRT:printf("Abort, Retry, Ignore, Fail?\n");break;
305 case SIGUSR2:printf("Killing your processes is not nice.\n");break;
310 static int DoArgs(int argc, char *argv[])
312 static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
313 static int cortabi[5]={SI_NONE,SI_GAMEPAD,
314 SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
315 static char *fccortab[5]={"none","arkanoid","shadow","4player","fkb"};
316 static int fccortabi[5]={SIFC_NONE,SIFC_ARKANOID,SIFC_SHADOW,
317 SIFC_4PLAYER,SIFC_FKB};
320 static char *inputa[2]={0,0};
321 static char *fcexp=0;
322 static int docheckie[4];
324 static int docheckie2[2]={0,0};
327 static ARGPSTRUCT FCEUArgs[]={
328 {"-soundvol",0,&soundvol,0},
329 {"-cpalette",0,&cpalette,0x4001},
331 {"-ntsccol",0,&ntsccol,0},
332 {"-pal",&docheckie[0],0,0},
333 {"-input1",0,&inputa[0],0x4001},{"-input2",0,&inputa[1],0x4001},
334 {"-fcexp",0,&fcexp,0x4001},
336 {"-gg",0,&eoptions,0x8000|EO_GG},
337 {"-no8lim",0,&eoptions,0x8000|EO_NO8LIM},
338 {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME},
339 {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE},
340 {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES},
341 {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE},
342 {"-noautowrite",0,&eoptions,0x8000|EO_NOAUTOWRITE},
343 {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
344 {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
345 {"-sound",0,&Settings.sound_rate,0},
346 {"-showfps",0,&Settings.showfps,0},
348 {"-connect",&docheckie2[0],&netplayhost,0x4001},
349 {"-server",&docheckie2[1],0,0},
350 {"-netport",0,&Port,0},
352 {0,(void *)DriverArgs,0,0},
356 memset(docheckie,0,sizeof(docheckie));
357 ret=ParseArguments(argc, argv, FCEUArgs);
368 Settings.region_force=2;
369 FCEUI_SetGameGenie(eoptions&EO_GG);
370 FCEUI_DisableSpriteLimitation(eoptions&EO_NO8LIM);
371 FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
375 if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
376 if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
379 FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
380 FCEUI_SetSoundVolume(80);
384 else if(docheckie2[1])
388 FCEUI_SetNetworkPlay(netplay);
397 if(!strncmp(fccortab[y],fcexp,8))
399 UsrInputTypeFC=fccortabi[y];
414 if(!strncmp(cortab[y],inputa[x],8))
416 UsrInputType[x]=cortabi[y];
419 powerpadside&=~(1<<x);
420 powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
432 int main(int argc, char *argv[])
434 int last_arg_parsed, ret;
442 if(!DriverInitialize())
447 if(!FCEUI_Initialize())
449 GetBaseDirectory(BaseDirectory);
450 FCEUI_SetBaseDirectory(BaseDirectory);
451 lastLoadedGameName[0] = 0;
457 last_arg_parsed=DoArgs(argc-1,&argv[1]);
458 platform_late_init();
462 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
468 if (argc > 1 && !last_arg_parsed)
470 strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
471 lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
485 ret=LoadConfig(lastLoadedGameName);
490 FCEUI_SetEmuMode(Settings.accurate_mode);
491 fceugi=FCEUI_LoadGame(lastLoadedGameName);
495 if (Settings.region_force)
496 FCEUI_SetVidSystem(Settings.region_force - 1);
500 if ((eoptions&EO_GG) && geniestage == 0) {
501 strcpy(infostring, "gg.rom is missing, GG disabled");
503 FCEUI_SetGameGenie(0);
505 GameInterface(GI_INFOSTRING, infostring);
506 FCEU_DispMessage("%s", infostring);
510 switch(LoadGameLastError) {
511 default: menu_update_msg("failed to load ROM"); break;
512 case 2: menu_update_msg("Can't find a ROM for ips/movie"); break;
513 case 10: menu_update_msg("FDS BIOS ROM is missing, read docs"); break;
514 case 11: menu_update_msg("Error reading auxillary FDS file"); break;
522 if (ret == 1) break; // exit emu
523 if (ret == 2) { // reload ROM
531 FCEUI_GetCurrentVidSystem(&srendline,&erendline);
532 platform_apply_config();
535 RefreshThrottleFPS();
548 static int DriverInitialize(void)
550 SetSignals((void *)CloseStuff);
552 if(!InitVideo()) return 0;
557 static void DriverKill(void)
559 // SaveConfig(NULL); // done explicitly in menu now
562 if(cpalette) free(cpalette);
572 void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
574 FCEUD_UpdateInput(); // must update input before blitting because of save confirmation stuff
575 BlitPrepare(xbuf == NULL);
576 if(!(eoptions&EO_NOTHROTTLE))
579 WriteSound(Buffer,Count);
582 BlitScreen(xbuf == NULL);
583 // make sure last frame won't get skipped, because we need it for menu bg