-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjoypad.c
More file actions
68 lines (54 loc) · 1.22 KB
/
Copy pathjoypad.c
File metadata and controls
68 lines (54 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "joypad.h"
// TODO: interrupts!
enum {
INPUT_RIGHT_OR_A = 0x1,
INPUT_LEFT_OR_B = 0x2,
INPUT_UP_OR_SELECT = 0x4,
INPUT_DOWN_OR_START = 0x8,
INPUT_SELECT_DIRECTION_KEYS = 0x10,
INPUT_SELECT_BUTTON_KEYS = 0x20
};
static uint8_t reg = 0xFF;
static int state[8] = {0};
void joypad_key_up (int key) {
state[key] = 0;
}
void joypad_key_down (int key) {
state[key] = 1;
}
// TODO CHECK THIS LOGIC, COULD BE BROKEN DUE TO ISSUE WITH SOME CPU COMMAND
uint8_t joypad_read_reg (void) {
if (reg & INPUT_SELECT_DIRECTION_KEYS) {
if (state[JOYPAD_UP]) {
reg &= ~INPUT_UP_OR_SELECT;
}
if (state[JOYPAD_DOWN]) {
reg &= ~INPUT_DOWN_OR_START;
}
if (state[JOYPAD_LEFT]) {
reg &= ~INPUT_LEFT_OR_B;
}
if (state[JOYPAD_RIGHT]) {
reg &= ~INPUT_RIGHT_OR_A;
}
}
else if (reg & INPUT_SELECT_BUTTON_KEYS) {
if (state[JOYPAD_BUTTON_A]) {
reg &= ~INPUT_RIGHT_OR_A;
}
if (state[JOYPAD_BUTTON_B]) {
reg &= ~INPUT_LEFT_OR_B;
}
if (state[JOYPAD_BUTTON_SELECT]) {
reg &= ~INPUT_UP_OR_SELECT;
}
if (state[JOYPAD_BUTTON_START]) {
reg &= ~INPUT_DOWN_OR_START;
}
}
return reg;
}
void joypad_write_reg (uint8_t val) {
reg = 0xff;
reg &= ~val;
}