SameBoy/Core/joypad.c

225 lines
6.8 KiB
C
Raw Normal View History

2016-03-30 20:07:55 +00:00
#include "gb.h"
#include <assert.h>
2016-03-30 20:07:55 +00:00
2022-07-23 18:46:54 +00:00
static inline bool should_bounce(GB_gameboy_t *gb)
{
// Bouncing is super rare on an AGS, so don't emulate it on GB_MODEL_AGB_B (when addeed)
return !GB_is_sgb(gb) && !gb-> no_bouncing_emulation && !(gb->model & GB_MODEL_GBP_BIT) /*&& gb->model != GB_MODEL_AGB_B*/;
2022-07-23 18:46:54 +00:00
}
static inline uint16_t bounce_for_key(GB_gameboy_t *gb, GB_key_t key)
{
if (gb->model > GB_MODEL_CGB_E) {
// AGB are less bouncy
2022-07-23 23:35:46 +00:00
return 0xBFF;
2022-07-23 18:46:54 +00:00
}
if (key == GB_KEY_START || key == GB_KEY_SELECT) {
2022-07-23 23:35:46 +00:00
return 0x1FFF;
2022-07-23 18:46:54 +00:00
}
2022-07-23 23:35:46 +00:00
return 0xFFF;
2022-07-23 18:46:54 +00:00
}
static inline bool get_input(GB_gameboy_t *gb, uint8_t player, GB_key_t key)
{
if (player != 0) {
return gb->keys[player][key];
}
bool ret = gb->keys[player][key];
if (likely(gb->key_bounce_timing[key] == 0)) return ret;
2022-07-23 23:35:46 +00:00
if (likely((gb->key_bounce_timing[key] & 0x3FF) > 0x300)) return ret;
2022-07-23 18:46:54 +00:00
uint16_t semi_random = ((((key << 5) + gb->div_counter) * 17) ^ ((gb->apu.apu_cycles + gb->display_cycles) * 13));
semi_random >>= 3;
if (semi_random < gb->key_bounce_timing[key]) {
ret ^= true;
}
return ret;
}
void GB_update_joyp(GB_gameboy_t *gb)
2016-03-30 20:07:55 +00:00
{
2019-07-19 17:27:53 +00:00
if (gb->model & GB_MODEL_NO_SFC_BIT) return;
2019-07-16 18:42:57 +00:00
uint8_t key_selection = 0;
uint8_t previous_state = 0;
2016-03-30 20:07:55 +00:00
/* Todo: add delay to key selection */
previous_state = gb->io_registers[GB_IO_JOYP] & 0xF;
key_selection = (gb->io_registers[GB_IO_JOYP] >> 4) & 3;
gb->io_registers[GB_IO_JOYP] &= 0xF0;
uint8_t current_player = gb->sgb? gb->sgb->current_player : 0;
2016-03-30 20:07:55 +00:00
switch (key_selection) {
case 3:
2018-11-15 23:53:01 +00:00
if (gb->sgb && gb->sgb->player_count > 1) {
gb->io_registers[GB_IO_JOYP] |= 0xF - current_player;
2018-11-11 22:37:06 +00:00
}
else {
/* Nothing is wired, all up */
gb->io_registers[GB_IO_JOYP] |= 0x0F;
}
2016-03-30 20:07:55 +00:00
break;
case 2:
/* Direction keys */
for (uint8_t i = 0; i < 4; i++) {
2022-07-23 18:46:54 +00:00
gb->io_registers[GB_IO_JOYP] |= (!get_input(gb, current_player, i)) << i;
2016-03-30 20:07:55 +00:00
}
/* Forbid pressing two opposing keys, this breaks a lot of games; even if it's somewhat possible. */
2021-12-18 22:28:24 +00:00
if (likely(!gb->illegal_inputs_allowed)) {
if (!(gb->io_registers[GB_IO_JOYP] & 1)) {
gb->io_registers[GB_IO_JOYP] |= 2;
}
if (!(gb->io_registers[GB_IO_JOYP] & 4)) {
gb->io_registers[GB_IO_JOYP] |= 8;
}
}
2016-03-30 20:07:55 +00:00
break;
case 1:
/* Other keys */
for (uint8_t i = 0; i < 4; i++) {
2022-07-23 18:46:54 +00:00
gb->io_registers[GB_IO_JOYP] |= (!get_input(gb, current_player, i + 4)) << i;
2016-03-30 20:07:55 +00:00
}
break;
case 0:
for (uint8_t i = 0; i < 4; i++) {
2022-07-23 18:46:54 +00:00
gb->io_registers[GB_IO_JOYP] |= (!(get_input(gb, current_player, i) || get_input(gb, current_player, i + 4))) << i;
2016-03-30 20:07:55 +00:00
}
break;
2022-01-03 15:17:35 +00:00
nodefault;
2016-03-30 20:07:55 +00:00
}
2022-07-23 18:46:54 +00:00
// TODO: Implement the lame anti-debouncing mechanism as seen on the DMG schematics
if (previous_state & ~(gb->io_registers[GB_IO_JOYP] & 0xF)) {
2022-01-09 14:43:32 +00:00
if (!(gb->io_registers[GB_IO_IF] & 0x10)) {
gb->joyp_accessed = true;
gb->io_registers[GB_IO_IF] |= 0x10;
}
2016-03-30 20:07:55 +00:00
}
gb->io_registers[GB_IO_JOYP] |= 0xC0;
}
2019-07-16 18:42:57 +00:00
void GB_icd_set_joyp(GB_gameboy_t *gb, uint8_t value)
{
uint8_t previous_state = gb->io_registers[GB_IO_JOYP] & 0xF;
gb->io_registers[GB_IO_JOYP] &= 0xF0;
gb->io_registers[GB_IO_JOYP] |= value & 0xF;
if (previous_state & ~(gb->io_registers[GB_IO_JOYP] & 0xF)) {
2022-01-09 14:43:32 +00:00
if (!(gb->io_registers[GB_IO_IF] & 0x10)) {
gb->joyp_accessed = true;
gb->io_registers[GB_IO_IF] |= 0x10;
}
2019-07-16 18:42:57 +00:00
}
gb->io_registers[GB_IO_JOYP] |= 0xC0;
2019-07-16 18:42:57 +00:00
}
void GB_set_key_state(GB_gameboy_t *gb, GB_key_t index, bool pressed)
{
assert(index >= 0 && index < GB_KEY_MAX);
2022-07-23 18:46:54 +00:00
if (should_bounce(gb) && pressed != gb->keys[0][index]) {
2022-07-24 11:49:58 +00:00
gb->joypad_is_stable = false;
2022-07-23 18:46:54 +00:00
gb->key_bounce_timing[index] = bounce_for_key(gb, index);
}
2018-11-11 22:37:06 +00:00
gb->keys[0][index] = pressed;
GB_update_joyp(gb);
2018-11-11 22:37:06 +00:00
}
void GB_set_key_state_for_player(GB_gameboy_t *gb, GB_key_t index, unsigned player, bool pressed)
{
assert(index >= 0 && index < GB_KEY_MAX);
assert(player < 4);
2022-07-23 18:46:54 +00:00
if (should_bounce(gb) && pressed != gb->keys[player][index]) {
2022-07-24 10:16:19 +00:00
gb->joypad_is_stable = false;
2022-07-23 18:46:54 +00:00
gb->key_bounce_timing[index] = bounce_for_key(gb, index);
}
2018-11-11 22:37:06 +00:00
gb->keys[player][index] = pressed;
GB_update_joyp(gb);
}
2021-12-02 09:23:44 +00:00
void GB_set_key_mask(GB_gameboy_t *gb, GB_key_mask_t mask)
{
2022-07-23 18:46:54 +00:00
for (unsigned i = 0; i < GB_KEY_MAX; i++) {
bool pressed = mask & (1 << i);
if (should_bounce(gb) && pressed != gb->keys[0][i]) {
2022-07-24 10:16:19 +00:00
gb->joypad_is_stable = false;
2022-07-23 18:46:54 +00:00
gb->key_bounce_timing[i] = bounce_for_key(gb, i);
2021-12-02 09:23:44 +00:00
}
2022-07-23 18:46:54 +00:00
gb->keys[0][i] = pressed;
2021-12-02 09:23:44 +00:00
}
GB_update_joyp(gb);
}
void GB_set_key_mask_for_player(GB_gameboy_t *gb, GB_key_mask_t mask, unsigned player)
{
2022-07-23 18:46:54 +00:00
for (unsigned i = 0; i < GB_KEY_MAX; i++) {
bool pressed = mask & (1 << i);
if (should_bounce(gb) && pressed != gb->keys[player][i]) {
2022-07-24 10:16:19 +00:00
gb->joypad_is_stable = false;
2022-07-23 18:46:54 +00:00
gb->key_bounce_timing[i] = bounce_for_key(gb, i);
2021-12-02 09:23:44 +00:00
}
2022-07-23 18:46:54 +00:00
gb->keys[player][i] = pressed;
2021-12-02 09:23:44 +00:00
}
GB_update_joyp(gb);
}
2021-12-17 23:25:06 +00:00
2022-07-23 18:46:54 +00:00
void GB_joypad_run(GB_gameboy_t *gb, unsigned cycles)
{
2022-07-24 10:16:19 +00:00
if (gb->joypad_is_stable) return;
2022-07-23 18:46:54 +00:00
bool should_update_joyp = false;
2022-07-24 10:16:19 +00:00
gb->joypad_is_stable = true;
2022-07-23 18:46:54 +00:00
if (gb->joyp_switching_delay) {
2022-07-24 10:16:19 +00:00
gb->joypad_is_stable = false;
2022-07-23 18:46:54 +00:00
if (gb->joyp_switching_delay > cycles) {
gb->joyp_switching_delay -= cycles;
}
else {
gb->joyp_switching_delay = 0;
gb->io_registers[GB_IO_JOYP] = (gb->joyp_switch_value & 0xF0) | (gb->io_registers[GB_IO_JOYP] & 0x0F);
should_update_joyp = true;
}
}
for (unsigned i = 0; i < GB_KEY_MAX; i++) {
if (gb->key_bounce_timing[i]) {
2022-07-24 10:16:19 +00:00
gb->joypad_is_stable = false;
2022-07-23 18:46:54 +00:00
should_update_joyp = true;
if (gb->key_bounce_timing[i] > cycles) {
gb->key_bounce_timing[i] -= cycles;
}
else {
gb->key_bounce_timing[i] = 0;
}
}
}
if (should_update_joyp) {
GB_update_joyp(gb);
}
}
2021-12-17 23:25:06 +00:00
bool GB_get_joyp_accessed(GB_gameboy_t *gb)
{
return gb->joyp_accessed;
}
void GB_clear_joyp_accessed(GB_gameboy_t *gb)
{
gb->joyp_accessed = false;
}
2021-12-18 22:28:24 +00:00
void GB_set_allow_illegal_inputs(GB_gameboy_t *gb, bool allow)
{
gb->illegal_inputs_allowed = allow;
}
2022-07-23 18:46:54 +00:00
2022-07-24 10:56:33 +00:00
void GB_set_emulate_joypad_bouncing(GB_gameboy_t *gb, bool emulate)
2022-07-23 18:46:54 +00:00
{
gb->no_bouncing_emulation = !emulate;
}