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