mappers updated to 0.98.16
[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 "dface.h"
42
43 // just for printing some iNES info for user..
44 #include "../../fce.h"
45 #include "../../ines.h"
46
47 // internals
48 extern char lastLoadedGameName[2048];
49 extern uint8 Exit; // exit emu loop flag
50 void CloseGame(void);
51
52 FCEUGI *fceugi = NULL;
53 static int ntsccol=0,ntschue=-1,ntsctint=-1;
54 int soundvol=70;
55 int inited=0;
56
57 int srendlinev[2]={0,0};
58 int erendlinev[2]={239,239};
59 int srendline,erendline;
60
61
62 static char BaseDirectory[2048];
63
64 int eoptions=0;
65
66 static void DriverKill(void);
67 static int DriverInitialize(void);
68
69 static int gametype;
70 #include "input.c"
71
72 static void ParseGI(FCEUGI *gi)
73 {
74  gametype=gi->type;
75
76  InputType[0]=UsrInputType[0];
77  InputType[1]=UsrInputType[1];
78  InputTypeFC=UsrInputTypeFC;
79
80  if(gi->input[0]>=0)
81   InputType[0]=gi->input[0];
82  if(gi->input[1]>=0)
83   InputType[1]=gi->input[1];
84  if(gi->inputfc>=0)
85   InputTypeFC=gi->inputfc;
86  FCEUI_GetCurrentVidSystem(&srendline,&erendline);
87 }
88
89 void FCEUD_PrintError(char *s)
90 {
91  puts(s);
92 }
93
94 static char *cpalette=0;
95 static 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   return;
104  }
105  fread(tmpp,1,192,fp);
106  FCEUI_SetPaletteArray((uint8 *)tmpp);
107  fclose(fp);
108 }
109
110 static CFGSTRUCT fceuconfig[]={
111         AC(soundvol),
112         ACS(cpalette),
113         AC(ntsctint),
114         AC(ntschue),
115         AC(ntsccol),
116         AC(UsrInputTypeFC),
117         ACA(UsrInputType),
118         AC(powerpadside),
119         AC(powerpadsc),
120         AC(eoptions),
121         ACA(srendlinev),
122         ACA(erendlinev),
123         ADDCFGSTRUCT(DriverConfig),
124         ENDCFGSTRUCT
125 };
126
127 void SaveConfig(const char *name)
128 {
129         char tdir[2048];
130         if (name)
131              sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
132         else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
133         DriverInterface(DES_GETNTSCTINT,&ntsctint);
134         DriverInterface(DES_GETNTSCHUE,&ntschue);
135         SaveFCEUConfig(tdir,fceuconfig);
136 }
137
138 static void LoadConfig(const char *name)
139 {
140         char tdir[2048];
141         if (name)
142              sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
143         else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
144         LoadFCEUConfig(tdir,fceuconfig);
145         if(ntsctint>=0) DriverInterface(DES_SETNTSCTINT,&ntsctint);
146         if(ntschue>=0) DriverInterface(DES_SETNTSCHUE,&ntschue);
147 }
148
149 static void LoadLLGN(void)
150 {
151  char tdir[2048];
152  FILE *f;
153  int len;
154  sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
155  f=fopen(tdir, "r");
156  if(f)
157  {
158   len = fread(lastLoadedGameName, 1, sizeof(lastLoadedGameName)-1, f);
159   lastLoadedGameName[len] = 0;
160   fclose(f);
161  }
162 }
163
164 static void SaveLLGN(void)
165 {
166  // save last loaded game name
167  if (lastLoadedGameName[0])
168  {
169   char tdir[2048];
170   FILE *f;
171   sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
172   f=fopen(tdir, "w");
173   if(f)
174   {
175    fwrite(lastLoadedGameName, 1, strlen(lastLoadedGameName), f);
176    fclose(f);
177    sync();
178   }
179  }
180 }
181
182
183 static void CreateDirs(void)
184 {
185  char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg"};
186  char tdir[2048];
187  int x;
188
189  mkdir(BaseDirectory,S_IRWXU);
190  for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
191  {
192   sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
193   mkdir(tdir,S_IRWXU);
194  }
195 }
196
197 static void SetSignals(void (*t)(int))
198 {
199   int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2};
200   int x;
201   for(x=0;x<11;x++)
202    signal(sigs[x],t);
203 }
204
205 static void CloseStuff(int signum)
206 {
207         DriverKill();
208         printf("\nSignal %d has been caught and dealt with...\n",signum);
209         switch(signum)
210         {
211          case SIGINT:printf("How DARE you interrupt me!\n");break;
212          case SIGTERM:printf("MUST TERMINATE ALL HUMANS\n");break;
213          case SIGHUP:printf("Reach out and hang-up on someone.\n");break;
214          case SIGPIPE:printf("The pipe has broken!  Better watch out for floods...\n");break;
215          case SIGSEGV:printf("Iyeeeeeeeee!!!  A segmentation fault has occurred.  Have a fluffy day.\n");break;
216          /* So much SIGBUS evil. */
217          #ifdef SIGBUS
218          #if(SIGBUS!=SIGSEGV)
219          case SIGBUS:printf("I told you to be nice to the driver.\n");break;
220          #endif
221          #endif
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;
225          case SIGUSR1:
226          case SIGUSR2:printf("Killing your processes is not nice.\n");break;
227         }
228         exit(1);
229 }
230
231 static int DoArgs(int argc, char *argv[])
232 {
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};
239
240         int x, ret;
241         static char *inputa[2]={0,0};
242         static char *fcexp=0;
243         static int docheckie[4];
244
245         static ARGPSTRUCT FCEUArgs[]={
246          {"-soundvol",0,&soundvol,0},
247          {"-cpalette",0,&cpalette,0x4001},
248
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},
253
254          {"-gg",&docheckie[1],0,0},
255          {"-no8lim",0,&eoptions,0x8001},
256          {"-subase",0,&eoptions,0x8002},
257          {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME},
258          {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE},
259          {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES},
260          {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE},
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},
264          {0,0,0,0}
265         };
266
267         memset(docheckie,0,sizeof(docheckie));
268         ret=ParseArguments(argc, argv, FCEUArgs);
269         if(cpalette)
270         {
271          if(cpalette[0]=='0')
272           if(cpalette[1]==0)
273           {
274            free(cpalette);
275            cpalette=0;
276           }
277         }
278         if(docheckie[0])
279          Settings.region_force=2;
280         if(docheckie[1])
281          FCEUI_SetGameGenie(1);
282         FCEUI_DisableSpriteLimitation(1);
283         FCEUI_SaveExtraDataUnderBase(eoptions&2);
284         FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
285
286         for(x=0;x<2;x++)
287         {
288          if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
289          if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
290         }
291
292         printf("main() setrendered lines: %d, %d, %d, %d\n",srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
293         printf("main() clip sides %d\n", eoptions&EO_CLIPSIDES);
294         srendlinev[0]=0;
295         FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
296         FCEUI_SetRenderedLines(0,erendlinev[0],srendlinev[1],erendlinev[1]);
297         FCEUI_SetSoundVolume(soundvol);
298         DriverInterface(DES_NTSCCOL,&ntsccol); // TODO
299         DoDriverArgs();
300
301         if(fcexp)
302         {
303          int y;
304          for(y=0;y<5;y++)
305          {
306           if(!strncmp(fccortab[y],fcexp,8))
307           {
308            UsrInputTypeFC=fccortabi[y];
309            break;
310           }
311          }
312          free(fcexp);
313         }
314         for(x=0;x<2;x++)
315         {
316          int y;
317
318          if(!inputa[x])
319           continue;
320
321          for(y=0;y<5;y++)
322          {
323           if(!strncmp(cortab[y],inputa[x],8))
324           {
325            UsrInputType[x]=cortabi[y];
326            if(y==3)
327            {
328             powerpadside&=~(1<<x);
329             powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
330            }
331            free(inputa[x]);
332           }
333          }
334         }
335         return ret;
336 }
337
338
339 #include "usage.h"
340
341 int CLImain(int argc, char *argv[])
342 {
343         int last_arg_parsed;
344         /* TODO if(argc<=1)
345         {
346          ShowUsage(argv[0]);
347          return 1;
348         }*/
349
350         if(!DriverInitialize())
351         {
352          return 1;
353         }
354
355         if(!FCEUI_Initialize())
356          return(1);
357         GetBaseDirectory(BaseDirectory);
358         FCEUI_SetBaseDirectory(BaseDirectory);
359         lastLoadedGameName[0] = 0;
360
361         CreateDirs();
362         LoadConfig(NULL);
363         last_arg_parsed=DoArgs(argc-1,&argv[1]);
364         gp2x_opt_setup();
365         gp2x_cpuclock_gamma_update();
366         LoadLLGN();
367         if(cpalette)
368          LoadCPalette();
369         if(InitSound())
370          inited|=1;
371
372         if (argc > 1 && !last_arg_parsed)
373         {
374          strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
375          lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
376          Exit = 0;
377         }
378         else
379         {
380          Exit = 1;
381         }
382
383         while (1)
384         {
385          if(!Exit)
386          {
387           if (fceugi)
388            CloseGame();
389           fceugi=FCEUI_LoadGame(lastLoadedGameName);
390           if (fceugi)
391           {
392            LoadConfig(lastLoadedGameName);
393            if (Settings.region_force)
394             FCEUI_SetVidSystem(Settings.region_force - 1);
395            ParseGI(fceugi);
396            //RefreshThrottleFPS();
397            InitOtherInput();
398
399            // additional print for gpfce
400            // TODO: handlers for other formats then iNES
401            {
402           int MapperNo;
403           iNES_HEADER *head = iNESGetHead(); // TODO: ReMake
404           MapperNo = (head->ROM_type>>4);
405           MapperNo|=(head->ROM_type2&0xF0);
406           FCEU_DispMessage("%s, Mapper: %d%s%s", PAL?"PAL":"NTSC", MapperNo, (head->ROM_type&2)?", BB":"", (head->ROM_type&4)?", T":"");
407            }
408           }
409           else
410           {
411            switch(LoadGameLastError) {
412             default: strcpy(menuErrorMsg, "failed to load ROM"); break;
413             case  2: strcpy(menuErrorMsg, "Can't find a ROM for movie"); break;
414             case 10: strcpy(menuErrorMsg, "FDS BIOS ROM is missing, read docs"); break;
415             case 11: strcpy(menuErrorMsg, "Error reading auxillary FDS file"); break;
416            }
417           }
418          }
419          if(Exit || !fceugi)
420          {
421           int ret;
422           ret = gp2x_menu_do();
423           if (ret == 1) break;          // exit emu
424           if (ret == 2) {               // reload ROM
425            Exit = 0;
426            continue;
427           }
428          }
429
430          PrepareOtherInput();
431          gp2x_video_changemode(Settings.scaling == 3 ? 15 : 8);
432          switch (Settings.scaling & 3) {
433                  case 0: gp2x_video_RGB_setscaling(0, 320, 240); gp2x_video_set_offs(0); break;
434                  case 1: gp2x_video_RGB_setscaling(0, 256, 240); gp2x_video_set_offs(32); break;
435                  case 2: gp2x_video_RGB_setscaling(0, 256, 240); gp2x_video_set_offs(32); break; // TODO
436                  case 3: gp2x_video_RGB_setscaling(0, 320, 240); gp2x_video_set_offs(32); break;
437          }
438          gp2x_start_sound(Settings.sound_rate, 16, 0);
439          FCEUI_Emulate();
440         }
441
442         if (fceugi)
443          CloseGame();
444
445         SaveLLGN();
446         DriverKill();
447         return 0;
448 }
449
450 static int DriverInitialize(void)
451 {
452    SetSignals((void *)CloseStuff);
453
454    if(InitJoysticks())
455     inited|=2;
456    if(!InitVideo()) return 0;
457    inited|=4;
458    return 1;
459 }
460
461 static void DriverKill(void)
462 {
463  // SaveConfig(NULL); // done explicitly in menu now
464  SetSignals(SIG_IGN);
465
466  if(inited&2)
467   KillJoysticks();
468  if(inited&4)
469   KillVideo();
470  if(inited&1)
471   KillSound();
472  inited=0;
473 }
474
475 void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
476 {
477  if(!Count && !(eoptions&EO_NOTHROTTLE))
478   SpeedThrottle();
479  BlitScreen(xbuf);
480  if(Count && !(eoptions&EO_NOTHROTTLE))
481   WriteSound(Buffer,Count);
482  FCEUD_UpdateInput();
483 }
484