/
serv_8042.c
412 lines (369 loc) · 10.3 KB
/
serv_8042.c
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* (C) Copyright 1992, ..., 2014 the "DOSEMU-Development-Team".
*
* for details see file COPYING in the DOSEMU distribution
*/
/*
* DANG_BEGIN_MODULE
*
* Description: 8042 Keyboard controller chip emulation for DOSEMU.
*
* Exports: keyb_8042_init(void), keyb_8042_reset(void)
*
* Maintainers: Scott Buchholz, Rainer Zimmermann
*
* REMARK
* This code provides truly rudimentary 8042 controller emulation.
* Not having any documentation on the 8042 makes it hard to improve. :)
*
* /REMARK
* DANG_END_MODULE
*
*/
#include "emu.h"
#include "iodev.h"
#include "int.h"
#include "port.h"
#include "memory.h"
#include "keyboard.h"
#include "keyb_server.h"
#include "keyb_clients.h"
#include "speaker.h"
#include "hma.h"
#define RESET_LINE_MASK 1
/* accurate emulation of special 8042 and keyboard commands - currently untested...
*/
#define KEYB_CMD 1
Bit8u port60_buffer = 0;
Boolean port60_ready = 0;
#if KEYB_CMD
/* variable indicating the command status of the keyboard/8042.
* if non-zero, e.g. a parameter byte to a command is expected.
*/
int wstate = 0;
int rstate = 0;
static int keyb_ctrl_scanmap = 1;
static int keyb_ctrl_typematic = 0x23;
static int keyb_ctrl_enable = 1;
static int keyb_ctrl_isdata = 0;
static Bit8u keyb_ctrl_command = 0x01;
static inline void keyb_ctrl_clearbuf(void)
{
/* this probably ought to do something :) */
}
/* write byte to the 8042's output buffer */
void output_byte_8042(Bit8u value) {
port60_buffer=value;
port60_ready=1;
if (keyb_ctrl_command & 0x01) { /* if interrupt enabled */
k_printf("8042: scheduling IRQ1\n");
pic_request(PIC_IRQ1);
}
else
k_printf("8042: interrupt flag OFF!\n");
}
static void ack(void) {
output_byte_8042(0xfa);
}
static void write_port60(Bit8u value)
{
switch (wstate) {
case 0x00:
switch (value) {
case 0xed: /* set mode indicators */
h_printf("8042: write port 0x60 set mode indicators\n");
ack();
wstate=0xed;
break;
case 0xee: /* port check */
h_printf("8042: write port 0x60 test mode 0xee\n");
output_byte_8042(0xee);
break;
case 0xf0: /* set keyb scan byte */
h_printf("8042: write port 0x60 set keyb scan type\n");
ack();
wstate=0xf0;
break;
case 0xf2: /* get keyb type */
h_printf("8042: write port 0x60 get keyb type\n");
ack();
rstate=0xf2;
break;
case 0xf3: /* set typematic speed */
h_printf("8042: write port 0x60 set typematic speed\n");
ack();
wstate=0xf3;
break;
case 0xf4: /* clear buffer */
h_printf("8042: write port 0x60 clear buffer\n");
keyb_ctrl_clearbuf();
keyb_ctrl_enable=1;
ack();
break;
case 0xf5: /* default, w/disable */
h_printf("8042: write port 0x60 set default, w/disable\n");
keyb_8042_reset();
keyb_ctrl_enable = 0;
ack();
break;
case 0xf6: /* set default */
h_printf("8042: write port 0x60 set default\n");
keyb_8042_reset();
ack();
break;
case 0xf7: /* set all keys to typematic */
case 0xf8: /* set all keys to make/break */
case 0xf9: /* set all keys to make */
case 0xfa: /* set all keys to typematic make/break */
h_printf("8042: write port 0x60 set mode (0x%02x)\n", value);
keyb_ctrl_clearbuf();
ack();
/* set mode ??? */
break;
case 0xfb: /* set single key to typematic & wait */
case 0xfc: /* set to make/break & wait */
case 0xfd: /* set to make & wait */
h_printf("8042: write port 0x60 set mod (0x%02x)\n", value);
keyb_ctrl_clearbuf();
ack();
wstate=value;
break;
case 0xfe: /* resend */
h_printf("8042: write port 0x60 resend\n");
output_byte_8042(port60_buffer);
break;
case 0xff: /* reset */
h_printf("8042: write port 0x60 reset\n");
ack();
rstate=0xff; /* wait for port 60h read */
break;
default:
h_printf("8042: write port 0x60 unsupported command 0x%02x =>Error\n", value);
output_byte_8042(0xfe);
break;
}
break;
case 0x60:
h_printf("8042: write 8042 command byte 0x%02x\n", value);
keyb_ctrl_command=value;
/* no ack() */
wstate=0;
break;
case 0xd1:
h_printf("8042: drive output port lines, value=0x%02x\n", value);
switch (value) {
case 0xdf: /* enable A20 */
h_printf("8042: enable A20 line\n");
set_a20(1);
break;
case 0xdd: /* disable A20) */
h_printf("8042: disable A20 line\n");
set_a20(0);
break;
}
port60_ready=0;
wstate=0;
break;
case 0xed: /* set LED mode */
{
t_modifiers modifiers = 0;
h_printf("8042: write port 0x60 set LED mode to 0x%02x\n", value);
/* TESTME this mapping is an educated guess */
if (value & 0x01) modifiers |= MODIFIER_SCR;
if (value & 0x02) modifiers |= MODIFIER_NUM;
if (value & 0x04) modifiers |= MODIFIER_CAPS;
keyb_client_set_leds(modifiers);
ack();
wstate=0;
break;
}
case 0xf0: /* get/set keyboard scan map */
h_printf("8042: write port 0x60 get/set keyboard scan map 0x%02x\n", value);
ack();
if (value == 0)
output_byte_8042(keyb_ctrl_scanmap);
else
keyb_ctrl_scanmap=value;
wstate=0;
break;
case 0xf3: /* set typematic rate */
h_printf("8042: write port 0x60 set typematic rate 0x%02x\n", value);
keyb_ctrl_typematic = value;
ack();
wstate=0;
break;
default:
h_printf("8042: write port 0x60 illegal state (0x%02x), resending\n",
wstate);
wstate=0;
write_port60(value);
}
}
/* write to port 64h (8042 command register) */
static void write_port64(Bit8u value) {
k_printf("8042: write port64h, =%02x\n",value);
switch(value) {
case 0x20: /* read 8042 command byte */
output_byte_8042(keyb_ctrl_command);
break;
case 0x60: /* write 8042 command byte */
wstate=0x60;
break;
#if 0 /* not sure if these are ok and/or needed. */
case 0xa4: /* passwort installed test */
output_byte_8042(0xfa); /* no password */
break;
case 0xa5: /* load password */
/* XXX ... we should read bytes from port60 until 0 is found */
break;
case 0xa9: /* aux interface test */
output_byte_8042(0x00); /* ok */
break;
case 0xaa: /* 8042 self test */
output_byte_8042(0x55); /* ok */
break;
case 0xab: /* keyboard interface test */
output_byte_8042(0x00); /* ok */
break;
case 0xc0: /* read 8042 input port */
output_byte_8042(0xff); /* just send _something_... */
break;
#endif
case 0xd1: /* next write to port 0x60 drives hardware port */
wstate=0xd1;
break;
case 0xf0 ... 0xff: /* produce 6ms pulse on hardware port */
wstate=0;
port60_ready=0;
if (!(value & RESET_LINE_MASK)) {
h_printf("8042: produce 6ms pulse on cpu reset line\n");
cpu_reset();
}
else
h_printf("8042: produce 6ms pulse on hardware port, ignored\n");
break;
default:
h_printf("8042: write port 0x64 unsupported command 0x%02x, ignored\n",
value);
/* various other commands... ignore */
break;
}
}
#endif
static Bit8u read_port60(void)
{
Bit8u r = port60_buffer;
port60_ready = 0;
h_printf("8042: read port 0x60 = 0x%02x\n", r);
#if KEYB_CMD
switch (rstate) {
case 0xf2: /* get keyboard type, MSB */
h_printf("8042: read port 0x60, getting keyboard type (MSB)\n");
output_byte_8042(0x83);
rstate = 0x72;
break;
case 0x72: /* get keyboard type, LSB */
h_printf("8042: read port 0x60, getting keyboard type (LSB)\n");
output_byte_8042(0xab);
rstate = 0;
break;
case 0xff: /* reset keyboard */
h_printf("8042: read port 0x60, resetting\n");
output_byte_8042(0xaa); /* BAT completion code */
rstate = 0;
break;
default: /* invalid state ?! */
rstate = 0;
break;
}
#endif
return r;
}
Bit8u keyb_io_read(ioport_t port)
{
Bit8u r = 0;
switch (port) {
case 0x60:
r = read_port60();
/* We ought to untrigger IRQ1, in case DOS was reading port60h with interrupts off,
* but currently the PIC code doesn't support this. */
#if 0
if (!port60_ready)
pic_untrigger(PIC_IRQ1);
#endif
k_printf("8042: read port 0x60 read=0x%02x\n",r);
break;
case 0x61:
/* Handle only PC-Speaker right now */
r = spkr_io_read(port);
break;
case 0x64:
r= 0x1c | (port60_ready ? 0x01 : 0x00);
k_printf("8042: read port 0x64 status check=0x%02x, port60_ready=%d\n",
r, port60_ready);
}
return r;
}
void keyb_io_write(ioport_t port, Bit8u value)
{
switch (port) {
case 0x60:
k_printf("8042: write port 0x60 outb = 0x%x\n", value);
#if KEYB_CMD
write_port60(value);
#endif
break;
case 0x61:
if (value & 0x80) {
k_printf("8042: IRQ ACK, %i\n", port60_ready);
port60_ready = 0;
int_check_queue(); /* reschedule irq1 if appropriate */
}
spkr_io_write(port, value);
break;
case 0x64:
k_printf("8042: write port 0x64 outb = 0x%x\n", value);
write_port64(value);
break;
}
}
void keyb_8042_init(void)
{
emu_iodev_t io_device;
/* 8042 keyboard controller */
io_device.read_portb = keyb_io_read;
io_device.write_portb = keyb_io_write;
io_device.read_portw = NULL;
io_device.write_portw = NULL;
io_device.read_portd = NULL;
io_device.write_portd = NULL;
io_device.handler_name = "8042 Keyboard data";
io_device.start_addr = 0x0060;
io_device.end_addr = 0x0060;
io_device.irq = 1;
io_device.fd = -1;
port_register_handler(io_device, 0);
io_device.handler_name = "8042 Keyboard command";
io_device.start_addr = 0x0064;
io_device.end_addr = 0x0064;
io_device.irq = EMU_NO_IRQ;
port_register_handler(io_device, 0);
io_device.handler_name = "Keyboard controller port B";
io_device.start_addr = 0x0061;
io_device.end_addr = 0x0061;
port_register_handler(io_device, 0);
}
void keyb_8042_reset(void)
{
#if KEYB_CMD
rstate = 0;
wstate = 0;
keyb_ctrl_scanmap = 1;
keyb_ctrl_typematic = 0x23;
keyb_ctrl_enable = 1;
keyb_ctrl_isdata = 0;
keyb_ctrl_clearbuf();
#endif
port60_buffer = 0;
port60_ready = 0;
}