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