| 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_ADDR0 0x4000000-640*480\r |
| 50 | #define FRAMEBUFF_ADDR1 0x4000000-640*480*2\r |
| 51 | #define FRAMEBUFF_ADDR2 0x4000000-640*480*3\r |
| 52 | #define FRAMEBUFF_ADDR3 0x4000000-640*480*4\r |
| 53 | \r |
| 54 | static const int gp2x_screenaddrs[] = { FRAMEBUFF_ADDR0, FRAMEBUFF_ADDR1, FRAMEBUFF_ADDR2, FRAMEBUFF_ADDR3 };\r |
| 55 | static unsigned short gp2x_screenaddr_old[4];\r |
| 56 | \r |
| 57 | \r |
| 58 | /* video stuff */\r |
| 59 | void gp2x_video_flip(void)\r |
| 60 | {\r |
| 61 | unsigned int address = gp2x_screenaddrs[screensel&3];\r |
| 62 | unsigned short msb16 = (unsigned short)(address >> 16);\r |
| 63 | unsigned short lsb16 = (unsigned short)(address);\r |
| 64 | \r |
| 65 | gp2x_memregs[0x290E>>1] = lsb16;\r |
| 66 | gp2x_memregs[0x2910>>1] = msb16;\r |
| 67 | gp2x_memregs[0x2912>>1] = lsb16;\r |
| 68 | gp2x_memregs[0x2914>>1] = msb16;\r |
| 69 | \r |
| 70 | // jump to other buffer:\r |
| 71 | gp2x_screen = gp2x_screens[++screensel&3];\r |
| 72 | }\r |
| 73 | \r |
| 74 | /* doulblebuffered flip */\r |
| 75 | void gp2x_video_flip2(void)\r |
| 76 | {\r |
| 77 | unsigned int address = gp2x_screenaddrs[screensel&1];\r |
| 78 | unsigned short msb16 = (unsigned short)(address >> 16);\r |
| 79 | unsigned short lsb16 = (unsigned short)(address);\r |
| 80 | \r |
| 81 | gp2x_memregs[0x290E>>1] = lsb16;\r |
| 82 | gp2x_memregs[0x2910>>1] = msb16;\r |
| 83 | gp2x_memregs[0x2912>>1] = lsb16;\r |
| 84 | gp2x_memregs[0x2914>>1] = msb16;\r |
| 85 | \r |
| 86 | // jump to other buffer:\r |
| 87 | gp2x_screen = gp2x_screens[++screensel&1];\r |
| 88 | }\r |
| 89 | \r |
| 90 | \r |
| 91 | void gp2x_video_changemode2(int bpp)\r |
| 92 | {\r |
| 93 | gp2x_memregs[0x28DA>>1]=(((bpp+1)/8)<<9)|0xAB; /*8/15/16/24bpp...*/\r |
| 94 | gp2x_memregs[0x290C>>1]=320*((bpp+1)/8); /*line width in bytes*/\r |
| 95 | }\r |
| 96 | \r |
| 97 | \r |
| 98 | void gp2x_video_changemode(int bpp)\r |
| 99 | {\r |
| 100 | gp2x_video_changemode2(bpp);\r |
| 101 | \r |
| 102 | gp2x_memset_all_buffers(0, 0, 640*480);\r |
| 103 | gp2x_video_flip();\r |
| 104 | }\r |
| 105 | \r |
| 106 | \r |
| 107 | void gp2x_video_setpalette(int *pal, int len)\r |
| 108 | {\r |
| 109 | unsigned short *g=(unsigned short *)pal;\r |
| 110 | volatile unsigned short *memreg = &gp2x_memregs[0x295A>>1];\r |
| 111 | gp2x_memregs[0x2958>>1] = 0;\r |
| 112 | \r |
| 113 | len *= 2;\r |
| 114 | while(len--) *memreg=*g++;\r |
| 115 | }\r |
| 116 | \r |
| 117 | \r |
| 118 | // TV Compatible function //\r |
| 119 | void gp2x_video_RGB_setscaling(int W, int H)\r |
| 120 | {\r |
| 121 | float escalaw, escalah;\r |
| 122 | int bpp = (gp2x_memregs[0x28DA>>1]>>9)&0x3;\r |
| 123 | \r |
| 124 | escalaw = 1024.0; // RGB Horiz LCD\r |
| 125 | escalah = 320.0; // RGB Vert LCD\r |
| 126 | \r |
| 127 | if(gp2x_memregs[0x2800>>1]&0x100) //TV-Out\r |
| 128 | {\r |
| 129 | escalaw=489.0; // RGB Horiz TV (PAL, NTSC)\r |
| 130 | if (gp2x_memregs[0x2818>>1] == 287) //PAL\r |
| 131 | escalah=274.0; // RGB Vert TV PAL\r |
| 132 | else if (gp2x_memregs[0x2818>>1] == 239) //NTSC\r |
| 133 | escalah=331.0; // RGB Vert TV NTSC\r |
| 134 | }\r |
| 135 | \r |
| 136 | // scale horizontal\r |
| 137 | gp2x_memregs[0x2906>>1]=(unsigned short)((float)escalaw *(W/320.0));\r |
| 138 | // scale vertical\r |
| 139 | gp2x_memregl[0x2908>>2]=(unsigned long)((float)escalah *bpp *(H/240.0));\r |
| 140 | }\r |
| 141 | \r |
| 142 | \r |
| 143 | /* LCD updates @ 80Hz? */\r |
| 144 | void gp2x_video_wait_vsync(void)\r |
| 145 | {\r |
| 146 | gp2x_memregs[0x2846>>1] = 0x20|2; //(gp2x_memregs[0x2846>>1] | 0x20) & ~2;\r |
| 147 | while(!(gp2x_memregs[0x2846>>1] & 2));// usleep(1);\r |
| 148 | }\r |
| 149 | \r |
| 150 | \r |
| 151 | void gp2x_memcpy_all_buffers(void *data, int offset, int len)\r |
| 152 | {\r |
| 153 | memcpy((char *)gp2x_screens[0] + offset, data, len);\r |
| 154 | memcpy((char *)gp2x_screens[1] + offset, data, len);\r |
| 155 | memcpy((char *)gp2x_screens[2] + offset, data, len);\r |
| 156 | memcpy((char *)gp2x_screens[3] + offset, data, len);\r |
| 157 | }\r |
| 158 | \r |
| 159 | \r |
| 160 | void gp2x_memset_all_buffers(int offset, int byte, int len)\r |
| 161 | {\r |
| 162 | memset((char *)gp2x_screens[0] + offset, byte, len);\r |
| 163 | memset((char *)gp2x_screens[1] + offset, byte, len);\r |
| 164 | memset((char *)gp2x_screens[2] + offset, byte, len);\r |
| 165 | memset((char *)gp2x_screens[3] + offset, byte, len);\r |
| 166 | }\r |
| 167 | \r |
| 168 | \r |
| 169 | void gp2x_pd_clone_buffer2(void)\r |
| 170 | {\r |
| 171 | memcpy(gp2x_screen, gp2x_screens[2], 320*240);\r |
| 172 | memset(gp2x_screen, 0xe0, 320*8);\r |
| 173 | memset(gp2x_screen + 320*232, 0xe0, 320*8);\r |
| 174 | }\r |
| 175 | \r |
| 176 | \r |
| 177 | unsigned long gp2x_joystick_read(int allow_usb_joy)\r |
| 178 | {\r |
| 179 | int i;\r |
| 180 | unsigned long value=(gp2x_memregs[0x1198>>1] & 0x00FF);\r |
| 181 | if(value==0xFD) value=0xFA;\r |
| 182 | if(value==0xF7) value=0xEB;\r |
| 183 | if(value==0xDF) value=0xAF;\r |
| 184 | if(value==0x7F) value=0xBE;\r |
| 185 | value = ~((gp2x_memregs[0x1184>>1] & 0xFF00) | value | (gp2x_memregs[0x1186>>1] << 16));\r |
| 186 | \r |
| 187 | if (allow_usb_joy && num_of_joys > 0) {\r |
| 188 | // check the usb joy as well..\r |
| 189 | gp2x_usbjoy_update();\r |
| 190 | for (i = 0; i < num_of_joys; i++)\r |
| 191 | value |= gp2x_usbjoy_check(i);\r |
| 192 | }\r |
| 193 | \r |
| 194 | return value;\r |
| 195 | }\r |
| 196 | \r |
| 197 | static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0;\r |
| 198 | \r |
| 199 | void gp2x_start_sound(int rate, int bits, int stereo)\r |
| 200 | {\r |
| 201 | int frag = 0, bsize, buffers;\r |
| 202 | \r |
| 203 | // if no settings change, we don't need to do anything\r |
| 204 | if (rate == s_oldrate && s_oldbits == bits && s_oldstereo == stereo) return;\r |
| 205 | \r |
| 206 | if (sounddev > 0) close(sounddev);\r |
| 207 | sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC);\r |
| 208 | if (sounddev == -1)\r |
| 209 | printf("open(\"/dev/dsp\") failed with %i\n", errno);\r |
| 210 | \r |
| 211 | ioctl(sounddev, SNDCTL_DSP_SPEED, &rate);\r |
| 212 | ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);\r |
| 213 | ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);\r |
| 214 | // calculate buffer size\r |
| 215 | buffers = 16;\r |
| 216 | bsize = rate / 32;\r |
| 217 | if (rate > 22050) { bsize*=4; buffers*=2; } // 44k mode seems to be very demanding\r |
| 218 | while ((bsize>>=1)) frag++;\r |
| 219 | frag |= buffers<<16; // 16 buffers\r |
| 220 | ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);\r |
| 221 | printf("gp2x_set_sound: %i/%ibit/%s, %i buffers of %i bytes\n",\r |
| 222 | rate, bits, stereo?"stereo":"mono", frag>>16, 1<<(frag&0xffff));\r |
| 223 | \r |
| 224 | s_oldrate = rate; s_oldbits = bits; s_oldstereo = stereo;\r |
| 225 | usleep(100000);\r |
| 226 | }\r |
| 227 | \r |
| 228 | \r |
| 229 | void gp2x_sound_write(void *buff, int len)\r |
| 230 | {\r |
| 231 | write(sounddev, buff, len);\r |
| 232 | }\r |
| 233 | \r |
| 234 | void gp2x_sound_sync(void)\r |
| 235 | {\r |
| 236 | ioctl(sounddev, SOUND_PCM_SYNC, 0);\r |
| 237 | }\r |
| 238 | \r |
| 239 | void gp2x_sound_volume(int l, int r)\r |
| 240 | {\r |
| 241 | l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;\r |
| 242 | l<<=8; l|=r;\r |
| 243 | ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/\r |
| 244 | }\r |
| 245 | \r |
| 246 | \r |
| 247 | /* 940 */\r |
| 248 | void Pause940(int yes)\r |
| 249 | {\r |
| 250 | if(yes)\r |
| 251 | gp2x_memregs[0x0904>>1] &= 0xFFFE;\r |
| 252 | else\r |
| 253 | gp2x_memregs[0x0904>>1] |= 1;\r |
| 254 | }\r |
| 255 | \r |
| 256 | \r |
| 257 | void Reset940(int yes, int bank)\r |
| 258 | {\r |
| 259 | gp2x_memregs[0x3B48>>1] = ((yes&1) << 7) | (bank & 0x03); /* bank=3 */\r |
| 260 | }\r |
| 261 | \r |
| 262 | \r |
| 263 | \r |
| 264 | /* common */\r |
| 265 | void gp2x_init(void)\r |
| 266 | {\r |
| 267 | printf("entering init()\n"); fflush(stdout);\r |
| 268 | \r |
| 269 | memdev = open("/dev/mem", O_RDWR);\r |
| 270 | if (memdev == -1)\r |
| 271 | {\r |
| 272 | printf("open(\"/dev/mem\") failed with %i\n", errno);\r |
| 273 | exit(1);\r |
| 274 | }\r |
| 275 | \r |
| 276 | gp2x_memregs = mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000);\r |
| 277 | printf("memregs are @ %p\n", gp2x_memregs);\r |
| 278 | if(gp2x_memregs == MAP_FAILED)\r |
| 279 | {\r |
| 280 | printf("mmap(memregs) failed with %i\n", errno);\r |
| 281 | exit(1);\r |
| 282 | }\r |
| 283 | gp2x_memregl = (unsigned long *) gp2x_memregs;\r |
| 284 | \r |
| 285 | gp2x_screens[3] = mmap(0, 640*480*4, PROT_WRITE, MAP_SHARED, memdev, FRAMEBUFF_ADDR3);\r |
| 286 | if(gp2x_screens[3] == MAP_FAILED)\r |
| 287 | {\r |
| 288 | printf("mmap(gp2x_screen) failed with %i\n", errno);\r |
| 289 | exit(1);\r |
| 290 | }\r |
| 291 | printf("framebuffers point to %p\n", gp2x_screens[3]);\r |
| 292 | gp2x_screens[2] = (char *) gp2x_screens[3]+640*480;\r |
| 293 | gp2x_screens[1] = (char *) gp2x_screens[2]+640*480;\r |
| 294 | gp2x_screens[0] = (char *) gp2x_screens[1]+640*480;\r |
| 295 | \r |
| 296 | gp2x_screen = gp2x_screens[0];\r |
| 297 | screensel = 0;\r |
| 298 | \r |
| 299 | gp2x_screenaddr_old[0] = gp2x_memregs[0x290E>>1];\r |
| 300 | gp2x_screenaddr_old[1] = gp2x_memregs[0x2910>>1];\r |
| 301 | gp2x_screenaddr_old[2] = gp2x_memregs[0x2912>>1];\r |
| 302 | gp2x_screenaddr_old[3] = gp2x_memregs[0x2914>>1];\r |
| 303 | \r |
| 304 | // snd\r |
| 305 | mixerdev = open("/dev/mixer", O_RDWR);\r |
| 306 | if (mixerdev == -1)\r |
| 307 | printf("open(\"/dev/mixer\") failed with %i\n", errno);\r |
| 308 | \r |
| 309 | /* init usb joys -GnoStiC */\r |
| 310 | gp2x_usbjoy_init();\r |
| 311 | \r |
| 312 | printf("exitting init()\n"); fflush(stdout);\r |
| 313 | }\r |
| 314 | \r |
| 315 | char *ext_menu = 0, *ext_state = 0;\r |
| 316 | \r |
| 317 | void gp2x_deinit(void)\r |
| 318 | {\r |
| 319 | Reset940(1, 3);\r |
| 320 | Pause940(1);\r |
| 321 | \r |
| 322 | gp2x_video_changemode(15);\r |
| 323 | gp2x_memregs[0x290E>>1] = gp2x_screenaddr_old[0];\r |
| 324 | gp2x_memregs[0x2910>>1] = gp2x_screenaddr_old[1];\r |
| 325 | gp2x_memregs[0x2912>>1] = gp2x_screenaddr_old[2];\r |
| 326 | gp2x_memregs[0x2914>>1] = gp2x_screenaddr_old[3];\r |
| 327 | \r |
| 328 | munmap(gp2x_screens[0], 640*480*4);\r |
| 329 | munmap((void *)gp2x_memregs, 0x10000);\r |
| 330 | close(memdev);\r |
| 331 | close(mixerdev);\r |
| 332 | if (sounddev > 0) close(sounddev);\r |
| 333 | \r |
| 334 | gp2x_usbjoy_deinit();\r |
| 335 | \r |
| 336 | printf("all done, running ");\r |
| 337 | \r |
| 338 | // Zaq121's alternative frontend support from MAME\r |
| 339 | if(ext_menu && ext_state) {\r |
| 340 | printf("%s -state %s\n", ext_menu, ext_state);\r |
| 341 | execl(ext_menu, ext_menu, "-state", ext_state, NULL);\r |
| 342 | } else if(ext_menu) {\r |
| 343 | printf("%s\n", ext_menu);\r |
| 344 | execl(ext_menu, ext_menu, NULL);\r |
| 345 | } else {\r |
| 346 | printf("gp2xmenu\n");\r |
| 347 | chdir("/usr/gp2x");\r |
| 348 | execl("gp2xmenu", "gp2xmenu", NULL);\r |
| 349 | }\r |
| 350 | }\r |
| 351 | \r |
| 352 | \r |