| 1 | /**\r |
| 2 | * All this is mostly based on rlyeh's minimal library.\r |
| 3 | * Copied here to review all his code and understand what's going on.\r |
| 4 | **/\r |
| 5 | \r |
| 6 | /*\r |
| 7 | \r |
| 8 | GP2X minimal library v0.A by rlyeh, (c) 2005. emulnation.info@rlyeh (swap it!)\r |
| 9 | \r |
| 10 | Thanks to Squidge, Robster, snaff, Reesy and NK, for the help & previous work! :-)\r |
| 11 | \r |
| 12 | License\r |
| 13 | =======\r |
| 14 | \r |
| 15 | Free for non-commercial projects (it would be nice receiving a mail from you).\r |
| 16 | Other cases, ask me first.\r |
| 17 | \r |
| 18 | GamePark Holdings is not allowed to use this library and/or use parts from it.\r |
| 19 | \r |
| 20 | */\r |
| 21 | \r |
| 22 | \r |
| 23 | #include <stdio.h>\r |
| 24 | #include <stdlib.h>\r |
| 25 | #include <string.h>\r |
| 26 | #include <unistd.h>\r |
| 27 | #include <sys/mman.h>\r |
| 28 | #include <sys/types.h>\r |
| 29 | #include <sys/stat.h>\r |
| 30 | #include <sys/ioctl.h>\r |
| 31 | #include <sys/soundcard.h>\r |
| 32 | #include <fcntl.h>\r |
| 33 | #include <errno.h>\r |
| 34 | \r |
| 35 | #include "gp2x.h"\r |
| 36 | #include "usbjoy.h"\r |
| 37 | \r |
| 38 | volatile unsigned short *gp2x_memregs;\r |
| 39 | //static\r |
| 40 | volatile unsigned long *gp2x_memregl;\r |
| 41 | static void *gp2x_screens[4];\r |
| 42 | static int screensel = 0;\r |
| 43 | //static\r |
| 44 | int memdev = 0;\r |
| 45 | static int sounddev = 0, mixerdev = 0;\r |
| 46 | \r |
| 47 | void *gp2x_screen;\r |
| 48 | \r |
| 49 | #define FRAMEBUFF_WHOLESIZE (0x30000*4) // 320*240*2 + some more\r |
| 50 | #define FRAMEBUFF_ADDR0 (0x4000000-FRAMEBUFF_WHOLESIZE)\r |
| 51 | #define FRAMEBUFF_ADDR1 (FRAMEBUFF_ADDR0+0x30000)\r |
| 52 | #define FRAMEBUFF_ADDR2 (FRAMEBUFF_ADDR1+0x30000)\r |
| 53 | #define FRAMEBUFF_ADDR3 (FRAMEBUFF_ADDR2+0x30000)\r |
| 54 | \r |
| 55 | static const int gp2x_screenaddrs[4] = { FRAMEBUFF_ADDR0, FRAMEBUFF_ADDR1, FRAMEBUFF_ADDR2, FRAMEBUFF_ADDR3 };\r |
| 56 | static int gp2x_screenaddrs_use[4];\r |
| 57 | static unsigned short gp2x_screenaddr_old[4];\r |
| 58 | \r |
| 59 | \r |
| 60 | /* video stuff */\r |
| 61 | void gp2x_video_flip(void)\r |
| 62 | {\r |
| 63 | unsigned short lsw = (unsigned short) gp2x_screenaddrs_use[screensel&3];\r |
| 64 | unsigned short msw = (unsigned short)(gp2x_screenaddrs_use[screensel&3] >> 16);\r |
| 65 | \r |
| 66 | gp2x_memregs[0x2910>>1] = msw;\r |
| 67 | gp2x_memregs[0x2914>>1] = msw;\r |
| 68 | gp2x_memregs[0x290E>>1] = lsw;\r |
| 69 | gp2x_memregs[0x2912>>1] = lsw;\r |
| 70 | \r |
| 71 | // jump to other buffer:\r |
| 72 | gp2x_screen = gp2x_screens[++screensel&3];\r |
| 73 | }\r |
| 74 | \r |
| 75 | /* doulblebuffered flip */\r |
| 76 | void gp2x_video_flip2(void)\r |
| 77 | {\r |
| 78 | unsigned short msw = (unsigned short)(gp2x_screenaddrs_use[screensel&1] >> 16);\r |
| 79 | \r |
| 80 | gp2x_memregs[0x2910>>1] = msw;\r |
| 81 | gp2x_memregs[0x2914>>1] = msw;\r |
| 82 | gp2x_memregs[0x290E>>1] = 0;\r |
| 83 | gp2x_memregs[0x2912>>1] = 0;\r |
| 84 | \r |
| 85 | // jump to other buffer:\r |
| 86 | gp2x_screen = gp2x_screens[++screensel&1];\r |
| 87 | }\r |
| 88 | \r |
| 89 | \r |
| 90 | void gp2x_video_changemode2(int bpp)\r |
| 91 | {\r |
| 92 | gp2x_memregs[0x28DA>>1]=(((bpp+1)/8)<<9)|0xAB; /*8/15/16/24bpp...*/\r |
| 93 | gp2x_memregs[0x290C>>1]=320*((bpp+1)/8); /*line width in bytes*/\r |
| 94 | }\r |
| 95 | \r |
| 96 | \r |
| 97 | void gp2x_video_changemode(int bpp)\r |
| 98 | {\r |
| 99 | gp2x_video_changemode2(bpp);\r |
| 100 | \r |
| 101 | gp2x_memset_all_buffers(0, 0, 320*240*2);\r |
| 102 | gp2x_video_flip();\r |
| 103 | }\r |
| 104 | \r |
| 105 | \r |
| 106 | void gp2x_video_setpalette(int *pal, int len)\r |
| 107 | {\r |
| 108 | unsigned short *g=(unsigned short *)pal;\r |
| 109 | volatile unsigned short *memreg = &gp2x_memregs[0x295A>>1];\r |
| 110 | gp2x_memregs[0x2958>>1] = 0;\r |
| 111 | \r |
| 112 | len *= 2;\r |
| 113 | while(len--) *memreg=*g++;\r |
| 114 | }\r |
| 115 | \r |
| 116 | \r |
| 117 | // TV Compatible function //\r |
| 118 | void gp2x_video_RGB_setscaling(int ln_offs, int W, int H)\r |
| 119 | {\r |
| 120 | float escalaw, escalah;\r |
| 121 | int bpp = (gp2x_memregs[0x28DA>>1]>>9)&0x3;\r |
| 122 | unsigned short scalw;\r |
| 123 | \r |
| 124 | // set offset\r |
| 125 | gp2x_screenaddrs_use[0] = gp2x_screenaddrs[0] + ln_offs * 320 * bpp;\r |
| 126 | gp2x_screenaddrs_use[1] = gp2x_screenaddrs[1] + ln_offs * 320 * bpp;\r |
| 127 | gp2x_screenaddrs_use[2] = gp2x_screenaddrs[2] + ln_offs * 320 * bpp;\r |
| 128 | gp2x_screenaddrs_use[3] = gp2x_screenaddrs[3] + ln_offs * 320 * bpp;\r |
| 129 | \r |
| 130 | escalaw = 1024.0; // RGB Horiz LCD\r |
| 131 | escalah = 320.0; // RGB Vert LCD\r |
| 132 | \r |
| 133 | if(gp2x_memregs[0x2800>>1]&0x100) //TV-Out\r |
| 134 | {\r |
| 135 | escalaw=489.0; // RGB Horiz TV (PAL, NTSC)\r |
| 136 | if (gp2x_memregs[0x2818>>1] == 287) //PAL\r |
| 137 | escalah=274.0; // RGB Vert TV PAL\r |
| 138 | else if (gp2x_memregs[0x2818>>1] == 239) //NTSC\r |
| 139 | escalah=331.0; // RGB Vert TV NTSC\r |
| 140 | }\r |
| 141 | \r |
| 142 | // scale horizontal\r |
| 143 | scalw = (unsigned short)((float)escalaw *(W/320.0));\r |
| 144 | /* if there is no horizontal scaling, vertical doesn't work. Here is a nasty wrokaround... */\r |
| 145 | if (H != 240 && W == 320) scalw--;\r |
| 146 | gp2x_memregs[0x2906>>1]=scalw;\r |
| 147 | // scale vertical\r |
| 148 | gp2x_memregl[0x2908>>2]=(unsigned long)((float)escalah *bpp *(H/240.0));\r |
| 149 | }\r |
| 150 | \r |
| 151 | \r |
| 152 | /* LCD updates @ 80Hz? */\r |
| 153 | void gp2x_video_wait_vsync(void)\r |
| 154 | {\r |
| 155 | gp2x_memregs[0x2846>>1] = 0x20|2; //(gp2x_memregs[0x2846>>1] | 0x20) & ~2;\r |
| 156 | while(!(gp2x_memregs[0x2846>>1] & 2));// usleep(1);\r |
| 157 | }\r |
| 158 | \r |
| 159 | \r |
| 160 | void gp2x_memcpy_buffers(int buffers, void *data, int offset, int len)\r |
| 161 | {\r |
| 162 | if (buffers & (1<<0)) memcpy((char *)gp2x_screens[0] + offset, data, len);\r |
| 163 | if (buffers & (1<<1)) memcpy((char *)gp2x_screens[1] + offset, data, len);\r |
| 164 | if (buffers & (1<<2)) memcpy((char *)gp2x_screens[2] + offset, data, len);\r |
| 165 | if (buffers & (1<<3)) memcpy((char *)gp2x_screens[3] + offset, data, len);\r |
| 166 | }\r |
| 167 | \r |
| 168 | \r |
| 169 | void gp2x_memcpy_all_buffers(void *data, int offset, int len)\r |
| 170 | {\r |
| 171 | gp2x_memcpy_buffers(0xf, data, offset, len);\r |
| 172 | }\r |
| 173 | \r |
| 174 | \r |
| 175 | void gp2x_memset_all_buffers(int offset, int byte, int len)\r |
| 176 | {\r |
| 177 | memset((char *)gp2x_screens[0] + offset, byte, len);\r |
| 178 | memset((char *)gp2x_screens[1] + offset, byte, len);\r |
| 179 | memset((char *)gp2x_screens[2] + offset, byte, len);\r |
| 180 | memset((char *)gp2x_screens[3] + offset, byte, len);\r |
| 181 | }\r |
| 182 | \r |
| 183 | \r |
| 184 | void gp2x_pd_clone_buffer2(void)\r |
| 185 | {\r |
| 186 | memcpy(gp2x_screen, gp2x_screens[2], 320*240);\r |
| 187 | memset(gp2x_screen, 0xe0, 320*8);\r |
| 188 | memset(gp2x_screen + 320*232, 0xe0, 320*8);\r |
| 189 | }\r |
| 190 | \r |
| 191 | \r |
| 192 | unsigned long gp2x_joystick_read(int allow_usb_joy)\r |
| 193 | {\r |
| 194 | int i;\r |
| 195 | unsigned long value=(gp2x_memregs[0x1198>>1] & 0x00FF);\r |
| 196 | if(value==0xFD) value=0xFA;\r |
| 197 | if(value==0xF7) value=0xEB;\r |
| 198 | if(value==0xDF) value=0xAF;\r |
| 199 | if(value==0x7F) value=0xBE;\r |
| 200 | value = ~((gp2x_memregs[0x1184>>1] & 0xFF00) | value | (gp2x_memregs[0x1186>>1] << 16));\r |
| 201 | \r |
| 202 | if (allow_usb_joy && num_of_joys > 0) {\r |
| 203 | // check the usb joy as well..\r |
| 204 | gp2x_usbjoy_update();\r |
| 205 | for (i = 0; i < num_of_joys; i++)\r |
| 206 | value |= gp2x_usbjoy_check(i);\r |
| 207 | }\r |
| 208 | \r |
| 209 | return value;\r |
| 210 | }\r |
| 211 | \r |
| 212 | static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0;\r |
| 213 | \r |
| 214 | void gp2x_start_sound(int rate, int bits, int stereo)\r |
| 215 | {\r |
| 216 | int frag = 0, bsize, buffers;\r |
| 217 | \r |
| 218 | // if no settings change, we don't need to do anything\r |
| 219 | if (rate == s_oldrate && s_oldbits == bits && s_oldstereo == stereo) return;\r |
| 220 | \r |
| 221 | if (sounddev > 0) close(sounddev);\r |
| 222 | sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC);\r |
| 223 | if (sounddev == -1)\r |
| 224 | printf("open(\"/dev/dsp\") failed with %i\n", errno);\r |
| 225 | \r |
| 226 | ioctl(sounddev, SNDCTL_DSP_SPEED, &rate);\r |
| 227 | ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);\r |
| 228 | ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);\r |
| 229 | // calculate buffer size\r |
| 230 | buffers = 16;\r |
| 231 | bsize = rate / 32;\r |
| 232 | if (rate > 22050) { bsize*=4; buffers*=2; } // 44k mode seems to be very demanding\r |
| 233 | while ((bsize>>=1)) frag++;\r |
| 234 | frag |= buffers<<16; // 16 buffers\r |
| 235 | ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);\r |
| 236 | printf("gp2x_set_sound: %i/%ibit/%s, %i buffers of %i bytes\n",\r |
| 237 | rate, bits, stereo?"stereo":"mono", frag>>16, 1<<(frag&0xffff));\r |
| 238 | \r |
| 239 | s_oldrate = rate; s_oldbits = bits; s_oldstereo = stereo;\r |
| 240 | usleep(100000);\r |
| 241 | }\r |
| 242 | \r |
| 243 | \r |
| 244 | void gp2x_sound_write(void *buff, int len)\r |
| 245 | {\r |
| 246 | write(sounddev, buff, len);\r |
| 247 | }\r |
| 248 | \r |
| 249 | void gp2x_sound_sync(void)\r |
| 250 | {\r |
| 251 | ioctl(sounddev, SOUND_PCM_SYNC, 0);\r |
| 252 | }\r |
| 253 | \r |
| 254 | void gp2x_sound_volume(int l, int r)\r |
| 255 | {\r |
| 256 | l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;\r |
| 257 | l<<=8; l|=r;\r |
| 258 | ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/\r |
| 259 | }\r |
| 260 | \r |
| 261 | \r |
| 262 | /* 940 */\r |
| 263 | void Pause940(int yes)\r |
| 264 | {\r |
| 265 | if(yes)\r |
| 266 | gp2x_memregs[0x0904>>1] &= 0xFFFE;\r |
| 267 | else\r |
| 268 | gp2x_memregs[0x0904>>1] |= 1;\r |
| 269 | }\r |
| 270 | \r |
| 271 | \r |
| 272 | void Reset940(int yes, int bank)\r |
| 273 | {\r |
| 274 | gp2x_memregs[0x3B48>>1] = ((yes&1) << 7) | (bank & 0x03);\r |
| 275 | }\r |
| 276 | \r |
| 277 | \r |
| 278 | \r |
| 279 | /* common */\r |
| 280 | void gp2x_init(void)\r |
| 281 | {\r |
| 282 | printf("entering init()\n"); fflush(stdout);\r |
| 283 | \r |
| 284 | memdev = open("/dev/mem", O_RDWR);\r |
| 285 | if (memdev == -1)\r |
| 286 | {\r |
| 287 | printf("open(\"/dev/mem\") failed with %i\n", errno);\r |
| 288 | exit(1);\r |
| 289 | }\r |
| 290 | \r |
| 291 | gp2x_memregs = mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000);\r |
| 292 | printf("memregs are @ %p\n", gp2x_memregs);\r |
| 293 | if(gp2x_memregs == MAP_FAILED)\r |
| 294 | {\r |
| 295 | printf("mmap(memregs) failed with %i\n", errno);\r |
| 296 | exit(1);\r |
| 297 | }\r |
| 298 | gp2x_memregl = (unsigned long *) gp2x_memregs;\r |
| 299 | \r |
| 300 | gp2x_screens[0] = mmap(0, FRAMEBUFF_WHOLESIZE, PROT_WRITE, MAP_SHARED, memdev, FRAMEBUFF_ADDR0);\r |
| 301 | if(gp2x_screens[0] == MAP_FAILED)\r |
| 302 | {\r |
| 303 | printf("mmap(gp2x_screen) failed with %i\n", errno);\r |
| 304 | exit(1);\r |
| 305 | }\r |
| 306 | printf("framebuffers point to %p\n", gp2x_screens[0]);\r |
| 307 | gp2x_screens[1] = (char *) gp2x_screens[0]+0x30000;\r |
| 308 | gp2x_screens[2] = (char *) gp2x_screens[1]+0x30000;\r |
| 309 | gp2x_screens[3] = (char *) gp2x_screens[2]+0x30000;\r |
| 310 | \r |
| 311 | gp2x_screen = gp2x_screens[0];\r |
| 312 | screensel = 0;\r |
| 313 | \r |
| 314 | gp2x_screenaddr_old[0] = gp2x_memregs[0x290E>>1];\r |
| 315 | gp2x_screenaddr_old[1] = gp2x_memregs[0x2910>>1];\r |
| 316 | gp2x_screenaddr_old[2] = gp2x_memregs[0x2912>>1];\r |
| 317 | gp2x_screenaddr_old[3] = gp2x_memregs[0x2914>>1];\r |
| 318 | \r |
| 319 | memcpy(gp2x_screenaddrs_use, gp2x_screenaddrs, sizeof(gp2x_screenaddrs));\r |
| 320 | gp2x_memset_all_buffers(0, 0, 320*240*2);\r |
| 321 | \r |
| 322 | // snd\r |
| 323 | mixerdev = open("/dev/mixer", O_RDWR);\r |
| 324 | if (mixerdev == -1)\r |
| 325 | printf("open(\"/dev/mixer\") failed with %i\n", errno);\r |
| 326 | \r |
| 327 | /* init usb joys -GnoStiC */\r |
| 328 | gp2x_usbjoy_init();\r |
| 329 | \r |
| 330 | printf("exitting init()\n"); fflush(stdout);\r |
| 331 | }\r |
| 332 | \r |
| 333 | char *ext_menu = 0, *ext_state = 0;\r |
| 334 | \r |
| 335 | void gp2x_deinit(void)\r |
| 336 | {\r |
| 337 | Reset940(1, 3);\r |
| 338 | Pause940(1);\r |
| 339 | \r |
| 340 | gp2x_video_changemode(15);\r |
| 341 | gp2x_memregs[0x290E>>1] = gp2x_screenaddr_old[0];\r |
| 342 | gp2x_memregs[0x2910>>1] = gp2x_screenaddr_old[1];\r |
| 343 | gp2x_memregs[0x2912>>1] = gp2x_screenaddr_old[2];\r |
| 344 | gp2x_memregs[0x2914>>1] = gp2x_screenaddr_old[3];\r |
| 345 | \r |
| 346 | munmap(gp2x_screens[0], FRAMEBUFF_WHOLESIZE);\r |
| 347 | munmap((void *)gp2x_memregs, 0x10000);\r |
| 348 | close(memdev);\r |
| 349 | close(mixerdev);\r |
| 350 | if (sounddev > 0) close(sounddev);\r |
| 351 | \r |
| 352 | gp2x_usbjoy_deinit();\r |
| 353 | \r |
| 354 | printf("all done, running ");\r |
| 355 | \r |
| 356 | // Zaq121's alternative frontend support from MAME\r |
| 357 | if(ext_menu && ext_state) {\r |
| 358 | printf("%s -state %s\n", ext_menu, ext_state);\r |
| 359 | execl(ext_menu, ext_menu, "-state", ext_state, NULL);\r |
| 360 | } else if(ext_menu) {\r |
| 361 | printf("%s\n", ext_menu);\r |
| 362 | execl(ext_menu, ext_menu, NULL);\r |
| 363 | } else {\r |
| 364 | printf("gp2xmenu\n");\r |
| 365 | chdir("/usr/gp2x");\r |
| 366 | execl("gp2xmenu", "gp2xmenu", NULL);\r |
| 367 | }\r |
| 368 | }\r |
| 369 | \r |
| 370 | \r |