Here’s what’s going on in that small function, in plain English and then in cleaned-up C:
- It’s talking to the Game Boy’s P1 I/O port, which is how you read the joypad.
- Writing
0x10 to P1 “selects” the D-pad lines; reading P1 back gives you the up/down/left/right bits.
- Writing
0x20 selects the A/B/Start/Select lines; reading P1 back gives you those bits.
- It does the redundant reads (
bVar2 = P1; bVar2 = P1; …) as a crude debounce or delay.
- It then recombines the two nibbles into one byte, XORs it with the previous state, and masks to find “newly pressed” buttons.
- Finally, it stores:
DAT_c011 ← raw current state
DAT_c012 ← which buttons just went from “unpressed”→“pressed” this frame
De-obfuscated, it looks roughly like this:
// globals:
uint8_t prev_state; // DAT_c011
uint8_t just_pressed; // DAT_c012
void scan_joypad(void) {
uint8_t dpad, buttons, combined;
// Select D-pad (Up/Down/Left/Right)
P1 = 0x10;
// Read it a few times (debounce/delay)
dpad = P1;
dpad = P1;
// … repeat if you like
dpad = P1;
// Select face keys (A/B/Start/Select)
P1 = 0x20;
// Read it a few times
buttons = P1;
buttons = P1;
// … repeat
buttons = P1;
// Combine into one byte: high nibble = face keys, low nibble = D-pad
combined = ((buttons & 0x0F) << 4) // move A/B/Start/Select into high nibble
| ((buttons | 0xF0) >> 4); // just a trick to isolate those bits
// Compute which bits just transitioned 0→1
// (assuming 1 means “pressed” on this hardware model)
just_pressed = (prev_state ^ combined) & combined;
// Save for next scan
prev_state = combined;
}
What this gives you each time you call FUN_03bb() is:
prev_state = the full 8-bit joypad status (which buttons are down right now)
just_pressed = a mask of buttons that were just pressed this call
You’ll usually poll just_pressed looking for, say, the A-button bit to go high, and then feed that into whatever password-entry routine the ROM has.
Next steps:
• Hook or breakpoint on accesses to DAT_c012 in your emulator/RE tool to see exactly which button-bits they check.
• Log out the sequence of button-presses the game expects as the “password.”
Let me know if you’d like help with extracting that mask or simulating the button stream! 😊