merged ppu code, added input+zapper, FDS/VS insert in menu
[fceu.git] / drivers / gp2x / main.c
CommitLineData
35868d35 1/* FCE Ultra - NES/Famicom Emulator
35868d35 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
35868d35 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"
971a1d07 31#include "minimal.h"
35868d35 32#include "throttle.h"
b2b95d2e 33#include "menu.h"
34#include "gp2x.h"
35868d35 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
6587f346 43// just for printing some iNES info for user..
44#include "../../fce.h"
45#include "../../ines.h"
46
b2b95d2e 47// internals
48extern char lastLoadedGameName[2048];
49extern uint8 Exit; // exit emu loop flag
50void CloseGame(void);
51
52FCEUGI *fceugi = NULL;
35868d35 53static int ntsccol=0,ntschue=-1,ntsctint=-1;
54int soundvol=70;
55int inited=0;
35868d35 56
57int srendlinev[2]={0,0};
35868d35 58int erendlinev[2]={239,239};
35868d35 59int srendline,erendline;
60
61
62static char BaseDirectory[2048];
63
64int eoptions=0;
65
66static void DriverKill(void);
67static int DriverInitialize(void);
68
69static int gametype;
70#include "input.c"
71
72static 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
89void FCEUD_PrintError(char *s)
90{
91 puts(s);
92}
93
94static char *cpalette=0;
95static 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
110static 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
b547bda7 127void SaveConfig(const char *name)
35868d35 128{
129 char tdir[2048];
b547bda7 130 if (name)
131 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
132 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
35868d35 133 DriverInterface(DES_GETNTSCTINT,&ntsctint);
134 DriverInterface(DES_GETNTSCHUE,&ntschue);
135 SaveFCEUConfig(tdir,fceuconfig);
136}
137
b547bda7 138static void LoadConfig(const char *name)
35868d35 139{
140 char tdir[2048];
b547bda7 141 if (name)
142 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
143 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
35868d35 144 LoadFCEUConfig(tdir,fceuconfig);
145 if(ntsctint>=0) DriverInterface(DES_SETNTSCTINT,&ntsctint);
146 if(ntschue>=0) DriverInterface(DES_SETNTSCHUE,&ntschue);
147}
148
989672f4 149static 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
164static 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
35868d35 183static void CreateDirs(void)
184{
b547bda7 185 char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg"};
35868d35 186 char tdir[2048];
187 int x;
188
189 mkdir(BaseDirectory,S_IRWXU);
b547bda7 190 for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
35868d35 191 {
192 sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
193 mkdir(tdir,S_IRWXU);
194 }
195}
196
197static 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
205static 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
b2b95d2e 231static int DoArgs(int argc, char *argv[])
35868d35 232{
233 static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
234 static int cortabi[5]={SI_NONE,SI_GAMEPAD,
d97315ac 235 SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
35868d35 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
b2b95d2e 240 int x, ret;
35868d35 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},
35868d35 263 {0,(void *)DriverArgs,0,0},
264 {0,0,0,0}
265 };
266
267 memset(docheckie,0,sizeof(docheckie));
b2b95d2e 268 ret=ParseArguments(argc, argv, FCEUArgs);
35868d35 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])
989672f4 279 Settings.region_force=2;
35868d35 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);
971a1d07 298 DriverInterface(DES_NTSCCOL,&ntsccol); // TODO
35868d35 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 }
b2b95d2e 335 return ret;
35868d35 336}
337
b547bda7 338
35868d35 339#include "usage.h"
340
341int CLImain(int argc, char *argv[])
342{
b2b95d2e 343 int last_arg_parsed;
344 /* TODO if(argc<=1)
937bf65b 345 {
346 ShowUsage(argv[0]);
347 return 1;
b2b95d2e 348 }*/
937bf65b 349
350 if(!DriverInitialize())
351 {
352 return 1;
353 }
354
b2b95d2e 355 if(!FCEUI_Initialize())
35868d35 356 return(1);
357 GetBaseDirectory(BaseDirectory);
358 FCEUI_SetBaseDirectory(BaseDirectory);
b2b95d2e 359 lastLoadedGameName[0] = 0;
35868d35 360
361 CreateDirs();
b547bda7 362 LoadConfig(NULL);
b2b95d2e 363 last_arg_parsed=DoArgs(argc-1,&argv[1]);
b547bda7 364 gp2x_opt_setup();
989672f4 365 gp2x_cpuclock_gamma_update();
366 LoadLLGN();
35868d35 367 if(cpalette)
368 LoadCPalette();
369 if(InitSound())
370 inited|=1;
371
b2b95d2e 372 if (argc > 1 && !last_arg_parsed)
6587f346 373 {
b2b95d2e 374 strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
375 lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
376 Exit = 0;
377 }
378 else
379 {
b2b95d2e 380 Exit = 1;
6587f346 381 }
382
b2b95d2e 383 while (1)
384 {
385 if(!Exit)
386 {
387 if (fceugi)
388 CloseGame();
389 fceugi=FCEUI_LoadGame(lastLoadedGameName);
390 if (fceugi)
391 {
b547bda7 392 LoadConfig(lastLoadedGameName);
989672f4 393 if (Settings.region_force)
394 FCEUI_SetVidSystem(Settings.region_force - 1);
b2b95d2e 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;
e2d0dd92 403 iNES_HEADER *head = iNESGetHead(); // TODO: ReMake
b2b95d2e 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
989672f4 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 }
b2b95d2e 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
971a1d07 430 PrepareOtherInput();
b2b95d2e 431 gp2x_video_changemode(Settings.scaling == 3 ? 15 : 8);
989672f4 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);
b2b95d2e 439 FCEUI_Emulate();
440 }
35868d35 441
b547bda7 442 if (fceugi)
443 CloseGame();
444
989672f4 445 SaveLLGN();
35868d35 446 DriverKill();
b2b95d2e 447 return 0;
35868d35 448}
449
450static int DriverInitialize(void)
451{
452 SetSignals((void *)CloseStuff);
453
35868d35 454 if(!InitVideo()) return 0;
455 inited|=4;
35868d35 456 return 1;
457}
458
459static void DriverKill(void)
460{
989672f4 461 // SaveConfig(NULL); // done explicitly in menu now
35868d35 462 SetSignals(SIG_IGN);
463
35868d35 464 if(inited&4)
465 KillVideo();
466 if(inited&1)
467 KillSound();
35868d35 468 inited=0;
469}
470
937bf65b 471void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
35868d35 472{
971a1d07 473 if(!Count && !(eoptions&EO_NOTHROTTLE))
35868d35 474 SpeedThrottle();
937bf65b 475 BlitScreen(xbuf);
971a1d07 476 if(Count && !(eoptions&EO_NOTHROTTLE))
937bf65b 477 WriteSound(Buffer,Count);
35868d35 478 FCEUD_UpdateInput();
479}
480