some warnings fixed, nsf fixed, palettes, more code backported
[fceu.git] / drivers / gp2x / main.c
CommitLineData
35868d35 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
48static int ntsccol=0,ntschue=-1,ntsctint=-1;
49int soundvol=70;
50int inited=0;
51int swapbuttons=0;
52int showfps=0;
53
54int srendlinev[2]={0,0};
55//int srendlinev[2]={0,0};
56int erendlinev[2]={239,239};
57//int erendlinev[2]={231,239};
58int srendline,erendline;
59
60
61static char BaseDirectory[2048];
62
63int eoptions=0;
64
65static void DriverKill(void);
66static int DriverInitialize(void);
67
68static int gametype;
69#include "input.c"
70
71static 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
88void FCEUD_PrintError(char *s)
89{
90 puts(s);
91}
92
93static char *cpalette=0;
94static 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
109static 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
126static 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
135static 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
144static 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
158static 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
166static 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
192static 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,
d97315ac 196 SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
35868d35 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
302int CLImain(int argc, char *argv[])
303{
304 FCEUGI *tmp;
305 int ret;
306
937bf65b 307 if(argc<=1)
308 {
309 ShowUsage(argv[0]);
310 return 1;
311 }
312
313 if(!DriverInitialize())
314 {
315 return 1;
316 }
317
35868d35 318 if(!(ret=FCEUI_Initialize()))
319 return(1);
320 GetBaseDirectory(BaseDirectory);
321 FCEUI_SetBaseDirectory(BaseDirectory);
322
323 CreateDirs();
35868d35 324 LoadConfig();
325 DoArgs(argc-2,&argv[1]);
326 if(cpalette)
327 LoadCPalette();
328 if(InitSound())
329 inited|=1;
330
331 if(!(tmp=FCEUI_LoadGame(argv[argc-1])))
332 {
333 ret=0;
334 goto dk;
335 }
336 ParseGI(tmp);
337 //RefreshThrottleFPS();
35868d35 338 InitOtherInput();
339 FCEUI_Emulate();
340
341 dk:
342 DriverKill();
343 return(ret?0:1);
344}
345
346static int DriverInitialize(void)
347{
348 SetSignals((void *)CloseStuff);
349
350 if(InitJoysticks())
351 inited|=2;
352 if(!InitVideo()) return 0;
353 inited|=4;
354 if(!InitKeyboard()) return 0;
355 inited|=8;
356 return 1;
357}
358
359static void DriverKill(void)
360{
361 SaveConfig();
362 SetSignals(SIG_IGN);
363
364 if(inited&2)
365 KillJoysticks();
366 if(inited&8)
367 KillKeyboard();
368 if(inited&4)
369 KillVideo();
370 if(inited&1)
371 KillSound();
372 if(inited&16)
373 KillMouse();
374 inited=0;
375}
376
937bf65b 377void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
35868d35 378{
379 if(!Count && !NoWaiting && !(eoptions&EO_NOTHROTTLE))
380 SpeedThrottle();
937bf65b 381 BlitScreen(xbuf);
382 if(Count && !NoWaiting && !(eoptions&EO_NOTHROTTLE))
383 WriteSound(Buffer,Count);
35868d35 384 FCEUD_UpdateInput();
385}
386