3 * Copyright (C) 2006 Exophase <exophase@gmail.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
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 GNU
13 * General Public License for more details.
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 Street, Fifth Floor, Boston, MA 02110-1301 USA
22 // Special thanks to psp298 for the analog->dpad code!
24 void trigger_key(u32 key)
26 u32 p1_cnt = io_registers[REG_P1CNT];
29 if((p1_cnt >> 14) & 0x01)
31 u32 key_intersection = (p1_cnt & key) & 0x3FF;
35 if(key_intersection == (p1_cnt & 0x3FF))
36 raise_interrupt(IRQ_KEYPAD);
41 raise_interrupt(IRQ_KEYPAD);
48 u32 global_enable_analog = 1;
49 u32 analog_sensitivity_level = 4;
56 } button_repeat_state_type;
59 // These define autorepeat values (in microseconds), tweak as necessary.
61 #define BUTTON_REPEAT_START 200000
62 #define BUTTON_REPEAT_CONTINUE 50000
64 button_repeat_state_type button_repeat_state = BUTTON_NOT_HELD;
65 u32 button_repeat = 0;
66 gui_action_type cursor_repeat = CURSOR_NONE;
71 u32 gamepad_config_map[16] =
73 BUTTON_ID_MENU, // Triangle
74 BUTTON_ID_A, // Circle
76 BUTTON_ID_START, // Square
77 BUTTON_ID_L, // Ltrigger
78 BUTTON_ID_R, // Rtrigger
79 BUTTON_ID_DOWN, // Down
80 BUTTON_ID_LEFT, // Left
82 BUTTON_ID_RIGHT, // Right
83 BUTTON_ID_SELECT, // Select
84 BUTTON_ID_START, // Start
85 BUTTON_ID_UP, // Analog up
86 BUTTON_ID_DOWN, // Analog down
87 BUTTON_ID_LEFT, // Analog left
88 BUTTON_ID_RIGHT // Analog right
91 #define PSP_ALL_BUTTON_MASK 0xFFFF
93 gui_action_type get_gui_input()
95 SceCtrlData ctrl_data;
96 gui_action_type new_button = CURSOR_NONE;
99 static u32 last_buttons = 0;
100 static u64 button_repeat_timestamp;
104 sceCtrlPeekBufferPositive(&ctrl_data, 1);
105 ctrl_data.Buttons &= PSP_ALL_BUTTON_MASK;
106 new_buttons = (last_buttons ^ ctrl_data.Buttons) & ctrl_data.Buttons;
107 last_buttons = ctrl_data.Buttons;
109 if(new_buttons & PSP_CTRL_LEFT)
110 new_button = CURSOR_LEFT;
112 if(new_buttons & PSP_CTRL_RIGHT)
113 new_button = CURSOR_RIGHT;
115 if(new_buttons & PSP_CTRL_UP)
116 new_button = CURSOR_UP;
118 if(new_buttons & PSP_CTRL_DOWN)
119 new_button = CURSOR_DOWN;
121 if(new_buttons & PSP_CTRL_START)
122 new_button = CURSOR_SELECT;
124 if(new_buttons & PSP_CTRL_CIRCLE)
125 new_button = CURSOR_SELECT;
127 if(new_buttons & PSP_CTRL_CROSS)
128 new_button = CURSOR_EXIT;
130 if(new_buttons & PSP_CTRL_SQUARE)
131 new_button = CURSOR_BACK;
133 if(new_button != CURSOR_NONE)
135 get_ticks_us(&button_repeat_timestamp);
136 button_repeat_state = BUTTON_HELD_INITIAL;
137 button_repeat = new_buttons;
138 cursor_repeat = new_button;
142 if(ctrl_data.Buttons & button_repeat)
145 get_ticks_us(&new_ticks);
147 if(button_repeat_state == BUTTON_HELD_INITIAL)
149 if((new_ticks - button_repeat_timestamp) >
152 new_button = cursor_repeat;
153 button_repeat_timestamp = new_ticks;
154 button_repeat_state = BUTTON_HELD_REPEAT;
158 if(button_repeat_state == BUTTON_HELD_REPEAT)
160 if((new_ticks - button_repeat_timestamp) >
161 BUTTON_REPEAT_CONTINUE)
163 new_button = cursor_repeat;
164 button_repeat_timestamp = new_ticks;
173 #define PSP_CTRL_ANALOG_UP (1 << 28)
174 #define PSP_CTRL_ANALOG_DOWN (1 << 29)
175 #define PSP_CTRL_ANALOG_LEFT (1 << 30)
176 #define PSP_CTRL_ANALOG_RIGHT (1 << 31)
178 u32 button_psp_mask_to_config[] =
193 PSP_CTRL_ANALOG_DOWN,
194 PSP_CTRL_ANALOG_LEFT,
195 PSP_CTRL_ANALOG_RIGHT
198 u32 button_id_to_gba_mask[] =
216 gui_action_type get_gui_input_fs_hold(u32 button_id)
218 gui_action_type new_button = get_gui_input();
219 if((last_buttons & button_psp_mask_to_config[button_id]) == 0)
225 u32 rapidfire_flag = 1;
229 SceCtrlData ctrl_data;
231 u32 non_repeat_buttons;
235 u32 analog_sensitivity = 92 - (analog_sensitivity_level * 4);
236 u32 inv_analog_sensitivity = 256 - analog_sensitivity;
238 sceCtrlPeekBufferPositive(&ctrl_data, 1);
240 buttons = ctrl_data.Buttons;
242 if(global_enable_analog)
244 if(ctrl_data.Lx < analog_sensitivity)
245 buttons |= PSP_CTRL_ANALOG_LEFT;
247 if(ctrl_data.Lx > inv_analog_sensitivity)
248 buttons |= PSP_CTRL_ANALOG_RIGHT;
250 if(ctrl_data.Ly < analog_sensitivity)
251 buttons |= PSP_CTRL_ANALOG_UP;
253 if(ctrl_data.Ly > inv_analog_sensitivity)
254 buttons |= PSP_CTRL_ANALOG_DOWN;
257 non_repeat_buttons = (last_buttons ^ buttons) & buttons;
258 last_buttons = buttons;
260 for(i = 0; i < 16; i++)
262 if(non_repeat_buttons & button_psp_mask_to_config[i])
263 button_id = gamepad_config_map[i];
265 button_id = BUTTON_ID_NONE;
271 u16 *screen_copy = copy_screen();
272 u32 ret_val = menu(screen_copy);
278 case BUTTON_ID_LOADSTATE:
280 u8 current_savestate_filename[512];
281 get_savestate_filename_noshot(savestate_slot,
282 current_savestate_filename);
283 load_state(current_savestate_filename);
287 case BUTTON_ID_SAVESTATE:
289 u8 current_savestate_filename[512];
290 u16 *current_screen = copy_screen();
291 get_savestate_filename_noshot(savestate_slot,
292 current_savestate_filename);
293 save_state(current_savestate_filename, current_screen);
294 free(current_screen);
298 case BUTTON_ID_FASTFORWARD:
299 print_string("FASTFORWARD", 0xFFFF, 0x0000, 0, 50);
300 synchronize_flag ^= 1;
304 if(buttons & button_psp_mask_to_config[i])
306 button_id = gamepad_config_map[i];
307 if(button_id < BUTTON_ID_MENU)
309 new_key |= button_id_to_gba_mask[button_id];
313 if((button_id >= BUTTON_ID_RAPIDFIRE_A) &&
314 (button_id <= BUTTON_ID_RAPIDFIRE_L))
319 new_key |= button_id_to_gba_mask[button_id -
320 BUTTON_ID_RAPIDFIRE_A + BUTTON_ID_A];
324 new_key &= ~button_id_to_gba_mask[button_id -
325 BUTTON_ID_RAPIDFIRE_A + BUTTON_ID_A];
331 if((new_key | key) != key)
332 trigger_key(new_key);
336 io_registers[REG_P1] = (~key) & 0x3FF;
343 sceCtrlSetSamplingCycle(0);
344 sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
352 // GP2X SDL requires a user made input method
353 #include <sys/mman.h>
354 #include <sys/ioctl.h>
355 #include "gp2x/gp2x.h"
357 u32 gamepad_config_map[16] =
360 BUTTON_ID_LEFT, // Left
361 BUTTON_ID_DOWN, // Down
362 BUTTON_ID_RIGHT, // Right
363 BUTTON_ID_START, // Start
364 BUTTON_ID_SELECT, // Select
365 BUTTON_ID_L, // Ltrigger
366 BUTTON_ID_R, // Rtrigger
371 BUTTON_ID_VOLDOWN, // Vol down
372 BUTTON_ID_VOLUP, // Vol up
373 BUTTON_ID_FPS, // Push
374 BUTTON_ID_MENU // Vol middle
377 extern u32 gp2x_fps_debug;
378 extern u32 gpsp_gp2x_joystick_read(void);
380 gui_action_type get_gui_input()
382 gui_action_type new_button = CURSOR_NONE;
383 u32 buttons = gpsp_gp2x_joystick_read();
386 static u32 last_buttons = 0;
387 static u64 button_repeat_timestamp;
391 new_buttons = (last_buttons ^ buttons) & buttons;
392 last_buttons = buttons;
394 if(new_buttons & GP2X_A)
395 new_button = CURSOR_BACK;
397 if(new_buttons & GP2X_X)
398 new_button = CURSOR_EXIT;
400 if(new_buttons & GP2X_B)
401 new_button = CURSOR_SELECT;
403 if(new_buttons & GP2X_UP)
404 new_button = CURSOR_UP;
406 if(new_buttons & GP2X_DOWN)
407 new_button = CURSOR_DOWN;
409 if(new_buttons & GP2X_LEFT)
410 new_button = CURSOR_LEFT;
412 if(new_buttons & GP2X_RIGHT)
413 new_button = CURSOR_RIGHT;
416 if(new_button != CURSOR_NONE)
418 get_ticks_us(&button_repeat_timestamp);
419 button_repeat_state = BUTTON_HELD_INITIAL;
420 button_repeat = new_buttons;
421 cursor_repeat = new_button;
425 if(buttons & button_repeat)
428 get_ticks_us(&new_ticks);
430 if(button_repeat_state == BUTTON_HELD_INITIAL)
432 if((new_ticks - button_repeat_timestamp) >
435 new_button = cursor_repeat;
436 button_repeat_timestamp = new_ticks;
437 button_repeat_state = BUTTON_HELD_REPEAT;
441 if(button_repeat_state == BUTTON_HELD_REPEAT)
443 if((new_ticks - button_repeat_timestamp) >
444 BUTTON_REPEAT_CONTINUE)
446 new_button = cursor_repeat;
447 button_repeat_timestamp = new_ticks;
456 #define GP2X_VOL_MIDDLE (1 << 24)
458 u32 button_gp2x_mask_to_config[] =
478 u32 button_id_to_gba_mask[] =
498 static u32 rapidfire_flag = 1;
499 static u32 last_buttons;
503 u32 buttons = gpsp_gp2x_joystick_read();
506 if((buttons & GP2X_VOL_DOWN) && (buttons & GP2X_VOL_UP))
508 buttons &= ~(GP2X_VOL_DOWN | GP2X_VOL_UP);
509 buttons |= GP2X_VOL_MIDDLE;
513 if((buttons & GP2X_VOL_DOWN) && (buttons & GP2X_SELECT))
515 buttons &= ~(GP2X_VOL_DOWN | GP2X_SELECT);
516 buttons |= GP2X_VOL_MIDDLE;
519 handled_buttons = ((last_buttons ^ buttons) | GP2X_VOL_DOWN | GP2X_VOL_UP) & buttons;
520 last_buttons = buttons;
522 for(i = 0; i < 16; i++)
524 if(handled_buttons & button_gp2x_mask_to_config[i])
525 button_id = gamepad_config_map[i];
527 button_id = BUTTON_ID_NONE;
533 u16 *screen_copy = copy_screen();
534 u32 ret_val = menu(screen_copy);
540 case BUTTON_ID_LOADSTATE:
542 u8 current_savestate_filename[512];
543 get_savestate_filename_noshot(savestate_slot,
544 current_savestate_filename);
545 load_state(current_savestate_filename);
549 case BUTTON_ID_SAVESTATE:
551 u8 current_savestate_filename[512];
552 u16 *current_screen = copy_screen();
553 get_savestate_filename_noshot(savestate_slot,
554 current_savestate_filename);
555 save_state(current_savestate_filename, current_screen);
556 free(current_screen);
560 case BUTTON_ID_FASTFORWARD:
561 print_string("FASTFORWARD", 0xFFFF, 0x0000, 0, 50);
562 synchronize_flag ^= 1;
565 case BUTTON_ID_VOLUP:
566 gp2x_sound_volume(1);
569 case BUTTON_ID_VOLDOWN:
570 gp2x_sound_volume(0);
578 if(buttons & button_gp2x_mask_to_config[i])
580 button_id = gamepad_config_map[i];
581 if(button_id < BUTTON_ID_MENU)
583 new_key |= button_id_to_gba_mask[button_id];
587 if((button_id >= BUTTON_ID_RAPIDFIRE_A) &&
588 (button_id <= BUTTON_ID_RAPIDFIRE_L))
593 new_key |= button_id_to_gba_mask[button_id -
594 BUTTON_ID_RAPIDFIRE_A + BUTTON_ID_A];
598 new_key &= ~button_id_to_gba_mask[button_id -
599 BUTTON_ID_RAPIDFIRE_A + BUTTON_ID_A];
605 if((new_key | key) != key)
606 trigger_key(new_key);
610 io_registers[REG_P1] = (~key) & 0x3FF;
626 u32 key_map(SDLKey key_sym)
652 return BUTTON_SELECT;
665 u32 joy_map(u32 button)
679 return BUTTON_SELECT;
692 gui_action_type get_gui_input()
695 gui_action_type gui_action = CURSOR_NONE;
699 while(SDL_PollEvent(&event))
708 switch(event.key.keysym.sym)
711 gui_action = CURSOR_EXIT;
715 gui_action = CURSOR_DOWN;
719 gui_action = CURSOR_UP;
723 gui_action = CURSOR_LEFT;
727 gui_action = CURSOR_RIGHT;
731 gui_action = CURSOR_SELECT;
735 gui_action = CURSOR_BACK;
750 while(SDL_PollEvent(&event))
759 if(event.key.keysym.sym == SDLK_ESCAPE)
764 if(event.key.keysym.sym == SDLK_BACKSPACE)
766 u16 *screen_copy = copy_screen();
767 u32 ret_val = menu(screen_copy);
774 if(event.key.keysym.sym == SDLK_F1)
780 if(event.key.keysym.sym == SDLK_F2)
782 FILE *fp = fopen("palette_ram.bin", "wb");
783 printf("writing palette RAM\n");
784 fwrite(palette_ram, 1024, 1, fp);
786 printf("writing palette VRAM\n");
787 fp = fopen("vram.bin", "wb");
788 fwrite(vram, 1024 * 96, 1, fp);
790 printf("writing palette OAM RAM\n");
791 fp = fopen("oam_ram.bin", "wb");
792 fwrite(oam_ram, 1024, 1, fp);
794 printf("writing palette I/O registers\n");
795 fp = fopen("io_registers.bin", "wb");
796 fwrite(io_registers, 1024, 1, fp);
801 if(event.key.keysym.sym == SDLK_F3)
803 dump_translation_cache();
807 if(event.key.keysym.sym == SDLK_F5)
809 u8 current_savestate_filename[512];
810 u16 *current_screen = copy_screen();
811 get_savestate_filename_noshot(savestate_slot,
812 current_savestate_filename);
813 save_state(current_savestate_filename, current_screen);
814 free(current_screen);
818 if(event.key.keysym.sym == SDLK_F7)
820 u8 current_savestate_filename[512];
821 get_savestate_filename_noshot(savestate_slot,
822 current_savestate_filename);
823 load_state(current_savestate_filename);
829 if(event.key.keysym.sym == SDLK_BACKQUOTE)
831 synchronize_flag ^= 1;
835 key |= key_map(event.key.keysym.sym);
844 key &= ~(key_map(event.key.keysym.sym));
848 case SDL_JOYBUTTONDOWN:
850 key |= joy_map(event.jbutton.button);
855 case SDL_JOYBUTTONUP:
857 key &= ~(joy_map(event.jbutton.button));
863 io_registers[REG_P1] = (~key) & 0x3FF;
870 u32 joystick_count = SDL_NumJoysticks();
872 if(joystick_count > 0)
875 SDL_JoystickEventState(SDL_ENABLE);
882 #define input_savestate_builder(type) \
883 void input_##type##_savestate(file_tag_type savestate_file) \
885 file_##type##_variable(savestate_file, key); \
888 input_savestate_builder(read);
889 input_savestate_builder(write_mem);