Skip to content

Commit 613f5be

Browse files
Refactor GB joypad code
- Simplify GB joypad handling register logic - Add missing edge case when both bits 4 and 5 in P1/JOYP register is low. The result returns AND of direction and action face buttons (see Sameboy and other emus)
1 parent 7302105 commit 613f5be

File tree

1 file changed

+48
-70
lines changed

1 file changed

+48
-70
lines changed

src/core/gb/gb.cpp

Lines changed: 48 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,82 +2207,60 @@ uint8_t gbReadMemory(uint16_t address)
22072207

22082208
int b = gbMemory[0xff00];
22092209

2210-
if ((b & 0x30) == 0x20) {
2211-
b &= 0xf0;
2212-
2213-
int joy = 0;
2214-
if (gbSgbMode && gbSgbMultiplayer) {
2215-
switch (gbSgbNextController) {
2216-
case 0x0f:
2217-
joy = 0;
2218-
break;
2219-
case 0x0e:
2220-
joy = 1;
2221-
break;
2222-
case 0x0d:
2223-
joy = 2;
2224-
break;
2225-
case 0x0c:
2226-
joy = 3;
2227-
break;
2228-
default:
2229-
joy = 0;
2230-
}
2231-
}
2232-
int joystate = gbJoymask[joy];
2233-
if (!(joystate & 128))
2234-
b |= 0x08;
2235-
if (!(joystate & 64))
2236-
b |= 0x04;
2237-
if (!(joystate & 32))
2238-
b |= 0x02;
2239-
if (!(joystate & 16))
2240-
b |= 0x01;
2241-
2242-
gbMemory[0xff00] = (uint8_t)b;
2243-
} else if ((b & 0x30) == 0x10) {
2244-
b &= 0xf0;
2245-
2246-
int joy = 0;
2247-
if (gbSgbMode && gbSgbMultiplayer) {
2248-
switch (gbSgbNextController) {
2249-
case 0x0f:
2250-
joy = 0;
2251-
break;
2252-
case 0x0e:
2253-
joy = 1;
2254-
break;
2255-
case 0x0d:
2256-
joy = 2;
2257-
break;
2258-
case 0x0c:
2259-
joy = 3;
2260-
break;
2261-
default:
2262-
joy = 0;
2263-
}
2210+
// Choose joystick index for SGB multiplayer if enabled, otherwise 0
2211+
int joy = 0;
2212+
2213+
if (gbSgbMode && gbSgbMultiplayer) {
2214+
switch (gbSgbNextController) {
2215+
case 0x0f: joy = 0; break;
2216+
case 0x0e: joy = 1; break;
2217+
case 0x0d: joy = 2; break;
2218+
case 0x0c: joy = 3; break;
2219+
default: joy = 0; break;
22642220
}
2265-
int joystate = gbJoymask[joy];
2266-
if (!(joystate & 8))
2267-
b |= 0x08;
2268-
if (!(joystate & 4))
2269-
b |= 0x04;
2270-
if (!(joystate & 2))
2271-
b |= 0x02;
2272-
if (!(joystate & 1))
2273-
b |= 0x01;
2274-
2275-
gbMemory[0xff00] = (uint8_t)b;
2276-
} else {
2221+
}
2222+
2223+
int joystate = gbJoymask[joy];
2224+
2225+
if (gbSgbMode && gbSgbMultiplayer) {
2226+
}
2227+
2228+
// Bit 7 - Not used
2229+
// Bit 6 - Not used
2230+
// Bit 5 - P15 Select Action buttons (0=Select)
2231+
// Bit 4 - P14 Select Direction buttons (0=Select)
2232+
// Bit 3 - P13 Input: Down or Start (0=Pressed) (Read Only)
2233+
// Bit 2 - P12 Input: Up or Select (0=Pressed) (Read Only)
2234+
// Bit 1 - P11 Input: Left or B (0=Pressed) (Read Only)
2235+
// Bit 0 - P10 Input: Right or A (0=Pressed) (Read Only)
2236+
uint8_t data_dir =
2237+
(!(joystate & 0x80) ? 0x08 : 0x00) | // Down -> bit3
2238+
(!(joystate & 0x40) ? 0x04 : 0x00) | // Up -> bit2
2239+
(!(joystate & 0x20) ? 0x02 : 0x00) | // Left -> bit1
2240+
(!(joystate & 0x10) ? 0x01 : 0x00); // Right-> bit0
2241+
2242+
uint8_t data_action =
2243+
(!(joystate & 0x08) ? 0x08 : 0x00) | // Start -> bit3
2244+
(!(joystate & 0x04) ? 0x04 : 0x00) | // Select -> bit2
2245+
(!(joystate & 0x02) ? 0x02 : 0x00) | // B -> bit1
2246+
(!(joystate & 0x01) ? 0x01 : 0x00); // A -> bit0
2247+
2248+
b &= 0xf0; // keep selector bits in upper nibble
2249+
switch (b & 0x30) {
2250+
case 0x00: b |= data_dir & data_action; break;
2251+
case 0x10: b |= data_action; break;
2252+
case 0x20: b |= data_dir; break;
2253+
case 0x30:
22772254
if (gbSgbMode && gbSgbMultiplayer) {
2278-
gbMemory[0xff00] = 0xf0 | gbSgbNextController;
2255+
b = 0xf0 | gbSgbNextController;
22792256
} else {
2280-
gbMemory[0xff00] = 0xff;
2257+
b = 0xff;
22812258
}
2259+
break;
2260+
}
2261+
gbMemory[0xff00] = b;
22822262
}
2283-
}
22842263
return gbMemory[0xff00];
2285-
break;
22862264
case 0x01:
22872265
return gbMemory[0xff01];
22882266
case 0x02:

0 commit comments

Comments
 (0)