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 void SaveConfig(const char *name)
135 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
136 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
137 FCEUI_GetNTSCTH(&ntsctint, &ntschue);
138 SaveFCEUConfig(tdir,fceuconfig);
141 static void LoadConfig(const char *name)
145 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
146 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
147 FCEUI_GetNTSCTH(&ntsctint, &ntschue); /* Get default settings for if no config file exists. */
148 LoadFCEUConfig(tdir,fceuconfig);
149 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
152 static void LoadLLGN(void)
157 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
161 len = fread(lastLoadedGameName, 1, sizeof(lastLoadedGameName)-1, f);
162 lastLoadedGameName[len] = 0;
167 static void SaveLLGN(void)
169 // save last loaded game name
170 if (lastLoadedGameName[0] && !(eoptions&EO_NOAUTOWRITE))
174 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
178 fwrite(lastLoadedGameName, 1, strlen(lastLoadedGameName), f);
186 static void CreateDirs(void)
188 char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg","pal"};
192 mkdir(BaseDirectory,S_IRWXU);
193 for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
195 sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
200 static void SetSignals(void (*t)(int))
202 int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2};
208 static void CloseStuff(int signum)
211 printf("\nSignal %d has been caught and dealt with...\n",signum);
214 case SIGINT:printf("How DARE you interrupt me!\n");break;
215 case SIGTERM:printf("MUST TERMINATE ALL HUMANS\n");break;
216 case SIGHUP:printf("Reach out and hang-up on someone.\n");break;
217 case SIGPIPE:printf("The pipe has broken! Better watch out for floods...\n");break;
218 case SIGSEGV:printf("Iyeeeeeeeee!!! A segmentation fault has occurred. Have a fluffy day.\n");break;
220 case SIGBUS:printf("I told you to be nice to the driver.\n");break;
222 case SIGFPE:printf("Those darn floating points. Ne'er know when they'll bite!\n");break;
223 case SIGALRM:printf("Don't throw your clock at the meowing cats!\n");break;
224 case SIGABRT:printf("Abort, Retry, Ignore, Fail?\n");break;
226 case SIGUSR2:printf("Killing your processes is not nice.\n");break;
231 static int DoArgs(int argc, char *argv[])
233 static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
234 static int cortabi[5]={SI_NONE,SI_GAMEPAD,
235 SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
236 static char *fccortab[5]={"none","arkanoid","shadow","4player","fkb"};
237 static int fccortabi[5]={SIFC_NONE,SIFC_ARKANOID,SIFC_SHADOW,
238 SIFC_4PLAYER,SIFC_FKB};
241 static char *inputa[2]={0,0};
242 static char *fcexp=0;
243 static int docheckie[4];
245 static ARGPSTRUCT FCEUArgs[]={
246 {"-soundvol",0,&soundvol,0},
247 {"-cpalette",0,&cpalette,0x4001},
249 {"-ntsccol",0,&ntsccol,0},
250 {"-pal",&docheckie[0],0,0},
251 {"-input1",0,&inputa[0],0x4001},{"-input2",0,&inputa[1],0x4001},
252 {"-fcexp",0,&fcexp,0x4001},
254 {"-gg",0,&eoptions,0x8000|EO_GG},
255 {"-no8lim",0,&eoptions,0x8000|EO_NO8LIM},
256 {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME},
257 {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE},
258 {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES},
259 {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE},
260 {"-noautowrite",0,&eoptions,0x8000|EO_NOAUTOWRITE},
261 {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
262 {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
263 {0,(void *)DriverArgs,0,0},
267 memset(docheckie,0,sizeof(docheckie));
268 ret=ParseArguments(argc, argv, FCEUArgs);
279 Settings.region_force=2;
280 FCEUI_SetGameGenie(eoptions&EO_GG);
281 FCEUI_DisableSpriteLimitation(eoptions&EO_NO8LIM);
282 FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
286 if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
287 if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
290 FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
291 FCEUI_SetSoundVolume(80);
299 if(!strncmp(fccortab[y],fcexp,8))
301 UsrInputTypeFC=fccortabi[y];
316 if(!strncmp(cortab[y],inputa[x],8))
318 UsrInputType[x]=cortabi[y];
321 powerpadside&=~(1<<x);
322 powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
334 int CLImain(int argc, char *argv[])
343 if(!DriverInitialize())
348 if(!FCEUI_Initialize())
350 GetBaseDirectory(BaseDirectory);
351 FCEUI_SetBaseDirectory(BaseDirectory);
352 lastLoadedGameName[0] = 0;
356 last_arg_parsed=DoArgs(argc-1,&argv[1]);
359 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
365 if (argc > 1 && !last_arg_parsed)
367 strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
368 lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
382 LoadConfig(lastLoadedGameName);
383 FCEUI_SetEmuMode(Settings.accurate_mode);
384 fceugi=FCEUI_LoadGame(lastLoadedGameName);
388 if (Settings.region_force)
389 FCEUI_SetVidSystem(Settings.region_force - 1);
393 if ((eoptions&EO_GG) && geniestage == 0) {
394 strcpy(infostring, "gg.rom is missing, GG disabled");
396 FCEUI_SetGameGenie(0);
398 GameInterface(GI_INFOSTRING, infostring);
399 FCEU_DispMessage("%s", infostring);
403 switch(LoadGameLastError) {
404 default: strcpy(menuErrorMsg, "failed to load ROM"); break;
405 case 2: strcpy(menuErrorMsg, "Can't find a ROM for ips/movie"); break;
406 case 10: strcpy(menuErrorMsg, "FDS BIOS ROM is missing, read docs"); break;
407 case 11: strcpy(menuErrorMsg, "Error reading auxillary FDS file"); break;
414 ret = gp2x_menu_do();
415 if (ret == 1) break; // exit emu
416 if (ret == 2) { // reload ROM
424 FCEUI_GetCurrentVidSystem(&srendline,&erendline);
425 gp2x_video_changemode(Settings.scaling == 3 ? 15 : 8);
426 switch (Settings.scaling & 3) {
427 case 0: gp2x_video_set_offs(0); gp2x_video_RGB_setscaling(320, 240); break;
428 case 1: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(256, 240); break;
429 case 2: gp2x_video_set_offs(32+srendline*320); gp2x_video_RGB_setscaling(256, erendline-srendline); break;
430 case 3: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(320, 240); break;
433 gp2x_start_sound(Settings.sound_rate, 16, 0);
434 RefreshThrottleFPS();
447 static int DriverInitialize(void)
449 SetSignals((void *)CloseStuff);
451 if(!InitVideo()) return 0;
456 static void DriverKill(void)
458 // SaveConfig(NULL); // done explicitly in menu now
461 if(cpalette) free(cpalette);
471 void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
473 FCEUD_UpdateInput(); // must update input before blitting because of save confirmation stuff
474 BlitPrepare(xbuf == NULL);
475 if(!(eoptions&EO_NOTHROTTLE))
478 WriteSound(Buffer,Count);
481 BlitScreen(xbuf == NULL);
482 // make sure last frame won't get skipped, because we need it for menu bg