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"
46 // TODO! broken fs0, sram saves
48 void CleanSurface(void);
51 extern char lastLoadedGameName[2048];
52 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)
96 static char *cpalette=0;
97 static void LoadCPalette(void)
102 if(!(fp=fopen(cpalette,"rb")))
104 printf(" Error loading custom palette from file: %s\n",cpalette);
107 fread(tmpp,1,192,fp);
108 FCEUI_SetPaletteArray((uint8 *)tmpp);
112 static CFGSTRUCT fceuconfig[]={
125 ADDCFGSTRUCT(DriverConfig),
129 void SaveConfig(const char *name)
133 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
134 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
135 FCEUI_GetNTSCTH(&ntsctint, &ntschue);
136 SaveFCEUConfig(tdir,fceuconfig);
139 static void LoadConfig(const char *name)
143 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
144 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
145 FCEUI_GetNTSCTH(&ntsctint, &ntschue); /* Get default settings for if no config file exists. */
146 LoadFCEUConfig(tdir,fceuconfig);
147 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
150 static void LoadLLGN(void)
155 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
159 len = fread(lastLoadedGameName, 1, sizeof(lastLoadedGameName)-1, f);
160 lastLoadedGameName[len] = 0;
165 static void SaveLLGN(void)
167 // save last loaded game name
168 if (lastLoadedGameName[0])
172 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
176 fwrite(lastLoadedGameName, 1, strlen(lastLoadedGameName), f);
184 static void CreateDirs(void)
186 char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg"};
190 mkdir(BaseDirectory,S_IRWXU);
191 for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
193 sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
198 static void SetSignals(void (*t)(int))
200 int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2};
206 static void CloseStuff(int signum)
209 printf("\nSignal %d has been caught and dealt with...\n",signum);
212 case SIGINT:printf("How DARE you interrupt me!\n");break;
213 case SIGTERM:printf("MUST TERMINATE ALL HUMANS\n");break;
214 case SIGHUP:printf("Reach out and hang-up on someone.\n");break;
215 case SIGPIPE:printf("The pipe has broken! Better watch out for floods...\n");break;
216 case SIGSEGV:printf("Iyeeeeeeeee!!! A segmentation fault has occurred. Have a fluffy day.\n");break;
217 /* So much SIGBUS evil. */
220 case SIGBUS:printf("I told you to be nice to the driver.\n");break;
223 case SIGFPE:printf("Those darn floating points. Ne'er know when they'll bite!\n");break;
224 case SIGALRM:printf("Don't throw your clock at the meowing cats!\n");break;
225 case SIGABRT:printf("Abort, Retry, Ignore, Fail?\n");break;
227 case SIGUSR2:printf("Killing your processes is not nice.\n");break;
232 static int DoArgs(int argc, char *argv[])
234 static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
235 static int cortabi[5]={SI_NONE,SI_GAMEPAD,
236 SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
237 static char *fccortab[5]={"none","arkanoid","shadow","4player","fkb"};
238 static int fccortabi[5]={SIFC_NONE,SIFC_ARKANOID,SIFC_SHADOW,
239 SIFC_4PLAYER,SIFC_FKB};
242 static char *inputa[2]={0,0};
243 static char *fcexp=0;
244 static int docheckie[4];
246 static ARGPSTRUCT FCEUArgs[]={
247 {"-soundvol",0,&soundvol,0},
248 {"-cpalette",0,&cpalette,0x4001},
250 {"-ntsccol",0,&ntsccol,0},
251 {"-pal",&docheckie[0],0,0},
252 {"-input1",0,&inputa[0],0x4001},{"-input2",0,&inputa[1],0x4001},
253 {"-fcexp",0,&fcexp,0x4001},
255 {"-gg",&docheckie[1],0,0},
256 {"-no8lim",0,&eoptions,0x8001},
257 {"-subase",0,&eoptions,0x8002},
258 {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME},
259 {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE},
260 {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES},
261 {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE},
262 {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
263 {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
264 {0,(void *)DriverArgs,0,0},
268 memset(docheckie,0,sizeof(docheckie));
269 ret=ParseArguments(argc, argv, FCEUArgs);
280 Settings.region_force=2;
282 FCEUI_SetGameGenie(1);
283 FCEUI_DisableSpriteLimitation(1);
284 FCEUI_SaveExtraDataUnderBase(eoptions&2);
285 FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
289 if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
290 if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
293 printf("FCEUI_SetRenderedLines: %d, %d, %d, %d\n",srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
294 printf("clip sides: %d\n", eoptions&EO_CLIPSIDES);
295 FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
296 FCEUI_SetSoundVolume(soundvol);
304 if(!strncmp(fccortab[y],fcexp,8))
306 UsrInputTypeFC=fccortabi[y];
321 if(!strncmp(cortab[y],inputa[x],8))
323 UsrInputType[x]=cortabi[y];
326 powerpadside&=~(1<<x);
327 powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
339 int CLImain(int argc, char *argv[])
348 if(!DriverInitialize())
353 if(!FCEUI_Initialize())
355 GetBaseDirectory(BaseDirectory);
356 FCEUI_SetBaseDirectory(BaseDirectory);
357 lastLoadedGameName[0] = 0;
361 last_arg_parsed=DoArgs(argc-1,&argv[1]);
363 gp2x_cpuclock_gamma_update();
365 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
367 LoadCPalette(); // TODO
371 if (argc > 1 && !last_arg_parsed)
373 strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
374 lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
388 fceugi=FCEUI_LoadGame(lastLoadedGameName);
392 LoadConfig(lastLoadedGameName);
393 if (Settings.region_force)
394 FCEUI_SetVidSystem(Settings.region_force - 1);
396 //RefreshThrottleFPS();
399 GameInterface(GI_INFOSTRING, infostring);
400 FCEU_DispMessage("%s", infostring);
404 switch(LoadGameLastError) {
405 default: strcpy(menuErrorMsg, "failed to load ROM"); break;
406 case 2: strcpy(menuErrorMsg, "Can't find a ROM for movie"); break;
407 case 10: strcpy(menuErrorMsg, "FDS BIOS ROM is missing, read docs"); break;
408 case 11: strcpy(menuErrorMsg, "Error reading auxillary FDS file"); break;
415 ret = gp2x_menu_do();
416 if (ret == 1) break; // exit emu
417 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(0, 320, 240); break;
428 case 1: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(0, 256, 240); break;
429 case 2: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(srendline, 256, erendline-srendline); break;
430 case 3: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(0, 320, 240); break;
433 gp2x_start_sound(Settings.sound_rate, 16, 0);
445 static int DriverInitialize(void)
447 SetSignals((void *)CloseStuff);
449 if(!InitVideo()) return 0;
454 static void DriverKill(void)
456 // SaveConfig(NULL); // done explicitly in menu now
466 void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
468 if(!Count && !(eoptions&EO_NOTHROTTLE))
471 if(Count && !(eoptions&EO_NOTHROTTLE))
472 WriteSound(Buffer,Count);