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" |
4742480d |
31 | #include "warm.h" |
e8f5db5d |
32 | #include "pollux_dpc_set.h" |
2823a4c8 |
33 | |
90206450 |
34 | u32 gp2x_audio_volume = 74/2; |
2823a4c8 |
35 | u32 gpsp_gp2x_dev_audio = 0; |
36 | u32 gpsp_gp2x_dev = 0; |
4742480d |
37 | u32 gpsp_gp2x_gpiodev = 0; |
2823a4c8 |
38 | |
4742480d |
39 | static volatile u16 *gpsp_gp2x_memregs; |
40 | static volatile u32 *gpsp_gp2x_memregl; |
2823a4c8 |
41 | |
4cdfc0bc |
42 | #ifdef WIZ_BUILD |
43 | #include <linux/fb.h> |
44 | void *gpsp_gp2x_screen; |
5c6e71a0 |
45 | #define fb_buf_count 4 |
46 | static u32 fb_paddr[fb_buf_count]; |
47 | static void *fb_vaddr[fb_buf_count]; |
4cdfc0bc |
48 | static u32 fb_work_buf; |
5c6e71a0 |
49 | static int fb_buf_use; |
42c81190 |
50 | static int fbdev; |
4cdfc0bc |
51 | |
52 | static void fb_video_init() |
53 | { |
54 | struct fb_fix_screeninfo fbfix; |
55 | int i, ret; |
4cdfc0bc |
56 | |
57 | fbdev = open("/dev/fb0", O_RDWR); |
58 | if (fbdev < 0) { |
59 | perror("can't open fbdev"); |
60 | exit(1); |
61 | } |
62 | |
63 | ret = ioctl(fbdev, FBIOGET_FSCREENINFO, &fbfix); |
64 | if (ret == -1) |
65 | { |
66 | perror("ioctl(fbdev) failed"); |
67 | exit(1); |
68 | } |
69 | |
70 | printf("framebuffer: \"%s\" @ %08lx\n", fbfix.id, fbfix.smem_start); |
71 | fb_paddr[0] = fbfix.smem_start; |
4cdfc0bc |
72 | |
73 | fb_vaddr[0] = mmap(0, 320*240*2*fb_buf_count, PROT_READ|PROT_WRITE, |
74 | MAP_SHARED, gpsp_gp2x_dev, fb_paddr[0]); |
75 | if (fb_vaddr[0] == MAP_FAILED) |
76 | { |
77 | perror("mmap(fb_vaddr) failed"); |
78 | exit(1); |
79 | } |
80 | memset(fb_vaddr[0], 0, 320*240*2*fb_buf_count); |
81 | |
82 | printf(" %p -> %08x\n", fb_vaddr[0], fb_paddr[0]); |
83 | for (i = 1; i < fb_buf_count; i++) |
84 | { |
85 | fb_paddr[i] = fb_paddr[i-1] + 320*240*2; |
86 | fb_vaddr[i] = (char *)fb_vaddr[i-1] + 320*240*2; |
87 | printf(" %p -> %08x\n", fb_vaddr[i], fb_paddr[i]); |
88 | } |
89 | fb_work_buf = 0; |
90 | fb_buf_use = fb_buf_count; |
91 | |
92 | pollux_video_flip(); |
93 | warm_change_cb_upper(WCB_C_BIT|WCB_B_BIT, 1); |
94 | } |
95 | |
96 | void pollux_video_flip() |
97 | { |
4cdfc0bc |
98 | gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[fb_work_buf]; |
99 | gpsp_gp2x_memregl[0x4058>>2] |= 0x10; |
100 | fb_work_buf++; |
101 | if (fb_work_buf >= fb_buf_use) |
102 | fb_work_buf = 0; |
103 | gpsp_gp2x_screen = fb_vaddr[fb_work_buf]; |
104 | } |
105 | |
106 | void fb_use_buffers(int count) |
107 | { |
108 | if (count < 1) |
109 | count = 1; |
110 | else if (count > fb_buf_count) |
111 | count = fb_buf_count; |
112 | fb_buf_use = count; |
5c6e71a0 |
113 | memset(fb_vaddr[0], 0, 320*240*2*count); |
4cdfc0bc |
114 | } |
115 | |
42c81190 |
116 | void wiz_lcd_set_portrait(int y) |
117 | { |
118 | static int old_y = -1; |
119 | int cmd[2] = { 0, 0 }; |
120 | |
121 | if (old_y == y) |
122 | return; |
123 | cmd[0] = y ? 6 : 5; |
124 | ioctl(fbdev, _IOW('D', 90, int[2]), cmd); |
125 | gpsp_gp2x_memregl[0x4004>>2] = y ? 0x013f00ef : 0x00ef013f; |
126 | gpsp_gp2x_memregl[0x4000>>2] |= 1 << 3; |
127 | old_y = y; |
e8f5db5d |
128 | |
129 | /* the above ioctl resets LCD timings, so set them here */ |
130 | pollux_dpc_set(gpsp_gp2x_memregs, getenv("pollux_dpc_set")); |
42c81190 |
131 | } |
132 | |
4cdfc0bc |
133 | static void fb_video_exit() |
134 | { |
42c81190 |
135 | /* switch to default fb mem, turn portrait off */ |
4cdfc0bc |
136 | gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[0]; |
137 | gpsp_gp2x_memregl[0x4058>>2] |= 0x10; |
42c81190 |
138 | wiz_lcd_set_portrait(0); |
139 | close(fbdev); |
140 | } |
141 | |
142 | static int wiz_gamepak_fd = -1; |
143 | static u32 wiz_gamepak_size; |
144 | |
145 | static void wiz_gamepak_cleanup() |
146 | { |
147 | if (wiz_gamepak_size) |
148 | munmap(gamepak_rom, wiz_gamepak_size); |
149 | if (wiz_gamepak_fd >= 0) |
150 | close(wiz_gamepak_fd); |
151 | gamepak_rom = NULL; |
152 | wiz_gamepak_size = 0; |
153 | wiz_gamepak_fd = -1; |
4cdfc0bc |
154 | } |
42c81190 |
155 | |
156 | u32 wiz_load_gamepak(char *name) |
157 | { |
158 | char *dot_position = strrchr(name, '.'); |
159 | u32 ret; |
160 | |
161 | if (!strcasecmp(dot_position, ".zip")) |
162 | { |
163 | if (wiz_gamepak_fd >= 0) |
164 | { |
165 | wiz_gamepak_cleanup(); |
166 | printf("switching to ROM malloc\n"); |
167 | init_gamepak_buffer(); |
168 | } |
169 | return load_file_zip(name); |
170 | } |
171 | |
172 | if (wiz_gamepak_fd < 0) |
173 | { |
174 | extern void *gamepak_memory_map; |
175 | free(gamepak_rom); |
176 | free(gamepak_memory_map); |
177 | gamepak_memory_map = NULL; |
178 | printf("switching to ROM mmap\n"); |
179 | } |
180 | else |
181 | wiz_gamepak_cleanup(); |
182 | |
183 | wiz_gamepak_fd = open(name, O_RDONLY|O_NOATIME, S_IRUSR); |
184 | if (wiz_gamepak_fd < 0) |
185 | { |
186 | perror("wiz_load_gamepak: open failed"); |
187 | return -1; |
188 | } |
189 | |
190 | ret = lseek(wiz_gamepak_fd, 0, SEEK_END); |
191 | wiz_gamepak_size = gamepak_ram_buffer_size = ret; |
192 | |
193 | gamepak_rom = mmap(0, ret, PROT_READ, MAP_SHARED, wiz_gamepak_fd, 0); |
194 | if (gamepak_rom == MAP_FAILED) |
195 | { |
196 | perror("wiz_load_gamepak: mmap failed"); |
197 | return -1; |
198 | } |
199 | |
200 | return ret; |
201 | } |
202 | |
4cdfc0bc |
203 | #endif |
204 | |
5a01fba6 |
205 | static int get_romdir(char *buff, size_t size) |
2823a4c8 |
206 | { |
638cc626 |
207 | FILE *f; |
5a01fba6 |
208 | char *s; |
209 | int r = -1; |
210 | |
211 | f = fopen("romdir.txt", "r"); |
212 | if (f == NULL) |
213 | return -1; |
214 | |
215 | s = fgets(buff, size, f); |
216 | if (s) |
217 | { |
218 | r = strlen(s); |
219 | while (r > 0 && isspace(buff[r-1])) |
220 | buff[--r] = 0; |
221 | } |
222 | |
223 | fclose(f); |
224 | return r; |
225 | } |
226 | |
227 | void gp2x_init() |
228 | { |
229 | char buff[256]; |
230 | |
2823a4c8 |
231 | gpsp_gp2x_dev = open("/dev/mem", O_RDWR); |
232 | gpsp_gp2x_dev_audio = open("/dev/mixer", O_RDWR); |
8b6232a6 |
233 | gpsp_gp2x_memregl = (u32 *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, |
2823a4c8 |
234 | gpsp_gp2x_dev, 0xc0000000); |
8b6232a6 |
235 | gpsp_gp2x_memregs = (u16 *)gpsp_gp2x_memregl; |
638cc626 |
236 | warm_init(); |
4742480d |
237 | #ifdef WIZ_BUILD |
238 | gpsp_gp2x_gpiodev = open("/dev/GPIO", O_RDONLY); |
4cdfc0bc |
239 | fb_video_init(); |
240 | #endif |
2823a4c8 |
241 | |
5a01fba6 |
242 | if (get_romdir(buff, sizeof(buff)) > 0) |
243 | chdir(buff); |
638cc626 |
244 | |
2823a4c8 |
245 | gp2x_sound_volume(1); |
246 | } |
247 | |
248 | void gp2x_quit() |
249 | { |
5a01fba6 |
250 | char buff1[256], buff2[256]; |
638cc626 |
251 | |
5a01fba6 |
252 | getcwd(buff1, sizeof(buff1)); |
638cc626 |
253 | chdir(main_path); |
5a01fba6 |
254 | if (get_romdir(buff2, sizeof(buff2)) >= 0 && |
255 | strcmp(buff1, buff2) != 0) |
638cc626 |
256 | { |
5a01fba6 |
257 | FILE *f = fopen("romdir.txt", "w"); |
258 | if (f != NULL) |
259 | { |
260 | printf("writing romdir: %s\n", buff1); |
261 | fprintf(f, "%s", buff1); |
262 | fclose(f); |
263 | } |
638cc626 |
264 | } |
265 | |
266 | warm_finish(); |
4742480d |
267 | #ifdef WIZ_BUILD |
268 | close(gpsp_gp2x_gpiodev); |
4cdfc0bc |
269 | fb_video_exit(); |
42c81190 |
270 | wiz_gamepak_cleanup(); |
4742480d |
271 | #endif |
4cdfc0bc |
272 | munmap((void *)gpsp_gp2x_memregl, 0x10000); |
273 | close(gpsp_gp2x_dev_audio); |
274 | close(gpsp_gp2x_dev); |
90206450 |
275 | |
638cc626 |
276 | fcloseall(); |
5a01fba6 |
277 | sync(); |
42c81190 |
278 | exit(0); |
2823a4c8 |
279 | } |
280 | |
281 | void gp2x_sound_volume(u32 volume_up) |
282 | { |
283 | u32 volume; |
284 | if((volume_up == 0) && (gp2x_audio_volume > 0)) |
285 | gp2x_audio_volume--; |
286 | |
287 | if((volume_up != 0) && (gp2x_audio_volume < 100)) |
288 | gp2x_audio_volume++; |
289 | |
290 | volume = (gp2x_audio_volume * 0x50) / 100; |
291 | volume = (gp2x_audio_volume << 8) | gp2x_audio_volume; |
292 | ioctl(gpsp_gp2x_dev_audio, SOUND_MIXER_WRITE_PCM, &volume); |
293 | } |
294 | |
4742480d |
295 | u32 gpsp_gp2x_joystick_read(void) |
296 | { |
297 | #ifdef WIZ_BUILD |
298 | u32 value = 0; |
299 | read(gpsp_gp2x_gpiodev, &value, 4); |
300 | if(value & 0x02) |
301 | value |= 0x05; |
302 | if(value & 0x08) |
303 | value |= 0x14; |
304 | if(value & 0x20) |
305 | value |= 0x50; |
306 | if(value & 0x80) |
307 | value |= 0x41; |
308 | return value; |
309 | #else |
310 | u32 value = (gpsp_gp2x_memregs[0x1198 >> 1] & 0x00FF); |
311 | |
312 | if(value == 0xFD) |
313 | value = 0xFA; |
314 | if(value == 0xF7) |
315 | value = 0xEB; |
316 | if(value == 0xDF) |
317 | value = 0xAF; |
318 | if(value == 0x7F) |
319 | value = 0xBE; |
320 | |
321 | return ~((gpsp_gp2x_memregs[0x1184 >> 1] & 0xFF00) | value | |
322 | (gpsp_gp2x_memregs[0x1186 >> 1] << 16)); |
323 | #endif |
324 | } |
325 | |
638cc626 |
326 | // Fout = (m * Fin) / (p * 2^s) |
4742480d |
327 | void set_FCLK(u32 MHZ) |
328 | { |
638cc626 |
329 | u32 v; |
330 | u32 mdiv, pdiv, sdiv = 0; |
331 | #ifdef WIZ_BUILD |
332 | #define SYS_CLK_FREQ 27 |
333 | // m = MDIV, p = PDIV, s = SDIV |
334 | pdiv = 9; |
335 | mdiv = (MHZ * pdiv) / SYS_CLK_FREQ; |
336 | mdiv &= 0x3ff; |
337 | v = (pdiv<<18) | (mdiv<<8) | sdiv; |
338 | |
339 | gpsp_gp2x_memregl[0xf004>>2] = v; |
340 | gpsp_gp2x_memregl[0xf07c>>2] |= 0x8000; |
5a01fba6 |
341 | while (gpsp_gp2x_memregl[0xf07c>>2] & 0x8000) |
342 | ; |
638cc626 |
343 | #else |
344 | #define SYS_CLK_FREQ 7372800 |
345 | // m = MDIV + 8, p = PDIV + 2, s = SDIV |
346 | pdiv = 3; |
347 | mdiv = (MHZ * pdiv * 1000000) / SYS_CLK_FREQ; |
348 | mdiv &= 0xff; |
349 | v = ((mdiv-8)<<8) | ((pdiv-2)<<2) | sdiv; |
350 | gpsp_gp2x_memregs[0x910>>1] = v; |
4742480d |
351 | #endif |
638cc626 |
352 | } |
4742480d |
353 | |