| 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 | |
| 17 | SDL_Surface *screen; |
| 18 | void *gp2x_screen; |
| 19 | static int sounddev = 0; |
| 20 | |
| 21 | |
| 22 | /* video stuff */ |
| 23 | void 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 | |
| 37 | void 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 | |
| 61 | static SDL_Color psdl[256]; |
| 62 | |
| 63 | void 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 | |
| 72 | void 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 | |
| 87 | void gp2x_video_RGB_setscaling(int ln_offs, int W, int H) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | void gp2x_video_set_offs(int offs) |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | void gp2x_memcpy_buffers(int buffers, void *data, int offset, int len) |
| 96 | { |
| 97 | } |
| 98 | |
| 99 | |
| 100 | void gp2x_memcpy_all_buffers(void *data, int offset, int len) |
| 101 | { |
| 102 | } |
| 103 | |
| 104 | |
| 105 | void gp2x_memset_all_buffers(int offset, int byte, int len) |
| 106 | { |
| 107 | memset((char *)gp2x_screen + offset, byte, len); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | unsigned 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 | |
| 146 | static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0; |
| 147 | |
| 148 | void 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 | |
| 181 | void gp2x_sound_write(void *buff, int len) |
| 182 | { |
| 183 | if (sounddev > 0) |
| 184 | write(sounddev, buff, len); |
| 185 | } |
| 186 | |
| 187 | void gp2x_sound_sync(void) |
| 188 | { |
| 189 | if (sounddev > 0) |
| 190 | ioctl(sounddev, SOUND_PCM_SYNC, 0); |
| 191 | } |
| 192 | |
| 193 | void gp2x_sound_volume(int l, int r) |
| 194 | { |
| 195 | } |
| 196 | |
| 197 | |
| 198 | void gp2x_init(void) |
| 199 | { |
| 200 | printf("entering init()\n"); fflush(stdout); |
| 201 | |
| 202 | gp2x_screen = malloc(320*240*2 + 32); |
| 203 | if(gp2x_screen == NULL) return; |
| 204 | memset(gp2x_screen, 0, 320*240*2 + 32); |
| 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 | |
| 231 | char *ext_menu = 0, *ext_state = 0; |
| 232 | |
| 233 | void gp2x_deinit(void) |
| 234 | { |
| 235 | SDL_QuitSubSystem(SDL_INIT_VIDEO); |
| 236 | SDL_Quit(); |
| 237 | |
| 238 | free(gp2x_screen); |
| 239 | if (sounddev > 0) close(sounddev); |
| 240 | gp2x_usbjoy_deinit(); |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /* other stuff to be faked */ |
| 245 | void cpuctrl_init(void) |
| 246 | { |
| 247 | } |
| 248 | |
| 249 | void cpuctrl_deinit(void) |
| 250 | { |
| 251 | } |
| 252 | |
| 253 | void set_FCLK(unsigned MHZ) |
| 254 | { |
| 255 | } |
| 256 | |
| 257 | void set_RAM_Timings(int tRC, int tRAS, int tWR, int tMRD, int tRFC, int tRP, int tRCD) |
| 258 | { |
| 259 | } |
| 260 | |
| 261 | void set_gamma(int g100) |
| 262 | { |
| 263 | } |
| 264 | |
| 265 | |
| 266 | int mmuhack(void) |
| 267 | { |
| 268 | return 1; |
| 269 | } |
| 270 | |
| 271 | int mmuunhack(void) |
| 272 | { |
| 273 | return 1; |
| 274 | } |
| 275 | |
| 276 | void memset32(int *dest, int c, int count) |
| 277 | { |
| 278 | memset(dest, c, count*4); |
| 279 | } |
| 280 | |
| 281 | void spend_cycles(int c) |
| 282 | { |
| 283 | usleep(c/200); |
| 284 | } |
| 285 | |
| 286 | /* don't scale, just convert */ |
| 287 | void soft_scale(void *dst, unsigned short *pal, int line_offs, int lines) |
| 288 | { |
| 289 | unsigned char *src = (unsigned char *)dst + (line_offs + lines) * 320; |
| 290 | unsigned short *dest = (unsigned short *)dst + (line_offs + lines) * 320; |
| 291 | int count = lines*320; |
| 292 | |
| 293 | while (count--) |
| 294 | *(--dest) = pal[*(--src)]; |
| 295 | } |
| 296 | |