Optimization to joypad code

This commit is contained in:
Lior Halphon 2022-07-24 13:16:19 +03:00
parent 0f31c7a1ba
commit 4b03cc05c1
3 changed files with 9 additions and 0 deletions

View File

@ -689,6 +689,7 @@ struct GB_gameboy_internal_s {
bool joyp_accessed;
bool illegal_inputs_allowed;
bool no_bouncing_emulation;
bool joypad_is_stable;
/* Timing */
uint64_t last_sync;

View File

@ -132,6 +132,7 @@ void GB_set_key_state_for_player(GB_gameboy_t *gb, GB_key_t index, unsigned play
assert(index >= 0 && index < GB_KEY_MAX);
assert(player < 4);
if (should_bounce(gb) && pressed != gb->keys[player][index]) {
gb->joypad_is_stable = false;
gb->key_bounce_timing[index] = bounce_for_key(gb, index);
}
gb->keys[player][index] = pressed;
@ -143,6 +144,7 @@ void GB_set_key_mask(GB_gameboy_t *gb, GB_key_mask_t mask)
for (unsigned i = 0; i < GB_KEY_MAX; i++) {
bool pressed = mask & (1 << i);
if (should_bounce(gb) && pressed != gb->keys[0][i]) {
gb->joypad_is_stable = false;
gb->key_bounce_timing[i] = bounce_for_key(gb, i);
}
gb->keys[0][i] = pressed;
@ -156,6 +158,7 @@ void GB_set_key_mask_for_player(GB_gameboy_t *gb, GB_key_mask_t mask, unsigned p
for (unsigned i = 0; i < GB_KEY_MAX; i++) {
bool pressed = mask & (1 << i);
if (should_bounce(gb) && pressed != gb->keys[player][i]) {
gb->joypad_is_stable = false;
gb->key_bounce_timing[i] = bounce_for_key(gb, i);
}
gb->keys[player][i] = pressed;
@ -166,8 +169,11 @@ void GB_set_key_mask_for_player(GB_gameboy_t *gb, GB_key_mask_t mask, unsigned p
void GB_joypad_run(GB_gameboy_t *gb, unsigned cycles)
{
if (gb->joypad_is_stable) return;
bool should_update_joyp = false;
gb->joypad_is_stable = true;
if (gb->joyp_switching_delay) {
gb->joypad_is_stable = false;
if (gb->joyp_switching_delay > cycles) {
gb->joyp_switching_delay -= cycles;
}
@ -180,6 +186,7 @@ void GB_joypad_run(GB_gameboy_t *gb, unsigned cycles)
for (unsigned i = 0; i < GB_KEY_MAX; i++) {
if (gb->key_bounce_timing[i]) {
gb->joypad_is_stable = false;
should_update_joyp = true;
if (gb->key_bounce_timing[i] > cycles) {
gb->key_bounce_timing[i] -= cycles;

View File

@ -1508,6 +1508,7 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value)
gb->joyp_switch_value = value;
gb->joyp_switching_delay = 24;
value &= gb->io_registers[GB_IO_JOYP];
gb->joypad_is_stable = false;
}
GB_sgb_write(gb, value);
gb->io_registers[GB_IO_JOYP] = (value & 0xF0) | (gb->io_registers[GB_IO_JOYP] & 0x0F);