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