Skip to content

Commit

Permalink
GB I/O: Fix JOYP IRQs
Browse files Browse the repository at this point in the history
  • Loading branch information
endrift committed Jun 23, 2018
1 parent b67b192 commit f1a9ebe
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/gb/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ static const uint8_t _registerMask[] = {
[REG_IE] = 0xE0,
};

static uint8_t _readKeys(struct GB* gb);

static void _writeSGBBits(struct GB* gb, int bits) {
if (!bits) {
gb->sgbBit = -1;
Expand Down Expand Up @@ -394,10 +396,12 @@ void GBIOWrite(struct GB* gb, unsigned address, uint8_t value) {
}
break;
case REG_JOYP:
gb->memory.io[REG_JOYP] = value | 0x0F;
_readKeys(gb);
if (gb->model == GB_MODEL_SGB) {
_writeSGBBits(gb, (value >> 4) & 3);
}
break;
return;
case REG_TIMA:
if (value && mTimingUntil(&gb->timing, &gb->timer.irq) > 1) {
mTimingDeschedule(&gb->timing, &gb->timer.irq);
Expand Down Expand Up @@ -522,7 +526,8 @@ static uint8_t _readKeys(struct GB* gb) {
if (gb->sgbCurrentController != 0) {
keys = 0;
}
switch (gb->memory.io[REG_JOYP] & 0x30) {
uint8_t joyp = gb->memory.io[REG_JOYP];
switch (joyp & 0x30) {
case 0x30:
keys = gb->sgbCurrentController;
break;
Expand All @@ -535,7 +540,12 @@ static uint8_t _readKeys(struct GB* gb) {
keys |= keys >> 4;
break;
}
return (0xC0 | (gb->memory.io[REG_JOYP] | 0xF)) ^ (keys & 0xF);
gb->memory.io[REG_JOYP] = (0xCF | joyp) ^ (keys & 0xF);
if (joyp & ~gb->memory.io[REG_JOYP] & 0xF) {
gb->memory.io[REG_IF] |= (1 << GB_IRQ_KEYPAD);
GBUpdateIRQs(gb);
}
return gb->memory.io[REG_JOYP];
}

uint8_t GBIORead(struct GB* gb, unsigned address) {
Expand Down Expand Up @@ -639,10 +649,7 @@ uint8_t GBIORead(struct GB* gb, unsigned address) {
}

void GBTestKeypadIRQ(struct GB* gb) {
if (_readKeys(gb)) {
gb->memory.io[REG_IF] |= (1 << GB_IRQ_KEYPAD);
GBUpdateIRQs(gb);
}
_readKeys(gb);
}

struct GBSerializedState;
Expand Down

0 comments on commit f1a9ebe

Please sign in to comment.