vgbc/input/joypad.cpp

65 lines
1.3 KiB
C++
Raw Normal View History

#include <input/joypad.h>
Joypad::Joypad(Cpu& cpu)
: cpu(cpu), action_select(false),
direction_select(false),
keystates{false}
{}
void Joypad::setKeyState(Key k, bool pressed)
{
bool prevState = keystates[k];
keystates[k] = pressed;
if(!pressed || prevState == pressed)
return;
switch(k)
{
case A:
case B:
case Select:
case Start:
if (action_select)
cpu.signalInterrupt(INT_Joypad);
break;
case Right:
case Left:
case Up:
case Down:
if (direction_select)
cpu.signalInterrupt(INT_Joypad);
break;
}
}
void Joypad::write8(u16 /*addr*/, u8 data)
{
action_select = !(data & P1ActionSelect);
direction_select = !(data & P1DirectionSelect);
}
u8 Joypad::read8(u16 /*addr*/)
{
u8 val = 0x0F;
if(action_select)
val &=
(keystates[Start] ? 0 : P1Start) |
(keystates[Select] ? 0 : P1Select) |
(keystates[B] ? 0 : P1B) |
(keystates[A] ? 0 : P1A);
if(direction_select)
val &=
(keystates[Down] ? 0 : P1Down) |
(keystates[Up] ? 0 : P1Up) |
(keystates[Left] ? 0 : P1Left) |
(keystates[Right] ? 0 : P1Right);
val |= (action_select ? 0 : P1ActionSelect);
val |= (direction_select ? 0 : P1DirectionSelect);
return val;
}