#include "ameteor/keypad.hpp"
#include "globals.hpp"
#include "ameteor.hpp"
namespace AMeteor
{
Keypad::Keypad () :
m_keyinput(IO.GetRef16(Io::KEYINPUT)),
m_keycnt(IO.GetRef16(Io::KEYCNT))
{
}
void Keypad::KeyPressed(int code)
{
if (m_keys.count(code))
m_keyinput &= ~m_keys[code];
}
void Keypad::KeyReleased(int code)
{
if (m_keys.count(code))
m_keyinput |= m_keys[code];
}
void Keypad::JoyButtonPressed (uint16_t joyid, uint16_t button)
{
uint32_t id = ((int)joyid) << 16 | button;
if (m_joys.count(id))
m_keyinput &= ~m_joys[id];
}
void Keypad::JoyButtonReleased (uint16_t joyid, uint16_t button)
{
uint32_t id = ((int)joyid) << 16 | button;
if (m_joys.count(id))
m_keyinput |= m_joys[id];
}
void Keypad::JoyMoved (uint16_t joyid, uint16_t axis, float pos)
{
uint32_t id = (((int)joyid) << 16) | ((pos < 0) << 15) | (axis & 0x7FFF);
if (pos == 0)
{
if (m_axis.count(id))
m_keyinput |= m_axis[id];
if (m_axis.count(id | (1 << 15)))
m_keyinput |= m_axis[id | (1 << 15)];
}
else
{
if (m_axis.count(id))
m_keyinput &= ~((uint16_t)m_axis[id]);
if (m_axis.count(id ^ 0x8000))
m_keyinput |= m_axis[id ^ 0x8000];
}
}
void Keypad::VBlank ()
{
if (m_keycnt & (0x1 << 14))
if (m_keycnt & (0x1 << 15))
{
if ((~m_keyinput & m_keycnt & 0x3FF) == (m_keycnt & 0x3FF))
CPU.SendInterrupt(0x1000);
}
else
{
if (~m_keyinput & m_keycnt & 0x3FF)
CPU.SendInterrupt(0x1000);
}
}
}