summaryrefslogtreecommitdiffstats
path: root/data/sysrom/keyboard.c
blob: c41555a27034bf8fc0db9cf4fc153973f759b1b4 (plain)
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
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2010 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 *   Boston MA 02110-1301, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * keyboard.c
 *
 * ABC8000 keyboard driver
 */

#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <ioreg.h>
#include <sys/cpu.h>
#include "sysrom.h"
#include "keyboard.h"

static void keyboard_irq(void);

static struct key_stroke kbd_ringbuf[256];
static uint8_t kbd_head, kbd_tail;

static const uint8_t kbd_reset[] =
{
	0xff,			/* Reset keyboard */
};
static const uint8_t kbd_init[] =
{
	0xf0,			/* Set scan code set */
	0x03,			/* Scan code set 3 */
	0xfa,			/* Set all keys Make/Break/Repeat */
	0xf3,			/* Set repeat rate and delay */
	0x00,			/* 250 ms, 30 CPS */
};
static uint8_t kbd_set_leds[] =
{
	0xed,			/* Write LEDs */
	0,			/* LED value */
};

static const uint8_t *kbd_cmd_ptr;
static uint16_t kbd_cmd_len;

static uint8_t kbd_lock_keys;
static uint8_t kbd_lock_state;

static uint8_t kbd_shift_keys;
static uint8_t kbd_shift_state;

static bool kbd_need_leds;
static bool kbd_break_pfx;

static void keyboard_reset(void)
{
	kbd_lock_state = KBD_NUM_LOCK;
	kbd_lock_keys  = 0;

	kbd_shift_state = 0;

	kbd_need_leds = false;	/* Not yet */
	kbd_break_pfx = false;

	kbd_cmd_ptr = kbd_reset;
	kbd_cmd_len = sizeof kbd_reset;

	writeb(*kbd_cmd_ptr, IO_KBD_DATA);
}

void keyboard_init(void)
{
	kbd_head = 0;
	kbd_tail = 255;

	__ivt[0x40] = keyboard_irq;

	/* Enable the keyboard receive interrupt */
	*IO_MFP_IERB |= 0x01;
	*IO_MFP_IMRB |= 0x01;

	/* Send reset to keyboard */
	keyboard_reset();
}

static void __attribute__((interrupt)) keyboard_irq(void)
{
	uint8_t scan;
	uint8_t data;
	uint8_t bucky;
	uint8_t sym;
	uint8_t type;

	while (readb(IO_KBD_STATUS) & 1) {
		scan = readb(IO_KBD_DATA);

		if (kbd_cmd_len) {
			switch (scan) {
			case 0xfa:
				kbd_cmd_ptr++;
				if (--kbd_cmd_len) {
					writeb(*kbd_cmd_ptr, IO_KBD_DATA);
				} else if (kbd_need_leds) {
					kbd_set_leds[1] = kbd_lock_state;
					kbd_cmd_ptr = kbd_set_leds;
					kbd_cmd_len = sizeof kbd_set_leds;
					writeb(*kbd_cmd_ptr, IO_KBD_DATA);
				}
				break;

			case 0xaa:
				keyboard_reset();
				break;

			case 0xfe:
				writeb(*kbd_cmd_ptr, IO_KBD_DATA);
				break;

			default:
				break; /* Ignore stray codes */
			}
		} else {
			type = key_type[scan];

			switch (type & 0x0f) {
			case KEY_IGNORE:
			default:
				break;

			case KEY_RESET:
				keyboard_reset();
				break;

			case KEY_BREAK:
				kbd_break_pfx = true;
				break;

			case KEY_SHIFT:
				data = key_data[scan][0];
				if (kbd_break_pfx) {
					kbd_shift_keys &= ~data;
				} else {
					kbd_shift_keys |= data;
				}
				kbd_shift_state =
					kbd_shift_keys | (kbd_shift_keys >> 4);
				break;

			case KEY_LOCK:
				data = key_data[scan][0];
				if (kbd_break_pfx) {
					kbd_lock_keys &= ~data;
				} else if (!(kbd_lock_keys & data)) {
					kbd_lock_keys  |= data;
					kbd_lock_state ^= data;
					kbd_set_leds[1] = kbd_lock_state;
					kbd_cmd_ptr = kbd_set_leds;
					kbd_cmd_len = sizeof kbd_set_leds;
					writeb(*kbd_cmd_ptr, IO_KBD_DATA);
				}
				break;

			case KEY_SYMBOL:
				bucky = kbd_shift_state & 7;
				if (kbd_lock_state & KBD_CAPS_LOCK) {
					uint8_t mask = (bucky & KBD_ALT_L) ? KEY_ALTALPHA : KEY_ALPHA;
					if (type & mask)
						bucky ^= KBD_SHIFT_L;
				}
				sym = key_data[scan][bucky];
				if (kbd_head != kbd_tail) {
					kbd_ringbuf[kbd_head].sym   = sym;
					kbd_ringbuf[kbd_head].scan  = scan;
					kbd_ringbuf[kbd_head].shift = kbd_shift_keys;
					kbd_ringbuf[kbd_head].lock  = kbd_lock_state;
					kbd_head = (kbd_head + 1) % ELEMENTS(kbd_ringbuf);
				}
				break;
			}
		}
	}
}