-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrollers.c
More file actions
89 lines (75 loc) · 1.94 KB
/
Copy pathcontrollers.c
File metadata and controls
89 lines (75 loc) · 1.94 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdint.h>
#include "SDL2/SDL.h"
#include "controllers.h"
void controllers_init(struct controllers *controllers)
{
*controllers = (struct controllers) {
.p1_status = 0,
.p2_status = 0,
.p1_latch = 0,
.p1_strobe = 0,
.p2_strobe = 0,
};
}
uint8_t controllers_read(struct controllers *controllers, uint16_t address)
{
if (address == 0) {
if (controllers->p1_strobe) {
controllers->p1_latch = controllers->p1_status;
}
uint8_t result = controllers->p1_latch & 0x01;
controllers->p1_latch = (controllers->p1_latch >> 1) | 0x80;
return result;
}
return 0;
}
void controllers_write(struct controllers *controllers, uint16_t address, uint8_t data)
{
if (address == 0) {
controllers->p1_strobe = data & 1;
} else {
controllers->p2_strobe = data & 1;
}
if (controllers->p1_strobe) {
controllers->p1_latch = controllers->p1_status;
}
}
int controllers_handle(struct controllers *controllers, SDL_Event *event)
{
if (event->type != SDL_KEYUP && event->type != SDL_KEYDOWN) {
return 0; // Not handled
}
uint8_t pressmask = 0;
switch (event->key.keysym.sym) {
case SDLK_a:
pressmask = 0x01;
break;
case SDLK_s:
pressmask = 0x02;
break;
case SDLK_h:
pressmask = 0x04;
break;
case SDLK_j:
pressmask = 0x08;
break;
case SDLK_UP:
pressmask = 0x10;
break;
case SDLK_DOWN:
pressmask = 0x20;
break;
case SDLK_LEFT:
pressmask = 0x40;
break;
case SDLK_RIGHT:
pressmask = 0x80;
break;
}
if (event->type == SDL_KEYDOWN) {
controllers->p1_status |= pressmask;
} else {
controllers->p1_status &= ~pressmask;
}
return 1;
}