From 479bee3707070e46ca4ff1688c57802fa34fa4f0 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Sat, 29 Sep 2018 12:55:31 -0700 Subject: [PATCH] GB, GBA: Fix broken opposing button filter (fixes #1191) --- CHANGES | 1 + include/mgba/internal/gb/gb.h | 2 ++ src/gb/core.c | 6 +++++- src/gb/io.c | 19 ++++++++++++++++++- src/gba/core.c | 5 +++++ 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 5243ebc50..95361c9ef 100644 --- a/CHANGES +++ b/CHANGES @@ -109,6 +109,7 @@ Bugfixes: - GBA Video: Fix caching with background toggling (fixes mgba.io/i/1118) - Wii: Fix drawing caching regression (fixes mgba.io/i/1185) - Switch: Fix incorrect mapping for fast forward cap + - GB, GBA: Fix broken opposing button filter (fixes mgba.io/i/1191) Misc: - mGUI: Add SGB border configuration option - mGUI: Add support for different settings types diff --git a/include/mgba/internal/gb/gb.h b/include/mgba/internal/gb/gb.h index 47c3a03df..ac9d6230d 100644 --- a/include/mgba/internal/gb/gb.h +++ b/include/mgba/internal/gb/gb.h @@ -117,6 +117,8 @@ struct GB { bool earlyExit; struct mTimingEvent eiPending; unsigned doubleSpeed; + + bool allowOpposingDirections; }; struct GBCartridge { diff --git a/src/gb/core.c b/src/gb/core.c index f1c2ce120..a5b29d6a9 100644 --- a/src/gb/core.c +++ b/src/gb/core.c @@ -210,8 +210,12 @@ static void _GBCoreLoadConfig(struct mCore* core, const struct mCoreConfig* conf mCoreConfigCopyValue(&core->config, config, "gb.model"); mCoreConfigCopyValue(&core->config, config, "sgb.model"); mCoreConfigCopyValue(&core->config, config, "cgb.model"); + mCoreConfigCopyValue(&core->config, config, "allowOpposingDirections"); + + int fakeBool = 0; + mCoreConfigGetIntValue(config, "allowOpposingDirections", &fakeBool); + gb->allowOpposingDirections = fakeBool; - int fakeBool; if (mCoreConfigGetIntValue(config, "sgb.borders", &fakeBool)) { gb->video.sgbBorders = fakeBool; gb->video.renderer->enableSGBBorder(gb->video.renderer, fakeBool); diff --git a/src/gb/io.c b/src/gb/io.c index d24576333..492553780 100644 --- a/src/gb/io.c +++ b/src/gb/io.c @@ -105,6 +105,7 @@ static const uint8_t _registerMask[] = { }; static uint8_t _readKeys(struct GB* gb); +static uint8_t _readKeysFiltered(struct GB* gb); static void _writeSGBBits(struct GB* gb, int bits) { if (!bits) { @@ -557,10 +558,26 @@ static uint8_t _readKeys(struct GB* gb) { return gb->memory.io[REG_JOYP]; } +static uint8_t _readKeysFiltered(struct GB* gb) { + uint8_t keys = _readKeys(gb); + if (!gb->allowOpposingDirections && (keys & 0x30) == 0x20) { + unsigned rl = keys & 0x03; + unsigned ud = keys & 0x0C; + keys &= 0xF0; + if (rl != 0x03) { + keys |= rl; + } + if (ud != 0x0C) { + keys |= ud; + } + } + return keys; +} + uint8_t GBIORead(struct GB* gb, unsigned address) { switch (address) { case REG_JOYP: - return _readKeys(gb); + return _readKeysFiltered(gb); case REG_IE: return gb->memory.ie; case REG_WAVE_0: diff --git a/src/gba/core.c b/src/gba/core.c index 9513ab899..05225bc56 100644 --- a/src/gba/core.c +++ b/src/gba/core.c @@ -239,6 +239,11 @@ static void _GBACoreLoadConfig(struct mCore* core, const struct mCoreConfig* con } } + int fakeBool = 0; + mCoreConfigGetIntValue(config, "allowOpposingDirections", &fakeBool); + gba->allowOpposingDirections = fakeBool; + + mCoreConfigCopyValue(&core->config, config, "allowOpposingDirections"); mCoreConfigCopyValue(&core->config, config, "gba.bios"); #ifndef DISABLE_THREADING