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>
36 #include "../common/config.h"
37 #include "../common/args.h"
38 #include "../common/unixdsp.h"
39 #include "../common/cheat.h"
41 #include "../../fce.h"
42 #include "../../cart.h"
47 void CleanSurface(void);
50 extern char lastLoadedGameName[2048];
51 extern uint8 Exit; // exit emu loop flag
55 FCEUGI *fceugi = NULL;
56 int ntsccol=0,ntschue=-1,ntsctint=-1;
60 int srendlinev[2]={8,0};
61 int erendlinev[2]={231,239};
62 int srendline,erendline;
65 static char BaseDirectory[2048];
69 static void DriverKill(void);
70 static int DriverInitialize(void);
75 static void ParseGI(FCEUGI *gi)
79 InputType[0]=UsrInputType[0];
80 InputType[1]=UsrInputType[1];
81 InputTypeFC=UsrInputTypeFC;
84 InputType[0]=gi->input[0];
86 InputType[1]=gi->input[1];
88 InputTypeFC=gi->inputfc;
91 void FCEUD_PrintError(char *s)
97 void LoadCPalette(void)
102 if(!(fp=fopen(cpalette,"rb")))
104 printf(" Error loading custom palette from file: %s\n",cpalette);
109 fread(tmpp,1,192,fp);
110 FCEUI_SetPaletteArray((uint8 *)tmpp);
114 static CFGSTRUCT fceuconfig[]={
127 ADDCFGSTRUCT(DriverConfig),
131 static const char *skip_path(const char *path)
134 if (path == NULL) return NULL;
135 for (p = path+strlen(path)-1; p > path && *p != '/'; p--);
140 int SaveConfig(const char *llgn_path)
142 const char *name = skip_path(llgn_path);
146 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
147 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
148 printf("saving cfg to %s ... ", tdir); fflush(stdout);
149 FCEUI_GetNTSCTH(&ntsctint, &ntschue);
150 ret=SaveFCEUConfig(tdir,fceuconfig);
151 printf(ret == 0 ? "done\n" : "failed\n");
155 static int LoadConfig(const char *llgn_path)
157 const char *name = skip_path(llgn_path);
161 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
162 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
163 printf("loading cfg from %s ... ", tdir); fflush(stdout);
164 FCEUI_GetNTSCTH(&ntsctint, &ntschue); /* Get default settings for if no config file exists. */
165 ret=LoadFCEUConfig(tdir,fceuconfig);
166 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
167 printf(ret == 0 ? "done\n" : "failed\n");
171 static void LoadLLGN(void)
176 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
180 len = fread(lastLoadedGameName, 1, sizeof(lastLoadedGameName)-1, f);
181 lastLoadedGameName[len] = 0;
186 static void SaveLLGN(void)
188 // save last loaded game name
189 if (lastLoadedGameName[0] && !(eoptions&EO_NOAUTOWRITE))
193 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
197 fwrite(lastLoadedGameName, 1, strlen(lastLoadedGameName), f);
205 static void CreateDirs(void)
207 char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg","pal"};
211 mkdir(BaseDirectory,S_IRWXU);
212 for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
214 sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
219 static void SetSignals(void (*t)(int))
221 int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2};
227 static void CloseStuff(int signum)
230 printf("\nSignal %d has been caught and dealt with...\n",signum);
233 case SIGINT:printf("How DARE you interrupt me!\n");break;
234 case SIGTERM:printf("MUST TERMINATE ALL HUMANS\n");break;
235 case SIGHUP:printf("Reach out and hang-up on someone.\n");break;
236 case SIGPIPE:printf("The pipe has broken! Better watch out for floods...\n");break;
237 case SIGSEGV:printf("Iyeeeeeeeee!!! A segmentation fault has occurred. Have a fluffy day.\n");break;
239 case SIGBUS:printf("I told you to be nice to the driver.\n");break;
241 case SIGFPE:printf("Those darn floating points. Ne'er know when they'll bite!\n");break;
242 case SIGALRM:printf("Don't throw your clock at the meowing cats!\n");break;
243 case SIGABRT:printf("Abort, Retry, Ignore, Fail?\n");break;
245 case SIGUSR2:printf("Killing your processes is not nice.\n");break;
250 static int DoArgs(int argc, char *argv[])
252 static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
253 static int cortabi[5]={SI_NONE,SI_GAMEPAD,
254 SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
255 static char *fccortab[5]={"none","arkanoid","shadow","4player","fkb"};
256 static int fccortabi[5]={SIFC_NONE,SIFC_ARKANOID,SIFC_SHADOW,
257 SIFC_4PLAYER,SIFC_FKB};
260 static char *inputa[2]={0,0};
261 static char *fcexp=0;
262 static int docheckie[4];
264 static ARGPSTRUCT FCEUArgs[]={
265 {"-soundvol",0,&soundvol,0},
266 {"-cpalette",0,&cpalette,0x4001},
268 {"-ntsccol",0,&ntsccol,0},
269 {"-pal",&docheckie[0],0,0},
270 {"-input1",0,&inputa[0],0x4001},{"-input2",0,&inputa[1],0x4001},
271 {"-fcexp",0,&fcexp,0x4001},
273 {"-gg",0,&eoptions,0x8000|EO_GG},
274 {"-no8lim",0,&eoptions,0x8000|EO_NO8LIM},
275 {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME},
276 {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE},
277 {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES},
278 {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE},
279 {"-noautowrite",0,&eoptions,0x8000|EO_NOAUTOWRITE},
280 {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
281 {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
282 {0,(void *)DriverArgs,0,0},
286 memset(docheckie,0,sizeof(docheckie));
287 ret=ParseArguments(argc, argv, FCEUArgs);
298 Settings.region_force=2;
299 FCEUI_SetGameGenie(eoptions&EO_GG);
300 FCEUI_DisableSpriteLimitation(eoptions&EO_NO8LIM);
301 FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
305 if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
306 if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
309 FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
310 FCEUI_SetSoundVolume(80);
318 if(!strncmp(fccortab[y],fcexp,8))
320 UsrInputTypeFC=fccortabi[y];
335 if(!strncmp(cortab[y],inputa[x],8))
337 UsrInputType[x]=cortabi[y];
340 powerpadside&=~(1<<x);
341 powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
353 int CLImain(int argc, char *argv[])
355 int last_arg_parsed, ret;
362 if(!DriverInitialize())
367 if(!FCEUI_Initialize())
369 GetBaseDirectory(BaseDirectory);
370 FCEUI_SetBaseDirectory(BaseDirectory);
371 lastLoadedGameName[0] = 0;
375 last_arg_parsed=DoArgs(argc-1,&argv[1]);
378 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
384 if (argc > 1 && !last_arg_parsed)
386 strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
387 lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
401 ret=LoadConfig(lastLoadedGameName);
406 FCEUI_SetEmuMode(Settings.accurate_mode);
407 fceugi=FCEUI_LoadGame(lastLoadedGameName);
411 if (Settings.region_force)
412 FCEUI_SetVidSystem(Settings.region_force - 1);
416 if ((eoptions&EO_GG) && geniestage == 0) {
417 strcpy(infostring, "gg.rom is missing, GG disabled");
419 FCEUI_SetGameGenie(0);
421 GameInterface(GI_INFOSTRING, infostring);
422 FCEU_DispMessage("%s", infostring);
426 switch(LoadGameLastError) {
427 default: strcpy(menuErrorMsg, "failed to load ROM"); break;
428 case 2: strcpy(menuErrorMsg, "Can't find a ROM for ips/movie"); break;
429 case 10: strcpy(menuErrorMsg, "FDS BIOS ROM is missing, read docs"); break;
430 case 11: strcpy(menuErrorMsg, "Error reading auxillary FDS file"); break;
437 ret = gp2x_menu_do();
438 if (ret == 1) break; // exit emu
439 if (ret == 2) { // reload ROM
447 FCEUI_GetCurrentVidSystem(&srendline,&erendline);
448 gp2x_video_changemode(Settings.scaling == 3 ? 15 : 8);
449 switch (Settings.scaling & 3) {
450 case 0: gp2x_video_set_offs(0); gp2x_video_RGB_setscaling(320, 240); break;
451 case 1: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(256, 240); break;
452 case 2: gp2x_video_set_offs(32+srendline*320); gp2x_video_RGB_setscaling(256, erendline-srendline); break;
453 case 3: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(320, 240); break;
456 gp2x_start_sound(Settings.sound_rate, 16, 0);
457 RefreshThrottleFPS();
470 static int DriverInitialize(void)
472 SetSignals((void *)CloseStuff);
474 if(!InitVideo()) return 0;
479 static void DriverKill(void)
481 // SaveConfig(NULL); // done explicitly in menu now
484 if(cpalette) free(cpalette);
494 void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
496 FCEUD_UpdateInput(); // must update input before blitting because of save confirmation stuff
497 BlitPrepare(xbuf == NULL);
498 if(!(eoptions&EO_NOTHROTTLE))
501 WriteSound(Buffer,Count);
504 BlitScreen(xbuf == NULL);
505 // make sure last frame won't get skipped, because we need it for menu bg