usb joys, custom pal
[fceu.git] / drivers / gp2x / main.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
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.
7  *
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.
12  *
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
16  */
17
18
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <signal.h>
22 #include <sys/time.h>
23 #include <sys/stat.h>
24 #include <string.h>
25 #include <strings.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include "main.h"
31 #include "minimal.h"
32 #include "throttle.h"
33 #include "menu.h"
34 #include "gp2x.h"
35
36 #include "../common/config.h"
37 #include "../common/args.h"
38 #include "../common/unixdsp.h"
39 #include "../common/cheat.h"
40
41 #include "../../fce.h"
42
43 #include "dface.h"
44
45
46 void CleanSurface(void);
47
48 // internals
49 extern char lastLoadedGameName[2048];
50 extern uint8 Exit; // exit emu loop flag
51 void CloseGame(void);
52
53 FCEUGI *fceugi = NULL;
54 int ntsccol=0,ntschue=-1,ntsctint=-1;
55 int soundvol=70;
56 int inited=0;
57
58 int srendlinev[2]={8,0};
59 int erendlinev[2]={231,239};
60 int srendline,erendline;
61
62
63 static char BaseDirectory[2048];
64
65 int eoptions=0;
66
67 static void DriverKill(void);
68 static int DriverInitialize(void);
69
70 static int gametype;
71 #include "input.c"
72
73 static void ParseGI(FCEUGI *gi)
74 {
75  gametype=gi->type;
76
77  InputType[0]=UsrInputType[0];
78  InputType[1]=UsrInputType[1];
79  InputTypeFC=UsrInputTypeFC;
80
81  if(gi->input[0]>=0)
82   InputType[0]=gi->input[0];
83  if(gi->input[1]>=0)
84   InputType[1]=gi->input[1];
85  if(gi->inputfc>=0)
86   InputTypeFC=gi->inputfc;
87 }
88
89 void FCEUD_PrintError(char *s)
90 {
91  puts(s);
92 }
93
94 char *cpalette=0;
95 void LoadCPalette(void)
96 {
97  char tmpp[192];
98  FILE *fp;
99
100  if(!(fp=fopen(cpalette,"rb")))
101  {
102   printf(" Error loading custom palette from file: %s\n",cpalette);
103   free(cpalette);
104   cpalette=0;
105   return;
106  }
107  fread(tmpp,1,192,fp);
108  FCEUI_SetPaletteArray((uint8 *)tmpp);
109  fclose(fp);
110 }
111
112 static CFGSTRUCT fceuconfig[]={
113         AC(soundvol),
114         ACS(cpalette),
115         AC(ntsctint),
116         AC(ntschue),
117         AC(ntsccol),
118         AC(UsrInputTypeFC),
119         ACA(UsrInputType),
120         AC(powerpadside),
121         AC(powerpadsc),
122         AC(eoptions),
123         ACA(srendlinev),
124         ACA(erendlinev),
125         ADDCFGSTRUCT(DriverConfig),
126         ENDCFGSTRUCT
127 };
128
129 void SaveConfig(const char *name)
130 {
131         char tdir[2048];
132         if (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);
137 }
138
139 static void LoadConfig(const char *name)
140 {
141         char tdir[2048];
142         if (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);
148 }
149
150 static void LoadLLGN(void)
151 {
152  char tdir[2048];
153  FILE *f;
154  int len;
155  sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
156  f=fopen(tdir, "r");
157  if(f)
158  {
159   len = fread(lastLoadedGameName, 1, sizeof(lastLoadedGameName)-1, f);
160   lastLoadedGameName[len] = 0;
161   fclose(f);
162  }
163 }
164
165 static void SaveLLGN(void)
166 {
167  // save last loaded game name
168  if (lastLoadedGameName[0])
169  {
170   char tdir[2048];
171   FILE *f;
172   sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
173   f=fopen(tdir, "w");
174   if(f)
175   {
176    fwrite(lastLoadedGameName, 1, strlen(lastLoadedGameName), f);
177    fclose(f);
178    sync();
179   }
180  }
181 }
182
183
184 static void CreateDirs(void)
185 {
186  char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg","pal"};
187  char tdir[2048];
188  int x;
189
190  mkdir(BaseDirectory,S_IRWXU);
191  for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
192  {
193   sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
194   mkdir(tdir,S_IRWXU);
195  }
196 }
197
198 static void SetSignals(void (*t)(int))
199 {
200   int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2};
201   int x;
202   for(x=0;x<11;x++)
203    signal(sigs[x],t);
204 }
205
206 static void CloseStuff(int signum)
207 {
208         DriverKill();
209         printf("\nSignal %d has been caught and dealt with...\n",signum);
210         switch(signum)
211         {
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          #if(SIGBUS!=SIGSEGV)
218          case SIGBUS:printf("I told you to be nice to the driver.\n");break;
219          #endif
220          case SIGFPE:printf("Those darn floating points.  Ne'er know when they'll bite!\n");break;
221          case SIGALRM:printf("Don't throw your clock at the meowing cats!\n");break;
222          case SIGABRT:printf("Abort, Retry, Ignore, Fail?\n");break;
223          case SIGUSR1:
224          case SIGUSR2:printf("Killing your processes is not nice.\n");break;
225         }
226         exit(1);
227 }
228
229 static int DoArgs(int argc, char *argv[])
230 {
231         static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
232         static int cortabi[5]={SI_NONE,SI_GAMEPAD,
233                                SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
234         static char *fccortab[5]={"none","arkanoid","shadow","4player","fkb"};
235         static int fccortabi[5]={SIFC_NONE,SIFC_ARKANOID,SIFC_SHADOW,
236                                  SIFC_4PLAYER,SIFC_FKB};
237
238         int x, ret;
239         static char *inputa[2]={0,0};
240         static char *fcexp=0;
241         static int docheckie[4];
242
243         static ARGPSTRUCT FCEUArgs[]={
244          {"-soundvol",0,&soundvol,0},
245          {"-cpalette",0,&cpalette,0x4001},
246
247          {"-ntsccol",0,&ntsccol,0},
248          {"-pal",&docheckie[0],0,0},
249          {"-input1",0,&inputa[0],0x4001},{"-input2",0,&inputa[1],0x4001},
250          {"-fcexp",0,&fcexp,0x4001},
251
252          {"-gg",&docheckie[1],0,0},
253          {"-no8lim",0,&eoptions,0x8000|EO_NO8LIM},
254          {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME},
255          {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE},
256          {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES},
257          {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE},
258          {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
259          {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
260          {0,(void *)DriverArgs,0,0},
261          {0,0,0,0}
262         };
263
264         memset(docheckie,0,sizeof(docheckie));
265         ret=ParseArguments(argc, argv, FCEUArgs);
266         if(cpalette)
267         {
268          if(cpalette[0]=='0')
269           if(cpalette[1]==0)
270           {
271            free(cpalette);
272            cpalette=0;
273           }
274         }
275         if(docheckie[0])
276          Settings.region_force=2;
277         if(docheckie[1])
278          FCEUI_SetGameGenie(1);
279         FCEUI_DisableSpriteLimitation(eoptions&EO_NO8LIM);
280         FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
281
282         for(x=0;x<2;x++)
283         {
284          if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
285          if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
286         }
287
288         FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
289         FCEUI_SetSoundVolume(soundvol);
290         DoDriverArgs();
291
292         if(fcexp)
293         {
294          int y;
295          for(y=0;y<5;y++)
296          {
297           if(!strncmp(fccortab[y],fcexp,8))
298           {
299            UsrInputTypeFC=fccortabi[y];
300            break;
301           }
302          }
303          free(fcexp);
304         }
305         for(x=0;x<2;x++)
306         {
307          int y;
308
309          if(!inputa[x])
310           continue;
311
312          for(y=0;y<5;y++)
313          {
314           if(!strncmp(cortab[y],inputa[x],8))
315           {
316            UsrInputType[x]=cortabi[y];
317            if(y==3)
318            {
319             powerpadside&=~(1<<x);
320             powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
321            }
322            free(inputa[x]);
323           }
324          }
325         }
326         return ret;
327 }
328
329
330 #include "usage.h"
331
332 int CLImain(int argc, char *argv[])
333 {
334         int last_arg_parsed;
335         /* TODO if(argc<=1)
336         {
337          ShowUsage(argv[0]);
338          return 1;
339         }*/
340
341         if(!DriverInitialize())
342         {
343          return 1;
344         }
345
346         if(!FCEUI_Initialize())
347          return(1);
348         GetBaseDirectory(BaseDirectory);
349         FCEUI_SetBaseDirectory(BaseDirectory);
350         lastLoadedGameName[0] = 0;
351
352         CreateDirs();
353         LoadConfig(NULL);
354         last_arg_parsed=DoArgs(argc-1,&argv[1]);
355         gp2x_opt_setup();
356         gp2x_cpuclock_gamma_update();
357         LoadLLGN();
358         FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
359         if(cpalette)
360          LoadCPalette();
361         if(InitSound())
362          inited|=1;
363
364         if (argc > 1 && !last_arg_parsed)
365         {
366          strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
367          lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
368          Exit = 0;
369         }
370         else
371         {
372          Exit = 1;
373         }
374
375         while (1)
376         {
377          if(!Exit)
378          {
379           if (fceugi)
380            CloseGame();
381           fceugi=FCEUI_LoadGame(lastLoadedGameName);
382           if (fceugi)
383           {
384            char infostring[32];
385            LoadConfig(lastLoadedGameName);
386            if (Settings.region_force)
387             FCEUI_SetVidSystem(Settings.region_force - 1);
388            ParseGI(fceugi);
389            InitOtherInput();
390
391            GameInterface(GI_INFOSTRING, infostring);
392            FCEU_DispMessage("%s", infostring);
393           }
394           else
395           {
396            switch(LoadGameLastError) {
397             default: strcpy(menuErrorMsg, "failed to load ROM"); break;
398             case  2: strcpy(menuErrorMsg, "Can't find a ROM for movie"); break;
399             case 10: strcpy(menuErrorMsg, "FDS BIOS ROM is missing, read docs"); break;
400             case 11: strcpy(menuErrorMsg, "Error reading auxillary FDS file"); break;
401            }
402           }
403          }
404          if(Exit || !fceugi)
405          {
406           int ret;
407           ret = gp2x_menu_do();
408           if (ret == 1) break;          // exit emu
409           if (ret == 2) {               // reload ROM
410            Exit = 0;
411            continue;
412           }
413          }
414
415          PrepareOtherInput();
416          RefreshThrottleFPS();
417          FCEUI_GetCurrentVidSystem(&srendline,&erendline);
418          gp2x_video_changemode(Settings.scaling == 3 ? 15 : 8);
419          switch (Settings.scaling & 3) {
420                  case 0: gp2x_video_set_offs(0);  gp2x_video_RGB_setscaling(0, 320, 240); break;
421                  case 1: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(0, 256, 240); break;
422                  case 2: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(srendline, 256, erendline-srendline); break;
423                  case 3: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(0, 320, 240); break;
424          }
425          CleanSurface();
426          gp2x_start_sound(Settings.sound_rate, 16, 0);
427          FCEUI_Emulate();
428         }
429
430         if (fceugi)
431          CloseGame();
432
433         SaveLLGN();
434         DriverKill();
435         return 0;
436 }
437
438 static int DriverInitialize(void)
439 {
440    SetSignals((void *)CloseStuff);
441
442    if(!InitVideo()) return 0;
443    inited|=4;
444    return 1;
445 }
446
447 static void DriverKill(void)
448 {
449  // SaveConfig(NULL); // done explicitly in menu now
450  SetSignals(SIG_IGN);
451
452  if(cpalette) free(cpalette);
453  cpalette=0;
454
455  if(inited&4)
456   KillVideo();
457  if(inited&1)
458   KillSound();
459  inited=0;
460 }
461
462 void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
463 {
464  if(!Count && !(eoptions&EO_NOTHROTTLE))
465   SpeedThrottle();
466  BlitScreen(xbuf);
467  if(Count && !(eoptions&EO_NOTHROTTLE))
468   WriteSound(Buffer,Count);
469  FCEUD_UpdateInput();
470 }
471