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);
350 #if defined(GP2X_BUILD) || defined(PND_BUILD)
352 extern u32 fps_debug;
354 gui_action_type get_gui_input()
356 gui_action_type new_button = CURSOR_NONE;
357 u32 buttons = gpsp_plat_joystick_read();
360 static u32 last_buttons = 0;
361 static u64 button_repeat_timestamp;
365 new_buttons = (last_buttons ^ buttons) & buttons;
366 last_buttons = buttons;
368 new_button = gpsp_plat_buttons_to_cursor(new_buttons);
369 if(new_button != CURSOR_NONE)
371 get_ticks_us(&button_repeat_timestamp);
372 button_repeat_state = BUTTON_HELD_INITIAL;
373 button_repeat = new_buttons;
374 cursor_repeat = new_button;
378 if(buttons & button_repeat)
381 get_ticks_us(&new_ticks);
383 if(button_repeat_state == BUTTON_HELD_INITIAL)
385 if((new_ticks - button_repeat_timestamp) >
388 new_button = cursor_repeat;
389 button_repeat_timestamp = new_ticks;
390 button_repeat_state = BUTTON_HELD_REPEAT;
394 if(button_repeat_state == BUTTON_HELD_REPEAT)
396 if((new_ticks - button_repeat_timestamp) >
397 BUTTON_REPEAT_CONTINUE)
399 new_button = cursor_repeat;
400 button_repeat_timestamp = new_ticks;
409 u32 button_id_to_gba_mask[] =
429 static u32 rapidfire_flag = 1;
430 static u32 last_buttons;
434 u32 buttons = gpsp_plat_joystick_read();
438 if((buttons & GP2X_VOL_DOWN) && (buttons & GP2X_VOL_UP))
440 buttons &= ~(GP2X_VOL_DOWN | GP2X_VOL_UP);
441 buttons |= GP2X_VOL_MIDDLE;
445 if((buttons & GP2X_VOL_DOWN) && (buttons & GP2X_SELECT))
447 buttons &= ~(GP2X_VOL_DOWN | GP2X_SELECT);
448 buttons |= GP2X_VOL_MIDDLE;
451 last_buttons &= ~(GP2X_VOL_DOWN | GP2X_VOL_UP);
454 handled_buttons = (last_buttons ^ buttons) & buttons;
455 last_buttons = buttons;
457 for(i = 0; i < PLAT_BUTTON_COUNT; i++)
459 if(handled_buttons & button_plat_mask_to_config[i])
460 button_id = gamepad_config_map[i];
462 button_id = BUTTON_ID_NONE;
468 u16 *screen_copy = copy_screen();
469 u32 ret_val = menu(screen_copy);
475 case BUTTON_ID_LOADSTATE:
477 u8 current_savestate_filename[512];
478 get_savestate_filename_noshot(savestate_slot,
479 current_savestate_filename);
480 load_state(current_savestate_filename);
484 case BUTTON_ID_SAVESTATE:
486 u8 current_savestate_filename[512];
487 u16 *current_screen = copy_screen();
488 get_savestate_filename_noshot(savestate_slot,
489 current_savestate_filename);
490 save_state(current_savestate_filename, current_screen);
491 free(current_screen);
495 case BUTTON_ID_FASTFORWARD:
496 synchronize_flag ^= 1;
500 case BUTTON_ID_VOLUP:
501 gp2x_sound_volume(1);
504 case BUTTON_ID_VOLDOWN:
505 gp2x_sound_volume(0);
514 if(buttons & button_plat_mask_to_config[i])
516 button_id = gamepad_config_map[i];
517 if(button_id < BUTTON_ID_MENU)
519 new_key |= button_id_to_gba_mask[button_id];
523 if((button_id >= BUTTON_ID_RAPIDFIRE_A) &&
524 (button_id <= BUTTON_ID_RAPIDFIRE_L))
529 new_key |= button_id_to_gba_mask[button_id -
530 BUTTON_ID_RAPIDFIRE_A + BUTTON_ID_A];
534 new_key &= ~button_id_to_gba_mask[button_id -
535 BUTTON_ID_RAPIDFIRE_A + BUTTON_ID_A];
541 if((new_key | key) != key)
542 trigger_key(new_key);
546 io_registers[REG_P1] = (~key) & 0x3FF;
562 u32 key_map(SDLKey key_sym)
588 return BUTTON_SELECT;
601 u32 joy_map(u32 button)
615 return BUTTON_SELECT;
628 gui_action_type get_gui_input()
631 gui_action_type gui_action = CURSOR_NONE;
635 while(SDL_PollEvent(&event))
644 switch(event.key.keysym.sym)
647 gui_action = CURSOR_EXIT;
651 gui_action = CURSOR_DOWN;
655 gui_action = CURSOR_UP;
659 gui_action = CURSOR_LEFT;
663 gui_action = CURSOR_RIGHT;
667 gui_action = CURSOR_SELECT;
671 gui_action = CURSOR_BACK;
686 while(SDL_PollEvent(&event))
695 if(event.key.keysym.sym == SDLK_ESCAPE)
700 if(event.key.keysym.sym == SDLK_BACKSPACE)
702 u16 *screen_copy = copy_screen();
703 u32 ret_val = menu(screen_copy);
710 if(event.key.keysym.sym == SDLK_F1)
716 if(event.key.keysym.sym == SDLK_F2)
718 FILE *fp = fopen("palette_ram.bin", "wb");
719 printf("writing palette RAM\n");
720 fwrite(palette_ram, 1024, 1, fp);
722 printf("writing palette VRAM\n");
723 fp = fopen("vram.bin", "wb");
724 fwrite(vram, 1024 * 96, 1, fp);
726 printf("writing palette OAM RAM\n");
727 fp = fopen("oam_ram.bin", "wb");
728 fwrite(oam_ram, 1024, 1, fp);
730 printf("writing palette I/O registers\n");
731 fp = fopen("io_registers.bin", "wb");
732 fwrite(io_registers, 1024, 1, fp);
737 if(event.key.keysym.sym == SDLK_F3)
739 dump_translation_cache();
743 if(event.key.keysym.sym == SDLK_F5)
745 u8 current_savestate_filename[512];
746 u16 *current_screen = copy_screen();
747 get_savestate_filename_noshot(savestate_slot,
748 current_savestate_filename);
749 save_state(current_savestate_filename, current_screen);
750 free(current_screen);
754 if(event.key.keysym.sym == SDLK_F7)
756 u8 current_savestate_filename[512];
757 get_savestate_filename_noshot(savestate_slot,
758 current_savestate_filename);
759 load_state(current_savestate_filename);
765 if(event.key.keysym.sym == SDLK_BACKQUOTE)
767 synchronize_flag ^= 1;
771 key |= key_map(event.key.keysym.sym);
780 key &= ~(key_map(event.key.keysym.sym));
784 case SDL_JOYBUTTONDOWN:
786 key |= joy_map(event.jbutton.button);
791 case SDL_JOYBUTTONUP:
793 key &= ~(joy_map(event.jbutton.button));
799 io_registers[REG_P1] = (~key) & 0x3FF;
806 u32 joystick_count = SDL_NumJoysticks();
808 if(joystick_count > 0)
811 SDL_JoystickEventState(SDL_ENABLE);
818 #define input_savestate_builder(type) \
819 void input_##type##_savestate(file_tag_type savestate_file) \
821 file_##type##_variable(savestate_file, key); \
824 input_savestate_builder(read);
825 input_savestate_builder(write_mem);