cc68a136 |
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 |
e5f426aa |
37 | #include "../common/arm_utils.h"\r |
cc68a136 |
38 | \r |
39 | volatile unsigned short *gp2x_memregs;\r |
40 | //static\r |
41 | volatile unsigned long *gp2x_memregl;\r |
42 | static void *gp2x_screens[4];\r |
43 | static int screensel = 0;\r |
44 | //static\r |
45 | int memdev = 0;\r |
46 | static int sounddev = 0, mixerdev = 0;\r |
47 | \r |
48 | void *gp2x_screen;\r |
49 | \r |
76276b0b |
50 | #define FRAMEBUFF_WHOLESIZE (0x30000*4) // 320*240*2 + some more\r |
51 | #define FRAMEBUFF_ADDR0 (0x4000000-FRAMEBUFF_WHOLESIZE)\r |
52 | #define FRAMEBUFF_ADDR1 (FRAMEBUFF_ADDR0+0x30000)\r |
53 | #define FRAMEBUFF_ADDR2 (FRAMEBUFF_ADDR1+0x30000)\r |
54 | #define FRAMEBUFF_ADDR3 (FRAMEBUFF_ADDR2+0x30000)\r |
cc68a136 |
55 | \r |
2433f409 |
56 | static const int gp2x_screenaddrs[4] = { FRAMEBUFF_ADDR0, FRAMEBUFF_ADDR1, FRAMEBUFF_ADDR2, FRAMEBUFF_ADDR3 };\r |
57 | static int gp2x_screenaddrs_use[4];\r |
85f8e929 |
58 | static unsigned short gp2x_screenaddr_old[4];\r |
cc68a136 |
59 | \r |
60 | \r |
61 | /* video stuff */\r |
62 | void gp2x_video_flip(void)\r |
63 | {\r |
2433f409 |
64 | unsigned short lsw = (unsigned short) gp2x_screenaddrs_use[screensel&3];\r |
65 | unsigned short msw = (unsigned short)(gp2x_screenaddrs_use[screensel&3] >> 16);\r |
cc68a136 |
66 | \r |
76276b0b |
67 | gp2x_memregs[0x2910>>1] = msw;\r |
68 | gp2x_memregs[0x2914>>1] = msw;\r |
2433f409 |
69 | gp2x_memregs[0x290E>>1] = lsw;\r |
70 | gp2x_memregs[0x2912>>1] = lsw;\r |
cc68a136 |
71 | \r |
72 | // jump to other buffer:\r |
73 | gp2x_screen = gp2x_screens[++screensel&3];\r |
74 | }\r |
75 | \r |
e11c5548 |
76 | /* doulblebuffered flip */\r |
77 | void gp2x_video_flip2(void)\r |
78 | {\r |
2433f409 |
79 | unsigned short msw = (unsigned short)(gp2x_screenaddrs_use[screensel&1] >> 16);\r |
cc68a136 |
80 | \r |
76276b0b |
81 | gp2x_memregs[0x2910>>1] = msw;\r |
82 | gp2x_memregs[0x2914>>1] = msw;\r |
83 | gp2x_memregs[0x290E>>1] = 0;\r |
84 | gp2x_memregs[0x2912>>1] = 0;\r |
e11c5548 |
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 |
cc68a136 |
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 |
e11c5548 |
95 | }\r |
96 | \r |
97 | \r |
98 | void gp2x_video_changemode(int bpp)\r |
99 | {\r |
100 | gp2x_video_changemode2(bpp);\r |
cc68a136 |
101 | \r |
76276b0b |
102 | gp2x_memset_all_buffers(0, 0, 320*240*2);\r |
cc68a136 |
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 |
2433f409 |
119 | void gp2x_video_RGB_setscaling(int ln_offs, int W, int H)\r |
cc68a136 |
120 | {\r |
121 | float escalaw, escalah;\r |
122 | int bpp = (gp2x_memregs[0x28DA>>1]>>9)&0x3;\r |
2433f409 |
123 | unsigned short scalw;\r |
124 | \r |
125 | // set offset\r |
126 | gp2x_screenaddrs_use[0] = gp2x_screenaddrs[0] + ln_offs * 320 * bpp;\r |
127 | gp2x_screenaddrs_use[1] = gp2x_screenaddrs[1] + ln_offs * 320 * bpp;\r |
128 | gp2x_screenaddrs_use[2] = gp2x_screenaddrs[2] + ln_offs * 320 * bpp;\r |
129 | gp2x_screenaddrs_use[3] = gp2x_screenaddrs[3] + ln_offs * 320 * bpp;\r |
cc68a136 |
130 | \r |
131 | escalaw = 1024.0; // RGB Horiz LCD\r |
132 | escalah = 320.0; // RGB Vert LCD\r |
133 | \r |
134 | if(gp2x_memregs[0x2800>>1]&0x100) //TV-Out\r |
135 | {\r |
136 | escalaw=489.0; // RGB Horiz TV (PAL, NTSC)\r |
137 | if (gp2x_memregs[0x2818>>1] == 287) //PAL\r |
138 | escalah=274.0; // RGB Vert TV PAL\r |
139 | else if (gp2x_memregs[0x2818>>1] == 239) //NTSC\r |
140 | escalah=331.0; // RGB Vert TV NTSC\r |
141 | }\r |
142 | \r |
143 | // scale horizontal\r |
2433f409 |
144 | scalw = (unsigned short)((float)escalaw *(W/320.0));\r |
145 | /* if there is no horizontal scaling, vertical doesn't work. Here is a nasty wrokaround... */\r |
146 | if (H != 240 && W == 320) scalw--;\r |
147 | gp2x_memregs[0x2906>>1]=scalw;\r |
cc68a136 |
148 | // scale vertical\r |
149 | gp2x_memregl[0x2908>>2]=(unsigned long)((float)escalah *bpp *(H/240.0));\r |
150 | }\r |
151 | \r |
152 | \r |
cc68a136 |
153 | void gp2x_video_wait_vsync(void)\r |
154 | {\r |
aae35e84 |
155 | unsigned short v = gp2x_memregs[0x1182>>1];\r |
156 | while (!((v ^ gp2x_memregs[0x1182>>1]) & 0x10)) spend_cycles(1024);\r |
cc68a136 |
157 | }\r |
158 | \r |
159 | \r |
a12e0116 |
160 | void gp2x_video_flush_cache(void)\r |
161 | {\r |
162 | // since we are using the mmu hack, we must flush the cache first\r |
163 | // (the params are most likely wrong, but they seem to work somehow)\r |
164 | //flushcache(addr, addr + 320*240*2, 0);\r |
165 | flushcache(gp2x_screen, (char *)gp2x_screen + 320*240*2, 0);\r |
166 | }\r |
167 | \r |
168 | \r |
860c6322 |
169 | void gp2x_memcpy_buffers(int buffers, void *data, int offset, int len)\r |
170 | {\r |
a9b3ffd3 |
171 | char *dst;\r |
172 | if (buffers & (1<<0)) { dst = (char *)gp2x_screens[0] + offset; if (dst != data) memcpy(dst, data, len); }\r |
173 | if (buffers & (1<<1)) { dst = (char *)gp2x_screens[1] + offset; if (dst != data) memcpy(dst, data, len); }\r |
174 | if (buffers & (1<<2)) { dst = (char *)gp2x_screens[2] + offset; if (dst != data) memcpy(dst, data, len); }\r |
175 | if (buffers & (1<<3)) { dst = (char *)gp2x_screens[3] + offset; if (dst != data) memcpy(dst, data, len); }\r |
860c6322 |
176 | }\r |
177 | \r |
178 | \r |
cc68a136 |
179 | void gp2x_memcpy_all_buffers(void *data, int offset, int len)\r |
180 | {\r |
860c6322 |
181 | gp2x_memcpy_buffers(0xf, data, offset, len);\r |
cc68a136 |
182 | }\r |
183 | \r |
184 | \r |
185 | void gp2x_memset_all_buffers(int offset, int byte, int len)\r |
186 | {\r |
187 | memset((char *)gp2x_screens[0] + offset, byte, len);\r |
188 | memset((char *)gp2x_screens[1] + offset, byte, len);\r |
189 | memset((char *)gp2x_screens[2] + offset, byte, len);\r |
190 | memset((char *)gp2x_screens[3] + offset, byte, len);\r |
191 | }\r |
192 | \r |
193 | \r |
e11c5548 |
194 | void gp2x_pd_clone_buffer2(void)\r |
195 | {\r |
a12e0116 |
196 | memcpy(gp2x_screen, gp2x_screens[2], 320*240*2);\r |
e11c5548 |
197 | }\r |
198 | \r |
199 | \r |
cc68a136 |
200 | unsigned long gp2x_joystick_read(int allow_usb_joy)\r |
201 | {\r |
202 | int i;\r |
203 | unsigned long value=(gp2x_memregs[0x1198>>1] & 0x00FF);\r |
204 | if(value==0xFD) value=0xFA;\r |
205 | if(value==0xF7) value=0xEB;\r |
206 | if(value==0xDF) value=0xAF;\r |
207 | if(value==0x7F) value=0xBE;\r |
208 | value = ~((gp2x_memregs[0x1184>>1] & 0xFF00) | value | (gp2x_memregs[0x1186>>1] << 16));\r |
209 | \r |
210 | if (allow_usb_joy && num_of_joys > 0) {\r |
211 | // check the usb joy as well..\r |
212 | gp2x_usbjoy_update();\r |
213 | for (i = 0; i < num_of_joys; i++)\r |
214 | value |= gp2x_usbjoy_check(i);\r |
215 | }\r |
216 | \r |
217 | return value;\r |
218 | }\r |
219 | \r |
220 | static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0;\r |
221 | \r |
222 | void gp2x_start_sound(int rate, int bits, int stereo)\r |
223 | {\r |
224 | int frag = 0, bsize, buffers;\r |
225 | \r |
226 | // if no settings change, we don't need to do anything\r |
227 | if (rate == s_oldrate && s_oldbits == bits && s_oldstereo == stereo) return;\r |
228 | \r |
229 | if (sounddev > 0) close(sounddev);\r |
230 | sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC);\r |
231 | if (sounddev == -1)\r |
232 | printf("open(\"/dev/dsp\") failed with %i\n", errno);\r |
233 | \r |
234 | ioctl(sounddev, SNDCTL_DSP_SPEED, &rate);\r |
235 | ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);\r |
236 | ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);\r |
237 | // calculate buffer size\r |
238 | buffers = 16;\r |
239 | bsize = rate / 32;\r |
240 | if (rate > 22050) { bsize*=4; buffers*=2; } // 44k mode seems to be very demanding\r |
241 | while ((bsize>>=1)) frag++;\r |
242 | frag |= buffers<<16; // 16 buffers\r |
243 | ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);\r |
244 | printf("gp2x_set_sound: %i/%ibit/%s, %i buffers of %i bytes\n",\r |
245 | rate, bits, stereo?"stereo":"mono", frag>>16, 1<<(frag&0xffff));\r |
246 | \r |
247 | s_oldrate = rate; s_oldbits = bits; s_oldstereo = stereo;\r |
248 | usleep(100000);\r |
249 | }\r |
250 | \r |
251 | \r |
252 | void gp2x_sound_write(void *buff, int len)\r |
253 | {\r |
254 | write(sounddev, buff, len);\r |
255 | }\r |
256 | \r |
42c7b147 |
257 | void gp2x_sound_sync(void)\r |
258 | {\r |
259 | ioctl(sounddev, SOUND_PCM_SYNC, 0);\r |
260 | }\r |
cc68a136 |
261 | \r |
262 | void gp2x_sound_volume(int l, int r)\r |
263 | {\r |
264 | l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;\r |
265 | l<<=8; l|=r;\r |
266 | ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/\r |
267 | }\r |
268 | \r |
269 | \r |
270 | /* 940 */\r |
271 | void Pause940(int yes)\r |
272 | {\r |
273 | if(yes)\r |
274 | gp2x_memregs[0x0904>>1] &= 0xFFFE;\r |
275 | else\r |
276 | gp2x_memregs[0x0904>>1] |= 1;\r |
277 | }\r |
278 | \r |
279 | \r |
b837b69b |
280 | void Reset940(int yes, int bank)\r |
cc68a136 |
281 | {\r |
76276b0b |
282 | gp2x_memregs[0x3B48>>1] = ((yes&1) << 7) | (bank & 0x03);\r |
cc68a136 |
283 | }\r |
284 | \r |
285 | \r |
286 | \r |
287 | /* common */\r |
288 | void gp2x_init(void)\r |
289 | {\r |
290 | printf("entering init()\n"); fflush(stdout);\r |
291 | \r |
292 | memdev = open("/dev/mem", O_RDWR);\r |
293 | if (memdev == -1)\r |
294 | {\r |
295 | printf("open(\"/dev/mem\") failed with %i\n", errno);\r |
296 | exit(1);\r |
297 | }\r |
298 | \r |
299 | gp2x_memregs = mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000);\r |
300 | printf("memregs are @ %p\n", gp2x_memregs);\r |
301 | if(gp2x_memregs == MAP_FAILED)\r |
302 | {\r |
303 | printf("mmap(memregs) failed with %i\n", errno);\r |
304 | exit(1);\r |
305 | }\r |
306 | gp2x_memregl = (unsigned long *) gp2x_memregs;\r |
307 | \r |
76276b0b |
308 | gp2x_screens[0] = mmap(0, FRAMEBUFF_WHOLESIZE, PROT_WRITE, MAP_SHARED, memdev, FRAMEBUFF_ADDR0);\r |
309 | if(gp2x_screens[0] == MAP_FAILED)\r |
cc68a136 |
310 | {\r |
311 | printf("mmap(gp2x_screen) failed with %i\n", errno);\r |
312 | exit(1);\r |
313 | }\r |
76276b0b |
314 | printf("framebuffers point to %p\n", gp2x_screens[0]);\r |
315 | gp2x_screens[1] = (char *) gp2x_screens[0]+0x30000;\r |
316 | gp2x_screens[2] = (char *) gp2x_screens[1]+0x30000;\r |
317 | gp2x_screens[3] = (char *) gp2x_screens[2]+0x30000;\r |
cc68a136 |
318 | \r |
319 | gp2x_screen = gp2x_screens[0];\r |
320 | screensel = 0;\r |
321 | \r |
85f8e929 |
322 | gp2x_screenaddr_old[0] = gp2x_memregs[0x290E>>1];\r |
323 | gp2x_screenaddr_old[1] = gp2x_memregs[0x2910>>1];\r |
324 | gp2x_screenaddr_old[2] = gp2x_memregs[0x2912>>1];\r |
325 | gp2x_screenaddr_old[3] = gp2x_memregs[0x2914>>1];\r |
326 | \r |
2433f409 |
327 | memcpy(gp2x_screenaddrs_use, gp2x_screenaddrs, sizeof(gp2x_screenaddrs));\r |
76276b0b |
328 | gp2x_memset_all_buffers(0, 0, 320*240*2);\r |
329 | \r |
cc68a136 |
330 | // snd\r |
331 | mixerdev = open("/dev/mixer", O_RDWR);\r |
332 | if (mixerdev == -1)\r |
333 | printf("open(\"/dev/mixer\") failed with %i\n", errno);\r |
334 | \r |
335 | /* init usb joys -GnoStiC */\r |
336 | gp2x_usbjoy_init();\r |
337 | \r |
338 | printf("exitting init()\n"); fflush(stdout);\r |
339 | }\r |
340 | \r |
341 | char *ext_menu = 0, *ext_state = 0;\r |
342 | \r |
343 | void gp2x_deinit(void)\r |
344 | {\r |
b837b69b |
345 | Reset940(1, 3);\r |
cc68a136 |
346 | Pause940(1);\r |
347 | \r |
348 | gp2x_video_changemode(15);\r |
85f8e929 |
349 | gp2x_memregs[0x290E>>1] = gp2x_screenaddr_old[0];\r |
350 | gp2x_memregs[0x2910>>1] = gp2x_screenaddr_old[1];\r |
351 | gp2x_memregs[0x2912>>1] = gp2x_screenaddr_old[2];\r |
352 | gp2x_memregs[0x2914>>1] = gp2x_screenaddr_old[3];\r |
353 | \r |
76276b0b |
354 | munmap(gp2x_screens[0], FRAMEBUFF_WHOLESIZE);\r |
cc68a136 |
355 | munmap((void *)gp2x_memregs, 0x10000);\r |
356 | close(memdev);\r |
357 | close(mixerdev);\r |
358 | if (sounddev > 0) close(sounddev);\r |
359 | \r |
360 | gp2x_usbjoy_deinit();\r |
361 | \r |
362 | printf("all done, running ");\r |
363 | \r |
364 | // Zaq121's alternative frontend support from MAME\r |
365 | if(ext_menu && ext_state) {\r |
366 | printf("%s -state %s\n", ext_menu, ext_state);\r |
367 | execl(ext_menu, ext_menu, "-state", ext_state, NULL);\r |
368 | } else if(ext_menu) {\r |
369 | printf("%s\n", ext_menu);\r |
370 | execl(ext_menu, ext_menu, NULL);\r |
371 | } else {\r |
372 | printf("gp2xmenu\n");\r |
373 | chdir("/usr/gp2x");\r |
374 | execl("gp2xmenu", "gp2xmenu", NULL);\r |
375 | }\r |
376 | }\r |
377 | \r |
378 | \r |