From 94a9fb3f712888681024fc3809c14b9073c26062 Mon Sep 17 00:00:00 2001 From: gameblabla Date: Fri, 29 Mar 2019 00:06:49 +0100 Subject: [PATCH] Support for remapping buttons on the Bittboy. --- bittboy/Makefile | 5 +- gui.c | 123 +++++++++++++++++++++++------------ input.c | 166 +++++++++++++++++++++++++++++------------------ main.c | 4 ++ 4 files changed, 193 insertions(+), 105 deletions(-) diff --git a/bittboy/Makefile b/bittboy/Makefile index 46168256..22464e8c 100644 --- a/bittboy/Makefile +++ b/bittboy/Makefile @@ -16,13 +16,12 @@ BIN = gpsp VPATH += .. ../arm CFLAGS += -DARM_ARCH -DPC_BUILD -Wall -CFLAGS += -O2 -fdata-sections -ffunction-sections -fno-PIC -fprofile-use +CFLAGS += -Ofast -fdata-sections -ffunction-sections -flto -fno-PIC -fprofile-use CFLAGS += `/opt/bittboy-toolchain/arm-miyoo-linux-musleabi/sysroot/usr/bin/sdl-config --cflags` # expecting to have PATH set up to get correct sdl-config first -LIBS += -lSDL -LIBS += -ldl -lpthread -lz -lgcov -lasound -Wl,--as-needed -Wl,--gc-sections -flto -s +LIBS = -lc -lgcc -lSDL -lasound -lz -Wl,--as-needed -Wl,--gc-sections -flto -s # Compilation: diff --git a/gui.c b/gui.c index e3e0dec0..340fd222 100644 --- a/gui.c +++ b/gui.c @@ -753,6 +753,11 @@ u32 gamepad_config_line_to_button[] = #endif +#ifdef PC_BUILD +u32 gamepad_config_line_to_button[] = + { 0, 2, 1, 3, 8, 9, 10, 11, 6, 7, 4, 5, 12, 13, 14, 15 }; +#endif + static const char *scale_options[] = { #ifdef PSP_BUILD @@ -891,6 +896,38 @@ enum file_options { #endif #define FILE_OPTION_COUNT (fo_main_option_count + PLAT_BUTTON_COUNT) +/* We need to quadruple the size of struct in order to avoid issues. */ + +void load_controllers() +{ + char config_path[512]; + FILE* fp; + + snprintf(config_path, sizeof(config_path), "%s" PATH_SEPARATOR "%s", main_path, "controls.cfg"); + fp = fopen(config_path, "rb"); + if (fp) + { + fread(&gamepad_config_map, 12*sizeof(uint32_t), sizeof(uint8_t), fp); + fread(&gamepad_config_line_to_button, 12*sizeof(uint32_t), sizeof(uint8_t), fp); + fclose(fp); + } +} + +void save_controllers() +{ + char config_path[512]; + FILE* fp; + + snprintf(config_path, sizeof(config_path), "%s" PATH_SEPARATOR "%s", main_path, "controls.cfg"); + fp = fopen(config_path, "wb"); + if (fp) + { + fwrite(&gamepad_config_map, 12*sizeof(uint32_t), sizeof(uint8_t), fp); + fwrite(&gamepad_config_line_to_button, 12*sizeof(uint32_t), sizeof(uint8_t), fp); + fclose(fp); + } +} + s32 load_config_file() { char config_path[512]; @@ -944,13 +981,12 @@ s32 load_config_file() } } - if(menu_button == -1 && PLAT_MENU_BUTTON >= 0) + /*if(menu_button == -1 && PLAT_MENU_BUTTON >= 0) { gamepad_config_map[PLAT_MENU_BUTTON] = BUTTON_ID_MENU; - } + }*/ #endif - - file_close(config_file); + file_close(config_file); } return 0; @@ -992,12 +1028,13 @@ s32 save_game_config_file() return -1; } + s32 save_config_file() { char config_path[512]; sprintf(config_path, "%s" PATH_SEPARATOR "%s", main_path, GPSP_CONFIG_FILENAME); - + file_open(config_file, config_path, write); save_game_config_file(); @@ -1132,7 +1169,7 @@ u32 menu(u16 *original_screen) auto void choose_menu(); auto void clear_help(); -#ifndef PC_BUILD +//#ifndef PC_BUILD static const char * const gamepad_help[] = { "Up button on GBA d-pad.", @@ -1162,8 +1199,8 @@ u32 menu(u16 *original_screen) static const char *gamepad_config_buttons[] = { "UP", - "DOWN", "LEFT", + "DOWN", "RIGHT", "A", "B", @@ -1171,20 +1208,13 @@ u32 menu(u16 *original_screen) "R", "START", "SELECT", - "MENU", - "FASTFORWARD", + /* Disabled for now */ + /*"FASTFORWARD", "LOAD STATE", - "SAVE STATE", - "RAPIDFIRE A", - "RAPIDFIRE B", - "RAPIDFIRE L", - "RAPIDFIRE R", - "VOLUME UP", - "VOLUME DOWN", - "DISPLAY FPS", + "SAVE STATE",*/ "NOTHING" }; -#endif +//#endif void menu_update_clock() { @@ -1207,6 +1237,7 @@ u32 menu(u16 *original_screen) { menu_get_clock_speed(); save_config_file(); + save_controllers(); quit(); } @@ -1304,7 +1335,7 @@ u32 menu(u16 *original_screen) void submenu_gamepad() { - + print_string("Input remapping menu", COLOR_ROM_INFO, COLOR_BG, 6, 10); } void submenu_analog() @@ -1314,20 +1345,20 @@ u32 menu(u16 *original_screen) void submenu_savestate() { - print_string("Savestate options:", COLOR_ACTIVE_ITEM, COLOR_BG, 10, 70); - menu_change_state(); + print_string("Savestate options:", COLOR_ACTIVE_ITEM, COLOR_BG, 10, 70); + menu_change_state(); } void submenu_main() { - strncpy(print_buffer, gamepak_filename, 80); - print_string(print_buffer, COLOR_ROM_INFO, COLOR_BG, 10, 10); - sprintf(print_buffer, "%s %s %s", gamepak_title, - gamepak_code, gamepak_maker); - print_string(print_buffer, COLOR_ROM_INFO, COLOR_BG, 10, 20); - - get_savestate_filename_noshot(savestate_slot, - current_savestate_filename); + print_string("Ported to Bittboy by Gameblabla", COLOR_ROM_INFO, COLOR_BG, 6, 184); + + strncpy(print_buffer, gamepak_filename, 80); + print_string(print_buffer, COLOR_ROM_INFO, COLOR_BG, 6, 10); + sprintf(print_buffer, "%s %s %s", gamepak_title, gamepak_code, gamepak_maker); + print_string(print_buffer, COLOR_ROM_INFO, COLOR_BG, 6, 20); + + get_savestate_filename_noshot(savestate_slot, current_savestate_filename); } const char *yes_no_options[] = { "no", "yes" }; @@ -1589,18 +1620,30 @@ u32 menu(u16 *original_screen) menu_option_type gamepad_config_options[] = { - submenu_option(NULL, "Back", "Return to the main menu.", 13) + gamepad_config_option("D-pad up ", 0), + gamepad_config_option("D-pad down ", 1), + gamepad_config_option("D-pad left ", 2), + gamepad_config_option("D-pad right ", 3), + gamepad_config_option("A ", 4), + gamepad_config_option("B ", 5), + gamepad_config_option("X ", 6), + gamepad_config_option("Y ", 7), + gamepad_config_option("Left Trigger ", 8), + gamepad_config_option("Right Trigger", 9), + gamepad_config_option("Start ", 10), + gamepad_config_option("Select ", 11), + submenu_option(NULL, "Back", "Return to the main menu.", 12) }; - menu_option_type analog_config_options[] = + /*menu_option_type analog_config_options[] = { submenu_option(NULL, "Back", "Return to the main menu.", 11) }; - +*/ #endif make_menu(gamepad_config, submenu_gamepad, NULL); - make_menu(analog_config, submenu_analog, NULL); + //make_menu(analog_config, submenu_analog, NULL); menu_option_type main_options[] = { @@ -1625,22 +1668,22 @@ u32 menu(u16 *original_screen) "Select to change the in-game behavior of buttons\n" "and d-pad.", 6), #ifndef WIZ_BUILD - submenu_option(&analog_config_menu, "Configure analog input", - "Select to change the in-game behavior of the analog nub.", 7), + /*submenu_option(&analog_config_menu, "Configure analog input", + "Select to change the in-game behavior of the analog nub.", 7),*/ #endif submenu_option(&cheats_misc_menu, "Cheats and Miscellaneous options", "Select to manage cheats, set backup behavior,\n" - "and set device clock speed.", 9), + "and set device clock speed.", 7), action_option(menu_load, NULL, "Load new game", "Select to load a new game\n" - "(will exit a game if currently playing).", 11), + "(will exit a game if currently playing).", 8), action_option(menu_restart, NULL, "Restart game", "Select to reset the GBA with the current game\n" - "loaded.", 12), + "loaded.", 9), action_option(menu_exit, NULL, "Return to game", - "Select to exit this menu and resume gameplay.", 13), + "Select to exit this menu and resume gameplay.", 10), action_option(menu_quit, NULL, "Exit gpSP", - "Select to exit gpSP and return to the menu.", 15) + "Select to exit gpSP and return to the menu.", 12) }; make_menu(main, submenu_main, NULL); diff --git a/input.c b/input.c index 3d9ab12d..418cb75c 100644 --- a/input.c +++ b/input.c @@ -25,10 +25,10 @@ extern uint16_t io_registers[1024 * 16]; void trigger_key(u32 key) { - u32 p1_cnt = io_registers[REG_P1CNT]; + u32 p1_cnt = io_registers[REG_P1CNT]; - if((p1_cnt >> 14) & 0x01) - { + if((p1_cnt >> 14) & 0x01) + { u32 key_intersection = (p1_cnt & key) & 0x3FF; if(p1_cnt >> 15) @@ -38,10 +38,10 @@ void trigger_key(u32 key) } else { - if(key_intersection) - raise_interrupt(IRQ_KEYPAD); - } - } + if(key_intersection) + raise_interrupt(IRQ_KEYPAD); + } + } } u32 key = 0; @@ -67,6 +67,24 @@ u32 button_repeat = 0; gui_action_type cursor_repeat = CURSOR_NONE; +#ifdef PC_BUILD +u32 gamepad_config_map[12] = +{ + BUTTON_ID_UP, // Analog up + BUTTON_ID_DOWN, // Analog down + BUTTON_ID_LEFT, // Analog left + BUTTON_ID_RIGHT, // Analog right + BUTTON_ID_START, // Circle + BUTTON_ID_SELECT, // Cross + BUTTON_ID_L, // Ltrigger + BUTTON_ID_R, // Rtrigger + BUTTON_ID_A, // Square + BUTTON_ID_B, // Select + BUTTON_ID_L, // Square + BUTTON_ID_R // Select +}; +#endif + #ifdef PSP_BUILD u32 gamepad_config_map[16] = @@ -601,44 +619,101 @@ u32 key_map(SDLKey key_sym) #if defined(PC_BUILD) -u32 key_map(SDLKey key_sym) +extern u32 gamepad_config_line_to_button[]; + +uint32_t tokey(uint32_t i) +{ + switch(gamepad_config_map[gamepad_config_line_to_button[i]]) + { + // UP + case 0: + return BUTTON_UP; + break; + // LEFT + case 1: + return BUTTON_LEFT; + break; + // DOWN + case 2: + return BUTTON_DOWN; + break; + // RIGHT + case 3: + return BUTTON_RIGHT; + break; + // A + case 4: + return BUTTON_A; + break; + // B + case 5: + return BUTTON_B; + break; + // L + case 6: + return BUTTON_L; + break; + // R + case 7: + return BUTTON_R; + break; + // Start + case 8: + return BUTTON_START; + break; + // Select + case 9: + return BUTTON_SELECT; + break; + } + return 0; +} + +u32 key_map(u32 key_sym) { switch(key_sym) { - case SDLK_LSHIFT: - return BUTTON_L; - - case SDLK_SPACE: - return BUTTON_R; - - case SDLK_DOWN: - return BUTTON_DOWN; - case SDLK_UP: - return BUTTON_UP; + return tokey(0); + + case SDLK_DOWN: + return tokey(1); case SDLK_LEFT: - return BUTTON_LEFT; + return tokey(2); case SDLK_RIGHT: - return BUTTON_RIGHT; + return tokey(3); + + case SDLK_LCTRL: + return tokey(4); - case SDLK_RETURN: - return BUTTON_START; + case SDLK_LALT: + return tokey(5); + + case SDLK_LSHIFT: + return tokey(6); - case SDLK_ESCAPE: - return BUTTON_SELECT; + case SDLK_SPACE: + return tokey(7); + + case SDLK_TAB: + return tokey(8); - case SDLK_LCTRL: - return BUTTON_A; + case SDLK_BACKSPACE: + return tokey(9); - case SDLK_LALT: - return BUTTON_B; + case SDLK_RETURN: + return tokey(10); + + case SDLK_ESCAPE: + return tokey(11); default: return BUTTON_NONE; } } + #endif #if defined(PC_BUILD) || defined(RPI_BUILD) @@ -795,40 +870,6 @@ u32 update_input() return ret_val; } else -#ifdef PC_BUILD - if(event.key.keysym.sym == SDLK_F1) - { - debug_on(); - } - else - - if(event.key.keysym.sym == SDLK_F2) - { - FILE *fp = fopen("palette_ram.bin", "wb"); - printf("writing palette RAM\n"); - fwrite(palette_ram, 1024, 1, fp); - fclose(fp); - printf("writing palette VRAM\n"); - fp = fopen("vram.bin", "wb"); - fwrite(vram, 1024 * 96, 1, fp); - fclose(fp); - printf("writing palette OAM RAM\n"); - fp = fopen("oam_ram.bin", "wb"); - fwrite(oam_ram, 1024, 1, fp); - fclose(fp); - printf("writing palette I/O registers\n"); - fp = fopen("io_registers.bin", "wb"); - fwrite(io_registers, 1024, 1, fp); - fclose(fp); - } - else - - if(event.key.keysym.sym == SDLK_F3) - { - dump_translation_cache(); - } - else -#endif if(event.key.keysym.sym == SDLK_F5) { char current_savestate_filename[512]; @@ -859,6 +900,7 @@ u32 update_input() { key |= key_map(event.key.keysym.sym); trigger_key(key); + } break; diff --git a/main.c b/main.c index 8113ef97..bebdc381 100644 --- a/main.c +++ b/main.c @@ -224,6 +224,8 @@ void init_main() flush_translation_cache_bios(); } +extern void load_controllers(); + int main(int argc, char *argv[]) { char bios_filename[512]; @@ -302,6 +304,8 @@ int main(int argc, char *argv[]) init_input(); video_resolution_large(); + + load_controllers(); if(argc > 1) {