move 1
[fceu.git] / drivers / gp2x / main.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
3  * Copyright notice for this file:
4  *  Copyright (C) 2002 Ben Parnell
5  *
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.
10  *
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.
15  *
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
19  */
20
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.
24 */
25
26
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <signal.h>
30 #include <sys/time.h>
31 #include <sys/stat.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 #include "main.h"
39 #include "throttle.h"
40
41 #include "../common/config.h"
42 #include "../common/args.h"
43 #include "../common/unixdsp.h"
44 #include "../common/cheat.h"
45
46 #include "dface.h"
47
48 static int ntsccol=0,ntschue=-1,ntsctint=-1;
49 int soundvol=70;
50 int inited=0;
51 int swapbuttons=0;
52 int showfps=0;
53
54 int srendlinev[2]={0,0};
55 //int srendlinev[2]={0,0};
56 int erendlinev[2]={239,239};
57 //int erendlinev[2]={231,239};
58 int srendline,erendline;
59
60
61 static char BaseDirectory[2048];
62
63 int eoptions=0;
64
65 static void DriverKill(void);
66 static int DriverInitialize(void);
67
68 static int gametype;
69 #include "input.c"
70
71 static void ParseGI(FCEUGI *gi)
72 {
73  gametype=gi->type;
74
75  InputType[0]=UsrInputType[0];
76  InputType[1]=UsrInputType[1];
77  InputTypeFC=UsrInputTypeFC;
78
79  if(gi->input[0]>=0)
80   InputType[0]=gi->input[0];
81  if(gi->input[1]>=0)
82   InputType[1]=gi->input[1];
83  if(gi->inputfc>=0)
84   InputTypeFC=gi->inputfc;
85  FCEUI_GetCurrentVidSystem(&srendline,&erendline);
86 }
87
88 void FCEUD_PrintError(char *s)
89 {
90  puts(s);
91 }
92
93 static char *cpalette=0;
94 static void LoadCPalette(void)
95 {
96  char tmpp[192];
97  FILE *fp;
98
99  if(!(fp=fopen(cpalette,"rb")))
100  {
101   printf(" Error loading custom palette from file: %s\n",cpalette);
102   return;
103  }
104  fread(tmpp,1,192,fp);
105  FCEUI_SetPaletteArray((uint8 *)tmpp);
106  fclose(fp);
107 }
108
109 static CFGSTRUCT fceuconfig[]={
110         AC(soundvol),
111         ACS(cpalette),
112         AC(ntsctint),
113         AC(ntschue),
114         AC(ntsccol),
115         AC(UsrInputTypeFC),
116         ACA(UsrInputType),
117         AC(powerpadside),
118         AC(powerpadsc),
119         AC(eoptions),
120         ACA(srendlinev),
121         ACA(erendlinev),
122         ADDCFGSTRUCT(DriverConfig),
123         ENDCFGSTRUCT
124 };
125
126 static void SaveConfig(void)
127 {
128         char tdir[2048];
129         sprintf(tdir,"%s"PSS"fceu.cfg",BaseDirectory);
130         DriverInterface(DES_GETNTSCTINT,&ntsctint);
131         DriverInterface(DES_GETNTSCHUE,&ntschue);
132         SaveFCEUConfig(tdir,fceuconfig);
133 }
134
135 static void LoadConfig(void)
136 {
137         char tdir[2048];
138         sprintf(tdir,"%s"PSS"fceu.cfg",BaseDirectory);
139         LoadFCEUConfig(tdir,fceuconfig);
140         if(ntsctint>=0) DriverInterface(DES_SETNTSCTINT,&ntsctint);
141         if(ntschue>=0) DriverInterface(DES_SETNTSCHUE,&ntschue);
142 }
143
144 static void CreateDirs(void)
145 {
146  char *subs[5]={"fcs","snaps","gameinfo","sav","cheats"};
147  char tdir[2048];
148  int x;
149
150  mkdir(BaseDirectory,S_IRWXU);
151  for(x=0;x<5;x++)
152  {
153   sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
154   mkdir(tdir,S_IRWXU);
155  }
156 }
157
158 static void SetSignals(void (*t)(int))
159 {
160   int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2};
161   int x;
162   for(x=0;x<11;x++)
163    signal(sigs[x],t);
164 }
165
166 static void CloseStuff(int signum)
167 {
168         DriverKill();
169         printf("\nSignal %d has been caught and dealt with...\n",signum);
170         switch(signum)
171         {
172          case SIGINT:printf("How DARE you interrupt me!\n");break;
173          case SIGTERM:printf("MUST TERMINATE ALL HUMANS\n");break;
174          case SIGHUP:printf("Reach out and hang-up on someone.\n");break;
175          case SIGPIPE:printf("The pipe has broken!  Better watch out for floods...\n");break;
176          case SIGSEGV:printf("Iyeeeeeeeee!!!  A segmentation fault has occurred.  Have a fluffy day.\n");break;
177          /* So much SIGBUS evil. */
178          #ifdef SIGBUS
179          #if(SIGBUS!=SIGSEGV)
180          case SIGBUS:printf("I told you to be nice to the driver.\n");break;
181          #endif
182          #endif
183          case SIGFPE:printf("Those darn floating points.  Ne'er know when they'll bite!\n");break;
184          case SIGALRM:printf("Don't throw your clock at the meowing cats!\n");break;
185          case SIGABRT:printf("Abort, Retry, Ignore, Fail?\n");break;
186          case SIGUSR1:
187          case SIGUSR2:printf("Killing your processes is not nice.\n");break;
188         }
189         exit(1);
190 }
191
192 static void DoArgs(int argc, char *argv[])
193 {
194         static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
195         static int cortabi[5]={SI_NONE,SI_GAMEPAD,
196                                SI_ZAPPER,SI_POWERPAD,SI_ARKANOID};
197         static char *fccortab[5]={"none","arkanoid","shadow","4player","fkb"};
198         static int fccortabi[5]={SIFC_NONE,SIFC_ARKANOID,SIFC_SHADOW,
199                                  SIFC_4PLAYER,SIFC_FKB};
200
201         int x;
202         static char *inputa[2]={0,0};
203         static char *fcexp=0;
204         static int docheckie[4];
205
206         static ARGPSTRUCT FCEUArgs[]={
207          {"-soundvol",0,&soundvol,0},
208          {"-cpalette",0,&cpalette,0x4001},
209
210          {"-ntsccol",0,&ntsccol,0},
211          {"-pal",&docheckie[0],0,0},
212          {"-input1",0,&inputa[0],0x4001},{"-input2",0,&inputa[1],0x4001},
213          {"-fcexp",0,&fcexp,0x4001},
214
215          {"-gg",&docheckie[1],0,0},
216          {"-no8lim",0,&eoptions,0x8001},
217          {"-subase",0,&eoptions,0x8002},
218          {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME},
219          {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE},
220          {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES},
221          {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE},
222          {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
223          {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
224              {"-swapbuttons",&swapbuttons, 0, 0},
225              {"-showfps",&showfps, 0, 0},
226          {0,(void *)DriverArgs,0,0},
227          {0,0,0,0}
228         };
229
230         memset(docheckie,0,sizeof(docheckie));
231         ParseArguments(argc, argv, FCEUArgs);
232         if(cpalette)
233         {
234          if(cpalette[0]=='0')
235           if(cpalette[1]==0)
236           {
237            free(cpalette);
238            cpalette=0;
239           }
240         }
241         if(docheckie[0])
242          FCEUI_SetVidSystem(1);
243         if(docheckie[1])
244          FCEUI_SetGameGenie(1);
245         FCEUI_DisableSpriteLimitation(1);
246         FCEUI_SaveExtraDataUnderBase(eoptions&2);
247         FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
248
249         for(x=0;x<2;x++)
250         {
251          if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
252          if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
253         }
254
255         printf("main() setrendered lines: %d, %d, %d, %d\n",srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
256         printf("main() clip sides %d\n", eoptions&EO_CLIPSIDES);
257         srendlinev[0]=0;
258         FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
259         FCEUI_SetRenderedLines(0,erendlinev[0],srendlinev[1],erendlinev[1]);
260         FCEUI_SetSoundVolume(soundvol);
261         DriverInterface(DES_NTSCCOL,&ntsccol);
262         DoDriverArgs();
263
264         if(fcexp)
265         {
266          int y;
267          for(y=0;y<5;y++)
268          {
269           if(!strncmp(fccortab[y],fcexp,8))
270           {
271            UsrInputTypeFC=fccortabi[y];
272            break;
273           }
274          }
275          free(fcexp);
276         }
277         for(x=0;x<2;x++)
278         {
279          int y;
280
281          if(!inputa[x])
282           continue;
283
284          for(y=0;y<5;y++)
285          {
286           if(!strncmp(cortab[y],inputa[x],8))
287           {
288            UsrInputType[x]=cortabi[y];
289            if(y==3)
290            {
291             powerpadside&=~(1<<x);
292             powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
293            }
294            free(inputa[x]);
295           }
296          }
297         }
298 }
299
300 #include "usage.h"
301
302 int CLImain(int argc, char *argv[])
303 {
304         FCEUGI *tmp;
305         int ret;
306
307         if(!(ret=FCEUI_Initialize()))
308          return(1);
309         GetBaseDirectory(BaseDirectory);
310         FCEUI_SetBaseDirectory(BaseDirectory);
311
312         CreateDirs();
313         if(argc<=1)
314         {
315          ShowUsage(argv[0]);
316          return 1;
317         }
318         LoadConfig();
319         DoArgs(argc-2,&argv[1]);
320         if(cpalette)
321          LoadCPalette();
322         if(InitSound())
323          inited|=1;
324
325         if(!(tmp=FCEUI_LoadGame(argv[argc-1])))
326         {
327          ret=0;
328          goto dk;
329         }
330         ParseGI(tmp);
331         //RefreshThrottleFPS();
332         if(!DriverInitialize())
333         {
334          ret=0;
335          goto dk;
336         }
337         InitOtherInput();
338         FCEUI_Emulate();
339
340         dk:
341         DriverKill();
342         return(ret?0:1);
343 }
344
345 static int DriverInitialize(void)
346 {
347    SetSignals((void *)CloseStuff);
348
349    if(InitJoysticks())
350     inited|=2;
351    if(!InitVideo()) return 0;
352    inited|=4;
353    if(!InitKeyboard()) return 0;
354    inited|=8;
355    return 1;
356 }
357
358 static void DriverKill(void)
359 {
360  SaveConfig();
361  SetSignals(SIG_IGN);
362
363  if(inited&2)
364   KillJoysticks();
365  if(inited&8)
366   KillKeyboard();
367  if(inited&4)
368   KillVideo();
369  if(inited&1)
370   KillSound();
371  if(inited&16)
372   KillMouse();
373  inited=0;
374 }
375
376 void FCEUD_Update(uint8 *XBuf, int32 *Buffer, int Count)
377 {
378  if(!Count && !NoWaiting && !(eoptions&EO_NOTHROTTLE))
379   SpeedThrottle();
380  BlitScreen(XBuf);
381  if(Count)
382   WriteSound(Buffer,Count,NoWaiting);
383  FCEUD_UpdateInput();
384 }
385