a4834b3a1a307d07e831f3d8fe17d73c68e4b8a4
[fceu.git] / drivers / gp2x / gp2x.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "../../driver.h"
6 #include "../common/config.h"
7 #include "../common/args.h"
8 #include "gp2x.h"
9 #include "gp2x-video.h"
10 #ifdef NETWORK
11 #include "unix-netplay.h"
12 #endif
13
14 #include "minimal.h"
15 #include "cpuctrl.h"
16 #include "squidgehack.h"
17
18 int GP2X_PORT_REV =
19 #include "rev.h"
20 ;
21
22 int CLImain(int argc, char *argv[]);
23
24 DSETTINGS Settings;
25 CFGSTRUCT DriverConfig[]={
26         ACA(Settings.KeyBinds),
27         ACA(Settings.JoyBinds),
28         AC(Settings.turbo_rate_add),
29         AC(Settings.sound_rate),
30         AC(Settings.showfps),
31         AC(Settings.scaling),
32         AC(Settings.frameskip),
33         AC(Settings.sstate_confirm),
34         AC(Settings.region_force),
35         AC(Settings.cpuclock),
36         AC(Settings.mmuhack),
37         AC(Settings.ramtimings),
38         AC(Settings.gamma),
39         // TODO
40         ENDCFGSTRUCT
41 };
42
43
44 char *DriverUsage=
45 "-joyx   y       Use joystick y as virtual joystick x.\n\
46 -sound x        Sound.\n\
47                  0 = Disabled.\n\
48                  Otherwise, x = playback rate.\n\
49 -showfps x      Display fps counter if x is nonzero\n\
50 -mmuhack x      Enable squidge's MMU hack if x is nonzero (GP2X).\n\
51 -ramtimings x   Enable RAM overclocking if x is nonzero (GP2X).\n\
52 "
53 #ifdef NETWORK
54 "-connect s      Connect to server 's' for TCP/IP network play.\n\
55 -server         Be a host/server for TCP/IP network play.\n\
56 -netport x      Use TCP/IP port x for network play."
57 #endif
58 ;
59
60 #ifdef NETWORK
61 static int docheckie[2]={0,0};
62 #endif
63 ARGPSTRUCT DriverArgs[]={
64          {"-sound",0,&Settings.sound_rate,0},
65          {"-showfps",0,&Settings.showfps,0},
66          {"-mmuhack",0,&Settings.mmuhack,0},
67          {"-ramtimings",0,&Settings.ramtimings,0},
68          {"-menu",0,&ext_menu,0x4001},
69          {"-menustate",0,&ext_state,0x4001},
70          #ifdef NETWORK
71          {"-connect",&docheckie[0],&netplayhost,0x4001},
72          {"-server",&docheckie[1],0,0},
73          {"-netport",0,&Port,0},
74          #endif
75          {0,0,0,0}
76 };
77
78
79
80 void GetBaseDirectory(char *BaseDirectory)
81 {
82  strcpy(BaseDirectory, "fceultra");
83 }
84
85 static void SetDefaults(void)
86 {
87  memset(&Settings,0,sizeof(Settings));
88  Settings.cpuclock = 150;
89  Settings.frameskip = -1; // auto
90  Settings.mmuhack = 1;
91  Settings.sound_rate = 22050;
92  Settings.turbo_rate_add = (8*2 << 24) / 60 + 1; // 8Hz turbofire
93  Settings.gamma = 100;
94  Settings.sstate_confirm = 1;
95  // default controls, RLDU SEBA
96  Settings.KeyBinds[ 0] = 0x010; // GP2X_UP
97  Settings.KeyBinds[ 4] = 0x020; // GP2X_DOWN
98  Settings.KeyBinds[ 2] = 0x040; // GP2X_LEFT
99  Settings.KeyBinds[ 6] = 0x080; // GP2X_RIGHT
100  Settings.KeyBinds[13] = 0x001; // GP2X_B
101  Settings.KeyBinds[14] = 0x002; // GP2X_X
102  Settings.KeyBinds[12] = 0x100; // GP2X_A
103  Settings.KeyBinds[15] = 0x200; // GP2X_Y
104  Settings.KeyBinds[ 8] = 0x008; // GP2X_START
105  Settings.KeyBinds[ 9] = 0x004; // GP2X_SELECT
106  Settings.KeyBinds[10] = 0x80000000; // GP2X_L
107  Settings.KeyBinds[11] = 0x40000000; // GP2X_R
108  Settings.KeyBinds[27] = 0xc0000000; // GP2X_PUSH
109 }
110
111 void DoDriverArgs(void)
112 {
113         #ifdef NETWORK
114         if(docheckie[0])
115          netplay=2;
116         else if(docheckie[1])
117          netplay=1;
118
119         if(netplay)
120          FCEUI_SetNetworkPlay(netplay);
121         #endif
122 }
123
124
125 char **g_argv;
126 int mmuhack_status = 0;
127
128
129 // TODO: cleanup
130 int main(int argc, char *argv[])
131 {
132         int ret;
133         g_argv = argv;
134
135         printf("Starting GPFCE - Port version " GP2X_PORT_VERSION " r%i (" __DATE__ ")\n", GP2X_PORT_REV);
136         puts("Based on FCE Ultra "VERSION_STRING" and 0.98.1x versions");
137         puts("Original port by Zheng Zhu");
138         puts("Menu/optimization/misc work by notaz\n");
139
140         gp2x_init();
141         cpuctrl_init();
142
143         SetDefaults();
144
145         ret = CLImain(argc,argv);
146
147         // unscale the screen, in case this is bad.
148         gp2x_video_RGB_setscaling(0, 320, 240);
149
150         if (mmuhack_status > 0)
151                 mmuunhack();
152
153         set_gamma(100);
154         cpuctrl_deinit();
155         gp2x_deinit();
156
157         return(ret?0:-1);
158 }
159
160
161 /* optional GP2X stuff to be done after config is loaded */
162 void gp2x_opt_setup(void)
163 {
164         if (Settings.mmuhack) {
165                 int ret = mmuhack();
166                 printf("squidge hack code finished and returned %s\n", ret > 0 ? "ok" : "fail");
167                 fflush(stdout);
168                 mmuhack_status = ret;
169         }
170         if (Settings.ramtimings) {
171                 printf("setting RAM timings.. "); fflush(stdout);
172                 // craigix: --trc 6 --tras 4 --twr 1 --tmrd 1 --trfc 1 --trp 2 --trcd 2
173                 set_RAM_Timings(6, 4, 1, 1, 1, 2, 2);
174                 printf("done.\n"); fflush(stdout);
175         }
176 }
177
178 void gp2x_cpuclock_gamma_update(void)
179 {
180         static int prev_cpuclock = 200, prev_gamma = 100;
181         if (Settings.cpuclock != 0 && Settings.cpuclock != prev_cpuclock)
182         {
183                 set_FCLK(Settings.cpuclock);
184                 prev_cpuclock = Settings.cpuclock;
185         }
186
187         if (Settings.gamma != 0 && Settings.gamma != prev_gamma)
188         {
189                 set_gamma(Settings.gamma);
190                 prev_gamma = Settings.gamma;
191         }
192 }
193
194