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