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