gp2x_test added, cli removed
[fceu.git] / drivers / gp2x_test / minimal.c
CommitLineData
e6ee7529 1/* emulate minimal lib using SDL */
2
3#include <stdio.h>
4#include <SDL.h>
5
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <fcntl.h>
9#include <unistd.h>
10#include <sys/ioctl.h>
11#include <sys/soundcard.h>
12#include <errno.h>
13
14#include "../gp2x/minimal.h"
15#include "../gp2x/usbjoy.h"
16
17SDL_Surface *screen;
18void *gp2x_screen;
19static int sounddev = 0;
20
21
22/* video stuff */
23void gp2x_video_flip(void)
24{
25 if(SDL_MUSTLOCK(screen))
26 SDL_LockSurface(screen);
27
28 memcpy(screen->pixels, gp2x_screen, 320*240*screen->format->BytesPerPixel);
29
30 if(SDL_MUSTLOCK(screen))
31 SDL_UnlockSurface(screen);
32
33 SDL_UpdateRect(screen, 0, 0, 0, 0);
34}
35
36
37void gp2x_video_changemode2(int bpp)
38{
39 const SDL_VideoInfo *vinf;
40 int flags=0;
41
42 vinf=SDL_GetVideoInfo();
43
44 if(vinf->hw_available)
45 flags|=SDL_HWSURFACE;
46
47 if (bpp == 8)
48 flags|=SDL_HWPALETTE;
49
50 screen = SDL_SetVideoMode(320, 240, bpp, flags);
51 if(!screen)
52 {
53 puts(SDL_GetError());
54 return;
55 }
56
57 SDL_WM_SetCaption("FCE Ultra","FCE Ultra");
58}
59
60
61static SDL_Color psdl[256];
62
63void gp2x_video_changemode(int bpp)
64{
65 gp2x_video_changemode2(bpp);
66 if (bpp == 8)
67 SDL_SetPalette(screen,SDL_PHYSPAL,psdl,0,256);
68 gp2x_video_flip();
69}
70
71
72void gp2x_video_setpalette(int *pal, int len)
73{
74 int i;
75
76 for (i = 0; i < len; i++)
77 {
78 psdl[i].r = pal[i] >> 16;
79 psdl[i].g = pal[i] >> 8;
80 psdl[i].b = pal[i];
81 }
82
83 SDL_SetPalette(screen,SDL_PHYSPAL,psdl,0,len);
84}
85
86
87void gp2x_video_RGB_setscaling(int ln_offs, int W, int H)
88{
89}
90
91void gp2x_video_set_offs(int offs)
92{
93}
94
95void gp2x_memcpy_buffers(int buffers, void *data, int offset, int len)
96{
97}
98
99
100void gp2x_memcpy_all_buffers(void *data, int offset, int len)
101{
102}
103
104
105void gp2x_memset_all_buffers(int offset, int byte, int len)
106{
107 memset((char *)gp2x_screen + offset, byte, len);
108}
109
110
111unsigned long gp2x_joystick_read(int allow_usb_joy)
112{
113 unsigned long keys_out = 0;
114 Uint8 *keys;
115 int i;
116
117 SDL_PumpEvents();
118 keys = SDL_GetKeyState(NULL);
119
120 if (keys[SDLK_UP]) keys_out |= GP2X_UP;
121 if (keys[SDLK_LEFT]) keys_out |= GP2X_LEFT;
122 if (keys[SDLK_DOWN]) keys_out |= GP2X_DOWN;
123 if (keys[SDLK_RIGHT]) keys_out |= GP2X_RIGHT;
124 if (keys[SDLK_RETURN]) keys_out |= GP2X_START;
125 if (keys[SDLK_BACKSLASH]) keys_out |= GP2X_SELECT;
126 if (keys[SDLK_s]) keys_out |= GP2X_L;
127 if (keys[SDLK_d]) keys_out |= GP2X_R;
128 if (keys[SDLK_z]) keys_out |= GP2X_A;
129 if (keys[SDLK_x]) keys_out |= GP2X_X;
130 if (keys[SDLK_c]) keys_out |= GP2X_B;
131 if (keys[SDLK_v]) keys_out |= GP2X_Y;
132 if (keys[SDLK_q]) keys_out |= GP2X_VOL_DOWN;
133 if (keys[SDLK_w]) keys_out |= GP2X_VOL_UP;
134 if (keys[SDLK_RIGHTBRACKET]) keys_out |= GP2X_PUSH;
135
136 if (allow_usb_joy && num_of_joys > 0) {
137 // check the usb joy as well..
138 gp2x_usbjoy_update();
139 for (i = 0; i < num_of_joys; i++)
140 keys_out |= gp2x_usbjoy_check(i);
141 }
142
143 return keys_out;
144}
145
146static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0;
147
148void gp2x_start_sound(int rate, int bits, int stereo)
149{
150 int frag = 0, bsize, buffers;
151
152 // if no settings change, we don't need to do anything
153 if (rate == s_oldrate && s_oldbits == bits && s_oldstereo == stereo) return;
154
155 if (sounddev > 0) close(sounddev);
156 sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC);
157 if (sounddev == -1)
158 {
159 printf("open(\"/dev/dsp\") failed with %i\n", errno);
160 return;
161 }
162
163 ioctl(sounddev, SNDCTL_DSP_SPEED, &rate);
164 ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);
165 ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);
166 // calculate buffer size
167 buffers = 16;
168 bsize = rate / 32;
169 if (rate > 22050) { bsize*=4; buffers*=2; } // 44k mode seems to be very demanding
170 while ((bsize>>=1)) frag++;
171 frag |= buffers<<16; // 16 buffers
172 ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);
173 printf("gp2x_set_sound: %i/%ibit/%s, %i buffers of %i bytes\n",
174 rate, bits, stereo?"stereo":"mono", frag>>16, 1<<(frag&0xffff));
175
176 s_oldrate = rate; s_oldbits = bits; s_oldstereo = stereo;
177 usleep(100000);
178}
179
180
181void gp2x_sound_write(void *buff, int len)
182{
183 if (sounddev > 0)
184 write(sounddev, buff, len);
185}
186
187void gp2x_sound_sync(void)
188{
189 if (sounddev > 0)
190 ioctl(sounddev, SOUND_PCM_SYNC, 0);
191}
192
193void gp2x_sound_volume(int l, int r)
194{
195}
196
197
198void gp2x_init(void)
199{
200 printf("entering init()\n"); fflush(stdout);
201
202 gp2x_screen = malloc(320*240*2);
203 if(gp2x_screen == NULL) return;
204 memset(gp2x_screen, 0, 320*240*2);
205
206 if(SDL_Init(SDL_INIT_NOPARACHUTE))
207 {
208 printf("Could not initialize SDL: %s.\n", SDL_GetError());
209 return;
210 }
211
212 if(SDL_InitSubSystem(SDL_INIT_VIDEO)==-1)
213 {
214 puts(SDL_GetError());
215 return;
216 }
217
218 SDL_ShowCursor(0);
219
220 gp2x_video_changemode(8);
221
222 /* init usb joys -GnoStiC */
223 gp2x_usbjoy_init();
224
225 SDL_PumpEvents();
226
227 printf("gp2x_init done.\n");
228}
229
230
231char *ext_menu = 0, *ext_state = 0;
232
233void gp2x_deinit(void)
234{
235 SDL_QuitSubSystem(SDL_INIT_VIDEO);
236 SDL_Quit();
237
238 if (sounddev > 0) close(sounddev);
239 gp2x_usbjoy_deinit();
240}
241
242
243/* other stuff to be faked */
244void cpuctrl_init(void)
245{
246}
247
248void cpuctrl_deinit(void)
249{
250}
251
252void set_FCLK(unsigned MHZ)
253{
254}
255
256void set_RAM_Timings(int tRC, int tRAS, int tWR, int tMRD, int tRFC, int tRP, int tRCD)
257{
258}
259
260void set_gamma(int g100)
261{
262}
263
264
265int mmuhack(void)
266{
267 return 1;
268}
269
270int mmuunhack(void)
271{
272 return 1;
273}
274
275void memset32(int *dest, int c, int count)
276{
277 memset(dest, c, count*4);
278}
279
280void spend_cycles(int c)
281{
282 usleep(c/200);
283}
284
285void soft_scale(void *dst, unsigned short *pal, int line_offs, int lines)
286{
287}
288