1 /* FCE Ultra - NES/Famicom Emulator
3 * Copyright notice for this file:
4 * Copyright (C) 2002 Ben Parnell
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /* This file contains or coordinates all of the code necessary to compile
22 on a UNIX system that can use svgalib, such as FreeBSD and Linux.
23 This code is not guaranteed to compile on FreeBSD, though.
28 #include <sys/types.h>
43 #include "../common/config.h"
44 #include "../common/args.h"
45 #include "../common/unixdsp.h"
46 #include "../common/cheat.h"
50 // just for printing some iNES info for user..
51 #include "../../fce.h"
52 #include "../../ines.h"
55 extern char lastLoadedGameName[2048];
56 extern uint8 Exit; // exit emu loop flag
59 FCEUGI *fceugi = NULL;
60 static int ntsccol=0,ntschue=-1,ntsctint=-1;
66 int srendlinev[2]={0,0};
67 int erendlinev[2]={239,239};
68 int srendline,erendline;
71 static char BaseDirectory[2048];
75 static void DriverKill(void);
76 static int DriverInitialize(void);
81 static void ParseGI(FCEUGI *gi)
85 InputType[0]=UsrInputType[0];
86 InputType[1]=UsrInputType[1];
87 InputTypeFC=UsrInputTypeFC;
90 InputType[0]=gi->input[0];
92 InputType[1]=gi->input[1];
94 InputTypeFC=gi->inputfc;
95 FCEUI_GetCurrentVidSystem(&srendline,&erendline);
98 void FCEUD_PrintError(char *s)
103 static char *cpalette=0;
104 static void LoadCPalette(void)
109 if(!(fp=fopen(cpalette,"rb")))
111 printf(" Error loading custom palette from file: %s\n",cpalette);
114 fread(tmpp,1,192,fp);
115 FCEUI_SetPaletteArray((uint8 *)tmpp);
119 static CFGSTRUCT fceuconfig[]={
132 ACA(lastLoadedGameName),
133 ADDCFGSTRUCT(DriverConfig),
137 static void SaveConfig(void)
140 sprintf(tdir,"%s"PSS"fceu.cfg",BaseDirectory);
141 DriverInterface(DES_GETNTSCTINT,&ntsctint);
142 DriverInterface(DES_GETNTSCHUE,&ntschue);
143 SaveFCEUConfig(tdir,fceuconfig);
146 static void LoadConfig(void)
149 sprintf(tdir,"%s"PSS"fceu.cfg",BaseDirectory);
150 LoadFCEUConfig(tdir,fceuconfig);
151 if(ntsctint>=0) DriverInterface(DES_SETNTSCTINT,&ntsctint);
152 if(ntschue>=0) DriverInterface(DES_SETNTSCHUE,&ntschue);
155 static void CreateDirs(void)
157 char *subs[5]={"fcs","snaps","gameinfo","sav","cheats"};
161 mkdir(BaseDirectory,S_IRWXU);
164 sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
169 static void SetSignals(void (*t)(int))
171 int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2};
177 static void CloseStuff(int signum)
180 printf("\nSignal %d has been caught and dealt with...\n",signum);
183 case SIGINT:printf("How DARE you interrupt me!\n");break;
184 case SIGTERM:printf("MUST TERMINATE ALL HUMANS\n");break;
185 case SIGHUP:printf("Reach out and hang-up on someone.\n");break;
186 case SIGPIPE:printf("The pipe has broken! Better watch out for floods...\n");break;
187 case SIGSEGV:printf("Iyeeeeeeeee!!! A segmentation fault has occurred. Have a fluffy day.\n");break;
188 /* So much SIGBUS evil. */
191 case SIGBUS:printf("I told you to be nice to the driver.\n");break;
194 case SIGFPE:printf("Those darn floating points. Ne'er know when they'll bite!\n");break;
195 case SIGALRM:printf("Don't throw your clock at the meowing cats!\n");break;
196 case SIGABRT:printf("Abort, Retry, Ignore, Fail?\n");break;
198 case SIGUSR2:printf("Killing your processes is not nice.\n");break;
203 static int DoArgs(int argc, char *argv[])
205 static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
206 static int cortabi[5]={SI_NONE,SI_GAMEPAD,
207 SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
208 static char *fccortab[5]={"none","arkanoid","shadow","4player","fkb"};
209 static int fccortabi[5]={SIFC_NONE,SIFC_ARKANOID,SIFC_SHADOW,
210 SIFC_4PLAYER,SIFC_FKB};
213 static char *inputa[2]={0,0};
214 static char *fcexp=0;
215 static int docheckie[4];
217 static ARGPSTRUCT FCEUArgs[]={
218 {"-soundvol",0,&soundvol,0},
219 {"-cpalette",0,&cpalette,0x4001},
221 {"-ntsccol",0,&ntsccol,0},
222 {"-pal",&docheckie[0],0,0},
223 {"-input1",0,&inputa[0],0x4001},{"-input2",0,&inputa[1],0x4001},
224 {"-fcexp",0,&fcexp,0x4001},
226 {"-gg",&docheckie[1],0,0},
227 {"-no8lim",0,&eoptions,0x8001},
228 {"-subase",0,&eoptions,0x8002},
229 {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME},
230 {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE},
231 {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES},
232 {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE},
233 {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
234 {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
235 {"-swapbuttons",&swapbuttons, 0, 0},
236 {"-showfps",&showfps, 0, 0},
237 {0,(void *)DriverArgs,0,0},
241 memset(docheckie,0,sizeof(docheckie));
242 ret=ParseArguments(argc, argv, FCEUArgs);
253 FCEUI_SetVidSystem(1);
255 FCEUI_SetGameGenie(1);
256 FCEUI_DisableSpriteLimitation(1);
257 FCEUI_SaveExtraDataUnderBase(eoptions&2);
258 FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
262 if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
263 if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
266 printf("main() setrendered lines: %d, %d, %d, %d\n",srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
267 printf("main() clip sides %d\n", eoptions&EO_CLIPSIDES);
269 FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
270 FCEUI_SetRenderedLines(0,erendlinev[0],srendlinev[1],erendlinev[1]);
271 FCEUI_SetSoundVolume(soundvol);
272 DriverInterface(DES_NTSCCOL,&ntsccol);
280 if(!strncmp(fccortab[y],fcexp,8))
282 UsrInputTypeFC=fccortabi[y];
297 if(!strncmp(cortab[y],inputa[x],8))
299 UsrInputType[x]=cortabi[y];
302 powerpadside&=~(1<<x);
303 powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
314 int CLImain(int argc, char *argv[])
323 if(!DriverInitialize())
328 if(!FCEUI_Initialize())
330 GetBaseDirectory(BaseDirectory);
331 FCEUI_SetBaseDirectory(BaseDirectory);
332 lastLoadedGameName[0] = 0;
337 last_arg_parsed=DoArgs(argc-1,&argv[1]);
343 if (argc > 1 && !last_arg_parsed)
345 strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
346 lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
351 lastLoadedGameName[0] = 0;
361 fceugi=FCEUI_LoadGame(lastLoadedGameName);
365 //RefreshThrottleFPS();
368 // additional print for gpfce
369 // TODO: handlers for other formats then iNES
372 iNES_HEADER *head = iNESGetHead();
373 MapperNo = (head->ROM_type>>4);
374 MapperNo|=(head->ROM_type2&0xF0);
375 FCEU_DispMessage("%s, Mapper: %d%s%s", PAL?"PAL":"NTSC", MapperNo, (head->ROM_type&2)?", BB":"", (head->ROM_type&4)?", T":"");
379 strcpy(menuErrorMsg, "failed to load ROM");
384 ret = gp2x_menu_do();
385 if (ret == 1) break; // exit emu
386 if (ret == 2) { // reload ROM
392 gp2x_video_changemode(Settings.scaling == 3 ? 15 : 8);
393 gp2x_video_RGB_setscaling(0, 320, 240);
394 gp2x_start_sound(22050, 16, 0);
402 static int DriverInitialize(void)
404 SetSignals((void *)CloseStuff);
408 if(!InitVideo()) return 0;
410 if(!InitKeyboard()) return 0;
415 static void DriverKill(void)
433 void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
435 if(!Count && !NoWaiting && !(eoptions&EO_NOTHROTTLE))
438 if(Count && !NoWaiting && !(eoptions&EO_NOTHROTTLE))
439 WriteSound(Buffer,Count);