8b6232a6 |
1 | /* |
2 | Parts used from cpuctrl, Copyright (C) 2005 Hermes/PS2Reality |
3 | Portions Copyright (C) 2009 notaz |
2823a4c8 |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
9 | |
10 | This program is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | GNU General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software |
17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
18 | |
19 | */ |
20 | |
21 | |
638cc626 |
22 | #define _GNU_SOURCE |
8b6232a6 |
23 | #include "../common.h" |
2823a4c8 |
24 | #include <sys/mman.h> |
25 | #include <sys/ioctl.h> |
26 | #include <sys/soundcard.h> |
638cc626 |
27 | #include <sys/types.h> |
28 | #include <unistd.h> |
8b6232a6 |
29 | #include <ctype.h> |
2823a4c8 |
30 | #include "gp2x.h" |
e8f5db5d |
31 | #include "pollux_dpc_set.h" |
2823a4c8 |
32 | |
43c24b30 |
33 | static u32 gpsp_gp2x_dev_audio; |
34 | static u32 gpsp_gp2x_dev; |
bbba3209 |
35 | #ifdef WIZ_BUILD |
43c24b30 |
36 | static u32 gpsp_gp2x_gpiodev; |
bbba3209 |
37 | #endif |
43c24b30 |
38 | |
39 | static u32 gp2x_audio_volume = 74/2; |
2823a4c8 |
40 | |
4742480d |
41 | static volatile u16 *gpsp_gp2x_memregs; |
42 | static volatile u32 *gpsp_gp2x_memregl; |
2823a4c8 |
43 | |
eb3668fc |
44 | u32 button_plat_mask_to_config[PLAT_BUTTON_COUNT] = |
43c24b30 |
45 | { |
46 | GP2X_UP, |
47 | GP2X_LEFT, |
48 | GP2X_DOWN, |
49 | GP2X_RIGHT, |
50 | GP2X_START, |
51 | GP2X_SELECT, |
52 | GP2X_L, |
53 | GP2X_R, |
54 | GP2X_A, |
55 | GP2X_B, |
56 | GP2X_X, |
57 | GP2X_Y, |
58 | GP2X_VOL_DOWN, |
59 | GP2X_VOL_UP, |
60 | GP2X_PUSH, |
61 | GP2X_VOL_MIDDLE |
62 | }; |
63 | |
eb3668fc |
64 | u32 gamepad_config_map[PLAT_BUTTON_COUNT] = |
43c24b30 |
65 | { |
66 | BUTTON_ID_UP, // Up |
67 | BUTTON_ID_LEFT, // Left |
68 | BUTTON_ID_DOWN, // Down |
69 | BUTTON_ID_RIGHT, // Right |
70 | BUTTON_ID_START, // Start |
71 | BUTTON_ID_SELECT, // Select |
72 | BUTTON_ID_L, // Ltrigger |
73 | BUTTON_ID_R, // Rtrigger |
74 | BUTTON_ID_FPS, // A |
75 | BUTTON_ID_A, // B |
76 | BUTTON_ID_B, // X |
77 | BUTTON_ID_MENU, // Y |
78 | BUTTON_ID_VOLDOWN, // Vol down |
79 | BUTTON_ID_VOLUP, // Vol up |
80 | BUTTON_ID_FPS, // Push |
81 | BUTTON_ID_MENU // Vol middle |
82 | }; |
83 | |
4cdfc0bc |
84 | #ifdef WIZ_BUILD |
85 | #include <linux/fb.h> |
86 | void *gpsp_gp2x_screen; |
5c6e71a0 |
87 | #define fb_buf_count 4 |
88 | static u32 fb_paddr[fb_buf_count]; |
89 | static void *fb_vaddr[fb_buf_count]; |
4cdfc0bc |
90 | static u32 fb_work_buf; |
5c6e71a0 |
91 | static int fb_buf_use; |
42c81190 |
92 | static int fbdev; |
4cdfc0bc |
93 | |
94 | static void fb_video_init() |
95 | { |
96 | struct fb_fix_screeninfo fbfix; |
97 | int i, ret; |
4cdfc0bc |
98 | |
99 | fbdev = open("/dev/fb0", O_RDWR); |
100 | if (fbdev < 0) { |
101 | perror("can't open fbdev"); |
102 | exit(1); |
103 | } |
104 | |
105 | ret = ioctl(fbdev, FBIOGET_FSCREENINFO, &fbfix); |
106 | if (ret == -1) |
107 | { |
108 | perror("ioctl(fbdev) failed"); |
109 | exit(1); |
110 | } |
111 | |
112 | printf("framebuffer: \"%s\" @ %08lx\n", fbfix.id, fbfix.smem_start); |
113 | fb_paddr[0] = fbfix.smem_start; |
4cdfc0bc |
114 | |
115 | fb_vaddr[0] = mmap(0, 320*240*2*fb_buf_count, PROT_READ|PROT_WRITE, |
116 | MAP_SHARED, gpsp_gp2x_dev, fb_paddr[0]); |
117 | if (fb_vaddr[0] == MAP_FAILED) |
118 | { |
119 | perror("mmap(fb_vaddr) failed"); |
120 | exit(1); |
121 | } |
122 | memset(fb_vaddr[0], 0, 320*240*2*fb_buf_count); |
123 | |
124 | printf(" %p -> %08x\n", fb_vaddr[0], fb_paddr[0]); |
125 | for (i = 1; i < fb_buf_count; i++) |
126 | { |
127 | fb_paddr[i] = fb_paddr[i-1] + 320*240*2; |
128 | fb_vaddr[i] = (char *)fb_vaddr[i-1] + 320*240*2; |
129 | printf(" %p -> %08x\n", fb_vaddr[i], fb_paddr[i]); |
130 | } |
131 | fb_work_buf = 0; |
132 | fb_buf_use = fb_buf_count; |
133 | |
134 | pollux_video_flip(); |
135 | warm_change_cb_upper(WCB_C_BIT|WCB_B_BIT, 1); |
136 | } |
137 | |
138 | void pollux_video_flip() |
139 | { |
4cdfc0bc |
140 | gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[fb_work_buf]; |
141 | gpsp_gp2x_memregl[0x4058>>2] |= 0x10; |
142 | fb_work_buf++; |
143 | if (fb_work_buf >= fb_buf_use) |
144 | fb_work_buf = 0; |
145 | gpsp_gp2x_screen = fb_vaddr[fb_work_buf]; |
146 | } |
147 | |
148 | void fb_use_buffers(int count) |
149 | { |
150 | if (count < 1) |
151 | count = 1; |
152 | else if (count > fb_buf_count) |
153 | count = fb_buf_count; |
154 | fb_buf_use = count; |
5c6e71a0 |
155 | memset(fb_vaddr[0], 0, 320*240*2*count); |
4cdfc0bc |
156 | } |
157 | |
42c81190 |
158 | void wiz_lcd_set_portrait(int y) |
159 | { |
160 | static int old_y = -1; |
161 | int cmd[2] = { 0, 0 }; |
162 | |
163 | if (old_y == y) |
164 | return; |
165 | cmd[0] = y ? 6 : 5; |
166 | ioctl(fbdev, _IOW('D', 90, int[2]), cmd); |
167 | gpsp_gp2x_memregl[0x4004>>2] = y ? 0x013f00ef : 0x00ef013f; |
168 | gpsp_gp2x_memregl[0x4000>>2] |= 1 << 3; |
169 | old_y = y; |
e8f5db5d |
170 | |
171 | /* the above ioctl resets LCD timings, so set them here */ |
172 | pollux_dpc_set(gpsp_gp2x_memregs, getenv("pollux_dpc_set")); |
42c81190 |
173 | } |
174 | |
4cdfc0bc |
175 | static void fb_video_exit() |
176 | { |
42c81190 |
177 | /* switch to default fb mem, turn portrait off */ |
4cdfc0bc |
178 | gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[0]; |
179 | gpsp_gp2x_memregl[0x4058>>2] |= 0x10; |
42c81190 |
180 | wiz_lcd_set_portrait(0); |
181 | close(fbdev); |
182 | } |
183 | |
184 | static int wiz_gamepak_fd = -1; |
185 | static u32 wiz_gamepak_size; |
186 | |
187 | static void wiz_gamepak_cleanup() |
188 | { |
189 | if (wiz_gamepak_size) |
190 | munmap(gamepak_rom, wiz_gamepak_size); |
191 | if (wiz_gamepak_fd >= 0) |
192 | close(wiz_gamepak_fd); |
193 | gamepak_rom = NULL; |
194 | wiz_gamepak_size = 0; |
195 | wiz_gamepak_fd = -1; |
4cdfc0bc |
196 | } |
42c81190 |
197 | |
198 | u32 wiz_load_gamepak(char *name) |
199 | { |
200 | char *dot_position = strrchr(name, '.'); |
201 | u32 ret; |
202 | |
203 | if (!strcasecmp(dot_position, ".zip")) |
204 | { |
205 | if (wiz_gamepak_fd >= 0) |
206 | { |
207 | wiz_gamepak_cleanup(); |
208 | printf("switching to ROM malloc\n"); |
209 | init_gamepak_buffer(); |
210 | } |
211 | return load_file_zip(name); |
212 | } |
213 | |
214 | if (wiz_gamepak_fd < 0) |
215 | { |
216 | extern void *gamepak_memory_map; |
217 | free(gamepak_rom); |
218 | free(gamepak_memory_map); |
219 | gamepak_memory_map = NULL; |
220 | printf("switching to ROM mmap\n"); |
221 | } |
222 | else |
223 | wiz_gamepak_cleanup(); |
224 | |
225 | wiz_gamepak_fd = open(name, O_RDONLY|O_NOATIME, S_IRUSR); |
226 | if (wiz_gamepak_fd < 0) |
227 | { |
228 | perror("wiz_load_gamepak: open failed"); |
229 | return -1; |
230 | } |
231 | |
232 | ret = lseek(wiz_gamepak_fd, 0, SEEK_END); |
233 | wiz_gamepak_size = gamepak_ram_buffer_size = ret; |
234 | |
235 | gamepak_rom = mmap(0, ret, PROT_READ, MAP_SHARED, wiz_gamepak_fd, 0); |
236 | if (gamepak_rom == MAP_FAILED) |
237 | { |
238 | perror("wiz_load_gamepak: mmap failed"); |
239 | return -1; |
240 | } |
241 | |
242 | return ret; |
243 | } |
244 | |
4cdfc0bc |
245 | #endif |
246 | |
5a01fba6 |
247 | static int get_romdir(char *buff, size_t size) |
2823a4c8 |
248 | { |
638cc626 |
249 | FILE *f; |
5a01fba6 |
250 | char *s; |
251 | int r = -1; |
252 | |
253 | f = fopen("romdir.txt", "r"); |
254 | if (f == NULL) |
255 | return -1; |
256 | |
257 | s = fgets(buff, size, f); |
258 | if (s) |
259 | { |
260 | r = strlen(s); |
261 | while (r > 0 && isspace(buff[r-1])) |
262 | buff[--r] = 0; |
263 | } |
264 | |
265 | fclose(f); |
266 | return r; |
267 | } |
268 | |
43c24b30 |
269 | void gpsp_plat_init(void) |
5a01fba6 |
270 | { |
271 | char buff[256]; |
272 | |
2823a4c8 |
273 | gpsp_gp2x_dev = open("/dev/mem", O_RDWR); |
274 | gpsp_gp2x_dev_audio = open("/dev/mixer", O_RDWR); |
8b6232a6 |
275 | gpsp_gp2x_memregl = (u32 *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, |
2823a4c8 |
276 | gpsp_gp2x_dev, 0xc0000000); |
8b6232a6 |
277 | gpsp_gp2x_memregs = (u16 *)gpsp_gp2x_memregl; |
638cc626 |
278 | warm_init(); |
4742480d |
279 | #ifdef WIZ_BUILD |
280 | gpsp_gp2x_gpiodev = open("/dev/GPIO", O_RDONLY); |
4cdfc0bc |
281 | fb_video_init(); |
282 | #endif |
2823a4c8 |
283 | |
5a01fba6 |
284 | if (get_romdir(buff, sizeof(buff)) > 0) |
285 | chdir(buff); |
638cc626 |
286 | |
2823a4c8 |
287 | gp2x_sound_volume(1); |
288 | } |
289 | |
43c24b30 |
290 | void gpsp_plat_quit(void) |
2823a4c8 |
291 | { |
5a01fba6 |
292 | char buff1[256], buff2[256]; |
638cc626 |
293 | |
5a01fba6 |
294 | getcwd(buff1, sizeof(buff1)); |
638cc626 |
295 | chdir(main_path); |
5a01fba6 |
296 | if (get_romdir(buff2, sizeof(buff2)) >= 0 && |
297 | strcmp(buff1, buff2) != 0) |
638cc626 |
298 | { |
5a01fba6 |
299 | FILE *f = fopen("romdir.txt", "w"); |
300 | if (f != NULL) |
301 | { |
302 | printf("writing romdir: %s\n", buff1); |
303 | fprintf(f, "%s", buff1); |
304 | fclose(f); |
305 | } |
638cc626 |
306 | } |
307 | |
308 | warm_finish(); |
4742480d |
309 | #ifdef WIZ_BUILD |
310 | close(gpsp_gp2x_gpiodev); |
4cdfc0bc |
311 | fb_video_exit(); |
42c81190 |
312 | wiz_gamepak_cleanup(); |
4742480d |
313 | #endif |
4cdfc0bc |
314 | munmap((void *)gpsp_gp2x_memregl, 0x10000); |
315 | close(gpsp_gp2x_dev_audio); |
316 | close(gpsp_gp2x_dev); |
90206450 |
317 | |
638cc626 |
318 | fcloseall(); |
5a01fba6 |
319 | sync(); |
42c81190 |
320 | exit(0); |
2823a4c8 |
321 | } |
322 | |
323 | void gp2x_sound_volume(u32 volume_up) |
324 | { |
325 | u32 volume; |
326 | if((volume_up == 0) && (gp2x_audio_volume > 0)) |
327 | gp2x_audio_volume--; |
328 | |
329 | if((volume_up != 0) && (gp2x_audio_volume < 100)) |
330 | gp2x_audio_volume++; |
331 | |
332 | volume = (gp2x_audio_volume * 0x50) / 100; |
333 | volume = (gp2x_audio_volume << 8) | gp2x_audio_volume; |
334 | ioctl(gpsp_gp2x_dev_audio, SOUND_MIXER_WRITE_PCM, &volume); |
335 | } |
336 | |
43c24b30 |
337 | u32 gpsp_plat_joystick_read(void) |
4742480d |
338 | { |
339 | #ifdef WIZ_BUILD |
340 | u32 value = 0; |
341 | read(gpsp_gp2x_gpiodev, &value, 4); |
342 | if(value & 0x02) |
343 | value |= 0x05; |
344 | if(value & 0x08) |
345 | value |= 0x14; |
346 | if(value & 0x20) |
347 | value |= 0x50; |
348 | if(value & 0x80) |
349 | value |= 0x41; |
350 | return value; |
351 | #else |
352 | u32 value = (gpsp_gp2x_memregs[0x1198 >> 1] & 0x00FF); |
353 | |
354 | if(value == 0xFD) |
355 | value = 0xFA; |
356 | if(value == 0xF7) |
357 | value = 0xEB; |
358 | if(value == 0xDF) |
359 | value = 0xAF; |
360 | if(value == 0x7F) |
361 | value = 0xBE; |
362 | |
363 | return ~((gpsp_gp2x_memregs[0x1184 >> 1] & 0xFF00) | value | |
364 | (gpsp_gp2x_memregs[0x1186 >> 1] << 16)); |
365 | #endif |
366 | } |
367 | |
43c24b30 |
368 | u32 gpsp_plat_buttons_to_cursor(u32 buttons) |
369 | { |
370 | gui_action_type new_button = CURSOR_NONE; |
371 | |
372 | if(buttons & GP2X_A) |
373 | new_button = CURSOR_BACK; |
374 | |
375 | if(buttons & GP2X_X) |
376 | new_button = CURSOR_EXIT; |
377 | |
378 | if(buttons & GP2X_B) |
379 | new_button = CURSOR_SELECT; |
380 | |
381 | if(buttons & GP2X_UP) |
382 | new_button = CURSOR_UP; |
383 | |
384 | if(buttons & GP2X_DOWN) |
385 | new_button = CURSOR_DOWN; |
386 | |
387 | if(buttons & GP2X_LEFT) |
388 | new_button = CURSOR_LEFT; |
389 | |
390 | if(buttons & GP2X_RIGHT) |
391 | new_button = CURSOR_RIGHT; |
392 | |
393 | if(buttons & GP2X_L) |
394 | new_button = CURSOR_L; |
395 | |
396 | if(buttons & GP2X_R) |
397 | new_button = CURSOR_R; |
398 | |
399 | return new_button; |
400 | } |
401 | |
638cc626 |
402 | // Fout = (m * Fin) / (p * 2^s) |
4742480d |
403 | void set_FCLK(u32 MHZ) |
404 | { |
638cc626 |
405 | u32 v; |
406 | u32 mdiv, pdiv, sdiv = 0; |
407 | #ifdef WIZ_BUILD |
408 | #define SYS_CLK_FREQ 27 |
409 | // m = MDIV, p = PDIV, s = SDIV |
410 | pdiv = 9; |
411 | mdiv = (MHZ * pdiv) / SYS_CLK_FREQ; |
412 | mdiv &= 0x3ff; |
413 | v = (pdiv<<18) | (mdiv<<8) | sdiv; |
414 | |
415 | gpsp_gp2x_memregl[0xf004>>2] = v; |
416 | gpsp_gp2x_memregl[0xf07c>>2] |= 0x8000; |
5a01fba6 |
417 | while (gpsp_gp2x_memregl[0xf07c>>2] & 0x8000) |
418 | ; |
638cc626 |
419 | #else |
420 | #define SYS_CLK_FREQ 7372800 |
421 | // m = MDIV + 8, p = PDIV + 2, s = SDIV |
422 | pdiv = 3; |
423 | mdiv = (MHZ * pdiv * 1000000) / SYS_CLK_FREQ; |
424 | mdiv &= 0xff; |
425 | v = ((mdiv-8)<<8) | ((pdiv-2)<<2) | sdiv; |
426 | gpsp_gp2x_memregs[0x910>>1] = v; |
4742480d |
427 | #endif |
638cc626 |
428 | } |
4742480d |
429 | |