~foura/uxn900

ref: 88b17af754568c3639f641175894501d04fdae74 uxn900/src/dev/controller.c -rw-r--r-- 3.5 KiB
88b17af7james palmer controller: add wip controller device. 4 months ago
                                                                                
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "uxn.h"
#include "dat.h"
#include "fns.h"

#include "dev/console.h"
#include "dev/controller.h"

enum {
	Ckp = 0x4a,

	Mshift = (1 << 2),
	Mfn    = (1 << 1),
	Mctrl  = (1 << 0),

	Rctrl = 0xd2,
		Rctrlon       = (1 << 6),
		Rctrlsoftmode = (1 << 1),
		Rctrlsoftnrst = (1 << 0),
	Rcode = 0xdb,
	Risr  = 0xe3,
	Rimr  = 0xe4,
	Redr  = 0xe8,
	Rsih  = 0xe9,
};

void
controller_init(Controller *ctrl)
{
	int i;
	Uint8 buf[2];

	/* read in ctrl register */
	buf[0] = Rctrl;
	buf[1] = 0;
	i2c_read(PHYSI2C1, Ckp, buf, 1, 1);

	/* turn on the keyboard */
	buf[0] = Rctrl;
	buf[1] |= Rctrlon;
	buf[1] |= Rctrlsoftmode;
	buf[1] |= Rctrlsoftnrst;
	i2c_write(PHYSI2C1, Ckp, buf, 1, 1);

	/* enable key event status */
	buf[0] = Rimr;
	buf[1] = 0xfe;
	i2c_write(PHYSI2C1, Ckp, buf, 1, 1);

	/* enable rising and falling interrupts (qemu workaround) */
	buf[0] = Redr;
	buf[1] = 0x57;
	i2c_write(PHYSI2C1, Ckp, buf, 1, 1);

	/* enable isr clear on read */
	buf[0] = Rsih;
	buf[1] = 0x05;
	i2c_write(PHYSI2C1, Ckp, buf, 1, 1);

	/* clear state */
	ctrl->raw.curmods = 0;
	ctrl->raw.prevmods = 0;
	for(i = 0; i < 8; i++) {
		ctrl->raw.cur[i] = 0;
		ctrl->raw.prev[i] = 0;
	}
}

static void
controller_button(Controller *ctrl, Uxn *u, Uint8 code)
{
	if(ctrl->raw.curmods & 2)
		return;

	switch(code) {
	case 0x12: ctrl->buttons |= 0x10; break; /* up */
	case 0x21: ctrl->buttons |= 0x20; break; /* down */
	case 0x1f: ctrl->buttons |= 0x40; break; /* left */
	case 0x22: ctrl->buttons |= 0x80; break; /* right */
	}
}

static void
controller_process_buttons(Controller *ctrl, Uxn *u)
{
	int col, row;
	Uint8 c;

	/* scan the keyboard matrix */
	ctrl->buttons = 0;
	for(col = 0; col < 8; col++) {
		c  = ctrl->raw.cur[col];

		for(row = 0; row < 8; row++) {
			if(c & 1)	
				controller_button(ctrl, u, col * 8 + row);

			c = c >> 1;
		}
	}

	uxn_eval(u, ctrl->vector);
}

static void
controller_key(Controller *ctrl, Uxn *u, Uint8 code)
{
}

static void
controller_process_keys(Controller *ctrl, Uxn *u)
{
	int col, row;
	Uint8 c;

	/* scan the keyboard matrix */
	for(col = 0; col < 8; col++) {
		/* newly pressed keys only */
		c  = ctrl->raw.cur[col];
		c ^= ctrl->raw.prev[col];
		c &= ctrl->raw.cur[col];

		for(row = 0; row < 8; row++) {
			if(c & 1)
				controller_key(ctrl, u, col * 8 + row);

			c = c >> 1;
		}
	}
}

void
controller_poll(Controller *ctrl, Uxn *u)
{
	Uint8 buf[2];
	int i, j;

	/* twl4030 remembers the last 2 events */
	for(i = 0; i < 2; i++) {
		/* check status register */
		buf[0] = Risr + 2*i;
		i2c_read(PHYSI2C1, Ckp, buf, 1, 1);
		if(!(buf[1] & 1))
			continue;

		/* read the key state */
		buf[0] = Rcode;
		for(j = 0; j < 8; j++) {
			i2c_read(PHYSI2C1, Ckp, buf, 1, 1);

			/* extract modifiers */
			if(j == 5) {
				ctrl->raw.prevmods = ctrl->raw.curmods;
				ctrl->raw.curmods  = buf[1] >> 4;
				buf[1] &= 0x0f;
			}

			ctrl->raw.prev[j] = ctrl->raw.cur[j];
			ctrl->raw.cur[j] = buf[1];

			buf[0]++;
		}

		controller_process_buttons(ctrl, u);
		controller_process_keys(ctrl, u);
	}
}

Uint8
controller_dei(Device *d, Uint8 port)
{
	switch(port) {
	case 0x0: return uxn_controller.vector >> 8;
	case 0x1: return uxn_controller.vector;
	case 0x2: return uxn_controller.buttons;
	case 0x3: return uxn_controller.key;
	}

	return 0;
}

void
controller_deo(Device *d, Uint8 port)
{
	Uint16 tmp;

	switch(port) {
	case 0x0:
		tmp = d->dat[port] << 8;
		tmp |= uxn_controller.vector & 0x00ff;

		uxn_controller.vector = tmp;
		break;
	case 0x1:
		tmp = d->dat[port];
		tmp |= uxn_controller.vector & 0xff00;

		uxn_controller.vector = tmp;
	}
}