2016-03-30 20:07:55 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "gb.h"
|
|
|
|
#include "joypad.h"
|
|
|
|
|
2016-06-18 17:29:11 +00:00
|
|
|
void GB_update_joyp(GB_gameboy_t *gb)
|
2016-03-30 20:07:55 +00:00
|
|
|
{
|
2016-06-18 17:29:11 +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;
|
|
|
|
switch (key_selection) {
|
|
|
|
case 3:
|
|
|
|
/* Nothing is wired, all up */
|
|
|
|
gb->io_registers[GB_IO_JOYP] |= 0x0F;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
/* Direction keys */
|
2016-06-18 17:29:11 +00:00
|
|
|
for (uint8_t i = 0; i < 4; i++) {
|
2016-03-30 20:07:55 +00:00
|
|
|
gb->io_registers[GB_IO_JOYP] |= (!gb->keys[i]) << i;
|
|
|
|
}
|
2016-09-20 23:15:02 +00:00
|
|
|
/* Forbid pressing two opposing keys, this breaks a lot of games; even if it's somewhat possible. */
|
|
|
|
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 */
|
2016-06-18 17:29:11 +00:00
|
|
|
for (uint8_t i = 0; i < 4; i++) {
|
2016-03-30 20:07:55 +00:00
|
|
|
gb->io_registers[GB_IO_JOYP] |= (!gb->keys[i + 4]) << i;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
/* Todo: verifiy this is correct */
|
2016-06-18 17:29:11 +00:00
|
|
|
for (uint8_t i = 0; i < 4; i++) {
|
2016-03-30 20:07:55 +00:00
|
|
|
gb->io_registers[GB_IO_JOYP] |= (!gb->keys[i]) << i;
|
|
|
|
gb->io_registers[GB_IO_JOYP] |= (!gb->keys[i + 4]) << i;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (previous_state != (gb->io_registers[GB_IO_JOYP] & 0xF)) {
|
|
|
|
/* Todo: disable when emulating CGB */
|
|
|
|
gb->io_registers[GB_IO_IF] |= 0x10;
|
|
|
|
}
|
|
|
|
gb->io_registers[GB_IO_JOYP] |= 0xC0; // No SGB support
|
|
|
|
}
|