This repository has been archived on 2022-06-22. You can view files and clone it, but cannot push or open issues or pull requests.
LaiNES/src/joypad.cpp
2014-05-26 16:03:36 +02:00

34 lines
711 B
C++

#include "gui.hpp"
namespace Joypad {
u8 joypad_bits[2]; // Joypad shift registers.
bool strobe; // Joypad strobe latch.
/* Read joypad state (NES register format) */
u8 read_state(int n)
{
// When strobe is high, it keeps reading A:
if (strobe)
return 0x40 | (GUI::get_joypad_state(n) & 1);
// Get the status of a button and shift the register:
u8 j = 0x40 | (joypad_bits[n] & 1);
joypad_bits[n] = 0x80 | (joypad_bits[n] >> 1);
return j;
}
void write_strobe(bool v)
{
// Read the joypad data on strobe's transition 1 -> 0.
if (strobe and !v)
for (int i = 0; i < 2; i++)
joypad_bits[i] = GUI::get_joypad_state(i);
strobe = v;
}
}