more menu work, scalers, sound
[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"
31#include "throttle.h"
b2b95d2e 32#include "menu.h"
33#include "gp2x.h"
35868d35 34
35#include "../common/config.h"
36#include "../common/args.h"
37#include "../common/unixdsp.h"
38#include "../common/cheat.h"
39
40#include "dface.h"
41
6587f346 42// just for printing some iNES info for user..
43#include "../../fce.h"
44#include "../../ines.h"
45
b2b95d2e 46// internals
47extern char lastLoadedGameName[2048];
48extern uint8 Exit; // exit emu loop flag
49void CloseGame(void);
50
51FCEUGI *fceugi = NULL;
35868d35 52static int ntsccol=0,ntschue=-1,ntsctint=-1;
53int soundvol=70;
54int inited=0;
35868d35 55
56int srendlinev[2]={0,0};
35868d35 57int erendlinev[2]={239,239};
35868d35 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
b547bda7 126void SaveConfig(const char *name)
35868d35 127{
128 char tdir[2048];
b547bda7 129 if (name)
130 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
131 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
35868d35 132 DriverInterface(DES_GETNTSCTINT,&ntsctint);
133 DriverInterface(DES_GETNTSCHUE,&ntschue);
134 SaveFCEUConfig(tdir,fceuconfig);
135}
136
b547bda7 137static void LoadConfig(const char *name)
35868d35 138{
139 char tdir[2048];
b547bda7 140 if (name)
141 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
142 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
35868d35 143 LoadFCEUConfig(tdir,fceuconfig);
144 if(ntsctint>=0) DriverInterface(DES_SETNTSCTINT,&ntsctint);
145 if(ntschue>=0) DriverInterface(DES_SETNTSCHUE,&ntschue);
146}
147
989672f4 148static void LoadLLGN(void)
149{
150 char tdir[2048];
151 FILE *f;
152 int len;
153 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
154 f=fopen(tdir, "r");
155 if(f)
156 {
157 len = fread(lastLoadedGameName, 1, sizeof(lastLoadedGameName)-1, f);
158 lastLoadedGameName[len] = 0;
159 fclose(f);
160 }
161}
162
163static void SaveLLGN(void)
164{
165 // save last loaded game name
166 if (lastLoadedGameName[0])
167 {
168 char tdir[2048];
169 FILE *f;
170 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
171 f=fopen(tdir, "w");
172 if(f)
173 {
174 fwrite(lastLoadedGameName, 1, strlen(lastLoadedGameName), f);
175 fclose(f);
176 sync();
177 }
178 }
179}
180
181
35868d35 182static void CreateDirs(void)
183{
b547bda7 184 char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg"};
35868d35 185 char tdir[2048];
186 int x;
187
188 mkdir(BaseDirectory,S_IRWXU);
b547bda7 189 for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
35868d35 190 {
191 sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
192 mkdir(tdir,S_IRWXU);
193 }
194}
195
196static void SetSignals(void (*t)(int))
197{
198 int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2};
199 int x;
200 for(x=0;x<11;x++)
201 signal(sigs[x],t);
202}
203
204static void CloseStuff(int signum)
205{
206 DriverKill();
207 printf("\nSignal %d has been caught and dealt with...\n",signum);
208 switch(signum)
209 {
210 case SIGINT:printf("How DARE you interrupt me!\n");break;
211 case SIGTERM:printf("MUST TERMINATE ALL HUMANS\n");break;
212 case SIGHUP:printf("Reach out and hang-up on someone.\n");break;
213 case SIGPIPE:printf("The pipe has broken! Better watch out for floods...\n");break;
214 case SIGSEGV:printf("Iyeeeeeeeee!!! A segmentation fault has occurred. Have a fluffy day.\n");break;
215 /* So much SIGBUS evil. */
216 #ifdef SIGBUS
217 #if(SIGBUS!=SIGSEGV)
218 case SIGBUS:printf("I told you to be nice to the driver.\n");break;
219 #endif
220 #endif
221 case SIGFPE:printf("Those darn floating points. Ne'er know when they'll bite!\n");break;
222 case SIGALRM:printf("Don't throw your clock at the meowing cats!\n");break;
223 case SIGABRT:printf("Abort, Retry, Ignore, Fail?\n");break;
224 case SIGUSR1:
225 case SIGUSR2:printf("Killing your processes is not nice.\n");break;
226 }
227 exit(1);
228}
229
b2b95d2e 230static int DoArgs(int argc, char *argv[])
35868d35 231{
232 static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
233 static int cortabi[5]={SI_NONE,SI_GAMEPAD,
d97315ac 234 SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
35868d35 235 static char *fccortab[5]={"none","arkanoid","shadow","4player","fkb"};
236 static int fccortabi[5]={SIFC_NONE,SIFC_ARKANOID,SIFC_SHADOW,
237 SIFC_4PLAYER,SIFC_FKB};
238
b2b95d2e 239 int x, ret;
35868d35 240 static char *inputa[2]={0,0};
241 static char *fcexp=0;
242 static int docheckie[4];
243
244 static ARGPSTRUCT FCEUArgs[]={
245 {"-soundvol",0,&soundvol,0},
246 {"-cpalette",0,&cpalette,0x4001},
247
248 {"-ntsccol",0,&ntsccol,0},
249 {"-pal",&docheckie[0],0,0},
250 {"-input1",0,&inputa[0],0x4001},{"-input2",0,&inputa[1],0x4001},
251 {"-fcexp",0,&fcexp,0x4001},
252
253 {"-gg",&docheckie[1],0,0},
254 {"-no8lim",0,&eoptions,0x8001},
255 {"-subase",0,&eoptions,0x8002},
256 {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME},
257 {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE},
258 {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES},
259 {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE},
260 {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
261 {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
35868d35 262 {0,(void *)DriverArgs,0,0},
263 {0,0,0,0}
264 };
265
266 memset(docheckie,0,sizeof(docheckie));
b2b95d2e 267 ret=ParseArguments(argc, argv, FCEUArgs);
35868d35 268 if(cpalette)
269 {
270 if(cpalette[0]=='0')
271 if(cpalette[1]==0)
272 {
273 free(cpalette);
274 cpalette=0;
275 }
276 }
277 if(docheckie[0])
989672f4 278 Settings.region_force=2;
35868d35 279 if(docheckie[1])
280 FCEUI_SetGameGenie(1);
281 FCEUI_DisableSpriteLimitation(1);
282 FCEUI_SaveExtraDataUnderBase(eoptions&2);
283 FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
284
285 for(x=0;x<2;x++)
286 {
287 if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
288 if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
289 }
290
291 printf("main() setrendered lines: %d, %d, %d, %d\n",srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
292 printf("main() clip sides %d\n", eoptions&EO_CLIPSIDES);
293 srendlinev[0]=0;
294 FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
295 FCEUI_SetRenderedLines(0,erendlinev[0],srendlinev[1],erendlinev[1]);
296 FCEUI_SetSoundVolume(soundvol);
297 DriverInterface(DES_NTSCCOL,&ntsccol);
298 DoDriverArgs();
299
300 if(fcexp)
301 {
302 int y;
303 for(y=0;y<5;y++)
304 {
305 if(!strncmp(fccortab[y],fcexp,8))
306 {
307 UsrInputTypeFC=fccortabi[y];
308 break;
309 }
310 }
311 free(fcexp);
312 }
313 for(x=0;x<2;x++)
314 {
315 int y;
316
317 if(!inputa[x])
318 continue;
319
320 for(y=0;y<5;y++)
321 {
322 if(!strncmp(cortab[y],inputa[x],8))
323 {
324 UsrInputType[x]=cortabi[y];
325 if(y==3)
326 {
327 powerpadside&=~(1<<x);
328 powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
329 }
330 free(inputa[x]);
331 }
332 }
333 }
b2b95d2e 334 return ret;
35868d35 335}
336
b547bda7 337
35868d35 338#include "usage.h"
339
340int CLImain(int argc, char *argv[])
341{
b2b95d2e 342 int last_arg_parsed;
343 /* TODO if(argc<=1)
937bf65b 344 {
345 ShowUsage(argv[0]);
346 return 1;
b2b95d2e 347 }*/
937bf65b 348
349 if(!DriverInitialize())
350 {
351 return 1;
352 }
353
b2b95d2e 354 if(!FCEUI_Initialize())
35868d35 355 return(1);
356 GetBaseDirectory(BaseDirectory);
357 FCEUI_SetBaseDirectory(BaseDirectory);
b2b95d2e 358 lastLoadedGameName[0] = 0;
35868d35 359
360 CreateDirs();
b547bda7 361 LoadConfig(NULL);
b2b95d2e 362 last_arg_parsed=DoArgs(argc-1,&argv[1]);
b547bda7 363 gp2x_opt_setup();
989672f4 364 gp2x_cpuclock_gamma_update();
365 LoadLLGN();
35868d35 366 if(cpalette)
367 LoadCPalette();
368 if(InitSound())
369 inited|=1;
370
b2b95d2e 371 if (argc > 1 && !last_arg_parsed)
6587f346 372 {
b2b95d2e 373 strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
374 lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
375 Exit = 0;
376 }
377 else
378 {
b2b95d2e 379 Exit = 1;
6587f346 380 }
381
b2b95d2e 382 while (1)
383 {
384 if(!Exit)
385 {
386 if (fceugi)
387 CloseGame();
388 fceugi=FCEUI_LoadGame(lastLoadedGameName);
389 if (fceugi)
390 {
b547bda7 391 LoadConfig(lastLoadedGameName);
989672f4 392 if (Settings.region_force)
393 FCEUI_SetVidSystem(Settings.region_force - 1);
b2b95d2e 394 ParseGI(fceugi);
395 //RefreshThrottleFPS();
396 InitOtherInput();
397
398 // additional print for gpfce
399 // TODO: handlers for other formats then iNES
400 {
401 int MapperNo;
402 iNES_HEADER *head = iNESGetHead();
403 MapperNo = (head->ROM_type>>4);
404 MapperNo|=(head->ROM_type2&0xF0);
405 FCEU_DispMessage("%s, Mapper: %d%s%s", PAL?"PAL":"NTSC", MapperNo, (head->ROM_type&2)?", BB":"", (head->ROM_type&4)?", T":"");
406 }
407 }
408 else
989672f4 409 {
410 switch(LoadGameLastError) {
411 default: strcpy(menuErrorMsg, "failed to load ROM"); break;
412 case 2: strcpy(menuErrorMsg, "Can't find a ROM for movie"); break;
413 case 10: strcpy(menuErrorMsg, "FDS BIOS ROM is missing, read docs"); break;
414 case 11: strcpy(menuErrorMsg, "Error reading auxillary FDS file"); break;
415 }
416 }
b2b95d2e 417 }
418 if(Exit || !fceugi)
419 {
420 int ret;
421 ret = gp2x_menu_do();
422 if (ret == 1) break; // exit emu
423 if (ret == 2) { // reload ROM
424 Exit = 0;
425 continue;
426 }
427 }
428
429 gp2x_video_changemode(Settings.scaling == 3 ? 15 : 8);
989672f4 430 switch (Settings.scaling & 3) {
431 case 0: gp2x_video_RGB_setscaling(0, 320, 240); gp2x_video_set_offs(0); break;
432 case 1: gp2x_video_RGB_setscaling(0, 256, 240); gp2x_video_set_offs(32); break;
433 case 2: gp2x_video_RGB_setscaling(0, 256, 240); gp2x_video_set_offs(32); break; // TODO
434 case 3: gp2x_video_RGB_setscaling(0, 320, 240); gp2x_video_set_offs(32); break;
435 }
436 gp2x_start_sound(Settings.sound_rate, 16, 0);
b2b95d2e 437 FCEUI_Emulate();
438 }
35868d35 439
b547bda7 440 if (fceugi)
441 CloseGame();
442
989672f4 443 SaveLLGN();
35868d35 444 DriverKill();
b2b95d2e 445 return 0;
35868d35 446}
447
448static int DriverInitialize(void)
449{
450 SetSignals((void *)CloseStuff);
451
452 if(InitJoysticks())
453 inited|=2;
454 if(!InitVideo()) return 0;
455 inited|=4;
456 if(!InitKeyboard()) return 0;
457 inited|=8;
458 return 1;
459}
460
461static void DriverKill(void)
462{
989672f4 463 // SaveConfig(NULL); // done explicitly in menu now
35868d35 464 SetSignals(SIG_IGN);
465
466 if(inited&2)
467 KillJoysticks();
468 if(inited&8)
469 KillKeyboard();
470 if(inited&4)
471 KillVideo();
472 if(inited&1)
473 KillSound();
474 if(inited&16)
475 KillMouse();
476 inited=0;
477}
478
937bf65b 479void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
35868d35 480{
481 if(!Count && !NoWaiting && !(eoptions&EO_NOTHROTTLE))
482 SpeedThrottle();
937bf65b 483 BlitScreen(xbuf);
484 if(Count && !NoWaiting && !(eoptions&EO_NOTHROTTLE))
485 WriteSound(Buffer,Count);
35868d35 486 FCEUD_UpdateInput();
487}
488