| 1 |
|
| #include "gb.h" |
| 2 |
|
| #include <assert.h> |
| 3 |
|
| #include <math.h> |
| 4 |
|
| |
| 5 |
2 |
| static inline bool should_bounce(GB_gameboy_t *gb) |
| 6 |
|
| { |
| 7 |
|
| // Bouncing is super rare on an AGS, so don't emulate it on GB_MODEL_AGB_B (when addeed) |
| 8 |
2 |
▶ | return !GB_is_sgb(gb) && !gb-> no_bouncing_emulation && !(gb->model & GB_MODEL_GBP_BIT) /*&& gb->model != GB_MODEL_AGB_B*/; |
Branch: 3 branches
| Branch | True | False | Total |
| return !GB_is_sgb(gb) && !gb-> no_bouncing_emulation && !(gb->model & GB_MODEL_GBP_BIT) /*&& gb->model != GB_MODEL_AGB_B*/; |
2 |
0 |
2 |
| return !GB_is_sgb(gb) && !gb-> no_bouncing_emulation && !(gb->model & GB_MODEL_GBP_BIT) /*&& gb->model != GB_MODEL_AGB_B*/; |
2 |
0 |
2 |
| return !GB_is_sgb(gb) && !gb-> no_bouncing_emulation && !(gb->model & GB_MODEL_GBP_BIT) /*&& gb->model != GB_MODEL_AGB_B*/; |
2 |
0 |
2 |
|
| 9 |
|
| } |
| 10 |
|
| |
| 11 |
2 |
| static inline uint16_t bounce_for_key(GB_gameboy_t *gb, GB_key_t key) |
| 12 |
|
| { |
| 13 |
2 |
▶ | if (gb->model > GB_MODEL_CGB_E) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->model > GB_MODEL_CGB_E) { |
0 |
2 |
2 |
|
| 14 |
|
| // AGB are less bouncy |
| 15 |
0 |
| return 0xBFF; |
| 16 |
|
| } |
| 17 |
2 |
▶ | if (key == GB_KEY_START || key == GB_KEY_SELECT) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (key == GB_KEY_START || key == GB_KEY_SELECT) { |
2 |
0 |
2 |
| if (key == GB_KEY_START || key == GB_KEY_SELECT) { |
0 |
2 |
2 |
|
| 18 |
0 |
| return 0x1FFF; |
| 19 |
|
| } |
| 20 |
2 |
| return 0xFFF; |
| 21 |
|
| } |
| 22 |
|
| |
| 23 |
8228 |
| static inline bool get_input(GB_gameboy_t *gb, uint8_t player, GB_key_t key) |
| 24 |
|
| { |
| 25 |
8228 |
▶ | if (gb->use_faux_analog[player] && key <= GB_KEY_DOWN) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (gb->use_faux_analog[player] && key <= GB_KEY_DOWN) { |
0 |
8228 |
8228 |
| if (gb->use_faux_analog[player] && key <= GB_KEY_DOWN) { |
0 |
0 |
0 |
|
| 26 |
0 |
▶ | if (gb->keys[player][key]) return true; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->keys[player][key]) return true; |
0 |
0 |
0 |
|
| 27 |
0 |
| uint8_t pattern = 0; |
| 28 |
|
| uint8_t index_in_pattern; |
| 29 |
0 |
| switch (key) { |
| 30 |
|
| // Most games only sample inputs in 30FPS, so we shift right once. |
| 31 |
0 |
| case GB_KEY_RIGHT: |
| 32 |
0 |
▶ | if (gb->faux_analog_inputs[player].x <= 0) return false; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->faux_analog_inputs[player].x <= 0) return false; |
0 |
0 |
0 |
|
| 33 |
0 |
| pattern = gb->faux_analog_inputs[player].x - 1; |
| 34 |
0 |
| index_in_pattern = gb->faux_analog_ticks; |
| 35 |
0 |
| break; |
| 36 |
0 |
| case GB_KEY_LEFT: |
| 37 |
0 |
▶ | if (gb->faux_analog_inputs[player].x >= 0) return false; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->faux_analog_inputs[player].x >= 0) return false; |
0 |
0 |
0 |
|
| 38 |
0 |
| pattern = -gb->faux_analog_inputs[player].x - 1; |
| 39 |
0 |
| index_in_pattern = gb->faux_analog_ticks; |
| 40 |
0 |
| break; |
| 41 |
0 |
| case GB_KEY_UP: |
| 42 |
0 |
▶ | if (gb->faux_analog_inputs[player].y >= 0) return false; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->faux_analog_inputs[player].y >= 0) return false; |
0 |
0 |
0 |
|
| 43 |
0 |
| pattern = -gb->faux_analog_inputs[player].y - 1; |
| 44 |
0 |
| index_in_pattern = gb->faux_analog_ticks + 2; |
| 45 |
0 |
| break; |
| 46 |
0 |
| case GB_KEY_DOWN: |
| 47 |
0 |
▶ | if (gb->faux_analog_inputs[player].y <= 0) return false; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->faux_analog_inputs[player].y <= 0) return false; |
0 |
0 |
0 |
|
| 48 |
0 |
| pattern = gb->faux_analog_inputs[player].y - 1; |
| 49 |
0 |
| index_in_pattern = gb->faux_analog_ticks + 2; |
| 50 |
0 |
| break; |
| 51 |
0 |
| nodefault; |
| 52 |
|
| } |
| 53 |
0 |
▶ | if (pattern == 7) return true; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pattern == 7) return true; |
0 |
0 |
0 |
|
| 54 |
|
| /* Dithering pattern */ |
| 55 |
|
| static const uint8_t patterns[] = { |
| 56 |
|
| 0x1, |
| 57 |
|
| 0x11, |
| 58 |
|
| 0x94, |
| 59 |
|
| 0x55, |
| 60 |
|
| 0x6d, |
| 61 |
|
| 0x77, |
| 62 |
|
| 0x7f |
| 63 |
|
| }; |
| 64 |
0 |
| return patterns[pattern] & (1 << (index_in_pattern & 6)); |
| 65 |
|
| } |
| 66 |
8228 |
▶ | if (player != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (player != 0) { |
0 |
8228 |
8228 |
|
| 67 |
0 |
| return gb->keys[player][key]; |
| 68 |
|
| } |
| 69 |
8228 |
| bool ret = gb->keys[player][key]; |
| 70 |
|
| |
| 71 |
8228 |
▶ | if (likely(gb->key_bounce_timing[key] == 0)) return ret; |
Branch: 1 branch
| Branch | True | False | Total |
| if (likely(gb->key_bounce_timing[key] == 0)) return ret; |
7204 |
1024 |
8228 |
|
| 72 |
1024 |
▶ | if (likely((gb->key_bounce_timing[key] & 0x3FF) > 0x300)) return ret; |
Branch: 1 branch
| Branch | True | False | Total |
| if (likely((gb->key_bounce_timing[key] & 0x3FF) > 0x300)) return ret; |
256 |
768 |
1024 |
|
| 73 |
768 |
| uint16_t semi_random = ((((key << 5) + gb->div_counter) * 17) ^ ((gb->apu.apu_cycles + gb->display_cycles) * 13)); |
| 74 |
768 |
| semi_random >>= 3; |
| 75 |
768 |
▶ | if (semi_random < gb->key_bounce_timing[key]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (semi_random < gb->key_bounce_timing[key]) { |
294 |
474 |
768 |
|
| 76 |
294 |
| ret ^= true; |
| 77 |
|
| } |
| 78 |
768 |
| return ret; |
| 79 |
|
| } |
| 80 |
|
| |
| 81 |
1031 |
| void GB_update_joyp(GB_gameboy_t *gb) |
| 82 |
|
| { |
| 83 |
1031 |
▶ | if (gb->model & GB_MODEL_NO_SFC_BIT) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->model & GB_MODEL_NO_SFC_BIT) return; |
0 |
1031 |
1031 |
|
| 84 |
|
| |
| 85 |
1031 |
| uint8_t key_selection = 0; |
| 86 |
1031 |
| uint8_t previous_state = 0; |
| 87 |
|
| |
| 88 |
1031 |
| previous_state = gb->io_registers[GB_IO_JOYP] & 0xF; |
| 89 |
1031 |
| key_selection = (gb->io_registers[GB_IO_JOYP] >> 4) & 3; |
| 90 |
1031 |
| gb->io_registers[GB_IO_JOYP] &= 0xF0; |
| 91 |
1031 |
▶ | uint8_t current_player = gb->sgb? gb->sgb->current_player : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| uint8_t current_player = gb->sgb? gb->sgb->current_player : 0; |
0 |
1031 |
1031 |
|
| 92 |
1031 |
| switch (key_selection) { |
| 93 |
0 |
| case 3: |
| 94 |
0 |
▶ | if (gb->sgb && gb->sgb->player_count > 1) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (gb->sgb && gb->sgb->player_count > 1) { |
0 |
0 |
0 |
| if (gb->sgb && gb->sgb->player_count > 1) { |
0 |
0 |
0 |
|
| 95 |
0 |
| gb->io_registers[GB_IO_JOYP] |= 0xF - current_player; |
| 96 |
|
| } |
| 97 |
|
| else { |
| 98 |
|
| /* Nothing is wired, all up */ |
| 99 |
0 |
| gb->io_registers[GB_IO_JOYP] |= 0x0F; |
| 100 |
|
| } |
| 101 |
0 |
| break; |
| 102 |
|
| |
| 103 |
5 |
| case 2: |
| 104 |
|
| /* Direction keys */ |
| 105 |
25 |
▶ | for (uint8_t i = 0; i < 4; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (uint8_t i = 0; i < 4; i++) { |
20 |
5 |
25 |
|
| 106 |
20 |
| gb->io_registers[GB_IO_JOYP] |= (!get_input(gb, current_player, i)) << i; |
| 107 |
|
| } |
| 108 |
|
| /* Forbid pressing two opposing keys, this breaks a lot of games; even if it's somewhat possible. */ |
| 109 |
5 |
▶ | if (likely(!gb->illegal_inputs_allowed)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (likely(!gb->illegal_inputs_allowed)) { |
5 |
0 |
5 |
|
| 110 |
5 |
▶ | if (!(gb->io_registers[GB_IO_JOYP] & 1)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(gb->io_registers[GB_IO_JOYP] & 1)) { |
0 |
5 |
5 |
|
| 111 |
0 |
| gb->io_registers[GB_IO_JOYP] |= 2; |
| 112 |
|
| } |
| 113 |
5 |
▶ | if (!(gb->io_registers[GB_IO_JOYP] & 4)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(gb->io_registers[GB_IO_JOYP] & 4)) { |
0 |
5 |
5 |
|
| 114 |
0 |
| gb->io_registers[GB_IO_JOYP] |= 8; |
| 115 |
|
| } |
| 116 |
|
| } |
| 117 |
5 |
| break; |
| 118 |
|
| |
| 119 |
0 |
| case 1: |
| 120 |
|
| /* Other keys */ |
| 121 |
0 |
▶ | for (uint8_t i = 0; i < 4; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (uint8_t i = 0; i < 4; i++) { |
0 |
0 |
0 |
|
| 122 |
0 |
| gb->io_registers[GB_IO_JOYP] |= (!get_input(gb, current_player, i + 4)) << i; |
| 123 |
|
| } |
| 124 |
0 |
| break; |
| 125 |
|
| |
| 126 |
1026 |
| case 0: |
| 127 |
5130 |
▶ | for (uint8_t i = 0; i < 4; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (uint8_t i = 0; i < 4; i++) { |
4104 |
1026 |
5130 |
|
| 128 |
4104 |
▶ | gb->io_registers[GB_IO_JOYP] |= (!(get_input(gb, current_player, i) || get_input(gb, current_player, i + 4))) << i; |
Branch: 2 branches
| Branch | True | False | Total |
| gb->io_registers[GB_IO_JOYP] |= (!(get_input(gb, current_player, i) || get_input(gb, current_player, i + 4))) << i; |
4104 |
0 |
4104 |
| gb->io_registers[GB_IO_JOYP] |= (!(get_input(gb, current_player, i) || get_input(gb, current_player, i + 4))) << i; |
3499 |
605 |
4104 |
|
| 129 |
|
| } |
| 130 |
1026 |
| break; |
| 131 |
|
| |
| 132 |
0 |
| nodefault; |
| 133 |
|
| } |
| 134 |
|
| |
| 135 |
|
| // TODO: Implement the lame anti-debouncing mechanism as seen on the DMG schematics |
| 136 |
1031 |
▶ | if (previous_state & ~(gb->io_registers[GB_IO_JOYP] & 0xF)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (previous_state & ~(gb->io_registers[GB_IO_JOYP] & 0xF)) { |
12 |
1019 |
1031 |
|
| 137 |
12 |
▶ | if (!(gb->io_registers[GB_IO_IF] & 0x10)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(gb->io_registers[GB_IO_IF] & 0x10)) { |
1 |
11 |
12 |
|
| 138 |
1 |
| gb->joyp_accessed = true; |
| 139 |
1 |
| gb->io_registers[GB_IO_IF] |= 0x10; |
| 140 |
|
| } |
| 141 |
|
| } |
| 142 |
|
| |
| 143 |
1031 |
| gb->io_registers[GB_IO_JOYP] |= 0xC0; |
| 144 |
|
| } |
| 145 |
|
| |
| 146 |
0 |
| void GB_icd_set_joyp(GB_gameboy_t *gb, uint8_t value) |
| 147 |
|
| { |
| 148 |
0 |
| uint8_t previous_state = gb->io_registers[GB_IO_JOYP] & 0xF; |
| 149 |
0 |
| gb->io_registers[GB_IO_JOYP] &= 0xF0; |
| 150 |
0 |
| gb->io_registers[GB_IO_JOYP] |= value & 0xF; |
| 151 |
|
| |
| 152 |
0 |
▶ | if (previous_state & ~(gb->io_registers[GB_IO_JOYP] & 0xF)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (previous_state & ~(gb->io_registers[GB_IO_JOYP] & 0xF)) { |
0 |
0 |
0 |
|
| 153 |
0 |
▶ | if (!(gb->io_registers[GB_IO_IF] & 0x10)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(gb->io_registers[GB_IO_IF] & 0x10)) { |
0 |
0 |
0 |
|
| 154 |
0 |
| gb->joyp_accessed = true; |
| 155 |
0 |
| gb->io_registers[GB_IO_IF] |= 0x10; |
| 156 |
|
| } |
| 157 |
|
| } |
| 158 |
0 |
| gb->io_registers[GB_IO_JOYP] |= 0xC0; |
| 159 |
0 |
| } |
| 160 |
|
| |
| 161 |
2 |
| void GB_set_key_state(GB_gameboy_t *gb, GB_key_t index, bool pressed) |
| 162 |
|
| { |
| 163 |
|
| assert(index >= 0 && index < GB_KEY_MAX); |
| 164 |
2 |
▶ | if (should_bounce(gb) && pressed != gb->keys[0][index]) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (should_bounce(gb) && pressed != gb->keys[0][index]) { |
2 |
0 |
2 |
| if (should_bounce(gb) && pressed != gb->keys[0][index]) { |
2 |
0 |
2 |
|
| 165 |
2 |
| gb->joypad_is_stable = false; |
| 166 |
2 |
| gb->key_bounce_timing[index] = bounce_for_key(gb, index); |
| 167 |
|
| } |
| 168 |
2 |
| gb->keys[0][index] = pressed; |
| 169 |
2 |
| GB_update_joyp(gb); |
| 170 |
2 |
| } |
| 171 |
|
| |
| 172 |
0 |
| void GB_set_key_state_for_player(GB_gameboy_t *gb, GB_key_t index, unsigned player, bool pressed) |
| 173 |
|
| { |
| 174 |
|
| assert(index >= 0 && index < GB_KEY_MAX); |
| 175 |
|
| assert(player < 4); |
| 176 |
0 |
▶ | if (should_bounce(gb) && pressed != gb->keys[player][index]) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (should_bounce(gb) && pressed != gb->keys[player][index]) { |
0 |
0 |
0 |
| if (should_bounce(gb) && pressed != gb->keys[player][index]) { |
0 |
0 |
0 |
|
| 177 |
0 |
| gb->joypad_is_stable = false; |
| 178 |
0 |
| gb->key_bounce_timing[index] = bounce_for_key(gb, index); |
| 179 |
|
| } |
| 180 |
0 |
| gb->keys[player][index] = pressed; |
| 181 |
0 |
| GB_update_joyp(gb); |
| 182 |
0 |
| } |
| 183 |
|
| |
| 184 |
0 |
| void GB_set_key_mask(GB_gameboy_t *gb, GB_key_mask_t mask) |
| 185 |
|
| { |
| 186 |
0 |
▶ | for (unsigned i = 0; i < GB_KEY_MAX; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned i = 0; i < GB_KEY_MAX; i++) { |
0 |
0 |
0 |
|
| 187 |
0 |
| bool pressed = mask & (1 << i); |
| 188 |
0 |
▶ | if (should_bounce(gb) && pressed != gb->keys[0][i]) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (should_bounce(gb) && pressed != gb->keys[0][i]) { |
0 |
0 |
0 |
| if (should_bounce(gb) && pressed != gb->keys[0][i]) { |
0 |
0 |
0 |
|
| 189 |
0 |
| gb->joypad_is_stable = false; |
| 190 |
0 |
| gb->key_bounce_timing[i] = bounce_for_key(gb, i); |
| 191 |
|
| } |
| 192 |
0 |
| gb->keys[0][i] = pressed; |
| 193 |
|
| } |
| 194 |
|
| |
| 195 |
0 |
| GB_update_joyp(gb); |
| 196 |
0 |
| } |
| 197 |
|
| |
| 198 |
0 |
| void GB_set_key_mask_for_player(GB_gameboy_t *gb, GB_key_mask_t mask, unsigned player) |
| 199 |
|
| { |
| 200 |
0 |
▶ | for (unsigned i = 0; i < GB_KEY_MAX; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned i = 0; i < GB_KEY_MAX; i++) { |
0 |
0 |
0 |
|
| 201 |
0 |
| bool pressed = mask & (1 << i); |
| 202 |
0 |
▶ | if (should_bounce(gb) && pressed != gb->keys[player][i]) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (should_bounce(gb) && pressed != gb->keys[player][i]) { |
0 |
0 |
0 |
| if (should_bounce(gb) && pressed != gb->keys[player][i]) { |
0 |
0 |
0 |
|
| 203 |
0 |
| gb->joypad_is_stable = false; |
| 204 |
0 |
| gb->key_bounce_timing[i] = bounce_for_key(gb, i); |
| 205 |
|
| } |
| 206 |
0 |
| gb->keys[player][i] = pressed; |
| 207 |
|
| } |
| 208 |
|
| |
| 209 |
0 |
| GB_update_joyp(gb); |
| 210 |
0 |
| } |
| 211 |
|
| |
| 212 |
5043155 |
| void GB_joypad_run(GB_gameboy_t *gb, unsigned cycles) |
| 213 |
|
| { |
| 214 |
5043155 |
▶ | if (gb->joypad_is_stable) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->joypad_is_stable) return; |
5042120 |
1035 |
5043155 |
|
| 215 |
1035 |
▶ | bool should_update_joyp = gb->use_faux_analog[gb->sgb? gb->sgb->current_player : 0]; |
Branch: 1 branch
| Branch | True | False | Total |
| bool should_update_joyp = gb->use_faux_analog[gb->sgb? gb->sgb->current_player : 0]; |
0 |
1035 |
1035 |
|
| 216 |
1035 |
| gb->joypad_is_stable = true; |
| 217 |
1035 |
▶ | if (gb->joyp_switching_delay) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->joyp_switching_delay) { |
0 |
1035 |
1035 |
|
| 218 |
0 |
| gb->joypad_is_stable = false; |
| 219 |
0 |
▶ | if (gb->joyp_switching_delay > cycles) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->joyp_switching_delay > cycles) { |
0 |
0 |
0 |
|
| 220 |
0 |
| gb->joyp_switching_delay -= cycles; |
| 221 |
|
| } |
| 222 |
|
| else { |
| 223 |
0 |
| gb->joyp_switching_delay = 0; |
| 224 |
0 |
| gb->io_registers[GB_IO_JOYP] = (gb->joyp_switch_value & 0xF0) | (gb->io_registers[GB_IO_JOYP] & 0x0F); |
| 225 |
0 |
| should_update_joyp = true; |
| 226 |
|
| } |
| 227 |
|
| } |
| 228 |
|
| |
| 229 |
9315 |
▶ | for (unsigned i = 0; i < GB_KEY_MAX; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned i = 0; i < GB_KEY_MAX; i++) { |
8280 |
1035 |
9315 |
|
| 230 |
8280 |
▶ | if (gb->key_bounce_timing[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->key_bounce_timing[i]) { |
1024 |
7256 |
8280 |
|
| 231 |
1024 |
| gb->joypad_is_stable = false; |
| 232 |
1024 |
| should_update_joyp = true; |
| 233 |
1024 |
▶ | if (gb->key_bounce_timing[i] > cycles) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->key_bounce_timing[i] > cycles) { |
1022 |
2 |
1024 |
|
| 234 |
1022 |
| gb->key_bounce_timing[i] -= cycles; |
| 235 |
|
| } |
| 236 |
|
| else { |
| 237 |
2 |
| gb->key_bounce_timing[i] = 0; |
| 238 |
|
| } |
| 239 |
|
| } |
| 240 |
|
| } |
| 241 |
|
| |
| 242 |
1035 |
▶ | if (should_update_joyp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (should_update_joyp) { |
1024 |
11 |
1035 |
|
| 243 |
1024 |
| GB_update_joyp(gb); |
| 244 |
|
| } |
| 245 |
|
| } |
| 246 |
|
| |
| 247 |
0 |
| bool GB_get_joyp_accessed(GB_gameboy_t *gb) |
| 248 |
|
| { |
| 249 |
0 |
| return gb->joyp_accessed; |
| 250 |
|
| } |
| 251 |
|
| |
| 252 |
0 |
| void GB_clear_joyp_accessed(GB_gameboy_t *gb) |
| 253 |
|
| { |
| 254 |
0 |
| gb->joyp_accessed = false; |
| 255 |
0 |
| } |
| 256 |
|
| |
| 257 |
0 |
| void GB_set_allow_illegal_inputs(GB_gameboy_t *gb, bool allow) |
| 258 |
|
| { |
| 259 |
0 |
| gb->illegal_inputs_allowed = allow; |
| 260 |
0 |
| } |
| 261 |
|
| |
| 262 |
0 |
| void GB_set_emulate_joypad_bouncing(GB_gameboy_t *gb, bool emulate) |
| 263 |
|
| { |
| 264 |
0 |
| gb->no_bouncing_emulation = !emulate; |
| 265 |
0 |
| } |
| 266 |
|
| |
| 267 |
0 |
| void GB_set_update_input_hint_callback(GB_gameboy_t *gb, GB_update_input_hint_callback_t callback) |
| 268 |
|
| { |
| 269 |
|
| if (!callback) { |
| 270 |
|
| GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb) |
| 271 |
|
| } |
| 272 |
0 |
| gb->update_input_hint_callback = callback; |
| 273 |
0 |
| } |
| 274 |
|
| |
| 275 |
0 |
| void GB_set_use_faux_analog_inputs(GB_gameboy_t *gb, unsigned player, bool use) |
| 276 |
|
| { |
| 277 |
0 |
▶ | if (gb->use_faux_analog[player] == use) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->use_faux_analog[player] == use) return; |
0 |
0 |
0 |
|
| 278 |
0 |
| gb->use_faux_analog[player] = use; |
| 279 |
0 |
▶ | for (GB_key_t key = GB_KEY_RIGHT; key <= GB_KEY_DOWN; key++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (GB_key_t key = GB_KEY_RIGHT; key <= GB_KEY_DOWN; key++) { |
0 |
0 |
0 |
|
| 280 |
0 |
| gb->keys[player][key] = false; |
| 281 |
|
| } |
| 282 |
0 |
| GB_update_joyp(gb); |
| 283 |
|
| } |
| 284 |
|
| |
| 285 |
0 |
| void GB_set_faux_analog_inputs(GB_gameboy_t *gb, unsigned player, double x, double y) |
| 286 |
|
| { |
| 287 |
|
| |
| 288 |
0 |
▶ | if (x > 1) x = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (x > 1) x = 1; |
0 |
0 |
0 |
|
| 289 |
0 |
▶ | else if (x < -1) x = -1; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (x < -1) x = -1; |
0 |
0 |
0 |
|
| 290 |
0 |
▶ | if (y > 1) y = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (y > 1) y = 1; |
0 |
0 |
0 |
|
| 291 |
0 |
▶ | else if (y < -1) y = -1; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (y < -1) y = -1; |
0 |
0 |
0 |
|
| 292 |
|
| |
| 293 |
0 |
| double abs_x = fabs(x), abs_y = fabs(y); |
| 294 |
0 |
▶ | if (abs_x <= 0.1) x = abs_x = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (abs_x <= 0.1) x = abs_x = 0; |
0 |
0 |
0 |
|
| 295 |
0 |
▶ | if (abs_y <= 0.1) y = abs_y = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (abs_y <= 0.1) y = abs_y = 0; |
0 |
0 |
0 |
|
| 296 |
0 |
▶ | if (!x && !y) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!x && !y) { |
0 |
0 |
0 |
| if (!x && !y) { |
0 |
0 |
0 |
|
| 297 |
0 |
| gb->faux_analog_inputs[player].x = gb->faux_analog_inputs[player].y = 0; |
| 298 |
|
| } |
| 299 |
|
| else { |
| 300 |
0 |
▶ | if (x) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (x) { |
0 |
0 |
0 |
|
| 301 |
0 |
| abs_x = (abs_x - 0.1) / 0.9; |
| 302 |
0 |
▶ | x = x > 0? abs_x : -abs_x; |
Branch: 1 branch
| Branch | True | False | Total |
| x = x > 0? abs_x : -abs_x; |
0 |
0 |
0 |
|
| 303 |
|
| } |
| 304 |
0 |
▶ | if (y) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (y) { |
0 |
0 |
0 |
|
| 305 |
0 |
| abs_y = (abs_y - 0.1) / 0.9; |
| 306 |
0 |
▶ | y = y > 0? abs_y : -abs_y; |
Branch: 1 branch
| Branch | True | False | Total |
| y = y > 0? abs_y : -abs_y; |
0 |
0 |
0 |
|
| 307 |
|
| } |
| 308 |
|
| // Square the circle |
| 309 |
0 |
▶ | double distance = MIN(sqrt(x * x + y * y), 1); |
Branch: 1 branch
| Branch | True | False | Total |
| double distance = MIN(sqrt(x * x + y * y), 1); |
0 |
0 |
0 |
|
| 310 |
0 |
▶ | double multiplier = 8 * distance / MAX(abs_x, abs_y); |
Branch: 1 branch
| Branch | True | False | Total |
| double multiplier = 8 * distance / MAX(abs_x, abs_y); |
0 |
0 |
0 |
|
| 311 |
|
| |
| 312 |
0 |
| gb->faux_analog_inputs[player].x = round(x * multiplier); |
| 313 |
0 |
| gb->faux_analog_inputs[player].y = round(y * multiplier); |
| 314 |
|
| } |
| 315 |
0 |
| GB_update_joyp(gb); |
| 316 |
0 |
| } |
| 317 |
|
| |
| 318 |
291 |
| void GB_update_faux_analog(GB_gameboy_t *gb) |
| 319 |
|
| { |
| 320 |
291 |
| gb->faux_analog_ticks++; |
| 321 |
1455 |
▶ | for (unsigned i = 0; i < 4; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned i = 0; i < 4; i++) { |
1164 |
291 |
1455 |
|
| 322 |
1164 |
▶ | if (!gb->use_faux_analog[i]) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!gb->use_faux_analog[i]) continue; |
1164 |
0 |
1164 |
|
| 323 |
0 |
▶ | if ((gb->faux_analog_inputs[i].x != 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| if ((gb->faux_analog_inputs[i].x != 0 && |
0 |
0 |
0 |
|
| 324 |
0 |
▶ | gb->faux_analog_inputs[i].x != 8 && |
Branch: 1 branch
| Branch | True | False | Total |
| gb->faux_analog_inputs[i].x != 8 && |
0 |
0 |
0 |
|
| 325 |
0 |
▶ | gb->faux_analog_inputs[i].x != -8) || |
Branch: 1 branch
| Branch | True | False | Total |
| gb->faux_analog_inputs[i].x != -8) || |
0 |
0 |
0 |
|
| 326 |
0 |
▶ | (gb->faux_analog_inputs[i].y != 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| (gb->faux_analog_inputs[i].y != 0 && |
0 |
0 |
0 |
|
| 327 |
0 |
▶ | gb->faux_analog_inputs[i].y != 8 && |
Branch: 1 branch
| Branch | True | False | Total |
| gb->faux_analog_inputs[i].y != 8 && |
0 |
0 |
0 |
|
| 328 |
0 |
▶ | gb->faux_analog_inputs[i].y != -8)) { |
Branch: 1 branch
| Branch | True | False | Total |
| gb->faux_analog_inputs[i].y != -8)) { |
0 |
0 |
0 |
|
| 329 |
0 |
| gb->joypad_is_stable = false; |
| 330 |
0 |
| return; |
| 331 |
|
| } |
| 332 |
|
| } |
| 333 |
|
| } |