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;
379 u32 gpsp_gp2x_joystick_read(void)
381 u32 value = (gpsp_gp2x_memregs[0x1198 >> 1] & 0x00FF);
392 return ~((gpsp_gp2x_memregs[0x1184 >> 1] & 0xFF00) | value |
393 (gpsp_gp2x_memregs[0x1186 >> 1] << 16));
396 gui_action_type get_gui_input()
398 gui_action_type new_button = CURSOR_NONE;
399 u32 buttons = gpsp_gp2x_joystick_read();
402 static u32 last_buttons = 0;
403 static u64 button_repeat_timestamp;
407 new_buttons = (last_buttons ^ buttons) & buttons;
408 last_buttons = buttons;
410 if(new_buttons & GP2X_A)
411 new_button = CURSOR_BACK;
413 if(new_buttons & GP2X_X)
414 new_button = CURSOR_EXIT;
416 if(new_buttons & GP2X_B)
417 new_button = CURSOR_SELECT;
419 if(new_buttons & GP2X_UP)
420 new_button = CURSOR_UP;
422 if(new_buttons & GP2X_DOWN)
423 new_button = CURSOR_DOWN;
425 if(new_buttons & GP2X_LEFT)
426 new_button = CURSOR_LEFT;
428 if(new_buttons & GP2X_RIGHT)
429 new_button = CURSOR_RIGHT;
432 if(new_button != CURSOR_NONE)
434 get_ticks_us(&button_repeat_timestamp);
435 button_repeat_state = BUTTON_HELD_INITIAL;
436 button_repeat = new_buttons;
437 cursor_repeat = new_button;
441 if(buttons & button_repeat)
444 get_ticks_us(&new_ticks);
446 if(button_repeat_state == BUTTON_HELD_INITIAL)
448 if((new_ticks - button_repeat_timestamp) >
451 new_button = cursor_repeat;
452 button_repeat_timestamp = new_ticks;
453 button_repeat_state = BUTTON_HELD_REPEAT;
457 if(button_repeat_state == BUTTON_HELD_REPEAT)
459 if((new_ticks - button_repeat_timestamp) >
460 BUTTON_REPEAT_CONTINUE)
462 new_button = cursor_repeat;
463 button_repeat_timestamp = new_ticks;
472 #define GP2X_VOL_MIDDLE (1 << 24)
474 u32 button_gp2x_mask_to_config[] =
494 u32 button_id_to_gba_mask[] =
514 static u32 rapidfire_flag = 1;
515 static u32 last_buttons;
516 u32 non_repeat_buttons;
519 u32 buttons = gpsp_gp2x_joystick_read();
522 if((buttons & GP2X_VOL_DOWN) && (buttons & GP2X_VOL_UP))
524 buttons &= ~(GP2X_VOL_DOWN | GP2X_VOL_UP);
525 buttons |= GP2X_VOL_MIDDLE;
528 non_repeat_buttons = (last_buttons ^ buttons) & buttons;
529 last_buttons = buttons;
531 for(i = 0; i < 16; i++)
533 if(non_repeat_buttons & button_gp2x_mask_to_config[i])
534 button_id = gamepad_config_map[i];
536 button_id = BUTTON_ID_NONE;
542 u16 *screen_copy = copy_screen();
543 u32 ret_val = menu(screen_copy);
549 case BUTTON_ID_LOADSTATE:
551 u8 current_savestate_filename[512];
552 get_savestate_filename_noshot(savestate_slot,
553 current_savestate_filename);
554 load_state(current_savestate_filename);
558 case BUTTON_ID_SAVESTATE:
560 u8 current_savestate_filename[512];
561 u16 *current_screen = copy_screen();
562 get_savestate_filename_noshot(savestate_slot,
563 current_savestate_filename);
564 save_state(current_savestate_filename, current_screen);
565 free(current_screen);
569 case BUTTON_ID_FASTFORWARD:
570 print_string("FASTFORWARD", 0xFFFF, 0x0000, 0, 50);
571 synchronize_flag ^= 1;
574 case BUTTON_ID_VOLUP:
575 gp2x_sound_volume(1);
578 case BUTTON_ID_VOLDOWN:
579 gp2x_sound_volume(0);
587 if(buttons & button_gp2x_mask_to_config[i])
589 button_id = gamepad_config_map[i];
590 if(button_id < BUTTON_ID_MENU)
592 new_key |= button_id_to_gba_mask[button_id];
596 if((button_id >= BUTTON_ID_RAPIDFIRE_A) &&
597 (button_id <= BUTTON_ID_RAPIDFIRE_L))
602 new_key |= button_id_to_gba_mask[button_id -
603 BUTTON_ID_RAPIDFIRE_A + BUTTON_ID_A];
607 new_key &= ~button_id_to_gba_mask[button_id -
608 BUTTON_ID_RAPIDFIRE_A + BUTTON_ID_A];
614 if((new_key | key) != key)
615 trigger_key(new_key);
619 io_registers[REG_P1] = (~key) & 0x3FF;
635 u32 key_map(SDLKey key_sym)
661 return BUTTON_SELECT;
674 u32 joy_map(u32 button)
688 return BUTTON_SELECT;
701 gui_action_type get_gui_input()
704 gui_action_type gui_action = CURSOR_NONE;
708 while(SDL_PollEvent(&event))
717 switch(event.key.keysym.sym)
720 gui_action = CURSOR_EXIT;
724 gui_action = CURSOR_DOWN;
728 gui_action = CURSOR_UP;
732 gui_action = CURSOR_LEFT;
736 gui_action = CURSOR_RIGHT;
740 gui_action = CURSOR_SELECT;
744 gui_action = CURSOR_BACK;
759 while(SDL_PollEvent(&event))
768 if(event.key.keysym.sym == SDLK_ESCAPE)
773 if(event.key.keysym.sym == SDLK_BACKSPACE)
775 u16 *screen_copy = copy_screen();
776 u32 ret_val = menu(screen_copy);
783 if(event.key.keysym.sym == SDLK_F1)
789 if(event.key.keysym.sym == SDLK_F2)
791 FILE *fp = fopen("palette_ram.bin", "wb");
792 printf("writing palette RAM\n");
793 fwrite(palette_ram, 1024, 1, fp);
795 printf("writing palette VRAM\n");
796 fp = fopen("vram.bin", "wb");
797 fwrite(vram, 1024 * 96, 1, fp);
799 printf("writing palette OAM RAM\n");
800 fp = fopen("oam_ram.bin", "wb");
801 fwrite(oam_ram, 1024, 1, fp);
803 printf("writing palette I/O registers\n");
804 fp = fopen("io_registers.bin", "wb");
805 fwrite(io_registers, 1024, 1, fp);
810 if(event.key.keysym.sym == SDLK_F3)
812 dump_translation_cache();
816 if(event.key.keysym.sym == SDLK_F5)
818 u8 current_savestate_filename[512];
819 u16 *current_screen = copy_screen();
820 get_savestate_filename_noshot(savestate_slot,
821 current_savestate_filename);
822 save_state(current_savestate_filename, current_screen);
823 free(current_screen);
827 if(event.key.keysym.sym == SDLK_F7)
829 u8 current_savestate_filename[512];
830 get_savestate_filename_noshot(savestate_slot,
831 current_savestate_filename);
832 load_state(current_savestate_filename);
838 if(event.key.keysym.sym == SDLK_BACKQUOTE)
840 synchronize_flag ^= 1;
844 key |= key_map(event.key.keysym.sym);
853 key &= ~(key_map(event.key.keysym.sym));
857 case SDL_JOYBUTTONDOWN:
859 key |= joy_map(event.jbutton.button);
864 case SDL_JOYBUTTONUP:
866 key &= ~(joy_map(event.jbutton.button));
872 io_registers[REG_P1] = (~key) & 0x3FF;
879 u32 joystick_count = SDL_NumJoysticks();
881 if(joystick_count > 0)
884 SDL_JoystickEventState(SDL_ENABLE);
891 #define input_savestate_builder(type) \
892 void input_##type##_savestate(file_tag_type savestate_file) \
894 file_##type##_variable(savestate_file, key); \
897 input_savestate_builder(read);
898 input_savestate_builder(write_mem);