| 1 | #include "gui.hpp" |
| 2 | |
| 3 | namespace Joypad { |
| 4 | |
| 5 | |
| 6 | u8 joypad_bits[2]; // Joypad shift registers. |
| 7 | bool strobe; // Joypad strobe latch. |
| 8 | |
| 9 | /* Read joypad state (NES register format) */ |
| 10 | u8 read_state(int n) |
| 11 | { |
| 12 | // When strobe is high, it keeps reading A: |
| 13 | if (strobe) |
| 14 | return 0x40 | (GUI::get_joypad_state(n) & 1); |
| 15 | |
| 16 | // Get the status of a button and shift the register: |
| 17 | u8 j = 0x40 | (joypad_bits[n] & 1); |
| 18 | joypad_bits[n] = 0x80 | (joypad_bits[n] >> 1); |
| 19 | return j; |
| 20 | } |
| 21 | |
| 22 | void write_strobe(bool v) |
| 23 | { |
| 24 | // Read the joypad data on strobe's transition 1 -> 0. |
| 25 | if (strobe and !v) |
| 26 | for (int i = 0; i < 2; i++) |
| 27 | joypad_bits[i] = GUI::get_joypad_state(i); |
| 28 | |
| 29 | strobe = v; |
| 30 | } |
| 31 | |
| 32 | |
| 33 | } |
| 34 | |