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.hw_filter),
67 void CleanSurface(void);
70 extern uint8 Exit; // exit emu loop flag
74 FCEUGI *fceugi = NULL;
75 int ntsccol=0,ntschue=-1,ntsctint=-1;
79 int srendlinev[2]={8,0};
80 int erendlinev[2]={231,239};
81 int srendline,erendline;
84 static char BaseDirectory[2048];
88 static void DriverKill(void);
89 static int DriverInitialize(void);
94 static void ParseGI(FCEUGI *gi)
98 InputType[0]=UsrInputType[0];
99 InputType[1]=UsrInputType[1];
100 InputTypeFC=UsrInputTypeFC;
103 InputType[0]=gi->input[0];
105 InputType[1]=gi->input[1];
107 InputTypeFC=gi->inputfc;
110 void FCEUD_PrintError(char *s)
116 void LoadCPalette(void)
121 if(!(fp=fopen(cpalette,"rb")))
123 printf(" Error loading custom palette from file: %s\n",cpalette);
128 fread(tmpp,1,192,fp);
129 FCEUI_SetPaletteArray((uint8 *)tmpp);
133 static CFGSTRUCT fceuconfig[]={
146 ADDCFGSTRUCT(DriverConfig),
150 static const char *skip_path(const char *path)
153 if (path == NULL) return NULL;
154 for (p = path+strlen(path)-1; p > path && *p != '/'; p--);
159 int SaveConfig(const char *llgn_path)
161 const char *name = skip_path(llgn_path);
167 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
168 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
169 printf("saving cfg to %s ... ", tdir); fflush(stdout);
170 FCEUI_GetNTSCTH(&ntsctint, &ntschue);
171 ret=SaveFCEUConfig(tdir,fceuconfig);
172 printf(ret == 0 ? "done\n" : "failed\n");
176 sprintf(tdir,"%s"PSS"cfg"PSS"%s_keys.cfg",BaseDirectory,name);
177 else sprintf(tdir,"%s"PSS"fceu_keys.cfg",BaseDirectory);
180 config_write_keys(f);
187 static int LoadConfig(const char *llgn_path)
189 const char *name = skip_path(llgn_path);
195 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
196 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
197 printf("loading cfg from %s ... ", tdir); fflush(stdout);
198 FCEUI_GetNTSCTH(&ntsctint, &ntschue); /* Get default settings for if no config file exists. */
199 ret=LoadFCEUConfig(tdir,fceuconfig);
200 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
201 printf(ret == 0 ? "done\n" : "failed\n");
205 sprintf(tdir,"%s"PSS"cfg"PSS"%s_keys.cfg",BaseDirectory,name);
206 else sprintf(tdir,"%s"PSS"fceu_keys.cfg",BaseDirectory);
209 l=fread(tdir,1,sizeof(tdir)-1,f);
211 config_read_keys(tdir);
218 static void LoadLLGN(void)
223 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
227 len = fread(lastLoadedGameName, 1, sizeof(lastLoadedGameName)-1, f);
228 lastLoadedGameName[len] = 0;
233 static void SaveLLGN(void)
235 // save last loaded game name
236 if (lastLoadedGameName[0] && !(eoptions&EO_NOAUTOWRITE))
240 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
244 fwrite(lastLoadedGameName, 1, strlen(lastLoadedGameName), f);
252 static void CreateDirs(void)
254 char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg","pal"};
258 mkdir(BaseDirectory,S_IRWXU);
259 for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
261 sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
266 static void SetSignals(void (*t)(int))
268 int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2};
274 static void CloseStuff(int signum)
277 printf("\nSignal %d has been caught and dealt with...\n",signum);
280 case SIGINT:printf("How DARE you interrupt me!\n");break;
281 case SIGTERM:printf("MUST TERMINATE ALL HUMANS\n");break;
282 case SIGHUP:printf("Reach out and hang-up on someone.\n");break;
283 case SIGPIPE:printf("The pipe has broken! Better watch out for floods...\n");break;
284 case SIGSEGV:printf("Iyeeeeeeeee!!! A segmentation fault has occurred. Have a fluffy day.\n");break;
286 case SIGBUS:printf("I told you to be nice to the driver.\n");break;
288 case SIGFPE:printf("Those darn floating points. Ne'er know when they'll bite!\n");break;
289 case SIGALRM:printf("Don't throw your clock at the meowing cats!\n");break;
290 case SIGABRT:printf("Abort, Retry, Ignore, Fail?\n");break;
292 case SIGUSR2:printf("Killing your processes is not nice.\n");break;
297 static int DoArgs(int argc, char *argv[])
299 static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
300 static int cortabi[5]={SI_NONE,SI_GAMEPAD,
301 SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
302 static char *fccortab[5]={"none","arkanoid","shadow","4player","fkb"};
303 static int fccortabi[5]={SIFC_NONE,SIFC_ARKANOID,SIFC_SHADOW,
304 SIFC_4PLAYER,SIFC_FKB};
307 static char *inputa[2]={0,0};
308 static char *fcexp=0;
309 static int docheckie[4];
311 static int docheckie2[2]={0,0};
314 static ARGPSTRUCT FCEUArgs[]={
315 {"-soundvol",0,&soundvol,0},
316 {"-cpalette",0,&cpalette,0x4001},
318 {"-ntsccol",0,&ntsccol,0},
319 {"-pal",&docheckie[0],0,0},
320 {"-input1",0,&inputa[0],0x4001},{"-input2",0,&inputa[1],0x4001},
321 {"-fcexp",0,&fcexp,0x4001},
323 {"-gg",0,&eoptions,0x8000|EO_GG},
324 {"-no8lim",0,&eoptions,0x8000|EO_NO8LIM},
325 {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME},
326 {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE},
327 {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES},
328 {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE},
329 {"-noautowrite",0,&eoptions,0x8000|EO_NOAUTOWRITE},
330 {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
331 {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
332 {"-sound",0,&Settings.sound_rate,0},
333 {"-showfps",0,&Settings.showfps,0},
335 {"-connect",&docheckie2[0],&netplayhost,0x4001},
336 {"-server",&docheckie2[1],0,0},
337 {"-netport",0,&Port,0},
339 {0,(void *)DriverArgs,0,0},
343 memset(docheckie,0,sizeof(docheckie));
344 ret=ParseArguments(argc, argv, FCEUArgs);
355 Settings.region_force=2;
356 FCEUI_SetGameGenie(eoptions&EO_GG);
357 FCEUI_DisableSpriteLimitation(eoptions&EO_NO8LIM);
358 FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
362 if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
363 if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
366 FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
367 FCEUI_SetSoundVolume(80);
371 else if(docheckie2[1])
375 FCEUI_SetNetworkPlay(netplay);
384 if(!strncmp(fccortab[y],fcexp,8))
386 UsrInputTypeFC=fccortabi[y];
401 if(!strncmp(cortab[y],inputa[x],8))
403 UsrInputType[x]=cortabi[y];
406 powerpadside&=~(1<<x);
407 powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
419 int main(int argc, char *argv[])
421 int last_arg_parsed, ret;
429 if(!DriverInitialize())
434 if(!FCEUI_Initialize())
436 GetBaseDirectory(BaseDirectory);
437 FCEUI_SetBaseDirectory(BaseDirectory);
438 lastLoadedGameName[0] = 0;
444 last_arg_parsed=DoArgs(argc-1,&argv[1]);
445 platform_late_init();
448 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
454 if (argc > 1 && !last_arg_parsed)
456 strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
457 lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
471 ret=LoadConfig(lastLoadedGameName);
476 FCEUI_SetEmuMode(Settings.accurate_mode);
477 fceugi=FCEUI_LoadGame(lastLoadedGameName);
481 if (Settings.region_force)
482 FCEUI_SetVidSystem(Settings.region_force - 1);
486 if ((eoptions&EO_GG) && geniestage == 0) {
487 strcpy(infostring, "gg.rom is missing, GG disabled");
489 FCEUI_SetGameGenie(0);
491 GameInterface(GI_INFOSTRING, infostring);
492 FCEU_DispMessage("%s", infostring);
496 switch(LoadGameLastError) {
497 default: menu_update_msg("failed to load ROM"); break;
498 case 2: menu_update_msg("Can't find a ROM for ips/movie"); break;
499 case 10: menu_update_msg("FDS BIOS ROM is missing, read docs"); break;
500 case 11: menu_update_msg("Error reading auxillary FDS file"); break;
508 if (ret == 1) break; // exit emu
509 if (ret == 2) { // reload ROM
517 FCEUI_GetCurrentVidSystem(&srendline,&erendline);
518 platform_apply_config();
521 RefreshThrottleFPS();
534 static int DriverInitialize(void)
536 SetSignals((void *)CloseStuff);
538 if(!InitVideo()) return 0;
543 static void DriverKill(void)
545 // SaveConfig(NULL); // done explicitly in menu now
548 if(cpalette) free(cpalette);
558 void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
560 FCEUD_UpdateInput(); // must update input before blitting because of save confirmation stuff
561 BlitPrepare(xbuf == NULL);
562 if(!(eoptions&EO_NOTHROTTLE))
565 WriteSound(Buffer,Count);
568 BlitScreen(xbuf == NULL);
569 // make sure last frame won't get skipped, because we need it for menu bg